Click or drag to resize
AB4D logo

Triangulator Class

Triangulator can be used to convert a polygon that is defined by a list of 2D points into triangles that can be than used to create 3D object.
Inheritance Hierarchy
SystemObject
  Ab3d.UtilitiesTriangulator

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

The Triangulator type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyIsClockwise
Gets a boolean that specifies if the specified points are oriented in clockwise direction.
Public propertyIsClosed
Gets a boolean that specifies if the specified points define a close path.
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).
Public propertyPolygonArea
Gets a double that specifies the area defined by the specified points.
Top
Methods
  NameDescription
Public methodCreateTriangleIndices
CreateTriangleIndices method triangulates the specified points and creates a list of triangle indices that define the 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
Triangulate method triangulates the specified points and creates a list of triangle indices that define the polygon.
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.

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. Then you can use this triangulator.

The algorithm that is used in Triangulator first analyzes the polygon defined by the specified points. If the polygon is convex, than a simple triangle fan algorithm is used. Otherwise the ear clipping algorithm is used (based on algorithm defined defined by David Eberly - http://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf). The current implementation of Triangulator does not support polygons with holes.

Triangulator can be used in two ways:
- with its static Triangulate(IListPoint) method, - with creating an instance of Triangulator class and than calling its CreateTriangleIndices method.

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.

Both Triangulate(IListPoint) and CreateTriangleIndices methods return a List of integer values that can be used as TriangleIndices in the mesh (points can be used as Positions in the mesh).

IMPORTANT:
When the points define an invalid polygon (for example when the polygon lines intersect each other) both Triangulate and CreateTriangleIndices can return a FormatException. Therefore if you do not control the way the points are defined (for example when user can define them in any way), than you need to catch the FormatException.

See Also