Newton Dynamics  4.00
ndConvexHull3d.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 __ND_CONVEXHULL_3D__
23 #define __ND_CONVEXHULL_3D__
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 
33 #define D_OLD_CONVEXHULL_3D
34 
37 
39 {
40  public:
41  D_CORE_API ndConvexHull3dFace();
42 
43  private:
44  void SetMark(ndInt32 mark) { m_mark = mark; }
45  ndInt32 GetMark() const { return m_mark; }
46  ndList<ndConvexHull3dFace>::ndNode* GetTwin(ndInt32 index) const;
47  ndFloat64 Evalue (const ndBigVector* const pointArray, const ndBigVector& point) const;
48  ndBigPlane GetPlaneEquation (const ndBigVector* const pointArray, bool& isvalid) const;
49 
50  public:
51  ndInt32 m_index[3];
52 
53  private:
54  ndInt32 m_mark;
56  friend class ndConvexHull3d;
57 };
58 
59 D_MSV_NEWTON_ALIGN_32
60 class ndConvexHull3d: public ndList<ndConvexHull3dFace>
61 {
62 #ifdef D_OLD_CONVEXHULL_3D
63  class ndNormalMap;
64 #endif
65 
66  public:
67  D_CORE_API ndConvexHull3d(const ndConvexHull3d& source);
68  D_CORE_API ndConvexHull3d(const ndFloat64* const vertexCloud, ndInt32 strideInBytes, ndInt32 count, ndFloat64 distTol, ndInt32 maxVertexCount = 0x7fffffff);
69  D_CORE_API virtual ~ndConvexHull3d();
70 
71  const ndArray<ndBigVector>& GetVertexPool() const;
72 
73  ndFloat64 GetDiagonal() const;
74  void GetAABB (ndBigVector& boxP0, ndBigVector& boxP1) const;
75  ndFloat64 RayCast (const ndBigVector& localP0, const ndBigVector& localP1) const;
76  void CalculateVolumeAndSurfaceArea (ndFloat64& volume, ndFloat64& surcafeArea) const;
77 
78  protected:
80  void BuildHull (const ndFloat64* const vertexCloud, ndInt32 strideInBytes, ndInt32 count, ndFloat64 distTol, ndInt32 maxVertexCount);
81 
82  virtual ndNode* AddFace (ndInt32 i0, ndInt32 i1, ndInt32 i2);
83  virtual void DeleteFace (ndNode* const node) ;
84  virtual ndInt32 InitVertexArray(ndConvexHull3dVertex* const points, ndInt32 count, void* const memoryPool, ndInt32 maxMemSize);
85 
86  bool CheckFlatSurface(ndConvexHull3dAABBTreeNode* vertexTree, ndConvexHull3dVertex* const points, ndInt32 count, ndFloat64 distTol, ndInt32 maxVertexCount);
87  void CalculateConvexHull2d (ndConvexHull3dAABBTreeNode* vertexTree, ndConvexHull3dVertex* const points, ndInt32 count, ndFloat64 distTol, ndInt32 maxVertexCount);
88  void CalculateConvexHull3d (ndConvexHull3dAABBTreeNode* vertexTree, ndConvexHull3dVertex* const points, ndInt32 count, ndFloat64 distTol, ndInt32 maxVertexCount);
89 
90  ndInt32 SupportVertex (ndConvexHull3dAABBTreeNode** const tree, const ndConvexHull3dVertex* const points, const ndBigVector& dir, const bool removeEntry = true) const;
91  ndFloat64 TetrahedrumVolume (const ndBigVector& p0, const ndBigVector& p1, const ndBigVector& p2, const ndBigVector& p3) const;
92 
93  ndInt32 GetUniquePoints(ndConvexHull3dVertex* const points, ndInt32 count);
94  ndConvexHull3dAABBTreeNode* BuildTree (ndConvexHull3dAABBTreeNode* const parent, ndConvexHull3dVertex* const points, ndInt32 count, ndInt32 baseIndex, ndInt8** const memoryPool, ndInt32& maxMemSize) const;
95 
96  bool Sanity() const;
97  void Save (const char* const filename) const;
98 
99  ndBigVector m_aabbP0;
100  ndBigVector m_aabbP1;
101  ndFloat64 m_diag;
102  ndArray<ndBigVector> m_points;
103 } D_GCC_NEWTON_ALIGN_32;
104 
105 inline const ndArray<ndBigVector>& ndConvexHull3d::GetVertexPool() const
106 {
107  return m_points;
108 }
109 
110 inline ndFloat64 ndConvexHull3d::GetDiagonal() const
111 {
112  return m_diag;
113 }
114 
115 inline void ndConvexHull3d::GetAABB (ndBigVector& boxP0, ndBigVector& boxP1) const
116 {
117  boxP0 = m_aabbP0;
118  boxP1 = m_aabbP1;
119 }
120 
121 #endif
ndArray< ndBigVector >
ndConvexHull3d::ndNormalMap
Definition: ndConvexHull3d.cpp:34
ndConvexHull3dVertex
Definition: ndConvexHull3d.cpp:106
ndConvexHull3dFace
Definition: ndConvexHull3d.h:39
ndList
Definition: ndList.h:33
ndBigPlane
Definition: ndPlane.h:50
ndConvexHull3dAABBTreeNode
Definition: ndConvexHull3d.cpp:112
ndBigVector
Definition: ndVectorArmNeon.h:463
ndConvexHull3d
Definition: ndConvexHull3d.h:61