| 1 | /* | 
|---|
| 2 | Bullet Continuous Collision Detection and Physics Library | 
|---|
| 3 | Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/ | 
|---|
| 4 |  | 
|---|
| 5 | This software is provided 'as-is', without any express or implied warranty. | 
|---|
| 6 | In no event will the authors be held liable for any damages arising from the use of this software. | 
|---|
| 7 | Permission is granted to anyone to use this software for any purpose,  | 
|---|
| 8 | including commercial applications, and to alter it and redistribute it freely,  | 
|---|
| 9 | subject to the following restrictions: | 
|---|
| 10 |  | 
|---|
| 11 | 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. | 
|---|
| 12 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. | 
|---|
| 13 | 3. This notice may not be removed or altered from any source distribution. | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 17 |  | 
|---|
| 18 | #ifndef btVoronoiSimplexSolver_H | 
|---|
| 19 | #define btVoronoiSimplexSolver_H | 
|---|
| 20 |  | 
|---|
| 21 | #include "btSimplexSolverInterface.h" | 
|---|
| 22 |  | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | #define VORONOI_SIMPLEX_MAX_VERTS 5 | 
|---|
| 26 |  | 
|---|
| 27 | struct btUsageBitfield{ | 
|---|
| 28 |         btUsageBitfield() | 
|---|
| 29 |         { | 
|---|
| 30 |                 reset(); | 
|---|
| 31 |         } | 
|---|
| 32 |  | 
|---|
| 33 |         void reset() | 
|---|
| 34 |         { | 
|---|
| 35 |                 usedVertexA = false; | 
|---|
| 36 |                 usedVertexB = false; | 
|---|
| 37 |                 usedVertexC = false; | 
|---|
| 38 |                 usedVertexD = false; | 
|---|
| 39 |         } | 
|---|
| 40 |         unsigned short usedVertexA      : 1; | 
|---|
| 41 |         unsigned short usedVertexB      : 1; | 
|---|
| 42 |         unsigned short usedVertexC      : 1; | 
|---|
| 43 |         unsigned short usedVertexD      : 1; | 
|---|
| 44 |         unsigned short unused1          : 1; | 
|---|
| 45 |         unsigned short unused2          : 1; | 
|---|
| 46 |         unsigned short unused3          : 1; | 
|---|
| 47 |         unsigned short unused4          : 1; | 
|---|
| 48 | }; | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | struct  btSubSimplexClosestResult | 
|---|
| 52 | { | 
|---|
| 53 |         btVector3       m_closestPointOnSimplex; | 
|---|
| 54 |         //MASK for m_usedVertices | 
|---|
| 55 |         //stores the simplex vertex-usage, using the MASK,  | 
|---|
| 56 |         // if m_usedVertices & MASK then the related vertex is used | 
|---|
| 57 |         btUsageBitfield m_usedVertices; | 
|---|
| 58 |         btScalar        m_barycentricCoords[4]; | 
|---|
| 59 |         bool m_degenerate; | 
|---|
| 60 |  | 
|---|
| 61 |         void    reset() | 
|---|
| 62 |         { | 
|---|
| 63 |                 m_degenerate = false; | 
|---|
| 64 |                 setBarycentricCoordinates(); | 
|---|
| 65 |                 m_usedVertices.reset(); | 
|---|
| 66 |         } | 
|---|
| 67 |         bool    isValid() | 
|---|
| 68 |         { | 
|---|
| 69 |                 bool valid = (m_barycentricCoords[0] >= btScalar(0.)) && | 
|---|
| 70 |                         (m_barycentricCoords[1] >= btScalar(0.)) && | 
|---|
| 71 |                         (m_barycentricCoords[2] >= btScalar(0.)) && | 
|---|
| 72 |                         (m_barycentricCoords[3] >= btScalar(0.)); | 
|---|
| 73 |  | 
|---|
| 74 |  | 
|---|
| 75 |                 return valid; | 
|---|
| 76 |         } | 
|---|
| 77 |         void    setBarycentricCoordinates(btScalar a=btScalar(0.),btScalar b=btScalar(0.),btScalar c=btScalar(0.),btScalar d=btScalar(0.)) | 
|---|
| 78 |         { | 
|---|
| 79 |                 m_barycentricCoords[0] = a; | 
|---|
| 80 |                 m_barycentricCoords[1] = b; | 
|---|
| 81 |                 m_barycentricCoords[2] = c; | 
|---|
| 82 |                 m_barycentricCoords[3] = d; | 
|---|
| 83 |         } | 
|---|
| 84 |  | 
|---|
| 85 | }; | 
|---|
| 86 |  | 
|---|
| 87 | /// btVoronoiSimplexSolver is an implementation of the closest point distance algorithm from a 1-4 points simplex to the origin. | 
|---|
| 88 | /// Can be used with GJK, as an alternative to Johnson distance algorithm. | 
|---|
| 89 | #ifdef NO_VIRTUAL_INTERFACE | 
|---|
| 90 | class btVoronoiSimplexSolver | 
|---|
| 91 | #else | 
|---|
| 92 | class btVoronoiSimplexSolver : public btSimplexSolverInterface | 
|---|
| 93 | #endif | 
|---|
| 94 | { | 
|---|
| 95 | public: | 
|---|
| 96 |  | 
|---|
| 97 |         int     m_numVertices; | 
|---|
| 98 |  | 
|---|
| 99 |         btVector3       m_simplexVectorW[VORONOI_SIMPLEX_MAX_VERTS]; | 
|---|
| 100 |         btVector3       m_simplexPointsP[VORONOI_SIMPLEX_MAX_VERTS]; | 
|---|
| 101 |         btVector3       m_simplexPointsQ[VORONOI_SIMPLEX_MAX_VERTS]; | 
|---|
| 102 |  | 
|---|
| 103 |          | 
|---|
| 104 |  | 
|---|
| 105 |         btVector3       m_cachedP1; | 
|---|
| 106 |         btVector3       m_cachedP2; | 
|---|
| 107 |         btVector3       m_cachedV; | 
|---|
| 108 |         btVector3       m_lastW; | 
|---|
| 109 |         bool            m_cachedValidClosest; | 
|---|
| 110 |  | 
|---|
| 111 |         btSubSimplexClosestResult m_cachedBC; | 
|---|
| 112 |  | 
|---|
| 113 |         bool    m_needsUpdate; | 
|---|
| 114 |          | 
|---|
| 115 |         void    removeVertex(int index); | 
|---|
| 116 |         void    reduceVertices (const btUsageBitfield& usedVerts); | 
|---|
| 117 |         bool    updateClosestVectorAndPoints(); | 
|---|
| 118 |  | 
|---|
| 119 |         bool    closestPtPointTetrahedron(const btVector3& p, const btVector3& a, const btVector3& b, const btVector3& c, const btVector3& d, btSubSimplexClosestResult& finalResult); | 
|---|
| 120 |         int             pointOutsideOfPlane(const btVector3& p, const btVector3& a, const btVector3& b, const btVector3& c, const btVector3& d); | 
|---|
| 121 |         bool    closestPtPointTriangle(const btVector3& p, const btVector3& a, const btVector3& b, const btVector3& c,btSubSimplexClosestResult& result); | 
|---|
| 122 |  | 
|---|
| 123 | public: | 
|---|
| 124 |  | 
|---|
| 125 |          void reset(); | 
|---|
| 126 |  | 
|---|
| 127 |          void addVertex(const btVector3& w, const btVector3& p, const btVector3& q); | 
|---|
| 128 |  | 
|---|
| 129 |  | 
|---|
| 130 |          bool closest(btVector3& v); | 
|---|
| 131 |  | 
|---|
| 132 |          btScalar maxVertex(); | 
|---|
| 133 |  | 
|---|
| 134 |          bool fullSimplex() const | 
|---|
| 135 |          { | 
|---|
| 136 |                  return (m_numVertices == 4); | 
|---|
| 137 |          } | 
|---|
| 138 |  | 
|---|
| 139 |          int getSimplex(btVector3 *pBuf, btVector3 *qBuf, btVector3 *yBuf) const; | 
|---|
| 140 |  | 
|---|
| 141 |          bool inSimplex(const btVector3& w); | 
|---|
| 142 |          | 
|---|
| 143 |          void backup_closest(btVector3& v) ; | 
|---|
| 144 |  | 
|---|
| 145 |          bool emptySimplex() const ; | 
|---|
| 146 |  | 
|---|
| 147 |          void compute_points(btVector3& p1, btVector3& p2) ; | 
|---|
| 148 |  | 
|---|
| 149 |          int numVertices() const  | 
|---|
| 150 |          { | 
|---|
| 151 |                  return m_numVertices; | 
|---|
| 152 |          } | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | }; | 
|---|
| 156 |  | 
|---|
| 157 | #endif //VoronoiSimplexSolver | 
|---|