Plasma Engine  2.0
Loading...
Searching...
No Matches
ConvexHull.h
1#pragma once
2
3#include <Core/CoreDLL.h>
4#include <Foundation/Containers/Deque.h>
5#include <Foundation/Containers/DynamicArray.h>
6#include <Foundation/Math/Vec3.h>
7
15class PL_CORE_DLL plConvexHullGenerator
16{
17public:
18 struct Face
19 {
20 PL_DECLARE_POD_TYPE();
21
22 plUInt16 m_uiVertexIdx[3];
23 };
24
27
31 void SetSimplificationMinTriangleAngle(plAngle angle) { m_MinTriangleAngle = angle; }
32
36 void SetSimplificationFlatVertexNormalThreshold(plAngle angle) { m_FlatVertexNormalThreshold = angle; }
37
43 void SetSimplificationMinTriangleEdgeLength(double fLen) { m_fMinTriangleEdgeLength = fLen; }
44
46 plResult Build(const plArrayPtr<const plVec3> vertices);
47
49 void Retrieve(plDynamicArray<plVec3>& out_vertices, plDynamicArray<Face>& out_faces);
50
52 void RetrieveVertices(plDynamicArray<plVec3>& out_vertices);
53
54private:
55 plResult ComputeCenterAndScale(const plArrayPtr<const plVec3> vertices);
56 plResult StoreNormalizedVertices(const plArrayPtr<const plVec3> vertices);
57 void StoreTriangle(plUInt16 i, plUInt16 j, plUInt16 k);
58 plResult InitializeHull();
59 plResult ComputeHull();
60 bool IsInside(plUInt32 vtxId) const;
61 void RemoveVisibleFaces(plUInt32 vtxId);
62 void PatchHole(plUInt32 vtxId);
63 bool PruneFlatVertices(double fNormalThreshold);
64 bool PruneDegenerateTriangles(double fMaxCosAngle);
65 bool PruneSmallTriangles(double fMaxEdgeLen);
66 plResult ProcessVertices(const plArrayPtr<const plVec3> vertices);
67
68 struct TwoSet
69 {
70 PL_ALWAYS_INLINE TwoSet()
71 {
72 a = 0xFFFF;
73 b = 0xFFFF;
74 }
75 PL_ALWAYS_INLINE void Add(plUInt16 x) { (a == 0xFFFF ? a : b) = x; }
76 PL_ALWAYS_INLINE bool Contains(plUInt16 x) { return a == x || b == x; }
77 PL_ALWAYS_INLINE void Remove(plUInt16 x) { (a == x ? a : b) = 0xFFFF; }
78 PL_ALWAYS_INLINE int GetSize() { return (a != 0xFFFF) + (b != 0xFFFF); }
79
80 plUInt16 a, b;
81 };
82
83 struct Triangle
84 {
85 plVec3d m_vNormal;
86 double m_fPlaneDistance;
87 plUInt16 m_uiVertexIdx[3];
88 bool m_bFlip;
89 bool m_bIsDegenerate;
90 };
91
92 // used for mesh simplification
93 plAngle m_MinTriangleAngle = plAngle::MakeFromDegree(22.0f);
94 plAngle m_FlatVertexNormalThreshold = plAngle::MakeFromDegree(5);
95 double m_fMinTriangleEdgeLength = 0.05;
96
97 plVec3d m_vCenter;
98 double m_fScale;
99
100 plVec3d m_vInside;
101
102 // all the 'good' vertices (no duplicates)
103 // normalized to be within a unit-cube
104 plDynamicArray<plVec3d> m_Vertices;
105
106 // Will be resized to Square(m_Vertices.GetCount())
107 // Index [i * m_Vertices.GetCount() + j] indicates which (up to two) other points
108 // combine with the edge i and j to make a triangle in the hull. Only defined when i < j.
110
111 plDeque<Triangle> m_Triangles;
112};
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
static constexpr plAngle MakeFromDegree(float fDegree)
Creates an instance of plAngle that was initialized from degree. (Performs a conversion)
Definition Angle_inl.h:33
This class encapsulates an array and it's size. It is recommended to use this class instead of plain ...
Definition ArrayPtr.h:37
Computes convex hulls for 3D meshes.
Definition ConvexHull.h:16
void SetSimplificationMinTriangleAngle(plAngle angle)
Used to remove degenerate and unnecessary triangles that have corners with very little angle change....
Definition ConvexHull.h:31
void SetSimplificationFlatVertexNormalThreshold(plAngle angle)
Used to remove vertices that do not contribute much to the silhouette. Vertices whose adjacent triang...
Definition ConvexHull.h:36
void SetSimplificationMinTriangleEdgeLength(double fLen)
The minimum triangle edge length. Every edge shorter than this will be discarded and replaced by a si...
Definition ConvexHull.h:43
Definition Deque.h:270
Definition DynamicArray.h:81
Definition ConvexHull.h:19
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54