# Transforming $2D$ outline into $3D$ plane

I am writing a program where I would like to allow the user to draw 4 connecting lines, such as:

And convert this shape into a 3D plane. Is this possible? Is there an existing algorithm to do so? If not, any idea of the steps I should be taking?

Things we can assume: the camera is at $0,0,0$, facing $[0,0,-1]$. The plane we create will be centered at $0$ on the $z$-axis.

Ideally I'd like the result to be in the form of a set of rotate, scale, translate vectors for a rectangle centered at $[0,0,0]$ of size $[1,1]$.

Please let me know if you need any more information. I don't really know where to start on this...

(I'm not sure if this question would be more suitable for stackoverflow or gamedev. If so, please feel free to move it. However the question is mainly math related so I'm going to try here first.)

• How exactly should the quadrilateral describe a $3D$ plane? I assume the shape in $3D$ is supposed to be a rectangle, but is it supposed to be a square, as well? I don't think there is a unique projection that would render a rectangle to the quadrilateral above. Given the aspect ratio of the rectangle, then there is a unique (up to scaling) transformation that yields the given quadrilateral. – robjohn Sep 8 '11 at 20:43
• What is your end goal? If you want the user's (2D) shape to be interpreted as a plane in 3-space, I think you're going to have to make some additional assumptions. As it stands, the sketch you provided could be a rectangle angled away from the camera, or a trapezoid parallel to the plane of the camera. More specifically I suppose, what role does the rectangle at at (0,0,0) play? – Andrew Christianson Sep 8 '11 at 21:06
• Yes, the drawn shape is assumed to be a rectangle. What I'd like ideally is to find a transformation that will transform the square at [0,0,0] to give the projected outline drawn by the user. Does that help? – Groky Sep 8 '11 at 21:17
• I think it is a computer vision problem. The user drawn outline is the perspective projection of the 3D rectangle. But if the camera is centered at (0,0,0), how can the 3D rectangle be too? You need to determine the image plane of the camera first. The user drawn outline is on the image plane. Usually the image plane can be choose as $z=-1$. – Shiyu Sep 9 '11 at 1:22
• Ah sorry Shiya - yes, of course the transformation will involve a translate away from the camera. You're right though when you say "The user drawn outline is the perspective projection of the 3D rectangle". This is the problem I'm attempting to solve. Please feel free to ignore the other specifications if you know how to solve this problem! – Groky Sep 9 '11 at 8:24

As Shiyu mentions in the comments, this is a well-studied computer vision problem. A key term for search is homography. E.g., here is the Wikipedia article. This topic is covered in most textbooks on computer vision. Here is one: Geometric Computation for Machine Vision by Kenichi Kanatani. See especially p.59, where he provides an algorithm for "camera registration" (another key search term) whose first step is: "Take an image of a rectangle placed in the scene." Which is exactly your situation.

• Thanks for the search term! Without knowing what something's called it's difficult to research! – Groky Sep 9 '11 at 16:01
• Another key term is "rectification" and in this case I believe "metric rectification" specifically. – hippietrail Aug 24 '17 at 3:00

In QuickDraw GX, the graphics system was $2D$, but used $3\times3$ perspective matrices for transformations.

A $2D$ point is imbedded in $3D$: $$[x,y]\mapsto[x,y,1]$$ and a $3D$ point is projected to $2D$: $$[x,y,z]\mapsto\left[\frac{x}{z},\frac{y}{z}\right]$$ A perspective mapping imbeds a $2D$ point in $3D$, performs a $3\times3$ matrix multiplication, then projects the $3D$ result back to $2D$: $$M:\left[\begin{array}{cc}x&y\end{array}\right]\mapsto\left[\begin{array}{ccc}x&y&1\end{array}\right]M=\left[\begin{array}{ccc}u&v&w\end{array}\right]\mapsto\left[\begin{array}{cc}\frac{u}{w}&\frac{v}{w}\end{array}\right]$$ Since this is a perspective mapping, it can map any $4$ points (no $3$ of which are collinear) to any $4$ points. Given $4$ points $[x_n,y_n]_{n=1}^4$, compute $$\left[\begin{array}{ccc}d_1&d_2&d_3\end{array}\right]=\left[\begin{array}{ccc}x_4&y_4&1\end{array}\right]\left[\begin{array}{ccc}x_1&y_1&1\\x_2&y_2&1\\x_3&y_3&1\end{array}\right]^{-1}$$ and define $$M_{[x\;y]}=\left[\begin{array}{ccc}d_1x_1&d_1y_1&d_1\\d_2x_2&d_2y_2&d_2\\d_3x_3&d_3y_3&d_3\end{array}\right]$$ Then, for $4$ other points $[u_n,v_n]_{n=1}^4$, we have $$M_{[x\;y]}^{-1}M_{[u\;v]}:\left[\begin{array}{cc}x_n&y_n\end{array}\right]\mapsto\left[\begin{array}{cc}u_n&v_n\end{array}\right]$$ Now to apply this to your problem. Using the rectangle and quadrilateral mentioned above, generate the $3\times3$ perspective mapping taking the rectangle to the quadrilateral and take the cross product of the first two rows. That will be the perpendicular to the plane for the quadrilateral. However, without more information about the positioning in $3D$, I don't think you can get much more.

• Thank you! I'll try to get my head around that and see what I can come up with. – Groky Sep 9 '11 at 16:02
• Do you really prove here that the perspective mapping has an inversive perspective mapping? – hhh Sep 13 '12 at 22:09
• You seem to be using Homogenous Coordinates, more here or here. – hhh Sep 13 '12 at 22:17
• @hhh: No, but if no $3$ of the points is collinear, $M_{[x\;y]}$ will be invertible. If you look at the definition of $\left[\begin{array}{ccc}d_1&d_2&d_3\end{array}\right]$ you will see that if any of the $d_k$ are zero, then $[x_4\;y_4]$ is collinear with two of the three other points. If none of the $d_k$ are zero, then the rows of $M_{[x\;y]}$ are dependent exactly when the three other points are collinear. – robjohn Sep 13 '12 at 22:26
• @hhh: yes, that is precisely what I am using. My matrices are transposed from the ones in those references. – robjohn Sep 13 '12 at 22:28