From ca8584f6f61ca277272000fad724506e55d7a45c Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 30 Oct 2021 02:58:40 +0200 Subject: [PATCH] Move functions independent from BulletShape into anonymous namespace --- components/resource/bulletshape.cpp | 112 +++++++++++++--------------- components/resource/bulletshape.hpp | 6 -- 2 files changed, 53 insertions(+), 65 deletions(-) diff --git a/components/resource/bulletshape.cpp b/components/resource/bulletshape.cpp index 798a6778e6..5e0415e59e 100644 --- a/components/resource/bulletshape.cpp +++ b/components/resource/bulletshape.cpp @@ -10,6 +10,53 @@ namespace Resource { +namespace +{ + btCollisionShape* duplicateCollisionShape(const btCollisionShape *shape) + { + if (shape == nullptr) + return nullptr; + + if (shape->isCompound()) + { + const btCompoundShape *comp = static_cast(shape); + btCompoundShape* newShape = new btCompoundShape; + + for (int i = 0, n = comp->getNumChildShapes(); i < n; ++i) + { + btCollisionShape* child = duplicateCollisionShape(comp->getChildShape(i)); + const btTransform& trans = comp->getChildTransform(i); + newShape->addChildShape(trans, child); + } + + return newShape; + } + + if (const btBvhTriangleMeshShape* trishape = dynamic_cast(shape)) + return new btScaledBvhTriangleMeshShape(const_cast(trishape), btVector3(1.f, 1.f, 1.f)); + + if (const btBoxShape* boxshape = dynamic_cast(shape)) + return new btBoxShape(*boxshape); + + if (shape->getShapeType() == TERRAIN_SHAPE_PROXYTYPE) + return new btHeightfieldTerrainShape(static_cast(*shape)); + + throw std::logic_error(std::string("Unhandled Bullet shape duplication: ") + shape->getName()); + } + + void deleteShape(btCollisionShape* shape) + { + if (shape->isCompound()) + { + btCompoundShape* compound = static_cast(shape); + for (int i = 0, n = compound->getNumChildShapes(); i < n; i++) + if (btCollisionShape* child = compound->getChildShape(i)) + deleteShape(child); + } + + delete shape; + } +} BulletShape::BulletShape() : mCollisionShape(nullptr) @@ -28,58 +75,10 @@ BulletShape::BulletShape(const BulletShape ©, const osg::CopyOp ©op) BulletShape::~BulletShape() { - deleteShape(mAvoidCollisionShape); - deleteShape(mCollisionShape); -} - -void BulletShape::deleteShape(btCollisionShape* shape) -{ - if(shape!=nullptr) - { - if(shape->isCompound()) - { - btCompoundShape* ms = static_cast(shape); - int a = ms->getNumChildShapes(); - for(int i=0; i getChildShape(i)); - } - delete shape; - } -} - -btCollisionShape* BulletShape::duplicateCollisionShape(const btCollisionShape *shape) const -{ - if(shape->isCompound()) - { - const btCompoundShape *comp = static_cast(shape); - btCompoundShape *newShape = new btCompoundShape; - - int numShapes = comp->getNumChildShapes(); - for(int i = 0;i < numShapes;++i) - { - btCollisionShape *child = duplicateCollisionShape(comp->getChildShape(i)); - const btTransform& trans = comp->getChildTransform(i); - newShape->addChildShape(trans, child); - } - - return newShape; - } - - if(const btBvhTriangleMeshShape* trishape = dynamic_cast(shape)) - { - btScaledBvhTriangleMeshShape* newShape = new btScaledBvhTriangleMeshShape(const_cast(trishape), btVector3(1.f, 1.f, 1.f)); - return newShape; - } - - if (const btBoxShape* boxshape = dynamic_cast(shape)) - { - return new btBoxShape(*boxshape); - } - - if (shape->getShapeType() == TERRAIN_SHAPE_PROXYTYPE) - return new btHeightfieldTerrainShape(static_cast(*shape)); - - throw std::logic_error(std::string("Unhandled Bullet shape duplication: ")+shape->getName()); + if (mAvoidCollisionShape != nullptr) + deleteShape(mAvoidCollisionShape); + if (mCollisionShape != nullptr) + deleteShape(mCollisionShape); } btCollisionShape *BulletShape::getCollisionShape() const @@ -115,14 +114,9 @@ BulletShapeInstance::BulletShapeInstance(osg::ref_ptr source) , mSource(source) { mCollisionBox = source->mCollisionBox; - mAnimatedShapes = source->mAnimatedShapes; - - if (source->mCollisionShape) - mCollisionShape = duplicateCollisionShape(source->mCollisionShape); - - if (source->mAvoidCollisionShape) - mAvoidCollisionShape = duplicateCollisionShape(source->mAvoidCollisionShape); + mCollisionShape = duplicateCollisionShape(source->mCollisionShape); + mAvoidCollisionShape = duplicateCollisionShape(source->mAvoidCollisionShape); } } diff --git a/components/resource/bulletshape.hpp b/components/resource/bulletshape.hpp index b40ab6b6a7..8b30464fe2 100644 --- a/components/resource/bulletshape.hpp +++ b/components/resource/bulletshape.hpp @@ -44,8 +44,6 @@ namespace Resource osg::ref_ptr makeInstance() const; - btCollisionShape* duplicateCollisionShape(const btCollisionShape* shape) const; - btCollisionShape* getCollisionShape() const; btCollisionShape* getAvoidCollisionShape() const; @@ -53,10 +51,6 @@ namespace Resource void setLocalScaling(const btVector3& scale); bool isAnimated() const; - - private: - - void deleteShape(btCollisionShape* shape); };