Aloha.zone.io

Aloha's technical notes.

View on GitHub

Transformation & Rasterization

Transformation

Basic 2D transformations

Homogeneous coordinates

A translation is different from linear transformation. Now it must be two matrix for combining them:

A matrix with both linear transformation and translation.

But we want one matrix to express them together. Now we introduce a new tool: homogeneous coordinates.

Defintion

To be simply, homogeneous coordinate works by adding an extra dimention to matrix, for describing higher dimention caused transformation, like projection. In other word, an n-dimention space will be described by n+1-dimention matrices.

We are discussing 2D transformation, so we express them in ternary form:

Accordingly, the transformation matrix will be 3D matrix as well:

Transform matrix in homogeneous coordinates:

The homogeneous transform grants us the characteristics below:

  1. “w” value will not be changed for vectors.
Translate vector :
  1. Vector + Vector = Vector
    • [x1, y1, 0] + [x2, y2, 0] = [x1 + x2, y1 + y2, 0]
  2. Point - Point = Vector
    • [x1, y1, 1] - [x2, y2, 1] = [x1 - x2, y1 - y2, 0]
  3. Point + Vector = Point
    • [x1, y1, 1] + [x2, y2, 0] = [x1 - x2, y1 - y2, 1]
Affine transformation

Applied homogeneous matrix, we have a transformation named “Affine transformation”.

Affine transformation:
With this utility, we can have expression the above three transformations in homogeneous style:

Of cource, the three transformations can be combined in one.

Transformation chaining

Matrix multiply is left calculation, like Matrix_1 x vector.

Calculation order is right to left:

M4 x M3 x M2 x M1 x vector

From M1, M2, M3, M4… Or calulate with associative law.

(M4 x M3 x M2 x M1) x vector

-> M5 x vector

In practise

Affine transformation matrix for 2D transformation is useful. In UI interface for drawing element, we typically pass this matrix for apply trasformation.

In the 3D transformation matrix, the 3rd row will always be [0, 0, 1], it does not need to be told as parameter.

So the transformation matrix in 2D drawing functions will typically be like:

[A, B, C, D, Tx, Ty]

For 3D

Affine transformation in 3D is just similar with 2D. Just make the transformation matrix to be 4D.

Viewing transformation

Viewport transformation

Project transformation

Perspective transformation

TBD


Rasterization

Resolution

Raster (Bitmap) vs. Vector

Zooming raster vs. vector

Raster (bitmap)

Bitmap structure example:

Vector

Comparison

When to use?

SVG vs. canvas

SVG, AKA Scalable Vector Graphics.

Canvas element.

Performance comparison:

Basic rasterization

What is rasterization? It is kind of a sampling.

An simple example: just imagine some colorful ink float on water, which show as a painting. Now we have a screen window, sink it under the water and slowly take it out. Then we’ll have a painted screen window, which is similar to rasterization.

Bresenham’s line algorithm is the most basic rasterization algorithm for drawing lines (primitive, vector) as bitmaps.

Illustration of the result of Bresenham's line algorithm.

Antialiasing

Theory

Antialisaing

Font rasterization

Refer to Font rasterization on Wiki.

Fonts are now vectors, then they could be scaled from extreme small to very large. Rasterization for fonts are map a vector path to a pixel matrix.

rasterization

Raw rasterized font without antialiasing:

Sample

Antialiasing in font

Nowadays, the most popular font rasterization library which supports both Windows and macOS is FreeType