# 3D to 2D rotation matrix

I have been trawling through this forum but am struggling to understand the maths a bit. Currently I have a 2D plane within a 3D space and I have the coordinates for them. I want to work on this 2D plane as if it is 2D since it is easier.

From what I've read, I want to create a rotation matrix which would make z constant so it can be effectively ignored. After carrying out my 2D calculations, I could then use the inverse of the matrix and bring it back into 3D space?

My problem is with the rotation matrix, is it a combination of rotating around the X-axis and Y-axis? Apologies for my maths ignorance! It would be great if someone could explain the steps/ point me in the right direction.

Many Thanks,

Kelvin.

EDIT: Here's an example of the KML data. This shows one triangle:

Triangle A

-1.465435652058573, 53.37698311217353, 68.20299999999998

-1.465442809960414, 53.37700937634325, 69.52299999999991

-1.465399364873617, 53.37701518696172, 68.20299999999998

-1.465435652058573, 53.37698311217353, 68.20299999999998

(The reason why it has 4 points is because the first and last are repeated to indicate a closed polygon. Sorry if it is confusing!)

EDIT 2:

Simon's answer to my question seems to make sense! If anyone has problem with the Gram-Schmidt process, have a look at this Youtube video. It was very useful for me! http://www.youtube.com/watch?v=ZRRG386v6DI

-
What do you mean by "I have the coordinates"? What data is exactly specified? – Simon Markett Jul 24 '12 at 10:12
Bzw: welcome to MSE. I think your question is quite good - motivation/own idea/question, but it would help making it even better if you specified all your data. – Simon Markett Jul 24 '12 at 10:15
If you know the plane in which your flat object lies, and you want to rotate that object while staying in the plane, what you could do is to find the normal (perpendicular) to your plane, and then use the Rodrigues rotation formula to build the rotation matrix you need. – J. M. Jul 24 '12 at 10:29
@SimonMarkett Hi Simon. Thanks for your welcome, by "I have the coordinates", it is actually a .KML file. I am a geographer trying to write a 3D solar panel positioning algorithm but am treating a roof face as a 2D plane as the maths would be easier for me. I've added an excerpt example of what the data looks like in my original post. I hope it helps! – Kel196 Jul 24 '12 at 16:50
I think you should delete the fourth point as it is identical to the first one. Presumably it is included in the data to indicate that the polygon is closed, but here it will only confuse people. – Rahul Jul 25 '12 at 10:45

First of all I still don't fully understand the data you have given. Are those the $x,y$ and $z$ coordinates of four points? Why do four points form a triangle?

Secondly, if I interpret your data correctly the different points have very similar values. That might lead to numerical complications, i.e. the differences you investigate might be of the same order as the inaccuracy of your computer. So apart from theoretical issues one has to be careful with the implementation.

Leaving that aside you can describe the plane inside the three dimensional space by two vectors. Let us call them $v_1$ and $v_2$. Every point in the plane is then a linear combination of those two (that is if the origin is a point in your plane, but otherwise you may just change the origin which is a linear translation and well behaved). You can find $v_1$ and $v_2$ by just choosing any two linearly independent vectors in your plane. You may then choose a third vector $v_3$ which is not in you plane and the three vectors together span your whole three dimensional space. What you want to do next is transforming the vectors into a so called orthonormal basis. This orthonormal basis may be understood as a sort of a new coordinate system, where the given plane is just the $x$-$y$-plane. You can find this orthonormal basis purely mechanical by applying the Gram-Schmidt process to your vectors $v_i$.

Now the rotation you are looking for is just a rotation of this coordinate system. It is very easy to write down its inverse. Indeed the inverse is just the matrix with the three orthonormal vectors as columns. Since you need the transformation in both directions this is no extra effort.

It turns out that the matrix you obtain this way is a rotation (+possibly a reflection) but you don't have to find the angles and stuff.

So as a recepy:

1. Find $v_1$ and $v_2$ in your plane and $v_3$ outside your plane.
2. Apply Gram-Schmidt
3. Put new vectors as columns in a matrix.
4. Invert the matrix.

Edit: Some important facts I still can't believe I forgot in the first go:

1. The inverse of an orthogonal matrix (i.e. a matrix with an orthonormal basis as columns; like in your situation) is just its transpose. So you get the rotation (+ possibly reflection) in one direction by taking the basis as rows and in the other direction by taking them as columns (you may want to check this to convince yourself.)
2. Given a triangle with corners $A,B$ and $C$ you can just choose $A$ to be the origin and take $v_1=B-A$ and $v_2=C-A$. This should probably also take care of any numerical issues (again, not my specialty).
-
Hi Simon, yes the data at x, y, z coordinates are in a project coordinate system WGS 1984. The reason why they have four points is because it required in .kml format to "close" the polygon. Your point about the different points having similar values is one that hadn't thought about, thank you! Currently I am trying to work out the conceptual maths side of a solar panel positioning programme. I will follow your post above by trying it using more simple values and will update my post when I have carried that out. – Kel196 Jul 26 '12 at 9:18
Hey Kelvin, feel free to update your question concerning theoretical matters. However I would advise you to post any numerical issues in a seperate question since they are in fact a whole different story. Actually I know very little about numerical stuff anyway. – Simon Markett Jul 26 '12 at 9:53
Hi Simon, thank you very much for your help, I think it is beginning to make sense. After converting to a 2D plane and carrying out my calculations, to convert back to the original coordinate system, would I multiply with the matrix created in step 3 to reverse the process? – Kel196 Jul 26 '12 at 10:07
Exactly! Since the inverse of the inverse is the original matrix. – Simon Markett Jul 26 '12 at 10:09
Duh, I forgot some important facts which should simplify things further. See edit. – Simon Markett Jul 26 '12 at 10:40

If I understand your question, you are given a set of three points in a plane in $\mathbb{R}^3$, and you wish to perform operations on the points in that plane, and then move them back to $\mathbb{R}^3$. If that is correct, try the following.

Given $\triangle PQR\in\mathbb{R}^3$ that is not degenerate, compute the vectors $$S=\frac{Q-P}{\|Q-P\|} \quad\text{and}\quad T=\frac{S\times(S\times(R-P))}{\|S\times(S\times(R-P))\|}\tag{1}$$ Next Given any points $\{U_k\}$ in the same plane with $\triangle PQR$, map them to $\mathbb{R}^2$ with $$x_k=S\cdot(U_k-P) \quad\text{and}\quad y_k=T\cdot(U_k-P)\tag{2}$$ after performing operations in $\mathbb{R}^2$ on these points (e.g. rotation), map $\{(x_k,y_k)\}$ back to $\mathbb{R}^3$ with $$U_k=x_kS+y_kT+P\tag{3}$$

-

What you need is a matrix that moves the 2d plane you have to one of the planes that coordinate axes form. You can then use rotation matrices given here. Such matrix is simple to construct. First choose any three vectors such that two of them are on the plane and the third isn't. Now write a matrix $A$ which has these three vectors as its columns. Notice how this matrix maps vectors $\vec{i}, \vec{j}, \vec{k}$ onto the vectors you chose. Finally, the rotation matrix you want is $A R A^{-1}$, where $R$ is one of the simple rotation matrices. Notice that this is the long way though. If you look at the link I gave, there is a general rotation matrix given.

-
I think it will in fact be easier. If we know an ONB of $\mathbb R^3$, where two base vectors span the given plane, it is easy to write down an isometry such that the plane is mapped to the $x-y$-plane. This will turn out to be a rotation matrix, but we actually don't really have to care. – Simon Markett Jul 24 '12 at 10:57
I should note, that while I am here talking about a plane containing the origin, same works for any other plane. Then instead of moving your plane on one of the coordinate planes, it will be made parallel to one of them. – Karolis Juodelė Jul 24 '12 at 11:01
Hi @KarolisJuodelė, thank you very much for your reply. I will follow up that link and give the rotation matrix a go this evening and re-post. I think I understand what you mean, but nothing beats understanding things other than actually doing it! – Kel196 Jul 24 '12 at 17:19