1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-06-26 13:11:34 +00:00

Merge branch 'skeleton_bone' into 'master'

Avoid manual memory management for Bone::mChildren and use range-based iteration

See merge request OpenMW/openmw!2149
This commit is contained in:
psi29a 2022-07-17 15:36:05 +00:00
commit 9d7f35bd53
2 changed files with 13 additions and 32 deletions

View file

@ -5,6 +5,8 @@
#include <components/debug/debuglog.hpp> #include <components/debug/debuglog.hpp>
#include <components/misc/stringops.hpp> #include <components/misc/stringops.hpp>
#include <algorithm>
namespace SceneUtil namespace SceneUtil
{ {
@ -75,23 +77,16 @@ Bone* Skeleton::getBone(const std::string &name)
Bone* bone = mRootBone.get(); Bone* bone = mRootBone.get();
for (osg::MatrixTransform* matrixTransform : found->second) for (osg::MatrixTransform* matrixTransform : found->second)
{ {
Bone* child = nullptr; const auto it = std::find_if(bone->mChildren.begin(), bone->mChildren.end(),
for (unsigned int i=0; i<bone->mChildren.size(); ++i) [&] (const auto& v) { return v->mNode == matrixTransform; });
{
if (bone->mChildren[i]->mNode == matrixTransform)
{
child = bone->mChildren[i];
break;
}
}
if (!child) if (it == bone->mChildren.end())
{ {
child = new Bone; bone = bone->mChildren.emplace_back(std::make_unique<Bone>()).get();
bone->mChildren.push_back(child);
mNeedToUpdateBoneMatrices = true; mNeedToUpdateBoneMatrices = true;
} }
bone = child; else
bone = it->get();
bone->mNode = matrixTransform; bone->mNode = matrixTransform;
} }
@ -110,8 +105,8 @@ void Skeleton::updateBoneMatrices(unsigned int traversalNumber)
{ {
if (mRootBone.get()) if (mRootBone.get())
{ {
for (unsigned int i=0; i<mRootBone->mChildren.size(); ++i) for (const auto& child : mRootBone->mChildren)
mRootBone->mChildren[i]->update(nullptr); child->update(nullptr);
} }
mNeedToUpdateBoneMatrices = false; 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) void Bone::update(const osg::Matrixf* parentMatrixInSkeletonSpace)
{ {
if (!mNode) if (!mNode)
@ -184,10 +172,8 @@ void Bone::update(const osg::Matrixf* parentMatrixInSkeletonSpace)
else else
mMatrixInSkeletonSpace = mNode->getMatrix(); mMatrixInSkeletonSpace = mNode->getMatrix();
for (unsigned int i=0; i<mChildren.size(); ++i) for (const auto& child : mChildren)
{ child->update(&mMatrixInSkeletonSpace);
mChildren[i]->update(&mMatrixInSkeletonSpace);
}
} }
} }

View file

@ -15,20 +15,15 @@ namespace SceneUtil
{ {
public: public:
Bone(); Bone();
~Bone();
osg::Matrixf mMatrixInSkeletonSpace; osg::Matrixf mMatrixInSkeletonSpace;
osg::MatrixTransform* mNode; 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. /// Update the skeleton-space matrix of this bone and all its children.
void update(const osg::Matrixf* parentMatrixInSkeletonSpace); 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. /// @brief Handles the bone matrices for any number of child RigGeometries.