Rotate normal vector onto axis plane

ypnos picture ypnos · Jun 21, 2009 · Viewed 17.5k times · Source

I have a set of data points in 3D space which apparently all fall onto a specific plane. I use PCA to compute the plane parameters. The 3rd component of PCA gives me the normal vector of the plane (weakest component).

What I want to do next is to transform all the points onto said plane and look at it in 2D.

My idea was to do the following:

  • Find a center point (average point) on the plane
  • Substract it from all data points to arrange them around the origin
  • Rotate the normal so that it becomes (0,0,-1)
  • Apply this rotation to all data points
  • Use orthogonal projection (basically, skip z axis)

Now I'm stuck at finding the right rotation operation. I tried working with acos or atan and setting up two rotation matrices. Seems both methods (using acos, using atan) give me the wrong result. Perhaps you can help me out here!

Matlab code follows:

b = atan(n(1) / n(2));
rotb = [cos(b) -sin(b) 0; sin(b) cos(b) 0; 0 0 1];
n2 = n * rotb;
a = atan(n(1) / n(3));
rota = [cos(a) 0 sin(a); 0 1 0; -sin(a) 0 cos(a)];
n3 = n2 * rotaows:

I expect n2 to have y component of zero. However that fails already for the vector (-0.6367, 0.7697, 0.0467).

Answer

Nosredna picture Nosredna · Jun 21, 2009

If you have a plane, you have a normal vector and an origin. I wouldn't do any "rotations" at all. You're just a few vector operations away from your answer.

  • Let's call your plane's normal vector the new z axis.
  • You can generate the new y axis by crossing the old x axis with the new z axis (your plane's normal).
  • Generate the new x axis by crossing the new z with the new y.
  • Make all your new axis vectors into unit vectors (length 1).
  • For every point you have, create a vector that's from your new origin to the point (vector subtraction of point - plane_origin). Just dot with the new x and new y unit vectors and you get a pair (x,y) you can plot!

If you have cross and dot product functions already, this is just a few lines of code. I know it works because most of the 3D videogames I wrote worked this way.

Tricks:

  • Pay attention to which directions your vectors are pointing. If they point the wrong way, negate the resultant vector or change the order of the cross product.
  • You have trouble if your plane's normal is exactly the same as your original x axis.