Skip to content

Commit

Permalink
Better approach to use std::variant (#1093)
Browse files Browse the repository at this point in the history
* Better approach to use std::variant

Signed-off-by: Maksim Derbasov <ntfs.hard@gmail.com>

* Iteration

Signed-off-by: Maksim Derbasov <ntfs.hard@gmail.com>

---------

Signed-off-by: Maksim Derbasov <ntfs.hard@gmail.com>
(cherry picked from commit 7765666)
  • Loading branch information
ntfshard authored and iche033 committed Dec 19, 2024
1 parent c28c1cc commit 6d02c15
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 81 deletions.
60 changes: 24 additions & 36 deletions ogre2/src/Ogre2GpuRays.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class GZ_RENDERING_OGRE2_HIDDEN
private: virtual void passPreExecute(
Ogre::CompositorPass *_pass) override;

/// \brief Callback when each pass is finisned executing.
/// \brief Callback when each pass is finished executing.
/// \param[in] _pass Ogre pass which has already executed
private: virtual void passPosExecute(
Ogre::CompositorPass *_pass) override;
Expand Down Expand Up @@ -156,7 +156,7 @@ class GZ_RENDERING_OGRE2_HIDDEN gz::rendering::Ogre2GpuRaysPrivate
/// range data
public: std::set<unsigned int> cubeFaceIdx;

/// \brief Main pass definition (used for visibility mask manipuluation).
/// \brief Main pass definition (used for visibility mask manipulation).
public: Ogre::CompositorPassSceneDef *mainPassSceneDef = nullptr;

/// \brief 1st pass compositor workspace. One for each cubemap camera
Expand Down Expand Up @@ -327,27 +327,21 @@ void Ogre2LaserRetroMaterialSwitcher::passPreExecute(
// get laser_retro
Variant tempLaserRetro = ogreVisual->UserData(laserRetroKey);

try
if (std::holds_alternative<float>(tempLaserRetro))
{
retroValue = std::get<float>(tempLaserRetro);
}
catch(...)
else if (std::holds_alternative<double>(tempLaserRetro))
{
try
{
retroValue = static_cast<float>(std::get<double>(tempLaserRetro));
}
catch(...)
{
try
{
retroValue = static_cast<float>(std::get<int>(tempLaserRetro));
}
catch(std::bad_variant_access &e)
{
gzerr << "Error casting user data: " << e.what() << "\n";
}
}
retroValue = static_cast<float>(std::get<double>(tempLaserRetro));
}
else if (std::holds_alternative<int>(tempLaserRetro))
{
retroValue = static_cast<float>(std::get<int>(tempLaserRetro));
}
else
{
gzerr << "Error casting user data: laser_retro\n";
}
}

Expand Down Expand Up @@ -448,27 +442,21 @@ void Ogre2LaserRetroMaterialSwitcher::passPreExecute(
// get laser_retro
Variant tempLaserRetro = visual->UserData(laserRetroKey);

try
if (std::holds_alternative<float>(tempLaserRetro))
{
retroValue = std::get<float>(tempLaserRetro);
}
catch (...)
else if (std::holds_alternative<double>(tempLaserRetro))
{
try
{
retroValue = static_cast<float>(std::get<double>(tempLaserRetro));
}
catch (...)
{
try
{
retroValue = static_cast<float>(std::get<int>(tempLaserRetro));
}
catch (std::bad_variant_access &e)
{
gzerr << "Error casting user data: " << e.what() << "\n";
}
}
retroValue = static_cast<float>(std::get<double>(tempLaserRetro));
}
else if (std::holds_alternative<int>(tempLaserRetro))
{
retroValue = static_cast<float>(std::get<int>(tempLaserRetro));
}
else
{
gzerr << "Error casting user data: laser_retro\n";
}
}

Expand Down
78 changes: 33 additions & 45 deletions ogre2/src/Ogre2ThermalCamera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ class Ogre2ThermalCameraMaterialSwitcher : public Ogre::Camera::Listener
/// \param[in] _resolution Temperature linear resolution
public: void SetLinearResolution(double _resolution);

/// \brief Callback when a camara is about to be rendered
/// \brief Callback when a camera is about to be rendered
/// \param[in] _cam Ogre camera pointer which is about to render
private: virtual void cameraPreRenderScene(
Ogre::Camera * _cam) override;

/// \brief Callback when a camera is finisned being rendered
/// \brief Callback when a camera is finished being rendered
/// \param[in] _cam Ogre camera pointer which has already render
private: virtual void cameraPostRenderScene(
Ogre::Camera * _cam) override;
Expand Down Expand Up @@ -279,38 +279,32 @@ void Ogre2ThermalCameraMaterialSwitcher::cameraPreRenderScene(
Variant tempAny = ogreVisual->UserData(tempKey);
if (tempAny.index() != 0 && !std::holds_alternative<std::string>(tempAny))
{
float temp = -1.0;
float temp = -1.0f;
bool foundTemp = true;
try
if (std::holds_alternative<float>(tempAny))
{
temp = std::get<float>(tempAny);
}
catch(...)
else if (std::holds_alternative<double>(tempAny))
{
try
{
temp = static_cast<float>(std::get<double>(tempAny));
}
catch(...)
{
try
{
temp = static_cast<float>(std::get<int>(tempAny));
}
catch(std::bad_variant_access &e)
{
gzerr << "Error casting user data: " << e.what() << "\n";
temp = -1.0;
foundTemp = false;
}
}
temp = static_cast<float>(std::get<double>(tempAny));
}
else if (std::holds_alternative<int>(tempAny))
{
temp = static_cast<float>(std::get<int>(tempAny));
}
else
{
gzerr << "Error casting user data: temperature\n";
temp = -1.0f;
foundTemp = false;
}

// if a non-positive temperature was given, clamp it to 0
if (foundTemp && temp < 0.0)
{
temp = 0.0;
gzwarn << "Unable to set negatve temperature for: "
gzwarn << "Unable to set negative temperature for: "
<< ogreVisual->Name() << ". Value cannot be lower than absolute "
<< "zero. Clamping temperature to 0 degrees Kelvin."
<< std::endl;
Expand Down Expand Up @@ -560,36 +554,30 @@ void Ogre2ThermalCameraMaterialSwitcher::cameraPreRenderScene(
{
float temp = -1.0;
bool foundTemp = true;
try
if (std::holds_alternative<float>(tempAny))
{
temp = std::get<float>(tempAny);
}
catch (...)
else if (std::holds_alternative<double>(tempAny))
{
try
{
temp = static_cast<float>(std::get<double>(tempAny));
}
catch (...)
{
try
{
temp = static_cast<float>(std::get<int>(tempAny));
}
catch (std::bad_variant_access &e)
{
gzerr << "Error casting user data: " << e.what() << "\n";
temp = -1.0;
foundTemp = false;
}
}
temp = static_cast<float>(std::get<double>(tempAny));
}
else if (std::holds_alternative<int>(tempAny))
{
temp = static_cast<float>(std::get<int>(tempAny));
}
else
{
gzerr << "Error casting user data: temperature\n";
temp = -1.0f;
foundTemp = false;
}

// if a non-positive temperature was given, clamp it to 0
if (foundTemp && temp < 0.0)
{
temp = 0.0;
gzwarn << "Unable to set negatve temperature for: " << visual->Name()
gzwarn << "Unable to set negative temperature for: " << visual->Name()
<< ". Value cannot be lower than absolute "
<< "zero. Clamping temperature to 0 degrees Kelvin."
<< std::endl;
Expand Down Expand Up @@ -1068,7 +1056,7 @@ void Ogre2ThermalCamera::CreateThermalTexture()
this->dataPtr->ogreThermalTexture->scheduleTransitionTo(
Ogre::GpuResidency::Resident);

// create compositor worksspace
// create compositor workspace
this->dataPtr->ogreCompositorWorkspace =
ogreCompMgr->addWorkspace(
this->scene->OgreSceneManager(),
Expand All @@ -1078,7 +1066,7 @@ void Ogre2ThermalCamera::CreateThermalTexture()
false);

// add thermal material switcher to render target listener
// so we can switch to use heat material when the camera is being udpated
// so we can switch to use heat material when the camera is being updated
Ogre::CompositorNode *node =
this->dataPtr->ogreCompositorWorkspace->getNodeSequence()[0];
auto channels = node->getLocalTextures();
Expand Down

0 comments on commit 6d02c15

Please sign in to comment.