From 6e67b83ca4f0b98e6d89e586457dec0d835a2791 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Sat, 1 Dec 2018 13:17:32 +0100 Subject: [PATCH] Modifs in NarrowPhaseBatch info classes --- .../CapsuleVsCapsuleNarrowPhaseInfoBatch.cpp | 18 +++++++++++++++--- .../CapsuleVsCapsuleNarrowPhaseInfoBatch.h | 2 +- .../narrowphase/NarrowPhaseAlgorithm.h | 1 - .../narrowphase/NarrowPhaseInfoBatch.cpp | 1 - .../narrowphase/NarrowPhaseInfoBatch.h | 7 ++----- .../SphereVsCapsuleNarrowPhaseInfoBatch.cpp | 18 +++++++++++++++--- .../SphereVsCapsuleNarrowPhaseInfoBatch.h | 2 +- .../SphereVsSphereNarrowPhaseInfoBatch.cpp | 18 +++++++++++++++--- .../SphereVsSphereNarrowPhaseInfoBatch.h | 6 +++--- 9 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/collision/narrowphase/CapsuleVsCapsuleNarrowPhaseInfoBatch.cpp b/src/collision/narrowphase/CapsuleVsCapsuleNarrowPhaseInfoBatch.cpp index 1034e13d..d90e9e8f 100644 --- a/src/collision/narrowphase/CapsuleVsCapsuleNarrowPhaseInfoBatch.cpp +++ b/src/collision/narrowphase/CapsuleVsCapsuleNarrowPhaseInfoBatch.cpp @@ -64,7 +64,12 @@ void CapsuleVsCapsuleNarrowPhaseInfoBatch::addNarrowPhaseInfo(OverlappingPair* p // Initialize the containers using cached capacity void CapsuleVsCapsuleNarrowPhaseInfoBatch::reserveMemory() { - NarrowPhaseInfoBatch::reserveMemory(); + overlappingPairs.reserve(mCachedCapacity); + shape1ToWorldTransforms.reserve(mCachedCapacity); + shape2ToWorldTransforms.reserve(mCachedCapacity); + lastFrameCollisionInfos.reserve(mCachedCapacity); + isColliding.reserve(mCachedCapacity); + contactPoints.reserve(mCachedCapacity); capsule1Radiuses.reserve(mCachedCapacity); capsule2Radiuses.reserve(mCachedCapacity); @@ -75,13 +80,20 @@ void CapsuleVsCapsuleNarrowPhaseInfoBatch::reserveMemory() { // Clear all the objects in the batch void CapsuleVsCapsuleNarrowPhaseInfoBatch::clear() { - NarrowPhaseInfoBatch::clear(); - // Note that we clear the following containers and we release their allocated memory. Therefore, // if the memory allocator is a single frame allocator, the memory is deallocated and will be // allocated in the next frame at a possibly different location in memory (remember that the // location of the allocated memory of a single frame allocator might change between two frames) + mCachedCapacity = overlappingPairs.size(); + + overlappingPairs.clear(true); + shape1ToWorldTransforms.clear(true); + shape2ToWorldTransforms.clear(true); + lastFrameCollisionInfos.clear(true); + isColliding.clear(true); + contactPoints.clear(true); + capsule1Radiuses.clear(true); capsule2Radiuses.clear(true); capsule1Heights.clear(true); diff --git a/src/collision/narrowphase/CapsuleVsCapsuleNarrowPhaseInfoBatch.h b/src/collision/narrowphase/CapsuleVsCapsuleNarrowPhaseInfoBatch.h index 914e02b9..1cdc2c70 100644 --- a/src/collision/narrowphase/CapsuleVsCapsuleNarrowPhaseInfoBatch.h +++ b/src/collision/narrowphase/CapsuleVsCapsuleNarrowPhaseInfoBatch.h @@ -66,7 +66,7 @@ struct CapsuleVsCapsuleNarrowPhaseInfoBatch : public NarrowPhaseInfoBatch { const Transform& shape2Transform); // Initialize the containers using cached capacity - void reserveMemory(); + virtual void reserveMemory(); /// Clear all the objects in the batch virtual void clear(); diff --git a/src/collision/narrowphase/NarrowPhaseAlgorithm.h b/src/collision/narrowphase/NarrowPhaseAlgorithm.h index 3d61edb9..3b7f68a2 100644 --- a/src/collision/narrowphase/NarrowPhaseAlgorithm.h +++ b/src/collision/narrowphase/NarrowPhaseAlgorithm.h @@ -59,7 +59,6 @@ class NarrowPhaseCallback { }; -// TODO DOD : Try to delete this class // Class NarrowPhaseAlgorithm /** * This abstract class is the base class for a narrow-phase collision diff --git a/src/collision/narrowphase/NarrowPhaseInfoBatch.cpp b/src/collision/narrowphase/NarrowPhaseInfoBatch.cpp index bb7c0d7e..61b2fb5b 100644 --- a/src/collision/narrowphase/NarrowPhaseInfoBatch.cpp +++ b/src/collision/narrowphase/NarrowPhaseInfoBatch.cpp @@ -125,7 +125,6 @@ void NarrowPhaseInfoBatch::clear() { // Release the memory of the TriangleShape (this memory was allocated in the // MiddlePhaseTriangleCallback::testTriangle() method) - // TODO DOD : Try to move this code if (collisionShapes1.size() > 0 && collisionShapes1[i]->getName() == CollisionShapeName::TRIANGLE) { collisionShapes1[i]->~CollisionShape(); collisionShapeAllocators[i]->release(collisionShapes1[i], sizeof(TriangleShape)); diff --git a/src/collision/narrowphase/NarrowPhaseInfoBatch.h b/src/collision/narrowphase/NarrowPhaseInfoBatch.h index abf5d7b0..f42c41e5 100644 --- a/src/collision/narrowphase/NarrowPhaseInfoBatch.h +++ b/src/collision/narrowphase/NarrowPhaseInfoBatch.h @@ -57,8 +57,6 @@ struct NarrowPhaseInfoBatch { public: - // TODO DOD : Try to remove most of the following lists - /// List of Broadphase overlapping pairs List overlappingPairs; @@ -96,20 +94,19 @@ struct NarrowPhaseInfoBatch { uint getNbObjects() const; /// Add shapes to be tested during narrow-phase collision detection into the batch - // TODO DOD : Remove this method (only use the one in specialized classed) void addNarrowPhaseInfo(OverlappingPair* pair, CollisionShape* shape1, CollisionShape* shape2, const Transform& shape1Transform, const Transform& shape2Transform, MemoryAllocator& shapeAllocator); /// Add a new contact point - void addContactPoint(uint index, const Vector3& contactNormal, decimal penDepth, + virtual void addContactPoint(uint index, const Vector3& contactNormal, decimal penDepth, const Vector3& localPt1, const Vector3& localPt2); /// Reset the remaining contact points void resetContactPoints(uint index); // Initialize the containers using cached capacity - void reserveMemory(); + virtual void reserveMemory(); /// Clear all the objects in the batch virtual void clear(); diff --git a/src/collision/narrowphase/SphereVsCapsuleNarrowPhaseInfoBatch.cpp b/src/collision/narrowphase/SphereVsCapsuleNarrowPhaseInfoBatch.cpp index ec8ac168..de617979 100644 --- a/src/collision/narrowphase/SphereVsCapsuleNarrowPhaseInfoBatch.cpp +++ b/src/collision/narrowphase/SphereVsCapsuleNarrowPhaseInfoBatch.cpp @@ -67,7 +67,12 @@ void SphereVsCapsuleNarrowPhaseInfoBatch::addNarrowPhaseInfo(OverlappingPair* pa // Initialize the containers using cached capacity void SphereVsCapsuleNarrowPhaseInfoBatch::reserveMemory() { - NarrowPhaseInfoBatch::reserveMemory(); + overlappingPairs.reserve(mCachedCapacity); + shape1ToWorldTransforms.reserve(mCachedCapacity); + shape2ToWorldTransforms.reserve(mCachedCapacity); + lastFrameCollisionInfos.reserve(mCachedCapacity); + isColliding.reserve(mCachedCapacity); + contactPoints.reserve(mCachedCapacity); isSpheresShape1.reserve(mCachedCapacity); sphereRadiuses.reserve(mCachedCapacity); @@ -78,13 +83,20 @@ void SphereVsCapsuleNarrowPhaseInfoBatch::reserveMemory() { // Clear all the objects in the batch void SphereVsCapsuleNarrowPhaseInfoBatch::clear() { - NarrowPhaseInfoBatch::clear(); - // Note that we clear the following containers and we release their allocated memory. Therefore, // if the memory allocator is a single frame allocator, the memory is deallocated and will be // allocated in the next frame at a possibly different location in memory (remember that the // location of the allocated memory of a single frame allocator might change between two frames) + mCachedCapacity = overlappingPairs.size(); + + overlappingPairs.clear(true); + shape1ToWorldTransforms.clear(true); + shape2ToWorldTransforms.clear(true); + lastFrameCollisionInfos.clear(true); + isColliding.clear(true); + contactPoints.clear(true); + isSpheresShape1.clear(true); sphereRadiuses.clear(true); capsuleRadiuses.clear(true); diff --git a/src/collision/narrowphase/SphereVsCapsuleNarrowPhaseInfoBatch.h b/src/collision/narrowphase/SphereVsCapsuleNarrowPhaseInfoBatch.h index beaa9bf1..54923b41 100644 --- a/src/collision/narrowphase/SphereVsCapsuleNarrowPhaseInfoBatch.h +++ b/src/collision/narrowphase/SphereVsCapsuleNarrowPhaseInfoBatch.h @@ -66,7 +66,7 @@ struct SphereVsCapsuleNarrowPhaseInfoBatch : public NarrowPhaseInfoBatch { const Transform& shape2Transform); // Initialize the containers using cached capacity - void reserveMemory(); + virtual void reserveMemory(); /// Clear all the objects in the batch virtual void clear(); diff --git a/src/collision/narrowphase/SphereVsSphereNarrowPhaseInfoBatch.cpp b/src/collision/narrowphase/SphereVsSphereNarrowPhaseInfoBatch.cpp index d0891389..e9ab2b67 100644 --- a/src/collision/narrowphase/SphereVsSphereNarrowPhaseInfoBatch.cpp +++ b/src/collision/narrowphase/SphereVsSphereNarrowPhaseInfoBatch.cpp @@ -61,7 +61,12 @@ void SphereVsSphereNarrowPhaseInfoBatch::addNarrowPhaseInfo(OverlappingPair* pai // Initialize the containers using cached capacity void SphereVsSphereNarrowPhaseInfoBatch::reserveMemory() { - NarrowPhaseInfoBatch::reserveMemory(); + overlappingPairs.reserve(mCachedCapacity); + shape1ToWorldTransforms.reserve(mCachedCapacity); + shape2ToWorldTransforms.reserve(mCachedCapacity); + lastFrameCollisionInfos.reserve(mCachedCapacity); + isColliding.reserve(mCachedCapacity); + contactPoints.reserve(mCachedCapacity); sphere1Radiuses.reserve(mCachedCapacity); sphere2Radiuses.reserve(mCachedCapacity); @@ -70,13 +75,20 @@ void SphereVsSphereNarrowPhaseInfoBatch::reserveMemory() { // Clear all the objects in the batch void SphereVsSphereNarrowPhaseInfoBatch::clear() { - NarrowPhaseInfoBatch::clear(); - // Note that we clear the following containers and we release their allocated memory. Therefore, // if the memory allocator is a single frame allocator, the memory is deallocated and will be // allocated in the next frame at a possibly different location in memory (remember that the // location of the allocated memory of a single frame allocator might change between two frames) + mCachedCapacity = overlappingPairs.size(); + + overlappingPairs.clear(true); + shape1ToWorldTransforms.clear(true); + shape2ToWorldTransforms.clear(true); + lastFrameCollisionInfos.clear(true); + isColliding.clear(true); + contactPoints.clear(true); + sphere1Radiuses.clear(true); sphere2Radiuses.clear(true); } diff --git a/src/collision/narrowphase/SphereVsSphereNarrowPhaseInfoBatch.h b/src/collision/narrowphase/SphereVsSphereNarrowPhaseInfoBatch.h index 71d5ec5e..953cd01e 100644 --- a/src/collision/narrowphase/SphereVsSphereNarrowPhaseInfoBatch.h +++ b/src/collision/narrowphase/SphereVsSphereNarrowPhaseInfoBatch.h @@ -52,7 +52,7 @@ struct SphereVsSphereNarrowPhaseInfoBatch : public NarrowPhaseInfoBatch { SphereVsSphereNarrowPhaseInfoBatch(MemoryAllocator& allocator); /// Destructor - virtual ~SphereVsSphereNarrowPhaseInfoBatch() = default; + virtual ~SphereVsSphereNarrowPhaseInfoBatch() override = default; /// Add shapes to be tested during narrow-phase collision detection into the batch virtual void addNarrowPhaseInfo(OverlappingPair* pair, CollisionShape* shape1, @@ -60,10 +60,10 @@ struct SphereVsSphereNarrowPhaseInfoBatch : public NarrowPhaseInfoBatch { const Transform& shape2Transform); // Initialize the containers using cached capacity - void reserveMemory(); + virtual void reserveMemory() override; /// Clear all the objects in the batch - virtual void clear(); + virtual void clear() override; }; }