T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Computer Graphics and Programming
Lecture 2
Vector and Matrix Calculation
Jeong-Yean Yang
2020/10/22
1
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Vector Space
• Vector Definition in 3 Dim. Space
• Euclidean Distance( Two Norm)
• Vector Addition:
2
( , , )
v
x y z
( , , ),
(
,
,
)
'
(
,
,
)
x
y
z
x
y
z
v
x y z
a
a a a
v
v
a
x
a y
a z
a
2
2
2
|| ||
v
x
y
z
Linear Vector Space
Vector always
Pass through origin.
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Moving Vector (1)
• Simple vector movement = Vector addition
• Example in 2D
3
'
v
v
a
X
Y
( , )
v
x y
X
Y
v
a
X
Y
v
a
a
v
a
x
y
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Moving Vector (2)
: Moving Coordinate
• It is called Transform in Graphics and Robotics
4
X
Y
v
XY space
v in
' X'Y' space
v in
:
( , )
v in XY space v
x y
' '
'
( ', ')
(0, 0) in X'Y' space
v in X Y space
v
x y
'
X
'
Y
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Basic Concept: Think Object in 2D space
• We set the Coordinate on each Object.
– Car, air plane, bullet, and so on.
• Moving is not vector addition,
but moving a car is moving coordinate, X’Y’.
5
X
Y
X
Y
'
X
'
Y
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Graphics with Coordinate Transform
6
X
Y
X
Y
Screen Coordinate
XY
Set Car Coordinate
With X’Y’
'
X
'
Y
' : Car Position
'
(0, 0) in X'Y'
v
v
(
,
)
'
'
x
y
x
y
Moving a a
X
X
a
Y
Y
a
Transform
'
(0, 0) ' '
(0
, 0
)
x
y
v
in X Y
v
a
a
in XY
x
a
y
a
Demo)
uWnd-08-Transform-Trans
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Translation Example
Demo: uWnd-08-Transform-Trans
7
'
X
'
Y
(-5,-10,0)
(5,-10,0)
(5,10,0)
(-5,10,0)
1) Create a rectangle car
2) Draw a rectangle car
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Translation (50,100,0)
8
(0,0)
X
Y
Translation with t(50,100,0)
100
50
'
X
'
Y
'
X
'
Y
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Animation with Translation
(Using uWnd::Run)
• Windows has Timer function
– Event OnTimer is subclassed
uWnd::Run()
• Repaint screen
– Calling Invalidate() repaint window uWnd::Redraw()
9
(x,100) (x+5,100)
Demo: uWnd-09-uWnd-Run
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Transform= Translation + Rotation
• Coordinate Rotation
10
+Scaling
X
Y
v
'
X
'
Y
X
Y
v
'
X
'
Y
' ' '
v in XY
v in X Y
v
'
X
'
Y
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
11
𝜃
𝜃
Rotation Formula
v=(x,y)
?
?
X
Y
'
X
'
Y
𝜃
X
Y
'
X
'
Y
'
Y
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
12
𝜃
𝜃
Rotation Formula
v=(x,y)
?
?
X
Y
'
X
'
Y
𝜃
X
Y
'
X
'
Y
𝜃
a
b
c
cos
cos
sin
tan
b
b
a
a
c
a
c
b
X
cos
'
sin
'
X
X
Y
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
13
𝜃
𝜃
Rotation Formula
v=(x,y)
?
?
X
Y
'
X
'
Y
𝜃
X
Y
'
X
'
Y
𝜃
a
b
c
cos
cos
sin
tan
b
b
a
a
c
a
c
b
cos
'
sin
'
X
X
Y
sin
'
cos
'
X
Y
Y
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Rotation Formula
14
cos
'
sin
'
X
X
Y
sin
'
cos
'
X
Y
Y
cos
'
'
sin
sin
cos
X
Y
X
Y
X
Y
'
X
'
Y
'
v
'
X
'
Y
'
v
' in X'Y'
v
Coordinate
Rotation
X
Y
'
X
'
Y
'
v
X
Y
'
v
Rotation in XY!!!
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Remind that Sin, Cos in C/C++ use Radian.
• R =sin(q)
– q is NOT degree, but is RADIAN
• Radian,
• Conversion, RAD(q) or DEG(R)
15
(
) 180 (deg)
rad
(
)
180
(deg) 1 (deg)
180
180
(
)
(deg)
180
rad
x
rad
x
(
)
180
1(
)
(deg)
180
(
)
(deg)
rad
rad
x
x rad
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Rotation along x, y, and z
16
1
0
0
( )
0
cos
sin
0
sin
cos
x
R
x
y
z
x
y
z
x
y
z
cos
0
sin
( )
0
1
0
sin
0
cos
y
R
cos
sin
0
( )
sin
cos
0
0
0
1
z
R
1
0
0
) '
0
cos
sin
cos
sin
0
sin
cos
sin
cos
x
x
x
example v
R v
y
y
z
z
y
z
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Demo: Rotation
uWnd-11-Transform-Rot
17
cos
'
'
sin
sin
cos
X
Y
X
Y
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Demo: Rotation
uWnd-11-Transform-Rot
18
'
'
* '
v
v T
v
R v
'
*
'
'
v
R v
v
v
T
v
'
v
''
v
v
'
v
''
v
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Is it Rotation with 30 degree?
• It does NOT seem 30 Deg. Why?
– + degree is defined as Counter Clock wise in general.
19
Y Axis
Is reversed..
30
30
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Demo: Reverse Y Axis and
Design Object Class uObj
• Demo: uWnd-12-NewDesign
• You check how C++ class is implemented
20
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Coordinate Transform
Rotation, Translation
21
x
y
x
y
R
y
T
x
y
T
x
y
x
R
Rotation
On XY
Rotation
On X’Y’
Transform on
Fixed Coordinate
Transform on
New Coordinate
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Two types
Fixed Coordinate Vs. Moving(New) Coordinate
• Both cases are same
• Fixed coordinate is easy for Vector Calculation
22
x
y
R
y
T
x
Transform on
Fixed Coordinate
y
T
x
y
x
Transform on
New Coordinate
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
First Think Fixed Coordinate
• v’= (R * v ) + T
• Here, a Good Idea is proposed…
23
x
y
Transform
'
v
Rv T
x
y
R
y
T
x
Transform on
Fixed Coordinate
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Homogeneous Transform
• Can we use Matrix Multiplication?
• Homogeneous Vector, X
• Homogeneous Matrix, H
24
'
v
Rv T
'
X
HX
0
1
'
X'
1
1
1
1
v
Rv
v
R
H
T
v
HX
T
1
v
X
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Homogeneous Vector and Matrix
• Because v=(x,y,z), Because R3x3, T3x1,
25
0
1
R
T
H
1
v
X
1
1
x
v
y
X
z
11
12
13
21
22
23
31
32
33
0
1
0
0
0
1
x
y
z
r
r
r
t
R
T
r
r
r
t
H
r
r
r
t
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Homogeneous Matrix for
ONLY Translation and ONLY Rotation
• General form with rotation and translation
• Translation Rotation
26
0
1
R
T
H
0
1
I
T
H
3 3
1
0
0
0
1
0
0
0
1
I
I
1
v
X
'
0
1
1
1
1
I
T
v
Iv T
v T
X
0
1
R
O
H
3 1
0
0
0
O
O
'
0
1
1
1
R
O
v
Rv
X
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Go Back to
Fixed or Moving(New) Coordinate
• Fixed coordinate is easy for Vector calculation
• But In Matrix Calculation, Commutative Law is needed
• Rule of Matrix Multiplication in Fixed Coordinate
• Rule of Matrix Multiplication in New Coordinate
27
'
v
Rv T
*
*
A B
B A
new
old
H
H
old
new
H
H
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Proof of
Transform in Fixed and New coordinate
• Fixed coordinate New coordinate
28
x
y
R
y
T
x
Transform on
Fixed Coordinate
y
T
x
x
Transform on
New Coordinate
R
H
0
1
0
1
0
1
T
R
I
T
R
O
R T
H
H
H
T
H
T
H
R
H
0
1
0
1
0
1
T
R
I
T
R
O
R T
H
H
H
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
We define C++ Class
for Homogeneous Transform
• uVector = (x,y,z)
• hVector = ( uVector, 1) = (x,y,z,1)
• hMat = = v[16]
29
11
12
13
21
22
23
31
32
33
0
0
0
1
x
y
z
r
r
r
t
r
r
r
t
r
r
r
t
v[0]
v[4]
v[8]
v[12]
v[1]
v[5]
v[9]
v[13]
v[2]
v[6]
v[10]
v[14]
v[3]
v[7]
v[11]
v[15]
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Overload and Override in C++
• Example of Overloading
– void test(float x, float y, float z)
– void test(float x, float y)
– void test( uVector)
– Function arguments varies for other purposes.
• Overriding : Sub classing Inheritance OOP
– See Overriding after 3D Object modeling.
30
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Overloading of
Homogeneous Matrix (hMat)
31
0
1
I
T
H
*
'
1
1
A
B
H
H
H
v
v
H
v
Hv
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
See Example
Rotation and Translation
32
uWnd-13-Homogeneous-Transform
uWnd-12-NewDesign
'
v
Rv T
0
1
0
1
0
1
I
T
R
O
R
T
H
'
1
0
1
1
v
R T
v
'
v
Rv T
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Simple Example of Position Control
(Proportional Control, P Control)
• How we control from X to Xd?
• Define a new Parameter, error, e = Xd – X
• Update a new Position X’ = X + K*e ( 0<K<1)
33
ˆ
[ , ]
X
x y
ˆ
[
,
] Desired Position
d
d
d
X
x
y
ˆ
ˆ
ˆ
ˆ
ˆ
ˆ
...
ˆ
ˆ
d
d
e
X
X
X
X
Ke
X
X
ˆ
ˆ
ˆ [
,
]
d
d
d
e
x
x y
y
X
X
Repeat
Until a
condition
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Example) uWnd-14-Control-Missile
• This example moves to
clicked positions
• We need to change
the angle of a missile
34
uWnd::OnTimer
Repeat
ˆ
ˆ
ˆ
ˆ
ˆ
ˆ
...
ˆ
ˆ
d
d
e
X
X
X
X
Ke
X
X
T&C LAB-AI
Relative or General Transform
Relative Transform
• Simple
• Vertex is updated in each
turn.
General Transform
• Transform must be
thought from ORIGIN.
• Good for CAD and
Graphics
35
0
ˆ
X
1
ˆ
X
2
ˆ
X
3
ˆ
X
01
dX
12
dX
23
dX
0
ˆ
X
1
ˆ
X
2
ˆ
X
3
ˆ
X
01
dX
02
dX
03
dX
1
, 1
ˆ
ˆ
ˆ
i
i
i i
X
X
dX
0
1
0, 1
ˆ
ˆ
ˆ
i
i
X
X
dX
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
Diagram of General Transform
36
Object
vertex
Transform,
T or R
Calculate
Temporary
Vertex
vertex
vertex
Temp
Calculate
Temporary
Vertex
Temp
Drawing
Temp
Drawing with
temp vertices
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
General Transform
example) uWnd-15-Control-Missile-Temp
• car.vertex is NOT updated.
• car.temp is updated.
37
Translation, t = o(origin) + e(error)
Temp = vertex + t
1
0
0, 1
ˆ
ˆ
ˆ
ˆ
temp
i
i
X
X
X
dX
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
GPU generates temporary vertices.
• Why my GPU has a lack of memory?
38
Object
vertex
Transform,
T or R
Calculate
Temporary
Vertex
vertex
vertex
Temp
Draw
Temporary
Vertex
Temp
Drawing
Temp
Drawing with
temp vertices
GPU
Memory
GPU
function
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
How to Calculate Missile’s Heading Angle,
uWnd-16-Control-Missile-Angle
• Declare a new function at
uVector
39
Xd
X
ˆ
ˆ
ˆ
[ ,
, 0]
d
x
y
e
X
X
e e
x
y
1
tan
tan
atan( , ) 0
2
atan2( , ) -
y
x
e
y
x
e
y
x
y x
y x
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
We will Define Object like,
uWnd-17-Control-Missile-Object-Complete
• uWnd-17-control-missile-object-complete changes,
40
Object
vertex
Transform,
T or R
Calculate
Temporary
Vertex
Draw
Temporary
Vertex
Drawing
Temp
uWnd-15-Control-Missile-Temp
Object
vertex
H matrix
transform
1. Calculate Temporary Vertex
2. Draw Temporary Vertex
uWnd-17-Control-Missile-Object-Complete
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
uWnd-17-Control-Missile-Object-
Complete
41
uWnd-15-Control-Missile-Temp
uWnd-17-Control-Missile-Object-Complete
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
42
T&C LAB-AI
Dept. of Intelligent Robot Eng. MU
Robotics
uWnd-18-Real-Missile
• What is the Difference?
• Real Guided Missile CANNOT rotate fast.
43
Fast rotation is impossible in our physical world
Limitation of Heading angle change