Skip to content

StefanJohnsen/Triangulate3DPolygon

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Triangulate 3D polygon

Triangulate3DPolygon is a header-only file offering a template-based solution for triangulating three-dimensional, non-complex polygons. This solution is self-contained, requiring no external libraries except for the inclusion of <vector>.

Compatibility and Dependencies

  • C++ 11 Standard and above
  • Standard Template Library (STL)

Supported polygons

The solution works for all kinds of 3D non-complex polygons, concave or convex, open or closed. The routine does not produce new vertices to triangulate the polygon.

OS Support

  • Windows
  • Linux
  • macOS

Solution

The solution uses two techniques for triangulation:

Convex Polygons: Fan triangulation method is utilized for convex polygons, efficiently dividing the polygon into triangles originating from a central point.

Concave Polygons: Earcut triangulation algorithm is used for concave polygons. This method identifies and cuts triangles from the inward-curving parts of the polygon, resulting in a triangulated mesh.

Usage

Copy TriangulatePolygon.h to your project and include the file.

#include <iostream>
#include "TriangulatePolygon.h"

int main()
{
    std::vector<triangulate::Point> polygon;

    polygon.emplace_back(0.0f, 0.0f, 0.0f);
    polygon.emplace_back(2.0f, 0.0f, 0.0f);
    polygon.emplace_back(2.0f, 2.0f, 0.0f);
    polygon.emplace_back(0.0f, 2.0f, 0.0f);
    
    const auto triangles = triangulate::triangulate(polygon);

    std::cout << "Triangles " << triangles.size() << std::endl;
}

References

The following sources have been utilized in the development of this solution.

Paul Bourke: The shortest line between two lines in 3D

OpenGL Wiki: Calculating a Surface Normal - Newell's Method

Wikipedia: Fan triangulation

Wikipedia: Two ears theorem

License

This software is released under the MIT License terms.

About

Triangulate planar non-complex polygons in 3D space

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages