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

Added Jacobians for Rot3::xyz and related conversions to euler angles #520

Merged
merged 2 commits into from
Sep 17, 2020

Conversation

Eskilade
Copy link
Contributor

Hi,

This PR adds the remaining Jacobians for conversions between Rot3 and euler angles (first PR here ). Some of the expressions below are a bit hairy, so please feel free to suggest a different way of doing it :)

@jlblancoc
Copy link
Member

jlblancoc commented Sep 12, 2020

Awesome!

@jlblancoc
Copy link
Member

Perhaps it would be convenient to add unit tests covering ypr near the extreme +-pi and +-pi/2 values?

Copy link
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

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

Great! I'm OK to approve as is, but I think the computation is quite heavy and could probably be improved quite a bit. Still, we did not have this before, and it might not make a huge difference in an application.

Vector3 q = xyz();
Vector3 Rot3::ypr(OptionalJacobian<3, 3> H) const {
Vector3 q = xyz(H);
if (H) H->row(0).swap(H->row(2));
Copy link
Member

Choose a reason for hiding this comment

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

nice !


Matrix39 qHm;
boost::tie(I, q) = RQ(m, qHm);
*H = qHm * mH;
Copy link
Member

Choose a reason for hiding this comment

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

So, this is of course perfectly fine. However, if you care about performance, this is (a) 81 mults, and (b) we could use of the fact that mH is super-sparse. In particular, SO3.cpp says

    mH << R * P3.block<3, 3>(0, 0), R * P3.block<3, 3>(3, 0),
        R * P3.block<3, 3>(6, 0);

Maybe add as a TODO. In fact I suspect the 3*3 matrix of derivatives might yield not too complicated symbolic expressions, and we do a lot of compute here (including multiplying a lot of things with zero). Might be worth some mathematica exploration.

@Eskilade
Copy link
Contributor Author

Eskilade commented Sep 13, 2020

Perhaps it would be convenient to add unit tests covering ypr near the extreme +-pi and +-pi/2 values?

Ok, I'll add more tests. However, the derivative breaks down for pitch values +- pi / 2. How do you prefer to handle that? Do you use nans?

@jlblancoc
Copy link
Member

However, the derivative breaks down for pitch values +- pi / 2. How do you prefer to handle that? Do you use nans?

That was my point, bringing out that discussion :-)
Let's see what @dellaert thinks, but I feel leaned to say that the unit test must pass for values near the edge, and raise an exception if exactly (within an epsilon) of the edges if the derivative (not even the numerical one) is really not well-defined there.

@dellaert
Copy link
Member

However, the derivative breaks down for pitch values +- pi / 2. How do you prefer to handle that? Do you use nans?

That was my point, bringing out that discussion :-)
Let's see what @dellaert thinks, but I feel leaned to say that the unit test must pass for values near the edge, and raise an exception if exactly (within an epsilon) of the edges if the derivative (not even the numerical one) is really not well-defined there.

I think an exception is a good idea. There is a singularity, for sure.

@Eskilade
Copy link
Contributor Author

Eskilade commented Sep 14, 2020

Hmm, I'm a bit unsure about which epsilon value we should use... If I set roll and yaw to zero, and if
a) is the largest entry in the difference between the numerical derivative and the calculated derivative in absolute value
b) the entry in the calculated derivative corresponding to (a)

then I get :

  • a) ~3e-08 b) ~10 for pitch = pi/2 - 0.1
  • a) ~3e-05 b) ~100 for pitch = pi/2 - 0.01
  • a) ~0.03 b) ~1e3 for pitch = pi/2 - 1e-3
  • a) ~30 b) ~1e4 for pitch = pi/2 - 1e-4
  • a) ~2e4 b) ~1e5 for pitch = pi/2 - 1e-5

What do you think? I hope the numbers are understandable.

Edit : I should add that I'm talking about the derivative of Rot3::RQ

@jlblancoc
Copy link
Member

  • a) ~3e-05 b) ~100 for pitch = pi/2 - 0.01

Thanks for the great summary and the time collecting the results!
I think "pi/2 - 0.01" would be safe enough. Don't set the unit test threshold too tight (e.g. if your error is ~3e-05 , mark the limit like 1 order of magnitude larger than that) since different CPU architectures may give slightly different numerical results for the same code and make the unit test to fail.
Thanks!

Also added more tests and a TODO to investigate a more efficient method of
getting the jacobian of Rot3::xyz
@dellaert
Copy link
Member

@Eskilade let me know when ready to merge....

@Eskilade
Copy link
Contributor Author

Yupp, ok for my part ! Btw, does this merit a mention in contributors or something similar ? :)

@dellaert
Copy link
Member

Of course, feel free to add your name, and then I'll merge :-)

@Eskilade
Copy link
Contributor Author

Where do I add it ? Like in which file :)

@varunagrawal
Copy link
Collaborator

You can add your name here: https://github.com/borglab/gtsam.org/blob/master/about.md

@Eskilade
Copy link
Contributor Author

Alright, but since that's a different repo, then you can go ahead and merge this PR :)

@dellaert dellaert merged commit c45ba00 into borglab:develop Sep 17, 2020
@lanyouzibetty
Copy link

@Eskilade Thanks for your awesome work. Can you tell me how to derive the jacobians for conversions between Rot3 and euler angles? I have not deduced it, thanks!

@Eskilade
Copy link
Contributor Author

Eskilade commented Jan 3, 2022

@lanyouzibetty, have a look here : https://github.com/Eskilade/orient/blob/master/documentation/conversion_formulas.pdf :)

@lanyouzibetty
Copy link

@Eskilade Thanks very much !

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

Successfully merging this pull request may close these issues.

5 participants