Click or drag to resize
AB4D logo

Triangulator Class

Triangulator can be used to convert one or more polygons that are defined by a list of 2D points into triangles (triangle indices).
Inheritance Hierarchy
SystemObject
  Ab3d.UtilitiesTriangulator

Namespace: Ab3d.Utilities
Assembly: Ab3d.PowerToys (in Ab3d.PowerToys.dll) Version: 11.1.8864.1045
Syntax
C#
public class Triangulator

The Triangulator type exposes the following members.

Constructors
 NameDescription
Public methodTriangulator(IListPoint) Constructor that takes positions that define the outer polygon. To add a hole call AddHole(PointCollection) method. To add multiple outer polygons and holes, use constructor that takes an array of list of points.
Public methodTriangulator(IListPoint, Boolean) Constructor that takes positions that define the outer polygon. To add a hole call AddHole(PointCollection) method. To add multiple outer polygons and holes, use constructor that takes an array of list of points.
Public methodTriangulator(IListPoint, Boolean) Constructor with multiple polygons (polygons are automatically classified as holes and outer polygons). The holes are identified by the orientation of positions (clockwise or anti-clockwise) that is different from the orientation of positions in the outer polygons.
Public methodTriangulator(IListPoint, IListInt32, Boolean) Constructor that takes positions and triangle indices that define the outer polygon. When Triangulator is created with triangle indices, then it is not possible to add holes.
Top
Properties
 NameDescription
Public propertyIsClockwise Gets a boolean that specifies if the specified points are oriented in clockwise direction. This property can be used only then Triangulator is created by providing a single list of point and not multiple polygons.
Public propertyIsClosed Gets a boolean that specifies if the specified points define a close path. This property can be used only then Triangulator is created by providing a single list of point and not multiple polygons.
Public propertyIsPolygonConvex Gets a boolean that specifies if the specified points define a convex polygon. In convex polygon every internal angle is less than or equal to 180 degrees and every line segment between two vertices remains inside or on the boundary of the polygon. For example square is convex, but star is not (it is concave). This property can be used only then Triangulator is created by providing a single list of point and not multiple polygons.
Public propertyIsPolygonPositionsOrderReversed Gets true when the positions order was reversed because the outer polygon was not defined in clockwise order.
Public propertyIsYAxisUp Gets Boolean that specifies if Y axis in 2D positions is pointing up (true by default). This affects the value of IsClockwise property. The value of this property is set with constructor.
Public propertyPolygonArea Gets a double that specifies the area defined by the specified points. This property can be used only then Triangulator is created by providing a single list of point and not multiple polygons.
Top
Methods
 NameDescription
Public methodAddHole AddHole method can be used when Triangulator was created with a single polygon. It adds the specified PointCollection as a hole in the original polygon. The orientation of positions (clockwise or anti-clockwise) in the hole must be different than the orientation of positions in the outer polygon. If the orientations are the same, then this methods reverses the order of positions in the hole (in this case false is returned). To get triangulated positions and triangle indices call Triangulate(PointCollection, ListInt32) method.
Public methodCreateTriangleIndices CreateTriangleIndices method triangulates the specified points and creates a list of triangle indices that define the polygon.
Public methodGetPositions Returns the positions that are used by this polygon.
Public methodStatic memberProject3DPositionTo2D(IListPoint3D) Returns a List of 2D points that are created with projecting the 3D positions onto the xy, xz or yz plane (removing one of the coordinates). The plane is determined with checking the normal of the first triangle and determining which component of the normal vector is the biggest (this coordinate is removing). This requires that the positions lie on the same plane. Input positions must not be null or have less that 3 elements.
Public methodStatic memberProject3DPositionTo2D(IListPoint3D, IListInt32) Returns a List of 2D points that are created with projecting the 3D positions onto the xy, xz or yz plane (removing one of the coordinates). The plane is determined with checking the normal of the first triangle and determining which component of the normal vector is the biggest (this coordinate is removing). This requires that the positions lie on the same plane. Input positions and polygonIndices must not be null or have less that 3 elements.
Public methodStatic memberTriangulate(IListPoint) Triangulate method triangulates the specified points and creates a list of triangle indices that define the polygon.
Public methodTriangulate(PointCollection, ListInt32) Triangulate method triangulates the polygons and returns positions and triangle indices that can be used to create a 3D mesh.
Public methodStatic memberTriangulate(IListPoint, PointCollection, ListInt32) Triangulate method triangulates the polygons and returns positions and triangle indices that can be used to create a 3D mesh.
Top
Remarks

Triangulator uses a process that is called triangulation. Triangulation gets a list of 2D points and creates triangles in such a way that they define a polygon. The triangles can be than used to create 3D objects.

The positions that define the polygons can be defined in Y-up or Y-down 2D coordinate system. By default Triangulator expects the Y-up coordinate system. If polygons are defined in Y-down coordinate system, then specify that in the constructor or set IsYAxisUp to false. This value is important to determine is the polygon is oriented in clockwise or in anti-clockwise direction.

To triangulate a single polygon without holes create Triangulator object with the list of positions and then call CreateTriangleIndices method.

To triangulate a single outer polygon with holes, create Triangulator object with the list of positions of the outer polygon, then call AddHole(PointCollection) methods and finally call Triangulate(PointCollection, ListInt32) method to get positions and triangle indices.

To triangulate a multiple polygons with holes, create Triangulator object with specifying an array of polygons (each is defined by a list of positions). Triangulator will analyze each polygon and define it as an outer shape or hole. The holes are identified by the orientation of positions (clockwise or anti-clockwise) that is different from the orientation of positions in the outer polygons. To get triangulated positions and triangle indices call Triangulate(PointCollection, ListInt32) method. If outer polygons are not defined in clockwise order then Triangulator reverses the order of position. In this case the IsPolygonPositionsOrderReversed is set to true.

It is also possible to use static Triangulate(IListPoint) or Triangulate(IListPoint, PointCollection, ListInt32) methods.

Triangulator uses ear clipping algorithm that is based on algorithm defined defined by David Eberly - http://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf). In simple cases (when the polygon is convex) also a simple triangle fan algorithm can used. The Triangulator supports polygons with holes (but only one level of outer polygons and holes and does not support inner holes or hierarchy of polygons with holes).

When creating an instance of Triangulator you need to specify the list of points. After that it is possible to get some of the properties of the polygon defined by the points: IsClockwise, IsClosed, IsPolygonConvex and PolygonArea. For example the IsClockwise property is useful when creating extruded objects from polygons - if the IsClockwise is false, the normals for the extruded object need to be flipped.

To triangulate a 3D polygon where positions lie on the same plane, use the static Project3DPositionTo2D(IListPoint3D) or Project3DPositionTo2D(IListPoint3D, IListInt32) to convert 3D polygon into 2D polygon.

See Also