heightfield.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. // dHeightfield Collider
  23. // Martijn Buijs 2006 http://home.planet.nl/~buijs512/
  24. // Based on Terrain & Cone contrib by:
  25. // Benoit CHAPEROT 2003-2004 http://www.jstarlab.com
  26. #ifndef _DHEIGHTFIELD_H_
  27. #define _DHEIGHTFIELD_H_
  28. //------------------------------------------------------------------------------
  29. #include <ode/common.h>
  30. #include "collision_kernel.h"
  31. #define HEIGHTFIELDMAXCONTACTPERCELL 10
  32. class HeightFieldVertex;
  33. class HeightFieldEdge;
  34. class HeightFieldTriangle;
  35. //
  36. // dxHeightfieldData
  37. //
  38. // Heightfield Data structure
  39. //
  40. struct dxHeightfieldData
  41. {
  42. dReal m_fWidth; // World space heightfield dimension on X axis
  43. dReal m_fDepth; // World space heightfield dimension on Z axis
  44. dReal m_fSampleWidth; // Vertex spacing on X axis edge (== m_vWidth / (m_nWidthSamples-1))
  45. dReal m_fSampleDepth; // Vertex spacing on Z axis edge (== m_vDepth / (m_nDepthSamples-1))
  46. dReal m_fSampleZXAspect; // Relation of Z axis spacing to X axis spacing (== m_fSampleDepth / m_fSampleWidth)
  47. dReal m_fInvSampleWidth; // Cache of inverse Vertex count on X axis edge (== m_vWidth / (m_nWidthSamples-1))
  48. dReal m_fInvSampleDepth; // Cache of inverse Vertex count on Z axis edge (== m_vDepth / (m_nDepthSamples-1))
  49. dReal m_fHalfWidth; // Cache of half of m_fWidth
  50. dReal m_fHalfDepth; // Cache of half of m_fDepth
  51. dReal m_fMinHeight; // Min sample height value (scaled and offset)
  52. dReal m_fMaxHeight; // Max sample height value (scaled and offset)
  53. dReal m_fThickness; // Surface thickness (added to bottom AABB)
  54. dReal m_fScale; // Sample value multiplier
  55. dReal m_fOffset; // Vertical sample offset
  56. int m_nWidthSamples; // Vertex count on X axis edge (number of samples)
  57. int m_nDepthSamples; // Vertex count on Z axis edge (number of samples)
  58. int m_bCopyHeightData; // Do we own the sample data?
  59. int m_bWrapMode; // Heightfield wrapping mode (0=finite, 1=infinite)
  60. int m_nGetHeightMode; // GetHeight mode ( 0=callback, 1=byte, 2=short, 3=float )
  61. const void* m_pHeightData; // Sample data array
  62. void* m_pUserData; // Callback user data
  63. dContactGeom m_contacts[HEIGHTFIELDMAXCONTACTPERCELL];
  64. dHeightfieldGetHeight* m_pGetHeightCallback; // Callback pointer.
  65. dxHeightfieldData();
  66. ~dxHeightfieldData();
  67. void SetData( int nWidthSamples, int nDepthSamples,
  68. dReal fWidth, dReal fDepth,
  69. dReal fScale, dReal fOffset,
  70. dReal fThickness, int bWrapMode );
  71. void ComputeHeightBounds();
  72. bool IsOnHeightfield2 ( const HeightFieldVertex * const CellCorner,
  73. const dReal * const pos, const bool isABC) const;
  74. dReal GetHeight(int x, int z);
  75. dReal GetHeight(dReal x, dReal z);
  76. };
  77. typedef int HeightFieldVertexCoords[2];
  78. class HeightFieldVertex
  79. {
  80. public:
  81. HeightFieldVertex(){};
  82. dVector3 vertex;
  83. HeightFieldVertexCoords coords;
  84. bool state;
  85. };
  86. class HeightFieldEdge
  87. {
  88. public:
  89. HeightFieldEdge(){};
  90. HeightFieldVertex *vertices[2];
  91. };
  92. class HeightFieldTriangle
  93. {
  94. public:
  95. HeightFieldTriangle(){};
  96. inline void setMinMax()
  97. {
  98. maxAAAB = vertices[0]->vertex[1] > vertices[1]->vertex[1] ? vertices[0]->vertex[1] : vertices[1]->vertex[1];
  99. maxAAAB = vertices[2]->vertex[1] > maxAAAB ? vertices[2]->vertex[1] : maxAAAB;
  100. };
  101. HeightFieldVertex *vertices[3];
  102. dReal planeDef[4];
  103. dReal maxAAAB;
  104. bool isUp;
  105. bool state;
  106. };
  107. class HeightFieldPlane
  108. {
  109. public:
  110. HeightFieldPlane():
  111. trianglelist(0),
  112. trianglelistReservedSize(0),
  113. trianglelistCurrentSize(0)
  114. {
  115. }
  116. ~HeightFieldPlane()
  117. {
  118. delete [] trianglelist;
  119. }
  120. inline void setMinMax()
  121. {
  122. const sizeint asize = trianglelistCurrentSize;
  123. if (asize > 0)
  124. {
  125. maxAAAB = trianglelist[0]->maxAAAB;
  126. for (sizeint k = 1; asize > k; k++)
  127. {
  128. if (trianglelist[k]->maxAAAB > maxAAAB)
  129. maxAAAB = trianglelist[k]->maxAAAB;
  130. }
  131. }
  132. };
  133. void resetTriangleListSize(const sizeint newSize)
  134. {
  135. if (trianglelistReservedSize < newSize)
  136. {
  137. delete [] trianglelist;
  138. trianglelistReservedSize = newSize;
  139. trianglelist = new HeightFieldTriangle *[newSize];
  140. }
  141. trianglelistCurrentSize = 0;
  142. }
  143. void addTriangle(HeightFieldTriangle *tri)
  144. {
  145. dIASSERT(trianglelistCurrentSize < trianglelistReservedSize);
  146. trianglelist[trianglelistCurrentSize++] = tri;
  147. }
  148. HeightFieldTriangle **trianglelist;
  149. sizeint trianglelistReservedSize;
  150. sizeint trianglelistCurrentSize;
  151. dReal maxAAAB;
  152. dReal planeDef[4];
  153. };
  154. //
  155. // dxHeightfield
  156. //
  157. // Heightfield geom structure
  158. //
  159. struct dxHeightfield : public dxGeom
  160. {
  161. dxHeightfieldData* m_p_data;
  162. dxHeightfield( dSpaceID space, dHeightfieldDataID data, int bPlaceable );
  163. ~dxHeightfield();
  164. void computeAABB();
  165. int dCollideHeightfieldZone( const int minX, const int maxX, const int minZ, const int maxZ,
  166. dxGeom *o2, const int numMaxContacts,
  167. int flags, dContactGeom *contact, int skip );
  168. enum
  169. {
  170. TEMP_PLANE_BUFFER_ELEMENT_COUNT_ALIGNMENT = 4,
  171. TEMP_HEIGHT_BUFFER_ELEMENT_COUNT_ALIGNMENT_X = 4,
  172. TEMP_HEIGHT_BUFFER_ELEMENT_COUNT_ALIGNMENT_Z = 4,
  173. TEMP_TRIANGLE_BUFFER_ELEMENT_COUNT_ALIGNMENT = 1 // Triangles are easy to reallocate and hard to predict
  174. };
  175. static inline sizeint AlignBufferSize(sizeint value, sizeint alignment) { dIASSERT((alignment & (alignment - 1)) == 0); return (value + (alignment - 1)) & ~(alignment - 1); }
  176. void allocateTriangleBuffer(sizeint numTri);
  177. void resetTriangleBuffer();
  178. void allocatePlaneBuffer(sizeint numTri);
  179. void resetPlaneBuffer();
  180. void allocateHeightBuffer(sizeint numX, sizeint numZ);
  181. void resetHeightBuffer();
  182. void sortPlanes(const sizeint numPlanes);
  183. HeightFieldPlane **tempPlaneBuffer;
  184. HeightFieldPlane *tempPlaneInstances;
  185. sizeint tempPlaneBufferSize;
  186. HeightFieldTriangle *tempTriangleBuffer;
  187. sizeint tempTriangleBufferSize;
  188. HeightFieldVertex **tempHeightBuffer;
  189. HeightFieldVertex *tempHeightInstances;
  190. sizeint tempHeightBufferSizeX;
  191. sizeint tempHeightBufferSizeZ;
  192. };
  193. //------------------------------------------------------------------------------
  194. #endif //_DHEIGHTFIELD_H_