Rotation matrix for a set of points

4.2k Views Asked by At

I've got a set of $N$ points $p_1,\dots,p_N$ that all belong to a real object. Consequently, there are $N-1$ vectors $\vec{v}_i$ when $\vec{v}_i$ points from $p_1$ to $p_i$.

Now, the object is rotated in some unknown way. $p_1$ stays in the same place (= no translation, just rotation), but all other points are now at their new location $p'_i$ - which means that the vectors also changed to $\vec{v}'_i$ (same length, but different directions).

I know all $p, p', \vec{v}$ and $\vec{v}'$ - using these values, how can I express the rotation via a rotation matrix?


I've tried to use cross-product to get the rotation axis and the scalar-product to get the rotation angle for a single vector, which enables me to compute a rotation matrix - but if I use different vectors I get different results!?

This is the way I do this:

$$\vec{a} = \frac{ \vec{v_2}\times\vec{v}_2' }{ |\vec{v_2}\times\vec{v}_2'| }$$ $$c = \frac{ \vec{v_2} * \vec{v}_2' }{ |\vec{v_2}| \cdot |\vec{v}_2'| }$$ $$s = sin(cos^{-1}(c))$$ $$t = 1 - c$$

With these values, the rotation matrix is (according to this website):

$$ R = \left( \begin{matrix} t*x*x + c & t*x*y - z*s & t*x*z + y*s\\ t*x*y + z*s & t*y*y + c & t*y*z - x*s\\ t*x*z - y*s & t*y*z + x*s & t*z*z + c \end{matrix} \right) $$

(with $\vec{a} = (x,y,z)^T$)

Thank you for any thoughts on this!

3

There are 3 best solutions below

1
On BEST ANSWER

I am guessing this is in 3D. Then if you are given at least $3$ vectors that are linearly independent, say $\vec v_2, \vec v_3$ and $\vec v_4$, then your rotation matrix $R$ satisfies

$$ R\begin{pmatrix} \vec v_2 & \vec v_3 & \vec v_4 \end{pmatrix} = \begin{pmatrix} \vec v'_2 & \vec v'_3 & \vec v'_4 \end{pmatrix}. $$

You can then invert the matrix $\begin{pmatrix} \vec v_2 & \vec v_3 & \vec v_4 \end{pmatrix}$ to get $R = \begin{pmatrix} \vec v'_2 & \vec v'_3 & \vec v'_4 \end{pmatrix}\begin{pmatrix} \vec v_2 & \vec v_3 & \vec v_4 \end{pmatrix}^{-1}$.

If your transformation is consistent, any triple of $\vec v_i$ should give you the same $R$.

If you have fewer than $3$ linearly independent vectors, then your rotation matrix is not unique.

1
On

First of all, your method is wrong. You are looking for the rotation matrix that transforms one vector (e.g. $v_2$) to its correspondent ($v_2'$). This matrix is, however, not unique. The rotation found by your method has always the rotation axis perpendicular to $v_2$ and $v_2'$. That's why when you use your method for $v_3$ and $v_3'$ you'll get a different rotation. Actually, in general cases, $v_2$ and $v_3$ will together determine a unique rotation, and the rotation axis is neither perpendicular to $v_2$ and $v_2'$, nor $v_3$ and $v_3'$.

The solution to your problem is elegant and easy, and can be found on this page: https://en.wikipedia.org/wiki/Wahba%27s_problem

In the original Wahba's problem, the cost function $J(R)$ contains weights $a_k$. Particularly in your case, all weights should be set to 1. All you need to do is to follow the SVD algorithm explained in the wiki. If your points $v_2'$, $v_3'$, … are all generated by the same rotation $R$, this algorithm should give you this matrix $R$, while ensuring $J(R) = 0$.

1
On

Rotations in 3D are quite difficult, and the most effective way to compute these is using quaternions (and this is not very intuitive mathematics).

The rotation matrix you describe is a matrix product of the rotation matrices for all 3 axes, for which the order in which you multiply needs to be specified (multiple ways lead to Rome). Another pitfall of this approach is the "Gimbal lock".

Quaternions provide an alternative approach. A rotation in 3D can always, and uniquely be described as a rotation $\theta$ around some axis $\vec{u}$ in 3D space (which generally is not aligned with any of the originally chosen axes, but it can be ofcourse). Quaternions circumvent the issue of defining the order of rotation and does not suffer from the Gimbal lock.

So how does one find this rotation angle and axis of rotation you might ask? This page goes into all details.

Centre the data

Using the vectors to define matrices $P_i$ as so: $$ \begin{pmatrix} 0 & -p_{i1} & -p_{i2} & -p_{i3}\\ p_{i1} & 0 & p_{i2} & -p_{i3}\\ p_{i2} & -p_{i3} & 0 & p_{i1}\\ p_{i3} & p_{i2} & -p_{i1} & 0 \end{pmatrix} $$ Find the eigenvector $\vec{x}$ with the largest eigenvalue of the matrix below (as noted in the reference, this is a symmetric matrix, so all eigenvalues are positive real numbers or zeros). $$\sum_n{P_i^TV_i}$$ The first entry of the normalized eigenvector will give you the angle $$cos(\theta/2) = \frac{x_1}{||x||^2}$$ If this angle is non-zero, the axis of rotation $\vec{u}$ is in contained in the remaining entries of the eigenvector. If you desire the rotational axis to be a unit vector, divide by $sin(\theta/2)$.