Hi,
This seems inherently a simple task, but I'm having real problems with it!

I have a plane, let's call it P, with its direction defined by its normal vector n, and the camera distance, d, both of which I know:

$\displaystyle P: \textbf{n}.\textbf{x} = d $

Upon P, lie a set of coordinates:
$\displaystyle
C = \left(\begin{array}{ccc}
x_1 & & x_n\\
y_1 & ... & y_n\\
z_1 & & z_n\\
\end{array}\right)
$

I need to rotate these coordinates onto the x-y plane to give a top-down view. I can't move the camera - it's fixed.

My initial attempt was to work out the rotation angles from the normal (x-rotation = phi, y-rotation = theta and z-rotation = psi):

$\displaystyle
\phi = cos^{-1}( n_z )
\psi = sin^{-1}( \frac{-n_x}{sin(\phi}})
$

I could then do:

$\displaystyle
C' = R_x(\phi) . R_z(\psi) . \left(\begin{array}{c}x_i\\y_i\\z_i\end{array}\rig ht)
$

(where (xi,yi,zi) is each coordinate in C).

For some reason, this worked sometimes, but other times the angle was completely wrong!

So I tried something else. When the P is rotated to align with the x-y plane we have:

$\displaystyle
R .
\left(\begin{array}{c}
X\\
Y\\
Z
\end{array}\right)
=
\left(\begin{array}{c}
X'\\
Y'\\
0
\end{array}\right)
$

And we know the combined x-y-z rotation matrix:


For ease of reading, hereafter we have:
$\displaystyle
r_3 = [ a, b, c ], where
\begin{array}{l}
a = \sin(\theta)\sin(\phi) \\
b = -\sin( \theta )\cos( \phi ) \\
c = \cos( \theta )
\end{array}
$

Since we don't know what X' and Y' will be, we only use the bottom row of the rotation
matrix. For three coordinates on the original plane, we then have 3 equations with which
to find phi and theta:

$\displaystyle
\begin{array}{l}
a.x_1 + b.y_1 + c.z_1 = 0 \\
a.x_2 + b.y_2 + c.z_2 = 0 \\
a.x_3 + b.y_3 + c.z_3 = 0 \\
\end{array}
$

Which gives rise to the linear system
$\displaystyle
A = \left(
\begin{array}{ccc}
x_1 & y_1 & z_1 \\
x_2 & y_2 & z_2 \\
x_3 & y_3 & z_3 \\
\end{array}
\right)
$
$\displaystyle
x =
\left(
\begin{array}{c}
a \\
b \\
c \\
\end{array}
\right)
$
$\displaystyle
Ax = \textbf{0}
$

Which we solve using Singular Value Decomposition to get a,b and c (take the last column of the "V" matrix). We can then work out phi and theta:

$\displaystyle
\begin{array}{l}
\theta = cos^{-1}( c )\\
\phi = cos^{-1}( \frac{b}{sin(\theta)} )
\end{array}
$

These values were then subbed into the compound rotation matrix given above, and psi was set to 0 giving R. Finally I multiplied each coordinate by R:
$\displaystyle
C' = R(\phi, \theta, \psi) \,. \, C
$

This again, didn't work. Although I'm convinced that in theory it should. Is anyone able to spot any errors I might have made? Or can anyone contribute a different idea? This has taken me the last week of work so any advice is most gratefully received!!!
Thanks!