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 #include "btConvexHullShape.h"
18 
21 
23 {
25  m_unscaledPoints.resize(numPoints);
26 
27  unsigned char* pointsAddress = (unsigned char*)points;
28 
29  for (int i=0;i<numPoints;i++)
30  {
31  btScalar* point = (btScalar*)pointsAddress;
32  m_unscaledPoints[i] = btVector3(point[0], point[1], point[2]);
33  pointsAddress += stride;
34  }
35 
37 
38 }
39 
40 
41 
43 {
44  m_localScaling = scaling;
46 }
47 
49 {
52 
53 }
54 
56 {
57  btVector3 supVec(btScalar(0.),btScalar(0.),btScalar(0.));
58  btScalar maxDot = btScalar(-BT_LARGE_FLOAT);
59 
60  // Here we take advantage of dot(a, b*c) = dot(a*b, c). Note: This is true mathematically, but not numerically.
61  if( 0 < m_unscaledPoints.size() )
62  {
63  btVector3 scaled = vec * m_localScaling;
64  int index = (int) scaled.maxDot( &m_unscaledPoints[0], m_unscaledPoints.size(), maxDot); // FIXME: may violate encapsulation of m_unscaledPoints
65  return m_unscaledPoints[index] * m_localScaling;
66  }
67 
68  return supVec;
69 }
70 
71 void btConvexHullShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
72 {
73  btScalar newDot;
74  //use 'w' component of supportVerticesOut?
75  {
76  for (int i=0;i<numVectors;i++)
77  {
78  supportVerticesOut[i][3] = btScalar(-BT_LARGE_FLOAT);
79  }
80  }
81 
82  for (int j=0;j<numVectors;j++)
83  {
84  btVector3 vec = vectors[j] * m_localScaling; // dot(a*b,c) = dot(a,b*c)
85  if( 0 < m_unscaledPoints.size() )
86  {
87  int i = (int) vec.maxDot( &m_unscaledPoints[0], m_unscaledPoints.size(), newDot);
88  supportVerticesOut[j] = getScaledPoint(i);
89  supportVerticesOut[j][3] = newDot;
90  }
91  else
92  supportVerticesOut[j][3] = -BT_LARGE_FLOAT;
93  }
94 
95 
96 
97 }
98 
99 
100 
102 {
104 
105  if ( getMargin()!=btScalar(0.) )
106  {
107  btVector3 vecnorm = vec;
108  if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
109  {
110  vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
111  }
112  vecnorm.normalize();
113  supVertex+= getMargin() * vecnorm;
114  }
115  return supVertex;
116 }
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 //currently just for debugging (drawing), perhaps future support for algebraic continuous collision detection
127 //Please note that you can debug-draw btConvexHullShape with the Raytracer Demo
129 {
130  return m_unscaledPoints.size();
131 }
132 
134 {
135  return m_unscaledPoints.size();
136 }
137 
139 {
140 
141  int index0 = i%m_unscaledPoints.size();
142  int index1 = (i+1)%m_unscaledPoints.size();
143  pa = getScaledPoint(index0);
144  pb = getScaledPoint(index1);
145 }
146 
148 {
149  vtx = getScaledPoint(i);
150 }
151 
153 {
154  return 0;
155 }
156 
158 {
159 
160  btAssert(0);
161 }
162 
163 //not yet
165 {
166  btAssert(0);
167  return false;
168 }
169 
171 const char* btConvexHullShape::serialize(void* dataBuffer, btSerializer* serializer) const
172 {
173  //int szc = sizeof(btConvexHullShapeData);
174  btConvexHullShapeData* shapeData = (btConvexHullShapeData*) dataBuffer;
176 
177  int numElem = m_unscaledPoints.size();
178  shapeData->m_numUnscaledPoints = numElem;
179 #ifdef BT_USE_DOUBLE_PRECISION
180  shapeData->m_unscaledPointsFloatPtr = 0;
181  shapeData->m_unscaledPointsDoublePtr = numElem ? (btVector3Data*)serializer->getUniquePointer((void*)&m_unscaledPoints[0]): 0;
182 #else
183  shapeData->m_unscaledPointsFloatPtr = numElem ? (btVector3Data*)serializer->getUniquePointer((void*)&m_unscaledPoints[0]): 0;
184  shapeData->m_unscaledPointsDoublePtr = 0;
185 #endif
186 
187  if (numElem)
188  {
189  int sz = sizeof(btVector3Data);
190  // int sz2 = sizeof(btVector3DoubleData);
191  // int sz3 = sizeof(btVector3FloatData);
192  btChunk* chunk = serializer->allocate(sz,numElem);
193  btVector3Data* memPtr = (btVector3Data*)chunk->m_oldPtr;
194  for (int i=0;i<numElem;i++,memPtr++)
195  {
196  m_unscaledPoints[i].serialize(*memPtr);
197  }
198  serializer->finalizeChunk(chunk,btVector3DataName,BT_ARRAY_CODE,(void*)&m_unscaledPoints[0]);
199  }
200 
201  return "btConvexHullShapeData";
202 }
203 
204 void btConvexHullShape::project(const btTransform& trans, const btVector3& dir, btScalar& minProj, btScalar& maxProj, btVector3& witnesPtMin,btVector3& witnesPtMax) const
205 {
206 #if 1
207  minProj = FLT_MAX;
208  maxProj = -FLT_MAX;
209 
210  int numVerts = m_unscaledPoints.size();
211  for(int i=0;i<numVerts;i++)
212  {
214  btVector3 pt = trans * vtx;
215  btScalar dp = pt.dot(dir);
216  if(dp < minProj)
217  {
218  minProj = dp;
219  witnesPtMin = pt;
220  }
221  if(dp > maxProj)
222  {
223  maxProj = dp;
224  witnesPtMax=pt;
225  }
226  }
227 #else
228  btVector3 localAxis = dir*trans.getBasis();
229  witnesPtMin = trans(localGetSupportingVertex(localAxis));
230  witnesPtMax = trans(localGetSupportingVertex(-localAxis));
231 
232  minProj = witnesPtMin.dot(dir);
233  maxProj = witnesPtMax.dot(dir);
234 #endif
235 
236  if(minProj>maxProj)
237  {
238  btSwap(minProj,maxProj);
239  btSwap(witnesPtMin,witnesPtMax);
240  }
241 
242 
243 }
244 
245