Skip to content

Commit

Permalink
Protect against non-finite inputs on Node and OrbitViewController (#655)
Browse files Browse the repository at this point in the history
Signed-off-by: Louise Poubel <louise@openrobotics.org>
  • Loading branch information
chapulina authored Jun 25, 2022
1 parent 859272a commit d773877
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
32 changes: 26 additions & 6 deletions include/ignition/rendering/base/BaseNode.hh
Original file line number Diff line number Diff line change
Expand Up @@ -336,16 +336,16 @@ namespace ignition
template <class T>
void BaseNode<T>::SetLocalPose(const math::Pose3d &_pose)
{
math::Pose3d pose = _pose;
pose.Pos() = pose.Pos() - pose.Rot() * this->origin;

if (!pose.IsFinite())
if (!_pose.IsFinite())
{
ignerr << "Unable to set pose of a node: "
<< "non-finite (nan, inf) values detected." << std::endl;
ignerr << "Unable to set non-finite pose [" << _pose
<< "] to node [" << this->Name() << "]" << std::endl;
return;
}

math::Pose3d pose = _pose;
pose.Pos() = pose.Pos() - pose.Rot() * this->origin;

if (!initialLocalPoseSet)
{
this->initialLocalPose = pose;
Expand Down Expand Up @@ -380,6 +380,13 @@ namespace ignition
template <class T>
void BaseNode<T>::SetLocalPosition(const math::Vector3d &_position)
{
if (!_position.IsFinite())
{
ignerr << "Unable to set non-finite position [" << _position
<< "] to node [" << this->Name() << "]" << std::endl;
return;
}

math::Pose3d pose = this->LocalPose();
pose.Pos() = _position;
this->SetLocalPose(pose);
Expand Down Expand Up @@ -411,6 +418,13 @@ namespace ignition
template <class T>
void BaseNode<T>::SetLocalRotation(const math::Quaterniond &_rotation)
{
if (!_rotation.IsFinite())
{
ignerr << "Unable to set non-finite rotation [" << _rotation
<< "] to node [" << this->Name() << "]" << std::endl;
return;
}

math::Pose3d pose = this->LocalPose();
pose.Rot() = _rotation;
this->SetLocalPose(pose);
Expand Down Expand Up @@ -525,6 +539,12 @@ namespace ignition
template <class T>
void BaseNode<T>::SetOrigin(const math::Vector3d &_origin)
{
if (!_origin.IsFinite())
{
ignerr << "Unable to set non-finite origin [" << _origin
<< "] to node [" << this->Name() << "]" << std::endl;
return;
}
this->origin = _origin;
}

Expand Down
23 changes: 21 additions & 2 deletions ogre2/src/Ogre2RayQuery.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,27 @@ void Ogre2RayQuery::SetFromCamera(const CameraPtr &_camera,
Ogre::Ray ray =
camera->ogreCamera->getCameraToViewportRay(screenPos.X(), screenPos.Y());

this->origin = Ogre2Conversions::Convert(ray.getOrigin());
this->direction = Ogre2Conversions::Convert(ray.getDirection());
auto originMath = Ogre2Conversions::Convert(ray.getOrigin());
if (originMath.IsFinite())
{
this->origin = originMath;
}
else
{
ignwarn << "Attempted to set non-finite origin from camera ["
<< camera->Name() << "]" << std::endl;
}

auto directionMath = Ogre2Conversions::Convert(ray.getDirection());
if (directionMath.IsFinite())
{
this->direction = directionMath;
}
else
{
ignwarn << "Attempted to set non-finite direction from camera ["
<< camera->Name() << "]" << std::endl;
}

this->dataPtr->camera = camera;

Expand Down
30 changes: 30 additions & 0 deletions src/OrbitViewController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*
*/

#include <cmath>

#include <ignition/common/Console.hh>

#include "ignition/rendering/OrbitViewController.hh"
Expand Down Expand Up @@ -92,6 +94,13 @@ const math::Vector3d &OrbitViewController::Target() const
//////////////////////////////////////////////////
void OrbitViewController::Zoom(const double _value)
{
if (!std::isfinite(_value))
{
ignerr << "Failed to zoom by non-finite value [" << _value << "]"
<< std::endl;
return;
}

if (!this->dataPtr->camera)
{
ignerr << "Camera is NULL" << std::endl;
Expand All @@ -113,12 +122,26 @@ void OrbitViewController::Zoom(const double _value)
//////////////////////////////////////////////////
void OrbitViewController::Pan(const math::Vector2d &_value)
{
if (!_value.IsFinite())
{
ignerr << "Failed to pan by non-finite value [" << _value << "]"
<< std::endl;
return;
}

if (!this->dataPtr->camera)
{
ignerr << "Camera is NULL" << std::endl;
return;
}

if (!this->dataPtr->camera->WorldPosition().IsFinite())
{
ignerr << "Camera world position isn't finite ["
<< this->dataPtr->camera->WorldPosition() << "]" << std::endl;
return;
}

double viewportWidth = this->dataPtr->camera->ImageWidth();
double viewportHeight = this->dataPtr->camera->ImageHeight();

Expand Down Expand Up @@ -148,6 +171,13 @@ void OrbitViewController::Pan(const math::Vector2d &_value)
//////////////////////////////////////////////////
void OrbitViewController::Orbit(const math::Vector2d &_value)
{
if (!_value.IsFinite())
{
ignerr << "Failed to orbit by non-finite value [" << _value << "]"
<< std::endl;
return;
}

if (!this->dataPtr->camera)
{
ignerr << "Camera is NULL" << std::endl;
Expand Down

0 comments on commit d773877

Please sign in to comment.