Newton Dynamics  4.00
ndShapeHeightfield.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_HEIGHT_FIELD__
23 #define __ND_SHAPE_HEIGHT_FIELD__
24 
25 #include "ndCollisionStdafx.h"
26 #include "ndShapeStaticMesh.h"
27 
29 {
30  public:
31  class ndTriangle
32  {
33  public:
34  ndInt32 m_i0;
35  ndInt32 m_i1;
36  ndInt32 m_i2;
37  ndInt32 m_material;
38  ndInt32 m_normal;
39  ndInt32 m_normal_edge01;
40  ndInt32 m_normal_edge12;
41  ndInt32 m_normal_edge20;
42  ndInt32 m_area;
43  };
44 
45  class ndGridQuad
46  {
47  public:
48  ndTriangle m_triangle0;
49  ndTriangle m_triangle1;
50  };
51 
52  enum ndGridConstruction
53  {
54  m_normalDiagonals = 0,
55  m_invertedDiagonals,
56  };
57 
58  D_CLASS_REFLECTION(ndShapeHeightfield);
59  D_COLLISION_API ndShapeHeightfield(
60  ndInt32 width, ndInt32 height, ndGridConstruction constructionMode,
61  ndFloat32 horizontalScale_x, ndFloat32 horizontalScale_z);
62  D_COLLISION_API ndShapeHeightfield(const ndLoadSaveBase::ndLoadDescriptor& desc);
63  D_COLLISION_API virtual ~ndShapeHeightfield();
64 
65  ndArray<ndReal>& GetElevationMap();
66  const ndArray<ndReal>& GetElevationMap() const;
67 
68  D_COLLISION_API void UpdateElevationMapAabb();
69  D_COLLISION_API void GetLocalAabb(const ndVector& p0, const ndVector& p1, ndVector& boxP0, ndVector& boxP1) const;
70 
71  protected:
72  virtual ndShapeInfo GetShapeInfo() const;
73  virtual ndShapeHeightfield* GetAsShapeHeightfield() { return this; }
74  virtual void DebugShape(const ndMatrix& matrix, ndShapeDebugNotify& debugCallback) const;
75  virtual ndFloat32 RayCast(ndRayCastNotify& callback, const ndVector& localP0, const ndVector& localP1, ndFloat32 maxT, const ndBody* const body, ndContactPoint& contactOut) const;
76  virtual void GetCollidingFaces(ndPolygonMeshDesc* const data) const;
77  virtual void Save(const ndLoadSaveBase::ndSaveDescriptor& desc) const;
78 
79  private:
80  void CalculateLocalObb();
81  ndInt32 FastInt(ndFloat32 x) const;
82  const ndInt32* GetIndexList() const;
83  void CalculateMinExtend2d(const ndVector& p0, const ndVector& p1, ndVector& boxP0, ndVector& boxP1) const;
84  void CalculateMinExtend3d(const ndVector& p0, const ndVector& p1, ndVector& boxP0, ndVector& boxP1) const;
85  ndFloat32 RayCastCell(const ndFastRay& ray, ndInt32 xIndex0, ndInt32 zIndex0, ndVector& normalOut, ndFloat32 maxT) const;
86  void CalculateMinAndMaxElevation(ndInt32 x0, ndInt32 x1, ndInt32 z0, ndInt32 z1, ndFloat32& minHeight, ndFloat32& maxHeight) const;
87 
88  ndVector m_minBox;
89  ndVector m_maxBox;
90  ndArray<ndInt8> m_atributeMap;
91  ndArray<ndReal> m_elevationMap;
92  ndFloat32 m_horizontalScale_x;
93  ndFloat32 m_horizontalScale_z;
94  ndFloat32 m_horizontalScaleInv_x;
95  ndFloat32 m_horizontalScaleInv_z;
96  ndInt32 m_width;
97  ndInt32 m_height;
98  ndGridConstruction m_diagonalMode;
99 
100  static ndVector m_yMask;
101  static ndVector m_padding;
102  static ndVector m_elevationPadding;
103  static ndInt32 m_cellIndices[][4];
104  friend class ndContactSolver;
105 };
106 
107 inline ndArray<ndReal>& ndShapeHeightfield::GetElevationMap()
108 {
109  return m_elevationMap;
110 }
111 
112 inline const ndArray<ndReal>& ndShapeHeightfield::GetElevationMap() const
113 {
114  return m_elevationMap;
115 }
116 
117 inline ndInt32 ndShapeHeightfield::FastInt(ndFloat32 x) const
118 {
119  ndInt32 i = ndInt32(x);
120  if (ndFloat32(i) > x)
121  {
122  i--;
123  }
124  return i;
125 }
126 
127 #endif
ndPolygonMeshDesc
Definition: ndPolygonMeshDesc.h:35
ndArray< ndReal >
ndShapeStaticMesh
Definition: ndShapeStaticMesh.h:32
ndBody
Definition: ndBody.h:43
ndShapeHeightfield
Definition: ndShapeHeightfield.h:29
ndContactSolver
Definition: ndContactSolver.h:60
ndRayCastNotify
Definition: ndRayCastNotify.h:31
ndContactPoint
Definition: ndContact.h:39
ndShapeHeightfield::ndGridQuad
Definition: ndShapeHeightfield.h:46
ndMatrix
Definition: ndMatrix.h:42
ndShapeDebugNotify
Definition: ndShapeInstance.h:38
ndShapeHeightfield::ndTriangle
Definition: ndShapeHeightfield.h:32
ndFastRay
Definition: ndFastRay.h:48
ndShapeInfo
Definition: ndShape.h:182
ndLoadSaveBase::ndLoadDescriptor
Definition: ndSaveLoadSytem.h:59
ndLoadSaveBase::ndSaveDescriptor
Definition: ndSaveLoadSytem.h:93
ndVector
Definition: ndVectorArmNeon.h:41