-
Notifications
You must be signed in to change notification settings - Fork 69
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
base: gz-math7
Are you sure you want to change the base?
Conversation
Hello @Avisheet! Thanks for the contribution. The change you made in the header looks correct, but it requires a corresponding change in |
we would probably need to retarget to |
Hello @azeey . 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. |
hello @scpeters , Can you elaborate what you are telling as i am unable to understand it properly |
@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. |
608965d
to
6f75941
Compare
@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. |
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 ? |
Signed-off-by: Avisheet <[email protected]>
Signed-off-by: Avisheet <[email protected]>
Signed-off-by: Avisheet <[email protected]>
Signed-off-by: Avisheet <[email protected]>
Signed-off-by: Avisheet <[email protected]>
Signed-off-by: Avisheet <[email protected]>
@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]>
.vscode/c_cpp_properties.json
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this file.
.vscode/settings.json
Outdated
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
} | ||
|
||
////////////////////////////////////////////////// | ||
float Color::operator[](const unsigned int _index) const | ||
const float& Color::operator[](const unsigned int _index) const |
There was a problem hiding this comment.
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])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revert
src/Color_TEST.cc
Outdated
// // 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); |
There was a problem hiding this comment.
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]>
@@ -16,6 +16,7 @@ | |||
*/ | |||
#include <cmath> | |||
#include <algorithm> | |||
#include<iostream> |
There was a problem hiding this comment.
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 ?
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. |
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. |
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. |
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. |
@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. |
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. |
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:
This modification allows you to use the operator to both access and mutate the individual components of the Color class.
Checklist
codecheck
passed (See contributing)