From 9d761291d620efdf23749d0ed2d82daee054c45a Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Wed, 6 Dec 2017 21:55:50 +0100 Subject: [PATCH] Small optimizations --- CMakeLists.txt | 2 ++ .../narrowphase/SAT/SATAlgorithm.cpp | 24 +-------------- src/collision/narrowphase/SAT/SATAlgorithm.h | 3 -- .../shapes/ConvexPolyhedronShape.cpp | 22 ++++++++++++++ src/collision/shapes/ConvexPolyhedronShape.h | 5 ++++ src/constraint/ContactPoint.h | 8 ++--- src/mathematics/mathematics_functions.cpp | 29 ++++++++++--------- 7 files changed, 50 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4cfde6e..e7e2df87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -192,8 +192,10 @@ SET (REACTPHYSICS3D_SOURCES "src/memory/PoolAllocator.cpp" "src/memory/SingleFrameAllocator.h" "src/memory/SingleFrameAllocator.cpp" + "src/memory/DefaultAllocator.h" "src/containers/Stack.h" "src/containers/LinkedList.h" + "src/containers/List.h" ) # Create the library diff --git a/src/collision/narrowphase/SAT/SATAlgorithm.cpp b/src/collision/narrowphase/SAT/SATAlgorithm.cpp index 7ef99e1f..5e40f45f 100644 --- a/src/collision/narrowphase/SAT/SATAlgorithm.cpp +++ b/src/collision/narrowphase/SAT/SATAlgorithm.cpp @@ -824,7 +824,7 @@ bool SATAlgorithm::computePolyhedronVsPolyhedronFaceContactPoints(bool isMinPene const HalfEdgeStructure::Face& referenceFace = referencePolyhedron->getFace(minFaceIndex); // 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 const HalfEdgeStructure::Face& incidentFace = incidentPolyhedron->getFace(incidentFaceIndex); @@ -914,28 +914,6 @@ bool SATAlgorithm::computePolyhedronVsPolyhedronFaceContactPoints(bool isMinPene 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 decimal SATAlgorithm::computeDistanceBetweenEdges(const Vector3& edge1A, const Vector3& edge2A, const Vector3& polyhedron2Centroid, diff --git a/src/collision/narrowphase/SAT/SATAlgorithm.h b/src/collision/narrowphase/SAT/SATAlgorithm.h index af49ca09..52884f8e 100644 --- a/src/collision/narrowphase/SAT/SATAlgorithm.h +++ b/src/collision/narrowphase/SAT/SATAlgorithm.h @@ -72,9 +72,6 @@ class SATAlgorithm { const Vector3& c, const Vector3& d, 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 decimal computeDistanceBetweenEdges(const Vector3& edge1A, const Vector3& edge2A, const Vector3& polyhedron2Centroid, const Vector3& edge1Direction, const Vector3& edge2Direction, diff --git a/src/collision/shapes/ConvexPolyhedronShape.cpp b/src/collision/shapes/ConvexPolyhedronShape.cpp index ab93584d..24f80de6 100644 --- a/src/collision/shapes/ConvexPolyhedronShape.cpp +++ b/src/collision/shapes/ConvexPolyhedronShape.cpp @@ -35,3 +35,25 @@ ConvexPolyhedronShape::ConvexPolyhedronShape(CollisionShapeName name) : 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; +} diff --git a/src/collision/shapes/ConvexPolyhedronShape.h b/src/collision/shapes/ConvexPolyhedronShape.h index 1d944cb8..50c6df89 100644 --- a/src/collision/shapes/ConvexPolyhedronShape.h +++ b/src/collision/shapes/ConvexPolyhedronShape.h @@ -87,6 +87,10 @@ class ConvexPolyhedronShape : public ConvexShape { /// Return the centroid of the polyhedron 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 @@ -94,6 +98,7 @@ inline bool ConvexPolyhedronShape::isPolyhedron() const { return true; } + } #endif diff --git a/src/constraint/ContactPoint.h b/src/constraint/ContactPoint.h index 40a3d13c..d78659f8 100644 --- a/src/constraint/ContactPoint.h +++ b/src/constraint/ContactPoint.h @@ -122,10 +122,10 @@ class ContactPoint { Vector3 getNormal() const; /// 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 - Vector3 getLocalPointOnShape2() const; + const Vector3& getLocalPointOnShape2() const; /// Return the cached penetration impulse 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 -inline Vector3 ContactPoint::getLocalPointOnShape1() const { +inline const Vector3& ContactPoint::getLocalPointOnShape1() const { return mLocalPointOnShape1; } // 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; } diff --git a/src/mathematics/mathematics_functions.cpp b/src/mathematics/mathematics_functions.cpp index e7c9f371..b82c9fbd 100755 --- a/src/mathematics/mathematics_functions.cpp +++ b/src/mathematics/mathematics_functions.cpp @@ -300,24 +300,26 @@ List reactphysics3d::clipPolygonWithPlanes(const List& polygon assert(planesPoints.size() == planesNormals.size()); uint nbMaxElements = polygonVertices.size() + planesPoints.size(); - List inputVertices(allocator, nbMaxElements); - List outputVertices(allocator, nbMaxElements); + List list1(allocator, nbMaxElements); + List list2(allocator, nbMaxElements); + List dotProducts(allocator, nbMaxElements * 2); - inputVertices.addRange(polygonVertices); + const List* inputVertices = &polygonVertices; + List* outputVertices = &list2; // For each clipping plane for (uint p=0; pclear(); - uint nbInputVertices = inputVertices.size(); + uint nbInputVertices = inputVertices->size(); uint vStart = nbInputVertices - 1; // For each edge of the polygon for (uint vEnd = 0; vEnd reactphysics3d::clipPolygonWithPlanes(const List& polygon decimal t = computePlaneSegmentIntersection(v1, v2, planesNormals[p].dot(planesPoints[p]), planesNormals[p]); if (t >= decimal(0) && t <= decimal(1.0)) { - outputVertices.add(v1 + t * (v2 - v1)); + outputVertices->add(v1 + t * (v2 - v1)); } else { - outputVertices.add(v2); + outputVertices->add(v2); } } // Add the second vertex - outputVertices.add(v2); + outputVertices->add(v2); } else { // If the second vertex is behind the clipping plane @@ -351,10 +353,10 @@ List reactphysics3d::clipPolygonWithPlanes(const List& polygon decimal t = computePlaneSegmentIntersection(v1, v2, -planesNormals[p].dot(planesPoints[p]), -planesNormals[p]); if (t >= decimal(0.0) && t <= decimal(1.0)) { - outputVertices.add(v1 + t * (v2 - v1)); + outputVertices->add(v1 + t * (v2 - v1)); } else { - outputVertices.add(v1); + outputVertices->add(v1); } } } @@ -363,9 +365,10 @@ List reactphysics3d::clipPolygonWithPlanes(const List& polygon } 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