2

I'm learning about more quaternion conversions and I have a question regarding quaternion to spherical rotation conversion code at the following section of this website:

http://web.archive.org/web/20041029003853/http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q59

What are angle and sin_angle in this code supposed to be? Angle is never used so I was guessing it's the rotation angle. Is sin_angle supposed to be sin_a?

If this was a function returning an array of float with the spherical rotation data, would [angle, latitude, longitude] be an appropriate representation of the converted quaternion?

cos_a  = W;
sin_a  = sqrt( 1.0 - cos_a * cos_a );
angle  = acos( cos_a ) * 2;
if ( fabs( sin_angle ) < 0.0005 ) sin_a = 1;
tx = X / sin_a;
ty = Y / sin_a;
tz = Z / sin_a;
latitude = -asin( ty );
if ( tx * tx + tz * tz < 0.0005 )
   longitude   = 0;
else
   longitude  = atan2( tx, tz );
if ( longitude < 0 )
   longitude += 360.0;
karamazovbros
  • 950
  • 1
  • 11
  • 40

1 Answers1

2

A quaternion is four numbers [X,Y,Z,W] that encode a rotation direction and an angle. The code extracts this information and converts the rotation axis into a latitude and longitude.

  • Quaternion construction from rotation axis [tx,ty,tz] and angle theta is
    • X = tx*SIN(theta/2)
    • Y = ty*SIN(theta/2)
    • Z = tz*SIN(theta/2)
    • W = COS(theta/2)

This code does the reverse, since angle = 2*ACOS(W) = 2*(theta/2) = theta. So the variable angle stores the rotation angle.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133
  • Is there a proof for this, because I haven't seen a conversion between these 2 forms before. Is there an example of a 3d library needing spherical coordinates from a quaternion? – karamazovbros Jul 15 '19 at 20:38
  • What you are asking is how to convert the cartesian coordinates of the direction vector `[tx,ty,tz]` to spherical coordinates. There are lots of references online for that sort of thing. – John Alexiou Jul 15 '19 at 23:37
  • Hmm, and which direction vector is this? I've seen quaternions converted to up/right/front vectors. – karamazovbros Jul 16 '19 at 00:29
  • As I said, the quaternion represents a _rotation axis_ and an angle. The direction vector is that of the rotation axis. Don't get that confused with the direction vectors of a rotation matrix. – John Alexiou Jul 16 '19 at 11:38
  • Thank you. I was looking at the axis angle to quat formula and the steps make more sense now. For longitude, should this formula be adding 360 or 2*PI? I want the initial values in radians. – karamazovbros Jul 16 '19 at 17:26
  • @karamazovbros - The angle that comes out of `2*ACOS(W)` is in radians. – John Alexiou Jul 17 '19 at 14:32
  • Longitude += 360? – karamazovbros Jul 17 '19 at 15:07
  • @karamazovbros - if you want angle that spans 360° you have to use the `ATAN2(dy,dx)` function, with `dx=W` and `dy=SQRT(X^2+Y^2+Z^2)`. Otherwise, you need to flip the sense of the rotation axis according to the sign of the `W` component. – John Alexiou Jul 17 '19 at 16:18
  • I guess what I'm asking is about the last line of code, longitude += 360, which seems odd. If longitude is in radians prior to that then wouldn't it add 2*PI? I just don't understand that last line. – karamazovbros Jul 17 '19 at 16:59
  • Maybe `j3d` returns degrees from `atan()`. I don't know. In 99% of math libraries angles are radians, but there are exceptions. I was programming in `Pro/Engineer` and failing because their trig functions expected angles in degrees. Go figure. – John Alexiou Jul 17 '19 at 17:28
  • Hmm, that could be, it was written a while ago. So if it atan returns radians should longitude add 2*PI, and if it returns degrees it's correct as 360? – karamazovbros Jul 17 '19 at 17:30
  • @karamazovbros - so `atan2()` returns values between `-180 .. +180` and having a negative longitude isn't accepted. You fix that by adding `360` to the negative values. This is the logic here. – John Alexiou Jul 17 '19 at 17:34