2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
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:
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.
16 #ifndef B3_VORONOI_SIMPLEX_SOLVER_H
17 #define B3_VORONOI_SIMPLEX_SOLVER_H
19 #include "Bullet3Common/b3Vector3.h"
21 #define VORONOI_SIMPLEX_MAX_VERTS 5
23 ///disable next define, or use defaultCollisionConfiguration->getSimplexSolver()->setEqualVertexThreshold(0.f) to disable/configure
24 //#define BT_USE_EQUAL_VERTEX_THRESHOLD
25 #define VORONOI_DEFAULT_EQUAL_VERTEX_THRESHOLD 0.0001f
27 struct b3UsageBitfield
41 unsigned short usedVertexA : 1;
42 unsigned short usedVertexB : 1;
43 unsigned short usedVertexC : 1;
44 unsigned short usedVertexD : 1;
45 unsigned short unused1 : 1;
46 unsigned short unused2 : 1;
47 unsigned short unused3 : 1;
48 unsigned short unused4 : 1;
51 struct b3SubSimplexClosestResult
53 b3Vector3 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 b3UsageBitfield m_usedVertices;
58 b3Scalar m_barycentricCoords[4];
64 setBarycentricCoordinates();
65 m_usedVertices.reset();
69 bool valid = (m_barycentricCoords[0] >= b3Scalar(0.)) &&
70 (m_barycentricCoords[1] >= b3Scalar(0.)) &&
71 (m_barycentricCoords[2] >= b3Scalar(0.)) &&
72 (m_barycentricCoords[3] >= b3Scalar(0.));
76 void setBarycentricCoordinates(b3Scalar a = b3Scalar(0.), b3Scalar b = b3Scalar(0.), b3Scalar c = b3Scalar(0.), b3Scalar d = b3Scalar(0.))
78 m_barycentricCoords[0] = a;
79 m_barycentricCoords[1] = b;
80 m_barycentricCoords[2] = c;
81 m_barycentricCoords[3] = d;
85 /// b3VoronoiSimplexSolver is an implementation of the closest point distance algorithm from a 1-4 points simplex to the origin.
86 /// Can be used with GJK, as an alternative to Johnson distance algorithm.
88 B3_ATTRIBUTE_ALIGNED16(class)
89 b3VoronoiSimplexSolver
92 B3_DECLARE_ALIGNED_ALLOCATOR();
96 b3Vector3 m_simplexVectorW[VORONOI_SIMPLEX_MAX_VERTS];
97 b3Vector3 m_simplexPointsP[VORONOI_SIMPLEX_MAX_VERTS];
98 b3Vector3 m_simplexPointsQ[VORONOI_SIMPLEX_MAX_VERTS];
100 b3Vector3 m_cachedP1;
101 b3Vector3 m_cachedP2;
105 b3Scalar m_equalVertexThreshold;
106 bool m_cachedValidClosest;
108 b3SubSimplexClosestResult m_cachedBC;
112 void removeVertex(int index);
113 void reduceVertices(const b3UsageBitfield& usedVerts);
114 bool updateClosestVectorAndPoints();
116 bool closestPtPointTetrahedron(const b3Vector3& p, const b3Vector3& a, const b3Vector3& b, const b3Vector3& c, const b3Vector3& d, b3SubSimplexClosestResult& finalResult);
117 int pointOutsideOfPlane(const b3Vector3& p, const b3Vector3& a, const b3Vector3& b, const b3Vector3& c, const b3Vector3& d);
118 bool closestPtPointTriangle(const b3Vector3& p, const b3Vector3& a, const b3Vector3& b, const b3Vector3& c, b3SubSimplexClosestResult& result);
121 b3VoronoiSimplexSolver()
122 : m_equalVertexThreshold(VORONOI_DEFAULT_EQUAL_VERTEX_THRESHOLD)
127 void addVertex(const b3Vector3& w, const b3Vector3& p, const b3Vector3& q);
129 void setEqualVertexThreshold(b3Scalar threshold)
131 m_equalVertexThreshold = threshold;
134 b3Scalar getEqualVertexThreshold() const
136 return m_equalVertexThreshold;
139 bool closest(b3Vector3 & v);
141 b3Scalar maxVertex();
143 bool fullSimplex() const
145 return (m_numVertices == 4);
148 int getSimplex(b3Vector3 * pBuf, b3Vector3 * qBuf, b3Vector3 * yBuf) const;
150 bool inSimplex(const b3Vector3& w);
152 void backup_closest(b3Vector3 & v);
154 bool emptySimplex() const;
156 void compute_points(b3Vector3 & p1, b3Vector3 & p2);
158 int numVertices() const
160 return m_numVertices;
164 #endif //B3_VORONOI_SIMPLEX_SOLVER_H