22 #ifndef __D_POLYHEDRA_H__
23 #define __D_POLYHEDRA_H__
25 #include "dCoreStdafx.h"
32 #include "dClassAlloc.h"
42 typedef dInt64 dEdgeKey;
48 dEdge (dInt32 vertex, dInt32 face, dUnsigned64 userdata = 0);
51 dInt32 m_incidentVertex;
52 dInt32 m_incidentFace;
53 dUnsigned64 m_userData;
58 } D_GCC_NEWTON_ALIGN_32 ;
71 :m_key(dUnsigned64(key))
76 :m_keyLow(dUnsigned32 (keyLow))
77 ,m_keyHigh(dUnsigned32 (keyHigh))
86 dInt32 GetLowKey()
const
88 return dInt32(m_keyLow);
91 dInt32 GetHighKey()
const
93 return dInt32(m_keyHigh);
96 bool operator<(
const dgPairKey& key)
const
98 return m_key < key.m_key;
101 bool operator>(
const dgPairKey& key)
const
103 return m_key > key.m_key;
113 dUnsigned32 m_keyLow;
114 dUnsigned32 m_keyHigh;
123 virtual bool ReportProgress(dFloat32 percentProgress)
const {
return true;}
125 virtual void BeginFace();
126 dEdge* AddFace (dInt32 v0, dInt32 v1, dInt32 v2);
127 dEdge* AddFace (dInt32 count,
const dInt32*
const index);
128 D_CORE_API
dEdge* AddFace (dInt32 count,
const dInt32*
const index,
const dInt64*
const userdata);
129 D_CORE_API
virtual bool EndFace ();
130 D_CORE_API
virtual void DeleteFace(
dEdge*
const edge);
132 D_CORE_API dInt32 GetFaceCount()
const;
133 dInt32 GetEdgeCount()
const;
134 dInt32 GetLastVertexIndex()
const;
136 dInt32 IncLRU()
const;
137 dInt32 GetLRU()
const;
138 void SetLRU(dInt32 lru)
const;
140 dEdge* FindEdge (dInt32 v0, dInt32 v1)
const;
141 dTreeNode* FindEdgeNode (dInt32 v0, dInt32 v1)
const;
143 D_CORE_API
dEdge* AddHalfEdge (dInt32 v0, dInt32 v1);
144 D_CORE_API
void DeleteEdge (
dEdge*
const edge);
145 void DeleteEdge (dInt32 v0, dInt32 v1);
149 D_CORE_API
bool FlipEdge (
dEdge*
const edge);
150 D_CORE_API
dEdge* SpliteEdge (dInt32 newIndex,
dEdge*
const edge);
151 D_CORE_API
dBigVector FaceNormal (
const dEdge*
const face,
const dFloat64*
const vertex, dInt32 strideInBytes)
const;
153 D_CORE_API
void SavePLY(
const char*
const fileName,
const dFloat64*
const vertex, dInt32 strideInBytes)
const;
155 void BeginConectedSurface()
const;
156 D_CORE_API
bool GetConectedSurface (
dPolyhedra &polyhedra)
const;
157 void EndConectedSurface()
const;
160 D_CORE_API
void ChangeEdgeIncidentVertex (
dEdge*
const edge, dInt32 newIndex);
161 D_CORE_API
void DeleteDegenerateFaces (
const dFloat64*
const pool, dInt32 dstStrideInBytes, dFloat64 minArea);
163 D_CORE_API
bool Optimize (
const dFloat64*
const pool, dInt32 strideInBytes, dFloat64 tol, dInt32 maxFaceCount = 1<<28);
164 D_CORE_API
void Triangulate (
const dFloat64*
const vertex, dInt32 strideInBytes,
dPolyhedra*
const leftOversOut);
165 D_CORE_API
void ConvexPartition (
const dFloat64*
const vertex, dInt32 strideInBytes,
dPolyhedra*
const leftOversOut);
166 D_CORE_API
bool IsFaceConvex(
dEdge*
const face,
const dFloat64*
const pool, dInt32 strideInBytes)
const;
169 D_CORE_API
dEdge* CollapseEdge(
dEdge*
const edge);
170 D_CORE_API
bool PolygonizeFace(
dEdge*
const face,
const dFloat64*
const pool, dInt32 stride);
171 D_CORE_API
bool TriangulateFace(
dEdge*
const face,
const dFloat64*
const pool, dInt32 stride);
174 void RefineTriangulation (
const dFloat64*
const vertex, dInt32 stride);
175 void RefineTriangulation (
const dFloat64*
const vertex, dInt32 stride,
const dBigVector& normal, dInt32 perimeterCount,
dEdge**
const perimeter);
176 void OptimizeTriangulation (
const dFloat64*
const vertex, dInt32 strideInBytes);
177 void RemoveInteriorEdges (
dPolyhedra& polyhedraOut,
const dFloat64*
const vertex, dInt32 strideInBytes);
178 void MarkAdjacentCoplanarFaces (
dPolyhedra& polyhedraOut,
dEdge*
const face,
const dFloat64*
const pool, dInt32 strideInBytes);
182 void RemoveHalfEdge (
dEdge*
const edge);
183 dEdge* OptimizeCollapseEdge (
dEdge*
const edge);
184 bool IsOkToCollapse (
const dBigVector*
const pool,
dEdge*
const edge)
const;
185 dFloat64 EdgePenalty (
const dBigVector*
const pool,
dEdge*
const edge, dFloat64 dist)
const;
189 dEdge* BestEdgePolygonizeFace(
const dBigVector& normal,
dEdge*
const edge,
const dFloat64*
const pool, dInt32 stride,
const dBigVector& point)
const;
191 static dInt32 GetInteriorDiagonals (
dPolyhedra& polyhedra,
dEdge**
const diagonals, dInt32 maxCount);
192 static dBigPlane UnboundedLoopPlane (dInt32 i0, dInt32 i1, dInt32 i2,
const dBigVector*
const pool);
193 static void RemoveOuterColinearEdges(
dPolyhedra& flatFace,
const dFloat64*
const vertex, dInt32 stride);
194 static void RemoveInteriorColinearEdges(
dPolyhedra& flatFace,
const dFloat64*
const vertex, dInt32 stride);
195 static bool IsEssensialDiagonal (
dEdge*
const diagonal,
const dBigVector& normal,
const dFloat64*
const pool, dInt32 stride);
196 static bool IsEssensialPointDiagonal (
dEdge*
const diagonal,
const dBigVector& normal,
const dFloat64*
const pool, dInt32 stride);
198 mutable dInt32 m_baseMark;
199 mutable dInt32 m_edgeMark;
200 mutable dInt32 m_faceSecuence;
201 friend class dPolyhedraDescriptor;
204 inline dEdge::dEdge ()
208 inline dEdge::dEdge (dInt32 vertex, dInt32 face, dUnsigned64 userdata)
209 :m_incidentVertex(vertex)
210 ,m_incidentFace(face)
211 ,m_userData(userdata)
219 inline dEdge::~dEdge ()
223 inline void dPolyhedra::BeginFace ()
227 inline dEdge* dPolyhedra::AddFace (dInt32 count,
const dInt32*
const index)
229 return AddFace (count, index,
nullptr);
232 inline dEdge* dPolyhedra::AddFace (dInt32 v0, dInt32 v1, dInt32 v2)
239 return AddFace (3, vertex,
nullptr);
242 inline dInt32 dPolyhedra::GetEdgeCount()
const
245 dInt32 edgeCount = 0;
246 Iterator iter(*
this);
247 for (iter.Begin(); iter; iter ++)
251 dAssert (edgeCount == GetCount());;
256 inline dPolyhedra::dPolyhedra()
265 inline dInt32 dPolyhedra::GetLastVertexIndex()
const
267 dInt32 maxVertexIndex = -1;
268 Iterator iter(*
this);
269 for (iter.Begin(); iter; iter ++)
271 const dEdge*
const edge = &(*iter);
272 if (edge->m_incidentVertex > maxVertexIndex)
274 maxVertexIndex = edge->m_incidentVertex;
277 return maxVertexIndex + 1;
280 inline dInt32 dPolyhedra::IncLRU()
const
283 dAssert (m_edgeMark < 0x7fffffff);
287 inline dInt32 dPolyhedra::GetLRU()
const
292 inline void dPolyhedra::SetLRU(dInt32 lru)
const
294 if (lru > m_edgeMark)
300 inline void dPolyhedra::BeginConectedSurface()
const
302 m_baseMark = IncLRU();
305 inline void dPolyhedra::EndConectedSurface()
const
309 inline dPolyhedra::dTreeNode* dPolyhedra::FindEdgeNode (dInt32 i0, dInt32 i1)
const
311 dgPairKey key (i0, i1);
312 return Find (key.GetVal());
315 inline dEdge *dPolyhedra::FindEdge (dInt32 i0, dInt32 i1)
const
317 dTreeNode*
const node = FindEdgeNode (i0, i1);
318 return node ? &node->GetInfo() :
nullptr;
321 inline void dPolyhedra::DeleteEdge (dInt32 v0, dInt32 v1)
323 dgPairKey pairKey (v0, v1);
324 dTreeNode*
const node = Find(pairKey.GetVal());
325 dEdge*
const edge = node ? &node->GetInfo() :
nullptr;