[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3OpenCL / NarrowphaseCollision / b3VoronoiSimplexSolver.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  https://bulletphysics.org
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 #ifndef B3_VORONOI_SIMPLEX_SOLVER_H
17 #define B3_VORONOI_SIMPLEX_SOLVER_H
18
19 #include "Bullet3Common/b3Vector3.h"
20
21 #define VORONOI_SIMPLEX_MAX_VERTS 5
22
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
26
27 struct b3UsageBitfield
28 {
29         b3UsageBitfield()
30         {
31                 reset();
32         }
33
34         void reset()
35         {
36                 usedVertexA = false;
37                 usedVertexB = false;
38                 usedVertexC = false;
39                 usedVertexD = false;
40         }
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;
49 };
50
51 struct b3SubSimplexClosestResult
52 {
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];
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] >= b3Scalar(0.)) &&
70                                          (m_barycentricCoords[1] >= b3Scalar(0.)) &&
71                                          (m_barycentricCoords[2] >= b3Scalar(0.)) &&
72                                          (m_barycentricCoords[3] >= b3Scalar(0.));
73
74                 return valid;
75         }
76         void setBarycentricCoordinates(b3Scalar a = b3Scalar(0.), b3Scalar b = b3Scalar(0.), b3Scalar c = b3Scalar(0.), b3Scalar d = b3Scalar(0.))
77         {
78                 m_barycentricCoords[0] = a;
79                 m_barycentricCoords[1] = b;
80                 m_barycentricCoords[2] = c;
81                 m_barycentricCoords[3] = d;
82         }
83 };
84
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.
87
88 B3_ATTRIBUTE_ALIGNED16(class)
89 b3VoronoiSimplexSolver
90 {
91 public:
92         B3_DECLARE_ALIGNED_ALLOCATOR();
93
94         int m_numVertices;
95
96         b3Vector3 m_simplexVectorW[VORONOI_SIMPLEX_MAX_VERTS];
97         b3Vector3 m_simplexPointsP[VORONOI_SIMPLEX_MAX_VERTS];
98         b3Vector3 m_simplexPointsQ[VORONOI_SIMPLEX_MAX_VERTS];
99
100         b3Vector3 m_cachedP1;
101         b3Vector3 m_cachedP2;
102         b3Vector3 m_cachedV;
103         b3Vector3 m_lastW;
104
105         b3Scalar m_equalVertexThreshold;
106         bool m_cachedValidClosest;
107
108         b3SubSimplexClosestResult m_cachedBC;
109
110         bool m_needsUpdate;
111
112         void removeVertex(int index);
113         void reduceVertices(const b3UsageBitfield& usedVerts);
114         bool updateClosestVectorAndPoints();
115
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);
119
120 public:
121         b3VoronoiSimplexSolver()
122                 : m_equalVertexThreshold(VORONOI_DEFAULT_EQUAL_VERTEX_THRESHOLD)
123         {
124         }
125         void reset();
126
127         void addVertex(const b3Vector3& w, const b3Vector3& p, const b3Vector3& q);
128
129         void setEqualVertexThreshold(b3Scalar threshold)
130         {
131                 m_equalVertexThreshold = threshold;
132         }
133
134         b3Scalar getEqualVertexThreshold() const
135         {
136                 return m_equalVertexThreshold;
137         }
138
139         bool closest(b3Vector3 & v);
140
141         b3Scalar maxVertex();
142
143         bool fullSimplex() const
144         {
145                 return (m_numVertices == 4);
146         }
147
148         int getSimplex(b3Vector3 * pBuf, b3Vector3 * qBuf, b3Vector3 * yBuf) const;
149
150         bool inSimplex(const b3Vector3& w);
151
152         void backup_closest(b3Vector3 & v);
153
154         bool emptySimplex() const;
155
156         void compute_points(b3Vector3 & p1, b3Vector3 & p2);
157
158         int numVertices() const
159         {
160                 return m_numVertices;
161         }
162 };
163
164 #endif  //B3_VORONOI_SIMPLEX_SOLVER_H