NvStanHull.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #ifndef NV_STAN_HULL_H
  2. #define NV_STAN_HULL_H
  3. /*
  4. NvStanHull.h : A convex hull generator written by Stan Melax
  5. */
  6. /*!
  7. **
  8. ** Copyright (c) 2009 by John W. Ratcliff mailto:[email protected]
  9. **
  10. ** Portions of this source has been released with the PhysXViewer application, as well as
  11. ** Rocket, CreateDynamics, ODF, and as a number of sample code snippets.
  12. **
  13. ** If you find this code useful or you are feeling particularily generous I would
  14. ** ask that you please go to http://www.amillionpixels.us and make a donation
  15. ** to Troy DeMolay.
  16. **
  17. ** DeMolay is a youth group for young men between the ages of 12 and 21.
  18. ** It teaches strong moral principles, as well as leadership skills and
  19. ** public speaking. The donations page uses the 'pay for pixels' paradigm
  20. ** where, in this case, a pixel is only a single penny. Donations can be
  21. ** made for as small as $4 or as high as a $100 block. Each person who donates
  22. ** will get a link to their own site as well as acknowledgement on the
  23. ** donations blog located here http://www.amillionpixels.blogspot.com/
  24. **
  25. ** If you wish to contact me you can use the following methods:
  26. **
  27. ** Skype ID: jratcliff63367
  28. ** Yahoo: jratcliff63367
  29. ** AOL: jratcliff1961
  30. ** email: [email protected]
  31. **
  32. **
  33. ** The MIT license:
  34. **
  35. ** Permission is hereby granted, free of charge, to any person obtaining a copy
  36. ** of this software and associated documentation files (the "Software"), to deal
  37. ** in the Software without restriction, including without limitation the rights
  38. ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  39. ** copies of the Software, and to permit persons to whom the Software is furnished
  40. ** to do so, subject to the following conditions:
  41. **
  42. ** The above copyright notice and this permission notice shall be included in all
  43. ** copies or substantial portions of the Software.
  44. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  45. ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  46. ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  47. ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  48. ** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  49. ** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  50. */
  51. #include "NvUserMemAlloc.h"
  52. namespace CONVEX_DECOMPOSITION
  53. {
  54. class HullResult
  55. {
  56. public:
  57. HullResult(void)
  58. {
  59. mPolygons = true;
  60. mNumOutputVertices = 0;
  61. mOutputVertices = 0;
  62. mNumFaces = 0;
  63. mNumIndices = 0;
  64. mIndices = 0;
  65. }
  66. bool mPolygons; // true if indices represents polygons, false indices are triangles
  67. NxU32 mNumOutputVertices; // number of vertices in the output hull
  68. NxF32 *mOutputVertices; // array of vertices, 3 floats each x,y,z
  69. NxU32 mNumFaces; // the number of faces produced
  70. NxU32 mNumIndices; // the total number of indices
  71. NxU32 *mIndices; // pointer to indices.
  72. // If triangles, then indices are array indexes into the vertex list.
  73. // If polygons, indices are in the form (number of points in face) (p1, p2, p3, ..) etc..
  74. };
  75. enum HullFlag
  76. {
  77. QF_TRIANGLES = (1<<0), // report results as triangles, not polygons.
  78. QF_REVERSE_ORDER = (1<<1), // reverse order of the triangle indices.
  79. QF_SKIN_WIDTH = (1<<2), // extrude hull based on this skin width
  80. QF_DEFAULT = (QF_TRIANGLES | QF_SKIN_WIDTH)
  81. };
  82. class HullDesc
  83. {
  84. public:
  85. HullDesc(void)
  86. {
  87. mFlags = QF_DEFAULT;
  88. mVcount = 0;
  89. mVertices = 0;
  90. mVertexStride = 0;
  91. mNormalEpsilon = 0.001f;
  92. mMaxVertices = 4096; // maximum number of points to be considered for a convex hull.
  93. mSkinWidth = 0.01f; // default is one centimeter
  94. };
  95. HullDesc(HullFlag flag,
  96. NxU32 vcount,
  97. const NxF32 *vertices,
  98. NxU32 stride)
  99. {
  100. mFlags = flag;
  101. mVcount = vcount;
  102. mVertices = vertices;
  103. mVertexStride = stride;
  104. mNormalEpsilon = 0.001f;
  105. mMaxVertices = 4096;
  106. mSkinWidth = 0.01f; // default is one centimeter
  107. }
  108. bool HasHullFlag(HullFlag flag) const
  109. {
  110. if ( mFlags & flag ) return true;
  111. return false;
  112. }
  113. void SetHullFlag(HullFlag flag)
  114. {
  115. mFlags|=flag;
  116. }
  117. void ClearHullFlag(HullFlag flag)
  118. {
  119. mFlags&=~flag;
  120. }
  121. NxU32 mFlags; // flags to use when generating the convex hull.
  122. NxU32 mVcount; // number of vertices in the input point cloud
  123. const NxF32 *mVertices; // the array of vertices.
  124. NxU32 mVertexStride; // the stride of each vertex, in bytes.
  125. NxF32 mNormalEpsilon; // the epsilon for removing duplicates. This is a normalized value, if normalized bit is on.
  126. NxF32 mSkinWidth;
  127. NxU32 mMaxVertices; // maximum number of vertices to be considered for the hull!
  128. };
  129. enum HullError
  130. {
  131. QE_OK, // success!
  132. QE_FAIL, // failed.
  133. QE_NOT_READY,
  134. };
  135. // This class is used when converting a convex hull into a triangle mesh.
  136. class ConvexHullVertex
  137. {
  138. public:
  139. NxF32 mPos[3];
  140. NxF32 mNormal[3];
  141. NxF32 mTexel[2];
  142. };
  143. // A virtual interface to receive the triangles from the convex hull.
  144. class ConvexHullTriangleInterface
  145. {
  146. public:
  147. virtual void ConvexHullTriangle(const ConvexHullVertex &v1,const ConvexHullVertex &v2,const ConvexHullVertex &v3) = 0;
  148. };
  149. class HullLibrary
  150. {
  151. public:
  152. HullError CreateConvexHull(const HullDesc &desc, // describes the input request
  153. HullResult &result); // contains the resulst
  154. HullError ReleaseResult(HullResult &result); // release memory allocated for this result, we are done with it.
  155. HullError CreateTriangleMesh(HullResult &answer,ConvexHullTriangleInterface *iface);
  156. private:
  157. NxF32 ComputeNormal(NxF32 *n,const NxF32 *A,const NxF32 *B,const NxF32 *C);
  158. void AddConvexTriangle(ConvexHullTriangleInterface *callback,const NxF32 *p1,const NxF32 *p2,const NxF32 *p3);
  159. void BringOutYourDead(const NxF32 *verts,NxU32 vcount, NxF32 *overts,NxU32 &ocount,NxU32 *indices,NxU32 indexcount);
  160. bool CleanupVertices(NxU32 svcount,
  161. const NxF32 *svertices,
  162. NxU32 stride,
  163. NxU32 &vcount, // output number of vertices
  164. NxF32 *vertices, // location to store the results.
  165. NxF32 normalepsilon,
  166. NxF32 *scale);
  167. };
  168. }; // end of namespace
  169. #endif