Homogeneous Transformation Module

These Module allow us to allows us to transform objects within our 3D space. This module combines translation, rotation, and scaling into a single matrix operation.

In this module we are dealing with the following code snippet of the Engine Loop:

main method
1def main(self):
2
3    ...
4
5    self.V_T_C, self.C_T_V, self.V_T_Cube = Matrix_Functions.homogeneous_transformation(self.window)
6    camera_vector_world = self.camera_model.get_camera_vectors(self.V_T_C)
7
8    ...

  • Translation Matrix: Moves the object in 3D space by shifting its position along the x, y, and z axes.

    ../_images/translation_matrix.png
  • Rotation Matrices: Three separate matrices for rotating the object around the roll, pitch, and yaw axes (corresponding to rotations around the x, y, and z axes).

    ../_images/real_rotation.png
  • Scale Matrix: Scales the object uniformly along all axes, which is especially useful for resizing objects in a 3D scene.

    ../_images/scaling_matrix.png

The following code snippets implement the creation of a homogeneous transformation matrix and its application to a camera system and a 3D cube:

create_homogeneous_transformation_matrix()
Matrix_Functions class
 1def create_homogeneous_transformation_matrix(
 2        translation_x: float, translation_y: float, translation_z: float,
 3        rotation_roll: float, rotation_pitch: float, rotation_yaw: float,
 4        scale: int) -> np.array:
 5
 6    rotation_matrix_roll = np.array([
 7        [1, 0, 0, 0],
 8        [0, cos(rotation_roll), -sin(rotation_roll), 0],
 9        [0, sin(rotation_roll), cos(rotation_roll), 0],
10        [0, 0, 0, 1]
11    ])
12
13    rotation_matrix_pitch = np.array([
14        [cos(rotation_pitch), 0, sin(rotation_pitch), 0],
15        [0, 1, 0, 0],
16        [-sin(rotation_pitch), 0, cos(rotation_pitch), 0],
17        [0, 0, 0, 1]
18    ])
19
20    rotation_matrix_yaw = np.array([
21        [cos(rotation_yaw), -sin(rotation_yaw), 0, 0],
22        [sin(rotation_yaw), cos(rotation_yaw), 0, 0],
23        [0, 0, 1, 0],
24        [0, 0, 0, 1]
25    ])
26
27    translation_matrix = np.array([
28        [1, 0, 0, translation_x],
29        [0, 1, 0, translation_y],
30        [0, 0, 1, translation_z],
31        [0, 0, 0, 1]
32    ])
33
34    if scale == 0:
35        scale = 1
36
37    scale_matrix = np.array([
38        [scale, 0, 0, 0],
39        [0, scale, 0, 0],
40        [0, 0, scale, 0],
41        [0, 0, 0, 1]
42    ])
43
44    transformation_matrix = np.matmul(
45        translation_matrix,
46        np.matmul(
47            scale_matrix,
48            np.matmul(
49                rotation_matrix_yaw,
50                np.matmul(rotation_matrix_pitch, rotation_matrix_roll)
51            )
52        )
53    )
54    return transformation_matrix

These matrices are multiplied in a specific order to generate one final transformation matrix.

Each time the loop runs, it calls the homogeneous_transformation method. This method grabs the slider/input parameters and uses them as arguments to create an instance of the transformation matrix (e.g., V_T_C, C_T_V, V_T_Cube).


homogeneous_transformation Method()
Matrix_Functions class
 1def homogeneous_transformation(cls, window):
 2V_T_C = cls.create_homogeneous_transformation_matrix(
 3    (window.get_camera_system_translation_x() - 10000) / 1000.0,
 4    (window.get_camera_system_translation_y() - 10000) / 1000.0,
 5    (window.get_camera_system_translation_z() - 10000) / 1000.0,
 6    cls.DEG_TO_RAD(window.get_camera_system_rotation_roll() / 10.0),
 7    cls.DEG_TO_RAD(window.get_camera_system_rotation_pitch() / 10.0),
 8    cls.DEG_TO_RAD(window.get_camera_system_rotation_yaw() / 10.0),
 9    1
10)
11
12
13C_T_V = np.linalg.inv(V_T_C)
14
15V_T_Cube = cls.create_homogeneous_transformation_matrix(
16    (window.get_cube_system_translation_x() - 10000) / 1000.0,
17    (window.get_cube_system_translation_y() - 10000) / 1000.0,
18    (window.get_cube_system_translation_z() - 10000) / 1000.0,
19    cls.DEG_TO_RAD(window.get_cube_system_rotation_roll() / 10.0),
20    cls.DEG_TO_RAD(window.get_cube_system_rotation_pitch() / 10.0),
21    cls.DEG_TO_RAD(window.get_cube_system_rotation_yaw() / 10.0),
22    window.get_cube_system_scale()
23)
24
25return V_T_C, C_T_V, V_T_Cube

This class method generates the homogeneous transformation matrix to both the camera system and a 3D cube within the scene.

  • Camera System: The camera’s transformation matrix (V_T_C) is used to position and rotate the camera but also to convert world space points into camera space. The inverse matrix (C_T_V) allows objects to be transformed from camera space back to world space.

  • Object Transformation : The cube’s transformation matrix (V_T_Cube) is applied to the 3D cube to position, rotate, and scale it within the scene relative to the camera’s viewpoint. It is also later used to convert points from world space to cube space, and cube to world space using its inverse.