From 25fb7aacad3e9e49674ae8686c27aa20a48375fb Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 26 Jul 2016 07:53:11 -0700 Subject: [PATCH] ShapeManager hands out const shape pointers --- interface/src/avatar/AvatarManager.cpp | 2 +- interface/src/avatar/AvatarMotionState.cpp | 4 ++-- interface/src/avatar/AvatarMotionState.h | 4 ++-- libraries/physics/src/PhysicalEntitySimulation.cpp | 2 +- libraries/physics/src/PhysicsEngine.cpp | 6 +++--- libraries/physics/src/ShapeFactory.cpp | 14 +++++++++----- libraries/physics/src/ShapeFactory.h | 4 ++-- libraries/physics/src/ShapeManager.cpp | 4 ++-- libraries/physics/src/ShapeManager.h | 4 ++-- 9 files changed, 24 insertions(+), 20 deletions(-) diff --git a/interface/src/avatar/AvatarManager.cpp b/interface/src/avatar/AvatarManager.cpp index bd76d2bd81..441130bd83 100644 --- a/interface/src/avatar/AvatarManager.cpp +++ b/interface/src/avatar/AvatarManager.cpp @@ -367,7 +367,7 @@ void AvatarManager::addAvatarToSimulation(Avatar* avatar) { ShapeInfo shapeInfo; avatar->computeShapeInfo(shapeInfo); - btCollisionShape* shape = ObjectMotionState::getShapeManager()->getShape(shapeInfo); + btCollisionShape* shape = const_cast(ObjectMotionState::getShapeManager()->getShape(shapeInfo)); if (shape) { // we don't add to the simulation now, we put it on a list to be added later AvatarMotionState* motionState = new AvatarMotionState(avatar, shape); diff --git a/interface/src/avatar/AvatarMotionState.cpp b/interface/src/avatar/AvatarMotionState.cpp index 9cc3095ae2..335245670b 100644 --- a/interface/src/avatar/AvatarMotionState.cpp +++ b/interface/src/avatar/AvatarMotionState.cpp @@ -17,7 +17,7 @@ #include "AvatarMotionState.h" #include "BulletUtil.h" -AvatarMotionState::AvatarMotionState(Avatar* avatar, btCollisionShape* shape) : ObjectMotionState(shape), _avatar(avatar) { +AvatarMotionState::AvatarMotionState(Avatar* avatar, const btCollisionShape* shape) : ObjectMotionState(shape), _avatar(avatar) { assert(_avatar); _type = MOTIONSTATE_TYPE_AVATAR; if (_shape) { @@ -47,7 +47,7 @@ PhysicsMotionType AvatarMotionState::computePhysicsMotionType() const { } // virtual and protected -btCollisionShape* AvatarMotionState::computeNewShape() { +const btCollisionShape* AvatarMotionState::computeNewShape() { ShapeInfo shapeInfo; _avatar->computeShapeInfo(shapeInfo); return getShapeManager()->getShape(shapeInfo); diff --git a/interface/src/avatar/AvatarMotionState.h b/interface/src/avatar/AvatarMotionState.h index 04aa5ea57c..66824d6e37 100644 --- a/interface/src/avatar/AvatarMotionState.h +++ b/interface/src/avatar/AvatarMotionState.h @@ -20,7 +20,7 @@ class Avatar; class AvatarMotionState : public ObjectMotionState { public: - AvatarMotionState(Avatar* avatar, btCollisionShape* shape); + AvatarMotionState(Avatar* avatar, const btCollisionShape* shape); virtual PhysicsMotionType getMotionType() const override { return _motionType; } @@ -72,7 +72,7 @@ protected: ~AvatarMotionState(); virtual bool isReadyToComputeShape() const override { return true; } - virtual btCollisionShape* computeNewShape() override; + virtual const btCollisionShape* computeNewShape() override; // The AvatarMotionState keeps a RAW backpointer to its Avatar because all AvatarMotionState // instances are "owned" by their corresponding Avatar instance and are deleted in the Avatar dtor. diff --git a/libraries/physics/src/PhysicalEntitySimulation.cpp b/libraries/physics/src/PhysicalEntitySimulation.cpp index 9714059e7c..be420604b3 100644 --- a/libraries/physics/src/PhysicalEntitySimulation.cpp +++ b/libraries/physics/src/PhysicalEntitySimulation.cpp @@ -225,7 +225,7 @@ void PhysicalEntitySimulation::getObjectsToAddToPhysics(VectorOfMotionStates& re << "at" << entity->getPosition() << " will be reduced"; } } - btCollisionShape* shape = ObjectMotionState::getShapeManager()->getShape(shapeInfo); + btCollisionShape* shape = const_cast(ObjectMotionState::getShapeManager()->getShape(shapeInfo)); if (shape) { EntityMotionState* motionState = new EntityMotionState(shape, entity); entity->setPhysicsInfo(static_cast(motionState)); diff --git a/libraries/physics/src/PhysicsEngine.cpp b/libraries/physics/src/PhysicsEngine.cpp index aa6c1b4e40..1c6d5d535e 100644 --- a/libraries/physics/src/PhysicsEngine.cpp +++ b/libraries/physics/src/PhysicsEngine.cpp @@ -76,7 +76,7 @@ void PhysicsEngine::addObjectToDynamicsWorld(ObjectMotionState* motionState) { switch(motionType) { case MOTION_TYPE_KINEMATIC: { if (!body) { - btCollisionShape* shape = motionState->getShape(); + btCollisionShape* shape = const_cast(motionState->getShape()); assert(shape); body = new btRigidBody(mass, motionState, shape, inertia); motionState->setRigidBody(body); @@ -93,7 +93,7 @@ void PhysicsEngine::addObjectToDynamicsWorld(ObjectMotionState* motionState) { } case MOTION_TYPE_DYNAMIC: { mass = motionState->getMass(); - btCollisionShape* shape = motionState->getShape(); + btCollisionShape* shape = const_cast(motionState->getShape()); assert(shape); shape->calculateLocalInertia(mass, inertia); if (!body) { @@ -120,7 +120,7 @@ void PhysicsEngine::addObjectToDynamicsWorld(ObjectMotionState* motionState) { default: { if (!body) { assert(motionState->getShape()); - body = new btRigidBody(mass, motionState, motionState->getShape(), inertia); + body = new btRigidBody(mass, motionState, const_cast(motionState->getShape()), inertia); motionState->setRigidBody(body); } else { body->setMassProps(mass, inertia); diff --git a/libraries/physics/src/ShapeFactory.cpp b/libraries/physics/src/ShapeFactory.cpp index 8576a9caee..f11b0f95dc 100644 --- a/libraries/physics/src/ShapeFactory.cpp +++ b/libraries/physics/src/ShapeFactory.cpp @@ -247,7 +247,7 @@ void deleteStaticMeshArray(btTriangleIndexVertexArray* dataArray) { delete dataArray; } -btCollisionShape* ShapeFactory::createShapeFromInfo(const ShapeInfo& info) { +const btCollisionShape* ShapeFactory::createShapeFromInfo(const ShapeInfo& info) { btCollisionShape* shape = NULL; int type = info.getType(); switch(type) { @@ -359,10 +359,14 @@ btCollisionShape* ShapeFactory::createShapeFromInfo(const ShapeInfo& info) { return shape; } -void ShapeFactory::deleteShape(btCollisionShape* shape) { +void ShapeFactory::deleteShape(const btCollisionShape* shape) { assert(shape); - if (shape->getShapeType() == (int)COMPOUND_SHAPE_PROXYTYPE) { - btCompoundShape* compoundShape = static_cast(shape); + // ShapeFactory is responsible for deleting all shapes, even the const ones that are stored + // in the ShapeManager, so we must cast to non-const here when deleting. + // so we cast to non-const here when deleting memory. + btCollisionShape* nonConstShape = const_cast(shape); + if (nonConstShape->getShapeType() == (int)COMPOUND_SHAPE_PROXYTYPE) { + btCompoundShape* compoundShape = static_cast(nonConstShape); const int numChildShapes = compoundShape->getNumChildShapes(); for (int i = 0; i < numChildShapes; i ++) { btCollisionShape* childShape = compoundShape->getChildShape(i); @@ -374,7 +378,7 @@ void ShapeFactory::deleteShape(btCollisionShape* shape) { } } } - delete shape; + delete nonConstShape; } // the dataArray must be created before we create the StaticMeshShape diff --git a/libraries/physics/src/ShapeFactory.h b/libraries/physics/src/ShapeFactory.h index 6202612eb9..a1022104dd 100644 --- a/libraries/physics/src/ShapeFactory.h +++ b/libraries/physics/src/ShapeFactory.h @@ -20,8 +20,8 @@ // translates between ShapeInfo and btShape namespace ShapeFactory { - btCollisionShape* createShapeFromInfo(const ShapeInfo& info); - void deleteShape(btCollisionShape* shape); + const btCollisionShape* createShapeFromInfo(const ShapeInfo& info); + void deleteShape(const btCollisionShape* shape); //btTriangleIndexVertexArray* createStaticMeshArray(const ShapeInfo& info); //void deleteStaticMeshArray(btTriangleIndexVertexArray* dataArray); diff --git a/libraries/physics/src/ShapeManager.cpp b/libraries/physics/src/ShapeManager.cpp index 35046adcfd..b61fb0037b 100644 --- a/libraries/physics/src/ShapeManager.cpp +++ b/libraries/physics/src/ShapeManager.cpp @@ -28,7 +28,7 @@ ShapeManager::~ShapeManager() { _shapeMap.clear(); } -btCollisionShape* ShapeManager::getShape(const ShapeInfo& info) { +const btCollisionShape* ShapeManager::getShape(const ShapeInfo& info) { if (info.getType() == SHAPE_TYPE_NONE) { return nullptr; } @@ -45,7 +45,7 @@ btCollisionShape* ShapeManager::getShape(const ShapeInfo& info) { shapeRef->refCount++; return shapeRef->shape; } - btCollisionShape* shape = ShapeFactory::createShapeFromInfo(info); + const btCollisionShape* shape = ShapeFactory::createShapeFromInfo(info); if (shape) { ShapeReference newRef; newRef.refCount = 1; diff --git a/libraries/physics/src/ShapeManager.h b/libraries/physics/src/ShapeManager.h index cdb2b78789..261c06ddb9 100644 --- a/libraries/physics/src/ShapeManager.h +++ b/libraries/physics/src/ShapeManager.h @@ -26,7 +26,7 @@ public: ~ShapeManager(); /// \return pointer to shape - btCollisionShape* getShape(const ShapeInfo& info); + const btCollisionShape* getShape(const ShapeInfo& info); /// \return true if shape was found and released bool releaseShape(const btCollisionShape* shape); @@ -46,7 +46,7 @@ private: class ShapeReference { public: int refCount; - btCollisionShape* shape; + const btCollisionShape* shape; DoubleHashKey key; ShapeReference() : refCount(0), shape(nullptr) {} };