In the last week our Computer Graphics class discussed how to infer projective mappings using a geometric approach. In this post I will show how the inferring can be done in a linear algebra way.

The main reference I use is the paper “Fundamentals of texture mapping and image warping”1. And I use Mathematica for symbolic computation.

Theoretical background

In the section 2.2.3 of the paper1, a linear system is constructed to solve for elements of a projective mapping in 2D. This can be extended to 3D easily.

Let us denote the projective mapping to be determined as following:

Suppose we have a point \( (x, y, z) \) in view space, and a point \( (u, v, w) \) in NDC space, the equations can be written as following:

We apply these equations on all 8 vertices of the frustum, then construct and solve the linear system for the projective mapping \( \mathbf{M} \).

Implementation

First we build the \( \mathbf{M} \), and an array of symbolic variables are stored in v for later use:

M = Table[Symbol["m" <> ToString[i]], {i, 0, 15}];
v = Flatten[M];

Then we construct matrices for points in view and NDC spaces:

k = f/ n;

X = {
   {l, t, -n, 1},
   {l, b, -n, 1},
   {r, b, -n, 1},
   {r, t, -n, 1},
   {k l, k t, -f, 1},
   {k l, k b, -f, 1},
   {k r, k b, -f, 1},
   {k r, k t, -f, 1}};

U = {
   {-1, 1, -1},
   {-1, -1, -1},
   {1, -1, -1},
   {1, 1, -1},
   {-1, 1, 1},
   {-1, -1, 1},
   {1, -1, 1},
   {1, 1, 1}};

Note that we use a scale factor k for the frustum in perspective projection. For the orthographic projection k should be \( 1 \).

Next we define a function to generate one constraint for our linear system:

Generate[{i_, j_}] := M[[j]].X[[i]] - U[[i, j]] M[[4]].X[[i]] == 0;

Now all data and tools are ready. We start to generate all constraints, and solve it.

c = Map[Generate, Table[{i, j}, {i, 1, 8}, {j, 1, 3}], {2}];
c = Flatten[c];

s = Solve[c, v];
ss = Simplify[s];
fss = Flatten[ss];
MatrixForm[M /. fss]

The final result is:

Note that the solution is not unique, and it is parameterized by a single scalar(\( m_0 \) in our case). This non-uniqueness can be seen from that we can multiply arbitrary non-zero factor on a point in homogeneous space without changing its meaning.

By letting \( m_0 \) equal \( \frac{2 n}{r - l} \), we can get the conventional perspective projection matrix (right-handed, and maps to \( [-1, 1]^3 \), with positive \(n\) and \(f\)):

References

  1. Heckbert, Paul S. Fundamentals of texture mapping and image warping. MS thesis. University of California, Berkeley, 1989. 2