mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-14 11:11:32 +00:00
optimises skeleton.cpp (#3158)
With this PR we optimise a function that is called quite often when loading new cells. We remove avoidable dynamic_casts. We remove an unused pair.second element. We convert a map to an unordered_map because its ordering is irrelevant in this case. We avoid adding the root Skeleton node to the bones' node path.
This commit is contained in:
parent
7af245d205
commit
b61140b8ba
2 changed files with 13 additions and 18 deletions
|
@ -1,6 +1,5 @@
|
||||||
#include "skeleton.hpp"
|
#include "skeleton.hpp"
|
||||||
|
|
||||||
#include <osg/Transform>
|
|
||||||
#include <osg/MatrixTransform>
|
#include <osg/MatrixTransform>
|
||||||
|
|
||||||
#include <components/debug/debuglog.hpp>
|
#include <components/debug/debuglog.hpp>
|
||||||
|
@ -12,24 +11,24 @@ namespace SceneUtil
|
||||||
class InitBoneCacheVisitor : public osg::NodeVisitor
|
class InitBoneCacheVisitor : public osg::NodeVisitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
InitBoneCacheVisitor(std::map<std::string, std::pair<osg::NodePath, osg::MatrixTransform*> >& cache)
|
typedef std::vector<osg::MatrixTransform*> TransformPath;
|
||||||
|
InitBoneCacheVisitor(std::unordered_map<std::string, TransformPath>& cache)
|
||||||
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
|
: osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
|
||||||
, mCache(cache)
|
, mCache(cache)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void apply(osg::Transform &node) override
|
void apply(osg::MatrixTransform &node) override
|
||||||
{
|
{
|
||||||
osg::MatrixTransform* bone = node.asMatrixTransform();
|
mPath.push_back(&node);
|
||||||
if (!bone)
|
mCache[Misc::StringUtils::lowerCase(node.getName())] = mPath;
|
||||||
return;
|
|
||||||
|
|
||||||
mCache[Misc::StringUtils::lowerCase(bone->getName())] = std::make_pair(getNodePath(), bone);
|
|
||||||
|
|
||||||
traverse(node);
|
traverse(node);
|
||||||
|
mPath.pop_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<std::string, std::pair<osg::NodePath, osg::MatrixTransform*> >& mCache;
|
TransformPath mPath;
|
||||||
|
std::unordered_map<std::string, TransformPath>& mCache;
|
||||||
};
|
};
|
||||||
|
|
||||||
Skeleton::Skeleton()
|
Skeleton::Skeleton()
|
||||||
|
@ -73,18 +72,13 @@ Bone* Skeleton::getBone(const std::string &name)
|
||||||
mRootBone.reset(new Bone);
|
mRootBone.reset(new Bone);
|
||||||
}
|
}
|
||||||
|
|
||||||
const osg::NodePath& path = found->second.first;
|
|
||||||
Bone* bone = mRootBone.get();
|
Bone* bone = mRootBone.get();
|
||||||
for (osg::NodePath::const_iterator it = path.begin(); it != path.end(); ++it)
|
for (osg::MatrixTransform* matrixTransform : found->second)
|
||||||
{
|
{
|
||||||
osg::MatrixTransform* matrixTransform = dynamic_cast<osg::MatrixTransform*>(*it);
|
|
||||||
if (!matrixTransform)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
Bone* child = nullptr;
|
Bone* child = nullptr;
|
||||||
for (unsigned int i=0; i<bone->mChildren.size(); ++i)
|
for (unsigned int i=0; i<bone->mChildren.size(); ++i)
|
||||||
{
|
{
|
||||||
if (bone->mChildren[i]->mNode == *it)
|
if (bone->mChildren[i]->mNode == matrixTransform)
|
||||||
{
|
{
|
||||||
child = bone->mChildren[i];
|
child = bone->mChildren[i];
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include <osg/Group>
|
#include <osg/Group>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace SceneUtil
|
namespace SceneUtil
|
||||||
{
|
{
|
||||||
|
@ -72,7 +73,7 @@ namespace SceneUtil
|
||||||
// As far as the scene graph goes we support multiple root bones.
|
// As far as the scene graph goes we support multiple root bones.
|
||||||
std::unique_ptr<Bone> mRootBone;
|
std::unique_ptr<Bone> mRootBone;
|
||||||
|
|
||||||
typedef std::map<std::string, std::pair<osg::NodePath, osg::MatrixTransform*> > BoneCache;
|
typedef std::unordered_map<std::string, std::vector<osg::MatrixTransform*> > BoneCache;
|
||||||
BoneCache mBoneCache;
|
BoneCache mBoneCache;
|
||||||
bool mBoneCacheInit;
|
bool mBoneCacheInit;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue