Transformation matrix to align an object towards a vector. I have an object(say, a prism) in 3D space and a vector. I need to align this object in the direction of the given vector. What would be the transformation matrix required to achieve it? Assume that starting point of vector is (a,b,c) and end point is (x,y,z).
 A: I'm going to assume aligning in this case means that the normal vector of the object points in the same direction as the desired vector. To this end, let's say that the vector $\pmb n$ is a normal vector of the object and also let's denote by $\pmb d$ (which is easily computed in terms of $x,y,z,a,b,c$) the desired vector. Your task is now to transform $\pmb n$ to a direction towards $\pmb d$, namely
$$\pmb n_t = \alpha \pmb d$$
for $\alpha \neq 0$. Here $\pmb n_t $ is the transformed $\pmb n$. All you need to do is to rotate $\pmb n$ to align with $\pmb d$. This could be achieved by cascaded Given rotation matrices, that is
$$\pmb n_t = G_x(\theta_x)G_y(\theta_y)G_z(\theta_z) \pmb n \tag{1}$$
where 
\begin{alignat}{1}
G_x(\theta_x) &= \begin{bmatrix}
1 & 0 & 0 \\
0 & \cos \theta_x &  -\sin \theta_x \\[3pt]
0 & \sin \theta_x  &  \cos \theta_x \\[3pt]
\end{bmatrix} \\[6pt]
G_y(\theta_y) &= \begin{bmatrix}
\cos \theta_y & 0 & \sin \theta_y \\[3pt]
0 & 1 & 0 \\[3pt]
-\sin \theta_y & 0 & \cos \theta_y \\
\end{bmatrix} \\[6pt]
G_z(\theta_z) &= \begin{bmatrix}
\cos \theta_z &  -\sin \theta_z & 0 \\[3pt]
\sin \theta_z &   \cos \theta_z & 0\\[3pt]
0 & 0 & 1\\
\end{bmatrix}
\end{alignat}
where $G_t(\theta_t)$ denotes a given rotation about the $t-$axis, rotated by $\theta_t$. An example would be 
$$G_z(90^\circ) \begin{bmatrix}1 \\ 0 \\ 0 \end{bmatrix} = \begin{bmatrix}0 \\ 1\\ 0 \end{bmatrix}  $$
which means that you have rotated the x axis towards the y axis. Note that the operation in equation $(1)$ is commutative, that is it does not matter which rotation you start with.
A: To align $\mathbf{a}$ with $\mathbf{d}$, assumed linearly independent, rotate about the axis $\mathbf{a}\times\mathbf{d}$ with an angle $\angle(\mathbf{a},\mathbf{d})=\arccos(\frac{\mathbf{a}}{\lvert\mathbf{a}\rvert}\cdot\frac{\mathbf{d}}{\lvert\mathbf{d}\rvert})$ (using the usual principle branch of arccos, angle between $0$ and $\pi$).
See the answer to this question if you don't know how to do this (note that you need to normalise the axis vector first).
