Geometric Transformations

This notebook introduces 2D and 3D geometrical transformations which are used in computer graphics.

Farhad Kamangar 2017

Transformations

One of the common and tasks in computer graphics is to change the position, orientation, or the size of the objects within the graphical scene. It is also very common to transform the coordinates from one coordinate system to another (for example from the world coordinates to the camera coordinates or to screen coordinates. These transformations can be efficiently handled using some matrix representations.

2-Dimensional Transformations

2D Translation

A point $P = \left[ {\matrix{ x \cr y \cr } } \right]$ in 2D space can be translated (moved) by $d_x$ in the $x$ direction and $d_y$ in the $y$ direction. The coordinates of the point after the translation will be: $P = \left[ {\matrix{ x+d_x \cr y+d_y \cr } } \right]$.

The translation operation can be shown in matrix form as: $$\left[ {\matrix{d_x \cr d_y \cr } } \right] + \left[ {\matrix{ {x} \cr {y} \cr } } \right] = \left[ {\matrix{ {x+d_x} \cr {y+d_y} \cr } } \right]$$

2D Scale

A point $P = \left[ {\matrix{ x \cr y \cr } } \right]$ in 2D space can be scaled by $s_x$ in the $x$ direction and $s_y$ in the $y$ direction. The coordinates of the point after the scaling will be: $P = \left[ {\matrix{s_x x \cr s_y y \cr } } \right]$.

The scaling operation can be shown in matrix form as: $$\left[ {\matrix{ {{s_x}} & 0 \cr 0 & {{s_y}} \cr } } \right]\left[ {\matrix{ x \cr y \cr } } \right] = \left[ {\matrix{ {{s_x}x} \cr {{s_y}y} \cr } } \right]$$

Example

Consider the triangle shown below. If we scale the coordinates of the triangle vertices by 3 in the $x$ direction and by 2 in the $y$ direction then the scale matrix will be: $ S = \left[ {\matrix{ 3 & 0 \cr 0 & 2 \cr } } \right]$

Now if we multiply the coordinates of each vertex of the triangle by the scale matrix then we get the large triangle as shown in below. Note the coordinates are pre-multiplied by the scale matrix. Also notice that the triangle appears to move and get larger. $$\left[ {\matrix{ 3 & 0 \cr 0 & 2 \cr } } \right]\left[ {\matrix{ x \cr y \cr } } \right] = \left[ {\matrix{ {3x} \cr {2y} \cr } } \right]$$

2D Rotation

A point $P = \left[ {\matrix{ x \cr y \cr } } \right]$ in 2D space can be rotated around the origin by $\alpha$ degrees.

Assuming that the positive rotation is counter-clockwise, The coordinates of the point after the rotation will be: $\left[ {\matrix{ {\cos (\alpha )x - \sin (\alpha )y} \cr {\sin (\alpha ) + \cos (\alpha )y} \cr } } \right]$

The rotation operation can be shown in matrix form as: $\left[ {\matrix{ {\cos (\alpha )} & { - \sin (\alpha )} \cr {\sin (\alpha )} & {\cos (\alpha )} \cr } } \right]\left[ {\matrix{ x \cr y \cr } } \right] = \left[ {\matrix{ {\cos (\alpha )x - \sin (\alpha )y} \cr {\sin (\alpha ) + \cos (\alpha )y} \cr } } \right]$

The matrix $R = \left[ {\matrix{ {\cos (\alpha )} & { - \sin (\alpha )} \cr {\sin (\alpha )} & {\cos (\alpha )} \cr } } \right]$ is the rotation matrix.

Example

Consider the triangle shown below. If we rotate the vertices of the triangle by 45 degrees around the origin then the rotation matrix will be:
$R=\left[ {\matrix{ {\cos (45)} & { - \sin (45)} \cr {\sin (45)} & {\cos (45)} \cr } } \right]$

The new coordinates of the vertices will be: $\left[ {\matrix{ {\cos (45)} & { - \sin (45)} \cr {\sin (45)} & {\cos (45)} \cr } } \right]\left[ {\matrix{ x \cr y \cr } } \right] = \left[ {\matrix{ {\cos (45)x - \sin (45)y} \cr {\sin (45)x + \cos (45)y} \cr } } \right]$

Unlike rotation and scale transforms, we need to add the translation matrix instead of multiplying. In order to be able to uniformly multiply all the transformation matrices, we use the homogeneous coordinate system.

Homogeneous Coordinates

Homogeneous coordinates which was introduced by August Ferdinand Möbius make geometrical transformations more efficient and allows composite transforms to be presented by a single matrix.

To represent a $P = \left[ {\matrix{ x \cr y \cr } } \right]$ in homogeneous coordinates, we simply add an additional variable, $w$ and represent the point as $P = \left[ {\matrix{ wx \cr wy \cr w } } \right]$.

To convert the homogenous coordinate back to standard Cartesian coordinate, simply divide the coordinates by the last coordinate $w$. Most of the times $w=1$, in which case we simply drop the third element of the homogenous coordinates.

Example:

Point $P = \left[ {\matrix{ 5 \cr 3 \cr } } \right]$ in Cartesian coordinates can be presented in the homogenous coordinates as $P = \left[ {\matrix{ 10 \cr 6 \cr 2} } \right]$ or $P = \left[ {\matrix{ 25 \cr 15 \cr 5} } \right]$ or $P = \left[ {\matrix{ 5 \cr 3 \cr 1} } \right]$

2-Dimensional Geometrical Transformations in Homogeneous Coordinates

In the homogeneous coordinates all the geometrical transformations, including translation, can be presented as matrix multiplication

Translation

$$\left[ {\matrix{ 1 & 0 & {d_x} \cr 0 & 1 & {d_y} \cr 0 & 0 & 1 \cr } } \right]\left[ {\matrix{ x \cr y \cr 1 \cr } } \right] = \left[ {\matrix{ {x + d_x} \cr {y + d_y} \cr 1 \cr } } \right]$$

Scale

$$\left[ {\matrix{ {s_x} & 0 & 0 \cr 0 & {s_y} & 0 \cr 0 & 0 & 1 \cr } } \right]\left[ {\matrix{ x \cr y \cr 1 \cr } } \right] = \left[ {\matrix{ {s_xx} \cr {s_yy} \cr 1 \cr } } \right]$$

Rotation

$$\left[ {\matrix{ {\cos (\alpha )} & { - \sin (\alpha )} & 0 \cr {\sin (\alpha )} & {\cos (\alpha )} & 0 \cr 0 & 0 & 1 \cr } } \right]\left[ {\matrix{ x \cr y \cr 1 \cr } } \right] = \left[ {\matrix{ {\cos (\alpha )x - \sin (\alpha )y} \cr {\sin (\alpha ) + \cos (\alpha )y} \cr 1 \cr } } \right]$$

3-Dimensional Transformations

All the 2-dimensional concepts in the previous sections can be extended to 3-dimensional geometrical transformations.

3D Translation

A point $P = \left[ {\matrix{ x \cr y \cr z \cr } } \right]$ in 3D space can be translated (moved) by $d_x$ in the $x$ direction, $d_y$ in the $y$ direction, $d_z$ in the $z$ direction. The coordinates of the point after the translation will be: $P = \left[ {\matrix{ x+d_x \cr y+d_y \cr z+d_z} } \right]$.

The 3d translation operation in the homogenous coordinates can be shown in matrix form as: $\left[ {\matrix{ 1 & 0 & 0 & {dx} \cr 0 & 1 & 0 & {dy} \cr 0 & 0 & 1 & {dz} \cr 0 & 0 & 0 & 1 \cr } } \right]\left[ {\matrix{ x \cr y \cr z \cr 1 \cr } } \right] = \left[ {\matrix{ {x + dx} \cr {y + dy} \cr {z + dz} \cr 1 \cr } } \right]$

In [1]:
import numpy as np
# Original point
P=np.array([[4],[5],[6],[1]])
# Define translations
dx=1
dy=20
dz=3
# Define translation matrix
TM=np.array([[1,0,0,dx],[0,1,0,dy],[0,0,1,dz],[0,0,0,1]])
# Translate the point
PP=TM.dot(P)
# Print the result
print("Original point : ",P)
print("Coordinates of the point after translation : ",PP)
Original point :  [[4]
 [5]
 [6]
 [1]]
Coordinates of the point after translation :  [[ 5]
 [25]
 [ 9]
 [ 1]]

3D Scale

A point $P = \left[ {\matrix{ x \cr y \cr z } } \right]$ in 3D space can be scaled by $s_x$ in the $x$ direction, $s_y$ in the $y$ direction, and $s_z$ in the $z$ direction. The coordinates of the point after the scaling will be: $P = \left[ {\matrix{s_x x \cr s_y y \cr s_z z} } \right]$.

The 3d scaling operation in homogenous coordinates can be shown in matrix form as: $\left[ {\matrix{ {{s_x}} & 0 & 0 & 0 \cr 0 & {{s_y}} & 0 & 0 \cr 0 & 0 & {{s_z}} & 0 \cr 0 & 0 & 0 & 1 \cr } } \right]\left[ {\matrix{ x \cr y \cr z \cr 1 \cr } } \right] = \left[ {\matrix{ {{s_x}x} \cr {{s_y}y} \cr {{s_z}z} \cr 1 \cr } } \right]$

3D Rotations

A point $P = \left[ {\matrix{ x \cr y \cr z } } \right]$ in 3D space can rotate around an arbitrary line. This kind of rotation can be represented by a composite matrix , which is the combination of simple transformation matrices. Simple rotation matrices are defined when the point is rotated around the 3 major axes. i.e. x-axis, y-axis, or z-axis. As a convention, the positive direction is the counterclockwise for an observer looking from the positive infinity of the corresponding axis

Rotation around the $x$ axis

A point $p$ in 3D space can be rotated around the $x$ axis by $\theta$ degrees. This rotation can be presented by pre-multiplying the point in the homogenous coordinates by the rotation matrix.

The rotation matrix around the $x$ axis is defined as: $R_x(\theta ) = \left[ {\matrix{ 1 & 0 & 0 & 0 \cr 0 & {\cos \theta } & { - \sin \theta } & 0 \cr 0 & {\sin \theta } & {\cos \theta } & 0 \cr 0 & 0 & 0 & 1 \cr } } \right]$

Notice that after the rotation the $x$ component of the point remains the same. The positive direction is the counterclockwise for an observer looking from the positive infinity of the $x$ axis.

Rotation around the $y$ axis

A point $p$ in 3D space can be rotated around the $y$ axis by $\theta$ degrees. This rotation can be presented by pre-multiplying the point in the homogenous coordinates by the rotation matrix.

The rotation matrix around the $y$ axis is defined as: $R_y(\theta ) = \left[ {\matrix{ {\cos \theta } & 0 & {\sin \theta } & 0 \cr 0 & 1 & 0 & 0 \cr { - \sin \theta } & 0 & {\cos \theta } & 0 \cr 0 & 0 & 0 & 1 \cr } } \right]$

Notice that after the rotation the $y$ component of the point remains the same. The positive direction is the counterclockwise for an observer looking from the positive infinity of the $y$ axis.

Rotation around the $z$ axis

A point $p$ in 3D space can be rotated around the $z$ axis by $\theta$ degrees. This rotation can be presented by pre-multiplying the point in the homogenous coordinates by the rotation matrix.

The rotation matrix around the $z$ axis is defined as: $Rz(\theta ) = \left[ {\matrix{ {\cos \theta } & { - \sin \theta } & 0 & 0 \cr {\sin \theta } & {\cos \theta } & 0 & 0 \cr 0 & 0 & 1 & 0 \cr 0 & 0 & 0 & 1 \cr } } \right]$

Notice that after the rotation the $z$ component of the point remains the same. The positive direction is the counterclockwise for an observer looking from the positive infinity of the $z$ axis.

3D Shear

A shear is a linear transformation that moves a point in a direction parallel to an axis. The amount of movement is proportional to the distance of the point from a given plane.

Shear transformation is different from rotation. A rotation does not change the angle between the lines and it will not change the length of the line segments, however, a shear transformation changes the angle between the lines and also the length of the line segments which are not parallel to the direction of shear will change.

There are 6 different shear matrices with respect to the $x$, $y$, and $z$ axis in 3d space.

Shear Parallel to $x$ proportional to the Distance from $xy$ plane :

The general form of the shear matrix which moves a point parallel to $x$ proportional to the distance from $xy$ plane is: $$S{H_{x_{xy}}} = \left[ {\matrix{ 1 & 0 & c & 0 \cr 0 & 1 & 0 & 0 \cr 0 & 0 & 1 & 0 \cr 0 & 0 & 0 & 1 \cr } } \right]$$

Example: Given a point $P = \left[ {\matrix{ x \cr y \cr z } } \right]$

The coordinates of point $P$ after it has been sheared by the shear matrix $SH_{x_{xy}}$ will be:

$$ \left[ {\matrix{ 1 & 0 & c & 0 \cr 0 & 1 & 0 & 0 \cr 0 & 0 & 1 & 0 \cr 0 & 0 & 0 & 1 \cr } } \right]\left[ {\matrix{ x \cr y \cr z \cr 1 } } \right]=\left[ {\matrix{ x+c*z \cr y \cr z \cr 1 } } \right]$$

Notice that after the shear only the $x$ coordinate of the point has changed (because the point only moves parallel to the $x$ axis. Also notice that the amount of movement of the point is proportional to the $z$ coordinates of the point.

Shear Parallel to $x$ proportional to the Distance from $xz$ plane :

$$S{H_{x_{xz}}} = \left[ {\matrix{ 1 & c & 0 & 0 \cr 0 & 1 & 0 & 0 \cr 0 & 0 & 1 & 0 \cr 0 & 0 & 0 & 1 \cr } } \right]$$

Notice that this shear,$S{H_{x_{xz}}}$, moves a point parallel to the $x$ axis and the amount of the movement will be proportional to the distance from the $xz$ plane (which is the $y$ coordinate of the point).

Shear Parallel to $y$ proportional to the Distance from $yz$ plane :

$$S{H_{y_{yz}}} = \left[ {\matrix{ 1 & 0 & 0 & 0 \cr c & 1 & 0 & 0 \cr 0 & 0 & 1 & 0 \cr 0 & 0 & 0 & 1 \cr } } \right]$$

Notice that this shear,$S{H_{y_{yz}}}$, moves a point parallel to the $y$ axis and the amount of the movement will be proportional to the distance from the $yz$ plane (which is the $x$ coordinate of the point).

Shear Parallel to $y$ proportional to the Distance from $xy$ plane :

$$S{H_{y_{xy}}} = \left[ {\matrix{ 1 & 0 & 0 & 0 \cr 0 & 1 & c & 0 \cr 0 & 0 & 1 & 0 \cr 0 & 0 & 0 & 1 \cr } } \right]$$

This transformation, i.e. $S{H_{y_{xy}}}$, moves a point parallel to the $y$ axis and the amount of the movement will be proportional to the distance from the $xy$ plane (which is the $z$ coordinate of the point).

Shear Parallel to $z$ proportional to the Distance from $xz$ plane :

$$S{H_{z_{xz}}} = \left[ {\matrix{ 1 & 0 & 0 & 0 \cr 0 & 1 & 0 & 0 \cr 0 & c & 1 & 0 \cr 0 & 0 & 0 & 1 \cr } } \right]$$

This transformation, i.e. $S{H_{z_{xz}}}$, moves a point parallel to the $z$ axis and the amount of the movement will be proportional to the distance from the $xz$ plane (which is the $y$ coordinate of the point).

Shear Parallel to $z$ proportional to the Distance from $yz$ plane :

$$S{H_{z_{xz}}} = \left[ {\matrix{ 1 & 0 & 0 & 0 \cr 0 & 1 & 0 & 0 \cr c & 0 & 1 & 0 \cr 0 & 0 & 0 & 1 \cr } } \right]$$

This transformation, i.e. $S{H_{z_{yz}}}$, moves a point parallel to the $z$ axis and the amount of the movement will be proportional to the distance from the $yz$ plane (which is the $x$ coordinate of the point).