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
Scene Modeling
Đặt các đối tượng trong cảnh tại các
vị trí khác nhau, thay đổi tỉ lệ và
biến đổi
object
A scene with several instances of the object
(c) SE/FIT/HUT 2002
Ví dụ
At each frame of the animation, the
object is transformed, in this case by a
rotation. It could also be transformed
by changing its size (scaling), or its
shape (deforming), or its location
(translation).
Further animation effects can be
achieved by not changing the object,
but the way it is viewed (i.e. the
window to viewport transformation) at
each frame (e.g. by zooming).
(c) SE/FIT/HUT 2002
4
Phép biến đổi - Transformations
Trong kỹ thuật đồ hoạ 3 bước: modeling, rendering, displaying
Với Modeling:
Transformation: là phép ánh xạ tọa độ điểm hay vector thành tọa độ hay
vector khác
Biến đổi mô hình hoá - Modeling transformations
build complex models by positioning simple components
Biến đổitạo góc nhìn - Viewing transformations
placing virtual camera in the world
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
cạnh khác nhau (e.g. faraway, near,
looking down, looking up)
WORLD
OBJECT
CAMERA
Khoa CNTT - DDHBK Hà nội
8682595
2
(c) SE/FIT/HUT 2002
7
Phép biến đổi Affine
Affine Transformations?
Ph
ép biến đổi
Affine l
àphépbiến đổitọa độ điểm đặctrưng
của đốitượng thành tậptương ứng các điểmmới để tạo ra các
hiệu ứng cho toàn đốitượng.
V
ídụ: phép biến đổitọa độ vớichỉ 2 điểm đầucuốicủa đoạn
thẳng tạo thành 2 điểmmới mà khi nối chúng vớinhautạothành
đoạnthẳng mới.
Các điểmnằmtrênđoạnthẳng sẽ có kếtquả là điểmnằmtrên
đoạnthẳng mớivới cùng phép biến đổi thông qua phép nội
suy.
(c) SE/FIT/HUT 2002
Phân loại - Transformations
Có 2 cách nhìn trên phép biến đổi
Object Transformation: thay
đổi tọa
độ của các điểm theo một số các
quy luật mà không ảnh hưởng đến
hệ tọa độ gốc.
Coordinate Transformation sinh ra
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:
T(P
x
,P
y
) = (Q
x
,Q
y
)
or:
T(P) = Q
(c) SE/FIT/HUT 2002
Matrix Representation
If affine transformation T maps P onto Q, then Q is related to P as
follows:
where a, b, c, d, tx and ty are all constants, and ad = bc
This gives rise to the following matrix representation:
i.e.
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
+
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
=
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
y
x
y
x
y
x
t
t
P
P
dc
ba
Q
Q
xxxx
t bP aP Q ++=
yyyy
t dP cP Q ++=
Tr MP Q +=
(c) SE/FIT/HUT 2002
12
Các phép biến đổihìnhhọchaichiều
Phương pháp biểudiễn đốitượng P = [ x y ]
Phép biến đổivị trí điểm
Thực thi phép biến đổi đúng trên 1 điểm ảnh sẽđúng trên toàn bộđối
tượng
⎥
⎦
⎤
⎢
⎣
⎡
=
dc
ba
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
Phép biến đổi
Phép bấtbiến
Phép biến đổitỉ lệ - Scaling
A scaling changes the size of an object with two scale factors, S
x
and S
y
Phép biếndạng
A shearing shears an object in a particular direction, (in 2D, it’s either in the x
or in the y direction
⎥
⎦
⎤
⎢
⎣
⎡
=
10
01
T
[][][ ]
()
[][]
''
10
0
** yxyax
a
yxTX ==
⎥
⎦
⎤
⎢
⎣
⎡
=
[][][ ] [ ][ ]
''
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α )
= x cosθ -y sinθ
y’ = ρ ( sinθ cosα + cosθ sinα )
= x sinθ + y cosθ
[x' y']= [xcosθ -ysinθ xsinθ +
ycosθ]
y
( x, y )
x
α
ρ
θ
ρ
( x’, y’ )
⎥
⎦
⎤
⎢
⎣
⎡
−
=
θθ
θθ
cossin
sincos
][T
(c) SE/FIT/HUT 2002
15
Thuộc tính cơ bản của phép biến đổi
Affine
Transformations
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
(c) SE/FIT/HUT 2002
Affine transformations preserve
affine combinations
It is rare that we want to perform just one elementary
transformation.
Usually an application requires that we build a complex
transformation out of several elementary ones
e.g. translate an object, rotate it, and scale it, all in one move
These individual transformations combine into one overall
transformation
This is called the composition of transformations.
The composition of two or more affine transformations is
also an affine transformation
Khoa CNTT - DDHBK Hà nội
8682595
4
(c) SE/FIT/HUT 2002
19
Thuộc tính
T
ác động lên tập các điểm đặc trưng của đối tượng tạo thành
phép biến đổi cho đối tượng
We have defined each transformation by their effects on single points
In practice these will be applied to multiple points to transfer entire scenes
or objects made up of many defining points
T
(c) SE/FIT/HUT 2002
Điểm gốc - Pivotal points
Cho phép quay và tỉ lệ Rotation and Scaling
The simple versions of rotation and scaling have been based around the origin.
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
multiplication but must instead be added as an extra term, or
vector
What we need is a “trick”, so that translations can be represented
in matrix multiplication form
This then means that they can be easily composed with other
transformations, by simply multiplying the matrices together
(c) SE/FIT/HUT 2002
23
Tọa độ đồng nhất
Homogeneous Transform
x' = ax + by + n
y' = bx + dy + m
Phương pháp biểudiễnmở rộng thông qua tọa độ đồng
nhấtcủa các vector vị trí
Với ứng dụng của phép chiếuhìnhhọcmàởđótọa độ
điểm đượcmôtả dướima trận[ x* y* h]
với x = x*/h, y = y*/h, z = z*/h và h là mộtsố thựctuỳ ý
(c) SE/FIT/HUT 2002
24
Ưu điểmcủaHệ tọa độ đồng nhất
Homogeneous Transform
Ðưaracáinhìnhợpnhấtcủa các phép biến đổidưới phép
nhân ma trận, hỗ trợ cho việcxử lý bằng cả phầncứng và
phầnmềm
Kếthợp các các phép biến đổitạothànhma trận tích đơngiản
duy nhất. Tránh nhầmlẫnvề thứ tự của các phép nhân khi sử
dụng.
Order matters: AB is generally not the same as BA
Chophépkếthợpvớicả các phép biến đổi đặcbiệt không
tuyến tính khác(non-affine) như:
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
]1[
1
010
001
]1[]1''[ nymx
nm
yxyx ++=
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
=
(t
x
, t
y
, t
z
)
(c) SE/FIT/HUT 2002
26
Phép tỉ lệ
]12.1.[
100
020
001
]1[]1''[ SySxS
S
yxyx =
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
=
(c) SE/FIT/HUT 2002
27
Phép quay
y
( x, y )
x
α
ρ
θ
ρ
( x’, y’ )
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
−=
100
0cossin
0sincos
]1[]1''[
φφ
φφ
yxyx
]1cos.sin.sin.cos.[
φφφφ
yxyx +−=
(c) SE/FIT/HUT 2002
28
Phép biến đổitổng hợp
(c) SE/FIT/HUT 2002
Coordinate Transforms
(c) SE/FIT/HUT 2002
30
Coordinate Transforms
(1,1)
u’
v’
(1,1)
u
v
x
y
Object defined in Local
Coordinate System
Object after transformation in
Global Coordinate System