Newton Dynamics  4.00
ndShape.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_H__
23 #define __ND_SHAPE_H__
24 
25 #include "ndCollisionStdafx.h"
26 
27 class ndBody;
28 class ndShape;
29 class ndShapeBox;
30 class ndShapeNull;
31 class ndShapeCone;
32 class ndShapePoint;
33 class ndShapeConvex;
34 class ndShapeSphere;
35 class ndShapeCapsule;
36 class ndContactPoint;
37 class ndShapeCompound;
38 class ndShapeCylinder;
39 class ndShapeCompound;
40 class ndRayCastNotify;
41 class ndShapeInstance;
42 class ndShapeStatic_bvh;
43 class ndShapeStaticMesh;
44 class ndShapeHeightfield;
46 class ndShapeDebugNotify;
49 
50 
51 enum ndShapeID
52 {
53  // do not change the order of these enum
54  m_box = 0,
55  m_cone,
56  m_sphere,
57  m_capsule,
58  m_cylinder,
59  m_chamferCylinder,
60  m_convexHull, // this must be the last convex shape ID
61 
62  // special and non convex collisions.
63  m_compound,
64  m_staticMesh,
65  m_heightField,
66  m_staticProceduralMesh,
67  m_nullCollision,
68  m_pointCollision,
69  m_polygonCollision,
70  m_boundingBoxHierachy,
71 
72  //m_deformableClothPatch,
73  //m_deformableSolidMesh,
74  // these are for internal use only
75  //m_contactCloud,
76  //m_lumpedMassCollision
77 };
78 
80 {
81  public:
83  {
84  memset(this, 0, sizeof(ndShapeMaterial));
85  }
86 
87  ndInt64 m_userId;
88  union nData
89  {
90  void* m_userData;
91  ndUnsigned64 m_alignPad;
92  } m_data;
93 
94  union dExtraData
95  {
96  ndUnsigned64 m_intData;
97  ndFloat32 m_floatData;
98  } m_userParam[6];
99 };
100 
101 struct ndBoxInfo
102 {
103  ndFloat32 m_x;
104  ndFloat32 m_y;
105  ndFloat32 m_z;
106 };
107 
109 {
110  ndFloat32 m_noUsed;
111 };
112 
114 {
115  ndFloat32 m_radius;
116 };
117 
119 {
120  ndFloat32 m_radio0;
121  ndFloat32 m_radio1;
122  ndFloat32 m_height;
123 };
124 
126 {
127  ndFloat32 m_radio0;
128  ndFloat32 m_radio1;
129  ndFloat32 m_height;
130 };
131 
133 {
134  ndFloat32 m_radius;
135  ndFloat32 m_height;
136 };
137 
139 {
140  ndFloat32 m_r;
141  ndFloat32 m_height;
142 };
143 
145 {
146  ndInt32 m_vertexCount;
147  ndInt32 m_strideInBytes;
148  ndInt32 m_faceCount;
149  ndVector* m_vertex;
150 };
151 
153 {
154  ndInt32 m_noUsed;
155 };
156 
158 {
159  ndInt32 m_noUsed;
160 };
161 
163 {
164  ndInt32 m_vertexCount;
165  ndInt32 m_indexCount;
166 };
167 
169 {
170  ndInt32 m_width;
171  ndInt32 m_height;
172  ndInt32 m_gridsDiagonals;
173  ndFloat32 m_verticalScale;
174  ndFloat32 m_horizonalScale_x;
175  ndFloat32 m_horizonalScale_z;
176  ndReal* m_elevation;
177  ndInt8* m_atributes;
178 };
179 
180 D_MSV_NEWTON_ALIGN_32
182 {
183  public:
184  ndMatrix m_offsetMatrix;
185  ndVector m_scale;
186  ndShapeMaterial m_shapeMaterial;
187  ndShapeID m_collisionType;
188  union
189  {
190  ndBoxInfo m_box;
191  ndConeInfo m_cone;
192  ndPointInfo m_point;
193  ndSphereInfo m_sphere;
194  ndCapsuleInfo m_capsule;
195  ndCollisionBvhInfo m_bvh;
196  ndCylinderInfo m_cylinder;
197  ndCompoundInfo m_compound;
198  ndConvexHullInfo m_convexhull;
199  ndHeighfieldInfo m_heightfield;
200  ndProceduralInfo m_procedural;
201  ndChamferCylinderInfo m_chamferCylinder;
202 
203  ndFloat32 m_paramArray[32];
204  };
205 } D_GCC_NEWTON_ALIGN_32;
206 
207 D_MSV_NEWTON_ALIGN_32
208 class ndShape: public ndContainersFreeListAlloc<ndShape>
209 {
210  public:
211  D_CLASS_REFLECTION(ndShape);
212  D_COLLISION_API virtual ~ndShape();
213 
214  D_COLLISION_API ndInt32 GetRefCount() const;
215  D_COLLISION_API virtual ndInt32 Release() const;
216  D_COLLISION_API virtual const ndShape* AddRef() const;
217 
218  virtual ndShapeBox* GetAsShapeBox() { return nullptr; }
219  virtual ndShapeNull* GetAsShapeNull() { return nullptr; }
220  virtual ndShapeCone* GetAsShapeCone() { return nullptr; }
221  virtual ndShapePoint* GetAsShapePoint() { return nullptr; }
222  virtual ndShapeConvex* GetAsShapeConvex() { return nullptr; }
223  virtual ndShapeSphere* GetAsShapeSphere() { return nullptr; }
224  virtual ndShapeCapsule* GetAsShapeCapsule() { return nullptr; }
225  virtual ndShapeCylinder* GetAsShapeCylinder() { return nullptr; }
226  virtual ndShapeCompound* GetAsShapeCompound() { return nullptr; }
227  virtual ndShapeStatic_bvh* GetAsShapeStaticBVH() { return nullptr; }
228  virtual ndShapeStaticMesh* GetAsShapeStaticMesh() { return nullptr; }
229  virtual ndShapeHeightfield* GetAsShapeHeightfield() { return nullptr; }
230  virtual ndShapeConvexPolygon* GetAsShapeAsConvexPolygon() { return nullptr; }
231  virtual ndShapeChamferCylinder* GetAsShapeChamferCylinder() { return nullptr; }
232  virtual ndShapeStaticProceduralMesh* GetAsShapeStaticProceduralMesh() { return nullptr; }
233 
234  virtual ndInt32 GetConvexVertexCount() const;
235 
236  ndVector GetObbSize() const;
237  ndVector GetObbOrigin() const;
238  ndFloat32 GetUmbraClipSize() const;
239 
240  D_COLLISION_API virtual void MassProperties();
241 
242  virtual void DebugShape(const ndMatrix& matrix, ndShapeDebugNotify& debugCallback) const = 0;
243 
244  virtual ndShapeInfo GetShapeInfo() const;
245  virtual ndFloat32 GetVolume() const = 0;
246  virtual ndFloat32 GetBoxMinRadius() const = 0;
247  virtual ndFloat32 GetBoxMaxRadius() const = 0;
248 
249  virtual void CalculateAabb(const ndMatrix& matrix, ndVector& p0, ndVector& p1) const = 0;
250  virtual ndVector SupportVertex(const ndVector& dir, ndInt32* const vertexIndex) const = 0;
251  virtual ndVector SupportVertexSpecialProjectPoint(const ndVector& point, const ndVector& dir) const = 0;
252  virtual ndVector SupportVertexSpecial(const ndVector& dir, ndFloat32 skinMargin, ndInt32* const vertexIndex) const = 0;
253  virtual ndInt32 CalculatePlaneIntersection(const ndVector& normal, const ndVector& point, ndVector* const contactsOut) const = 0;
254  virtual ndVector CalculateVolumeIntegral(const ndMatrix& globalMatrix, const ndVector& globalPlane, const ndShapeInstance& parentScale) const = 0;
255  virtual ndFloat32 RayCast(ndRayCastNotify& callback, const ndVector& localP0, const ndVector& localP1, ndFloat32 maxT, const ndBody* const body, ndContactPoint& contactOut) const = 0;
256 
257  virtual ndMatrix CalculateInertiaAndCenterOfMass(const ndMatrix& alignMatrix, const ndVector& localScale, const ndMatrix& matrix) const;
258  virtual ndFloat32 CalculateMassProperties(const ndMatrix& offset, ndVector& inertia, ndVector& crossInertia, ndVector& centerOfMass) const;
259 
260  D_COLLISION_API virtual void Save(const ndLoadSaveBase::ndSaveDescriptor& desc) const;
261 
262  protected:
263  D_COLLISION_API ndShape(ndShapeID id);
264  D_COLLISION_API ndShape (const ndShape& source);
265 
266  ndVector m_inertia;
267  ndVector m_crossInertia;
268  ndVector m_centerOfMass;
269  ndVector m_boxSize;
270  ndVector m_boxOrigin;
271  mutable ndAtomic<ndInt32> m_refCount;
272  ndShapeID m_collisionId;
273  static ndVector m_flushZero;
274 
275 } D_GCC_NEWTON_ALIGN_32;
276 
277 inline ndInt32 ndShape::GetConvexVertexCount() const
278 {
279  return 0;
280 }
281 
282 inline ndFloat32 ndShape::CalculateMassProperties(const ndMatrix&, ndVector&, ndVector&, ndVector&) const
283 {
284  ndAssert(0);
285  return 0;
286 }
287 
288 inline ndMatrix ndShape::CalculateInertiaAndCenterOfMass(const ndMatrix&, const ndVector&, const ndMatrix&) const
289 {
290  ndAssert(0);
291  return ndGetZeroMatrix();
292 }
293 
294 inline ndVector ndShape::GetObbOrigin() const
295 {
296  return m_boxOrigin;
297 }
298 
299 inline ndVector ndShape::GetObbSize() const
300 {
301  return m_boxSize;
302 }
303 
304 inline ndFloat32 ndShape::GetUmbraClipSize() const
305 {
306  return ndFloat32(3.0f) * GetBoxMaxRadius();
307 }
308 
309 #endif
310 
311 
ndShapeMaterial::nData
Definition: ndShape.h:89
ndBoxInfo
Definition: ndShape.h:102
ndConeInfo
Definition: ndShape.h:133
ndCylinderInfo
Definition: ndShape.h:119
ndShapeMaterial::dExtraData
Definition: ndShape.h:95
ndCompoundInfo
Definition: ndShape.h:153
ndConvexHullInfo
Definition: ndShape.h:145
ndShapeStaticMesh
Definition: ndShapeStaticMesh.h:32
ndBody
Definition: ndBody.h:43
ndShapeMaterial
Definition: ndShape.h:80
ndShapeNull
Definition: ndShapeNull.h:29
ndHeighfieldInfo
Definition: ndShape.h:169
ndShapeChamferCylinder
Definition: ndShapeChamferCylinder.h:32
ndShape
Definition: ndShape.h:209
ndShapeStaticProceduralMesh
Definition: ndShapeStaticProceduralMesh.h:29
ndShapeHeightfield
Definition: ndShapeHeightfield.h:29
ndShapeConvexPolygon
Definition: ndShapeConvexPolygon.h:32
ndRayCastNotify
Definition: ndRayCastNotify.h:31
ndShapeConvex
Definition: ndShapeConvex.h:34
ndContactPoint
Definition: ndContact.h:39
ndCapsuleInfo
Definition: ndShape.h:126
ndShapeCone
Definition: ndShapeCone.h:31
ndSphereInfo
Definition: ndShape.h:114
ndShapePoint
Definition: ndShapePoint.h:30
ndShapeCapsule
Definition: ndShapeCapsule.h:29
ndMatrix
Definition: ndMatrix.h:42
ndCollisionBvhInfo
Definition: ndShape.h:163
ndPointInfo
Definition: ndShape.h:109
ndShapeDebugNotify
Definition: ndShapeInstance.h:38
ndShapeBox
Definition: ndShapeBox.h:29
ndShapeInfo
Definition: ndShape.h:182
ndContainersFreeListAlloc
Definition: ndContainersAlloc.h:60
ndProceduralInfo
Definition: ndShape.h:158
ndShapeCompound
Definition: ndShapeCompound.h:33
ndShapeStatic_bvh
Definition: ndShapeStatic_bvh.h:29
ndShapeSphere
Definition: ndShapeSphere.h:31
ndShapeInstance
Definition: ndShapeInstance.h:62
ndChamferCylinderInfo
Definition: ndShape.h:139
ndLoadSaveBase::ndSaveDescriptor
Definition: ndSaveLoadSytem.h:93
ndVector
Definition: ndVectorArmNeon.h:41
ndShapeCylinder
Definition: ndShapeCylinder.h:31
ndAtomic< ndInt32 >