Khoa CNTT - DDHBK Hà nội
8682595
1
(c) SE/FIT/HUT 2002
1
Bài 4 Các phép biến đổi Đồ hoạ
Affine Transformations
Le Tan Hung
Email:
I KHái niệm cơ bản
II Các phép biến đổi
III Hệ tọa độ đồng nhất
(c) SE/FIT/HUT 2002
2
Mô hình hoá - Modelling
Mô hình hoá là tiến trình tạo mới thao
tác trên các mô hình của các đối tượng
hay hệ thống
Computer graphics ~ quan tâm đến mô
tả hình học của các đối tượng nhằm
cung cấp phương pháp biểu diễn số cho
các hình trên cơ sở về kích thước và các
thuộc tính có liên quan đến tiến trình tô
trát
Các đối tượng hình học thường được
mô tả bởi các thuật ngữ "thực thể cơ sở"
sub-parts (primitives), như circles, lines
polygons hay cubes
A scene trong đồ họa: chứa các thực
thể đối tượng
transformation from world coordinates to camera coordinates
Biến đổitạoHoạtcảnh - Animation
vary transformations over time to create motion
modeling
coordinate
Modeling
transformation
Viewing
transformation
world
coordinate
viewing
coordinate
(eye coordinate)
(c) SE/FIT/HUT 2002
5
Transformations - Modeling
world
(c) SE/FIT/HUT 2002
6
Viewing
Transformations - Viewing
Viewing là tiến trình tạo ra góc nhìn của
các mô hình trên màn hình 2D
Mô tả hình học của các đối tượng
hay các cảnh cung cấp bởi các mô
hình sẽ được chuyển đổi th
ành tập
các thực thể cơ sở hiển thị.
Một mô hình có thể quan sát trên các góc
hệ tọa độ khác và biểu diễn tất các
các đểm trên hệ tọa độ mới đó
Mỗi phương pháp có ưu nhược điểm riêng
về bản chất gần tương đồng nhau
1,1
.4, 2
Example: OBJECT TRANSFORMATION
(1,1)
(1,1)
Example: COORDINATE TRANSFORMATION
(c) SE/FIT/HUT 2002
9
Modeling Transformations
Transform objects/points Transform coordinate system
(c) SE/FIT/HUT 2002
2D Object Transformations
A 2D object transformation alters each point P into a
new point Q using a specific formula or algorithm.
It therefore alters the co-ordinates of P (P
x
,P
y
) into
new values which specify point Q (Q
x
,Q
y
)
This can be expressed using some function T, that
maps co-ordinate pairs into co-ordinate pairs:
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
=
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
y
x
y
x
y
T
[][][ ]
()( )
[]
[]
''
y dybx *y * xcyax
dc
ba
xTX =++=
⎥
⎦
⎤
⎢
⎣
⎡
=
y
x
z
p
M
p
W
Khoa CNTT - DDHBK Hà nội
8682595
3
(c) SE/FIT/HUT 2002
13
⎦
⎤
⎢
⎣
⎡
=
[][][ ] [ ][ ]
''
10
1
** yxdybx
b
yxTX =+=
⎥
⎦
⎤
⎢
⎣
⎡
=
x
z
y
(c) SE/FIT/HUT 2002
14
Phép quay- Rotation
x = ρ cos α, y = ρ sin α ;
x’ = ρ cos (θ +α ), y’ = ρ sin (θ +α )
;
x’ = ρ ( cosθ cosα - sinθ sinα )
Preservation of lines:
They preserve lines, so the image of a straight line is another straight line.
This vastly simplifies drawing transformed line segments.
We need only compute the image of the two endpoints of the original line
and then draw a straight line between them
Preservation of collinearity guarantees that polygons will transform into
polygons
Affine transformations map lines to lines;
(c) SE/FIT/HUT 2002
Thuộc tính
Preservation of parallelism
Preservation of parallelism guarantees that parallelograms will transform
into parallelograms
Preservation of proportional distances
Preservation of proportional distances means that mid-points of lines
remain mid-points
Affine transformations change volume by | Det(M) |;
(c) SE/FIT/HUT 2002
Kếthợp các phép biến đổi
Composition of Affine Transforms
Any affine transformation can be
decomposed into elementary
transformations.
Mọi phép biến đổiphứctạp đềucó
thể tạothànhtừ các phép biến đổicơ
sở như:
Dịch chuyển - Translation
Tỉ lệ - Scaling
Quay- Rotation
Biếndạng - Shearing
This means that when we rotate or scale, the object will also move, with
respect to the origin
Translate all points through (-c1,-c2)
Rotate all points about the origin by
Translate all points back through (c1,c2)
(c
1
,c
2
)
(0,0)
(c) SE/FIT/HUT 2002
Pivotal points
Often we wish to rotate or scale with respect to some pivotal
point, not the origin
Most significantly, we often wish to rotate or scale an object
about its centre, or midpoint
In this way, the object’s location does not change
To do this, we relate the rotation or scaling about the pivotal
point V, to an elementary rotation or scaling about the origin
We first translate all points so that V coincides with the origin
We then rotate or about the origin
then all points are translated back, so that V is restored to its original
location
(c) SE/FIT/HUT 2002
Hệ toạ độ đồng nhất
Vấn đề gặp phải:
An affine transformation is composed of a linear transformation
followed by a translation
Unfortunately, the translation portion is not a matrix
Phép chiếuphốicảnh - Perspective projections!
Uốn - Bends, Vuốt tapers v.v.v
Khoa CNTT - DDHBK Hà nội
8682595
5
(c) SE/FIT/HUT 2002
25
Phép biến đổivớitọa độ đồng nhất
Ma trậnbiến đổi đồng nhất
Phép tịnh tiến
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
=
1
0
0
][
nm
dc
ba
T
020
001
]1[]1''[ SySxS
S
yxyx =
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
=
(c) SE/FIT/HUT 2002
27
Phép quay
y
( x, y )
x
α
ρ
θ
ρ
( x’, y’ )
⎥
⎥
⎥
u
v
x
y
Object defined in Local
Coordinate System
Object after transformation in
Global Coordinate System
Khoa CNTT - DDHBK Hà nội
8682595
6
(c) SE/FIT/HUT 2002
31
x
y
x
y
Identity as a Coordinate Transform
P
100
010
001
Q
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
= ty
tx
(1,1)
u
v
(1+tx,1+ty)
u’
v’
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
=
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
+
=
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
1
1
1
0
1
ty
⎦
⎤
⎢
⎢
⎢
⎣
⎡
1
1
1
1
0
ty
tx
ty
tx
100
10
01
origin v (1, 0, 0)
u (0, 1, 0)
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
θθ
θθ
cossin
sincos
(1,1)
u
v
u
’
v
’
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
=
1
sin
cos
θ
θ
v
⎥
⎥
O
(c) SE/FIT/HUT 2002
34
x
y
Scaling
x
y
P
100
0
0
Q
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
= sy
sx
0
0
(1,1)
u
v
0
syu
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
=
1
0
sx
v
(c) SE/FIT/HUT 2002
35
Composite Transformations
⎟
⎟
⎟
⎠
⎞
⎜
⎜
⎜
⎝
⎛
⎢
⎢
⎣
⎡
−−
+−
1
sin)cos1(
sin)cos1(
11
11
θθ
θθ
yy
yx
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
−−+
+−+
1
sin)cos1(sin
sin)cos1(cos
(c) SE/FIT/HUT 2002
36
Modeling Transformations
To make full use of the computational optimisation made
possible by composite transforms, we only want to apply the
transformations to points at the very end
i.e. the transformation operation (multiplying point p by
transform matrix is the very last thing we do in the modelling
phase)
Specify points
in local coords
Specify
Transformations
(composite if necessary)
Send to
Pipeline
Khoa CNTT - DDHBK Hà nội
8682595
7
(c) SE/FIT/HUT 2002
37
+
+
=
This of course shouldn’t
mean all objects need to
share the same
transformations
(c) SE/FIT/HUT 2002
Biểudiễn điểm trong không gian 3 chiều
• [ x* y* z* h ] = [ x y z 1 ]. [ T ]
• [x' y' z' 1 ]= [ x*/h y*/h z*/h 1 ][ T ]
Ma trậnbiến đổi
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
=
snml
rjig
qfed
pcba
][T
(c) SE/FIT/HUT 2002
4
Phép tịnh tiến
[X'] = [ X ] . [ T(dx,dy,dz) ]
[ x' y' z' 1 ] =
[ x y z 1 ].[ T(dx,dy,dz) ]
= [ x+dx y+dy z+dz 1 ]
⎥
⎢
⎢
⎢
⎢
⎣
⎡
=
1000
0300
0020
0001
11
s
s
s
zyxzyx ][]'''[
]13.2.1.[szsysx=
(c) SE/FIT/HUT 2002
6
Rotation
In 2D, the only rotation possible was about the origin.
In 3D, there are 3 possible rotations, one about each of the x,
y and z axes
Positive rotations are anti-clockwise, negative rotations are
clockwise, when looking down a positive axis towards the
origin
x
y
z
x
φφ
φφ
cossin
sincos
][Tx
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
−
=
1000
0100
00cossin
00sincos
][
ϕϕ
ϕϕ
Tz
(c) SE/FIT/HUT 2002
8
Quay quanh trụcy
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
=
1000
01
01
01
11
ig
fd
cb
zyxzyx ][]'''[
]1[zfycxizybxgzydx ++++++=
(c) SE/FIT/HUT 2002
10
Phép lấy đốixứng
(reflections-secondary translation)
(c) SE/FIT/HUT 2002
11
Quay quanh mộttrụcbấtkỳ song song với các
trụctọa độ
⎥
⎥
⎥
−
=
1000
00
00
0001
φφ
φφ
φ
cossin
sincos
)]([T
,
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
−−
=
−
10
0100
yzzy
Tth
(c) SE/FIT/HUT 2002
12
Quay quanh mộttrụcbấtkỳ
3
(c) SE/FIT/HUT 2002
13
Solution
ChuyểnP1 về gốctọa độ.
Quay quanh trụcy saochoP1P2 nằmtrênmặtphẳng (y, z)
Quay quanh trụcx saochoP1P2 trùngvớitrụcz.
Quay quanh trụcz saochoP1P3 nằmtrênmặtphẳng (y, z)
Euler’s Theorem: Every rotation around the origin can be
decomposed into a rotation around the x-axis followed by a
rotation around the y-axis followed by a rotation around the
z-axis.
(c) SE/FIT/HUT 2002
14
Bước 1: ChuyểnP1 về gốctọa độ
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
3
P'
2
(x'
2
,y'
2
,z'
2
)
P'
1
L φ
z(x'
2
,0,z'
2
)x
2
12
2
12
2
2
2
2
)()()'()'( xxzzxzL −−==
(c) SE/FIT/HUT 2002
16
Bước 3: Quay quanh trục x.
/M; sin ψ = x
3
'''
/M;
Ma trậntổng hợpcủa các phép biến đổi [ T ] có
dạng sau đáp ứng toàn bộ quá trình biến đổiquay
đốitượng quanh mộttrụcbấtkỳ.
[ T ] = [T(-x1,-y1,-z1)][T(φ-90)][T(ϕ)][T(ψ)]
y
y'''
3
P'''
3
ψ
M
x'''
3
P'''
2
P'''
1
x
z
(c) SE/FIT/HUT 2002
18
Kếtquả sau biến đổicầnphải đưavề vị trí ban đầu qua các phép biến đổi
ngược.
[Tth]= [T(-x1,-y1,-z1)]x[T(φ-0)]x[T(ϕ)]x [T(ψ)]x[T(ψ)]x[T(ϕ)]x
[T(φ-90)]x[T(-x1,-y1,-z1)]
4
i
j
k
φ
Cross product: i x j = ?
(c) SE/FIT/HUT 2002
21
Coordinate Transformations
x
z
y
i
j
k
φ
Transform (x, y, z, 0) coordinate frame to (i, j, k, φ) coordinate frame.
Affine transformation matrix:
Maps (i, j, k, φ) coordinates into (x, y, z, 0) coordinates!
.
1000
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎜
⎜
⎜
⎜
⎜
⎝
⎛
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
=
⎟
⎟
⎟
⎟
⎟
⎠
⎞
⎜
⎜
⎜
⎞
⎜
⎜
⎜
⎜
⎜
⎝
⎛
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
=
⎟
⎟
⎟
⎟
⎟
⎠
⎞
⎜
⎜
Composition of coordinate change
z
φ
x
y
M
1
changes from coordinate frame (x,y,z,θ) to (x’,y’,z’,θ’).
M
2
changes from coordinate frame (x’,y’,z’,θ’) to (x’’,y’’,z’’,θ’’).
Change from coordinate frame (x,y,z,θ) to (x’’,y’’,z’’,θ’’): ?
z
’
φ
’
x
’
y
’
z
’
’
φ
’
’
x
’
’
y
x
y
z’
φ’
x’
y’
z
φ
x
y
z
’
’
φ
’
’
x
’
’
y
’
’
(c) SE/FIT/HUT 2002
26
Object vs. coordinate transformations (2)
Translate and then rotate object:
Rotate and then translate coordinate frame:
z
φ
x
y
’
’
z
’
φ
’
x
’
y
’
(c) SE/FIT/HUT 2002
27
Order of transformations
Let M
i
be the transformation matrix for transformation T
i
.
Sequence of object (point) transformations, T
1
, T
2
, T
3
.
Transformation matrix = M
3
x M
2
mô tả, có thể từ A
0
, nm, mm
đến m, km
(c) SE/FIT/HUT 2002
29
Hệ tọa độ thiếtbị
(DCS-Device Coordinate System)
Là hệ thống tọa độ củathiếtbị nơihiểnthị hình ảnh và không
gian của đốitượng mà ứng dụng mô tả.
ThiÕt bÞ hiÓn thÞ
subselect.me
Vïng täa ®é thiÕt bÞ
VGA=640x480
(c) SE/FIT/HUT 2002
30
Hệ tọa độ chuẩn
(NCS - Normalized Coordinate System)
Giải quyếtvấn đề khi ứng dụng chạy trên các thiếtbị khác
nhau
Có kích thước1x1
Wcs
chuyÓn ®æi 1
NCS Dcs
chuyÓn ®æi 2
6
(c) SE/FIT/HUT 2002
31
Display Co-ordinates
The integer, (x, y) screen co-ordinates are far too restrictive to be
(c) SE/FIT/HUT 2002
34
2-Dimensional Views
A 2-dimensional view consists of two rectangles:
A Window given in real world co-ordinates, defining the portion of
model (scene) to be drawn
A Viewport given in screen co-ordinates, defining the portion of the
screen which will be used to display the contents of the window
(wx
min
,wy
min
)
(wx
max
,wy
max
)
(vx
min
,vy
min
)
(vx
max
,vy
max
)
(wx,wy)
(vx,vy)
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎢
⎢
⎣
⎡
−
−
=
100
0
minmax
minmax
0
00
Xwmin-Xwmax
Xvmin-Xvmax
]1[
YwYw
YvYv
S
⎥
⎥
⎢
⎢
⎢
⎢
⎣
⎡
−
−
−−
−
−
=
=
1
minmax
minmax
minmin
Xwmin-Xwmax
Xvmin-Xvmax
minmin
0
minmax
minmax
0
00
Xwmin-Xwmax
Xvmin-Xvmax
][
]2[]1[]1[][
YwYw
Vxmin=-0.4
Vymax=0
Vymin=480
320
120