1 | /* |
---|
2 | btbtShapeHull implemented by John McCutchan. |
---|
3 | |
---|
4 | Bullet Continuous Collision Detection and Physics Library |
---|
5 | Copyright (c) 2003-2008 Erwin Coumans http://bulletphysics.com |
---|
6 | |
---|
7 | This software is provided 'as-is', without any express or implied warranty. |
---|
8 | In no event will the authors be held liable for any damages arising from the use of this software. |
---|
9 | Permission is granted to anyone to use this software for any purpose, |
---|
10 | including commercial applications, and to alter it and redistribute it freely, |
---|
11 | subject to the following restrictions: |
---|
12 | |
---|
13 | 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. |
---|
14 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. |
---|
15 | 3. This notice may not be removed or altered from any source distribution. |
---|
16 | */ |
---|
17 | |
---|
18 | #include "btShapeHull.h" |
---|
19 | #include "LinearMath/btConvexHull.h" |
---|
20 | |
---|
21 | #define NUM_UNITSPHERE_POINTS 42 |
---|
22 | |
---|
23 | static btVector3 btUnitSpherePoints[NUM_UNITSPHERE_POINTS+MAX_PREFERRED_PENETRATION_DIRECTIONS*2] = |
---|
24 | { |
---|
25 | btVector3(btScalar(0.000000) , btScalar(-0.000000),btScalar(-1.000000)), |
---|
26 | btVector3(btScalar(0.723608) , btScalar(-0.525725),btScalar(-0.447219)), |
---|
27 | btVector3(btScalar(-0.276388) , btScalar(-0.850649),btScalar(-0.447219)), |
---|
28 | btVector3(btScalar(-0.894426) , btScalar(-0.000000),btScalar(-0.447216)), |
---|
29 | btVector3(btScalar(-0.276388) , btScalar(0.850649),btScalar(-0.447220)), |
---|
30 | btVector3(btScalar(0.723608) , btScalar(0.525725),btScalar(-0.447219)), |
---|
31 | btVector3(btScalar(0.276388) , btScalar(-0.850649),btScalar(0.447220)), |
---|
32 | btVector3(btScalar(-0.723608) , btScalar(-0.525725),btScalar(0.447219)), |
---|
33 | btVector3(btScalar(-0.723608) , btScalar(0.525725),btScalar(0.447219)), |
---|
34 | btVector3(btScalar(0.276388) , btScalar(0.850649),btScalar(0.447219)), |
---|
35 | btVector3(btScalar(0.894426) , btScalar(0.000000),btScalar(0.447216)), |
---|
36 | btVector3(btScalar(-0.000000) , btScalar(0.000000),btScalar(1.000000)), |
---|
37 | btVector3(btScalar(0.425323) , btScalar(-0.309011),btScalar(-0.850654)), |
---|
38 | btVector3(btScalar(-0.162456) , btScalar(-0.499995),btScalar(-0.850654)), |
---|
39 | btVector3(btScalar(0.262869) , btScalar(-0.809012),btScalar(-0.525738)), |
---|
40 | btVector3(btScalar(0.425323) , btScalar(0.309011),btScalar(-0.850654)), |
---|
41 | btVector3(btScalar(0.850648) , btScalar(-0.000000),btScalar(-0.525736)), |
---|
42 | btVector3(btScalar(-0.525730) , btScalar(-0.000000),btScalar(-0.850652)), |
---|
43 | btVector3(btScalar(-0.688190) , btScalar(-0.499997),btScalar(-0.525736)), |
---|
44 | btVector3(btScalar(-0.162456) , btScalar(0.499995),btScalar(-0.850654)), |
---|
45 | btVector3(btScalar(-0.688190) , btScalar(0.499997),btScalar(-0.525736)), |
---|
46 | btVector3(btScalar(0.262869) , btScalar(0.809012),btScalar(-0.525738)), |
---|
47 | btVector3(btScalar(0.951058) , btScalar(0.309013),btScalar(0.000000)), |
---|
48 | btVector3(btScalar(0.951058) , btScalar(-0.309013),btScalar(0.000000)), |
---|
49 | btVector3(btScalar(0.587786) , btScalar(-0.809017),btScalar(0.000000)), |
---|
50 | btVector3(btScalar(0.000000) , btScalar(-1.000000),btScalar(0.000000)), |
---|
51 | btVector3(btScalar(-0.587786) , btScalar(-0.809017),btScalar(0.000000)), |
---|
52 | btVector3(btScalar(-0.951058) , btScalar(-0.309013),btScalar(-0.000000)), |
---|
53 | btVector3(btScalar(-0.951058) , btScalar(0.309013),btScalar(-0.000000)), |
---|
54 | btVector3(btScalar(-0.587786) , btScalar(0.809017),btScalar(-0.000000)), |
---|
55 | btVector3(btScalar(-0.000000) , btScalar(1.000000),btScalar(-0.000000)), |
---|
56 | btVector3(btScalar(0.587786) , btScalar(0.809017),btScalar(-0.000000)), |
---|
57 | btVector3(btScalar(0.688190) , btScalar(-0.499997),btScalar(0.525736)), |
---|
58 | btVector3(btScalar(-0.262869) , btScalar(-0.809012),btScalar(0.525738)), |
---|
59 | btVector3(btScalar(-0.850648) , btScalar(0.000000),btScalar(0.525736)), |
---|
60 | btVector3(btScalar(-0.262869) , btScalar(0.809012),btScalar(0.525738)), |
---|
61 | btVector3(btScalar(0.688190) , btScalar(0.499997),btScalar(0.525736)), |
---|
62 | btVector3(btScalar(0.525730) , btScalar(0.000000),btScalar(0.850652)), |
---|
63 | btVector3(btScalar(0.162456) , btScalar(-0.499995),btScalar(0.850654)), |
---|
64 | btVector3(btScalar(-0.425323) , btScalar(-0.309011),btScalar(0.850654)), |
---|
65 | btVector3(btScalar(-0.425323) , btScalar(0.309011),btScalar(0.850654)), |
---|
66 | btVector3(btScalar(0.162456) , btScalar(0.499995),btScalar(0.850654)) |
---|
67 | }; |
---|
68 | |
---|
69 | btShapeHull::btShapeHull (const btConvexShape* shape) |
---|
70 | { |
---|
71 | m_shape = shape; |
---|
72 | m_vertices.clear (); |
---|
73 | m_indices.clear(); |
---|
74 | m_numIndices = 0; |
---|
75 | } |
---|
76 | |
---|
77 | btShapeHull::~btShapeHull () |
---|
78 | { |
---|
79 | m_indices.clear(); |
---|
80 | m_vertices.clear (); |
---|
81 | } |
---|
82 | |
---|
83 | bool |
---|
84 | btShapeHull::buildHull (btScalar /*margin*/) |
---|
85 | { |
---|
86 | int numSampleDirections = NUM_UNITSPHERE_POINTS; |
---|
87 | { |
---|
88 | int numPDA = m_shape->getNumPreferredPenetrationDirections(); |
---|
89 | if (numPDA) |
---|
90 | { |
---|
91 | for (int i=0;i<numPDA;i++) |
---|
92 | { |
---|
93 | btVector3 norm; |
---|
94 | m_shape->getPreferredPenetrationDirection(i,norm); |
---|
95 | btUnitSpherePoints[numSampleDirections] = norm; |
---|
96 | numSampleDirections++; |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | btVector3 supportPoints[NUM_UNITSPHERE_POINTS+MAX_PREFERRED_PENETRATION_DIRECTIONS*2]; |
---|
102 | int i; |
---|
103 | for (i = 0; i < numSampleDirections; i++) |
---|
104 | { |
---|
105 | supportPoints[i] = m_shape->localGetSupportingVertex(btUnitSpherePoints[i]); |
---|
106 | } |
---|
107 | |
---|
108 | HullDesc hd; |
---|
109 | hd.mFlags = QF_TRIANGLES; |
---|
110 | hd.mVcount = static_cast<unsigned int>(numSampleDirections); |
---|
111 | |
---|
112 | #ifdef BT_USE_DOUBLE_PRECISION |
---|
113 | hd.mVertices = &supportPoints[0]; |
---|
114 | hd.mVertexStride = sizeof(btVector3); |
---|
115 | #else |
---|
116 | hd.mVertices = &supportPoints[0]; |
---|
117 | hd.mVertexStride = sizeof (btVector3); |
---|
118 | #endif |
---|
119 | |
---|
120 | HullLibrary hl; |
---|
121 | HullResult hr; |
---|
122 | if (hl.CreateConvexHull (hd, hr) == QE_FAIL) |
---|
123 | { |
---|
124 | return false; |
---|
125 | } |
---|
126 | |
---|
127 | m_vertices.resize (static_cast<int>(hr.mNumOutputVertices)); |
---|
128 | |
---|
129 | |
---|
130 | for (i = 0; i < static_cast<int>(hr.mNumOutputVertices); i++) |
---|
131 | { |
---|
132 | m_vertices[i] = hr.m_OutputVertices[i]; |
---|
133 | } |
---|
134 | m_numIndices = hr.mNumIndices; |
---|
135 | m_indices.resize(static_cast<int>(m_numIndices)); |
---|
136 | for (i = 0; i < static_cast<int>(m_numIndices); i++) |
---|
137 | { |
---|
138 | m_indices[i] = hr.m_Indices[i]; |
---|
139 | } |
---|
140 | |
---|
141 | // free temporary hull result that we just copied |
---|
142 | hl.ReleaseResult (hr); |
---|
143 | |
---|
144 | return true; |
---|
145 | } |
---|
146 | |
---|
147 | int |
---|
148 | btShapeHull::numTriangles () const |
---|
149 | { |
---|
150 | return static_cast<int>(m_numIndices / 3); |
---|
151 | } |
---|
152 | |
---|
153 | int |
---|
154 | btShapeHull::numVertices () const |
---|
155 | { |
---|
156 | return m_vertices.size (); |
---|
157 | } |
---|
158 | |
---|
159 | int |
---|
160 | btShapeHull::numIndices () const |
---|
161 | { |
---|
162 | return static_cast<int>(m_numIndices); |
---|
163 | } |
---|
164 | |
---|