# Solve for Angles in a Matrix

So, I have a linear equation, and I need to solve for some variables in said equation. However, since I don't know much about matrices, I don't know how to solve for the variables. The equation in question is the one from the Wikipedia page on perspective projection. It is as follows:

$$\begin{bmatrix}d_{x}\\d_{y}\\d_{z}\end{bmatrix}=\begin{bmatrix}1&0&0\\0&\cos\Theta_{x}&-\sin\Theta_{x}\\0&\sin\Theta_{x}&\cos\Theta_{x}\end{bmatrix}\begin{bmatrix}\cos\Theta_{y}&0&\sin\Theta_{y}\\0&1&0\\-\sin\Theta_{y}&0&\cos\Theta_{y}\end{bmatrix}\begin{bmatrix}\cos\Theta_{z}&-\sin\Theta_{z}&0\\\sin\Theta_{z}&\cos\Theta_{z}&0\\0&0&1\end{bmatrix}\left(\begin{bmatrix}a_{x}\\a_{y}\\a_{z}\end{bmatrix}-\begin{bmatrix}c_{x}\\c_{y}\\c_{z}\end{bmatrix}\right)$$

I know $a$, $c$, and $d$, but I need to find $\theta$. Let's say that $a$ is (5,0,0), $c$ is (10,0,0), and $d$ is (10,5,0). How, then, would I solve for $\theta$?

-
is $b = (10, 0 , 0)$ what you are calling $c$? Regards – Amzoti Jan 23 '13 at 23:02
@Amzoti Oh, oops! Yes. – Steven Fontaine Jan 23 '13 at 23:13

You have two vectors, $d$ and $a-c$, and are trying to solve for a rotation matrix $R$ that maps $a-c$ to $d$: $$d = R(a-c).$$

Here you've written $R$ as a composition of rotations using Euler angles. Before we worry about the Euler angles, let's find $R$.

Notice that this equation only has a solution when $d$ and $a-c$ have the same norm (which your example vectors do not), and that even when a solution exists, it is not unique.

One way to overcome these difficulties is to find the "best possible" rotation from $a-c$ to $d$, i.e.,

$$\min_R \|d-R(a-c)\|\quad \textrm{s.t.}\quad R^TR=1, \det R=1,$$

which always has a solution, given by the Kabsch algorithm. In this case, where our data set consists of only one pair of vectors, we can take a shortcut. The axis of the optimal rotation is given by $$\hat n = \frac{(a-c) \times d}{\|(a-c) \times d\|}$$ and the angle by $$\theta = \arccos\left(\frac{a-c}{\|a-c\|}\cdot\frac{d}{\|d\|}\right).$$

From this axis and angle, you can find $R$ using the Rodrigues rotation formula, and from there, compute the Euler angles. See http://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions for more details.

EDIT: Notice that the above assumes that $d$ and $a-c$ have non-zero norm, and are not colinear. These special corner cases require separate treatment.

-
"I don't know much about matrices." I have no clue on the algorithms to which you referred, and little idea of most of the terminology which you used.. Could you please elaborate some and/or provide links where I can find some explanation? – Steven Fontaine Jan 24 '13 at 2:33
Sure. Does it make sense how to compute $\hat n$ and $\theta$? Once you have those, use the formulas in en.wikipedia.org/wiki/… to get the matrix $R$ ($A$ on Wikipedia) and then use en.wikipedia.org/wiki/… to get $\theta_x, \theta_y, \theta_z$. – user7530 Jan 24 '13 at 3:00