Newton Dynamics  4.00
ndShapeCompound.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_SHAPE_COMPOUND_H__
23 #define __ND_SHAPE_COMPOUND_H__
24 
25 #include "ndCollisionStdafx.h"
26 #include "ndShape.h"
27 
28 class ndBodyKinematic;
29 
30 #define D_COMPOUND_STACK_DEPTH 256
31 
32 class ndShapeCompound: public ndShape
33 {
34  public:
35  enum ndNodeType
36  {
37  m_leaf,
38  m_node,
39  };
40 
41  class ndNodeBase;
42  class ndTreeArray : public ndTree<ndNodeBase*, ndInt32, ndContainersFreeListAlloc<ndNodeBase*>>
43  {
44  public:
45  D_COLLISION_API ndTreeArray();
46  D_COLLISION_API void AddNode(ndNodeBase* const node, ndInt32 index, const ndShapeInstance* const parent);
47  };
48 
49  D_CLASS_REFLECTION(ndShapeCompound);
50  D_COLLISION_API ndShapeCompound();
51  D_COLLISION_API ndShapeCompound(const ndLoadSaveBase::ndLoadDescriptor& desc);
52  D_COLLISION_API virtual ~ndShapeCompound();
53 
54  void SetOwner(const ndShapeInstance* const myInstance);
55 
56  D_COLLISION_API const ndTreeArray& GetTree() const;
57 
58  D_COLLISION_API virtual void BeginAddRemove();
59  D_COLLISION_API virtual ndTreeArray::ndNode* AddCollision(ndShapeInstance* const part);
60  D_COLLISION_API virtual void EndAddRemove();
61 
62  protected:
63  class ndSpliteInfo;
64  ndShapeCompound(const ndShapeCompound& source, const ndShapeInstance* const myInstance);
65  virtual ndShapeInfo GetShapeInfo() const;
66  virtual void DebugShape(const ndMatrix& matrix, ndShapeDebugNotify& debugCallback) const;
67  virtual ndFloat32 RayCast(ndRayCastNotify& callback, const ndVector& localP0, const ndVector& localP1, ndFloat32 maxT, const ndBody* const body, ndContactPoint& contactOut) const;
68  virtual void Save(const ndLoadSaveBase::ndSaveDescriptor& desc) const;
69 
70  virtual ndFloat32 GetVolume() const;
71  virtual ndFloat32 GetBoxMinRadius() const;
72  virtual ndFloat32 GetBoxMaxRadius() const;
73 
74  virtual ndShapeCompound* GetAsShapeCompound();
75  virtual ndVector SupportVertex(const ndVector& dir, ndInt32* const vertexIndex) const;
76  virtual ndVector SupportVertexSpecial(const ndVector& dir, ndFloat32 skinMargin, ndInt32* const vertexIndex) const;
77  virtual ndVector SupportVertexSpecialProjectPoint(const ndVector& point, const ndVector& dir) const;
78  virtual ndInt32 CalculatePlaneIntersection(const ndVector& normal, const ndVector& point, ndVector* const contactsOut) const;
79  virtual ndVector CalculateVolumeIntegral(const ndMatrix& globalMatrix, const ndVector& plane, const ndShapeInstance& parentScale) const;
80 
81  D_COLLISION_API virtual void CalculateAabb(const ndMatrix& matrix, ndVector& p0, ndVector& p1) const;
82  //D_COLLISION_API ndInt32 CalculatePlaneIntersection(const ndFloat32* const vertex, const ndInt32* const index, ndInt32 indexCount, ndInt32 strideInFloat, const dPlane& localPlane, dVector* const contactsOut) const;
83 
84  virtual void MassProperties();
85  void ApplyScale(const ndVector& scale);
86  void SetSubShapeOwner(ndBodyKinematic* const body);
87  void ImproveNodeFitness(ndNodeBase* const node) const;
88  ndFloat64 CalculateEntropy(ndInt32 count, ndNodeBase** array);
89  ndNodeBase* BuildTopDown(ndNodeBase** const leafArray, ndInt32 firstBox, ndInt32 lastBox, ndNodeBase** rootNodesMemory, ndInt32& rootIndex);
90  ndNodeBase* BuildTopDownBig(ndNodeBase** const leafArray, ndInt32 firstBox, ndInt32 lastBox, ndNodeBase** rootNodesMemory, ndInt32& rootIndex);
91  ndFloat32 CalculateSurfaceArea(ndNodeBase* const node0, ndNodeBase* const node1, ndVector& minBox, ndVector& maxBox) const;
92  ndMatrix CalculateInertiaAndCenterOfMass(const ndMatrix& alignMatrix, const ndVector& localScale, const ndMatrix& matrix) const;
93  ndFloat32 CalculateMassProperties(const ndMatrix& offset, ndVector& inertia, ndVector& crossInertia, ndVector& centerOfMass) const;
94 
95  ndTreeArray m_array;
96  ndFloat64 m_treeEntropy;
97  ndFloat32 m_boxMinRadius;
98  ndFloat32 m_boxMaxRadius;
99  ndNodeBase* m_root;
100  const ndShapeInstance* m_myInstance;
101  ndInt32 m_idIndex;
102 
103  friend class ndBodyKinematic;
104  friend class ndShapeInstance;
105  friend class ndContactSolver;
106 };
107 
108 inline ndShapeCompound* ndShapeCompound::GetAsShapeCompound()
109 {
110  return this;
111 }
112 
113 inline void ndShapeCompound::SetOwner(const ndShapeInstance* const instance)
114 {
115  m_myInstance = instance;
116 }
117 
119 {
120  public:
121  D_COLLISION_API ndNodeBase();
122  D_COLLISION_API ndNodeBase(const ndNodeBase& copyFrom);
123  D_COLLISION_API ndNodeBase(ndShapeInstance* const instance);
124  D_COLLISION_API ndNodeBase(ndNodeBase* const left, ndNodeBase* const right);
125  D_COLLISION_API ~ndNodeBase();
126 
127  //void Sanity(int level = 0);
128  D_COLLISION_API ndShapeInstance* GetShape() const;
129 
130  private:
131  void CalculateAABB();
132  void SetBox(const ndVector& p0, const ndVector& p1);
133 
134  ndVector m_p0;
135  ndVector m_p1;
136  ndVector m_size;
137  ndVector m_origin;
138  ndFloat32 m_area;
139  ndInt32 m_type;
140  ndNodeBase* m_left;
141  ndNodeBase* m_right;
142  ndNodeBase* m_parent;
143  ndTreeArray::ndNode* m_myNode;
144  ndShapeInstance* m_shapeInstance;
145 
146  friend class ndStackEntry;
147  friend class ndContactSolver;
148  friend class ndShapeCompound;
149  friend class ndStackBvhStackEntry;
150 };
151 
152 
153 #endif
154 
155 
156 
ndClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: ndClassAlloc.h:30
ndStackEntry
Definition: ndContactSolver.cpp:272
ndBody
Definition: ndBody.h:43
ndTree
Definition: ndTree.h:82
ndShape
Definition: ndShape.h:209
ndShapeCompound::ndNodeBase
Definition: ndShapeCompound.h:119
ndContactSolver
Definition: ndContactSolver.h:60
ndRayCastNotify
Definition: ndRayCastNotify.h:31
ndStackBvhStackEntry
Definition: ndContactSolver.cpp:231
ndShapeCompound::ndTreeArray
Definition: ndShapeCompound.h:43
ndContactPoint
Definition: ndContact.h:39
ndBodyKinematic
Definition: ndBodyKinematic.h:40
ndMatrix
Definition: ndMatrix.h:42
ndShapeDebugNotify
Definition: ndShapeInstance.h:38
ndShapeCompound::ndSpliteInfo
Definition: ndShapeCompound.cpp:136
ndShapeInfo
Definition: ndShape.h:182
ndShapeCompound
Definition: ndShapeCompound.h:33
ndShapeInstance
Definition: ndShapeInstance.h:62
ndLoadSaveBase::ndLoadDescriptor
Definition: ndSaveLoadSytem.h:59
ndLoadSaveBase::ndSaveDescriptor
Definition: ndSaveLoadSytem.h:93
ndVector
Definition: ndVectorArmNeon.h:41