From 68c6f6c9f881add15b6f3bae04c2eb719df6078c Mon Sep 17 00:00:00 2001 From: josh Date: Wed, 10 Jul 2024 14:19:16 -0400 Subject: [PATCH] Implement Polygon::ToPolyhedron --- include/J3ML/Geometry/Polygon.h | 8 ++++++++ src/J3ML/Geometry/Polygon.cpp | 17 +++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/include/J3ML/Geometry/Polygon.h b/include/J3ML/Geometry/Polygon.h index 94342cf..ff4ffed 100644 --- a/include/J3ML/Geometry/Polygon.h +++ b/include/J3ML/Geometry/Polygon.h @@ -80,6 +80,14 @@ namespace J3ML::Geometry { Vector3 ClosestPoint(const LineSegment &lineSegment, Vector3 *lineSegmentPt) const; Vector3 ClosestPoint(const Vector3 &point) const; + + /// Converts this Polygon to a Polyhedron representation. + /** This function will create a Polyhedron with two faces, one for the front face of this Polygon, + and one for the back face. + @todo Add ToPolyhedron(float polygonThickness) + @see Triangulate(), MinimalEnclosingAABB(). */ + Polyhedron ToPolyhedron() const; + protected: diff --git a/src/J3ML/Geometry/Polygon.cpp b/src/J3ML/Geometry/Polygon.cpp index 96fa400..62017c0 100644 --- a/src/J3ML/Geometry/Polygon.cpp +++ b/src/J3ML/Geometry/Polygon.cpp @@ -1,9 +1,9 @@ #include -#include +#include #include #include "J3ML/Geometry/Plane.h" #include "J3ML/Geometry/Line.h" -#include +#include namespace J3ML::Geometry { Vector3 Polygon::AnyPointFast() const { return !vertices.empty() ? vertices[0] : Vector3::NaN; } @@ -572,6 +572,19 @@ namespace J3ML::Geometry { return closestPt; } + Polyhedron Polygon::ToPolyhedron() const { + Polyhedron poly; + poly.v = vertices; + poly.f.push_back(Polyhedron::Face()); + poly.f.push_back(Polyhedron::Face()); + for(int i = 0; i < NumVertices(); ++i) + { + poly.f[0].v.push_back(i); + poly.f[1].v.push_back(NumVertices()-1-i); + } + return poly; + } + Vector3 Polygon::ClosestPoint(const LineSegment &lineSegment) const { return ClosestPoint(lineSegment, 0);