Difference Between Combining Quaternion Rotations and Rotation Matrices - matrix

Is the result of combining two quaternion rotations the same as that of two matrices and then converting that into a quaternion?
I have a quaternion (q1) and rotation matrix (m2) as input for a function (unfortunately non-negotiable) and would like to rotate the initial quaternion by the matrix resulting in a new quaternion. I have tried a fair few ways of doing this and have slightly bizarre results.
If I convert q1 into a matrix (m1), calculate m2.m1 and convert the result into a quaternion I get what is a likely quaternion result. However if I convert m2 into a quaternion using the exact same function and multiply those together (in both orders, I know it's non-commutative) I get something entirely different. I would like to realise the quaternion combination so that I can eventually SLERP from the current quaternion to the result.
All functions have come from here: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm and are being implemented in c++ and mathematica to test

There is an exact correspondence between 3x3 rotation matrices and unit quarternions, up to a sign change in the quarternion (the sign is irrelevant when in comes to performing rotation on 3D vectors).
This means that given two quarternions, q1, q2, and their corresponding matrices, m1, m2, the action of the quarternions on a vector v is the same as the action of the matrices on v:
q2*(q1*v*(q1^-1))*(q2^-1) = m2*m1*v
If your program does not achieve this result with an arbitrary vector v, there is likely an error in your formula somewhere.

Related

Accurately converting IMU angular velocities into a quaternion

Given three angular velocities vx, vy, vz about the x, y and z axes, measured in radians per second, as derived from an IMU's rate gyro, how do I produce an equivalent quaternion for the entire rotation between one sample and the next, i.e. the integral of rotation over time dt between the current sample and the previous sample?
The primary issue is that these three angular velocities are measured independently of each other, and yet rotations are not commutative. This means the order in which the angular velocities are applied during the integration would affect the computed quaternion, just as converting Euler angles to a quaternion produces a different quaternion depending on the order in which the Euler rotations are applied (e.g. x, then y, then z, vs. some other order).
I think the right thing to do is to split the timestep dt into a number of shorter time period samples, e.g. say N=10, then divide each velocity by that number, giving vx' = vx/N, vy' = vy/N, vz' = vz/N, and then applying the rotations N times in round robin fashion, in largest to smallest order, calculating the actual rotation over the interval dt/N in each case, and accumulating this into the final rotation quaternion.
I see a lot of references to quaternion derivatives when related questions are asked though, and I wonder if it might be possible to convert the angular velocities (which are derivatives of Euler angles) directly to a quaternion derivative (again though probably suffering from axis ordering sensitivity), then somehow integrate the quaternion derivative to convert back to a quaternion spanning time dt.
Seems like there should be a "right" way to do this, since every IMU that uses a rate gyro has to solve this problem. Any insights into this would be greatly appreciated!
I found the answer in this excellent post by Ashwin Narayan.
Update (1): the rowan library implements the necessary quaternion exponentiation in Python.
Update (2): User harold pointed to this answer, which shows the same quaternion exponentiation in C++ code, which is more legible than the NumPy code in rowan.

How to calculate an angle from a rotation matrix

I have a given 3x3 rotation matrix and I want to calculate the rotation angle around z axis. How do I get there?
For example, in this case below, how did they calculated the "-30deg rotation around the x axis"? Or how did they get to the "-74deg" value around that axis?
This is my original matrix:
Thank you!
It is simple if the rotation matrix is just a rotation matrix and there is no scaling. Here is a site that explains in more pretty terms then I am willing to diagram here. Basically the rotation matrix is composed of sinf(x) and cosf(x) of euler angles (well you can think of it like that at least). You can therefore use values within it to back calculate the euler angles.
http://nghiaho.com/?page_id=846
If you have scaling involved you will need to normalize each row of the matrix first. Then apply the above method.

How to get angle rotated of a 3D object from its transform martrix

I have a 3d object which is free to rotate along x,y and z axis and it is then saved as a transform matrix. In a case where the sequence of rotation is not known and the object is rotated for more than 3 times (eg :-if i rotate the object x-60degress, y-30 degrees, z-45 degrees then again x->30 degrees), is it possible to extract the angles rotated from the transform matrix?.I know that it is possible to get angles if the sequence of rotation is known, but if I have only the final transform matrix with me and nothing else, is it possible to get the angles rotated(x,y,and z) from the transform matrix ?
Euler angle conversion is a pretty well known topic. Just normalize the matrix orientation vectors and then use something like this c source code.
The matrix is the current state of things it has no knowledge of what the transformation has been in the past. It does not know how the matrix was built. You can just take the matrix into and decompose it into any pieces you like, as long as:
The data do not overlap. For example:Two X turns after each other is indistinguishable form each other (no way to know if its 1 2 or three different rotations summed).
The sequence order is known
A decomposition can be built out of the data (for example scale can be measured)

how to Rotate about an arbitrary axis?

Givens
1- X,y,and Z the world co-ordinate system
2-i,j,k another co-ordinate system.
3-the cosines in which each of i,j, and k make with the X,Y,Z.
problem
how to rotate the i,j,k system about i or j or k??
If you have the cosines of the angles formed by pairing each of i,j,k with each of xhat, yhat, and zhat (nine angles altogether), you have the makings for the direction cosine matrix. For example, see http://www.ae.illinois.edu/~tbretl/ae403/handouts/06-dcm.pdf (or just google direction cosine matrix). The direction cosine matrix is just another name for a transformation or rotation matrix.
Be careful, though!
There is no single standard scheme. You need to know that this is the case and read the literature carefully.
Are you rotating the object or transforming coordinates? Rotation and transformation are conjugate operations. Some people (many people!) use the term 'rotation matrix' when they mean 'transformation matrix', and vice versa.
Do you represent vectors as column vectors or row vectors? Here there is a lot more consistency; most people use column vectors rather than row vectors for things like positions, velocities, etc. BUT there are very good reasons to use row vectors (or column vectors if you are one of those contrarians) for things that properly belong in the dual space.
Quaternions have even more ambiguity of representation than matrices. There's nothing wrong with that (I use quaternions all the time), but you do have to beware of these ambiguities when you read a paper or book, look at someone else's code, or exchange data.
Finally, matrices and quaternions are only two of many charts on SO(3). There are lots of ways to represent rotations in 3-space.
You can first create either a rotation matrix or a quaternion. Then you use that to transform your vectors.
You can find the code to create a rotation matrix or a quaternion in pretty much any 3d maths library.
If I recall correctly you calculated the rotation quaternion as(assuming normalized axis):
q.x=axis.x*sin(alpha)
q.y=axis.y*sin(alpha)
q.y=axis.z*sin(alpha)
q.w=cos(alpha)

Quaternions and Transform Matrices

Tell me if I am wrong.
I'm starting using quaternions. Using a rotation matrix 4 x 4 (as used in OpenGL), I can compute model view matrix multiplying the current model view with a rotation matrix. The rotation matrix is derived from the quaternion.
The quaternion is a direction vector (even not normalized) and a rotation angle. Resulted rotation is dependent on the direction vector module and the w quaternion component.
But why I should use quaternions instead of Euler axis/angle notation? The latter is simpler to visualize and to manage...
All information that I found could be synthetized with this beatifull article:
http://en.wikipedia.org/wiki/Rotation_representation
Why it is better to use quaternions is explained in the article.
More compact than the DCM representation and less susceptible to round-off errors
The quaternion elements vary continuously over the unit sphere in R4, (denoted by S3) as the orientation changes, avoiding discontinuous jumps (inherent to three-dimensional parameterizations), this is often referred to as gimbal lock.
Expression of the DCM in terms of quaternion parameters involves no trigonometric functions
It is simple to combine two individual rotations represented as quaternions using a quaternion product
Unlike Euler angles, quaternions don't suffer from gimbal lock.
I disagree that quaternions are easier to visualize, but the main reason for using them is that it's easy to concatenate rotations without "matrix creep".
Quaternions are generally used for calculative simplicity - it's a lot easier (and faster) to do things like composing transformations when using quaternions. To quote the Wikipedia page you linked,
Combining two successive rotations,
each represented by an Euler axis and
angle, is not straightforward, and in
fact does not satisfy the law of
vector addition, which shows that
finite rotations are not really
vectors at all. It is best to employ
the direction cosine matrix (DCM), or
tensor, or quaternion notation,
calculate the product, and then
convert back to Euler axis and angle.
They also do not suffer from a problem common to axis/angle form, gimbal lock.
Quaternions are easier to visualize, manage and create in scenarios where you want to rotate about a particular axis that can be easily calculated. Determining a single rotation angle is much easier than decomposing a rotation into multiple angles.
Corrections to the OP: the vector represents the axis of rotation, not a direction, and the rotation component is the cosine of the half-angle, not the angle itself.
As mentioned, quaternions don't suffer from gimble lock.
For a given rotation, there is exactly one normalized quaternion representation.
There can be several seemingly unrelated axis/angle values that result in the same rotation.
Quaternion rotations can be easily combined.
It is extraordinarily complex to calculate an axis/angle notation that is the cumulation of two other axis/angle rotations.
Floating point numbers have a higher degree of accuracy when representing values between 0.0 and 1.0.
The short answer is that axis/angle notation can initially seem like the most reasonable representation, but in practice quaternions alleviate many problems that axis/angle notation presents.

Resources