btConvexPointCloudShape.cpp 3.8 KB

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