Newton Dynamics  4.00
dConvexHull3d.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_3D__
23 #define __DG_CONVEXHULL_3D__
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 
33 #define D_OLD_CONVEXHULL_3D
34 
37 
39 {
40  public:
42 
43  void SetMark(dInt32 mark) {m_mark = mark;}
44  dInt32 GetMark() const {return m_mark;}
45  dList<dConvexHull3dFace>::dListNode* GetTwin(dInt32 index) const { return m_twin[index];}
46 
47  private:
48  dFloat64 Evalue (const dBigVector* const pointArray, const dBigVector& point) const;
49  dBigPlane GetPlaneEquation (const dBigVector* const pointArray) const;
50 
51  public:
52  dInt32 m_index[3];
53  private:
54  dInt32 m_mark;
56  friend class dConvexHull3d;
57 };
58 
59 D_MSV_NEWTON_ALIGN_32
60 class dConvexHull3d: public dClassAlloc, public dList<dConvexHull3dFace>
61 {
62 #ifdef D_OLD_CONVEXHULL_3D
63  class dNormalMap;
64 #endif
65 
66  public:
67  D_CORE_API dConvexHull3d(const dConvexHull3d& source);
68  D_CORE_API dConvexHull3d(const dFloat64* const vertexCloud, dInt32 strideInBytes, dInt32 count, dFloat64 distTol, dInt32 maxVertexCount = 0x7fffffff);
69  D_CORE_API virtual ~dConvexHull3d();
70 
71  dInt32 GetVertexCount() const;
72  const dBigVector* GetVertexPool() const;
73  const dBigVector& GetVertex(dInt32 i) const;
74 
75  dFloat64 GetDiagonal() const;
76  void GetAABB (dBigVector& boxP0, dBigVector& boxP1) const;
77  dFloat64 RayCast (const dBigVector& localP0, const dBigVector& localP1) const;
78  void CalculateVolumeAndSurfaceArea (dFloat64& volume, dFloat64& surcafeArea) const;
79 
80  protected:
81  dConvexHull3d();
82  void BuildHull (const dFloat64* const vertexCloud, dInt32 strideInBytes, dInt32 count, dFloat64 distTol, dInt32 maxVertexCount);
83 
84  virtual dListNode* AddFace (dInt32 i0, dInt32 i1, dInt32 i2);
85  virtual void DeleteFace (dListNode* const node) ;
86  virtual dInt32 InitVertexArray(dConvexHull3dVertex* const points, const dFloat64* const vertexCloud, dInt32 strideInBytes, dInt32 count, void* const memoryPool, dInt32 maxMemSize);
87 
88  bool CheckFlatSurface(dConvexHull3dAABBTreeNode* vertexTree, dConvexHull3dVertex* const points, dInt32 count, dFloat64 distTol, dInt32 maxVertexCount);
89  void CalculateConvexHull2d (dConvexHull3dAABBTreeNode* vertexTree, dConvexHull3dVertex* const points, dInt32 count, dFloat64 distTol, dInt32 maxVertexCount);
90  void CalculateConvexHull3d (dConvexHull3dAABBTreeNode* vertexTree, dConvexHull3dVertex* const points, dInt32 count, dFloat64 distTol, dInt32 maxVertexCount);
91 
92  dInt32 SupportVertex (dConvexHull3dAABBTreeNode** const tree, const dConvexHull3dVertex* const points, const dBigVector& dir, const bool removeEntry = true) const;
93  dFloat64 TetrahedrumVolume (const dBigVector& p0, const dBigVector& p1, const dBigVector& p2, const dBigVector& p3) const;
94 
95  dInt32 GetUniquePoints(dConvexHull3dVertex* const points, const dFloat64* const vertexCloud, dInt32 strideInBytes, dInt32 count, void* const memoryPool, dInt32 maxMemSize);
96  dConvexHull3dAABBTreeNode* BuildTree (dConvexHull3dAABBTreeNode* const parent, dConvexHull3dVertex* const points, dInt32 count, dInt32 baseIndex, dInt8** const memoryPool, dInt32& maxMemSize) const;
97  static dInt32 ConvexCompareVertex(const dConvexHull3dVertex* const A, const dConvexHull3dVertex* const B, void* const context);
98  bool Sanity() const;
99  void Save (const char* const filename) const;
100 
101  dInt32 m_count;
102  dFloat64 m_diag;
103  dBigVector m_aabbP0;
104  dBigVector m_aabbP1;
105  dArray<dBigVector> m_points;
106 } D_GCC_NEWTON_ALIGN_32;
107 
108 inline dInt32 dConvexHull3d::GetVertexCount() const
109 {
110  return m_count;
111 }
112 
113 inline const dBigVector* dConvexHull3d::GetVertexPool() const
114 {
115  return &m_points[0];
116 }
117 
118 inline const dBigVector& dConvexHull3d::GetVertex(dInt32 index) const
119 {
120  return m_points[index];
121 }
122 
123 inline dFloat64 dConvexHull3d::GetDiagonal() const
124 {
125  return m_diag;
126 }
127 
128 
129 inline void dConvexHull3d::GetAABB (dBigVector& boxP0, dBigVector& boxP1) const
130 {
131  boxP0 = m_aabbP0;
132  boxP1 = m_aabbP1;
133 }
134 
135 #endif
dConvexHull3dAABBTreeNode
Definition: dConvexHull3d.cpp:110
dArray< dBigVector >
dConvexHull3dFace
Definition: dConvexHull3d.h:39
dClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: dClassAlloc.h:29
dBigVector
Definition: dVectorArmNeon.h:1521
dBigPlane
Definition: dPlane.h:50
dConvexHull3dVertex
Definition: dConvexHull3d.cpp:104
dList
Definition: dList.h:32
dConvexHull3d
Definition: dConvexHull3d.h:61
dConvexHull3d::dNormalMap
Definition: dConvexHull3d.cpp:35