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

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btConvexHullShape.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 #if defined (_WIN32) || defined (__i386__)
17 #define BT_USE_SSE_IN_API
18 #endif
19
20 #include "btConvexHullShape.h"
21 #include "BulletCollision/CollisionShapes/btCollisionMargin.h"
22
23 #include "LinearMath/btQuaternion.h"
24 #include "LinearMath/btSerializer.h"
25
26 btConvexHullShape ::btConvexHullShape (const btScalar* points,int numPoints,int stride) : btPolyhedralConvexAabbCachingShape ()
27 {
28 m_shapeType = CONVEX_HULL_SHAPE_PROXYTYPE;
29 m_unscaledPoints.resize(numPoints);
30
31 unsigned char* pointsAddress = (unsigned char*)points;
32
33 for (int i=0;i<numPoints;i++)
34 {
35 btScalar* point = (btScalar*)pointsAddress;
36 m_unscaledPoints[i] = btVector3(point[0], point[1], point[2]);
37 pointsAddress += stride;
38 }
39
40 recalcLocalAabb();
41
42 }
43
44
45
46 void btConvexHullShape::setLocalScaling(const btVector3& scaling)
47 {
48 m_localScaling = scaling;
49 recalcLocalAabb();
50 }
51
52 void btConvexHullShape::addPoint(const btVector3& point, bool recalculateLocalAabb)
53 {
54 m_unscaledPoints.push_back(point);
55 if (recalculateLocalAabb)
56 recalcLocalAabb();
57
58 }
59
60 btVector3 btConvexHullShape::localGetSupportingVertexWithoutMargin(const btVector3& vec)const
61 {
62 btVector3 supVec(btScalar(0.),btScalar(0.),btScalar(0.));
63 btScalar maxDot = btScalar(-BT_LARGE_FLOAT);
64
65 // Here we take advantage of dot(a, b*c) = dot(a*b, c). Note: This is true mathematically, but not numerically.
66 if( 0 < m_unscaledPoints.size() )
67 {
68 btVector3 scaled = vec * m_localScaling;
69 int index = (int) scaled.maxDot( &m_unscaledPoints[0], m_unscaledPoints.size(), maxDot); // FIXME: may violate encapsulation of m_unscaledPoints
70 return m_unscaledPoints[index] * m_localScaling;
71 }
72
73 return supVec;
74 }
75
76 void btConvexHullShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
77 {
78 btScalar newDot;
79 //use 'w' component of supportVerticesOut?
80 {
81 for (int i=0;i<numVectors;i++)
82 {
83 supportVerticesOut[i][3] = btScalar(-BT_LARGE_FLOAT);
84 }
85 }
86
87 for (int j=0;j<numVectors;j++)
88 {
89 btVector3 vec = vectors[j] * m_localScaling; // dot(a*b,c) = dot(a,b*c)
90 if( 0 < m_unscaledPoints.size() )
91 {
92 int i = (int) vec.maxDot( &m_unscaledPoints[0], m_unscaledPoints.size(), newDot);
93 supportVerticesOut[j] = getScaledPoint(i);
94 supportVerticesOut[j][3] = newDot;
95 }
96 else
97 supportVerticesOut[j][3] = -BT_LARGE_FLOAT;
98 }
99
100
101
102 }
103
104
105
106 btVector3 btConvexHullShape::localGetSupportingVertex(const btVector3& vec)const
107 {
108 btVector3 supVertex = localGetSupportingVertexWithoutMargin(vec);
109
110 if ( getMargin()!=btScalar(0.) )
111 {
112 btVector3 vecnorm = vec;
113 if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
114 {
115 vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
116 }
117 vecnorm.normalize();
118 supVertex+= getMargin() * vecnorm;
119 }
120 return supVertex;
121 }
122
123
124
125
126
127
128
129
130
131 //currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection
132 //Please note that you can debug-draw btConvexHullShape with the Raytracer Demo
133 int btConvexHullShape::getNumVertices() const
134 {
135 return m_unscaledPoints.size();
136 }
137
138 int btConvexHullShape::getNumEdges() const
139 {
140 return m_unscaledPoints.size();
141 }
142
143 void btConvexHullShape::getEdge(int i,btVector3& pa,btVector3& pb) const
144 {
145
146 int index0 = i%m_unscaledPoints.size();
147 int index1 = (i+1)%m_unscaledPoints.size();
148 pa = getScaledPoint(index0);
149 pb = getScaledPoint(index1);
150 }
151
152 void btConvexHullShape::getVertex(int i,btVector3& vtx) const
153 {
154 vtx = getScaledPoint(i);
155 }
156
157 int btConvexHullShape::getNumPlanes() const
158 {
159 return 0;
160 }
161
162 void btConvexHullShape::getPlane(btVector3& ,btVector3& ,int ) const
163 {
164
165 btAssert(0);
166 }
167
168 //not yet
169 bool btConvexHullShape::isInside(const btVector3& ,btScalar ) const
170 {
171 btAssert(0);
172 return false;
173 }
174
176 const char* btConvexHullShape::serialize(void* dataBuffer, btSerializer* serializer) const
177 {
178 //int szc = sizeof(btConvexHullShapeData);
179 btConvexHullShapeData* shapeData = (btConvexHullShapeData*) dataBuffer;
180 btConvexInternalShape::serialize(&shapeData->m_convexInternalShapeData, serializer);
181
182 int numElem = m_unscaledPoints.size();
183 shapeData->m_numUnscaledPoints = numElem;
184 #ifdef BT_USE_DOUBLE_PRECISION
185 shapeData->m_unscaledPointsFloatPtr = 0;
186 shapeData->m_unscaledPointsDoublePtr = numElem ? (btVector3Data*)serializer->getUniquePointer((void*)&m_unscaledPoints[0]): 0;
187 #else
188 shapeData->m_unscaledPointsFloatPtr = numElem ? (btVector3Data*)serializer->getUniquePointer((void*)&m_unscaledPoints[0]): 0;
189 shapeData->m_unscaledPointsDoublePtr = 0;
190 #endif
191
192 if (numElem)
193 {
194 int sz = sizeof(btVector3Data);
195 // int sz2 = sizeof(btVector3DoubleData);
196 // int sz3 = sizeof(btVector3FloatData);
197 btChunk* chunk = serializer->allocate(sz,numElem);
198 btVector3Data* memPtr = (btVector3Data*)chunk->m_oldPtr;
199 for (int i=0;i<numElem;i++,memPtr++)
200 {
201 m_unscaledPoints[i].serialize(*memPtr);
202 }
203 serializer->finalizeChunk(chunk,btVector3DataName,BT_ARRAY_CODE,(void*)&m_unscaledPoints[0]);
204 }
205
206 return "btConvexHullShapeData";
207 }
208
209 void btConvexHullShape::project(const btTransform& trans, const btVector3& dir, btScalar& minProj, btScalar& maxProj, btVector3& witnesPtMin,btVector3& witnesPtMax) const
210 {
211 #if 1
212 minProj = FLT_MAX;
213 maxProj = -FLT_MAX;
214
215 int numVerts = m_unscaledPoints.size();
216 for(int i=0;i<numVerts;i++)
217 {
218 btVector3 vtx = m_unscaledPoints[i] * m_localScaling;
219 btVector3 pt = trans * vtx;
220 btScalar dp = pt.dot(dir);
221 if(dp < minProj)
222 {
223 minProj = dp;
224 witnesPtMin = pt;
225 }
226 if(dp > maxProj)
227 {
228 maxProj = dp;
229 witnesPtMax=pt;
230 }
231 }
232 #else
233 btVector3 localAxis = dir*trans.getBasis();
234 witnesPtMin = trans(localGetSupportingVertex(localAxis));
235 witnesPtMax = trans(localGetSupportingVertex(-localAxis));
236
237 minProj = witnesPtMin.dot(dir);
238 maxProj = witnesPtMax.dot(dir);
239 #endif
240
241 if(minProj>maxProj)
242 {
243 btSwap(minProj,maxProj);
244 btSwap(witnesPtMin,witnesPtMax);
245 }
246
247
248 }
249
250
SIMD_EPSILON
#define SIMD_EPSILON
Definition: btScalar.h:494
btAlignedObjectArray::push_back
void push_back(const T &_Val)
Definition: btAlignedObjectArray.h:276
BT_LARGE_FLOAT
#define BT_LARGE_FLOAT
Definition: btScalar.h:280
btChunk
Definition: btSerializer.h:51
btVector3::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:640
btConvexHullShape::isInside
virtual bool isInside(const btVector3 &pt, btScalar tolerance) const
Definition: btConvexHullShape.cpp:169
btConvexHullShape::getNumPlanes
virtual int getNumPlanes() const
Definition: btConvexHullShape.cpp:157
btConvexHullShape::getNumEdges
virtual int getNumEdges() const
Definition: btConvexHullShape.cpp:138
btConvexHullShape::localGetSupportingVertex
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
Definition: btConvexHullShape.cpp:106
btConvexInternalShape::serialize
virtual const char * serialize(void *dataBuffer, btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
Definition: btConvexInternalShape.h:166
btSerializer::getUniquePointer
virtual void * getUniquePointer(void *oldPtr)=0
btConvexHullShapeData::m_convexInternalShapeData
btConvexInternalShapeData m_convexInternalShapeData
Definition: btConvexHullShape.h:104
btConvexHullShapeData::m_unscaledPointsDoublePtr
btVector3DoubleData * m_unscaledPointsDoublePtr
Definition: btConvexHullShape.h:107
btAssert
#define btAssert(x)
Definition: btScalar.h:113
btVector3::maxDot
long maxDot(const btVector3 *array, long array_count, btScalar &dotOut) const
returns index of maximum dot product between this and vectors in array[]
Definition: btVector3.h:1001
btConvexHullShape::batchedUnitVectorGetSupportingVertexWithoutMargin
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
Definition: btConvexHullShape.cpp:76
btConvexHullShape::m_unscaledPoints
btAlignedObjectArray< btVector3 > m_unscaledPoints
Definition: btConvexHullShape.h:28
btVector3Data
#define btVector3Data
Definition: btVector3.h:29
btConvexInternalShape::m_localScaling
btVector3 m_localScaling
Definition: btConvexInternalShape.h:35
btConvexHullShape::getVertex
virtual void getVertex(int i, btVector3 &vtx) const
Definition: btConvexHullShape.cpp:152
btCollisionMargin.h
btConvexHullShape::getScaledPoint
btVector3 getScaledPoint(int i) const
Definition: btConvexHullShape.h:61
btVector3::dot
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:235
btConvexInternalShape::getMargin
virtual btScalar getMargin() const
Definition: btConvexInternalShape.h:113
btSerializer
Definition: btSerializer.h:68
btVector3::normalize
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition: btVector3.h:297
btConvexHullShape::btConvexHullShape
btConvexHullShape(const btScalar *points=0, int numPoints=0, int stride=sizeof(btVector3))
this constructor optionally takes in a pointer to points.
Definition: btConvexHullShape.cpp:26
btVector3DataName
#define btVector3DataName
Definition: btVector3.h:30
btConvexHullShape::getEdge
virtual void getEdge(int i, btVector3 &pa, btVector3 &pb) const
Definition: btConvexHullShape.cpp:143
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition: btAlignedObjectArray.h:149
btConvexHullShapeData
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btConvexHullShape.h:102
btQuaternion.h
btSwap
void btSwap(T &a, T &b)
Definition: btScalar.h:585
btTransform::getBasis
btMatrix3x3 & getBasis()
Return the basis matrix for the rotation.
Definition: btTransform.h:112
btConvexHullShape::getNumVertices
virtual int getNumVertices() const
Definition: btConvexHullShape.cpp:133
btConvexHullShapeData::m_unscaledPointsFloatPtr
btVector3FloatData * m_unscaledPointsFloatPtr
Definition: btConvexHullShape.h:106
btConvexHullShape::project
virtual void project(const btTransform &trans, const btVector3 &dir, btScalar &minProj, btScalar &maxProj, btVector3 &witnesPtMin, btVector3 &witnesPtMax) const
Definition: btConvexHullShape.cpp:209
BT_ARRAY_CODE
#define BT_ARRAY_CODE
Definition: btSerializer.h:124
btConvexHullShape::serialize
virtual const char * serialize(void *dataBuffer, btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
Definition: btConvexHullShape.cpp:176
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btPolyhedralConvexAabbCachingShape
The btPolyhedralConvexAabbCachingShape adds aabb caching to the btPolyhedralConvexShape.
Definition: btPolyhedralConvexShape.h:73
btSerializer::finalizeChunk
virtual void finalizeChunk(btChunk *chunk, const char *structType, int chunkCode, void *oldPtr)=0
btConvexHullShape::setLocalScaling
virtual void setLocalScaling(const btVector3 &scaling)
in case we receive negative scaling
Definition: btConvexHullShape.cpp:46
btAlignedObjectArray::resize
void resize(int newsize, const T &fillData=T())
Definition: btAlignedObjectArray.h:220
CONVEX_HULL_SHAPE_PROXYTYPE
Definition: btBroadphaseProxy.h:35
btConvexHullShape::addPoint
void addPoint(const btVector3 &point, bool recalculateLocalAabb=true)
Definition: btConvexHullShape.cpp:52
btConvexHullShape.h
btConvexHullShape::getPlane
virtual void getPlane(btVector3 &planeNormal, btVector3 &planeSupport, int i) const
Definition: btConvexHullShape.cpp:162
btCollisionShape::m_shapeType
int m_shapeType
Definition: btCollisionShape.h:30
btChunk::m_oldPtr
void * m_oldPtr
Definition: btSerializer.h:56
btPolyhedralConvexAabbCachingShape::recalcLocalAabb
void recalcLocalAabb()
Definition: btPolyhedralConvexShape.cpp:450
btSerializer::allocate
virtual btChunk * allocate(size_t size, int numElements)=0
btConvexHullShape::localGetSupportingVertexWithoutMargin
virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &vec) const
Definition: btConvexHullShape.cpp:60
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:278
btSerializer.h
btConvexHullShapeData::m_numUnscaledPoints
int m_numUnscaledPoints
Definition: btConvexHullShape.h:109