Skip to content

Commit

Permalink
simplify tree "rebalancing"
Browse files Browse the repository at this point in the history
We now unconditionally ensure that the graph is a tree (as it should),
by always removing the parent from the target node, and reconnecting
said node to the new transformation.

The very complex implementation currently there did not seem to be needed.
  • Loading branch information
doudou committed Jun 25, 2019
1 parent 405114b commit dc9f56c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 68 deletions.
67 changes: 4 additions & 63 deletions src/TransformerGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,43 +485,6 @@ static void invertOSGTransform(osg::Vec3d& trans, osg::Quat& quat,
std::swap(source_frame, target_frame);
}

static void makeRoot(osg::Node& _transformer,
osg::ref_ptr<osg::PositionAttitudeTransform> desiredRoot,
std::set<osg::Node*> ancestors)
{
PositionAttitudeTransform* transformer = getTransform(&_transformer);

osg::Vec3d trans = osg::Vec3d(0, 0, 0);
osg::Quat rot = osg::Quat(0, 0, 0, 1);

ref_ptr<PositionAttitudeTransform> currentNode(desiredRoot);
ref_ptr<PositionAttitudeTransform> lastNode(transformer);
while(currentNode != transformer && !ancestors.count(currentNode))
{
osg::Vec3d nextTrans = currentNode->getPosition();
osg::Quat nextRot = currentNode->getAttitude();
currentNode->setPosition(trans);
currentNode->setAttitude(rot);
rot = nextRot.inverse();
trans = -(rot * nextTrans);

ref_ptr<PositionAttitudeTransform> parent(getTransform(currentNode->getParent(0)));
parent->removeChild(currentNode);
lastNode->addChild(currentNode);
lastNode = currentNode;
currentNode = parent;
}
}

void TransformerGraph::makeRoot(osg::Node& _transformer, std::string const& frame)
{
ref_ptr<PositionAttitudeTransform> desiredRoot(FindFrame::find(_transformer, frame));
if (!desiredRoot)
return;

return ::makeRoot(_transformer, desiredRoot, std::set<osg::Node*>());
}

bool TransformerGraph::setTransformation(osg::Node &transformer,const std::string &_source_frame,const std::string &_target_frame,
const osg::Quat &_quat, const osg::Vec3d &_trans)
{
Expand Down Expand Up @@ -549,36 +512,14 @@ bool TransformerGraph::setTransformation(osg::Node &transformer,const std::strin
}
else if (target->getParent(0) != source)
{
if (target == &transformer || (source->getParent(0) == &transformer && target->getParent(0) != &transformer))
if (target == &transformer)
{
invertOSGTransform(trans, quat, source, target, source_frame, target_frame);
}

if (target->getParent(0) != &transformer)
{
std::set<osg::Node*> ancestors;
osg::ref_ptr<osg::Node> sourceAncestor = source;
while (sourceAncestor != &transformer)
{
ancestors.insert(sourceAncestor);
if (sourceAncestor->getParent(0) == target)
{
target->removeChild(sourceAncestor);
getTransform(&transformer)->addChild(sourceAncestor);
ancestors.clear();
break;
}
else
sourceAncestor = sourceAncestor->getParent(0);
}

::makeRoot(transformer, getTransform(target), ancestors);

}

osg::ref_ptr<osg::Node> node = target; //insures that node is not deleted
removeFrame(transformer,target_frame);
source->addChild(node);
osg::ref_ptr<osg::Node> node = target; //insures that node is not deleted
target->getParent(0)->removeChild(target);
source->addChild(target);
}

target->setAttitude(quat);
Expand Down
5 changes: 0 additions & 5 deletions src/TransformerGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,6 @@ namespace vizkit3d
static bool setTransformation(osg::Node &transformer,const std::string &source_frame,const std::string &target_frame,
const osg::Quat &quat, const osg::Vec3d &trans);

/** Ensures that the transform node for the given frame is a direct
* child of the transformer node
*/
static void makeRoot(osg::Node& transformer, std::string const& desiredRoot);

/**
* Gets the transformation between two coordinate frames. Thereby unlike setTransformation the frames do not have to be subsequently.
*
Expand Down

0 comments on commit dc9f56c

Please sign in to comment.