I am working a project that involves calculating quaternions of ellipsoids providing that I already know the unit vectors of their principal axes. The reason for this is the program I am using to work with these ellipsoids require quaternions. The description from the manual of that program of quaternions is: "The particles must define a quaternion for their orientation. Note that particles defined as ellipsoid have 3 shape parameters. The 3 values must be non-zero for each particle set by this command. They are used to specify the aspect ratios of an ellipsoidal particle, which is oriented by default with its x-axis along the simulation box’s x-axis, and similarly for y and z. If this body is rotated (via the right-hand rule) by an angle theta around a unit rotation vector (a,b,c), then the quaternion that represents its new orientation is given by $(cos(\theta/2), a*sin(\theta/2), b*sin(\theta/2), c*sin(\theta/2))$"
From some source in the literature (which I am not sure if is correct), I calculate quaternions like this:
- Construct a orientation matrix whose columns are the principal axes
- Solve for e-values and e-vectors of that matrix. The e-values should be (1, $cos(\theta) -i sin(\theta)$,$cos(\theta) +i sin(\theta)$) where theta is the rotational angle. The unit e-vector $e = [e_1 e_2 e_3]^T$ that correspond to e-value 1 is an invariant principal axis of rotation.
- The quaternions are: $q_w = cos(\theta/2)$, $q_i = e_1sin(\theta/2)$, $q_j = e_2sin(\theta/2)$, $q_k = e_3sin(\theta/2)$
However, when I try to check if the ellipsoids using the quaternions I calculated agree with the principal axes (using a graphic tool), they don't agree with each other.
I am not an expert in Maths, and only do this because of my project. I really appreciate if someone can help me to point out what I did wrong, or give me some good references, or even just explain to me what the manual description means.
Cheers,
Knowing the orientation vectors is essentially the same as knowing the transformation matrix, which is constructed by putting these vectors (they have to be normalized and orthogonal) into matrix columns.
First possible issue: arrange the vectors so that they form a positively oriented triad (the determinant of the matrix should be 1, not -1). Otherwise, your matrix is not a rotation but a rotation+reflection. Fixing it is easy, as it's an ellipsoid and you can flip the sign of any main axis vector without changing anything. Second possible issue is that the order of axes is different and you are putting the long axis where the short axis should be (it depends what is the "home" position of the ellipsoid before rotation).
Then, the easiest way is to use the invariant properties of the matrix to construct the axis and angle of rotation.
The trace of the matrix (sum of diagonal) always equals $2\cos\theta+1$, so this is the cheapest way of getting the rotation angle. Note that you still don't know if it's clockwise or anticlockwise (cosine destroys the sign), but you didn't choose the axis orientation either, so it's fine.
The axis is easiest to get from the antisymmetric part of the matrix. If your matrix is $R$, then calculate
$$ R-R^T= \begin{bmatrix} 0&c&-b\\ -c & 0 &a \\ b & -a & 0 \end{bmatrix} $$ So, taking the difference of the matrix to its transpose, you can read the axis $(a,b,c)$ from the off-diagonal elements. Essentially, you have $$a=R_{23}-R_{32},\quad b=R_{31}-R_{13},\quad c=R_{12}-R_{21}$$
The length of this vector is not unit, but $2\sin\theta$, so you must normalize it (you can also verify this angle if it matches the previous one calculated from the trace).
Now that you have the normalized $(a,b,c)$ axis (unit vector) and the angle $\theta$ you can construct the quaternion.
There's always a possibility that there is another minus that we lost when defining the angle and axis direction - try both and see if it helps.