btGImpactConvexDecompositionShape.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. This source file is part of GIMPACT Library.
  3. For the latest info, see http://gimpact.sourceforge.net/
  4. Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
  5. email: [email protected]
  6. This software is provided 'as-is', without any express or implied warranty.
  7. In no event will the authors be held liable for any damages arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it freely,
  10. subject to the following restrictions:
  11. 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.
  12. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. 3. This notice may not be removed or altered from any source distribution.
  14. */
  15. #include "btGImpactConvexDecompositionShape.h"
  16. #include "BulletCollision/CollisionShapes/btConvexHullShape.h"
  17. #include "ConvexBuilder.h"
  18. class GIM_ConvexDecomposition : public ConvexDecomposition::ConvexDecompInterface
  19. {
  20. protected:
  21. btGImpactConvexDecompositionShape * m_compoundShape;
  22. btAlignedObjectArray<btCollisionShape*> m_convexShapes;
  23. public:
  24. int mBaseCount;
  25. int mHullCount;
  26. bool m_transformSubShapes;
  27. GIM_ConvexDecomposition(btGImpactConvexDecompositionShape * compoundShape,bool transformSubShapes)
  28. {
  29. mBaseCount = 0;
  30. mHullCount = 0;
  31. m_compoundShape = compoundShape;
  32. m_transformSubShapes = transformSubShapes;
  33. }
  34. virtual ~GIM_ConvexDecomposition()
  35. {
  36. int i;
  37. for (i=0;i<m_convexShapes.size();i++)
  38. {
  39. btCollisionShape* shape = m_convexShapes[i];
  40. delete shape;
  41. }
  42. }
  43. virtual void ConvexDecompResult(ConvexDecomposition::ConvexResult &result)
  44. {
  45. //calc centroid, to shift vertices around center of mass
  46. btVector3 centroid(0,0,0);
  47. btAlignedObjectArray<btVector3> vertices;
  48. if(m_transformSubShapes)
  49. {
  50. //const unsigned int *src = result.mHullIndices;
  51. for (unsigned int i=0; i<result.mHullVcount; i++)
  52. {
  53. btVector3 vertex(result.mHullVertices[i*3],result.mHullVertices[i*3+1],result.mHullVertices[i*3+2]);
  54. centroid += vertex;
  55. }
  56. centroid *= 1.f/(float(result.mHullVcount) );
  57. }
  58. // collect vertices
  59. for (unsigned int i=0; i<result.mHullVcount; i++)
  60. {
  61. btVector3 vertex(result.mHullVertices[i*3],result.mHullVertices[i*3+1],result.mHullVertices[i*3+2]);
  62. if(m_transformSubShapes)
  63. {
  64. vertex -= centroid ;
  65. }
  66. vertices.push_back(vertex);
  67. }
  68. // build convex shape
  69. btCollisionShape* convexShape = new btConvexHullShape(
  70. &(vertices[0].getX()),vertices.size(),sizeof(btVector3));
  71. m_convexShapes.push_back(convexShape);
  72. convexShape->setMargin(m_compoundShape->getMargin());
  73. if(m_transformSubShapes)
  74. {
  75. btTransform trans;
  76. trans.setIdentity();
  77. trans.setOrigin(centroid);
  78. // add convex shape
  79. m_compoundShape->addChildShape(trans,convexShape);
  80. }
  81. else
  82. {
  83. btTransform trans;
  84. trans.setIdentity();
  85. //trans.setOrigin(centroid);
  86. // add convex shape
  87. m_compoundShape->addChildShape(trans,convexShape);
  88. //m_compoundShape->addChildShape(convexShape);
  89. }
  90. }
  91. void processDecomposition(int part)
  92. {
  93. btGImpactMeshShapePart::TrimeshPrimitiveManager * trimeshInterface =
  94. m_compoundShape->getTrimeshInterface(part);
  95. trimeshInterface->lock();
  96. //collect vertices
  97. btAlignedObjectArray<float> vertices;
  98. vertices.reserve(trimeshInterface->get_vertex_count()*3);
  99. for(int vi = 0;vi<trimeshInterface->get_vertex_count();vi++)
  100. {
  101. btVector3 vec;
  102. trimeshInterface->get_vertex(vi,vec);
  103. vertices.push_back(vec[0]);
  104. vertices.push_back(vec[1]);
  105. vertices.push_back(vec[2]);
  106. }
  107. //collect indices
  108. btAlignedObjectArray<unsigned int> indices;
  109. indices.reserve(trimeshInterface->get_primitive_count()*3);
  110. for(int i = 0;i<trimeshInterface->get_primitive_count();i++)
  111. {
  112. unsigned int i0, i1,i2;
  113. trimeshInterface->get_indices(i,i0,i1,i2);
  114. indices.push_back(i0);
  115. indices.push_back(i1);
  116. indices.push_back(i2);
  117. }
  118. trimeshInterface->unlock();
  119. unsigned int depth = 5;
  120. float cpercent = 5;
  121. float ppercent = 15;
  122. unsigned int maxv = 16;
  123. float skinWidth = 0.0f;
  124. ConvexDecomposition::DecompDesc desc;
  125. desc.mVcount = trimeshInterface->get_vertex_count();
  126. desc.mVertices = &vertices[0];
  127. desc.mTcount = trimeshInterface->get_primitive_count();
  128. desc.mIndices = &indices[0];
  129. desc.mDepth = depth;
  130. desc.mCpercent = cpercent;
  131. desc.mPpercent = ppercent;
  132. desc.mMaxVertices = maxv;
  133. desc.mSkinWidth = skinWidth;
  134. desc.mCallback = this;
  135. //convexDecomposition.performConvexDecomposition(desc);
  136. ConvexBuilder cb(desc.mCallback);
  137. cb.process(desc);
  138. }
  139. };
  140. void btGImpactConvexDecompositionShape::buildConvexDecomposition(bool transformSubShapes)
  141. {
  142. m_decomposition = new GIM_ConvexDecomposition(this,transformSubShapes);
  143. int part_count = m_trimeshInterfaces.size();
  144. for (int i = 0;i<part_count ;i++ )
  145. {
  146. m_decomposition->processDecomposition(i);
  147. }
  148. postUpdate();
  149. }
  150. btGImpactConvexDecompositionShape::~btGImpactConvexDecompositionShape()
  151. {
  152. delete m_decomposition;
  153. }
  154. void btGImpactConvexDecompositionShape::processAllTriangles(btTriangleCallback* callback,const btVector3& aabbMin,const btVector3& aabbMax) const
  155. {
  156. int part_count = m_trimeshInterfaces.size();
  157. for (int part = 0;part<part_count ;part++ )
  158. {
  159. void * ptr = (void * )&m_trimeshInterfaces[part];
  160. btGImpactMeshShapePart::TrimeshPrimitiveManager * trimeshInterface =
  161. static_cast<btGImpactMeshShapePart::TrimeshPrimitiveManager *>(ptr);
  162. trimeshInterface->lock();
  163. btPrimitiveTriangle triangle;
  164. int i = trimeshInterface->get_primitive_count();
  165. while(i--)
  166. {
  167. trimeshInterface->get_primitive_triangle(i,triangle);
  168. callback->processTriangle(triangle.m_vertices,part,i);
  169. }
  170. trimeshInterface->unlock();
  171. }
  172. }