ashen-aetna

Ashen Aetna

— Rustily stumbling around on an ash-covered volcano

(A tutorial on/in/about/with 3D graphics, Rust, Vulkan, ash)

Not so linear

Translations

We have just seen how we can so nicely write linear transformations in form of matrices. The problem: Some of the important functions are not linear. The worst offender: Shifting (translation) by a constant (non-zero) vector, say: shifting by , that is

Why is that not linear? Well, check the previous chapter for the definition: . That fails, for example for :

Or, even easier to check: (where is short for , and where we note that every linear map sends to ). If this map is not linear, we cannot write it as a matrix. But we want to! (Matrices are well-understood, convenient, there are crates we can use for computations involving them …)

What do we do? We use a clever trick. We use vectors with four components (of course, you could have guessed this: After all, it was the mysterious fourth component that set us on our current tangent): quadruplets of numbers would have worked as well as the triplets we used in the introduction of vector spaces, and we can easily work with those and matrices of size 4x4 instead of 3x3. And then we write every vector as

Yep, always with a in the last component.

(Note: the set of all these vectors, is not a vector space (if we add two of its elements, we leave the set, because the last component becomes different from 1) — but that’s not a problem. It’s a subset of , and that is a vector space.)

Every 3x3-matrix can be seen as a 4x4 matrix.

Say, we have . Then the 4x4-matrix is , which supposed to be short for:

Let’s check what happens if we multiply this by the vector , that is by :

Indeed, we obtain that four-component vector which corresponds to the result of in the three-component setting.

In other words: We have not lost anything. Has something become better?

Oh! Have a look at the following:

That’s a matrix describing exactly the shift that we have identified as not-a-matrix in the beginning of this chapter!

As you can imagine, shifting things around (say, moving some 3D model to a different position) is not entirely unimportant in a videogame, for example. So, this is great and we should start liking 4x4-matrices better than 3x3-matrices and should write points with three coordinates with four coordinates instead, the last of which is 1.

Why this works, is not so clear from the above. For now, it’s a clever hack. We will consider this in more detail in the next chapter. First, let us have a look at other “not always so linear” maps:

Projections

If you want to draw something such that it is visible that there is some depth to it, you may resort to some perspective projection with all lines that run “into” the image moving closer together and approaching some “vanishing point” on the horizon. You can see a similar effect in photographic images, too: image of train tracks (with thanks to Wikipedia). See the railroad tracks? See how the railroad’s borders come closer together the further “into” the image they move?

We have a problem with that. To see, why, we have to talk about parallel lines.

A line through some point with direction (vector) is the entirety of points that can be reached by adding a multiple of to , in symbols:

or shorter: . Note that this representation is not unique: The same line can be written in different ways. For example, if we set and , then , even though and are different, and so are and . This claim is not that ; rather, it is that for every number we can find a number such that , and vice versa (for every some corresponding ). And that is the case. (Try it. You should end up with and , I hope.)

Two lines are parallel if they have the same direction. Again, this is not (if the lines are written in the form and ), but

. It boils down to “ is a multiple of ”. So, two lines are parallel if (and only if) they can be written in the forms and , with possibly different and , but the same .

Now let us look at what a linear transformation — say, — does with parallel lines ( and ). By its linearity,

that is . In the same way, . Comparing the directions and of these results, we note: Transforming parallel lines by a linear map results in parallel lines.

Going back to the picture: What were clearly parallel lines in reality was turned into clearly non-parallel lines in the photograph. (Lines from approximately the bottom left corner to some point, and from the bottom right corner to the same point. They do not have the same direction.)

That means: Whatever transformation has taken place to turn reality into a photo, it was not a linear map. What a pity. Taking pictures in the same way as a camera sounds like it would be useful for somewhat realistic ways of turning virtual scenes into images on a screen.

Not to be too unfair to projections: There are projections that are linear. For example the one turning into a point by simply ignoring the third component (this was how points in our “box-like” kind-of-screen were interpreted as points on the screen when we were last dealing with graphics and not only with hopefully helpful theory).

But the camera’s perspective projection? Nope, it isn’t. Fortunately for us, we do not even need a new trick to rectify this problem.

Let us try to better understand what is going on with this miraculous additional component in our vectors.

Continue