[Math] Transformation matrix between a 2D and a 3D coordinate system

linear algebraprojectiontransformation

Supposing I have this data :
2D points (known coordinates): P2D in a 2D coordinate system CS2D. and their corresponding (their equivalent)

3D points (known coordinates) : P3D. in a 2D coordinate system CS3D.
PS : CS2D & CS3D don't have the same origin.

How would I find a transformation matrix T(Rotation, Translation, Scaling) such as it allows to go from any 3D point to it's correspondent 2D point or the inverse ? Some kind of : P2D = T * P3D
How would I approach this problem ?

Best Answer

Since this is a math site, I'll answer this in a mathematical format. It will probably translate relatively directly to whatever software library you are currently using.

The first step is to identify the 3D coordinates of the following points:

  • $\mathbf p_0,$ the point that has coordinates $(0,0)$ according to the 2D coordinate system;
  • $\mathbf p_1,$ the point that has coordinates $(1,0)$ according to the 2D coordinate system;
  • $\mathbf p_2,$ the point that has coordinates $(0,1)$ according to the 2D coordinate system.

That is, the three points $\mathbf p_0,$ $\mathbf p_1,$ and $\mathbf p_2,$ which describe the origin of the 2D coordinates and points on the two axes of the 2D coordinates, have 3D coordinates that could be written as column vectors like this: $$ \mathbf p_0 = \begin{pmatrix} p_{01} \\ p_{02} \\ p_{03} \end{pmatrix},\quad \mathbf p_1 = \begin{pmatrix} p_{11} \\ p_{12} \\ p_{13} \end{pmatrix},\quad \mathbf p_2 = \begin{pmatrix} p_{21} \\ p_{22} \\ p_{23} \end{pmatrix}. $$ From these points we can derive two vectors: $\mathbf e_1 = \mathbf p_1 - \mathbf p_0$ and $\mathbf e_2 = \mathbf p_2 - \mathbf p_0,$ where the subtraction is a componentwise vector operation.

Now to translate any 2D coordinates $(x,y)$ into 3D coordinates, you perform scalar multiplication (scaling) and componentwise addition of these vectors to produce a 3D point $\mathbf p$: $$ \mathbf p = \mathbf p_0 + x \mathbf e_1 + y \mathbf e_2. $$ (That is, $x\mathbf e_1$ multiplies each coordinate of $\mathbf e_1$ by $x,$ and so forth.)

If your library supports matrices and matrix multiplication (which seems likely), you can put the coordinates of $\mathbf e_1$ and $\mathbf e_2$ side by side in two columns within a matrix, and then for any 2D coordinates $(x,y)$ peform the following matrix multiplication, which produces a $3$-element column vector: $$ \mathbf p = \mathbf p_0 + \begin{pmatrix} e_{11} & e_{21} \\ e_{12} & e_{22} \\ e_{13} & e_{23} \end{pmatrix} \begin{pmatrix} x \\ y \end{pmatrix} $$

And that is how you convert any 2D coordinates to 3D.

To convert in the opposite direction, from 3D to 2D, I'll assume you want orthogonal projection, that is, you want to map each point in the 3D space to the closest point in the 2D plane. You do this with the same vectors $\mathbf e_1$ and $\mathbf e_2$ that you used for the 2D-to-3D transformation, as well as the 3D coordinates $\mathbf p_0$ of the origin of the 2D system.

Given a point in 3D coordinates, expressed as a column vector $\mathbf p,$ take the following dot products (aka inner products): \begin{align} x &= (\mathbf p - \mathbf p_0) \cdot \mathbf e_1, \\ y &= (\mathbf p - \mathbf p_0) \cdot \mathbf e_2. \end{align} Then $(x,y)$ are the desired 2D coordinates.

Depending on your library, it might not have an explicit dot product operation but might allow you to transpose a vector instead, in which case you can use matrix multiplication on the "vectors" in order to compute the dot products, for example, using the superscript $T$ to indicate a transpose, as in $\mathbf v^T,$ $$ x = (\mathbf p - \mathbf p_0)^T \mathbf e_1. $$

Related Question