2015-11-02 22:49:22 +00:00
|
|
|
#include "visitor.hpp"
|
|
|
|
|
2018-07-14 02:48:59 +00:00
|
|
|
#include <osg/Drawable>
|
2017-02-04 01:15:55 +00:00
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
|
2015-11-02 22:49:22 +00:00
|
|
|
#include <osgParticle/ParticleSystem>
|
|
|
|
|
2018-08-21 19:41:05 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
2022-08-02 22:00:54 +00:00
|
|
|
#include <components/misc/strings/algorithm.hpp>
|
2015-11-02 22:49:22 +00:00
|
|
|
|
2021-09-04 16:07:23 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <string_view>
|
|
|
|
|
2015-11-02 22:49:22 +00:00
|
|
|
namespace SceneUtil
|
|
|
|
{
|
|
|
|
|
2017-02-04 01:15:55 +00:00
|
|
|
bool FindByNameVisitor::checkGroup(osg::Group &group)
|
2015-11-02 22:49:22 +00:00
|
|
|
{
|
|
|
|
if (Misc::StringUtils::ciEqual(group.getName(), mNameToFind))
|
|
|
|
{
|
|
|
|
mFoundNode = &group;
|
2017-02-04 01:15:55 +00:00
|
|
|
return true;
|
2015-11-02 22:49:22 +00:00
|
|
|
}
|
2017-02-04 01:15:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-04 20:29:59 +00:00
|
|
|
void FindByClassVisitor::apply(osg::Node &node)
|
|
|
|
{
|
2021-09-04 16:07:23 +00:00
|
|
|
if (Misc::StringUtils::ciEqual(std::string_view(node.className()), mNameToFind))
|
2017-10-12 10:56:03 +00:00
|
|
|
mFoundNodes.push_back(&node);
|
2018-07-14 02:48:59 +00:00
|
|
|
|
2017-10-12 10:56:03 +00:00
|
|
|
traverse(node);
|
2017-10-04 20:29:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-04 01:15:55 +00:00
|
|
|
void FindByNameVisitor::apply(osg::Group &group)
|
|
|
|
{
|
2021-09-08 17:23:35 +00:00
|
|
|
if (!mFoundNode && !checkGroup(group))
|
2017-02-04 01:15:55 +00:00
|
|
|
traverse(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindByNameVisitor::apply(osg::MatrixTransform &node)
|
|
|
|
{
|
2021-09-08 17:23:35 +00:00
|
|
|
if (!mFoundNode && !checkGroup(node))
|
2017-02-04 01:15:55 +00:00
|
|
|
traverse(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindByNameVisitor::apply(osg::Geometry&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-07-14 02:48:59 +00:00
|
|
|
void NodeMapVisitor::apply(osg::MatrixTransform& trans)
|
|
|
|
{
|
refactors a case insensitive map (#3184)
This PR aims to spark the retirement of a questionable pattern I have found all over our code base. I will illustrate how this pattern encourages code duplication, lacks type safety, requires documentation and can be prone to bugs.
```
std::map<std::string, Object> mMap; // Stored in all lowercase for a case-insensitive lookup
std::string lowerKey = Misc::StringUtils::lowerCase(key);
mMap.emplace(lowerKey, object);
std::string lowerKey = Misc::StringUtils::lowerCase(key);
mMap.find(lowerKey);
mMap.find(key); // Not found. Oops!
```
An alternative approach produces no such issues.
```
std::unordered_map<std::string, Object, Misc::StringUtils::CiHash, Misc::StringUtils::CiEqual> mMap;
mMap.emplace(key, object);
mMap.find(key);
```
Of course, such an alternative will work for a `map` as well, but an `unordered_map` is generally preferable over a `map` with these changes because we have moved `lowerCase` into the comparison operator.
In this PR I have refactored `Animation::mNodeMap` accordingly. I have reviewed and adapted all direct and indirect usage of `Animation::mNodeMap` to ensure we do not change behaviour with this PR.
2021-10-25 07:18:26 +00:00
|
|
|
// Choose first found node in file
|
2021-02-02 15:33:40 +00:00
|
|
|
|
2021-02-02 19:41:17 +00:00
|
|
|
if (trans.libraryName() == std::string("osgAnimation"))
|
|
|
|
{
|
refactors a case insensitive map (#3184)
This PR aims to spark the retirement of a questionable pattern I have found all over our code base. I will illustrate how this pattern encourages code duplication, lacks type safety, requires documentation and can be prone to bugs.
```
std::map<std::string, Object> mMap; // Stored in all lowercase for a case-insensitive lookup
std::string lowerKey = Misc::StringUtils::lowerCase(key);
mMap.emplace(lowerKey, object);
std::string lowerKey = Misc::StringUtils::lowerCase(key);
mMap.find(lowerKey);
mMap.find(key); // Not found. Oops!
```
An alternative approach produces no such issues.
```
std::unordered_map<std::string, Object, Misc::StringUtils::CiHash, Misc::StringUtils::CiEqual> mMap;
mMap.emplace(key, object);
mMap.find(key);
```
Of course, such an alternative will work for a `map` as well, but an `unordered_map` is generally preferable over a `map` with these changes because we have moved `lowerCase` into the comparison operator.
In this PR I have refactored `Animation::mNodeMap` accordingly. I have reviewed and adapted all direct and indirect usage of `Animation::mNodeMap` to ensure we do not change behaviour with this PR.
2021-10-25 07:18:26 +00:00
|
|
|
std::string nodeName = trans.getName();
|
2021-02-02 19:41:17 +00:00
|
|
|
// Convert underscores to whitespaces as a workaround for Collada (OpenMW's animation system uses whitespace-separated names)
|
refactors a case insensitive map (#3184)
This PR aims to spark the retirement of a questionable pattern I have found all over our code base. I will illustrate how this pattern encourages code duplication, lacks type safety, requires documentation and can be prone to bugs.
```
std::map<std::string, Object> mMap; // Stored in all lowercase for a case-insensitive lookup
std::string lowerKey = Misc::StringUtils::lowerCase(key);
mMap.emplace(lowerKey, object);
std::string lowerKey = Misc::StringUtils::lowerCase(key);
mMap.find(lowerKey);
mMap.find(key); // Not found. Oops!
```
An alternative approach produces no such issues.
```
std::unordered_map<std::string, Object, Misc::StringUtils::CiHash, Misc::StringUtils::CiEqual> mMap;
mMap.emplace(key, object);
mMap.find(key);
```
Of course, such an alternative will work for a `map` as well, but an `unordered_map` is generally preferable over a `map` with these changes because we have moved `lowerCase` into the comparison operator.
In this PR I have refactored `Animation::mNodeMap` accordingly. I have reviewed and adapted all direct and indirect usage of `Animation::mNodeMap` to ensure we do not change behaviour with this PR.
2021-10-25 07:18:26 +00:00
|
|
|
std::replace(nodeName.begin(), nodeName.end(), '_', ' ');
|
|
|
|
mMap.emplace(nodeName, &trans);
|
2021-02-02 19:41:17 +00:00
|
|
|
}
|
refactors a case insensitive map (#3184)
This PR aims to spark the retirement of a questionable pattern I have found all over our code base. I will illustrate how this pattern encourages code duplication, lacks type safety, requires documentation and can be prone to bugs.
```
std::map<std::string, Object> mMap; // Stored in all lowercase for a case-insensitive lookup
std::string lowerKey = Misc::StringUtils::lowerCase(key);
mMap.emplace(lowerKey, object);
std::string lowerKey = Misc::StringUtils::lowerCase(key);
mMap.find(lowerKey);
mMap.find(key); // Not found. Oops!
```
An alternative approach produces no such issues.
```
std::unordered_map<std::string, Object, Misc::StringUtils::CiHash, Misc::StringUtils::CiEqual> mMap;
mMap.emplace(key, object);
mMap.find(key);
```
Of course, such an alternative will work for a `map` as well, but an `unordered_map` is generally preferable over a `map` with these changes because we have moved `lowerCase` into the comparison operator.
In this PR I have refactored `Animation::mNodeMap` accordingly. I have reviewed and adapted all direct and indirect usage of `Animation::mNodeMap` to ensure we do not change behaviour with this PR.
2021-10-25 07:18:26 +00:00
|
|
|
else
|
|
|
|
mMap.emplace(trans.getName(), &trans);
|
2018-07-14 02:48:59 +00:00
|
|
|
|
|
|
|
traverse(trans);
|
|
|
|
}
|
|
|
|
|
2018-07-18 02:28:05 +00:00
|
|
|
void RemoveVisitor::remove()
|
|
|
|
{
|
|
|
|
for (RemoveVec::iterator it = mToRemove.begin(); it != mToRemove.end(); ++it)
|
|
|
|
{
|
|
|
|
if (!it->second->removeChild(it->first))
|
2018-08-21 19:41:05 +00:00
|
|
|
Log(Debug::Error) << "error removing " << it->first->getName();
|
2018-07-18 02:28:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CleanObjectRootVisitor::apply(osg::Drawable& drw)
|
|
|
|
{
|
|
|
|
applyDrawable(drw);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CleanObjectRootVisitor::apply(osg::Group& node)
|
|
|
|
{
|
|
|
|
applyNode(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CleanObjectRootVisitor::apply(osg::MatrixTransform& node)
|
|
|
|
{
|
|
|
|
applyNode(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CleanObjectRootVisitor::apply(osg::Node& node)
|
2018-07-14 02:48:59 +00:00
|
|
|
{
|
2018-07-18 02:28:05 +00:00
|
|
|
applyNode(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CleanObjectRootVisitor::applyNode(osg::Node& node)
|
|
|
|
{
|
|
|
|
if (node.getStateSet())
|
2018-10-09 06:21:12 +00:00
|
|
|
node.setStateSet(nullptr);
|
2018-07-18 02:28:05 +00:00
|
|
|
|
|
|
|
if (node.getNodeMask() == 0x1 && node.getNumParents() == 1)
|
2020-10-17 08:26:35 +00:00
|
|
|
mToRemove.emplace_back(&node, node.getParent(0));
|
2018-07-18 02:28:05 +00:00
|
|
|
else
|
|
|
|
traverse(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CleanObjectRootVisitor::applyDrawable(osg::Node& node)
|
|
|
|
{
|
|
|
|
osg::NodePath::iterator parent = getNodePath().end()-2;
|
|
|
|
// We know that the parent is a Group because only Groups can have children.
|
|
|
|
osg::Group* parentGroup = static_cast<osg::Group*>(*parent);
|
|
|
|
|
|
|
|
// Try to prune nodes that would be empty after the removal
|
|
|
|
if (parent != getNodePath().begin())
|
|
|
|
{
|
|
|
|
// This could be extended to remove the parent's parent, and so on if they are empty as well.
|
|
|
|
// But for NIF files, there won't be a benefit since only TriShapes can be set to STATIC dataVariance.
|
|
|
|
osg::Group* parentParent = static_cast<osg::Group*>(*(parent - 1));
|
|
|
|
if (parentGroup->getNumChildren() == 1 && parentGroup->getDataVariance() == osg::Object::STATIC)
|
|
|
|
{
|
2020-10-17 08:26:35 +00:00
|
|
|
mToRemove.emplace_back(parentGroup, parentParent);
|
2018-07-18 02:28:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 08:26:35 +00:00
|
|
|
mToRemove.emplace_back(&node, parentGroup);
|
2018-07-18 02:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveTriBipVisitor::apply(osg::Drawable& drw)
|
|
|
|
{
|
|
|
|
applyImpl(drw);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveTriBipVisitor::apply(osg::Group& node)
|
|
|
|
{
|
|
|
|
traverse(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveTriBipVisitor::apply(osg::MatrixTransform& node)
|
|
|
|
{
|
|
|
|
traverse(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveTriBipVisitor::applyImpl(osg::Node& node)
|
|
|
|
{
|
|
|
|
const std::string toFind = "tri bip";
|
|
|
|
if (Misc::StringUtils::ciCompareLen(node.getName(), toFind, toFind.size()) == 0)
|
|
|
|
{
|
|
|
|
osg::Group* parent = static_cast<osg::Group*>(*(getNodePath().end()-2));
|
|
|
|
// Not safe to remove in apply(), since the visitor is still iterating the child list
|
2020-10-17 08:26:35 +00:00
|
|
|
mToRemove.emplace_back(&node, parent);
|
2018-07-18 02:28:05 +00:00
|
|
|
}
|
2018-07-14 02:48:59 +00:00
|
|
|
}
|
2015-11-02 22:49:22 +00:00
|
|
|
}
|