Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle non-positive object temperatures #243

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions ogre2/src/Ogre2ThermalCamera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ void Ogre2ThermalCameraMaterialSwitcher::preRenderTargetUpdate(
if (tempAny.index() != 0 && !std::holds_alternative<std::string>(tempAny))
{
float temp = -1.0;
bool foundTemp = true;
try
{
temp = std::get<float>(tempAny);
Expand All @@ -286,30 +287,36 @@ void Ogre2ThermalCameraMaterialSwitcher::preRenderTargetUpdate(
{
ignerr << "Error casting user data: " << e.what() << "\n";
temp = -1.0;
foundTemp = false;
}
}
}

// only accept positive temperature (in kelvin)
if (temp >= 0.0)
// if a non-positive temperature was given, clamp it to 0
if (foundTemp && temp < 0.0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only clamps the temp value when foundTemp is true. Is it possible for foundTemp to be false and temp to be < 0.0? If so, should you handle that case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for foundTemp to be false and temp to be < 0.0?

I don't think so. The only way foundTemp is set to true is if temperature is defined/attached to a visual, which would mean that <temperature> needs to be specified in the SDF file in the ign-gazebo context. If <temperature> is not specified, then this code should run (background object where object colors are used to define temperatures - since we're using rgb here, it shouldn't be possible to have an rgb value that is < 0): https://github.com/ignitionrobotics/ign-rendering/blob/8e0de0dd3e499c0fc0191a2cd8b794b0aab69980/ogre2/src/Ogre2ThermalCamera.cc#L387-L417

{
for (unsigned int i = 0; i < item->getNumSubItems(); ++i)
{
Ogre::SubItem *subItem = item->getSubItem(i);
temp = 0.0;
ignwarn << "Unable to set negatve temperature for: "
<< ogreVisual->Name() << ". Value cannot be lower than absolute "
<< "zero. Clamping temperature to 0 degrees Kelvin."
<< std::endl;
}
for (unsigned int i = 0; i < item->getNumSubItems(); ++i)
{
Ogre::SubItem *subItem = item->getSubItem(i);

// normalize temperature value
float color = (temp / this->resolution) / ((1 << bitDepth) - 1.0);
// normalize temperature value
float color = (temp / this->resolution) / ((1 << bitDepth) - 1.0);

// set g, b, a to 0. This will be used by shaders to determine
// if particular fragment is a heat source or not
// see media/materials/programs/thermal_camera_fs.glsl
subItem->setCustomParameter(this->customParamIdx,
Ogre::Vector4(color, 0, 0, 0.0));
Ogre::HlmsDatablock *datablock = subItem->getDatablock();
this->datablockMap[subItem] = datablock;
// set g, b, a to 0. This will be used by shaders to determine
// if particular fragment is a heat source or not
// see media/materials/programs/thermal_camera_fs.glsl
subItem->setCustomParameter(this->customParamIdx,
Ogre::Vector4(color, 0, 0, 0.0));
Ogre::HlmsDatablock *datablock = subItem->getDatablock();
this->datablockMap[subItem] = datablock;

subItem->setMaterial(this->heatSourceMaterial);
}
subItem->setMaterial(this->heatSourceMaterial);
}
}
// get heat signature and the corresponding min/max temperature values
Expand Down
29 changes: 13 additions & 16 deletions ogre2/src/media/materials/programs/thermal_camera_fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,26 @@ void main()
bool isHeatSource = (rgba.g == 0.0 && rgba.b == 0.0 && rgba.a == 0.0);
float heat = rgba.r;

if (heat > 0.0)
if (isHeatSource)
{
if (isHeatSource)
{
// heat is normalized so convert back to work in kelvin
// for 16 bit camera and 0.01 resolution:
// ((1 << bitDepth) - 1) * resolution = 655.35
temp = heat * bitMaxValue * resolution;
// heat is normalized so convert back to work in kelvin
// for 16 bit camera and 0.01 resolution:
// ((1 << bitDepth) - 1) * resolution = 655.35
temp = heat * bitMaxValue * resolution;

// set temperature variation for heat source
heatRange = heatSourceTempRange;
}
else
{
// other non-heat source objects are assigned ambient temperature
temp = ambient;
}
// set temperature variation for heat source
heatRange = heatSourceTempRange;
}
else
{
// other non-heat source objects are assigned ambient temperature
temp = ambient;
}

// add temperature variation, either as a function of color or depth
if (rgbToTemp == 1)
{
if (heat > 0.0 && !isHeatSource)
if (!isHeatSource)
{
// convert to grayscale: darker = warmer
// (https://docs.opencv.org/3.4/de/d25/imgproc_color_conversions.html)
Expand Down