diff --git a/components/sceneutil/skeleton.cpp b/components/sceneutil/skeleton.cpp index 9f646ade5c..5c8449a50d 100644 --- a/components/sceneutil/skeleton.cpp +++ b/components/sceneutil/skeleton.cpp @@ -5,6 +5,8 @@ #include <components/debug/debuglog.hpp> #include <components/misc/stringops.hpp> +#include <algorithm> + namespace SceneUtil { @@ -75,23 +77,16 @@ Bone* Skeleton::getBone(const std::string &name) Bone* bone = mRootBone.get(); for (osg::MatrixTransform* matrixTransform : found->second) { - Bone* child = nullptr; - for (unsigned int i=0; i<bone->mChildren.size(); ++i) - { - if (bone->mChildren[i]->mNode == matrixTransform) - { - child = bone->mChildren[i]; - break; - } - } + const auto it = std::find_if(bone->mChildren.begin(), bone->mChildren.end(), + [&] (const auto& v) { return v->mNode == matrixTransform; }); - if (!child) + if (it == bone->mChildren.end()) { - child = new Bone; - bone->mChildren.push_back(child); + bone = bone->mChildren.emplace_back(std::make_unique<Bone>()).get(); mNeedToUpdateBoneMatrices = true; } - bone = child; + else + bone = it->get(); bone->mNode = matrixTransform; } @@ -110,8 +105,8 @@ void Skeleton::updateBoneMatrices(unsigned int traversalNumber) { if (mRootBone.get()) { - for (unsigned int i=0; i<mRootBone->mChildren.size(); ++i) - mRootBone->mChildren[i]->update(nullptr); + for (const auto& child : mRootBone->mChildren) + child->update(nullptr); } mNeedToUpdateBoneMatrices = false; @@ -165,13 +160,6 @@ Bone::Bone() { } -Bone::~Bone() -{ - for (unsigned int i=0; i<mChildren.size(); ++i) - delete mChildren[i]; - mChildren.clear(); -} - void Bone::update(const osg::Matrixf* parentMatrixInSkeletonSpace) { if (!mNode) @@ -184,10 +172,8 @@ void Bone::update(const osg::Matrixf* parentMatrixInSkeletonSpace) else mMatrixInSkeletonSpace = mNode->getMatrix(); - for (unsigned int i=0; i<mChildren.size(); ++i) - { - mChildren[i]->update(&mMatrixInSkeletonSpace); - } + for (const auto& child : mChildren) + child->update(&mMatrixInSkeletonSpace); } } diff --git a/components/sceneutil/skeleton.hpp b/components/sceneutil/skeleton.hpp index 10f4640249..7ca4887999 100644 --- a/components/sceneutil/skeleton.hpp +++ b/components/sceneutil/skeleton.hpp @@ -15,20 +15,15 @@ namespace SceneUtil { public: Bone(); - ~Bone(); osg::Matrixf mMatrixInSkeletonSpace; osg::MatrixTransform* mNode; - std::vector<Bone*> mChildren; + std::vector<std::unique_ptr<Bone>> mChildren; /// Update the skeleton-space matrix of this bone and all its children. void update(const osg::Matrixf* parentMatrixInSkeletonSpace); - - private: - Bone(const Bone&); - void operator=(const Bone&); }; /// @brief Handles the bone matrices for any number of child RigGeometries.