2017-08-01 14:30:58 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2012 Advanced Micro Devices, Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
//Originally written by Erwin Coumans
|
|
|
|
|
|
|
|
#include "b3ConvexUtility.h"
|
|
|
|
#include "Bullet3Geometry/b3ConvexHullComputer.h"
|
|
|
|
#include "Bullet3Geometry/b3GrahamScan2dConvexHull.h"
|
|
|
|
#include "Bullet3Common/b3Quaternion.h"
|
|
|
|
#include "Bullet3Common/b3HashMap.h"
|
|
|
|
|
|
|
|
b3ConvexUtility::~b3ConvexUtility()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
bool b3ConvexUtility::initializePolyhedralFeatures(const b3Vector3* orgVertices, int numPoints, bool mergeCoplanarTriangles)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
b3ConvexHullComputer conv;
|
2019-01-03 14:26:51 +01:00
|
|
|
conv.compute(&orgVertices[0].getX(), sizeof(b3Vector3), numPoints, 0.f, 0.f);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
b3AlignedObjectArray<b3Vector3> faceNormals;
|
|
|
|
int numFaces = conv.faces.size();
|
|
|
|
faceNormals.resize(numFaces);
|
|
|
|
b3ConvexHullComputer* convexUtil = &conv;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
b3AlignedObjectArray<b3MyFace> tmpFaces;
|
2017-08-01 14:30:58 +02:00
|
|
|
tmpFaces.resize(numFaces);
|
|
|
|
|
|
|
|
int numVertices = convexUtil->vertices.size();
|
|
|
|
m_vertices.resize(numVertices);
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int p = 0; p < numVertices; p++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_vertices[p] = convexUtil->vertices[p];
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < numFaces; i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
int face = convexUtil->faces[i];
|
|
|
|
//printf("face=%d\n",face);
|
2019-01-03 14:26:51 +01:00
|
|
|
const b3ConvexHullComputer::Edge* firstEdge = &convexUtil->edges[face];
|
|
|
|
const b3ConvexHullComputer::Edge* edge = firstEdge;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
b3Vector3 edges[3];
|
|
|
|
int numEdges = 0;
|
|
|
|
//compute face normals
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int src = edge->getSourceVertex();
|
|
|
|
tmpFaces[i].m_indices.push_back(src);
|
|
|
|
int targ = edge->getTargetVertex();
|
|
|
|
b3Vector3 wa = convexUtil->vertices[src];
|
|
|
|
|
|
|
|
b3Vector3 wb = convexUtil->vertices[targ];
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Vector3 newEdge = wb - wa;
|
2017-08-01 14:30:58 +02:00
|
|
|
newEdge.normalize();
|
2019-01-03 14:26:51 +01:00
|
|
|
if (numEdges < 2)
|
2017-08-01 14:30:58 +02:00
|
|
|
edges[numEdges++] = newEdge;
|
|
|
|
|
|
|
|
edge = edge->getNextEdgeOfFace();
|
2019-01-03 14:26:51 +01:00
|
|
|
} while (edge != firstEdge);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
b3Scalar planeEq = 1e30f;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (numEdges == 2)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
faceNormals[i] = edges[0].cross(edges[1]);
|
|
|
|
faceNormals[i].normalize();
|
|
|
|
tmpFaces[i].m_plane[0] = faceNormals[i].getX();
|
|
|
|
tmpFaces[i].m_plane[1] = faceNormals[i].getY();
|
|
|
|
tmpFaces[i].m_plane[2] = faceNormals[i].getZ();
|
|
|
|
tmpFaces[i].m_plane[3] = planeEq;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Assert(0); //degenerate?
|
2017-08-01 14:30:58 +02:00
|
|
|
faceNormals[i].setZero();
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int v = 0; v < tmpFaces[i].m_indices.size(); v++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
b3Scalar eq = m_vertices[tmpFaces[i].m_indices[v]].dot(faceNormals[i]);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (planeEq > eq)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
planeEq = eq;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tmpFaces[i].m_plane[3] = -planeEq;
|
|
|
|
}
|
|
|
|
|
|
|
|
//merge coplanar faces and copy them to m_polyhedron
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Scalar faceWeldThreshold = 0.999f;
|
2017-08-01 14:30:58 +02:00
|
|
|
b3AlignedObjectArray<int> todoFaces;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < tmpFaces.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
todoFaces.push_back(i);
|
|
|
|
|
|
|
|
while (todoFaces.size())
|
|
|
|
{
|
|
|
|
b3AlignedObjectArray<int> coplanarFaceGroup;
|
2019-01-03 14:26:51 +01:00
|
|
|
int refFace = todoFaces[todoFaces.size() - 1];
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
coplanarFaceGroup.push_back(refFace);
|
|
|
|
b3MyFace& faceA = tmpFaces[refFace];
|
|
|
|
todoFaces.pop_back();
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Vector3 faceNormalA = b3MakeVector3(faceA.m_plane[0], faceA.m_plane[1], faceA.m_plane[2]);
|
|
|
|
for (int j = todoFaces.size() - 1; j >= 0; j--)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
int i = todoFaces[j];
|
|
|
|
b3MyFace& faceB = tmpFaces[i];
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Vector3 faceNormalB = b3MakeVector3(faceB.m_plane[0], faceB.m_plane[1], faceB.m_plane[2]);
|
|
|
|
if (faceNormalA.dot(faceNormalB) > faceWeldThreshold)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
coplanarFaceGroup.push_back(i);
|
|
|
|
todoFaces.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool did_merge = false;
|
2019-01-03 14:26:51 +01:00
|
|
|
if (coplanarFaceGroup.size() > 1)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
//do the merge: use Graham Scan 2d convex hull
|
|
|
|
|
|
|
|
b3AlignedObjectArray<b3GrahamVector3> orgpoints;
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Vector3 averageFaceNormal = b3MakeVector3(0, 0, 0);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < coplanarFaceGroup.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
// m_polyhedron->m_faces.push_back(tmpFaces[coplanarFaceGroup[i]]);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
b3MyFace& face = tmpFaces[coplanarFaceGroup[i]];
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Vector3 faceNormal = b3MakeVector3(face.m_plane[0], face.m_plane[1], face.m_plane[2]);
|
|
|
|
averageFaceNormal += faceNormal;
|
|
|
|
for (int f = 0; f < face.m_indices.size(); f++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
int orgIndex = face.m_indices[f];
|
|
|
|
b3Vector3 pt = m_vertices[orgIndex];
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
bool found = false;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < orgpoints.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
//if ((orgpoints[i].m_orgIndex == orgIndex) || ((rotatedPt-orgpoints[i]).length2()<0.0001))
|
|
|
|
if (orgpoints[i].m_orgIndex == orgIndex)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
found = true;
|
2017-08-01 14:30:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
2019-01-03 14:26:51 +01:00
|
|
|
orgpoints.push_back(b3GrahamVector3(pt, orgIndex));
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b3MyFace combinedFace;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < 4; i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
combinedFace.m_plane[i] = tmpFaces[coplanarFaceGroup[0]].m_plane[i];
|
|
|
|
|
|
|
|
b3AlignedObjectArray<b3GrahamVector3> hull;
|
|
|
|
|
|
|
|
averageFaceNormal.normalize();
|
2019-01-03 14:26:51 +01:00
|
|
|
b3GrahamScanConvexHull2D(orgpoints, hull, averageFaceNormal);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < hull.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
combinedFace.m_indices.push_back(hull[i].m_orgIndex);
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int k = 0; k < orgpoints.size(); k++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (orgpoints[k].m_orgIndex == hull[i].m_orgIndex)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
orgpoints[k].m_orgIndex = -1; // invalidate...
|
2017-08-01 14:30:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// are there rejected vertices?
|
|
|
|
bool reject_merge = false;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < orgpoints.size(); i++)
|
|
|
|
{
|
|
|
|
if (orgpoints[i].m_orgIndex == -1)
|
|
|
|
continue; // this is in the hull...
|
2017-08-01 14:30:58 +02:00
|
|
|
// this vertex is rejected -- is anybody else using this vertex?
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int j = 0; j < tmpFaces.size(); j++)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
b3MyFace& face = tmpFaces[j];
|
|
|
|
// is this a face of the current coplanar group?
|
|
|
|
bool is_in_current_group = false;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int k = 0; k < coplanarFaceGroup.size(); k++)
|
|
|
|
{
|
|
|
|
if (coplanarFaceGroup[k] == j)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
is_in_current_group = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (is_in_current_group) // ignore this face...
|
2017-08-01 14:30:58 +02:00
|
|
|
continue;
|
|
|
|
// does this face use this rejected vertex?
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int v = 0; v < face.m_indices.size(); v++)
|
|
|
|
{
|
|
|
|
if (face.m_indices[v] == orgpoints[i].m_orgIndex)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
// this rejected vertex is used in another face -- reject merge
|
|
|
|
reject_merge = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (reject_merge)
|
2017-08-01 14:30:58 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (reject_merge)
|
2017-08-01 14:30:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!reject_merge)
|
|
|
|
{
|
|
|
|
// do this merge!
|
|
|
|
did_merge = true;
|
|
|
|
m_faces.push_back(combinedFace);
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (!did_merge)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < coplanarFaceGroup.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
b3MyFace face = tmpFaces[coplanarFaceGroup[i]];
|
|
|
|
m_faces.push_back(face);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
initialize();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsAlmostZero(const b3Vector3& v)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (fabsf(v.getX()) > 1e-6 || fabsf(v.getY()) > 1e-6 || fabsf(v.getZ()) > 1e-6) return false;
|
2017-08-01 14:30:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct b3InternalVertexPair
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
b3InternalVertexPair(short int v0, short int v1)
|
|
|
|
: m_v0(v0),
|
|
|
|
m_v1(v1)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (m_v1 > m_v0)
|
|
|
|
b3Swap(m_v0, m_v1);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
short int m_v0;
|
|
|
|
short int m_v1;
|
|
|
|
int getHash() const
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
return m_v0 + (m_v1 << 16);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
bool equals(const b3InternalVertexPair& other) const
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
return m_v0 == other.m_v0 && m_v1 == other.m_v1;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct b3InternalEdge
|
|
|
|
{
|
|
|
|
b3InternalEdge()
|
2019-01-03 14:26:51 +01:00
|
|
|
: m_face0(-1),
|
|
|
|
m_face1(-1)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
short int m_face0;
|
|
|
|
short int m_face1;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifdef TEST_INTERNAL_OBJECTS
|
|
|
|
bool b3ConvexUtility::testContainment() const
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int p = 0; p < 8; p++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
b3Vector3 LocalPt;
|
2019-01-03 14:26:51 +01:00
|
|
|
if (p == 0)
|
|
|
|
LocalPt = m_localCenter + b3Vector3(m_extents[0], m_extents[1], m_extents[2]);
|
|
|
|
else if (p == 1)
|
|
|
|
LocalPt = m_localCenter + b3Vector3(m_extents[0], m_extents[1], -m_extents[2]);
|
|
|
|
else if (p == 2)
|
|
|
|
LocalPt = m_localCenter + b3Vector3(m_extents[0], -m_extents[1], m_extents[2]);
|
|
|
|
else if (p == 3)
|
|
|
|
LocalPt = m_localCenter + b3Vector3(m_extents[0], -m_extents[1], -m_extents[2]);
|
|
|
|
else if (p == 4)
|
|
|
|
LocalPt = m_localCenter + b3Vector3(-m_extents[0], m_extents[1], m_extents[2]);
|
|
|
|
else if (p == 5)
|
|
|
|
LocalPt = m_localCenter + b3Vector3(-m_extents[0], m_extents[1], -m_extents[2]);
|
|
|
|
else if (p == 6)
|
|
|
|
LocalPt = m_localCenter + b3Vector3(-m_extents[0], -m_extents[1], m_extents[2]);
|
|
|
|
else if (p == 7)
|
|
|
|
LocalPt = m_localCenter + b3Vector3(-m_extents[0], -m_extents[1], -m_extents[2]);
|
|
|
|
|
|
|
|
for (int i = 0; i < m_faces.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
const b3Vector3 Normal(m_faces[i].m_plane[0], m_faces[i].m_plane[1], m_faces[i].m_plane[2]);
|
|
|
|
const b3Scalar d = LocalPt.dot(Normal) + m_faces[i].m_plane[3];
|
2019-01-03 14:26:51 +01:00
|
|
|
if (d > 0.0f)
|
2017-08-01 14:30:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void b3ConvexUtility::initialize()
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
b3HashMap<b3InternalVertexPair, b3InternalEdge> edges;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
b3Scalar TotalArea = 0.0f;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
m_localCenter.setValue(0, 0, 0);
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < m_faces.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
int numVertices = m_faces[i].m_indices.size();
|
|
|
|
int NbTris = numVertices;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int j = 0; j < NbTris; j++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
int k = (j + 1) % numVertices;
|
|
|
|
b3InternalVertexPair vp(m_faces[i].m_indices[j], m_faces[i].m_indices[k]);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3InternalEdge* edptr = edges.find(vp);
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Vector3 edge = m_vertices[vp.m_v1] - m_vertices[vp.m_v0];
|
2017-08-01 14:30:58 +02:00
|
|
|
edge.normalize();
|
|
|
|
|
|
|
|
bool found = false;
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Vector3 diff, diff2;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int p = 0; p < m_uniqueEdges.size(); p++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
diff = m_uniqueEdges[p] - edge;
|
|
|
|
diff2 = m_uniqueEdges[p] + edge;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
// if ((diff.length2()==0.f) ||
|
2017-08-01 14:30:58 +02:00
|
|
|
// (diff2.length2()==0.f))
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (IsAlmostZero(diff) ||
|
|
|
|
IsAlmostZero(diff2))
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
m_uniqueEdges.push_back(edge);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (edptr)
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
//TBD: figure out why I added this assert
|
|
|
|
// b3Assert(edptr->m_face0>=0);
|
|
|
|
// b3Assert(edptr->m_face1<0);
|
2017-08-01 14:30:58 +02:00
|
|
|
edptr->m_face1 = i;
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
b3InternalEdge ed;
|
|
|
|
ed.m_face0 = i;
|
2019-01-03 14:26:51 +01:00
|
|
|
edges.insert(vp, ed);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_CONNECTED_FACES
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < m_faces.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
int numVertices = m_faces[i].m_indices.size();
|
|
|
|
m_faces[i].m_connectedFaces.resize(numVertices);
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int j = 0; j < numVertices; j++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
int k = (j + 1) % numVertices;
|
|
|
|
b3InternalVertexPair vp(m_faces[i].m_indices[j], m_faces[i].m_indices[k]);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3InternalEdge* edptr = edges.find(vp);
|
|
|
|
b3Assert(edptr);
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Assert(edptr->m_face0 >= 0);
|
|
|
|
b3Assert(edptr->m_face1 >= 0);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int connectedFace = (edptr->m_face0 == i) ? edptr->m_face1 : edptr->m_face0;
|
2017-08-01 14:30:58 +02:00
|
|
|
m_faces[i].m_connectedFaces[j] = connectedFace;
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //USE_CONNECTED_FACES
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < m_faces.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
int numVertices = m_faces[i].m_indices.size();
|
2019-01-03 14:26:51 +01:00
|
|
|
int NbTris = numVertices - 2;
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
const b3Vector3& p0 = m_vertices[m_faces[i].m_indices[0]];
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int j = 1; j <= NbTris; j++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
int k = (j + 1) % numVertices;
|
2017-08-01 14:30:58 +02:00
|
|
|
const b3Vector3& p1 = m_vertices[m_faces[i].m_indices[j]];
|
|
|
|
const b3Vector3& p2 = m_vertices[m_faces[i].m_indices[k]];
|
|
|
|
b3Scalar Area = ((p0 - p1).cross(p0 - p2)).length() * 0.5f;
|
2019-01-03 14:26:51 +01:00
|
|
|
b3Vector3 Center = (p0 + p1 + p2) / 3.0f;
|
2017-08-01 14:30:58 +02:00
|
|
|
m_localCenter += Area * Center;
|
|
|
|
TotalArea += Area;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_localCenter /= TotalArea;
|
|
|
|
|
|
|
|
#ifdef TEST_INTERNAL_OBJECTS
|
2019-01-03 14:26:51 +01:00
|
|
|
if (1)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_radius = FLT_MAX;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < m_faces.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
const b3Vector3 Normal(m_faces[i].m_plane[0], m_faces[i].m_plane[1], m_faces[i].m_plane[2]);
|
|
|
|
const b3Scalar dist = b3Fabs(m_localCenter.dot(Normal) + m_faces[i].m_plane[3]);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (dist < m_radius)
|
2017-08-01 14:30:58 +02:00
|
|
|
m_radius = dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
b3Scalar MinX = FLT_MAX;
|
|
|
|
b3Scalar MinY = FLT_MAX;
|
|
|
|
b3Scalar MinZ = FLT_MAX;
|
|
|
|
b3Scalar MaxX = -FLT_MAX;
|
|
|
|
b3Scalar MaxY = -FLT_MAX;
|
|
|
|
b3Scalar MaxZ = -FLT_MAX;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < m_vertices.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
const b3Vector3& pt = m_vertices[i];
|
2019-01-03 14:26:51 +01:00
|
|
|
if (pt.getX() < MinX) MinX = pt.getX();
|
|
|
|
if (pt.getX() > MaxX) MaxX = pt.getX();
|
|
|
|
if (pt.getY() < MinY) MinY = pt.getY();
|
|
|
|
if (pt.getY() > MaxY) MaxY = pt.getY();
|
|
|
|
if (pt.getZ() < MinZ) MinZ = pt.getZ();
|
|
|
|
if (pt.getZ() > MaxZ) MaxZ = pt.getZ();
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
mC.setValue(MaxX + MinX, MaxY + MinY, MaxZ + MinZ);
|
|
|
|
mE.setValue(MaxX - MinX, MaxY - MinY, MaxZ - MinZ);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
// const b3Scalar r = m_radius / sqrtf(2.0f);
|
2017-08-01 14:30:58 +02:00
|
|
|
const b3Scalar r = m_radius / sqrtf(3.0f);
|
|
|
|
const int LargestExtent = mE.maxAxis();
|
2019-01-03 14:26:51 +01:00
|
|
|
const b3Scalar Step = (mE[LargestExtent] * 0.5f - r) / 1024.0f;
|
2017-08-01 14:30:58 +02:00
|
|
|
m_extents[0] = m_extents[1] = m_extents[2] = r;
|
2019-01-03 14:26:51 +01:00
|
|
|
m_extents[LargestExtent] = mE[LargestExtent] * 0.5f;
|
2017-08-01 14:30:58 +02:00
|
|
|
bool FoundBox = false;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int j = 0; j < 1024; j++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
if (testContainment())
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
FoundBox = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_extents[LargestExtent] -= Step;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
if (!FoundBox)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_extents[0] = m_extents[1] = m_extents[2] = r;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Refine the box
|
2019-01-03 14:26:51 +01:00
|
|
|
const b3Scalar Step = (m_radius - r) / 1024.0f;
|
|
|
|
const int e0 = (1 << LargestExtent) & 3;
|
|
|
|
const int e1 = (1 << e0) & 3;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int j = 0; j < 1024; j++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
const b3Scalar Saved0 = m_extents[e0];
|
|
|
|
const b3Scalar Saved1 = m_extents[e1];
|
|
|
|
m_extents[e0] += Step;
|
|
|
|
m_extents[e1] += Step;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
if (!testContainment())
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_extents[e0] = Saved0;
|
|
|
|
m_extents[e1] = Saved1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|