2017-08-01 14:30:58 +02:00
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
|
|
|
Copyright (c) 2003-2009 Erwin Coumans 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.
|
|
|
|
*/
|
2019-01-03 14:26:51 +01:00
|
|
|
#if defined(_WIN32) || defined(__i386__)
|
2017-08-01 14:30:58 +02:00
|
|
|
#define BT_USE_SSE_IN_API
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "BulletCollision/CollisionShapes/btPolyhedralConvexShape.h"
|
|
|
|
#include "btConvexPolyhedron.h"
|
|
|
|
#include "LinearMath/btConvexHullComputer.h"
|
|
|
|
#include <new>
|
|
|
|
#include "LinearMath/btGeometryUtil.h"
|
|
|
|
#include "LinearMath/btGrahamScan2dConvexHull.h"
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btPolyhedralConvexShape::btPolyhedralConvexShape() : btConvexInternalShape(),
|
|
|
|
m_polyhedron(0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
btPolyhedralConvexShape::~btPolyhedralConvexShape()
|
|
|
|
{
|
|
|
|
if (m_polyhedron)
|
|
|
|
{
|
|
|
|
m_polyhedron->~btConvexPolyhedron();
|
|
|
|
btAlignedFree(m_polyhedron);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-07 16:11:04 +02:00
|
|
|
void btPolyhedralConvexShape::setPolyhedralFeatures(btConvexPolyhedron& polyhedron)
|
|
|
|
{
|
|
|
|
if (m_polyhedron)
|
|
|
|
{
|
|
|
|
*m_polyhedron = polyhedron;
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
else
|
2018-09-07 16:11:04 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
void* mem = btAlignedAlloc(sizeof(btConvexPolyhedron), 16);
|
2018-09-07 16:11:04 +02:00
|
|
|
m_polyhedron = new (mem) btConvexPolyhedron(polyhedron);
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
bool btPolyhedralConvexShape::initializePolyhedralFeatures(int shiftVerticesByMargin)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
if (m_polyhedron)
|
|
|
|
{
|
|
|
|
m_polyhedron->~btConvexPolyhedron();
|
|
|
|
btAlignedFree(m_polyhedron);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
void* mem = btAlignedAlloc(sizeof(btConvexPolyhedron), 16);
|
2017-08-01 14:30:58 +02:00
|
|
|
m_polyhedron = new (mem) btConvexPolyhedron;
|
|
|
|
|
|
|
|
btAlignedObjectArray<btVector3> orgVertices;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < getNumVertices(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btVector3& newVertex = orgVertices.expand();
|
2019-01-03 14:26:51 +01:00
|
|
|
getVertex(i, newVertex);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
btConvexHullComputer conv;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
if (shiftVerticesByMargin)
|
|
|
|
{
|
|
|
|
btAlignedObjectArray<btVector3> planeEquations;
|
2019-01-03 14:26:51 +01:00
|
|
|
btGeometryUtil::getPlaneEquationsFromVertices(orgVertices, planeEquations);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btAlignedObjectArray<btVector3> shiftedPlaneEquations;
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int p = 0; p < planeEquations.size(); p++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 plane = planeEquations[p];
|
|
|
|
// btScalar margin = getMargin();
|
|
|
|
plane[3] -= getMargin();
|
|
|
|
shiftedPlaneEquations.push_back(plane);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
btAlignedObjectArray<btVector3> tmpVertices;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btGeometryUtil::getVerticesFromPlaneEquations(shiftedPlaneEquations, tmpVertices);
|
|
|
|
|
|
|
|
conv.compute(&tmpVertices[0].getX(), sizeof(btVector3), tmpVertices.size(), 0.f, 0.f);
|
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
conv.compute(&orgVertices[0].getX(), sizeof(btVector3), orgVertices.size(), 0.f, 0.f);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2018-09-07 16:11:04 +02:00
|
|
|
#ifndef BT_RECONSTRUCT_FACES
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2018-09-07 16:11:04 +02:00
|
|
|
int numVertices = conv.vertices.size();
|
|
|
|
m_polyhedron->m_vertices.resize(numVertices);
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int p = 0; p < numVertices; p++)
|
2018-09-07 16:11:04 +02:00
|
|
|
{
|
|
|
|
m_polyhedron->m_vertices[p] = conv.vertices[p];
|
|
|
|
}
|
|
|
|
|
|
|
|
int v0, v1;
|
|
|
|
for (int j = 0; j < conv.faces.size(); j++)
|
|
|
|
{
|
|
|
|
btVector3 edges[3];
|
|
|
|
int numEdges = 0;
|
|
|
|
btFace combinedFace;
|
|
|
|
const btConvexHullComputer::Edge* edge = &conv.edges[conv.faces[j]];
|
|
|
|
v0 = edge->getSourceVertex();
|
2019-01-03 14:26:51 +01:00
|
|
|
int prevVertex = v0;
|
2018-09-07 16:11:04 +02:00
|
|
|
combinedFace.m_indices.push_back(v0);
|
|
|
|
v1 = edge->getTargetVertex();
|
|
|
|
while (v1 != v0)
|
|
|
|
{
|
|
|
|
btVector3 wa = conv.vertices[prevVertex];
|
|
|
|
btVector3 wb = conv.vertices[v1];
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 newEdge = wb - wa;
|
2018-09-07 16:11:04 +02:00
|
|
|
newEdge.normalize();
|
2019-01-03 14:26:51 +01:00
|
|
|
if (numEdges < 2)
|
2018-09-07 16:11:04 +02:00
|
|
|
edges[numEdges++] = newEdge;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2018-09-07 16:11:04 +02:00
|
|
|
//face->addIndex(v1);
|
|
|
|
combinedFace.m_indices.push_back(v1);
|
|
|
|
edge = edge->getNextEdgeOfFace();
|
|
|
|
prevVertex = v1;
|
|
|
|
int v01 = edge->getSourceVertex();
|
|
|
|
v1 = edge->getTargetVertex();
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2018-09-07 16:11:04 +02:00
|
|
|
btAssert(combinedFace.m_indices.size() > 2);
|
|
|
|
|
|
|
|
btVector3 faceNormal = edges[0].cross(edges[1]);
|
|
|
|
faceNormal.normalize();
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar planeEq = 1e30f;
|
2018-09-07 16:11:04 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int v = 0; v < combinedFace.m_indices.size(); v++)
|
2018-09-07 16:11:04 +02:00
|
|
|
{
|
|
|
|
btScalar eq = m_polyhedron->m_vertices[combinedFace.m_indices[v]].dot(faceNormal);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (planeEq > eq)
|
2018-09-07 16:11:04 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
planeEq = eq;
|
2018-09-07 16:11:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
combinedFace.m_plane[0] = faceNormal.getX();
|
|
|
|
combinedFace.m_plane[1] = faceNormal.getY();
|
|
|
|
combinedFace.m_plane[2] = faceNormal.getZ();
|
|
|
|
combinedFace.m_plane[3] = -planeEq;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2018-09-07 16:11:04 +02:00
|
|
|
m_polyhedron->m_faces.push_back(combinedFace);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
#else //BT_RECONSTRUCT_FACES
|
2018-09-07 16:11:04 +02:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
btAlignedObjectArray<btVector3> faceNormals;
|
|
|
|
int numFaces = conv.faces.size();
|
|
|
|
faceNormals.resize(numFaces);
|
|
|
|
btConvexHullComputer* convexUtil = &conv;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btAlignedObjectArray<btFace> tmpFaces;
|
2017-08-01 14:30:58 +02:00
|
|
|
tmpFaces.resize(numFaces);
|
|
|
|
|
|
|
|
int numVertices = convexUtil->vertices.size();
|
|
|
|
m_polyhedron->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_polyhedron->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 btConvexHullComputer::Edge* firstEdge = &convexUtil->edges[face];
|
|
|
|
const btConvexHullComputer::Edge* edge = firstEdge;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
btVector3 edges[3];
|
|
|
|
int numEdges = 0;
|
|
|
|
//compute face normals
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int src = edge->getSourceVertex();
|
|
|
|
tmpFaces[i].m_indices.push_back(src);
|
|
|
|
int targ = edge->getTargetVertex();
|
|
|
|
btVector3 wa = convexUtil->vertices[src];
|
|
|
|
|
|
|
|
btVector3 wb = convexUtil->vertices[targ];
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 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
|
|
|
|
|
|
|
btScalar 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
|
|
|
btAssert(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
|
|
|
{
|
|
|
|
btScalar eq = m_polyhedron->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
|
|
|
btScalar faceWeldThreshold = 0.999f;
|
2017-08-01 14:30:58 +02:00
|
|
|
btAlignedObjectArray<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())
|
|
|
|
{
|
|
|
|
btAlignedObjectArray<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);
|
|
|
|
btFace& faceA = tmpFaces[refFace];
|
|
|
|
todoFaces.pop_back();
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 faceNormalA(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];
|
|
|
|
btFace& faceB = tmpFaces[i];
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 faceNormalB(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
|
|
|
|
|
|
|
|
btAlignedObjectArray<GrahamVector3> orgpoints;
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 averageFaceNormal(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
|
|
|
|
|
|
|
btFace& face = tmpFaces[coplanarFaceGroup[i]];
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 faceNormal(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];
|
|
|
|
btVector3 pt = m_polyhedron->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(GrahamVector3(pt, orgIndex));
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
btFace 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];
|
|
|
|
|
|
|
|
btAlignedObjectArray<GrahamVector3> hull;
|
|
|
|
|
|
|
|
averageFaceNormal.normalize();
|
2019-01-03 14:26:51 +01:00
|
|
|
GrahamScanConvexHull2D(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
|
|
|
btFace& 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_polyhedron->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
|
|
|
{
|
|
|
|
btFace face = tmpFaces[coplanarFaceGroup[i]];
|
|
|
|
m_polyhedron->m_faces.push_back(face);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2018-09-07 16:11:04 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //BT_RECONSTRUCT_FACES
|
2018-09-07 16:11:04 +02:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
m_polyhedron->initialize();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef MIN
|
2019-01-03 14:26:51 +01:00
|
|
|
#define MIN(_a, _b) ((_a) < (_b) ? (_a) : (_b))
|
2017-08-01 14:30:58 +02:00
|
|
|
#endif
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 btPolyhedralConvexShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0) const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 supVec(0, 0, 0);
|
2017-08-01 14:30:58 +02:00
|
|
|
#ifndef __SPU__
|
|
|
|
int i;
|
|
|
|
btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
|
|
|
|
|
|
|
|
btVector3 vec = vec0;
|
|
|
|
btScalar lenSqr = vec.length2();
|
|
|
|
if (lenSqr < btScalar(0.0001))
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
vec.setValue(1, 0, 0);
|
|
|
|
}
|
|
|
|
else
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar rlen = btScalar(1.) / btSqrt(lenSqr);
|
2017-08-01 14:30:58 +02:00
|
|
|
vec *= rlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
btVector3 vtx;
|
|
|
|
btScalar newDot;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int k = 0; k < getNumVertices(); k += 128)
|
|
|
|
{
|
|
|
|
btVector3 temp[128];
|
|
|
|
int inner_count = MIN(getNumVertices() - k, 128);
|
|
|
|
for (i = 0; i < inner_count; i++)
|
|
|
|
getVertex(i, temp[i]);
|
|
|
|
i = (int)vec.maxDot(temp, inner_count, newDot);
|
2017-08-01 14:30:58 +02:00
|
|
|
if (newDot > maxDot)
|
|
|
|
{
|
|
|
|
maxDot = newDot;
|
|
|
|
supVec = temp[i];
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //__SPU__
|
2017-08-01 14:30:58 +02:00
|
|
|
return supVec;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btPolyhedralConvexShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
#ifndef __SPU__
|
|
|
|
int i;
|
|
|
|
|
|
|
|
btVector3 vtx;
|
|
|
|
btScalar newDot;
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (i = 0; i < numVectors; i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
supportVerticesOut[i][3] = btScalar(-BT_LARGE_FLOAT);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int j = 0; j < numVectors; j++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
const btVector3& vec = vectors[j];
|
|
|
|
|
|
|
|
for (int k = 0; k < getNumVertices(); k += 128)
|
|
|
|
{
|
|
|
|
btVector3 temp[128];
|
|
|
|
int inner_count = MIN(getNumVertices() - k, 128);
|
|
|
|
for (i = 0; i < inner_count; i++)
|
|
|
|
getVertex(i, temp[i]);
|
|
|
|
i = (int)vec.maxDot(temp, inner_count, newDot);
|
|
|
|
if (newDot > supportVerticesOut[j][3])
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
supportVerticesOut[j] = temp[i];
|
|
|
|
supportVerticesOut[j][3] = newDot;
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //__SPU__
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btPolyhedralConvexShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
#ifndef __SPU__
|
|
|
|
//not yet, return box inertia
|
|
|
|
|
|
|
|
btScalar margin = getMargin();
|
|
|
|
|
|
|
|
btTransform ident;
|
|
|
|
ident.setIdentity();
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 aabbMin, aabbMax;
|
|
|
|
getAabb(ident, aabbMin, aabbMax);
|
|
|
|
btVector3 halfExtents = (aabbMax - aabbMin) * btScalar(0.5);
|
|
|
|
|
|
|
|
btScalar lx = btScalar(2.) * (halfExtents.x() + margin);
|
|
|
|
btScalar ly = btScalar(2.) * (halfExtents.y() + margin);
|
|
|
|
btScalar lz = btScalar(2.) * (halfExtents.z() + margin);
|
|
|
|
const btScalar x2 = lx * lx;
|
|
|
|
const btScalar y2 = ly * ly;
|
|
|
|
const btScalar z2 = lz * lz;
|
2017-08-01 14:30:58 +02:00
|
|
|
const btScalar scaledmass = mass * btScalar(0.08333333);
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
inertia = scaledmass * (btVector3(y2 + z2, x2 + z2, x2 + y2));
|
|
|
|
#endif //__SPU__
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btPolyhedralConvexAabbCachingShape::setLocalScaling(const btVector3& scaling)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btConvexInternalShape::setLocalScaling(scaling);
|
|
|
|
recalcLocalAabb();
|
|
|
|
}
|
|
|
|
|
|
|
|
btPolyhedralConvexAabbCachingShape::btPolyhedralConvexAabbCachingShape()
|
2019-01-03 14:26:51 +01:00
|
|
|
: btPolyhedralConvexShape(),
|
|
|
|
m_localAabbMin(1, 1, 1),
|
|
|
|
m_localAabbMax(-1, -1, -1),
|
|
|
|
m_isLocalAabbValid(false)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btPolyhedralConvexAabbCachingShape::getAabb(const btTransform& trans, btVector3& aabbMin, btVector3& aabbMax) const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
getNonvirtualAabb(trans, aabbMin, aabbMax, getMargin());
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btPolyhedralConvexAabbCachingShape::recalcLocalAabb()
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_isLocalAabbValid = true;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
#if 1
|
2017-08-01 14:30:58 +02:00
|
|
|
static const btVector3 _directions[] =
|
2019-01-03 14:26:51 +01:00
|
|
|
{
|
|
|
|
btVector3(1., 0., 0.),
|
|
|
|
btVector3(0., 1., 0.),
|
|
|
|
btVector3(0., 0., 1.),
|
|
|
|
btVector3(-1., 0., 0.),
|
|
|
|
btVector3(0., -1., 0.),
|
|
|
|
btVector3(0., 0., -1.)};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
btVector3 _supporting[] =
|
2019-01-03 14:26:51 +01:00
|
|
|
{
|
|
|
|
btVector3(0., 0., 0.),
|
|
|
|
btVector3(0., 0., 0.),
|
|
|
|
btVector3(0., 0., 0.),
|
|
|
|
btVector3(0., 0., 0.),
|
|
|
|
btVector3(0., 0., 0.),
|
|
|
|
btVector3(0., 0., 0.)};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
batchedUnitVectorGetSupportingVertexWithoutMargin(_directions, _supporting, 6);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_localAabbMax[i] = _supporting[i][i] + m_collisionMargin;
|
|
|
|
m_localAabbMin[i] = _supporting[i + 3][i] - m_collisionMargin;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 vec(btScalar(0.), btScalar(0.), btScalar(0.));
|
2017-08-01 14:30:58 +02:00
|
|
|
vec[i] = btScalar(1.);
|
|
|
|
btVector3 tmp = localGetSupportingVertex(vec);
|
|
|
|
m_localAabbMax[i] = tmp[i];
|
|
|
|
vec[i] = btScalar(-1.);
|
|
|
|
tmp = localGetSupportingVertex(vec);
|
|
|
|
m_localAabbMin[i] = tmp[i];
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|