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

SphericalCoordinates: Added SPHERICAL_DEG and SPHERICAL_RAD, deprecated SPHERICAL #597

Closed
wants to merge 1 commit into from

Conversation

peci1
Copy link
Contributor

@peci1 peci1 commented May 26, 2024

🦟 Bug fix

Fixes #235 (comment)

Summary

This change is made to unify clarify the units that are inputs/outputs to the coordinate transforms.

This PR deprecates enum value SPHERICAL used downstream at least in https://github.com/gazebosim/gz-sim/blob/40aaddc7eb4ca12a03b64e18ffc101db9f9c24ec/src/Util.cc#L694 .

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.

@@ -281,7 +281,7 @@ namespace gz
public: void UpdateTransformationMatrix();

/// \brief Convert between positions in SPHERICAL/ECEF/LOCAL/GLOBAL frame
/// Spherical coordinates use radians, while the other frames use meters.
/// Spherical coordinates use degrees, while the other frames use meters.
Copy link
Member

Choose a reason for hiding this comment

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

I'm concerned about the behavior change here if people don't read the release notes, trying to think if there are better alternatives

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree this would be impractical. This PR is open for discussion. One obvious solution would be to rename the function, e.g. to TransformPosition (and, for consistency, also rename VelocityTransform). That would mean much more "unrelated" downstream changes, but at least no code would silently fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe use a new CoordinateType for SPHERICAL_DEGREES? We can deprecate SPHERICAL if need be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renaming the enum constant and deprecated the old one sounds like a good way to go. At least, it wouldn't affect downstream uses which only use metric coords. And it would issue a deprecation warning on all uses of spherical conversions. I'll try to sketch an update of this PR with this rename.

Thinking about it a bit more, the problem this PR resolves is caused by the fact that gz math is missing a Coordinate3 class, which could hold either metric or spherical coords, but different from Vector3, it would know which ones are stored. And it could have accessors that check the type of the stored values. Would something like that be a welcome PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

It does sound like a great idea. I'd be happy to review it. But given the tight timeline for feature and code freeze I think we will have to merge it after September and backport.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've drafted #616 adding the CoordinateVector3 class. I guess this falls behind the feature freeze, so it is waiting for Gazebo 9.

The only thing that I don't like is that if we choose to go this way, it would again start to make sense to have SPHERICAL constant, while SPHERICAL_DEG and SPHERICAL_RAD would lose sense. I don't really know what to do with this.

Copy link
Contributor

@arjo129 arjo129 Aug 14, 2024

Choose a reason for hiding this comment

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

Hmmm one alternative is to merge #616 into this PR and deprecate these functions altogether. In my mind that would be the cleanest way from the end user perspective.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't say I fully undestand your proposal. I'll try to summarize and please tell if I understood you correctly.

There's also #596 which deprecates GlobalFromLocalVelocity etc. and in its current version, it replaces it with GlobalVelocityFromLocal which does the local frame computations correctly.

So if we took PRs 596, 597 and 616 together, the result could be:

  • keep SPHERICAL constant and do not deprecate it
  • do not add SPHERICAL_DEG and SPHERICAL_RAD constants (as is currently done in this PR)
  • deprecate LOCAL and LOCAL2 constants and add LOCAL_TANGENT (as done in 596)
    • alternatively, the LOCAL constant could be kept (and LOCAL_TANGENT not added and LOCAL2 deprecated) with a dual meaning for one tick-tock cycle - in (Position|Velocity)Transform(Vector3d), it would lead to the old wrong behavior, while in (Position|Velocity)Transform(CoordinateVector3) it would yield the correct results. There would still be a chance that when migrating the code, users would miss the note that the computation differs, but they would anyways need to edit the line of code using the function - and I guess if they're already editing it, they should have an idea how to do the migration...
  • deprecate GlobalFromLocalVelocity(Vector3d) and friends (as done in 596)
  • add GlobalFromLocalVelocity(CoordinateVector3) etc. (as done in 616)
  • do not add GlobalVelocityFromLocal(Vector3) from 596
  • deprecate PositionTransform(Vector3d) and VelocityTransform(Vector3d) and add their CoordinateVector3 counterparts (as done in 616)

This would solve all the problems 596 and 597 tried to solve.

It would also have quite some effect on downstream code, effectively requiring almost all users of SphericalCoordinates.hh to do some changes. But the result would be that the downstream code will be working correctly and it will be unambiguous regarding units. There might probably be a little performance penalty (CoordinateVector3 uses PIMPL with unique_ptr and std::variant). But I don't think this class would ever be used in a performance-critical path...

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 we are on the same page.

If we go with #616 then I don't see any value in SPHERICAL_DEG and SPHERICAL_RAD. The end users will have to rewrite their code twice. Yes, this change is more drastic but it will provide a much better UX. It probably makes sense to include #616 with ionic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great! I'll give it a go later today and add some tests.

@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 @peci1, 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.

@peci1
Copy link
Contributor Author

peci1 commented Aug 6, 2024

I think it would be beneficial to have this in Ionic. I'll have a closer look at it next week.

…ed SPHERICAL.

This change is made to clarify which units are inputs/outputs to the coordinate transforms.

Signed-off-by: Martin Pecka <[email protected]>
@peci1 peci1 force-pushed the position-transform-degrees branch from ddc58d4 to dffb6ab Compare August 13, 2024 11:09
@peci1 peci1 changed the title Switched the input/output of PositionTransform for SPHERICAL frame from radians to degrees. SphericalCoordinates: Added SPHERICAL_DEG and SPHERICAL_RAD, deprecated SPHERICAL Aug 13, 2024
@peci1
Copy link
Contributor Author

peci1 commented Aug 13, 2024

I took a little bit different approach in the end. I've kept SPHERICAL to represent spherical coordinates in radians, but deprecated it in favor of the more verbose SPHERICAL_RAD. And I've added SPHERICAL_DEG to allow passing in/out spherical coords in degrees.

Now I think every piece of code that uses PositionTransform should be verbose enough to make it clear what angular units are in the in/out vectors.

@peci1
Copy link
Contributor Author

peci1 commented Aug 16, 2024

Continued as a part of #616

@peci1 peci1 closed this Aug 16, 2024
arjo129 pushed a commit that referenced this pull request Aug 20, 2024
As discussed in #597 (comment) , it seems that gz-math would benefit from having a CoordinateVector3 class that explicitly holds either spherical or metric coordinates.

This PR merges the fixes from #596 and #597 - it deprecates LOCAL2 frame and provides an unambiguous interface regarding angular units.
---------

Signed-off-by: Martin Pecka <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta Targeting beta release of upcoming collection 🏛️ ionic Gazebo Ionic
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants