2017-08-01 12:30:58 +00:00
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
|
|
|
Copyright (c) 2011 Advanced Micro Devices, Inc. http://bulletphysics.org
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
In no event will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it freely,
|
|
|
|
subject to the following restrictions:
|
|
|
|
|
|
|
|
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.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
///This file was written by Erwin Coumans
|
|
|
|
///Separating axis rest based on work from Pierre Terdiman, see
|
|
|
|
///And contact clipping based on work from Simon Hobbs
|
|
|
|
|
|
|
|
#include "btConvexPolyhedron.h"
|
|
|
|
#include "LinearMath/btHashMap.h"
|
|
|
|
|
|
|
|
btConvexPolyhedron::btConvexPolyhedron()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
btConvexPolyhedron::~btConvexPolyhedron()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-11 11:18:05 +00:00
|
|
|
inline bool IsAlmostZero1(const btVector3& v)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (btFabs(v.x()) > 1e-6 || btFabs(v.y()) > 1e-6 || btFabs(v.z()) > 1e-6) return false;
|
2017-08-01 12:30:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct btInternalVertexPair
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btInternalVertexPair(short int v0, short int v1)
|
|
|
|
: m_v0(v0),
|
|
|
|
m_v1(v1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (m_v1 > m_v0)
|
|
|
|
btSwap(m_v0, m_v1);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
short int m_v0;
|
|
|
|
short int m_v1;
|
|
|
|
int getHash() const
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return m_v0 + (m_v1 << 16);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
bool equals(const btInternalVertexPair& other) const
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
return m_v0 == other.m_v0 && m_v1 == other.m_v1;
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct btInternalEdge
|
|
|
|
{
|
|
|
|
btInternalEdge()
|
2019-01-03 13:26:51 +00:00
|
|
|
: m_face0(-1),
|
|
|
|
m_face1(-1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
short int m_face0;
|
|
|
|
short int m_face1;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifdef TEST_INTERNAL_OBJECTS
|
|
|
|
bool btConvexPolyhedron::testContainment() const
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int p = 0; p < 8; p++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btVector3 LocalPt;
|
2019-01-03 13:26:51 +00:00
|
|
|
if (p == 0)
|
|
|
|
LocalPt = m_localCenter + btVector3(m_extents[0], m_extents[1], m_extents[2]);
|
|
|
|
else if (p == 1)
|
|
|
|
LocalPt = m_localCenter + btVector3(m_extents[0], m_extents[1], -m_extents[2]);
|
|
|
|
else if (p == 2)
|
|
|
|
LocalPt = m_localCenter + btVector3(m_extents[0], -m_extents[1], m_extents[2]);
|
|
|
|
else if (p == 3)
|
|
|
|
LocalPt = m_localCenter + btVector3(m_extents[0], -m_extents[1], -m_extents[2]);
|
|
|
|
else if (p == 4)
|
|
|
|
LocalPt = m_localCenter + btVector3(-m_extents[0], m_extents[1], m_extents[2]);
|
|
|
|
else if (p == 5)
|
|
|
|
LocalPt = m_localCenter + btVector3(-m_extents[0], m_extents[1], -m_extents[2]);
|
|
|
|
else if (p == 6)
|
|
|
|
LocalPt = m_localCenter + btVector3(-m_extents[0], -m_extents[1], m_extents[2]);
|
|
|
|
else if (p == 7)
|
|
|
|
LocalPt = m_localCenter + btVector3(-m_extents[0], -m_extents[1], -m_extents[2]);
|
|
|
|
|
|
|
|
for (int i = 0; i < m_faces.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btVector3 Normal(m_faces[i].m_plane[0], m_faces[i].m_plane[1], m_faces[i].m_plane[2]);
|
|
|
|
const btScalar d = LocalPt.dot(Normal) + m_faces[i].m_plane[3];
|
2019-01-03 13:26:51 +00:00
|
|
|
if (d > 0.0f)
|
2017-08-01 12:30:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConvexPolyhedron::initialize()
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btHashMap<btInternalVertexPair, btInternalEdge> edges;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < m_faces.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int numVertices = m_faces[i].m_indices.size();
|
|
|
|
int NbTris = numVertices;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int j = 0; j < NbTris; j++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
int k = (j + 1) % numVertices;
|
|
|
|
btInternalVertexPair vp(m_faces[i].m_indices[j], m_faces[i].m_indices[k]);
|
2017-08-01 12:30:58 +00:00
|
|
|
btInternalEdge* edptr = edges.find(vp);
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 edge = m_vertices[vp.m_v1] - m_vertices[vp.m_v0];
|
2017-08-01 12:30:58 +00:00
|
|
|
edge.normalize();
|
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int p = 0; p < m_uniqueEdges.size(); p++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-06-11 11:18:05 +00:00
|
|
|
if (IsAlmostZero1(m_uniqueEdges[p] - edge) ||
|
|
|
|
IsAlmostZero1(m_uniqueEdges[p] + edge))
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
m_uniqueEdges.push_back(edge);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (edptr)
|
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btAssert(edptr->m_face0 >= 0);
|
|
|
|
btAssert(edptr->m_face1 < 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
edptr->m_face1 = i;
|
2019-01-03 13:26:51 +00:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btInternalEdge ed;
|
|
|
|
ed.m_face0 = i;
|
2019-01-03 13:26:51 +00:00
|
|
|
edges.insert(vp, ed);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_CONNECTED_FACES
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < m_faces.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int numVertices = m_faces[i].m_indices.size();
|
|
|
|
m_faces[i].m_connectedFaces.resize(numVertices);
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int j = 0; j < numVertices; j++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
int k = (j + 1) % numVertices;
|
|
|
|
btInternalVertexPair vp(m_faces[i].m_indices[j], m_faces[i].m_indices[k]);
|
2017-08-01 12:30:58 +00:00
|
|
|
btInternalEdge* edptr = edges.find(vp);
|
|
|
|
btAssert(edptr);
|
2019-01-03 13:26:51 +00:00
|
|
|
btAssert(edptr->m_face0 >= 0);
|
|
|
|
btAssert(edptr->m_face1 >= 0);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
int connectedFace = (edptr->m_face0 == i) ? edptr->m_face1 : edptr->m_face0;
|
2017-08-01 12:30:58 +00:00
|
|
|
m_faces[i].m_connectedFaces[j] = connectedFace;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
#endif //USE_CONNECTED_FACES
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2018-09-07 14:11:04 +00:00
|
|
|
initialize2();
|
|
|
|
}
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConvexPolyhedron::initialize2()
|
2018-09-07 14:11:04 +00:00
|
|
|
{
|
|
|
|
m_localCenter.setValue(0, 0, 0);
|
|
|
|
btScalar TotalArea = 0.0f;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < m_faces.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
int numVertices = m_faces[i].m_indices.size();
|
2019-01-03 13:26:51 +00:00
|
|
|
int NbTris = numVertices - 2;
|
|
|
|
|
2017-08-01 12:30:58 +00:00
|
|
|
const btVector3& p0 = m_vertices[m_faces[i].m_indices[0]];
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int j = 1; j <= NbTris; j++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
int k = (j + 1) % numVertices;
|
2017-08-01 12:30:58 +00:00
|
|
|
const btVector3& p1 = m_vertices[m_faces[i].m_indices[j]];
|
|
|
|
const btVector3& p2 = m_vertices[m_faces[i].m_indices[k]];
|
|
|
|
btScalar Area = ((p0 - p1).cross(p0 - p2)).length() * 0.5f;
|
2019-01-03 13:26:51 +00:00
|
|
|
btVector3 Center = (p0 + p1 + p2) / 3.0f;
|
2017-08-01 12:30:58 +00:00
|
|
|
m_localCenter += Area * Center;
|
|
|
|
TotalArea += Area;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_localCenter /= TotalArea;
|
|
|
|
|
|
|
|
#ifdef TEST_INTERNAL_OBJECTS
|
2019-01-03 13:26:51 +00:00
|
|
|
if (1)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_radius = FLT_MAX;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < m_faces.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btVector3 Normal(m_faces[i].m_plane[0], m_faces[i].m_plane[1], m_faces[i].m_plane[2]);
|
|
|
|
const btScalar dist = btFabs(m_localCenter.dot(Normal) + m_faces[i].m_plane[3]);
|
2019-01-03 13:26:51 +00:00
|
|
|
if (dist < m_radius)
|
2017-08-01 12:30:58 +00:00
|
|
|
m_radius = dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
btScalar MinX = FLT_MAX;
|
|
|
|
btScalar MinY = FLT_MAX;
|
|
|
|
btScalar MinZ = FLT_MAX;
|
|
|
|
btScalar MaxX = -FLT_MAX;
|
|
|
|
btScalar MaxY = -FLT_MAX;
|
|
|
|
btScalar MaxZ = -FLT_MAX;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < m_vertices.size(); i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btVector3& pt = m_vertices[i];
|
2019-01-03 13:26:51 +00:00
|
|
|
if (pt.x() < MinX) MinX = pt.x();
|
|
|
|
if (pt.x() > MaxX) MaxX = pt.x();
|
|
|
|
if (pt.y() < MinY) MinY = pt.y();
|
|
|
|
if (pt.y() > MaxY) MaxY = pt.y();
|
|
|
|
if (pt.z() < MinZ) MinZ = pt.z();
|
|
|
|
if (pt.z() > MaxZ) MaxZ = pt.z();
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
mC.setValue(MaxX + MinX, MaxY + MinY, MaxZ + MinZ);
|
|
|
|
mE.setValue(MaxX - MinX, MaxY - MinY, MaxZ - MinZ);
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
// const btScalar r = m_radius / sqrtf(2.0f);
|
2017-08-01 12:30:58 +00:00
|
|
|
const btScalar r = m_radius / sqrtf(3.0f);
|
|
|
|
const int LargestExtent = mE.maxAxis();
|
2019-01-03 13:26:51 +00:00
|
|
|
const btScalar Step = (mE[LargestExtent] * 0.5f - r) / 1024.0f;
|
2017-08-01 12:30:58 +00:00
|
|
|
m_extents[0] = m_extents[1] = m_extents[2] = r;
|
2019-01-03 13:26:51 +00:00
|
|
|
m_extents[LargestExtent] = mE[LargestExtent] * 0.5f;
|
2017-08-01 12:30:58 +00:00
|
|
|
bool FoundBox = false;
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int j = 0; j < 1024; j++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
if (testContainment())
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
FoundBox = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_extents[LargestExtent] -= Step;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!FoundBox)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_extents[0] = m_extents[1] = m_extents[2] = r;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Refine the box
|
2019-01-03 13:26:51 +00:00
|
|
|
const btScalar Step = (m_radius - r) / 1024.0f;
|
|
|
|
const int e0 = (1 << LargestExtent) & 3;
|
|
|
|
const int e1 = (1 << e0) & 3;
|
2017-08-01 12:30:58 +00:00
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int j = 0; j < 1024; j++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
const btScalar Saved0 = m_extents[e0];
|
|
|
|
const btScalar Saved1 = m_extents[e1];
|
|
|
|
m_extents[e0] += Step;
|
|
|
|
m_extents[e1] += Step;
|
|
|
|
|
2019-01-03 13:26:51 +00:00
|
|
|
if (!testContainment())
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
m_extents[e0] = Saved0;
|
|
|
|
m_extents[e1] = Saved1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
void btConvexPolyhedron::project(const btTransform& trans, const btVector3& dir, btScalar& minProj, btScalar& maxProj, btVector3& witnesPtMin, btVector3& witnesPtMax) const
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
minProj = FLT_MAX;
|
|
|
|
maxProj = -FLT_MAX;
|
|
|
|
int numVerts = m_vertices.size();
|
2019-01-03 13:26:51 +00:00
|
|
|
for (int i = 0; i < numVerts; i++)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
btVector3 pt = trans * m_vertices[i];
|
|
|
|
btScalar dp = pt.dot(dir);
|
2019-01-03 13:26:51 +00:00
|
|
|
if (dp < minProj)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
minProj = dp;
|
|
|
|
witnesPtMin = pt;
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (dp > maxProj)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
|
|
|
maxProj = dp;
|
|
|
|
witnesPtMax = pt;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 13:26:51 +00:00
|
|
|
if (minProj > maxProj)
|
2017-08-01 12:30:58 +00:00
|
|
|
{
|
2019-01-03 13:26:51 +00:00
|
|
|
btSwap(minProj, maxProj);
|
|
|
|
btSwap(witnesPtMin, witnesPtMax);
|
2017-08-01 12:30:58 +00:00
|
|
|
}
|
|
|
|
}
|