btConvexPointCloudShape.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "btConvexPointCloudShape.h"
  14. #include "bullet/BulletCollision//CollisionShapes/btCollisionMargin.h"
  15. #include "bullet/LinearMath/btQuaternion.h"
  16. void btConvexPointCloudShape::setLocalScaling(const btVector3& scaling)
  17. {
  18. m_localScaling = scaling;
  19. recalcLocalAabb();
  20. }
  21. #ifndef __SPU__
  22. btVector3 btConvexPointCloudShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const
  23. {
  24. btVector3 supVec(btScalar(0.),btScalar(0.),btScalar(0.));
  25. btScalar maxDot = btScalar(-BT_LARGE_FLOAT);
  26. btVector3 vec = vec0;
  27. btScalar lenSqr = vec.length2();
  28. if (lenSqr < btScalar(0.0001))
  29. {
  30. vec.setValue(1,0,0);
  31. } else
  32. {
  33. btScalar rlen = btScalar(1.) / btSqrt(lenSqr );
  34. vec *= rlen;
  35. }
  36. if( m_numPoints > 0 )
  37. {
  38. // Here we take advantage of dot(a*b, c) = dot( a, b*c) to do less work. Note this transformation is true mathematically, not numerically.
  39. // btVector3 scaled = vec * m_localScaling;
  40. int index = (int) vec.maxDot( &m_unscaledPoints[0], m_numPoints, maxDot); //FIXME: may violate encapsulation of m_unscaledPoints
  41. return getScaledPoint(index);
  42. }
  43. return supVec;
  44. }
  45. void btConvexPointCloudShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
  46. {
  47. for( int j = 0; j < numVectors; j++ )
  48. {
  49. const btVector3& vec = vectors[j] * m_localScaling; // dot( a*c, b) = dot(a, b*c)
  50. btScalar maxDot;
  51. int index = (int) vec.maxDot( &m_unscaledPoints[0], m_numPoints, maxDot);
  52. supportVerticesOut[j][3] = btScalar(-BT_LARGE_FLOAT);
  53. if( 0 <= index )
  54. {
  55. //WARNING: don't swap next lines, the w component would get overwritten!
  56. supportVerticesOut[j] = getScaledPoint(index);
  57. supportVerticesOut[j][3] = maxDot;
  58. }
  59. }
  60. }
  61. btVector3 btConvexPointCloudShape::localGetSupportingVertex(const btVector3& vec)const
  62. {
  63. btVector3 supVertex = localGetSupportingVertexWithoutMargin(vec);
  64. if ( getMargin()!=btScalar(0.) )
  65. {
  66. btVector3 vecnorm = vec;
  67. if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
  68. {
  69. vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
  70. }
  71. vecnorm.normalize();
  72. supVertex+= getMargin() * vecnorm;
  73. }
  74. return supVertex;
  75. }
  76. #endif
  77. //currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection
  78. //Please note that you can debug-draw btConvexHullShape with the Raytracer Demo
  79. int btConvexPointCloudShape::getNumVertices() const
  80. {
  81. return m_numPoints;
  82. }
  83. int btConvexPointCloudShape::getNumEdges() const
  84. {
  85. return 0;
  86. }
  87. void btConvexPointCloudShape::getEdge(int i,btVector3& pa,btVector3& pb) const
  88. {
  89. btAssert (0);
  90. }
  91. void btConvexPointCloudShape::getVertex(int i,btVector3& vtx) const
  92. {
  93. vtx = m_unscaledPoints[i]*m_localScaling;
  94. }
  95. int btConvexPointCloudShape::getNumPlanes() const
  96. {
  97. return 0;
  98. }
  99. void btConvexPointCloudShape::getPlane(btVector3& ,btVector3& ,int ) const
  100. {
  101. btAssert(0);
  102. }
  103. //not yet
  104. bool btConvexPointCloudShape::isInside(const btVector3& ,btScalar ) const
  105. {
  106. btAssert(0);
  107. return false;
  108. }