# calculate rotation from 2 3d lines

I am trying to extract the transformation of a segment described by two 3d points $(a_0,b_0)$ into the transformed points $(a_1,b_1)$.

I have been able to calculate the translation, and I am assuming that there is no scale of the segment.

How can I calculate the rotation?

Thanks!

-
–  nikamed May 9 '13 at 3:10

A combination of translation and rotation is simply a rotation around a different axis. So I'd leave out the translation aspect for now, and concentrate on finding the axis of rotation. If you want to, you can later on separate things into a rotation around the origin followed by a translation.

# Direction of the axis

As each point is rotated around the axis, it stays in a plane perpendicular to that axis. So you know that the difference vectors $a_1-a_0$ and $b_1-b_0$ are both perpendicular to the direction vector of the axis of rotation. You can therefore compute that vector using the cross product:

$$r = (a_1-a_0)\times(b_1-b_0)$$

# Location of the axis

Now you have the direction of that axis, but still need its location. To find that, consider the fact that it will have equal distance to both preimage and image points. So it must lie on the perpendicular bisector plane between $a_0$ and $a_1$. That plane can be described as

$$A = \left\{p\in\mathbb R^3\middle\vert \left<a_0-a_1,p\right> = \tfrac12\left<a_0-a_1,a_0+a_1\right> \right\}$$

where $\left<\cdot,\cdot\right>$ denotes the scalar product (dot product). Similarly it has to have equal distance to $b_0$ and $b_1$, so it also has to lie in

$$B = \left\{p\in\mathbb R^3\middle\vert \left<b_0-b_1,p\right> = \tfrac12\left<b_0-b_1,b_0+b_1\right> \right\}$$

The axis of rotation will now be the intersection of planes $A$ and $B$. The above formulations of these planes will result in two equations for the three coordinates of $p$. The solution space will therefore be one-dimensional: this is the axis of rotation.

It will be a line in the direction of $r$ computed above, so it turns out that the separate computation of $r$ up front wasn't strictly neccessary, since the orthogonality to both difference vectors will be implied by this intersection. But it will still appear as part of that solution, and will be useful later on. You can even use it to check your solution here.

# Angle

To compute the angle of rotation, you can intersect that axis of rotation with the plane in which $a_0$ is rotated to $a_1$. To do this, look for a point $p$ on that axis of rotation (i.e. $p\in A, p\in B$) such that

$$\left<r, p-a_0\right>=0$$

This point $p$ is the orthogonal projection of $a_0$ onto the axis of rotation. Now the vectors $a_0-p$ and $a_1-p$ describe your rotation in that plane, and you can compute the angle $\theta$ from

$$\cos\theta = \frac{\left<a_0-p,a_1-p\right>} {\lVert a_0-p\rVert\cdot\lVert a_1-p\rVert}$$

Since this does not give you the sign of $\theta$, you might want to adjust the orientation of your axis to match common conventions. For example, you might want to ensure that $r,(a_0-p),(a_1-p)$ follow the right hand rule, or some similar condition. You can do this by plugging all three vectors as columns into a determinant. If the sign of the determinant is negative, change all signs in $r$ and you should be fine.

# Matrix notation

Now that you have both the axis and the angle of rotation, you are in the world of the axis-angle formalism. You can use that to convert to a rotation matrix. The wikipedia articles I just linked to only talk about rotations around the origin, ignoring the location of the axis. You can apply that transformation to $a_0$, and then take the difference between that and $a_1$ to obtain the translation you have to apply after a rotation around the origin. You can also combine both of this into a single $4\times 4$ affine transformation matrix.

-