Skip to content

Commit

Permalink
Check valid camera near far clip distances (#1082)
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <ichen@openrobotics.org>
  • Loading branch information
iche033 authored Nov 1, 2024
1 parent c130dd3 commit 1f4def6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
13 changes: 13 additions & 0 deletions include/gz/rendering/base/BaseCamera.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef GZ_RENDERING_BASE_BASECAMERA_HH_
#define GZ_RENDERING_BASE_BASECAMERA_HH_

#include <cmath>
#include <string>

#include <gz/math/Matrix3.hh>
Expand Down Expand Up @@ -709,6 +710,12 @@ namespace gz
template <class T>
void BaseCamera<T>::SetFarClipPlane(const double _far)
{
if (_far <= 0 || !std::isfinite(_far))
{
gzerr << "Far clip distance must be a finite number greater than 0."
<< std::endl;
return;
}
this->farClip = _far;
}

Expand All @@ -723,6 +730,12 @@ namespace gz
template <class T>
void BaseCamera<T>::SetNearClipPlane(const double _near)
{
if (_near <= 0 || !std::isfinite(_near))
{
gzerr << "Near clip distance must be a finite number greater than 0."
<< std::endl;
return;
}
this->nearClip = _near;
}

Expand Down
4 changes: 2 additions & 2 deletions ogre/src/OgreCamera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,14 @@ void OgreCamera::SetNearClipPlane(const double _near)
{
// this->nearClip = _near;
BaseCamera::SetNearClipPlane(_near);
this->ogreCamera->setNearClipDistance(_near);
this->ogreCamera->setNearClipDistance(this->nearClip);
}

//////////////////////////////////////////////////
void OgreCamera::SetFarClipPlane(const double _far)
{
BaseCamera::SetFarClipPlane(_far);
this->ogreCamera->setFarClipDistance(_far);
this->ogreCamera->setFarClipDistance(this->farClip);
}

//////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions ogre2/src/Ogre2Camera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,14 @@ void Ogre2Camera::SetProjectionType(CameraProjectionType _type)
void Ogre2Camera::SetNearClipPlane(const double _near)
{
BaseCamera::SetNearClipPlane(_near);
this->ogreCamera->setNearClipDistance(_near);
this->ogreCamera->setNearClipDistance(this->nearClip);
}

//////////////////////////////////////////////////
void Ogre2Camera::SetFarClipPlane(const double _far)
{
BaseCamera::SetFarClipPlane(_far);
this->ogreCamera->setFarClipDistance(_far);
this->ogreCamera->setFarClipDistance(this->farClip);
}

//////////////////////////////////////////////////
Expand Down
8 changes: 8 additions & 0 deletions test/common_test/Camera_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,18 @@ TEST_F(CameraTest, ViewProjectionMatrix)
camera->SetNearClipPlane(0.1);
EXPECT_DOUBLE_EQ(0.1, camera->NearClipPlane());

// set invalid clip distance and verify it is not set
camera->SetNearClipPlane(0.0);
EXPECT_DOUBLE_EQ(0.1, camera->NearClipPlane());

EXPECT_GT(camera->FarClipPlane(), 0);
camera->SetFarClipPlane(800);
EXPECT_DOUBLE_EQ(800, camera->FarClipPlane());

// set invalid clip distance and verify it is not set
camera->SetFarClipPlane(-10.0);
EXPECT_DOUBLE_EQ(800, camera->FarClipPlane());

EXPECT_NE(projMatrix, camera->ProjectionMatrix());

// view matrix
Expand Down

0 comments on commit 1f4def6

Please sign in to comment.