btConvexHullShape.cpp 6.7 KB

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