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