Small optimizations

This commit is contained in:
Daniel Chappuis 2017-12-06 21:55:50 +01:00
parent 4f76553c59
commit 9d761291d6
7 changed files with 50 additions and 43 deletions

View File

@ -192,8 +192,10 @@ SET (REACTPHYSICS3D_SOURCES
"src/memory/PoolAllocator.cpp" "src/memory/PoolAllocator.cpp"
"src/memory/SingleFrameAllocator.h" "src/memory/SingleFrameAllocator.h"
"src/memory/SingleFrameAllocator.cpp" "src/memory/SingleFrameAllocator.cpp"
"src/memory/DefaultAllocator.h"
"src/containers/Stack.h" "src/containers/Stack.h"
"src/containers/LinkedList.h" "src/containers/LinkedList.h"
"src/containers/List.h"
) )
# Create the library # Create the library

View File

@ -824,7 +824,7 @@ bool SATAlgorithm::computePolyhedronVsPolyhedronFaceContactPoints(bool isMinPene
const HalfEdgeStructure::Face& referenceFace = referencePolyhedron->getFace(minFaceIndex); const HalfEdgeStructure::Face& referenceFace = referencePolyhedron->getFace(minFaceIndex);
// Find the incident face on the other polyhedron (most anti-parallel face) // Find the incident face on the other polyhedron (most anti-parallel face)
uint incidentFaceIndex = findMostAntiParallelFaceOnPolyhedron(incidentPolyhedron, axisIncidentSpace); uint incidentFaceIndex = incidentPolyhedron->findMostAntiParallelFace(axisIncidentSpace);
// Get the incident face // Get the incident face
const HalfEdgeStructure::Face& incidentFace = incidentPolyhedron->getFace(incidentFaceIndex); const HalfEdgeStructure::Face& incidentFace = incidentPolyhedron->getFace(incidentFaceIndex);
@ -914,28 +914,6 @@ bool SATAlgorithm::computePolyhedronVsPolyhedronFaceContactPoints(bool isMinPene
return contactPointsFound; return contactPointsFound;
} }
// Find and return the index of the polyhedron face with the most anti-parallel face normal given a direction vector
// This is used to find the incident face on a polyhedron of a given reference face of another polyhedron
uint SATAlgorithm::findMostAntiParallelFaceOnPolyhedron(const ConvexPolyhedronShape* polyhedron, const Vector3& direction) const {
PROFILE("SATAlgorithm::findMostAntiParallelFaceOnPolyhedron", mProfiler);
decimal minDotProduct = DECIMAL_LARGEST;
uint mostAntiParallelFace = 0;
// For each face of the polyhedron
for (uint i=0; i < polyhedron->getNbFaces(); i++) {
// Get the face normal
decimal dotProduct = polyhedron->getFaceNormal(i).dot(direction);
if (dotProduct < minDotProduct) {
minDotProduct = dotProduct;
mostAntiParallelFace = i;
}
}
return mostAntiParallelFace;
}
// Compute and return the distance between the two edges in the direction of the candidate separating axis // Compute and return the distance between the two edges in the direction of the candidate separating axis
decimal SATAlgorithm::computeDistanceBetweenEdges(const Vector3& edge1A, const Vector3& edge2A, const Vector3& polyhedron2Centroid, decimal SATAlgorithm::computeDistanceBetweenEdges(const Vector3& edge1A, const Vector3& edge2A, const Vector3& polyhedron2Centroid,

View File

@ -72,9 +72,6 @@ class SATAlgorithm {
const Vector3& c, const Vector3& d, const Vector3& c, const Vector3& d,
const Vector3& bCrossA, const Vector3& dCrossC) const; const Vector3& bCrossA, const Vector3& dCrossC) const;
// Find and return the index of the polyhedron face with the most anti-parallel face normal given a direction vector
uint findMostAntiParallelFaceOnPolyhedron(const ConvexPolyhedronShape* polyhedron, const Vector3& direction) const;
/// Compute and return the distance between the two edges in the direction of the candidate separating axis /// Compute and return the distance between the two edges in the direction of the candidate separating axis
decimal computeDistanceBetweenEdges(const Vector3& edge1A, const Vector3& edge2A, const Vector3& polyhedron2Centroid, decimal computeDistanceBetweenEdges(const Vector3& edge1A, const Vector3& edge2A, const Vector3& polyhedron2Centroid,
const Vector3& edge1Direction, const Vector3& edge2Direction, const Vector3& edge1Direction, const Vector3& edge2Direction,

View File

@ -35,3 +35,25 @@ ConvexPolyhedronShape::ConvexPolyhedronShape(CollisionShapeName name)
: ConvexShape(name, CollisionShapeType::CONVEX_POLYHEDRON) { : ConvexShape(name, CollisionShapeType::CONVEX_POLYHEDRON) {
} }
// Find and return the index of the polyhedron face with the most anti-parallel face
// normal given a direction vector. This is used to find the incident face on
// a polyhedron of a given reference face of another polyhedron
uint ConvexPolyhedronShape::findMostAntiParallelFace(const Vector3& direction) const {
decimal minDotProduct = DECIMAL_LARGEST;
uint mostAntiParallelFace = 0;
// For each face of the polyhedron
for (uint i=0; i < getNbFaces(); i++) {
// Get the face normal
decimal dotProduct = getFaceNormal(i).dot(direction);
if (dotProduct < minDotProduct) {
minDotProduct = dotProduct;
mostAntiParallelFace = i;
}
}
return mostAntiParallelFace;
}

View File

@ -87,6 +87,10 @@ class ConvexPolyhedronShape : public ConvexShape {
/// Return the centroid of the polyhedron /// Return the centroid of the polyhedron
virtual Vector3 getCentroid() const=0; virtual Vector3 getCentroid() const=0;
/// Find and return the index of the polyhedron face with the most anti-parallel face
/// normal given a direction vector
uint findMostAntiParallelFace(const Vector3& direction) const;
}; };
// Return true if the collision shape is a polyhedron // Return true if the collision shape is a polyhedron
@ -94,6 +98,7 @@ inline bool ConvexPolyhedronShape::isPolyhedron() const {
return true; return true;
} }
} }
#endif #endif

View File

@ -122,10 +122,10 @@ class ContactPoint {
Vector3 getNormal() const; Vector3 getNormal() const;
/// Return the contact point on the first proxy shape in the local-space of the proxy shape /// Return the contact point on the first proxy shape in the local-space of the proxy shape
Vector3 getLocalPointOnShape1() const; const Vector3& getLocalPointOnShape1() const;
/// Return the contact point on the second proxy shape in the local-space of the proxy shape /// Return the contact point on the second proxy shape in the local-space of the proxy shape
Vector3 getLocalPointOnShape2() const; const Vector3& getLocalPointOnShape2() const;
/// Return the cached penetration impulse /// Return the cached penetration impulse
decimal getPenetrationImpulse() const; decimal getPenetrationImpulse() const;
@ -157,12 +157,12 @@ inline Vector3 ContactPoint::getNormal() const {
} }
// Return the contact point on the first proxy shape in the local-space of the proxy shape // Return the contact point on the first proxy shape in the local-space of the proxy shape
inline Vector3 ContactPoint::getLocalPointOnShape1() const { inline const Vector3& ContactPoint::getLocalPointOnShape1() const {
return mLocalPointOnShape1; return mLocalPointOnShape1;
} }
// Return the contact point on the second proxy shape in the local-space of the proxy shape // Return the contact point on the second proxy shape in the local-space of the proxy shape
inline Vector3 ContactPoint::getLocalPointOnShape2() const { inline const Vector3& ContactPoint::getLocalPointOnShape2() const {
return mLocalPointOnShape2; return mLocalPointOnShape2;
} }

View File

@ -300,24 +300,26 @@ List<Vector3> reactphysics3d::clipPolygonWithPlanes(const List<Vector3>& polygon
assert(planesPoints.size() == planesNormals.size()); assert(planesPoints.size() == planesNormals.size());
uint nbMaxElements = polygonVertices.size() + planesPoints.size(); uint nbMaxElements = polygonVertices.size() + planesPoints.size();
List<Vector3> inputVertices(allocator, nbMaxElements); List<Vector3> list1(allocator, nbMaxElements);
List<Vector3> outputVertices(allocator, nbMaxElements); List<Vector3> list2(allocator, nbMaxElements);
List<decimal> dotProducts(allocator, nbMaxElements * 2);
inputVertices.addRange(polygonVertices); const List<Vector3>* inputVertices = &polygonVertices;
List<Vector3>* outputVertices = &list2;
// For each clipping plane // For each clipping plane
for (uint p=0; p<planesPoints.size(); p++) { for (uint p=0; p<planesPoints.size(); p++) {
outputVertices.clear(); outputVertices->clear();
uint nbInputVertices = inputVertices.size(); uint nbInputVertices = inputVertices->size();
uint vStart = nbInputVertices - 1; uint vStart = nbInputVertices - 1;
// For each edge of the polygon // For each edge of the polygon
for (uint vEnd = 0; vEnd<nbInputVertices; vEnd++) { for (uint vEnd = 0; vEnd<nbInputVertices; vEnd++) {
Vector3& v1 = inputVertices[vStart]; const Vector3& v1 = (*inputVertices)[vStart];
Vector3& v2 = inputVertices[vEnd]; const Vector3& v2 = (*inputVertices)[vEnd];
decimal v1DotN = (v1 - planesPoints[p]).dot(planesNormals[p]); decimal v1DotN = (v1 - planesPoints[p]).dot(planesNormals[p]);
decimal v2DotN = (v2 - planesPoints[p]).dot(planesNormals[p]); decimal v2DotN = (v2 - planesPoints[p]).dot(planesNormals[p]);
@ -332,15 +334,15 @@ List<Vector3> reactphysics3d::clipPolygonWithPlanes(const List<Vector3>& polygon
decimal t = computePlaneSegmentIntersection(v1, v2, planesNormals[p].dot(planesPoints[p]), planesNormals[p]); decimal t = computePlaneSegmentIntersection(v1, v2, planesNormals[p].dot(planesPoints[p]), planesNormals[p]);
if (t >= decimal(0) && t <= decimal(1.0)) { if (t >= decimal(0) && t <= decimal(1.0)) {
outputVertices.add(v1 + t * (v2 - v1)); outputVertices->add(v1 + t * (v2 - v1));
} }
else { else {
outputVertices.add(v2); outputVertices->add(v2);
} }
} }
// Add the second vertex // Add the second vertex
outputVertices.add(v2); outputVertices->add(v2);
} }
else { // If the second vertex is behind the clipping plane else { // If the second vertex is behind the clipping plane
@ -351,10 +353,10 @@ List<Vector3> reactphysics3d::clipPolygonWithPlanes(const List<Vector3>& polygon
decimal t = computePlaneSegmentIntersection(v1, v2, -planesNormals[p].dot(planesPoints[p]), -planesNormals[p]); decimal t = computePlaneSegmentIntersection(v1, v2, -planesNormals[p].dot(planesPoints[p]), -planesNormals[p]);
if (t >= decimal(0.0) && t <= decimal(1.0)) { if (t >= decimal(0.0) && t <= decimal(1.0)) {
outputVertices.add(v1 + t * (v2 - v1)); outputVertices->add(v1 + t * (v2 - v1));
} }
else { else {
outputVertices.add(v1); outputVertices->add(v1);
} }
} }
} }
@ -363,9 +365,10 @@ List<Vector3> reactphysics3d::clipPolygonWithPlanes(const List<Vector3>& polygon
} }
inputVertices = outputVertices; inputVertices = outputVertices;
outputVertices = p % 2 == 0 ? &list1 : &list2;
} }
return outputVertices; return *outputVertices;
} }
// Project a point onto a plane that is given by a point and its unit length normal // Project a point onto a plane that is given by a point and its unit length normal