b3VoronoiSimplexSolver.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  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. #ifndef B3_VORONOI_SIMPLEX_SOLVER_H
  14. #define B3_VORONOI_SIMPLEX_SOLVER_H
  15. #include "Bullet3Common/b3Vector3.h"
  16. #define VORONOI_SIMPLEX_MAX_VERTS 5
  17. ///disable next define, or use defaultCollisionConfiguration->getSimplexSolver()->setEqualVertexThreshold(0.f) to disable/configure
  18. //#define BT_USE_EQUAL_VERTEX_THRESHOLD
  19. #define VORONOI_DEFAULT_EQUAL_VERTEX_THRESHOLD 0.0001f
  20. struct b3UsageBitfield
  21. {
  22. b3UsageBitfield()
  23. {
  24. reset();
  25. }
  26. void reset()
  27. {
  28. usedVertexA = false;
  29. usedVertexB = false;
  30. usedVertexC = false;
  31. usedVertexD = false;
  32. }
  33. unsigned short usedVertexA : 1;
  34. unsigned short usedVertexB : 1;
  35. unsigned short usedVertexC : 1;
  36. unsigned short usedVertexD : 1;
  37. unsigned short unused1 : 1;
  38. unsigned short unused2 : 1;
  39. unsigned short unused3 : 1;
  40. unsigned short unused4 : 1;
  41. };
  42. struct b3SubSimplexClosestResult
  43. {
  44. b3Vector3 m_closestPointOnSimplex;
  45. //MASK for m_usedVertices
  46. //stores the simplex vertex-usage, using the MASK,
  47. // if m_usedVertices & MASK then the related vertex is used
  48. b3UsageBitfield m_usedVertices;
  49. b3Scalar m_barycentricCoords[4];
  50. bool m_degenerate;
  51. void reset()
  52. {
  53. m_degenerate = false;
  54. setBarycentricCoordinates();
  55. m_usedVertices.reset();
  56. }
  57. bool isValid()
  58. {
  59. bool valid = (m_barycentricCoords[0] >= b3Scalar(0.)) &&
  60. (m_barycentricCoords[1] >= b3Scalar(0.)) &&
  61. (m_barycentricCoords[2] >= b3Scalar(0.)) &&
  62. (m_barycentricCoords[3] >= b3Scalar(0.));
  63. return valid;
  64. }
  65. void setBarycentricCoordinates(b3Scalar a = b3Scalar(0.), b3Scalar b = b3Scalar(0.), b3Scalar c = b3Scalar(0.), b3Scalar d = b3Scalar(0.))
  66. {
  67. m_barycentricCoords[0] = a;
  68. m_barycentricCoords[1] = b;
  69. m_barycentricCoords[2] = c;
  70. m_barycentricCoords[3] = d;
  71. }
  72. };
  73. /// b3VoronoiSimplexSolver is an implementation of the closest point distance algorithm from a 1-4 points simplex to the origin.
  74. /// Can be used with GJK, as an alternative to Johnson distance algorithm.
  75. B3_ATTRIBUTE_ALIGNED16(class)
  76. b3VoronoiSimplexSolver
  77. {
  78. public:
  79. B3_DECLARE_ALIGNED_ALLOCATOR();
  80. int m_numVertices;
  81. b3Vector3 m_simplexVectorW[VORONOI_SIMPLEX_MAX_VERTS];
  82. b3Vector3 m_simplexPointsP[VORONOI_SIMPLEX_MAX_VERTS];
  83. b3Vector3 m_simplexPointsQ[VORONOI_SIMPLEX_MAX_VERTS];
  84. b3Vector3 m_cachedP1;
  85. b3Vector3 m_cachedP2;
  86. b3Vector3 m_cachedV;
  87. b3Vector3 m_lastW;
  88. b3Scalar m_equalVertexThreshold;
  89. bool m_cachedValidClosest;
  90. b3SubSimplexClosestResult m_cachedBC;
  91. bool m_needsUpdate;
  92. void removeVertex(int index);
  93. void reduceVertices(const b3UsageBitfield& usedVerts);
  94. bool updateClosestVectorAndPoints();
  95. bool closestPtPointTetrahedron(const b3Vector3& p, const b3Vector3& a, const b3Vector3& b, const b3Vector3& c, const b3Vector3& d, b3SubSimplexClosestResult& finalResult);
  96. int pointOutsideOfPlane(const b3Vector3& p, const b3Vector3& a, const b3Vector3& b, const b3Vector3& c, const b3Vector3& d);
  97. bool closestPtPointTriangle(const b3Vector3& p, const b3Vector3& a, const b3Vector3& b, const b3Vector3& c, b3SubSimplexClosestResult& result);
  98. public:
  99. b3VoronoiSimplexSolver()
  100. : m_equalVertexThreshold(VORONOI_DEFAULT_EQUAL_VERTEX_THRESHOLD)
  101. {
  102. }
  103. void reset();
  104. void addVertex(const b3Vector3& w, const b3Vector3& p, const b3Vector3& q);
  105. void setEqualVertexThreshold(b3Scalar threshold)
  106. {
  107. m_equalVertexThreshold = threshold;
  108. }
  109. b3Scalar getEqualVertexThreshold() const
  110. {
  111. return m_equalVertexThreshold;
  112. }
  113. bool closest(b3Vector3 & v);
  114. b3Scalar maxVertex();
  115. bool fullSimplex() const
  116. {
  117. return (m_numVertices == 4);
  118. }
  119. int getSimplex(b3Vector3 * pBuf, b3Vector3 * qBuf, b3Vector3 * yBuf) const;
  120. bool inSimplex(const b3Vector3& w);
  121. void backup_closest(b3Vector3 & v);
  122. bool emptySimplex() const;
  123. void compute_points(b3Vector3 & p1, b3Vector3 & p2);
  124. int numVertices() const
  125. {
  126. return m_numVertices;
  127. }
  128. };
  129. #endif //B3_VORONOI_SIMPLEX_SOLVER_H