Skip to content

Commit

Permalink
Visual::SetPose performance improvement / minor fixes (#3350)
Browse files Browse the repository at this point in the history
Cache sdf pose element in Visual::SetPose to speed up the
poseMsgs profiling block in Scene::PreRender by 10x for
some use cases.

Also fix undefined behavior in Scene.cc

Signed-off-by: Janosch Machowinski <J.Machowinski@cellumation.com>
Co-authored-by: Janosch Machowinski <J.Machowinski@cellumation.com>
  • Loading branch information
jmachowinski and Janosch Machowinski authored Oct 5, 2023
1 parent d1c455b commit 13f8dfe
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
6 changes: 2 additions & 4 deletions gazebo/rendering/Scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2142,8 +2142,7 @@ void Scene::PreRender()
ignition::math::Pose3d pose = msgs::ConvertIgn(pIter->second);
GZ_ASSERT(iter->second, "Visual pointer is NULL");
iter->second->SetPose(pose);
PoseMsgs_M::iterator prev = pIter++;
this->dataPtr->poseMsgs.erase(prev);
pIter = this->dataPtr->poseMsgs.erase(pIter);
}
else
++pIter;
Expand All @@ -2157,8 +2156,7 @@ void Scene::PreRender()
ignition::math::Pose3d pose = msgs::ConvertIgn(pIter->second);
lIter->second->SetPosition(pose.Pos());
lIter->second->SetRotation(pose.Rot());
auto prev = pIter++;
this->dataPtr->poseMsgs.erase(prev);
pIter = this->dataPtr->poseMsgs.erase(pIter);
}
else
++pIter;
Expand Down
20 changes: 17 additions & 3 deletions gazebo/rendering/Visual.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ void Visual::Fini()
}
this->dataPtr->scene.reset();

dataPtr->poseElem.reset();

if (this->dataPtr->sdf)
this->dataPtr->sdf->Reset();
this->dataPtr->sdf.reset();
Expand Down Expand Up @@ -458,6 +460,7 @@ void Visual::Load()
}

// Set the pose of the scene node
this->dataPtr->poseElem.reset();
this->SetPose(pose);
this->dataPtr->initialRelativePose = pose;

Expand Down Expand Up @@ -1984,14 +1987,25 @@ void Visual::SetRotation(const ignition::math::Quaterniond &_rot)
this->dataPtr->sceneNode->setOrientation(
Ogre::Quaternion(_rot.W(), _rot.X(), _rot.Y(), _rot.Z()));

this->dataPtr->sdf->GetElement("pose")->Set(this->Pose());
if(!dataPtr->poseElem)
{
dataPtr->poseElem = this->dataPtr->sdf->GetElement("pose");
}

dataPtr->poseElem->Set(this->Pose());
}

//////////////////////////////////////////////////
void Visual::SetPose(const ignition::math::Pose3d &_pose)
{
this->SetPosition(_pose.Pos());
this->SetRotation(_pose.Rot());
this->dataPtr->sceneNode->setPosition(_pose.Pos().X(), _pose.Pos().Y(), _pose.Pos().Z());
this->dataPtr->sceneNode->setOrientation(
Ogre::Quaternion(_pose.Rot().W(), _pose.Rot().X(), _pose.Rot().Y(), _pose.Rot().Z()));
if(!dataPtr->poseElem)
{
dataPtr->poseElem = this->dataPtr->sdf->GetElement("pose");
}
dataPtr->poseElem->Set(this->Pose());
}

//////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions gazebo/rendering/VisualPrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ namespace gazebo

/// \brief The SDF element for the visual.
public: sdf::ElementPtr sdf;
public: sdf::ElementPtr poseElem;

/// \brief The unique name for the visual's material.
public: std::string myMaterialName;
Expand Down

0 comments on commit 13f8dfe

Please sign in to comment.