Bullet Collision Detection & Physics Library
btConvexPolyhedron.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2011 Advanced Micro Devices, Inc. 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 
20 
21 #include "btConvexPolyhedron.h"
22 #include "LinearMath/btHashMap.h"
23 
24 
26 {
27 
28 }
30 {
31 
32 }
33 
34 
35 inline bool IsAlmostZero(const btVector3& v)
36 {
37  if(btFabs(v.x())>1e-6 || btFabs(v.y())>1e-6 || btFabs(v.z())>1e-6) return false;
38  return true;
39 }
40 
42 {
43  btInternalVertexPair(short int v0,short int v1)
44  :m_v0(v0),
45  m_v1(v1)
46  {
47  if (m_v1>m_v0)
48  btSwap(m_v0,m_v1);
49  }
50  short int m_v0;
51  short int m_v1;
52  int getHash() const
53  {
54  return m_v0+(m_v1<<16);
55  }
56  bool equals(const btInternalVertexPair& other) const
57  {
58  return m_v0==other.m_v0 && m_v1==other.m_v1;
59  }
60 };
61 
63 {
65  :m_face0(-1),
66  m_face1(-1)
67  {
68  }
69  short int m_face0;
70  short int m_face1;
71 };
72 
73 //
74 
75 #ifdef TEST_INTERNAL_OBJECTS
77 {
78  for(int p=0;p<8;p++)
79  {
80  btVector3 LocalPt;
81  if(p==0) LocalPt = m_localCenter + btVector3(m_extents[0], m_extents[1], m_extents[2]);
82  else if(p==1) LocalPt = m_localCenter + btVector3(m_extents[0], m_extents[1], -m_extents[2]);
83  else if(p==2) LocalPt = m_localCenter + btVector3(m_extents[0], -m_extents[1], m_extents[2]);
84  else if(p==3) LocalPt = m_localCenter + btVector3(m_extents[0], -m_extents[1], -m_extents[2]);
85  else if(p==4) LocalPt = m_localCenter + btVector3(-m_extents[0], m_extents[1], m_extents[2]);
86  else if(p==5) LocalPt = m_localCenter + btVector3(-m_extents[0], m_extents[1], -m_extents[2]);
87  else if(p==6) LocalPt = m_localCenter + btVector3(-m_extents[0], -m_extents[1], m_extents[2]);
88  else if(p==7) LocalPt = m_localCenter + btVector3(-m_extents[0], -m_extents[1], -m_extents[2]);
89 
90  for(int i=0;i<m_faces.size();i++)
91  {
92  const btVector3 Normal(m_faces[i].m_plane[0], m_faces[i].m_plane[1], m_faces[i].m_plane[2]);
93  const btScalar d = LocalPt.dot(Normal) + m_faces[i].m_plane[3];
94  if(d>0.0f)
95  return false;
96  }
97  }
98  return true;
99 }
100 #endif
101 
103 {
104 
106 
107 
108 
109 
110  for(int i=0;i<m_faces.size();i++)
111  {
112  int numVertices = m_faces[i].m_indices.size();
113  int NbTris = numVertices;
114  for(int j=0;j<NbTris;j++)
115  {
116  int k = (j+1)%numVertices;
117  btInternalVertexPair vp(m_faces[i].m_indices[j],m_faces[i].m_indices[k]);
118  btInternalEdge* edptr = edges.find(vp);
119  btVector3 edge = m_vertices[vp.m_v1]-m_vertices[vp.m_v0];
120  edge.normalize();
121 
122  bool found = false;
123 
124  for (int p=0;p<m_uniqueEdges.size();p++)
125  {
126 
127  if (IsAlmostZero(m_uniqueEdges[p]-edge) ||
128  IsAlmostZero(m_uniqueEdges[p]+edge))
129  {
130  found = true;
131  break;
132  }
133  }
134 
135  if (!found)
136  {
137  m_uniqueEdges.push_back(edge);
138  }
139 
140  if (edptr)
141  {
142  btAssert(edptr->m_face0>=0);
143  btAssert(edptr->m_face1<0);
144  edptr->m_face1 = i;
145  } else
146  {
147  btInternalEdge ed;
148  ed.m_face0 = i;
149  edges.insert(vp,ed);
150  }
151  }
152  }
153 
154 #ifdef USE_CONNECTED_FACES
155  for(int i=0;i<m_faces.size();i++)
156  {
157  int numVertices = m_faces[i].m_indices.size();
158  m_faces[i].m_connectedFaces.resize(numVertices);
159 
160  for(int j=0;j<numVertices;j++)
161  {
162  int k = (j+1)%numVertices;
163  btInternalVertexPair vp(m_faces[i].m_indices[j],m_faces[i].m_indices[k]);
164  btInternalEdge* edptr = edges.find(vp);
165  btAssert(edptr);
166  btAssert(edptr->m_face0>=0);
167  btAssert(edptr->m_face1>=0);
168 
169  int connectedFace = (edptr->m_face0==i)?edptr->m_face1:edptr->m_face0;
170  m_faces[i].m_connectedFaces[j] = connectedFace;
171  }
172  }
173 #endif//USE_CONNECTED_FACES
174 
175  initialize2();
176 }
177 
179 {
180  m_localCenter.setValue(0, 0, 0);
181  btScalar TotalArea = 0.0f;
182  for(int i=0;i<m_faces.size();i++)
183  {
184  int numVertices = m_faces[i].m_indices.size();
185  int NbTris = numVertices-2;
186 
187  const btVector3& p0 = m_vertices[m_faces[i].m_indices[0]];
188  for(int j=1;j<=NbTris;j++)
189  {
190  int k = (j+1)%numVertices;
191  const btVector3& p1 = m_vertices[m_faces[i].m_indices[j]];
192  const btVector3& p2 = m_vertices[m_faces[i].m_indices[k]];
193  btScalar Area = ((p0 - p1).cross(p0 - p2)).length() * 0.5f;
194  btVector3 Center = (p0+p1+p2)/3.0f;
195  m_localCenter += Area * Center;
196  TotalArea += Area;
197  }
198  }
199  m_localCenter /= TotalArea;
200 
201 
202 
203 
204 #ifdef TEST_INTERNAL_OBJECTS
205  if(1)
206  {
207  m_radius = FLT_MAX;
208  for(int i=0;i<m_faces.size();i++)
209  {
210  const btVector3 Normal(m_faces[i].m_plane[0], m_faces[i].m_plane[1], m_faces[i].m_plane[2]);
211  const btScalar dist = btFabs(m_localCenter.dot(Normal) + m_faces[i].m_plane[3]);
212  if(dist<m_radius)
213  m_radius = dist;
214  }
215 
216 
217  btScalar MinX = FLT_MAX;
218  btScalar MinY = FLT_MAX;
219  btScalar MinZ = FLT_MAX;
220  btScalar MaxX = -FLT_MAX;
221  btScalar MaxY = -FLT_MAX;
222  btScalar MaxZ = -FLT_MAX;
223  for(int i=0; i<m_vertices.size(); i++)
224  {
225  const btVector3& pt = m_vertices[i];
226  if(pt.x()<MinX) MinX = pt.x();
227  if(pt.x()>MaxX) MaxX = pt.x();
228  if(pt.y()<MinY) MinY = pt.y();
229  if(pt.y()>MaxY) MaxY = pt.y();
230  if(pt.z()<MinZ) MinZ = pt.z();
231  if(pt.z()>MaxZ) MaxZ = pt.z();
232  }
233  mC.setValue(MaxX+MinX, MaxY+MinY, MaxZ+MinZ);
234  mE.setValue(MaxX-MinX, MaxY-MinY, MaxZ-MinZ);
235 
236 
237 
238 // const btScalar r = m_radius / sqrtf(2.0f);
239  const btScalar r = m_radius / sqrtf(3.0f);
240  const int LargestExtent = mE.maxAxis();
241  const btScalar Step = (mE[LargestExtent]*0.5f - r)/1024.0f;
242  m_extents[0] = m_extents[1] = m_extents[2] = r;
243  m_extents[LargestExtent] = mE[LargestExtent]*0.5f;
244  bool FoundBox = false;
245  for(int j=0;j<1024;j++)
246  {
247  if(testContainment())
248  {
249  FoundBox = true;
250  break;
251  }
252 
253  m_extents[LargestExtent] -= Step;
254  }
255  if(!FoundBox)
256  {
257  m_extents[0] = m_extents[1] = m_extents[2] = r;
258  }
259  else
260  {
261  // Refine the box
262  const btScalar Step = (m_radius - r)/1024.0f;
263  const int e0 = (1<<LargestExtent) & 3;
264  const int e1 = (1<<e0) & 3;
265 
266  for(int j=0;j<1024;j++)
267  {
268  const btScalar Saved0 = m_extents[e0];
269  const btScalar Saved1 = m_extents[e1];
270  m_extents[e0] += Step;
271  m_extents[e1] += Step;
272 
273  if(!testContainment())
274  {
275  m_extents[e0] = Saved0;
276  m_extents[e1] = Saved1;
277  break;
278  }
279  }
280  }
281  }
282 #endif
283 }
284 void btConvexPolyhedron::project(const btTransform& trans, const btVector3& dir, btScalar& minProj, btScalar& maxProj, btVector3& witnesPtMin,btVector3& witnesPtMax) const
285 {
286  minProj = FLT_MAX;
287  maxProj = -FLT_MAX;
288  int numVerts = m_vertices.size();
289  for(int i=0;i<numVerts;i++)
290  {
291  btVector3 pt = trans * m_vertices[i];
292  btScalar dp = pt.dot(dir);
293  if(dp < minProj)
294  {
295  minProj = dp;
296  witnesPtMin = pt;
297  }
298  if(dp > maxProj)
299  {
300  maxProj = dp;
301  witnesPtMax = pt;
302  }
303  }
304  if(minProj>maxProj)
305  {
306  btSwap(minProj,maxProj);
307  btSwap(witnesPtMin,witnesPtMax);
308  }
309 }
btAlignedObjectArray< btVector3 > m_vertices
btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:906
void push_back(const T &_Val)
bool equals(const btInternalVertexPair &other) const
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:652
#define btAssert(x)
Definition: btScalar.h:131
btAlignedObjectArray< btVector3 > m_uniqueEdges
bool IsAlmostZero(const btVector3 &v)
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:235
The btHashMap template class implements a generic and lightweight hashmap.
Definition: btHashMap.h:213
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition: btVector3.h:309
btInternalVertexPair(short int v0, short int v1)
const btScalar & x() const
Return the x value.
Definition: btVector3.h:587
int size() const
return the number of elements in the array
btAlignedObjectArray< btFace > m_faces
void btSwap(T &a, T &b)
Definition: btScalar.h:621
btConvexPolyhedron()
This file was written by Erwin Coumans Separating axis rest based on work from Pierre Terdiman...
void insert(const Key &key, const Value &value)
Definition: btHashMap.h:262
const btScalar & y() const
Return the y value.
Definition: btVector3.h:589
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
const Value * find(const Key &key) const
Definition: btHashMap.h:422
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
void resize(int newsize, const T &fillData=T())
bool testContainment() const
void project(const btTransform &trans, const btVector3 &dir, btScalar &minProj, btScalar &maxProj, btVector3 &witnesPtMin, btVector3 &witnesPtMax) const
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
int maxAxis() const
Return the axis with the largest value Note return values are 0,1,2 for x, y, or z.
Definition: btVector3.h:487
btScalar btFabs(btScalar x)
Definition: btScalar.h:475
const btScalar & z() const
Return the z value.
Definition: btVector3.h:591