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.
pull/3163/head
Bo Svensson 3 years ago committed by GitHub
parent 7af245d205
commit b61140b8ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,5 @@
#include "skeleton.hpp"
#include <osg/Transform>
#include <osg/MatrixTransform>
#include <components/debug/debuglog.hpp>
@ -12,24 +11,24 @@ namespace SceneUtil
class InitBoneCacheVisitor : public osg::NodeVisitor
{
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)
, mCache(cache)
{
}
void apply(osg::Transform &node) override
void apply(osg::MatrixTransform &node) override
{
osg::MatrixTransform* bone = node.asMatrixTransform();
if (!bone)
return;
mCache[Misc::StringUtils::lowerCase(bone->getName())] = std::make_pair(getNodePath(), bone);
mPath.push_back(&node);
mCache[Misc::StringUtils::lowerCase(node.getName())] = mPath;
traverse(node);
mPath.pop_back();
}
private:
std::map<std::string, std::pair<osg::NodePath, osg::MatrixTransform*> >& mCache;
TransformPath mPath;
std::unordered_map<std::string, TransformPath>& mCache;
};
Skeleton::Skeleton()
@ -73,18 +72,13 @@ Bone* Skeleton::getBone(const std::string &name)
mRootBone.reset(new Bone);
}
const osg::NodePath& path = found->second.first;
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;
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];
break;

@ -4,6 +4,7 @@
#include <osg/Group>
#include <memory>
#include <unordered_map>
namespace SceneUtil
{
@ -72,7 +73,7 @@ namespace SceneUtil
// As far as the scene graph goes we support multiple root bones.
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;
bool mBoneCacheInit;

Loading…
Cancel
Save