ĐSTT
ĐSTT
Project Report
MEMBERS
INTRODUCTION
First of all, we want to express such a deep gratitude to our lecturer, who has taken charge
of Linear Algebra dedicatedly this semester . All the members have gained a lot of knowledge that is
beneficial to our schooling as well as had a great chance to discover such an interesting topic of this
project mutually.
While making a report, it is difficult to avoid errors so we are looking forward to your comments.
Therefore, we can learn from experience for better upcoming reports
ACKNOWLEDGEMENT
In 3D space, you need a minimum of six parameters to fully define a pose (position and ori-
entation). Orientation in space, however, can be represented in several other ways, each with its own
advantages and disadvantages.
Observe that this project is intended as a demonstration of different techniques used for rotation
of a three dimensional object on a computer screen. These representations, some of which will use more
than the necessary minimum of three parameters, but they all stem from the orthogonal matrices, or we
can say rotational matrices, an ideal and common way to create an object’s rotation. In general, they can
be easily composed and applied to objects using matrix multiplication, making them suitable for many
applications in computer graphics, animation, and engineering.
Of course, Matlab has built in procedures to work with 3D objects. We will explore these
procedures at the end of the project as well.
TABLE OF CONTENTS
1 Theoretical Basis 1
1.3 Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Methodology 9
2.2 Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.2 Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1 Theoretical Basis
In the context of 3D rotation matrices, this means that the columns (or rows) of the matrix
represent orthogonal unit vectors that define the coordinate system after the rotation. These vectors are
not rotating vectors, they are the consequence of two coordinate systems rotated with respect to each
other.
Properties
• Every two rows and two columns have a dot product of zero.
We have two coordinate systems A and B, with the coordinates (xA , yA , zA ) and (xB , yB , zB ).
The coordinate system B is rotated with respect to the coordinate system A around the axis z for the
angle θ.
Figure 1: Example 1
Problem: Knowing the coordinates of the vector P in the coordinate system B and the angle
of rotation θ. Find the coordinates of the vector p in the coordinate system A.
Note: pA and pB are actually denoting the same vector P but expressed in different coordinate
systems.
pxA pxB
pA = pyA pB = pyB
pzA pzB
↔ pA = RB
A
· pB
A
The matrix RB is the rotation matrix, which means that the rotation matrix transforms
projections of a vector from the coordinate system B into the coordinate system A.
The figure below shows the top views of the unit vectors of the coordinate systems A and B.
iB · iA = cos θ, jB · iA = − sin θ, kB · iA = 0
iB · jA = sin θ, jB · jA = cos θ, kB · jA = 0
iB · kA = 0, jB · kA = 0, kB · kA = 1
A
Hence, our rotation matrix RB now becomes:
cos θ − sin θ 0
A
Rz = RB =
sin θ cos θ 0
0 0 1
Note: There is the same demonstration for X and Y-Axis Rotation Matrix, so we get:
1 0 0 cos θ 0 sin θ
Rx = − sin θ Ry =
0 cos θ 0 1 0
0 sin θ cos θ − sin θ 0 cos θ
The Euler Angles are three angles introduced by Leonhard Euler to describe the orientation
of a rigid body with respect to a fixed coordinate system (x, y, z). To be more specific, this is a sequence
of rotations with respect to some fixed or translating frame that is used to uniquely define the current
rotational state (orientation) of the body.
There are several approaches for selecting parameters that describe the orientation of a rigid
body, and one of them is to use the Euler angles. In general, they are typically denoted as (θ1 , θ2 , θ3 ) or
(roll, pitch, yaw), depending on each author that different set of names are used.
Euler angles are defined as follows: Consider 2 Cartesian right-handed 3D reference frames, of
which one is called the fixed frame and the other is the mobile frame. These two frames overlap initially.
Hence, the orientation of the third frame is defined as follows:
1. First, Yaw rotation around Z by θ1 degrees. This rotation of the cube defines a new coordinate
system X ′ Y ′ Z ′ .
3. Finally, Roll rotation around X ′′ by θ3 degrees. This rotation defines the final coordinate system
X ′′′ Y ′′′ Z ′′′
So, our Fixed frame is denoted by (x, y, z) and Mobile frame is denoted by (X, Y, Z). After the
third rotation, we will get the corresponding final coordinate system (X ′′′ , Y ′′′ , Z ′′′ ), which is also our
3D object’s orientation.
Convention
In addition, the sequential order of the three rotations is supremely important. There are
216(63 ) possible sequences that we can have in total. However, a sequence of three rotations in which two
consecutive rotations are about the same axis (e.g., y → y → z) cannot describe a general orientation.
This results in a mathematical issue known as "Gimbal lock." Gimbal lock occurs when one
axis of rotation becomes aligned with another, leading to a loss of one degree of freedom in describing
the orientation.
In the case of y → y → z, after the second rotation around the Y-axis, the Z-axis becomes
coincident with the Y-axis’ s original orientation . Further rotations around the Z-axis do not change the
orientation because the Y-axis is already aligned with it. Thus, there exist only 12 Euler angle conventions:
cos θ1 − sin θ1 0 cos θ2 0 sin θ2 1 0 0
Rz (θ1 ) = sin θ1 Ry′ (θ2 ) = 0 Rx′′ (θ3 ) = 0 − sin θ3
cos θ1 0 1 0 cos θ3
0 0 1 − sin θ2 0 cos θ2 0 sin θ3 cos θ3
with c(), s() are shortened for cos() and sin() respectively.
1.3 Quaternions
q = q0 + i · q1 + j · q2 + k · q3 (1)
where q0 , q1 , q2 , q3 are real integers, and i, j, and k are mutually orthogonal imaginary unit
vectors. The q0 term is referred to as the "real" component, while the remaining three words are known
as the "imaginary" components. In practice (and for the duration of this work), the imaginary notation
is presumed, and simply the four coefficients are used to express a quaternion, as shown in equation 2:
q = (q0 , q1 , q2 , q3 ) (2)
Properties
√ p
1. The length (magnitude) of a quaternion is |q| = qq ∗ = q02 + q12 + q22 + q32
Step 1: Find the size of each quaternion component. This leaves the sign of each component
undefined.
r
1 + r11 + r22 + r33
|q0 | =
4
r
1 + r11 − r22 − r33
|q1 | =
4
r
1 − r11 + r22 − r33
|q2 | =
4
r
1 − r11 − r22 + r33
|q3 | =
4
Step 2: To solve for the sign, find the maximum value of q0 , q1 , q2 , q3 and assume its sign is
positive. Then calculate the remaining ingredients as shown in the table below. Using the maximum
amount avoids division by small numbers, which would reduce numerical precision.
Table 1
The reason for the sign ambiguity is that every rotation has two possible quaternion represen-
tations. If one is known, the other can be found by taking the negation of all four of her terms. This
reverses both the angle of rotation and the axis of rotation. All rotational quaternions (q0 , q1 , q2 , q3 ) and
(−q0 , − q1 , − q2 , − q3 ) produce identical rotations. To convert from a rotation matrix to a quaternion,
you must arbitrarily choose one of two possible answers, as described in steps 1 and 2.
Euler angles are a complex subject. The main reason is that there are many mutually exclusive
ways to define Euler angles. Different authors are likely to use different conventions without explicitly
stating the underlying assumptions, making it difficult to combine expressions and code from multiple
sources. This article uses the following definition of Euler angles:
• Rotation order for yaw, pitch, and roll, rotation around the z, y, and x axes
• Active rotation (also known as alibi) (points are rotated rather than the coordinate system)
This is a general rule and the easiest to explain to most people. Considering the above definition,
we can convert Euler angles to quaternions as follows:
u v w u v w
q0 = c( )c( )c( ) + s( )s( )s( )
2 2 2 2 2 2
u v w u v w
q1 = s( )c( )c( ) − c( )s( )s( )
2 2 2 2 2 2
u v w u v w
q2 = c( )s( )c( ) + s( )c( )s( )
2 2 2 2 2 2
u v w u v w
q3 = c( )c( )s( ) − s( )s( )c( )
2 2 2 2 2 2
where: u = roll angle; v = pitch angle; w = yaw angle; c(), s() are shortened for cos(), sin()
The above equations work for all values of Euler angles, including gimbal lock conditions where the pitch
angle is +90o or −90o .
2 Methodology
In general, applying Euler Angles to rotate an object in 3D dimension consists of 5 main steps,
in particular
Each rotation can be around one of three axes. Given that successive axes must be different,
there are 12 possible sequences of rotation axes, divided in two groups:
For instance, the convention x-y-z means rotating about the x-axis first, then the y-axis, and
finally the z-axis.
Determine the angles of each rotation around the chosen axis. These angles are typically denoted
as (θ1 , θ2 , θ3 ) . For instance, in the z-y-z convention, these angles (θ1 , θ2 , θ3 ) are equivalent to a set of
rotations by θ1 degrees around the z axis, then θ2 degrees around the y axis, and θ3 degrees around the
z axis again.
To apply Euler Angles to rotate objects in 3D space, it is necessary to represent the orientation
with a 3x3 rotation matrix. A rotation of θ1 about the axis z, a rotation of θ2 about the axis y, and a
rotation of θ3 about the axis x, respectively correspond to the following three rotation matrices:
cos θ1 − sin θ1 0 cos θ2 0 sin θ2 1 0 0
Rz (θ1 ) = sin θ1 Ry′ (θ2 ) = 0 Rx′′ (θ3 ) = 0 − sin θ3
cos θ1 0 1 0 cos θ3
0 0 1 − sin θ2 0 cos θ2 0 sin θ3 cos θ3
The above matrices are referred to as basic rotation matrices. Rather than performing each
elemental rotation separately, we can combine the three rotation matrices above into a single one by
multiplying them in the appropriate order.
Taking the convention XYZ as an example, the first rotation is about the x axis, then write
Rx (θ), where is the angle of rotation. For every subsequent rotation, post-multiply (right multiply)
the current result with the next rotation matrix. For example, the convention ZYX corresponds to the
product R = Rz (θ1 ) · Ry′ (θ2 ) · Rx′′ (θ3 ).
After we get the rotation matrix, we perform a matrix-vector multiplication by right multiplying
the unit vectors of our object one by one with the rotation matrix. The resulting vectors will be the
transformed vectors after applying the rotation specified by the rotation matrix. Or we can combine
three vectors into a coordinate matrix by representing the vectors vertically and left multiply the rotation
matrix.
To reverse a rotation (that is, to return a rotated point to its original coordinate in the reference
frame), simply reverse the order of the rotations, and also change the signs of the three rotation angles.
So if the forward rotation is yaw(w), pitch(v), roll(u), then the inverse rotation is roll(-u),
pitch(-v), yaw(-w).
For instance, we must apply rotations to an object that requires a roll rotation about 45o ,
followed by a 30o pitch rotation, and ends with a 60o yaw rotation
Step 1: Choose Euler angles convention: because the order of rotations is roll then pitch and
Step 2: Define the Euler angles: because roll rotation is about 45o , followed by a 30o pitch
rotation, and ends with a 60o yaw rotation, so the angles are: θ1 = 45o , θ2 = 60o , θ3 = 30o ,
Step 4: Combine Rotation Matrices: According to the XYZ convention, we multiply these
matrices in the order: R = Rx (45) · Ry′ (30) · Rz′′ (60)
Step 5: Apply the rotations: right multiplying the 3x3 initial coordinate matrix with the
rotation matrix, we get the matrix after rotations:
1 0 0
Initial : I=
0 1 0
0 0 1
0.4330 −0.75 0.5
Af terapplyingrotations : RI = −0.6124
0.7891 0.0474
0.4356 0.6597 0.6124
We left to multiply the matrix R = Rz (−60o ) · Ry′ (−30o ) · Rx′′ (−45o ) with the matrix in step
5 to return the initial matrix (initial orientation).
2.2 Quaternions
• Normalization
Convert between quaternion representations and other rotation representations, such as Euler
angles or rotation matrices, when needed. Methods for conversion include:
u v w u v w
q0 = c( )c( )c( ) + s( )s( )s( )(1)
2 2 2 2 2 2
u v w u v w
q1 = s( )c( )c( ) − c( )s( )s( )
2 2 2 2 2 2
u v w u v w
q2 = c( )s( )c( ) + s( )c( )s( )
2 2 2 2 2 2
u v w u v w
q3 = c( )c( )s( ) − s( )s( )c( )(2)
2 2 2 2 2 2
where: u = roll angle; v = pitch angle; w = yaw angle; c(), s() are shortened for cos(), sin()
Note: eq (1) and (2) work for all values of Euler angles, including gimbal lock conditions where
the pitch angle is +90o or −90o .
* Find the magnitude of each quaternion component. This leaves the sign of each component
undefined:
r
1 + r11 + r22 + r33
|q0 | =
4
r
1 + r11 − r22 − r33
|q1 | =
4
r
1 − r11 + r22 − r33
|q2 | =
4
r
1 − r11 − r22 + r33
|q3 | =
4
** To resolve the signs, find the largest of q0, q1, q2, q3 and assume its sign is positive. Then
compute the remaining components as shown in the table below. Taking the largest magnitude avoids
division by small numbers, which would reduce numerical accuracy.
Table 2
The reason for the sign ambiguity is that every rotation has two possible quaternion represen-
tations. If one is known, the other can be found by taking the negation of all four of her terms. This
reverses both the angle of rotation and the axis of rotation. All rotational quaternions (q0 , q1 , q2 , q3 )
and (−q0 , − q1 , − q2 , − q3 ) produce identical rotations
Normalization
By dividing each component of the quaternion by its magnitude to normalize it. Then, verify
that the resulting quaternion is a unit quaternion by confirming its magnitude ( 1) to ensure numerical
stability and accuracy.
Apply Rotations
Apply the quaternion rotation to the object or entity by multiplying the object’s current ori-
entation by the rotation quaternion. This operation effectively transforms the object’s orientation.
For instance, we must apply rotations to an object that requires a roll rotation about 45o ,
followed by a 30o pitch rotation, and ends with a 60o yaw rotation
0.4330 −0.75 0.5
T hisisourrotationmatrix : R= −0.6124
0.7891 0.0474
0.4356 0.6597 0.6124
r
1 + 0.4330 − 0.04774 − 0.6124
|q1 | = = 0.4397
4
r
1 − 0.4330 + 0.04774 − 0.6124
|q2 | = = 0.0224
4
r
1 − 0.4330 − 0.04774 + 0.6124
|q3 | = = 0.5319
4
q1 = 0.4397
q2 = 0.0223
q3 = 0.5320
45 30 60 45 30 60
q0 = c( )c( )c( ) + s( )s( )s( ) = 0.8224
2 2 2 2 2 2
45 30 60 45 30 60
q1 = s( )c( )c( ) − c( )s( )s( ) = 0.2006
2 2 2 2 2 2
45 30 60 45 30 60
q2 = c( )s( )c( ) + s( )c( )s( ) = 0.3919
2 2 2 2 2 2
45 30 60 45 30 60
q3 = c( )c( )s( ) − s( )s( )c( ) = 0.3694
2 2 2 2 2 2
Step 2: Normalization
Rotation matrix:
p
||q|| = 0.72332 + 0.43962 + 0.02332 + 0.53202 ≈ 1
q0 q1 q2 q3
q normalized ( , , , ) = (0.7223, 0.4396, 0.0233, 0.5320)
||q|| ||q|| ||q|| ||q||
Euler angles:
p
||q|| = 0.82242 + 0.20062 + 0.39192 + 0.36942 ≈ 1
q0 q1 q2 q3
q normalized ( , , , ) = (0.8224, 0.2006, 0.3919, 0.3694)
||q|| ||q|| ||q|| ||q||
Finally, for each vertex of the 3D objec/t, convert the vertex position to a quaternion, then we
apply the rotation quaternion using quaternion multiplication.
• Set the vector part of the quaternion to the coordinates of the vertex.
So, the quaternion representation p of a vertex would be p = (0,x,y,z) and Quaternion rotation
requires 2 multiplications.
where: p contains the point/ vertex to be rotated (see step 1); q is a rotation quaternion
(normalized); q 1 is the inverse of q; p’ contains the coordinates of the rotated point/ vertex.
Active rotation is when the point is rotated with respect to the coordinate system, and passive
rotation is when the coordinate system is rotated with respect to the point. The two rotations are opposite
from each other.
Strengths:
Euler angles are intuitive and easy to understand conceptually. They directly represent rotations
around fundamental axes (x, y, z) and provide a simple way to describe the orientation of an object in
3D space.
Moreover, they are easy to work with in basic applications or when a simple rotation represen-
tation is sufficient.
Weakness:
Gimbal lock can happen when we use Euler angles to represent Rotation. This phenomenon
occurs when two of the three axes of a 3D object’s rotation align, resulting in loss of one degree of freedom.
This means that you can no longer rotate the object around the third axis independently, and the object
gets stuck in a fixed orientation.
For example, if you rotate an object by 90 degrees around the X axis, then the Y and Z axes
will be parallel, and you will not be able to rotate the object around the Y axis anymore.
Improvement:
These are mathematical structures that can encode rotations without relying on three angles, and
thus avoid the alignment of axes. However, they are not very intuitive or easy to manipulate, so
you might still prefer to use Euler angles for some tasks.
For example, instead of using XYZ order, you can use ZYX order, which means that you rotate
the object around the Z axis first, then the Y axis, then the X axis. This can avoid some cases of
gimbal lock, but not all of them
For example, constraining the rotation angles to be within ±85 degrees to reduce the risk of gimbal
lock.
3.2 Quaternions
Strengths:
Quaternions are efficient for analyzing situations that involve rotations in R3. Quaternions avoid
singularities when defining rigid body orientation, and therefore avoid the gimbal lock, which makes them
more robust and stable for applications such as computer graphics, robotics, and aerospace. Weakness:
Quaternions can be more complex to understand and work with compared to simpler rep-
resentations like Euler angles. Their mathematical properties, such as quaternion multiplication and
normalization, may require some learning curve for those unfamiliar with them.
Improvement:
Apply another representation of rotation which is more understandable and familiar to solve the
problem such as Euler angles.
Gain insight into the geometric interpretation of quaternions. Create intuitive visualizations and
interactive demonstrations to help understand how quaternion values correspond to 3D rotations.
The following section outlines the code structure and key components implemented in this
project, providing a detailed explanation of the algorithms, logic, and techniques used to achieve the
desired functionality
1 clear
2 clc
Yaw, pitch, and roll are collectively referred to as "Euler angles." They describe the orientation
or rotation of an object in a 3D space relative to its axes:
Yaw: Rotation occurs about the z-axis, meaning rotation occurs clockwise about the axis of
rotation, meaning the x vector rotates towards the right
x cos ψ − sin ψ 0 x0
1
Yaw: y1 = sin ψ
cos ψ 0 y0
z1 0 0 1 z0
1 psi =60;
Pitch: Rotation occurs about the z-axis, meaning rotation occurs clockwise about the axis of rotation,
meaning the x vector rotates towards the right
x cos θ 0 sin θ x
2 1
Pitch: y2 = 0
1 0 y1
z2 − sin θ 0 cos θ z1
1 theta =30;
Pitch: Rotation occurs about the y-axis, meaning positive roll results in clockwise rotation about the
x-axis, meaning the y-vector goes to the right (or down)
x 1 0 0 x
3 2
Pitch: y3 = 0 − sin ϕ y2
cos ϕ
z3 0 sin ϕ cos ϕ z2
1 phi =45;
Compiled Rotation
1 yaw = [ cosd ( psi ) , - sind ( psi ) , 0; sind ( psi ) , cosd ( psi ) , 0; 0 , 0 , 1];
2 pitch = [ cosd ( theta ) , 0 , sind ( theta ) ; 0 , 1 , 0; - sind ( theta ) , 0 , cosd (
theta ) ];
3 roll = [1 , 0 , 0; 0 , cosd ( phi ) , - sind ( phi ) ; 0 , sind ( phi ) , cosd ( phi ) ];
4 % Euler Angle
5 R = roll * pitch * yaw
6 points2 = zeros (3 ,731) ;
7 for i = 1:1:731
8 points2 (: , i ) = R * points1 (: , i ) ;
9 end
10 points2 = transpose ( points2 ) ;
11 figure (4)
12 patchStruct . Vertices = points2 ;
13 patchStruct . Faces = data . ConnectivityList ;
14 patchStruct . FaceColor = 'g ';
15 patch ( patchStruct )
16 axis equal
17 set ( gca , ' Ydir ' , ' reverse ')
18 xlabel ( 'x ')
19 ylabel ( 'y ')
20 zlabel ( 'z ')
21 title ( ' Euler Angle ')
22 view ([ -53.0 23.3])
1 % Quaternion
2 points3 = rotatepoint ( quat2 , points1 ) ;
3 figure (5)
4 patchStruct . Vertices = points3 ;
5 patchStruct . Faces = data . ConnectivityList ;
6 patchStruct . FaceColor = 'g ';
7 patch ( patchStruct )
8 axis equal
9 set ( gca , ' Ydir ' , ' reverse ')
10 xlabel ( 'x ')
11 ylabel ( 'y ')
12 zlabel ( 'z ')
13 title ( ' Quaternion ')
14 view ([ -53.0 23.3])
CONCLUSION
While orthogonal matrices are a useful tool for rotating 3D objects, in which Euler Angles have
many practical advantages, they are not without limitations. Quaternions may be a preferred choice in
situations where these limitations become significant, such as in animation and computer graphics.
REFERENCES
Euler Angles
[1]: Aleksandar Haber. (2023).Clear Graphical and Mathematical Explanation of Euler Angles
https://s.net.vn/Nk4F
[2]: By Ilian Bonev, Ph.D., Eng. How is Orientation in Space Represented with Euler Angles?
https://s.net.vn/d7NZ
[3]:Wikipedia Youtube
https://en.wikipedia.org/wiki/Euler_angles
https://www.youtube.com/watch?v=qrav-c42DrE.
Quaternion
[1]: D. Rose. (May, 2015). Rotation Quaternions, and How to Use Them
https://danceswithcode.net/engineeringnotes/quaternions/quaternions.html