2
Bullet Continuous Collision Detection and Physics Library
3
Copyright (c) 2011 Advanced Micro Devices, Inc. http://bulletphysics.org
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:
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.
17
///This file was written by Erwin Coumans
18
///Separating axis rest based on work from Pierre Terdiman, see
19
///And contact clipping based on work from Simon Hobbs
22
#include "btConvexPolyhedron.h"
23
#include "LinearMath/btHashMap.h"
25
btConvexPolyhedron::btConvexPolyhedron()
29
btConvexPolyhedron::~btConvexPolyhedron()
35
inline bool IsAlmostZero(const btVector3& v)
37
if(fabsf(v.x())>1e-6 || fabsf(v.y())>1e-6 || fabsf(v.z())>1e-6) return false;
41
struct btInternalVertexPair
43
btInternalVertexPair(short int v0,short int v1)
54
return m_v0+(m_v1<<16);
56
bool equals(const btInternalVertexPair& other) const
58
return m_v0==other.m_v0 && m_v1==other.m_v1;
75
void btConvexPolyhedron::initialize()
77
btHashMap<btInternalVertexPair,btInternalEdge> edges;
79
float TotalArea = 0.0f;
81
m_localCenter.setValue(0, 0, 0);
82
for(int i=0;i<m_faces.size();i++)
84
int numVertices = m_faces[i].m_indices.size();
85
int NbTris = numVertices;
86
for(int j=0;j<NbTris;j++)
88
int k = (j+1)%numVertices;
89
btInternalVertexPair vp(m_faces[i].m_indices[j],m_faces[i].m_indices[k]);
90
btInternalEdge* edptr = edges.find(vp);
91
btVector3 edge = m_vertices[vp.m_v1]-m_vertices[vp.m_v0];
96
for (int p=0;p<m_uniqueEdges.size();p++)
99
if (IsAlmostZero(m_uniqueEdges[p]-edge) ||
100
IsAlmostZero(m_uniqueEdges[p]+edge))
109
m_uniqueEdges.push_back(edge);
114
btAssert(edptr->m_face0>=0);
115
btAssert(edptr->m_face1<0);
126
for(int i=0;i<m_faces.size();i++)
128
int numVertices = m_faces[i].m_indices.size();
129
m_faces[i].m_connectedFaces.resize(numVertices);
131
for(int j=0;j<numVertices;j++)
133
int k = (j+1)%numVertices;
134
btInternalVertexPair vp(m_faces[i].m_indices[j],m_faces[i].m_indices[k]);
135
btInternalEdge* edptr = edges.find(vp);
137
btAssert(edptr->m_face0>=0);
138
btAssert(edptr->m_face1>=0);
140
int connectedFace = (edptr->m_face0==i)?edptr->m_face1:edptr->m_face0;
141
m_faces[i].m_connectedFaces[j] = connectedFace;
145
for(int i=0;i<m_faces.size();i++)
147
int numVertices = m_faces[i].m_indices.size();
148
int NbTris = numVertices-2;
150
const btVector3& p0 = m_vertices[m_faces[i].m_indices[0]];
151
for(int j=1;j<=NbTris;j++)
153
int k = (j+1)%numVertices;
154
const btVector3& p1 = m_vertices[m_faces[i].m_indices[j]];
155
const btVector3& p2 = m_vertices[m_faces[i].m_indices[k]];
156
float Area = ((p0 - p1).cross(p0 - p2)).length() * 0.5f;
157
btVector3 Center = (p0+p1+p2)/3.0f;
158
m_localCenter += Area * Center;
162
m_localCenter /= TotalArea;
167
void btConvexPolyhedron::project(const btTransform& trans, const btVector3& dir, float& min, float& max) const
171
int numVerts = m_vertices.size();
172
for(int i=0;i<numVerts;i++)
174
btVector3 pt = trans * m_vertices[i];
175
float dp = pt.dot(dir);
176
if(dp < min) min = dp;
177
if(dp > max) max = dp;
b'\\ No newline at end of file'