Quaternion and Matrix

390 Views Asked by At

I have a quaternion for rotation and a matrix for changing axis(change coordinate from camera to my rendering scene ). I have tested two method and i except to have equal resuls but results are different

Matrix4x4 openglChangeCoordinate={0 1 0 0,
                                 1 0 0 0,
                                 0 0 1 0,
                                 0 0 0 1};

Transform tr1;
tr1= Q2Mat(20,1,1,0.2);//quaternion to matrix
tr1= openglChangeCoordinate*tr1;//multiply previos matrix


Transform tr2;
tr2=openglChangeCoordinate;
double* vec={1 ,1, 0.2};
double* newVec=tr2.TransformPoint(vec);
Transform tr3;
tr3=Q2Mat(20,newVec[0],newVec[1],newVec[2]);

why tr1 and tr3 are different?

The issue come from change coordinate from a tracker(stereo camera ) to OpenGL rendering scene. we try to describe every transformation by matrix but we are unsuccessful.the second approach get correct result. is there any way to solve problem only by multiply matrixes ?

1

There are 1 best solutions below

6
On BEST ANSWER

It would be strange if the two resulting matrices would be close to each other.

The way a quaternion $q$ relates to a rotation $Q$ is by the formula $$ Qx=qxq^{-1} $$ or, if vector-matrix products are used instead of matrix-vector products, $$ xQ=q^{-1}xq, $$ where on the right $x$ is interpreted as a purely imaginary quaternion, and then imaginary part of the product is interpreted back as a 3D vector.


Any quaternion can be written as $q=|q|\cdot(\cos\phi+\sin\phi\cdot e)$ where $e$ is a purely imaginary unit quaternion. The rotation associated with it is around the axis $n$ with angle $2\phi$.


So let's interpret your calculations. The first relates to the quaternion $$q_1=20+\mathfrak i+\mathfrak j+0.2\,\mathfrak k$$ with norm $\sqrt{402.0}=20.051$ and angle of $\arccos(20/20.051)=4.085°$ with axis $n=(0.7,0.7,0.14)$. The orthogonal matrix of the corresponding rotation is then applied to an affine transformation matrix of determinant $-1$, so the matrix part of the resulting affine transformation will also have determinant $-1$.

The second calculation transforms the axis of rotation by permuting the first two components (doing nothing for this vector) and then increasing the first component by $1$, resulting in $2,1,0.2$. This changes the direction and length of the axis, which in turn changes the angle of rotation, the norm of the quaternion $$ q_2=20+2\,\mathfrak i+\mathfrak j+0.2\,\mathfrak k $$ now is $\sqrt{205.04}=20.126$ with angle $\phi=6.405$ and rotation axis $(0.891,0.445,0.089)$. So it is to be expected that the outcome is a different rotation. The end result of this computation is the rotation matrix of the quaternion, which is orthogonal with determinant $1$ and zero translation vector, and thus can, for this reason alone, not coincide with the first result.


If the transform matrix were really orthogonal, with positive determinant, and translation part zero, then it would correspond to a quaternion $q_0$, $xT=q_0^{-1}xq_0$, and the combined transformation could be refactored as $$ xTQ=q_1^{-1}q_0^{-1}xq_0q_1 =q_0^{-1}(q_0q_1q_0^{-1})^{-1}x(q_0q_1q_0^{-1})q_0 =x\tilde Q T $$ where $\tilde Q$ corresponds to $\tilde q_1$ results from applying $T^{-1}$ to the vector part of $q_1$.