Newton Dynamics  4.00
dConvexHull4d.h
1 /* Copyright (c) <2003-2019> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef __DG_CONVEXHULL_4D__
23 #define __DG_CONVEXHULL_4D__
24 
25 #include "dCoreStdafx.h"
26 #include "dList.h"
27 #include "dArray.h"
28 #include "dPlane.h"
29 #include "dVector.h"
30 #include "dMatrix.h"
31 #include "dQuaternion.h"
32 
34 
36 {
37  public:
38  void operator = (const dBigVector& a)
39  {
40  m_x = a.m_x;
41  m_y = a.m_y;
42  m_z = a.m_z;
43  m_w = a.m_w;
44  m_index = 0;
45  m_mark = 0;
46  }
47 
48  dInt32 m_index;
49  dInt32 m_mark;
50 };
51 
53 {
54  public:
56  {
57  public:
58  dInt32 m_index[4];
60  };
61 
62 
64  {
65  public:
66  dgTetrahedrumPlane (const dBigVector& p0, const dBigVector& p1, const dBigVector& p2, const dBigVector& p3);
67  dFloat64 Evalue (const dBigVector& point) const;
68  dFloat64 m_dist;
69  };
70 
72  dgTetrahedrumPlane GetPlaneEquation (const dConvexHull4dVector* const points) const;
73  dFloat64 GetTetraVolume (const dConvexHull4dVector* const pointArray) const;
74  dBigVector CircumSphereCenter (const dConvexHull4dVector* const pointArray) const;
75  dFloat64 Evalue (const dConvexHull4dVector* const pointArray, const dBigVector& point) const;
76 
77  dInt32 GetMark() const { return m_mark; }
78  void SetMark(dInt32 mark) { m_mark = mark; }
79 
80  private:
81  void Init (const dConvexHull4dVector* const points, dInt32 v0, dInt32 v1, dInt32 v2, dInt32 v3);
82 
83  public:
84  dgTetrahedrumFace m_faces[4];
85  dInt32 m_mark;
86  dInt32 m_uniqueID;
87 
88 #ifdef _DEBUG
89  dInt32 m_debugID;
90 #endif
91  friend class dConvexHull4d;
92  friend class dDelaunayTetrahedralization;
93 };
94 
95 class dConvexHull4d: public dList<dConvexHull4dTetraherum>
96 {
97  public:
98  D_CORE_API dConvexHull4d(const dConvexHull4d& source);
99  D_CORE_API dConvexHull4d(const dFloat64* const vertexCloud, dInt32 strideInBytes, dInt32 count, dFloat64 distTol);
100  D_CORE_API virtual ~dConvexHull4d();
101 
102  dInt32 GetVertexCount() const;
103  dInt32 GetVertexIndex(dInt32 i) const;
104  const dBigVector& GetVertex(dInt32 i) const;
105 
106  const dConvexHull4dVector* GetHullVertexArray() const;
107  dFloat64 GetTetraVolume (const dConvexHull4dTetraherum* const tetra) const;
108 
109  dInt32 IncMark ();
110  void Save (const char* const filename) const;
111 
112  protected:
113  dConvexHull4d();
114 
115  void BuildHull (const dFloat64* const vertexCloud, dInt32 strideInBytes, dInt32 count, dFloat64 distTol);
116 
117  virtual dInt32 AddVertex (const dBigVector& vertex);
118  virtual dInt32 InitVertexArray(dConvexHull4dVector* const points, const dFloat64* const vertexCloud, dInt32 strideInBytes, dInt32 count, void* const memoryPool, dInt32 maxMemSize);
119 
120  virtual dListNode* AddFace (dInt32 i0, dInt32 i1, dInt32 i2, dInt32 i3);
121  virtual void DeleteFace (dListNode* const node);
122 
123  dListNode* FindFacingNode(const dBigVector& vertex);
124 
125  void InsertNewVertex(dInt32 vertexIndex, dListNode* const frontFace, dList<dListNode*>& deletedFaces, dList<dListNode*>& newFaces);
126  dInt32 SupportVertex (dConvexHull4dAABBTreeNode** const tree, const dConvexHull4dVector* const points, const dBigVector& dir, const bool removeEntry = true) const;
127 
128  void CalculateConvexHull (dConvexHull4dAABBTreeNode* vertexTree, dConvexHull4dVector* const points, dInt32 count, dFloat64 distTol);
129  void LinkSibling (dListNode* node0, dListNode* node1) const;
130  bool Sanity() const;
131  dConvexHull4dAABBTreeNode* BuildTree (dConvexHull4dAABBTreeNode* const parent, dConvexHull4dVector* const points, dInt32 count, dInt32 baseIndex, dInt8** const memoryPool, dInt32& maxMemSize) const;
132 
133  static dInt32 ConvexCompareVertex(const dConvexHull4dVector* const A, const dConvexHull4dVector* const B, void* const context);
134 
136  {
137  public:
138  dgNormalMap();
139  private:
140  void TessellateTriangle(dInt32 level, const dVector& p0, const dVector& p1, const dVector& p2, dBigVector* const buffer, dInt32& count);
141 
142  dBigVector m_normal[1024];
143  dInt32 m_count;
144  friend class dConvexHull4d;
145  };
146  static const dgNormalMap& GetNormaMap();
147 
148  dInt32 m_mark;
149  dInt32 m_count;
150  dFloat64 m_diag;
152 };
153 
154 inline dInt32 dConvexHull4d::IncMark ()
155 {
156  m_mark ++;
157  return m_mark;
158 }
159 
160 inline dInt32 dConvexHull4d::GetVertexCount() const
161 {
162  return m_count;
163 }
164 
165 inline dInt32 dConvexHull4d::GetVertexIndex(dInt32 index) const
166 {
167  dAssert (index >= 0);
168  dAssert (index < m_count);
169  return m_points[index].m_index;
170 }
171 
172 
173 inline const dBigVector& dConvexHull4d::GetVertex(dInt32 index) const
174 {
175  dAssert (index >= 0);
176  dAssert (index < m_count);
177  return m_points[index];
178 }
179 
180 inline const dConvexHull4dVector* dConvexHull4d::GetHullVertexArray() const
181 {
182  return &m_points[0];
183 }
184 
185 inline dFloat64 dConvexHull4d::GetTetraVolume (const dConvexHull4dTetraherum* const tetra) const
186 {
187  return tetra->GetTetraVolume (&m_points[0]);
188 }
189 
190 #endif
dConvexHull4d::dgNormalMap
Definition: dConvexHull4d.h:136
dArray< dConvexHull4dVector >
dConvexHull4d
Definition: dConvexHull4d.h:96
dConvexHull4dTetraherum::dgTetrahedrumFace
Definition: dConvexHull4d.h:56
dBigVector
Definition: dVectorArmNeon.h:1521
dDelaunayTetrahedralization
Definition: dDelaunayTetrahedralization.h:29
dVector
Definition: dVectorArmNeon.h:1104
dConvexHull4dVector
Definition: dConvexHull4d.h:36
dList
Definition: dList.h:32
dConvexHull4dTetraherum
Definition: dConvexHull4d.h:53
dConvexHull4dTetraherum::dgTetrahedrumPlane
Definition: dConvexHull4d.h:64
dConvexHull4dAABBTreeNode
Definition: dConvexHull4d.cpp:120