0% found this document useful (0 votes)
5 views

ĐSTT

BTLDSTT

Uploaded by

vietvvv2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

ĐSTT

BTLDSTT

Uploaded by

vietvvv2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

VIETNAM NATIONAL UNIVERSITY HO CHI MINH CITY

HO CHI MINH CITY UNIVERSITY OF TECHNOLOGY


FACULTY OF MECHANICAL ENGINEERING

Linear Algebra (MT1007)

Project Report

INSTRUCTOR: Nguyễn Tiến Dũng


Class: CC12
Group: 10

MEMBERS

Name ID Mail Tasks Contribution


Nguyễn Nhật Quế Nhi 2352869 [email protected] 1.1, 3 20%
Trần Ngọc Quế Trân 2353208 [email protected] 1.3, 2.2 20%
Phạm Kim Ngân 2352795 [email protected] 1.3, 2.2 20%
Võ Anh Việt 2353325 [email protected] 1.2, 2.1 20%
La Hoàng Thiên Hải 2352305 [email protected] 4 20%

Ho Chi Minh City, May 2024


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

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

Thank you so much for taking time to read our report !

Project Report (MT1007) 2023-2024 Page /26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

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.

Project Report (MT1007) 2023-2024 Page /26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

TABLE OF CONTENTS

1 Theoretical Basis 1

1.1 Rotation Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 Euler Angles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1.3 Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.3.1 Convert rotation matrix to quaternion . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.3.2 Convert Euler angles to quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2 Methodology 9

2.1 Euler Angles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2.2 Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

3 Discussion and Extension 17

3.1 Euler Angles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

3.2 Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

4 DATA & CODE Source 19

Project Report (MT1007) 2023-2024 Page /26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

1 Theoretical Basis

1.1 Rotation Matrix

3D - Rotation Matrices (3x3):

A 3D rotation matrix is an orthogonal matrix. An orthogonal matrix is a square matrix whose


rows and columns are orthogonal unit vectors.

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

• AT = A−1 or AAT = AT A = I (The rotation is an orthogonal matrix).

• Every two rows and two columns have a dot product of zero.

• Every row and every column has a magnitude of one.

Here is an example, Z-Axis Rotation Matrix

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

Project Report (MT1007) 2023-2024 Page 1/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

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

The vector expressed in the coordinate system A and B are:

pA = pxA · iA + pyA · jA + pzA · kA

pB = pxB · iB + pyB · jB + pzB · kB

But, pA = pB = p → pxA iA + pyA jA + pzA kA = pxB iB + pyB jB + pzB kB


By scalar multiplying the equation with iA , jA , kA we get:

pxA = pxB iB · iA + pyB jB · iA + pzB kB · iA

pyA = pxB iB · jA + pyB jB · jA + pzB kB · jA

pzA = pxB iB · kA + pyB jB · kA + pzB kB · kA

They can be written in the matrix form as follow:


     
pxA iB · ia jB · iA kB · iA pxB
     
=  iB · ja jB · jA kB · jA 
     
pyA  pyB 
     
pzA iB · ka jB · kA kB · kA 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.

Figure 2: Rotation around Z-axis

Project Report (MT1007) 2023-2024 Page 2/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

By using dot product of these unit vectors, we have:

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

And finally, we can get the value of pA = RB


A
· pB

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 θ

Figure 3: Rotation around X-axis Figure 4: Rotation around Y-axis

Project Report (MT1007) 2023-2024 Page 3/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

1.2 Euler Angles

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:

Figure 5: 2 Cartesian right-handed 3D reference frames

1. First, Yaw rotation around Z by θ1 degrees. This rotation of the cube defines a new coordinate
system X ′ Y ′ Z ′ .

2. Then, Pitch rotation around Y ′ by θ2 degrees. A new coordinate system is formed X ′′ Y ′′ Z ′′ .

3. Finally, Roll rotation around X ′′ by θ3 degrees. This rotation defines the final coordinate system
X ′′′ Y ′′′ Z ′′′

Project Report (MT1007) 2023-2024 Page 4/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

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:

• Proper Euler angles (ZXZ, XYX, YZY, XZX, YXY).

• Tait–Bryan angles (XYZ, YZX, ZXY, ZYX, YXZ).

Equivalent Rotation Matrix for a convention Z → Y → X

     
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

R = Rz (θ1 ) · Ry′ (θ2 ) · Rx′′ (θ3 )


 
−s(θ1 )s(θ3 )c(θ2 ) + c(θ1 )(θ3 ) −s(θ1 )c(θ2 )c(θ3 ) − s(θ3 )c(θ1 ) s(θ1 )s(θ2 )
 
=  s(θ1 )c(θ3 ) + s(θ3 )c(θ1 )c(θ2 ) −s(θ1 )s(θ3 ) + c(θ1 )c(θ2 )c(θ3 ) −s(θ2 )c(θ1 )
 
 
s(θ2 )s(θ3 ) s(θ2 )c(θ3 ) c(θ2 )

with c(), s() are shortened for cos() and sin() respectively.

Project Report (MT1007) 2023-2024 Page 5/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

1.3 Quaternions

A quaternion is defined as four elements

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

2. A quaternion is a "unit" quaternion if |q| = 1.

3. All rotation quaternions must be unit quaternions.

4. The quaternion q = (1, 0, 0, 0) is the identity quaternion. It represents no rotation. If q is an


arbitrary quaternion and i is the identity quaternion, then qi = iq = q.

5. The conjugate of a quaternion is q ∗ = (q0 , − q1 , − q2 , − q3 )


q∗
6. The inverse of a quaternion is q −1 =
|q|2

1.3.1 Convert rotation matrix to quaternion

The rotation matrix R is given as:


 
r r12 r13
 11 
R = r21
 
r22 r23 
 
r31 r32 r33

Project Report (MT1007) 2023-2024 Page 6/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

The corresponding quaternion can be found in two steps.

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.

if q0 is largest: if q1 is largest: if q2 is largest: if q3 is largest:


r32 − r23 r32 − r23 r13 − r31 r21 − r12
q1 = q0 = q0 = q0 =
4q0 4q1 4q2 4q3

r13 − r31 r12 + r21 r12 + r21 r13 + r31


q2 = q2 = q1 = q1 =
4q0 4q1 4q2 4q3

r21 − r12 r13 + r31 r23 + r32 r23 + r32


q3 = q3 = q3 = q2 =
4q0 4q1 4q2 4q3

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.

Project Report (MT1007) 2023-2024 Page 7/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

1.3.2 Convert Euler angles to quaternions

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:

• Tait-Bryan variant of Euler angles

• Rotation order for yaw, pitch, and roll, rotation around the z, y, and x axes

• Self-rotation (axis moves each time it rotates)

• Active rotation (also known as alibi) (points are rotated rather than the coordinate system)

• Right-handed coordinate system with right-handed rotation

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 .

Project Report (MT1007) 2023-2024 Page 8/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

2 Methodology

2.1 Euler Angles

In general, applying Euler Angles to rotate an object in 3D dimension consists of 5 main steps,
in particular

• Determine the coordinate system:

• Choose Euler angles convention

• Define the Euler angles:

• Converting the Euler angles to rotation matrix:

• Combine Rotation Matrices

• Apply the rotations.

Choose Euler angles convention

A general rotation of a rigid body (e.g. a 3D reconstruction) can be described as a series of 3


rotations about the axes of the coordinate system (i.e. x, y and z axes). The angles of the 3 rotations are
known as Euler angles (a subset is also known as Tait-Bryan angles).

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:

• Proper Euler angles (z-x-z, x-y-x, y-z-y, z-y-z, x-z-x, y-x-y)

• Tait–Bryan angles (x-y-z, y-z-x, z-x-y, x-z-y, z-y-x, y-x-z).

For instance, the convention x-y-z means rotating about the x-axis first, then the y-axis, and
finally the z-axis.

Define the Euler angles

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.

Converting the Euler angles to rotation matrix

Project Report (MT1007) 2023-2024 Page 9/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

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

Combine Rotation Matrices

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 ).

Apply the rotations

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.

Inverse the rotation (optional)

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).

Applying the Methodology:

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

Project Report (MT1007) 2023-2024 Page 10/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

then yaw rotation, so the convention is XYZ.

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 3: Convert the Euler angles to rotation matrix:


     
cos 60 − sin 60 0 cos 30 0 sin 30 1 0 0
     
Rz′′ (60) =  sin 60 cos 60 0 Ry′ (30) =  0 Rx (45) = 0 cos 45 − sin 45
     
1 0 
     
0 0 1 − sin 30 0 cos 30 0 sin 45 cos 45

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

Step 6: Inverse the rotation (OPTIONAL):

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).

Project Report (MT1007) 2023-2024 Page 11/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

2.2 Quaternions

In general, applying Quaternion to rotate an object in 3D dimension consists of 3 main steps,


in particular:

• Create Rotation Quaternion

• Normalization

• Apply the rotations.

Create Rotation Quaternion

Convert between quaternion representations and other rotation representations, such as Euler
angles or rotation matrices, when needed. Methods for conversion include:

Euler angles to quaternions (e.g., ZYX or XYZ order).

We can convert from Euler angles to Quaternion as follows:

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 .

Rotation Matrix to quaternions.

Given the rotation matrix R:


 
r r12 r13
 11 
R = r21
 
r22 r23 
 
r31 r32 r33

Project Report (MT1007) 2023-2024 Page 12/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

We can find the equivalent quaternion using two steps.

* 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.

if q0 is largest: if q1 is largest: if q2 is largest: if q3 is largest:


r32 − r23 r32 − r23 r13 − r31 r21 − r12
q1 = q0 = q0 = q0 =
4q0 4q1 4q2 4q3

r13 − r31 r12 + r21 r12 + r21 r13 + r31


q2 = q2 = q1 = q1 =
4q0 4q1 4q2 4q3

r21 − r12 r13 + r31 r23 + r32 r23 + r32


q3 = q3 = q3 = q2 =
4q0 4q1 4q2 4q3

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

Project Report (MT1007) 2023-2024 Page 13/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

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.

Applying the Methodology:

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: Create Rotation Quaternion:

Convert Rotation Matrix to Quaternions

 
0.4330 −0.75 0.5
 
T hisisourrotationmatrix : R= −0.6124
 
0.7891 0.0474
 
0.4356 0.6597 0.6124

* Find the magnitude of each quaternion component.


r √
1 + 0.4330 + 0.04774 + 0.6124 327
|q0 | = =
4 25

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

Project Report (MT1007) 2023-2024 Page 14/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

** We see that q0 has the largest magnitude (≈ 0.7233), so now

q1 = 0.4397

q2 = 0.0223

q3 = 0.5320

→ ourquaternionq(0, 7233, 0.4397, 0.0233, 0.5320)

Convert Euler Angles to Quaternions

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

→ ourquaternionq(0.8224, 0.2006, 0.3919, 0.3604)

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

Project Report (MT1007) 2023-2024 Page 15/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

q0 q1 q2 q3
q normalized ( , , , ) = (0.8224, 0.2006, 0.3919, 0.3694)
||q|| ||q|| ||q|| ||q||

Step 3: Apply Rotation

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.

To convert a 3D vertex (x,y,z) to quaternion representation:

• Set the scalar part of the quaternion to 0

• 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.

For active rotation: p′ = q −1 pq

For passive rotation: p′ = qpq−1

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.

Project Report (MT1007) 2023-2024 Page 16/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

3 Discussion and Extension

3.1 Euler Angles

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:

• Use a different representation of rotation such as quaternions or Axis-Angle

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.

• Smart rotation order

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

• Limit the range of rotation angles to avoid these critical configurations.

For example, constraining the rotation angles to be within ±85 degrees to reduce the risk of gimbal
lock.

Project Report (MT1007) 2023-2024 Page 17/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

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:

• Use a different representation of rotation

Apply another representation of rotation which is more understandable and familiar to solve the
problem such as Euler angles.

• Develop intuitive representation:

Gain insight into the geometric interpretation of quaternions. Create intuitive visualizations and
interactive demonstrations to help understand how quaternion values correspond to 3D rotations.

Project Report (MT1007) 2023-2024 Page 18/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

4 DATA & CODE Source

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

Clear the screen

1 clear
2 clc

Considering an orthogonal right-handed axis system

1 data = stlread (" Part1 . STL ")


2 points1 = data . Points ;
3 patchStruct . Vertices = points1_trans ;
4 patchStruct . Faces = data . ConnectivityList ;
5 patchStruct . FaceColor = 'g ';
6 patch ( patchStruct )
7 view ([ -123.1 26.6])
8 axis equal
9 points1_trans = transpose ( points1 ) ;
10 set ( gca , ' Ydir ' , ' reverse ')
11 xlabel ( 'x ')
12 ylabel ( 'y ')
13 zlabel ( 'z ')
14 title ( ' Original ')
15 view ([ -53.0 23.3])

Figure 6: The original object

Project Report (MT1007) 2023-2024 Page 19/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

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

Project Report (MT1007) 2023-2024 Page 20/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

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])

Figure 7: The object rotated by Euler Angles

Project Report (MT1007) 2023-2024 Page 21/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

1 % Quaternion converted from Euler Angle


2 q0 = cosd ( phi /2) * cosd ( theta /2) * cosd ( psi /2) + sind ( phi /2) * sind ( theta /2) *
sind ( psi /2) ;
3 q1 = sind ( phi /2) * cosd ( theta /2) * cosd ( psi /2) - cosd ( phi /2) * sind ( theta /2) *
sind ( psi /2) ;
4 q2 = cosd ( phi /2) * sind ( theta /2) * cosd ( psi /2) + sind ( phi /2) * cosd ( theta /2) *
sind ( psi /2) ;
5 q3 = cosd ( phi /2) * cosd ( theta /2) * sind ( psi /2) - sind ( phi /2) * sind ( theta /2) *
cosd ( psi /2) ;
6 quat1 = [ q0 q1 q2 q3 ];
7 quat1 = quaternion ( quat1 ) ;
8 disp ( ' Quaternion converted from Euler Angle : ')
9 disp ( quat1 )

Figure 8: Quaternion converted from Euler Angle

Project Report (MT1007) 2023-2024 Page 22/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

1 % Quaternion converted from Rotation Matrix


2 q0 = sqrt (1 + R (1 ,1) + R (2 ,2) + R (3 ,3) ) /2;
3 q1 = sqrt (1 + R (1 ,1) - R (2 ,2) - R (3 ,3) ) /2;
4 q2 = sqrt (1 - R (1 ,1) + R (2 ,2) - R (3 ,3) ) /2;
5 q3 = sqrt (1 - R (1 ,1) - R (2 ,2) + R (3 ,3) ) /2;
6 if (( q0 > q1 ) && ( q0 > q2 ) && ( q0 > q3 ) )
7 q1 = ( R (3 ,2) - R (2 ,3) ) / (4* q0 ) ;
8 q2 = ( R (1 ,3) - R (3 ,1) ) / (4* q0 ) ;
9 q3 = ( R (2 ,1) - R (1 ,2) ) / (4* q0 ) ;
10 else
11 if (( q1 > q0 ) && ( q1 > q2 ) && ( q1 > q3 ) )
12 q0 = ( R (3 ,2) - R (2 ,3) ) /(4* q1 ) ;
13 q2 = ( R (1 ,2) + R (2 ,1) ) /(4* q1 ) ;
14 q3 = ( R (1 ,3) + R (3 ,1) ) /(4* q1 ) ;
15 else
16 if (( q2 > q0 ) && ( q2 > q1 ) && ( q2 > q3 ) )
17 q0 = ( R (1 ,3) - R (3 ,1) ) /(4* q2 ) ;
18 q1 = ( R (1 ,2) + R (2 ,1) ) /(4* q2 ) ;
19 q3 = ( R (2 ,3) + R (3 ,2) ) /(4* q2 ) ;
20 else
21 if (( q3 > q0 ) && ( q3 > q1 ) && ( q3 > q2 ) )
22 q0 = ( R (2 ,1) - R (1 ,2) ) /(4* q3 ) ;
23 q1 = ( R (1 ,3) + R (3 ,1) ) /(4* q3 ) ;
24 q2 = ( R (2 ,3) + R (3 ,2) ) /(4* q3 ) ;
25 end
26 end
27 end
28 end
29 qnorm = sqrt ( q0 ^2 + q1 ^2 + q2 ^2 + q3 ^2) ;
30 quat2 = [ q0 q1 q2 q3 ]./ qnorm ;
31 quat2 = quaternion ( quat2 ) ;
32 disp ( ' Quaternion converted from Rotation Matrix : ')
33 disp ( quat2 )

Figure 9: Quaternion converted from Rotation Matrix

Project Report (MT1007) 2023-2024 Page 23/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

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])

Figure 10: The object rotated by Quaternion

Project Report (MT1007) 2023-2024 Page 24/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

1 % Quaternion using MATLAB FUNCTION


2 quat3 = rotm2quat ( R ) ;
3 quat3 = quaternion ( quat3 ) ;
4 disp (" Quaternion using MATLAB funtion :") ;
5 disp ( quat3 ) ;
6 points4 = rotatepoint ( quat3 , points1 ) ;
7 figure (6)
8 patchStruct . Vertices = points4 ;
9 patchStruct . Faces = data . ConnectivityList ;
10 patchStruct . FaceColor = 'g ';
11 patch ( patchStruct )
12 axis equal
13 points1 = transpose ( points1 ) ;
14 set ( gca , ' Ydir ' , ' reverse ')
15 xlabel ( 'x ')
16 ylabel ( 'y ')
17 zlabel ( 'z ')
18 title ( ' Quaternion ( MATLAB Function ) ')
19 view ([ -53.0 23.3])

Figure 11: The object rotated by Quaternion using Matlab function

Project Report (MT1007) 2023-2024 Page 25/26


Ho Chi Minh City University of Technology
Faculty of Mechanical Enginnering

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

3D - Rotation Matrices [1]: CueMath. Orthogonal Matrix


https://www.cuemath.com/algebra/orthogonal-matrix/#
[2]: Aleksandar Haber. (2023). Rotation Matrix Tutorial for Robotics and Aerospace Engineering.
https://s.net.vn/f6NN
[3]:Youtube
https://s.net.vn/IdqE,https://www.youtube.com/watch?v=qrav-c42DrE.

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

Project Report (MT1007) 2023-2024 Page 26/26

You might also like