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

Change aspect to AspectRatio() #635

Merged
merged 12 commits into from
Jun 9, 2022
Merged

Conversation

AzulRadio
Copy link
Contributor

@AzulRadio AzulRadio commented May 26, 2022

🦟 Bug fix

Fixes ignition crash bug introduced in gz-sim#1499

Summary

gz-sim#1499 adds a feature to change the FOV of the user camera at runtime. But if we scale the window after changing the FOV, Gazebo has a chance to crash. (Always crash on Ogre, sometimes crash on Ogre2)

Changing aspect to AspectRatio() in OgreCamera::SetHFOV() solves the bug.

Also add comment to warn users not to use aspect in BaseCamera but to use OrgeCamera::AspectRatio() instead.

Why this bug happened:

Resizing the window needs the correct aspect ratio value. There are 2 aspect ratio values available: one in Ogre library (AspectRatio()), and the other is in BaseCamera (this->aspect).

When we change the FOV and resize the window, the aspect ratio and fov value in the Ogre library will both change but aspect in the BaseCamera only update its fov value but not aspect ratio. However, in Ogre2Camera::SetHFOV(), it is still using the deprecated aspect ratio value from the BaseCamera.

If you check with gdb, the value read by AspectRatio() (directly from Ogre) and aspect from BaseCamera no longer agree with each other after changing FOV and resizing the window. That is why there is a crash.

Checklist

  • Signed all commits for DCO
  • Added tests
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.

@github-actions github-actions bot added the 🏯 fortress Ignition Fortress label May 26, 2022
Copy link
Contributor

@ahcorde ahcorde left a comment

Choose a reason for hiding this comment

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

Copy link
Contributor

@jennuine jennuine left a comment

Choose a reason for hiding this comment

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

Other than linters this LGTM, will need @iche033 's stamp of approval

include/ignition/rendering/base/BaseCamera.hh Outdated Show resolved Hide resolved
@iche033
Copy link
Contributor

iche033 commented May 26, 2022

Ideally we also update the value of this->aspect after setting FOV and/or image size, potentially in BaseCamera's SetImageWidth / SetImageHeight functions so that it's not outdated. Can you also add a quick test in Camera_TEST.cc?

@codecov
Copy link

codecov bot commented May 26, 2022

Codecov Report

Merging #635 (1c581e9) into ign-rendering6 (a3dc1ae) will decrease coverage by 25.47%.
The diff coverage is 87.50%.

❗ Current head 1c581e9 differs from pull request most recent head 545a534. Consider uploading reports for the commit 545a534 to get more accurate results

@@                 Coverage Diff                 @@
##           ign-rendering6     #635       +/-   ##
===================================================
- Coverage           80.00%   54.52%   -25.48%     
===================================================
  Files                   1      198      +197     
  Lines                  15    20270    +20255     
===================================================
+ Hits                   12    11052    +11040     
- Misses                  3     9218     +9215     
Impacted Files Coverage Δ
ogre/src/OgreCamera.cc 0.00% <0.00%> (ø)
include/ignition/rendering/base/BaseCamera.hh 66.66% <100.00%> (ø)
ogre2/src/Ogre2Camera.cc 88.88% <100.00%> (ø)
ogre2/src/Ogre2DepthCamera.cc 88.37% <100.00%> (ø)
ogre2/src/Ogre2ThermalCamera.cc 88.60% <100.00%> (ø)
test_config.h
ogre2/src/Ogre2RenderTarget.cc 86.63% <0.00%> (ø)
ogre2/src/Ogre2LidarVisual.cc 80.95% <0.00%> (ø)
ogre2/src/Ogre2Light.cc 98.30% <0.00%> (ø)
ogre/src/OgreRenderPass.cc 0.00% <0.00%> (ø)
... and 194 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a3dc1ae...545a534. Read the comment docs.

@AzulRadio
Copy link
Contributor Author

@iche033 Just to clarify, do you mean we should update this->aspect in BaseCamera::SetImageWidth, BaseCamera::SetImageHeight, and BaseCamera::SetHFOV, and add a test to check if aspect agrees with AspectRatio?

@chapulina chapulina added the OOBE 📦✨ Out-of-box experience label May 27, 2022
@iche033
Copy link
Contributor

iche033 commented May 31, 2022

BaseCamera::SetImageWidth, BaseCamera::SetImageHeight

yes I would expect the aspect ratio to change when user calls these 2 functions. I think the HFOV should not affect aspect ratio though but we should double check that.

@AzulRadio
Copy link
Contributor Author

@iche033 Aspect ratio change added in fbbbc4b. I'm not sure how to add the test to Camera_TEST because aspect is a protected variable (not accessible in Camera_TEST). I verified with GDB and it's now changing when resizing the window.

Copy link
Contributor

@jennuine jennuine left a comment

Choose a reason for hiding this comment

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

For a Camera_TEST you can check that after SetImageWidth AspectRatio() agrees.

e.g.,

double width = 100
camera.SetImageWidth(width)
double aspectRatio = width / camera.ImageHeight()
EXPECT_NEAR(aspectRatio, camera.AspectRatio(), 1e-6)

Similarly for SetImageHeight

ogre2/src/Ogre2Camera.cc Show resolved Hide resolved
Comment on lines 181 to 184
double width = 100
camera.SetImageWidth(width)
double aspectRatio = width / camera.ImageHeight()
EXPECT_NEAR(aspectRatio, camera.AspectRatio(), 1e-6)
Copy link
Contributor

@jennuine jennuine Jun 6, 2022

Choose a reason for hiding this comment

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

This is missing ; at the end of each line and camera is a pointer. This test fails though, you will need to try to investigate why and how to correct this

Since you are using ogre on your system, temporarily update ogre2 to ogre here:

static const std::vector<const char *> kRenderEngineTestValues{"ogre2", "optix"};

To compile the test, run colcon build --cmake-args -DBUILD_TESTING=ON --merge-install --packages-select ignition-rendering6 in the root of your workspace. To run the test you can do ./build/ignition-rendering6/bin/UNIT_Camera_Test

Copy link
Contributor

Choose a reason for hiding this comment

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

You should also add a similar test with SetImageHeight

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in 8d4e542

Copy link
Contributor

@jennuine jennuine left a comment

Choose a reason for hiding this comment

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

Nice work! Left some minor comments

@@ -286,6 +286,7 @@ namespace ignition
void BaseCamera<T>::SetImageWidth(const unsigned int _width)
{
this->RenderTarget()->SetWidth(_width);
this->SetAspectRatio(1.0 * _width / this->ImageHeight());
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume you are multiplying by 1.0 to cast the result to a double. It's better to use static_cast<double> to be explicit with the cast making the purpose clear. Same for SetImageHeight

Suggested change
this->SetAspectRatio(1.0 * _width / this->ImageHeight());
this->SetAspectRatio(
static_cast<double>(_width) / static_cast<double>(this->ImageHeight()));

Comment on lines 178 to 184
camera->SetImageHeight(80u);
EXPECT_EQ(80u, camera->ImageHeight());

double height = 80;
camera->SetImageHeight(height);
double aspectRatio = camera->ImageWidth() / height;
EXPECT_NEAR(aspectRatio, camera->AspectRatio(), 1e-6);
Copy link
Contributor

Choose a reason for hiding this comment

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

Since your new tests are almost the same as the previous test, lets remove the new test and continue on the previous one. E.g.,

Suggested change
camera->SetImageHeight(80u);
EXPECT_EQ(80u, camera->ImageHeight());
double height = 80;
camera->SetImageHeight(height);
double aspectRatio = camera->ImageWidth() / height;
EXPECT_NEAR(aspectRatio, camera->AspectRatio(), 1e-6);
unsigned int height = 80;
camera->SetImageHeight(height);
EXPECT_EQ(height, camera->ImageHeight());
double aspectRatio =
static_cast<double>(camera->ImageWidth()) / static_cast<double>(height));
EXPECT_NEAR(aspectRatio, camera->AspectRatio(), 1e-6);

The same should be done for width.

Also, go ahead and update this expectation below to use the new variables:

EXPECT_EQ(100u*80u*3u, camera->ImageMemorySize());

e.g., EXPECT_EQ(width*height*3u, ...)

Copy link
Contributor

@jennuine jennuine left a comment

Choose a reason for hiding this comment

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

LGTM, @iche033 do you mind to take another look?

Copy link
Contributor

@iche033 iche033 left a comment

Choose a reason for hiding this comment

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

looks good to me.

@AzulRadio AzulRadio merged commit da11d8e into ign-rendering6 Jun 9, 2022
@AzulRadio AzulRadio deleted the azulradio/HFOV_fix branch June 9, 2022 18:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏯 fortress Ignition Fortress OOBE 📦✨ Out-of-box experience
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants