btConvexHull.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Stan Melax Convex Hull Computation
  3. Copyright (c) 2008 Stan Melax http://www.melax.com/
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. ///includes modifications/improvements by John Ratcliff, see BringOutYourDead below.
  14. #ifndef BT_CD_HULL_H
  15. #define BT_CD_HULL_H
  16. #include "btVector3.h"
  17. #include "btAlignedObjectArray.h"
  18. typedef btAlignedObjectArray<unsigned int> TUIntArray;
  19. class HullResult
  20. {
  21. public:
  22. HullResult(void)
  23. {
  24. mPolygons = true;
  25. mNumOutputVertices = 0;
  26. mNumFaces = 0;
  27. mNumIndices = 0;
  28. }
  29. bool mPolygons; // true if indices represents polygons, false indices are triangles
  30. unsigned int mNumOutputVertices; // number of vertices in the output hull
  31. btAlignedObjectArray<btVector3> m_OutputVertices; // array of vertices
  32. unsigned int mNumFaces; // the number of faces produced
  33. unsigned int mNumIndices; // the total number of indices
  34. btAlignedObjectArray<unsigned int> m_Indices; // pointer to indices.
  35. // If triangles, then indices are array indexes into the vertex list.
  36. // If polygons, indices are in the form (number of points in face) (p1, p2, p3, ..) etc..
  37. };
  38. enum HullFlag
  39. {
  40. QF_TRIANGLES = (1 << 0), // report results as triangles, not polygons.
  41. QF_REVERSE_ORDER = (1 << 1), // reverse order of the triangle indices.
  42. QF_DEFAULT = QF_TRIANGLES
  43. };
  44. class HullDesc
  45. {
  46. public:
  47. HullDesc(void)
  48. {
  49. mFlags = QF_DEFAULT;
  50. mVcount = 0;
  51. mVertices = 0;
  52. mVertexStride = sizeof(btVector3);
  53. mNormalEpsilon = 0.001f;
  54. mMaxVertices = 4096; // maximum number of points to be considered for a convex hull.
  55. mMaxFaces = 4096;
  56. };
  57. HullDesc(HullFlag flag,
  58. unsigned int vcount,
  59. const btVector3* vertices,
  60. unsigned int stride = sizeof(btVector3))
  61. {
  62. mFlags = flag;
  63. mVcount = vcount;
  64. mVertices = vertices;
  65. mVertexStride = stride;
  66. mNormalEpsilon = btScalar(0.001);
  67. mMaxVertices = 4096;
  68. }
  69. bool HasHullFlag(HullFlag flag) const
  70. {
  71. if (mFlags & flag) return true;
  72. return false;
  73. }
  74. void SetHullFlag(HullFlag flag)
  75. {
  76. mFlags |= flag;
  77. }
  78. void ClearHullFlag(HullFlag flag)
  79. {
  80. mFlags &= ~flag;
  81. }
  82. unsigned int mFlags; // flags to use when generating the convex hull.
  83. unsigned int mVcount; // number of vertices in the input point cloud
  84. const btVector3* mVertices; // the array of vertices.
  85. unsigned int mVertexStride; // the stride of each vertex, in bytes.
  86. btScalar mNormalEpsilon; // the epsilon for removing duplicates. This is a normalized value, if normalized bit is on.
  87. unsigned int mMaxVertices; // maximum number of vertices to be considered for the hull!
  88. unsigned int mMaxFaces;
  89. };
  90. enum HullError
  91. {
  92. QE_OK, // success!
  93. QE_FAIL // failed.
  94. };
  95. class btPlane
  96. {
  97. public:
  98. btVector3 normal;
  99. btScalar dist; // distance below origin - the D from plane equasion Ax+By+Cz+D=0
  100. btPlane(const btVector3& n, btScalar d) : normal(n), dist(d) {}
  101. btPlane() : normal(), dist(0) {}
  102. };
  103. class ConvexH
  104. {
  105. public:
  106. class HalfEdge
  107. {
  108. public:
  109. short ea; // the other half of the edge (index into edges list)
  110. unsigned char v; // the vertex at the start of this edge (index into vertices list)
  111. unsigned char p; // the facet on which this edge lies (index into facets list)
  112. HalfEdge() {}
  113. HalfEdge(short _ea, unsigned char _v, unsigned char _p) : ea(_ea), v(_v), p(_p) {}
  114. };
  115. ConvexH()
  116. {
  117. }
  118. ~ConvexH()
  119. {
  120. }
  121. btAlignedObjectArray<btVector3> vertices;
  122. btAlignedObjectArray<HalfEdge> edges;
  123. btAlignedObjectArray<btPlane> facets;
  124. ConvexH(int vertices_size, int edges_size, int facets_size);
  125. };
  126. class int4
  127. {
  128. public:
  129. int x, y, z, w;
  130. int4(){};
  131. int4(int _x, int _y, int _z, int _w)
  132. {
  133. x = _x;
  134. y = _y;
  135. z = _z;
  136. w = _w;
  137. }
  138. const int& operator[](int i) const { return (&x)[i]; }
  139. int& operator[](int i) { return (&x)[i]; }
  140. };
  141. class PHullResult
  142. {
  143. public:
  144. PHullResult(void)
  145. {
  146. mVcount = 0;
  147. mIndexCount = 0;
  148. mFaceCount = 0;
  149. mVertices = 0;
  150. }
  151. unsigned int mVcount;
  152. unsigned int mIndexCount;
  153. unsigned int mFaceCount;
  154. btVector3* mVertices;
  155. TUIntArray m_Indices;
  156. };
  157. ///The HullLibrary class can create a convex hull from a collection of vertices, using the ComputeHull method.
  158. ///The btShapeHull class uses this HullLibrary to create a approximate convex mesh given a general (non-polyhedral) convex shape.
  159. class HullLibrary
  160. {
  161. btAlignedObjectArray<class btHullTriangle*> m_tris;
  162. public:
  163. btAlignedObjectArray<int> m_vertexIndexMapping;
  164. HullError CreateConvexHull(const HullDesc& desc, // describes the input request
  165. HullResult& result); // contains the resulst
  166. HullError ReleaseResult(HullResult& result); // release memory allocated for this result, we are done with it.
  167. private:
  168. bool ComputeHull(unsigned int vcount, const btVector3* vertices, PHullResult& result, unsigned int vlimit);
  169. class btHullTriangle* allocateTriangle(int a, int b, int c);
  170. void deAllocateTriangle(btHullTriangle*);
  171. void b2bfix(btHullTriangle* s, btHullTriangle* t);
  172. void removeb2b(btHullTriangle* s, btHullTriangle* t);
  173. void checkit(btHullTriangle* t);
  174. btHullTriangle* extrudable(btScalar epsilon);
  175. int calchull(btVector3* verts, int verts_count, TUIntArray& tris_out, int& tris_count, int vlimit);
  176. int calchullgen(btVector3* verts, int verts_count, int vlimit);
  177. int4 FindSimplex(btVector3* verts, int verts_count, btAlignedObjectArray<int>& allow);
  178. class ConvexH* ConvexHCrop(ConvexH& convex, const btPlane& slice);
  179. void extrude(class btHullTriangle* t0, int v);
  180. ConvexH* test_cube();
  181. //BringOutYourDead (John Ratcliff): When you create a convex hull you hand it a large input set of vertices forming a 'point cloud'.
  182. //After the hull is generated it give you back a set of polygon faces which index the *original* point cloud.
  183. //The thing is, often times, there are many 'dead vertices' in the point cloud that are on longer referenced by the hull.
  184. //The routine 'BringOutYourDead' find only the referenced vertices, copies them to an new buffer, and re-indexes the hull so that it is a minimal representation.
  185. void BringOutYourDead(const btVector3* verts, unsigned int vcount, btVector3* overts, unsigned int& ocount, unsigned int* indices, unsigned indexcount);
  186. bool CleanupVertices(unsigned int svcount,
  187. const btVector3* svertices,
  188. unsigned int stride,
  189. unsigned int& vcount, // output number of vertices
  190. btVector3* vertices, // location to store the results.
  191. btScalar normalepsilon,
  192. btVector3& scale);
  193. };
  194. #endif //BT_CD_HULL_H