Bullet Collision Detection & Physics Library: btShapeHull.cpp Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btShapeHull.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans http://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 //btShapeHull was implemented by John McCutchan.
17
18
19 #include "btShapeHull.h"
20 #include "LinearMath/btConvexHull.h"
21
22 #define NUM_UNITSPHERE_POINTS 42
23
24 btShapeHull::btShapeHull (const btConvexShape* shape)
25 {
26 m_shape = shape;
27 m_vertices.clear ();
28 m_indices.clear();
29 m_numIndices = 0;
30 }
31
32 btShapeHull::~btShapeHull ()
33 {
34 m_indices.clear();
35 m_vertices.clear ();
36 }
37
38 bool
39 btShapeHull::buildHull (btScalar /*margin*/)
40 {
41 int numSampleDirections = NUM_UNITSPHERE_POINTS;
42 {
43 int numPDA = m_shape->getNumPreferredPenetrationDirections();
44 if (numPDA)
45 {
46 for (int i=0;i<numPDA;i++)
47 {
48 btVector3 norm;
49 m_shape->getPreferredPenetrationDirection(i,norm);
50 getUnitSpherePoints()[numSampleDirections] = norm;
51 numSampleDirections++;
52 }
53 }
54 }
55
56 btVector3 supportPoints[NUM_UNITSPHERE_POINTS+MAX_PREFERRED_PENETRATION_DIRECTIONS*2];
57 int i;
58 for (i = 0; i < numSampleDirections; i++)
59 {
60 supportPoints[i] = m_shape->localGetSupportingVertex(getUnitSpherePoints()[i]);
61 }
62
63 HullDesc hd;
64 hd.mFlags = QF_TRIANGLES;
65 hd.mVcount = static_cast<unsigned int>(numSampleDirections);
66
67 #ifdef BT_USE_DOUBLE_PRECISION
68 hd.mVertices = &supportPoints[0];
69 hd.mVertexStride = sizeof(btVector3);
70 #else
71 hd.mVertices = &supportPoints[0];
72 hd.mVertexStride = sizeof (btVector3);
73 #endif
74
75 HullLibrary hl;
76 HullResult hr;
77 if (hl.CreateConvexHull (hd, hr) == QE_FAIL)
78 {
79 return false;
80 }
81
82 m_vertices.resize (static_cast<int>(hr.mNumOutputVertices));
83
84
85 for (i = 0; i < static_cast<int>(hr.mNumOutputVertices); i++)
86 {
87 m_vertices[i] = hr.m_OutputVertices[i];
88 }
89 m_numIndices = hr.mNumIndices;
90 m_indices.resize(static_cast<int>(m_numIndices));
91 for (i = 0; i < static_cast<int>(m_numIndices); i++)
92 {
93 m_indices[i] = hr.m_Indices[i];
94 }
95
96 // free temporary hull result that we just copied
97 hl.ReleaseResult (hr);
98
99 return true;
100 }
101
102 int
103 btShapeHull::numTriangles () const
104 {
105 return static_cast<int>(m_numIndices / 3);
106 }
107
108 int
109 btShapeHull::numVertices () const
110 {
111 return m_vertices.size ();
112 }
113
114 int
115 btShapeHull::numIndices () const
116 {
117 return static_cast<int>(m_numIndices);
118 }
119
120
121 btVector3* btShapeHull::getUnitSpherePoints()
122 {
123 static btVector3 sUnitSpherePoints[NUM_UNITSPHERE_POINTS+MAX_PREFERRED_PENETRATION_DIRECTIONS*2] =
124 {
125 btVector3(btScalar(0.000000) , btScalar(-0.000000),btScalar(-1.000000)),
126 btVector3(btScalar(0.723608) , btScalar(-0.525725),btScalar(-0.447219)),
127 btVector3(btScalar(-0.276388) , btScalar(-0.850649),btScalar(-0.447219)),
128 btVector3(btScalar(-0.894426) , btScalar(-0.000000),btScalar(-0.447216)),
129 btVector3(btScalar(-0.276388) , btScalar(0.850649),btScalar(-0.447220)),
130 btVector3(btScalar(0.723608) , btScalar(0.525725),btScalar(-0.447219)),
131 btVector3(btScalar(0.276388) , btScalar(-0.850649),btScalar(0.447220)),
132 btVector3(btScalar(-0.723608) , btScalar(-0.525725),btScalar(0.447219)),
133 btVector3(btScalar(-0.723608) , btScalar(0.525725),btScalar(0.447219)),
134 btVector3(btScalar(0.276388) , btScalar(0.850649),btScalar(0.447219)),
135 btVector3(btScalar(0.894426) , btScalar(0.000000),btScalar(0.447216)),
136 btVector3(btScalar(-0.000000) , btScalar(0.000000),btScalar(1.000000)),
137 btVector3(btScalar(0.425323) , btScalar(-0.309011),btScalar(-0.850654)),
138 btVector3(btScalar(-0.162456) , btScalar(-0.499995),btScalar(-0.850654)),
139 btVector3(btScalar(0.262869) , btScalar(-0.809012),btScalar(-0.525738)),
140 btVector3(btScalar(0.425323) , btScalar(0.309011),btScalar(-0.850654)),
141 btVector3(btScalar(0.850648) , btScalar(-0.000000),btScalar(-0.525736)),
142 btVector3(btScalar(-0.525730) , btScalar(-0.000000),btScalar(-0.850652)),
143 btVector3(btScalar(-0.688190) , btScalar(-0.499997),btScalar(-0.525736)),
144 btVector3(btScalar(-0.162456) , btScalar(0.499995),btScalar(-0.850654)),
145 btVector3(btScalar(-0.688190) , btScalar(0.499997),btScalar(-0.525736)),
146 btVector3(btScalar(0.262869) , btScalar(0.809012),btScalar(-0.525738)),
147 btVector3(btScalar(0.951058) , btScalar(0.309013),btScalar(0.000000)),
148 btVector3(btScalar(0.951058) , btScalar(-0.309013),btScalar(0.000000)),
149 btVector3(btScalar(0.587786) , btScalar(-0.809017),btScalar(0.000000)),
150 btVector3(btScalar(0.000000) , btScalar(-1.000000),btScalar(0.000000)),
151 btVector3(btScalar(-0.587786) , btScalar(-0.809017),btScalar(0.000000)),
152 btVector3(btScalar(-0.951058) , btScalar(-0.309013),btScalar(-0.000000)),
153 btVector3(btScalar(-0.951058) , btScalar(0.309013),btScalar(-0.000000)),
154 btVector3(btScalar(-0.587786) , btScalar(0.809017),btScalar(-0.000000)),
155 btVector3(btScalar(-0.000000) , btScalar(1.000000),btScalar(-0.000000)),
156 btVector3(btScalar(0.587786) , btScalar(0.809017),btScalar(-0.000000)),
157 btVector3(btScalar(0.688190) , btScalar(-0.499997),btScalar(0.525736)),
158 btVector3(btScalar(-0.262869) , btScalar(-0.809012),btScalar(0.525738)),
159 btVector3(btScalar(-0.850648) , btScalar(0.000000),btScalar(0.525736)),
160 btVector3(btScalar(-0.262869) , btScalar(0.809012),btScalar(0.525738)),
161 btVector3(btScalar(0.688190) , btScalar(0.499997),btScalar(0.525736)),
162 btVector3(btScalar(0.525730) , btScalar(0.000000),btScalar(0.850652)),
163 btVector3(btScalar(0.162456) , btScalar(-0.499995),btScalar(0.850654)),
164 btVector3(btScalar(-0.425323) , btScalar(-0.309011),btScalar(0.850654)),
165 btVector3(btScalar(-0.425323) , btScalar(0.309011),btScalar(0.850654)),
166 btVector3(btScalar(0.162456) , btScalar(0.499995),btScalar(0.850654))
167 };
168 return sUnitSpherePoints;
169 }
170
btConvexShape::localGetSupportingVertex
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const =0
btShapeHull::m_numIndices
unsigned int m_numIndices
Definition: btShapeHull.h:34
HullResult::mNumIndices
unsigned int mNumIndices
Definition: btConvexHull.h:41
btShapeHull::numIndices
int numIndices() const
Definition: btShapeHull.cpp:115
HullResult::m_Indices
btAlignedObjectArray< unsigned int > m_Indices
Definition: btConvexHull.h:42
btConvexShape::getPreferredPenetrationDirection
virtual void getPreferredPenetrationDirection(int index, btVector3 &penetrationVector) const =0
btShapeHull.h
btShapeHull::getUnitSpherePoints
static btVector3 * getUnitSpherePoints()
Definition: btShapeHull.cpp:121
btShapeHull::m_indices
btAlignedObjectArray< unsigned int > m_indices
Definition: btShapeHull.h:33
btShapeHull::~btShapeHull
~btShapeHull()
Definition: btShapeHull.cpp:32
btConvexShape
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:31
HullDesc::mVertices
const btVector3 * mVertices
Definition: btConvexHull.h:101
btAlignedObjectArray::clear
void clear()
clear the array, deallocated memory. Generally it is better to use array.resize(0), to reduce performance overhead of run-time memory (de)allocations.
Definition: btAlignedObjectArray.h:184
HullDesc::mVcount
unsigned int mVcount
Definition: btConvexHull.h:100
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:149
HullLibrary::CreateConvexHull
HullError CreateConvexHull(const HullDesc &desc, HullResult &result)
Definition: btConvexHull.cpp:691
btShapeHull::buildHull
bool buildHull(btScalar margin)
Definition: btShapeHull.cpp:39
btConvexShape::getNumPreferredPenetrationDirections
virtual int getNumPreferredPenetrationDirections() const =0
MAX_PREFERRED_PENETRATION_DIRECTIONS
#define MAX_PREFERRED_PENETRATION_DIRECTIONS
Definition: btConvexShape.h:27
HullResult::m_OutputVertices
btAlignedObjectArray< btVector3 > m_OutputVertices
Definition: btConvexHull.h:39
NUM_UNITSPHERE_POINTS
#define NUM_UNITSPHERE_POINTS
Definition: btShapeHull.cpp:22
btShapeHull::m_shape
const btConvexShape * m_shape
Definition: btShapeHull.h:35
btShapeHull::btShapeHull
btShapeHull(const btConvexShape *shape)
Definition: btShapeHull.cpp:24
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
QF_TRIANGLES
Definition: btConvexHull.h:50
HullDesc::mFlags
unsigned int mFlags
Definition: btConvexHull.h:99
btConvexHull.h
HullResult
Definition: btConvexHull.h:27
HullResult::mNumOutputVertices
unsigned int mNumOutputVertices
Definition: btConvexHull.h:38
btAlignedObjectArray::resize
void resize(int newsize, const T &fillData=T())
Definition: btAlignedObjectArray.h:212
QE_FAIL
Definition: btConvexHull.h:111
HullDesc::mVertexStride
unsigned int mVertexStride
Definition: btConvexHull.h:102
btShapeHull::numTriangles
int numTriangles() const
Definition: btShapeHull.cpp:103
HullLibrary
The HullLibrary class can create a convex hull from a collection of vertices, using the ComputeHull m...
Definition: btConvexHull.h:184
btShapeHull::numVertices
int numVertices() const
Definition: btShapeHull.cpp:109
HullLibrary::ReleaseResult
HullError ReleaseResult(HullResult &result)
Definition: btConvexHull.cpp:816
btShapeHull::m_vertices
btAlignedObjectArray< btVector3 > m_vertices
Definition: btShapeHull.h:32
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:278
HullDesc
Definition: btConvexHull.h:56