Home > matGeom > polygons2d > triangulatePolygon.m

triangulatePolygon

PURPOSE ^

Computes a triangulation of the input polygon.

SYNOPSIS ^

function tri = triangulatePolygon(poly)

DESCRIPTION ^

 Computes a triangulation of the input polygon.

   TRI = triangulatePolygon(POLY)
   Computes a triangulation TRI of the polygon defined by POLY
   POLY contains the polygon vertices, as a Nv-by-2 array of double. 
   TRI is a Nt-by-3 array containing indices of vertices forming the
   triangles. 

   Example
     % creates a simple polygon and computes its Delaunay triangulation
     poly = [0 0 ; 10 0;5 10;15 15;5 20;-5 10];
     figure;drawPolygon(poly); axis equal
     tri = triangulatePolygon(poly);
     figure;
     % patch('Faces', tri, 'Vertices', poly, 'facecolor', 'c');
     drawMesh(poly, tri, 'facecolor', 'c');
     axis equal

     % Another example for which constrains were necessary
     poly2 = [10 10;80 10; 140 20;30 20; 80 30; 140 30; 120 40;10 40];
     tri2 = triangulatePolygon(poly2);
     figure; drawMesh(poly2, tri2);
     hold on, drawPolygon(poly2, 'linewidth', 2);
     axis equal
     axis([0 150 0 50])

   See also
     delaunayTriangulation, drawMesh, patch

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function tri = triangulatePolygon(poly)
0002 % Computes a triangulation of the input polygon.
0003 %
0004 %   TRI = triangulatePolygon(POLY)
0005 %   Computes a triangulation TRI of the polygon defined by POLY
0006 %   POLY contains the polygon vertices, as a Nv-by-2 array of double.
0007 %   TRI is a Nt-by-3 array containing indices of vertices forming the
0008 %   triangles.
0009 %
0010 %   Example
0011 %     % creates a simple polygon and computes its Delaunay triangulation
0012 %     poly = [0 0 ; 10 0;5 10;15 15;5 20;-5 10];
0013 %     figure;drawPolygon(poly); axis equal
0014 %     tri = triangulatePolygon(poly);
0015 %     figure;
0016 %     % patch('Faces', tri, 'Vertices', poly, 'facecolor', 'c');
0017 %     drawMesh(poly, tri, 'facecolor', 'c');
0018 %     axis equal
0019 %
0020 %     % Another example for which constrains were necessary
0021 %     poly2 = [10 10;80 10; 140 20;30 20; 80 30; 140 30; 120 40;10 40];
0022 %     tri2 = triangulatePolygon(poly2);
0023 %     figure; drawMesh(poly2, tri2);
0024 %     hold on, drawPolygon(poly2, 'linewidth', 2);
0025 %     axis equal
0026 %     axis([0 150 0 50])
0027 %
0028 %   See also
0029 %     delaunayTriangulation, drawMesh, patch
0030 %
0031 
0032 % ------
0033 % Author: David Legland
0034 % e-mail: david.legland@inrae.fr
0035 % Created: 2011-11-25,    using Matlab 7.9.0.529 (R2009b)
0036 % Copyright 2011 INRA - Cepia Software Platform.
0037 
0038 % compute constraints
0039 nv = size(poly, 1);
0040 cons = [(1:nv)' [2:nv 1]'];
0041 
0042 if verLessThan('matlab', '8.1')
0043     % Code for versions before R2013a
0044     
0045     % delaunay triangulation
0046     dt = DelaunayTri(poly(:,1), poly(:, 2), cons); %#ok<DDELTRI>
0047     
0048     % find which triangles are contained in polygon
0049     centers = incenters(dt);
0050     inds = isPointInPolygon(centers, poly);
0051     
0052     % keep selected triangles
0053     tri = dt.Triangulation(inds, :);
0054 
0055 else
0056     % Code for versions R2013a and later
0057 
0058     % delaunay triangulation
0059     % dt = DelaunayTri(poly(:,1), poly(:, 2), cons);
0060     dt = delaunayTriangulation(poly(:,1), poly(:, 2), cons);
0061 
0062     % find which triangles are contained in polygon
0063     % centers = incenters(dt);
0064     centers = incenter(dt);
0065     inds = isPointInPolygon(centers, poly);
0066 
0067     % keep selected triangles
0068     % tri = dt.Triangulation(inds, :);
0069     tri = dt.ConnectivityList(inds, :);
0070 end

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019