ConvexDecomposition.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #ifndef CONVEX_DECOMPOSITION_H
  2. #define CONVEX_DECOMPOSITION_H
  3. /*----------------------------------------------------------------------
  4. Copyright (c) 2004 Open Dynamics Framework Group
  5. www.physicstools.org
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification, are permitted provided
  8. that the following conditions are met:
  9. Redistributions of source code must retain the above copyright notice, this list of conditions
  10. and the following disclaimer.
  11. Redistributions in binary form must reproduce the above copyright notice,
  12. this list of conditions and the following disclaimer in the documentation
  13. and/or other materials provided with the distribution.
  14. Neither the name of the Open Dynamics Framework Group nor the names of its contributors may
  15. be used to endorse or promote products derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. -----------------------------------------------------------------------*/
  24. // http://codesuppository.blogspot.com
  25. //
  26. // mailto: [email protected]
  27. //
  28. // http://www.amillionpixels.us
  29. //
  30. #ifdef _WIN32
  31. #include <memory.h> //memcpy
  32. #endif
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include "LinearMath/btAlignedObjectArray.h"
  36. extern unsigned int MAXDEPTH ;
  37. extern float CONCAVE_PERCENT ;
  38. extern float MERGE_PERCENT ;
  39. typedef btAlignedObjectArray< unsigned int > UintVector;
  40. namespace ConvexDecomposition
  41. {
  42. class ConvexResult
  43. {
  44. public:
  45. ConvexResult(void)
  46. {
  47. mHullVcount = 0;
  48. mHullVertices = 0;
  49. mHullTcount = 0;
  50. mHullIndices = 0;
  51. }
  52. ConvexResult(unsigned int hvcount,const float *hvertices,unsigned int htcount,const unsigned int *hindices)
  53. {
  54. mHullVcount = hvcount;
  55. if ( mHullVcount )
  56. {
  57. mHullVertices = new float[mHullVcount*sizeof(float)*3];
  58. memcpy(mHullVertices, hvertices, sizeof(float)*3*mHullVcount );
  59. }
  60. else
  61. {
  62. mHullVertices = 0;
  63. }
  64. mHullTcount = htcount;
  65. if ( mHullTcount )
  66. {
  67. mHullIndices = new unsigned int[sizeof(unsigned int)*mHullTcount*3];
  68. memcpy(mHullIndices,hindices, sizeof(unsigned int)*mHullTcount*3 );
  69. }
  70. else
  71. {
  72. mHullIndices = 0;
  73. }
  74. }
  75. ConvexResult(const ConvexResult &r)
  76. {
  77. mHullVcount = r.mHullVcount;
  78. if ( mHullVcount )
  79. {
  80. mHullVertices = new float[mHullVcount*sizeof(float)*3];
  81. memcpy(mHullVertices, r.mHullVertices, sizeof(float)*3*mHullVcount );
  82. }
  83. else
  84. {
  85. mHullVertices = 0;
  86. }
  87. mHullTcount = r.mHullTcount;
  88. if ( mHullTcount )
  89. {
  90. mHullIndices = new unsigned int[sizeof(unsigned int)*mHullTcount*3];
  91. memcpy(mHullIndices, r.mHullIndices, sizeof(unsigned int)*mHullTcount*3 );
  92. }
  93. else
  94. {
  95. mHullIndices = 0;
  96. }
  97. }
  98. ~ConvexResult(void)
  99. {
  100. delete [] mHullVertices;
  101. delete [] mHullIndices;
  102. }
  103. // the convex hull.
  104. unsigned int mHullVcount;
  105. float * mHullVertices;
  106. unsigned int mHullTcount;
  107. unsigned int *mHullIndices;
  108. float mHullVolume; // the volume of the convex hull.
  109. float mOBBSides[3]; // the width, height and breadth of the best fit OBB
  110. float mOBBCenter[3]; // the center of the OBB
  111. float mOBBOrientation[4]; // the quaternion rotation of the OBB.
  112. float mOBBTransform[16]; // the 4x4 transform of the OBB.
  113. float mOBBVolume; // the volume of the OBB
  114. float mSphereRadius; // radius and center of best fit sphere
  115. float mSphereCenter[3];
  116. float mSphereVolume; // volume of the best fit sphere
  117. };
  118. class ConvexDecompInterface
  119. {
  120. public:
  121. virtual ~ConvexDecompInterface() {};
  122. virtual void ConvexDebugTri(const float *p1,const float *p2,const float *p3,unsigned int color) { };
  123. virtual void ConvexDebugPoint(const float *p,float dist,unsigned int color) { };
  124. virtual void ConvexDebugBound(const float *bmin,const float *bmax,unsigned int color) { };
  125. virtual void ConvexDebugOBB(const float *sides, const float *matrix,unsigned int color) { };
  126. virtual void ConvexDecompResult(ConvexResult &result) = 0;
  127. };
  128. // just to avoid passing a zillion parameters to the method the
  129. // options are packed into this descriptor.
  130. class DecompDesc
  131. {
  132. public:
  133. DecompDesc(void)
  134. {
  135. mVcount = 0;
  136. mVertices = 0;
  137. mTcount = 0;
  138. mIndices = 0;
  139. mDepth = 5;
  140. mCpercent = 5;
  141. mPpercent = 5;
  142. mMaxVertices = 32;
  143. mSkinWidth = 0;
  144. mCallback = 0;
  145. }
  146. // describes the input triangle.
  147. unsigned int mVcount; // the number of vertices in the source mesh.
  148. const float *mVertices; // start of the vertex position array. Assumes a stride of 3 floats.
  149. unsigned int mTcount; // the number of triangles in the source mesh.
  150. unsigned int *mIndices; // the indexed triangle list array (zero index based)
  151. // options
  152. unsigned int mDepth; // depth to split, a maximum of 10, generally not over 7.
  153. float mCpercent; // the concavity threshold percentage. 0=20 is reasonable.
  154. float mPpercent; // the percentage volume conservation threshold to collapse hulls. 0-30 is reasonable.
  155. // hull output limits.
  156. unsigned int mMaxVertices; // maximum number of vertices in the output hull. Recommended 32 or less.
  157. float mSkinWidth; // a skin width to apply to the output hulls.
  158. ConvexDecompInterface *mCallback; // the interface to receive back the results.
  159. };
  160. // perform approximate convex decomposition on a mesh.
  161. unsigned int performConvexDecomposition(const DecompDesc &desc); // returns the number of hulls produced.
  162. void calcConvexDecomposition(unsigned int vcount,
  163. const float *vertices,
  164. unsigned int tcount,
  165. const unsigned int *indices,
  166. ConvexDecompInterface *callback,
  167. float masterVolume,
  168. unsigned int depth);
  169. }
  170. #endif