Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flipping a quaternion from right to left handed coordinates

Tags:

math

3d

I need to flip a quaternion from right:
x = left to right
y = front to back
z = top to bottom

to left handed coordinates where:
x = left to right
y = top to bottom
z = front to back

How would I go about doing this?

like image 765
cmann Avatar asked Aug 13 '09 22:08

cmann


People also ask

How do you rotate a quaternion?

For rotation quaternions, the inverse equals the conjugate. So for rotation quaternions, q−1 = q* = ( q0, −q1, −q2, −q3 ). Inverting or conjugating a rotation quaternion has the effect of reversing the axis of rotation, which modifies it to rotate in the opposite direction from the original.

What is X Y z and W in quaternion?

A quaternion can represent a 3D rotation and is defined by 4 real numbers. x, y and z represent a vector. w is a scalar that stores the rotation around the vector.

What is the inverse of unit quaternion?

In the case q is a unit quaternion, the inverse is its conjugate q∗.

How do you turn a quaternion into a rotation matrix?

Combine the quaternion rotations into a single representation, then apply the quaternion rotation to arbitrarily initialized Cartesian points. Combine the rotation matrices into a single representation, then apply the rotation matrix to the same initial Cartesian points.


2 Answers

Once you do that, you no longer have a quaternion, i.e. the usual rules for multiplying them won't work. The identity i^2 = j^2 = k^2 = ijk = -1 will no longer hold if you swap j and k (y and z in your right handed system).

like image 147
Jim Lewis Avatar answered Oct 27 '22 16:10

Jim Lewis


I don't think any of these answers is correct.

Andres is correct that quaternions don't have handedness (*). Handedness (or what I'll call "axis conventions") is a property that humans apply; it's how we map our concepts of "forward, right, up" to the X, Y, Z axes.

These things are true:

  • Pure-rotation matrices (orthogonal, determinant 1, etc) can be converted to a unit quaternion and back, recovering the original matrix.
  • Matrices that are not pure rotations (ones that have determinant -1, for example matrices that flip a single axis) are also called "improper rotations", and cannot be converted to a unit quaternion and back. Your mat_to_quat() routine may not blow up, but it won't give you the right answer (in the sense that quat_to_mat(mat_to_quat(M)) == M).
  • A change-of-basis that swaps handedness has determinant -1. It is an improper rotation: equivalent to a rotation (maybe identity) composed with a mirroring about the origin.

To change the basis of a quaternion, say from ROS (right-handed) to Unity (left-handed), we can use the method of .

mat3x3 ros_to_unity = /* construct this by hand */;
mat3x3 unity_to_ros = ros_to_unity.inverse();
quat q_ros = ...;
mat3x3 m_unity = ros_to_unity * mat3x3(q_ros) * unity_to_ros ;
quat q_unity = mat_to_quat(m_unity);

Lines 1-4 are simply the method of https://stackoverflow.com/a/39519079/194921: "How do you perform a change-of-basis on a matrix?"

Line 5 is interesting. We know mat_to_quat() only works on pure-rotation matrices. How do we know that m_unity is a pure rotation? It's certainly conceivable that it's not, because unity_to_ros and ros_to_unity both have determinant -1 (as a result of the handedness switch).

The hand-wavy answer is that the handedness is switching twice, so the result has no handedness switch. The deeper answer has to do with the fact that similarity transformations preserve certain aspects of the operator, but I don't have enough math to make the proof.

Note that this will give you a correct result, but you can probably do it more quickly if unity_to_ros is a simple matrix (say, with just an axis swap). But you should probably derive that faster method by expanding the math done here.

(*) Actually, there is the distinction between Hamilton and JPL quaternions; but everybody uses Hamilton so there's no need to muddy the waters with that.

like image 35
Paul Du Bois Avatar answered Oct 27 '22 17:10

Paul Du Bois