Newton Dynamics  4.00
ndConvexHull4d.h
1 /* Copyright (c) <2003-2022> <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 __NDG_CONVEXHULL_4D__
23 #define __NDG_CONVEXHULL_4D__
24 
25 #include "ndCoreStdafx.h"
26 #include "ndList.h"
27 #include "ndArray.h"
28 #include "ndPlane.h"
29 #include "ndVector.h"
30 #include "ndMatrix.h"
31 #include "ndQuaternion.h"
32 
34 
36 {
37  public:
38  void operator = (const ndBigVector& 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  ndInt32 m_index;
49  ndInt32 m_mark;
50 };
51 
53 {
54  public:
56  {
57  public:
58  ndInt32 m_index[4];
60  };
61 
63  {
64  public:
65  ndTetrahedrumPlane (const ndBigVector& p0, const ndBigVector& p1, const ndBigVector& p2, const ndBigVector& p3);
66  ndFloat64 Evalue (const ndBigVector& point) const;
67  ndFloat64 m_dist;
68  };
69 
70  D_CORE_API ndConvexHull4dTetraherum();
71  D_CORE_API ndBigVector CircumSphereCenter(const ndConvexHull4dVector* const pointArray) const;
72 
73  private:
74  void Init (const ndConvexHull4dVector* const points, ndInt32 v0, ndInt32 v1, ndInt32 v2, ndInt32 v3);
75  ndTetrahedrumPlane GetPlaneEquation(const ndConvexHull4dVector* const points) const;
76  ndFloat64 GetTetraVolume(const ndConvexHull4dVector* const pointArray) const;
77  ndFloat64 Evalue(const ndConvexHull4dVector* const pointArray, const ndBigVector& point) const;
78  ndInt32 GetMark() const { return m_mark; }
79  void SetMark(ndInt32 mark) { m_mark = mark; }
80 
81  public:
82  ndTetrahedrumFace m_faces[4];
83  ndInt32 m_mark;
84  ndInt32 m_uniqueID;
85 
86 #ifdef _DEBUG
87  ndInt32 m_debugID;
88 #endif
89  friend class ndConvexHull4d;
90  friend class ndDelaunayTetrahedralization;
91 };
92 
93 class ndConvexHull4d: public ndList<ndConvexHull4dTetraherum>
94 {
95  public:
96  class ndTempList: public ndList<ndNode*, ndContainersFreeListAlloc<ndNode*>>
97  {
98  };
99 
100  D_CORE_API ndConvexHull4d(const ndConvexHull4d& source);
101  D_CORE_API ndConvexHull4d(const ndFloat64* const vertexCloud, ndInt32 strideInBytes, ndInt32 count, ndFloat64 distTol);
102  D_CORE_API virtual ~ndConvexHull4d();
103 
104  ndInt32 GetVertexCount() const;
105  ndInt32 GetVertexIndex(ndInt32 i) const;
106  const ndBigVector& GetVertex(ndInt32 i) const;
107 
108  const ndConvexHull4dVector* GetHullVertexArray() const;
109  ndFloat64 GetTetraVolume (const ndConvexHull4dTetraherum* const tetra) const;
110 
111  ndInt32 IncMark ();
112  void Save (const char* const filename) const;
113 
114  protected:
115  ndConvexHull4d();
116 
117  void BuildHull (const ndFloat64* const vertexCloud, ndInt32 strideInBytes, ndInt32 count, ndFloat64 distTol);
118 
119  virtual ndInt32 AddVertex (const ndBigVector& vertex);
120  virtual ndInt32 InitVertexArray(ndConvexHull4dVector* const points, const ndFloat64* const vertexCloud, ndInt32 strideInBytes, ndInt32 count, void* const memoryPool, ndInt32 maxMemSize);
121 
122  virtual ndNode* AddFace (ndInt32 i0, ndInt32 i1, ndInt32 i2, ndInt32 i3);
123  virtual void DeleteFace (ndNode* const node);
124 
125  ndNode* FindFacingNode(const ndBigVector& vertex);
126 
127  void InsertNewVertex(ndInt32 vertexIndex, ndNode* const frontFace, ndTempList& deletedFaces, ndTempList& newFaces);
128  ndInt32 SupportVertex (ndConvexHull4dAABBTreeNode** const tree, const ndConvexHull4dVector* const points, const ndBigVector& dir, const bool removeEntry = true) const;
129 
130  void CalculateConvexHull (ndConvexHull4dAABBTreeNode* vertexTree, ndConvexHull4dVector* const points, ndInt32 count, ndFloat64 distTol);
131  void LinkSibling (ndNode* node0, ndNode* node1) const;
132  bool Sanity() const;
133  ndConvexHull4dAABBTreeNode* BuildTree (ndConvexHull4dAABBTreeNode* const parent, ndConvexHull4dVector* const points, ndInt32 count, ndInt32 baseIndex, ndInt8** const memoryPool, ndInt32& maxMemSize) const;
134 
136  {
137  public:
138  ndNormalMap();
139  private:
140  void TessellateTriangle(ndInt32 level, const ndVector& p0, const ndVector& p1, const ndVector& p2, ndBigVector* const buffer, ndInt32& count);
141 
142  ndBigVector m_normal[1024];
143  ndInt32 m_count;
144  friend class ndConvexHull4d;
145  };
146  static const ndNormalMap& GetNormaMap();
147 
148  ndInt32 m_mark;
149  ndInt32 m_count;
150  ndFloat64 m_diag;
152 };
153 
154 inline ndInt32 ndConvexHull4d::IncMark ()
155 {
156  m_mark ++;
157  return m_mark;
158 }
159 
160 inline ndInt32 ndConvexHull4d::GetVertexCount() const
161 {
162  return m_count;
163 }
164 
165 inline ndInt32 ndConvexHull4d::GetVertexIndex(ndInt32 index) const
166 {
167  ndAssert (index >= 0);
168  ndAssert (index < m_count);
169  return m_points[index].m_index;
170 }
171 
172 
173 inline const ndBigVector& ndConvexHull4d::GetVertex(ndInt32 index) const
174 {
175  ndAssert (index >= 0);
176  ndAssert (index < m_count);
177  return m_points[index];
178 }
179 
180 inline const ndConvexHull4dVector* ndConvexHull4d::GetHullVertexArray() const
181 {
182  return &m_points[0];
183 }
184 
185 inline ndFloat64 ndConvexHull4d::GetTetraVolume (const ndConvexHull4dTetraherum* const tetra) const
186 {
187  return tetra->GetTetraVolume (&m_points[0]);
188 }
189 
190 #endif
ndConvexHull4dTetraherum::ndTetrahedrumPlane
Definition: ndConvexHull4d.h:63
ndArray< ndConvexHull4dVector >
ndConvexHull4dTetraherum::ndTetrahedrumFace
Definition: ndConvexHull4d.h:56
ndConvexHull4d
Definition: ndConvexHull4d.h:94
ndConvexHull4dAABBTreeNode
Definition: ndConvexHull4d.cpp:124
ndConvexHull4d::ndNormalMap
Definition: ndConvexHull4d.h:136
ndList
Definition: ndList.h:33
ndDelaunayTetrahedralization
Definition: ndDelaunayTetrahedralization.h:29
ndConvexHull4dVector
Definition: ndConvexHull4d.h:36
ndBigVector
Definition: ndVectorArmNeon.h:463
ndConvexHull4d::ndTempList
Definition: ndConvexHull4d.h:97
ndConvexHull4dTetraherum
Definition: ndConvexHull4d.h:53
ndVector
Definition: ndVectorArmNeon.h:41