Click or drag to resize
Ab4d.SharpEngine 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
  Ab4d.SharpEngine.UtilitiesTriangulator

Namespace: Ab4d.SharpEngine.Utilities
Assembly: Ab4d.SharpEngine (in Ab4d.SharpEngine.dll) Version: 2.0.8956+4c7684e186ca1be74e7a284fbe739d9a1b843d3c
Syntax
C#
public class Triangulator

The Triangulator type exposes the following members.

Constructors
 NameDescription
Public methodTriangulator(Vector2) Constructor that takes positions that define the outer polygon. To add a hole call AddHole(Vector2) method. To add multiple outer polygons and holes, use constructor that takes an array of list of points.
Public methodTriangulator(IListVector2, 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(Vector2, Boolean) Constructor that takes positions that define the outer polygon. To add a hole call AddHole(Vector2) method. To add multiple outer polygons and holes, use constructor that takes an array of list of points.
Public methodTriangulator(Vector2, Int32, 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 float 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 array of positions as a hole in the original polygon. The orientation of positions (clockwise or anti-clockwise) in the hole must be different from the orientation of positions in the outer polygon. If the orientations are the same, then this method reverses the order of positions in the hole (in this case false is returned). To get triangulated positions and triangle indices call Triangulate(ListVector2, 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(Vector3) Returns an array of 2D points that are created by projecting the 3D positions onto the xy, xz or yz plane (removing one of the coordinates). The plane is determined by checking the normal of the first triangle and determining which component of the normal vector is the biggest (this coordinate is removed). This requires that the positions lie on the same plane. Input positions must not be null or have less than 3 elements.
Public methodStatic memberProject3DPositionTo2D(Vector3, Int32) Returns an array of 2D points that are created by projecting the 3D positions onto the xy, xz or yz plane (removing one of the coordinates). The plane is determined by checking the normal of the first triangle and determining which component of the normal vector is the biggest (this coordinate is removed). This requires that the positions lie on the same plane. Input positions and polygonIndices must not be null or have less than 3 elements.
Public methodStatic memberTriangulate(Vector2) Triangulate method triangulates the specified points and creates a list of triangle indices that define the polygon.
Public methodTriangulate(ListVector2, ListInt32) Triangulate method triangulates the polygons that were specified in the constructor and holes that were added by the AddHole(Vector2) method. It sets the triangulated positions (the outer polygons and holes are connected into a single polygon) and triangle indices that can be used to create a 3D mesh.
Public methodStatic memberTriangulate(Vector2, ListVector2, ListInt32) Triangulate method triangulates the polygons with holes and returns positions and triangle indices that can be used to create a 3D mesh. Shape with holes is represented by multiple arrays of positions that are defined by the polygons property. 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. The orientation of the outer polygons is defined by the orientation of the first polygon. The method returns a list of triangulated positions (the outer polygons and holes are connected into a single polygon) and a list of triangle indices that 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 that are defined by triangle indices - three consecutive indexes define the three position that define a triangle. The triangles can be than used to create 3D meshes.

The positions that define the 2D 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 if 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(Vector2) methods and finally call Triangulate(ListVector2, 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(ListVector2, 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(Vector2) or Triangulate(Vector2, ListVector2, ListInt32) methods.

Triangulator uses ear clipping algorithm that is based on algorithm 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 be 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(Vector3) or Project3DPositionTo2D(Vector3, Int32) to convert 3D polygon into 2D polygon.

See Also