btConvexHullShape.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
  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. #if defined (_WIN32) || defined (__i386__)
  14. #define BT_USE_SSE_IN_API
  15. #endif
  16. #include "btConvexHullShape.h"
  17. #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
  18. #include "LinearMath/btQuaternion.h"
  19. #include "LinearMath/btSerializer.h"
  20. #include "btConvexPolyhedron.h"
  21. #include "LinearMath/btConvexHullComputer.h"
  22. btConvexHullShape ::btConvexHullShape (const btScalar* points,int numPoints,int stride) : btPolyhedralConvexAabbCachingShape ()
  23. {
  24. m_shapeType = CONVEX_HULL_SHAPE_PROXYTYPE;
  25. m_unscaledPoints.resize(numPoints);
  26. unsigned char* pointsAddress = (unsigned char*)points;
  27. for (int i=0;i<numPoints;i++)
  28. {
  29. btScalar* point = (btScalar*)pointsAddress;
  30. m_unscaledPoints[i] = btVector3(point[0], point[1], point[2]);
  31. pointsAddress += stride;
  32. }
  33. recalcLocalAabb();
  34. }
  35. void btConvexHullShape::setLocalScaling(const btVector3& scaling)
  36. {
  37. m_localScaling = scaling;
  38. recalcLocalAabb();
  39. }
  40. void btConvexHullShape::addPoint(const btVector3& point, bool recalculateLocalAabb)
  41. {
  42. m_unscaledPoints.push_back(point);
  43. if (recalculateLocalAabb)
  44. recalcLocalAabb();
  45. }
  46. btVector3 btConvexHullShape::localGetSupportingVertexWithoutMargin(const btVector3& vec)const
  47. {
  48. btVector3 supVec(btScalar(0.),btScalar(0.),btScalar(0.));
  49. btScalar maxDot = btScalar(-BT_LARGE_FLOAT);
  50. // Here we take advantage of dot(a, b*c) = dot(a*b, c). Note: This is true mathematically, but not numerically.
  51. if( 0 < m_unscaledPoints.size() )
  52. {
  53. btVector3 scaled = vec * m_localScaling;
  54. int index = (int) scaled.maxDot( &m_unscaledPoints[0], m_unscaledPoints.size(), maxDot); // FIXME: may violate encapsulation of m_unscaledPoints
  55. return m_unscaledPoints[index] * m_localScaling;
  56. }
  57. return supVec;
  58. }
  59. void btConvexHullShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
  60. {
  61. btScalar newDot;
  62. //use 'w' component of supportVerticesOut?
  63. {
  64. for (int i=0;i<numVectors;i++)
  65. {
  66. supportVerticesOut[i][3] = btScalar(-BT_LARGE_FLOAT);
  67. }
  68. }
  69. for (int j=0;j<numVectors;j++)
  70. {
  71. btVector3 vec = vectors[j] * m_localScaling; // dot(a*b,c) = dot(a,b*c)
  72. if( 0 < m_unscaledPoints.size() )
  73. {
  74. int i = (int) vec.maxDot( &m_unscaledPoints[0], m_unscaledPoints.size(), newDot);
  75. supportVerticesOut[j] = getScaledPoint(i);
  76. supportVerticesOut[j][3] = newDot;
  77. }
  78. else
  79. supportVerticesOut[j][3] = -BT_LARGE_FLOAT;
  80. }
  81. }
  82. btVector3 btConvexHullShape::localGetSupportingVertex(const btVector3& vec)const
  83. {
  84. btVector3 supVertex = localGetSupportingVertexWithoutMargin(vec);
  85. if ( getMargin()!=btScalar(0.) )
  86. {
  87. btVector3 vecnorm = vec;
  88. if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
  89. {
  90. vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
  91. }
  92. vecnorm.normalize();
  93. supVertex+= getMargin() * vecnorm;
  94. }
  95. return supVertex;
  96. }
  97. void btConvexHullShape::optimizeConvexHull()
  98. {
  99. btConvexHullComputer conv;
  100. conv.compute(&m_unscaledPoints[0].getX(), sizeof(btVector3),m_unscaledPoints.size(),0.f,0.f);
  101. int numVerts = conv.vertices.size();
  102. m_unscaledPoints.resize(0);
  103. for (int i=0;i<numVerts;i++)
  104. {
  105. m_unscaledPoints.push_back(conv.vertices[i]);
  106. }
  107. }
  108. //currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection
  109. //Please note that you can debug-draw btConvexHullShape with the Raytracer Demo
  110. int btConvexHullShape::getNumVertices() const
  111. {
  112. return m_unscaledPoints.size();
  113. }
  114. int btConvexHullShape::getNumEdges() const
  115. {
  116. return m_unscaledPoints.size();
  117. }
  118. void btConvexHullShape::getEdge(int i,btVector3& pa,btVector3& pb) const
  119. {
  120. int index0 = i%m_unscaledPoints.size();
  121. int index1 = (i+1)%m_unscaledPoints.size();
  122. pa = getScaledPoint(index0);
  123. pb = getScaledPoint(index1);
  124. }
  125. void btConvexHullShape::getVertex(int i,btVector3& vtx) const
  126. {
  127. vtx = getScaledPoint(i);
  128. }
  129. int btConvexHullShape::getNumPlanes() const
  130. {
  131. return 0;
  132. }
  133. void btConvexHullShape::getPlane(btVector3& ,btVector3& ,int ) const
  134. {
  135. btAssert(0);
  136. }
  137. //not yet
  138. bool btConvexHullShape::isInside(const btVector3& ,btScalar ) const
  139. {
  140. btAssert(0);
  141. return false;
  142. }
  143. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  144. const char* btConvexHullShape::serialize(void* dataBuffer, btSerializer* serializer) const
  145. {
  146. //int szc = sizeof(btConvexHullShapeData);
  147. btConvexHullShapeData* shapeData = (btConvexHullShapeData*) dataBuffer;
  148. btConvexInternalShape::serialize(&shapeData->m_convexInternalShapeData, serializer);
  149. int numElem = m_unscaledPoints.size();
  150. shapeData->m_numUnscaledPoints = numElem;
  151. #ifdef BT_USE_DOUBLE_PRECISION
  152. shapeData->m_unscaledPointsFloatPtr = 0;
  153. shapeData->m_unscaledPointsDoublePtr = numElem ? (btVector3Data*)serializer->getUniquePointer((void*)&m_unscaledPoints[0]): 0;
  154. #else
  155. shapeData->m_unscaledPointsFloatPtr = numElem ? (btVector3Data*)serializer->getUniquePointer((void*)&m_unscaledPoints[0]): 0;
  156. shapeData->m_unscaledPointsDoublePtr = 0;
  157. #endif
  158. if (numElem)
  159. {
  160. int sz = sizeof(btVector3Data);
  161. // int sz2 = sizeof(btVector3DoubleData);
  162. // int sz3 = sizeof(btVector3FloatData);
  163. btChunk* chunk = serializer->allocate(sz,numElem);
  164. btVector3Data* memPtr = (btVector3Data*)chunk->m_oldPtr;
  165. for (int i=0;i<numElem;i++,memPtr++)
  166. {
  167. m_unscaledPoints[i].serialize(*memPtr);
  168. }
  169. serializer->finalizeChunk(chunk,btVector3DataName,BT_ARRAY_CODE,(void*)&m_unscaledPoints[0]);
  170. }
  171. // Fill padding with zeros to appease msan.
  172. memset(shapeData->m_padding3, 0, sizeof(shapeData->m_padding3));
  173. return "btConvexHullShapeData";
  174. }
  175. void btConvexHullShape::project(const btTransform& trans, const btVector3& dir, btScalar& minProj, btScalar& maxProj, btVector3& witnesPtMin,btVector3& witnesPtMax) const
  176. {
  177. #if 1
  178. minProj = FLT_MAX;
  179. maxProj = -FLT_MAX;
  180. int numVerts = m_unscaledPoints.size();
  181. for(int i=0;i<numVerts;i++)
  182. {
  183. btVector3 vtx = m_unscaledPoints[i] * m_localScaling;
  184. btVector3 pt = trans * vtx;
  185. btScalar dp = pt.dot(dir);
  186. if(dp < minProj)
  187. {
  188. minProj = dp;
  189. witnesPtMin = pt;
  190. }
  191. if(dp > maxProj)
  192. {
  193. maxProj = dp;
  194. witnesPtMax=pt;
  195. }
  196. }
  197. #else
  198. btVector3 localAxis = dir*trans.getBasis();
  199. witnesPtMin = trans(localGetSupportingVertex(localAxis));
  200. witnesPtMax = trans(localGetSupportingVertex(-localAxis));
  201. minProj = witnesPtMin.dot(dir);
  202. maxProj = witnesPtMax.dot(dir);
  203. #endif
  204. if(minProj>maxProj)
  205. {
  206. btSwap(minProj,maxProj);
  207. btSwap(witnesPtMin,witnesPtMax);
  208. }
  209. }