Overview
The triangulate operation decomposes complex polygons (including those with holes) into a set of triangles. This is essential for rendering, collision detection, and finite element analysis. OpenGeometry uses the robust ear-clipping (earcut) algorithm with automatic projection to handle 3D polygons.Function Signature
The outer boundary vertices of the polygon in order (either clockwise or counter-clockwise). Must have at least 3 vertices to form a valid polygon.
A vector of hole polygons, each defined by its own ordered vertex list. Holes must be entirely contained within the outer boundary. Pass an empty vector if there are no holes.
Return Type
ReturnsVec<[usize; 3]> - a vector of triangle indices, where each [usize; 3] array contains three indices into the combined vertex list (outer boundary vertices followed by all hole vertices).
Index Mapping
- Indices
0..face_vertices.len()refer to outer boundary vertices - Indices
face_vertices.len()..refer to hole vertices in order
- Outer boundary: 5 vertices (indices 0-4)
- Hole 1: 4 vertices (indices 5-8)
- Hole 2: 3 vertices (indices 9-11)
How It Works
- Normal Calculation: Computes the average normal of the polygon using Newell’s method
- Projection Selection: Chooses the best 2D projection plane based on the dominant axis:
- If |Z| is dominant: Project to XY plane
- If |X| is dominant: Project to YZ plane
- Otherwise: Project to XZ plane
- Vertex Flattening: Converts all 3D vertices to 2D coordinates in the chosen plane
- Hole Index Tracking: Records the starting index of each hole in the flattened array
- Earcut Algorithm: Runs the robust ear-clipping triangulation
- Winding Order Correction: Automatically reverses triangles if needed for consistent orientation
Code Examples
Simple Triangle
Quadrilateral Triangulation
Polygon with a Hole
Complex Polygon with Multiple Holes
3D Non-Planar Polygon (Auto-Projected)
Visual Examples
Winding Order
The function automatically corrects winding order:- Analyzes the first triangle’s orientation
- Reverses all triangles if winding is inconsistent
- Ensures consistent face normals for rendering
Implementation Details
Source Location
~/workspace/source/main/opengeometry/src/operations/triangulate.rs:3
Algorithm
Uses the earcutr library, which implements:- Fast ear-clipping triangulation
- Robust handling of self-touching vertices
- Support for complex polygons with holes
Projection Axis Selection
Normal Calculation (Newell’s Method)
Edge Cases
- Insufficient Vertices: Returns empty vector if outer boundary has < 3 vertices
- Degenerate Polygons: May produce no triangles or unexpected results
- Self-Intersecting: Earcut algorithm handles some cases but behavior is undefined
- Holes Outside Boundary: Results are undefined; ensure holes are contained
- Non-Planar Polygons: Automatically projected, but significant non-planarity may cause artifacts
Performance Considerations
- Time Complexity: O(n log n) average case for earcut algorithm
- Memory: O(n) for vertex storage and triangle output
- Efficient for polygons with hundreds of vertices
- Minimal overhead from projection
Common Use Cases
- Rendering: Convert arbitrary polygons to GPU-compatible triangle meshes
- Physics: Generate collision meshes from polygon boundaries
- CAD/CAM: Prepare geometry for manufacturing or simulation
- GIS: Triangulate geographic regions with exclusion zones
- Architecture: Process floor plans with room boundaries and obstacles
See Also
Extrude
Often used after triangulation to create 3D geometry
Sweep
Creates surfaces that may need triangulation

