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

Color [] changed to call by reference #579

Open
wants to merge 8 commits into
base: gz-math7
Choose a base branch
from

Conversation

Avisheet
Copy link

@Avisheet Avisheet commented Feb 21, 2024

Summary

The issue you're describing is related to the operator[] overload for the Color class. The problem is that the operator is declared to return a float instead of a reference to a float. As a result, when you use the operator to access a component of the color and attempt to mutate it, the mutation doesn't apply to the actual color instance.

To fix this issue, you should modify the declaration of the operator[] to return a reference to a float. Here's the corrected declaration:

public: float& operator[](const unsigned int _index);

This modification allows you to use the operator to both access and mutate the individual components of the Color class.

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 (optional)

@github-actions github-actions bot added 🌱 garden Ignition Garden 🎵 harmonic Gazebo Harmonic labels Feb 21, 2024
@azeey
Copy link
Contributor

azeey commented Feb 22, 2024

Hello @Avisheet! Thanks for the contribution. The change you made in the header looks correct, but it requires a corresponding change in Color.cc. Would you be able to make that change? Also, could you add a unit test?

@scpeters
Copy link
Member

we would probably need to retarget to main as well since this breaks API, unless we add an operator overload instead of changing the current one

@Avisheet
Copy link
Author

Hello @azeey .
Thank you for your feedback. I've made the necessary changes in Color.cc to align with the modification in Color.hh. Additionally, I have added a corresponding unit test to ensure the functionality is working as expected.

Please review the changes and let me know if everything looks good to you. If you have any further suggestions or if there's anything else you'd like me to address, please feel free to let me know.

@Avisheet
Copy link
Author

hello @scpeters , Can you elaborate what you are telling as i am unable to understand it properly

@Avisheet
Copy link
Author

@azeey I have done some more changes in the Vector3_TEST.cc file to ensure that we get an expected result as "Operators can be used to [strictly order] vectors."Do check them once and tell me the modifications I need to do if any.

@Avisheet Avisheet force-pushed the gz-math7 branch 2 times, most recently from 608965d to 6f75941 Compare February 22, 2024 15:14
@azeey
Copy link
Contributor

azeey commented Feb 22, 2024

@Avisheet I've added a checklist to your PR description; it should have been prepopulated when you created the PR unless you removed it. Please make sure to complete all relevant items on the checklist.

@Avisheet
Copy link
Author

Avisheet commented Feb 22, 2024

Hey @azeey . The existing behavior is that we return NAN on unknown indices. But if we want to support assignment then it cannot return any such const rvalue . so, we probably have to break that existing functionality . How should we handle that ?
As a result of this behavior we are getting failure in 2 test cases.

@Avisheet
Copy link
Author

Avisheet commented Mar 1, 2024

@azeey can you review it and provide me a feedback . So that I can work more in it according to that.

Signed-off-by: Avisheet <[email protected]>
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this file.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this file.


/// \brief Array index operator, const version
/// \param[in] _index Color component index(0=red, 1=green, 2=blue,
/// 3=alpha)
/// \return r, g, b, or a when _index is 0, 1, 2 or 3. A NAN_F value is
/// returned if the _index is invalid
public: float operator[](const unsigned int _index) const;
public: const float& operator[](const unsigned int _index) const;
Copy link
Contributor

Choose a reason for hiding this comment

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

It's better to return a POD type by value, so please revert this change.

src/Color.cc Outdated Show resolved Hide resolved
}

//////////////////////////////////////////////////
float Color::operator[](const unsigned int _index) const
const float& Color::operator[](const unsigned int _index) const
Copy link
Contributor

Choose a reason for hiding this comment

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

Please revert this per my comment in the header.

@@ -176,7 +176,7 @@ def test_color(self):
self.assertAlmostEqual(0.5, clr[1])
self.assertAlmostEqual(1.0, clr[2])
self.assertAlmostEqual(1.0, clr[3])
self.assertTrue(math.isnan(clr[4]))
# self.assertTrue(math.isnan(clr[4]))
Copy link
Contributor

Choose a reason for hiding this comment

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

revert

Comment on lines 451 to 463
// // this tests _that_ the expected exception is thrown
EXPECT_THROW({
try
{
clr[4] = 0.1f;
}
catch( const std::runtime_error& e )
{
// and this tests that it has the correct message
EXPECT_STREQ( "Index Error: Color index out of range", e.what() );
throw;
}
}, std::runtime_error);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be sufficient to check that clr[4] is a NAN here.

Signed-off-by: Avisheet <[email protected]>
@azeey azeey added the beta Targeting beta release of upcoming collection label Jul 29, 2024
@arjo129
Copy link
Contributor

arjo129 commented Aug 6, 2024

Hi @Avisheet, was wondering what the status on this was. Do we intend shipping this change with ionic (code freeze 26/8)? If we can't come to a consensus by then I will remove the beta label.

Please address @azeey's feedback on PODs.

@@ -16,6 +16,7 @@
*/
#include <cmath>
#include <algorithm>
#include<iostream>
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to include this ?

@scpeters scpeters changed the title Changed to call by reference Color [] changed to call by reference Aug 14, 2024
@arjo129 arjo129 removed the beta Targeting beta release of upcoming collection label Aug 19, 2024
@arjo129
Copy link
Contributor

arjo129 commented Aug 19, 2024

Hi @Avisheet as we haven't heard back from you, I'm removing the beta label. Both @adityapande-1995 and @azeey 's remarks need addressing.

@Avisheet Avisheet closed this Aug 19, 2024
@arjo129
Copy link
Contributor

arjo129 commented Aug 19, 2024

Hey @Avisheet you don't have to close the PR if you'd like we can merge this in after the code freeze if you can't address it immediately.

@Avisheet
Copy link
Author

Hey @arjo129 , sorry for the inconvenience, but as I have some of my major projects going on, I won't be able to contribute to it for 1 more month.

@arjo129
Copy link
Contributor

arjo129 commented Aug 19, 2024

No worries. I can reopen this we will merge it after your commitments! Its just that we will be putting it in the next gazebo release. Let me know if that works for you or if you don;t want to further pursue this that is also fine.

@Avisheet
Copy link
Author

@arjo129 Yep, it's fine to release it with another gazebo release. I will be working on it for sure, but it's just I was a bit stuck with my major project.

@arjo129 arjo129 reopened this Aug 19, 2024
@arjo129 arjo129 removed 🌱 garden Ignition Garden 🎵 harmonic Gazebo Harmonic labels Aug 19, 2024
@Avisheet
Copy link
Author

Hey, I had a doubt that how can I check and run all the test cases on my own device, before giving the PR, so that I can know where the error is, after doing some changes in code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

5 participants