c SE/FIT/HUT 2002 1Affine Transformations Le Tan Hung Email: hunglt@it-hut.edu.vn I KHái niệm cơ bản II Các phép biến đổi III Hệ tọa độ đồng nhất Mô hình hoá - Modelling Mô hình hoá l
Trang 1(c) SE/FIT/HUT 2002 1
Affine Transformations
Le Tan Hung
Email: hunglt@it-hut.edu.vn
I KHái niệm cơ bản
II Các phép biến đổi III Hệ tọa độ đồng nhất
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).
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 đổi tạo góc nhìn - Viewing transformations
placing virtual camera in the world
transformation from world coordinates to camera coordinates
Biến đổi tạo Hoạt cảnh - Animation
vary transformations over time to create motion
modeling coordinateModeling transformation Viewing transformation
world coordinate viewing coordinate (eye coordinate)
Transformations - Modeling
world
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
Trang 2Khoa CNTT - DDHBK Hà nội
hunglt@it-hut.edu.vn
8682595
Phép biến đổi Affine
Affine Transformations?
Phép biến đổiAffine là phép biến đổi tọa độ điểm đặc trưng
của đối tượng thành tập tương ứng các điểm mới để tạo ra các
hiệu ứng cho toàn đối tượng
Ví dụ: phép biến đổi tọa độ với chỉ 2 điểm đầu cuối của đoạn
thẳng tạo thành 2 điểm mới mà khi nối chúng với nhau tạo thành
đoạn thẳng mới
Các điểm nằm trên đoạn thẳng sẽ có kết quả là điểm nằm trên
đoạn thẳng mới vớ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
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 (Px,Py) into
new values which specify point Q (Qx,Qy)
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 d c b a Q
Q
x x x
y y y
Tr MP
Các phép biến đổi hình học hai chiều
Phương pháp biểu diễn đối tượng P = [ x y ]
Phép biến đổi vị 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
⎥
⎢
=
d c b a T
[ ] [ ] [* y]* [ (ax cy) ( bx dy) ] [x' y']
d c b a x T
y
x z
Trang 3(c) SE/FIT/HUT 2002 13
Phép biến đổi
Phép bất biến
Phép biến đổi tỉ lệ - Scaling
A scaling changes the size of an object with two scale factors, S x and S y
Phép biến dạng
A shearing shears an object in a particular direction, (in 2D, it’s either in the x
or in the y direction
⎥
⎤
⎢
⎡
= 1 0 0 1
T
[ ] [ ] [ ] [ ( ) ] [' ']
1 0
0
*
[ ] [ ] [ ] [ ] [' ']
1 0
1
*
x z
y
Phép quay- Rotation
x = ρ cos α, y = ρ sin α ;
;
= x cos θ - y sinθ
[x' y']= [xcosθ - ysinθ xsinθ +
ycosθ]
y
( x, y )
x
α
ρ
θ
ρ
( x’, y’ )
⎥
⎤
⎢
⎡
−
=
θ θ θ θ cos sin sin cos ]
[T
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ết hợ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 đổi phức tạp đều có
thể tạo thành từ các phép biến đổi cơ
sở như:
Dịch chuyển - Translation
Tỉ lệ - Scaling
Quay- Rotation
Biến dạ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
Trang 4Khoa CNTT - DDHBK Hà nội
hunglt@it-hut.edu.vn
8682595
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ểu diễn mở rộng thông qua tọa độ đồng
nhất của các vector vị trí
Với ứng dụng của phép chiếu hình học mà ở đó tọa độ
điểm được mô tả dưới ma trận [ x* y* h]
với x = x*/h, y = y*/h, z = z*/h và h là một số thực tuỳ ý
Ưu điểm của Hệ tọa độ đồng nhất Homogeneous Transform
Ðưa ra cái nhìn hợp nhất của các phép biến đổi dưới phép nhân ma trận, hỗ trợ cho việc xử lý bằng cả phần cứng và phần mềm
Kết hợp các các phép biến đổi tạo thành ma trận tích đơn giản duy nhất Tránh nhầm lẫn về thứ tự của các phép nhân khi sử dụng
Order matters: AB is generally not the same as BA
Cho phép kết hợp với cả các phép biến đổi đặc biệt không tuyến tính khác(non-affine) như:
Phép chiếu phối cảnh - Perspective projections!
Uốn - Bends, Vuốt tapers v.v.v
Trang 5(c) SE/FIT/HUT 2002 25
Phép biến đổi với tọa độ đồng nhất
Ma trận biến đổi đồng nhất
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
= 1 0
0 ]
[
n m d c b a T
] 1 [
1 0 1 0 0 0 1 ] 1 [ ] 1 ' '
n m y x y
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
=
(tx, ty, tz)
Phép tỉ lệ
] 1 2 1 1 0 0 0 2 0 0 0 1 ] 1 [ ] 1 ' '
S y x y
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
=
Phép quay
y
( x, y )
x
α
ρ
θ
ρ
( x’, y’ )
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
−
=
1 0 0
0 cos sin
0 sin cos ] 1
[
]
1
'
'
φ φ
y
x
y
x
] 1 cos sin sin
cos
[x φ−y φ x φ+y φ
=
Phép biến đổi tổng hợp
(c) SE/FIT/HUT 2002
Coordinate Transforms
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
Trang 6Khoa CNTT - DDHBK Hà nội
hunglt@it-hut.edu.vn
8682595
x
y
x
y
Identity as a Coordinate Transform
P 1
0
0
0
1
0
0
0
1
Q
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
=
(1,1)
u
v
(1,1)
u’
v’
x
y
Translation
x
y
P 1 0 0 1 0 0 1 Q
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
tx
(1,1)
u
v
(1+tx,1+ty)
u’
v’
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
=
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
1 1 0 0
ty tx ty tx
1 0 0 1 0 0 1
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡ +
=
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
1 1 1 0 1
ty tx ty tx
1 0 0 1 0 0 1
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡ +
=
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
1 1 1 1 0
ty tx ty tx
1 0 0 1 0 0 1
origin v (1, 0, 0) u (0, 1, 0)
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
= 1
ty tx O
x
y
Rotation
x
y
P 1 0
0
0
0 Q
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
θ
θ
cos
sin
sin
cos
(1,1)
u
v
u’
v’
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
= 1 sin
cos
θ
θ
v
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡−
= 1 cos
sin
θ
θ
u
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
= 1 0
0
O
x
y
Scaling
x
y
P 1 0 0 0
0 Q
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
sx
0 0
(1,1)
u
v
(sx*1,sy*1)
u
v
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
= 1 0
0
O
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
= 1
0
sy u
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
= 1 0
sx v
Composite Transformations
⎟
⎟
⎠
⎞
⎜
⎜
⎝
⎛
−
−
+
−
−
=
1 0
0
sin ) cos 1
(
cos
sin
sin ) cos 1
(
sin
cos
1 1
1 1
θ θ θ
θ
θ θ θ
θ
x y
y x
M
x
y
x
y
(1,1)
u
v
u’
v
’
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
−
− +
− 1 sin ) cos 1 ( sin ) cos 1 (
1 1 1 1
θ θ θ θ
y y y x
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
−
− + +
− + 1 sin ) cos 1 ( sin
sin ) cos 1 ( cos
1 1 1 1
θ θ θ
θ θ θ
y y y x
⎥
⎥
⎦
⎤
⎢
⎢
⎣
⎡
−
− + +
− +
− 1 sin ) cos 1 ( cos
sin ) cos 1 ( sin
1 1 1 1
θ θ θ
θ θ θ
y y y x
O =
v =
u =
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
Trang 7(c) SE/FIT/HUT 2002 37
= This of course shouldn’t
mean all objects need to
share the same
transformations
+
=
+
transform transform+ transform+
Obviously we want something more versatile
Trang 8(c) SE/FIT/HUT 2002 1
Bài 4B:
Phép biến đổi trong không gian
Ma trận biến đổi 3 chiều 3D Matrix Transformations
Các phép biến đổi chuyển vị - translation, tỉ lệ-scaling và quay-rotation sử dụng trong không gian 2D đều co thể mở rộng trong không gian 3D
Again, using homogeneous coordinates it is possible to represent each type of transformation in a matrix form
In 3D, each transformation is represented by a 4x4 matrix
Các phép biến đổi hình học 3 chiều
Biểu diễ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ận biến đổi
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
=
s n m l
r j i g
q f e d
p c b a ]
[T
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 ]
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
=
1 0 1 0 0
0 0 1 0
0 0 0 1 )]
, , ( [
dz dy dx
dz dy dx T
Phép tỉ lệ
• s1, s2, s3 là các hệ số tỉ lệ tương ứng
trên các trục toạ độ
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
=
1 0 0 0
0 3 0 0
0 0 2 0
0 0 0 1 1 1
s s s z y x
z
y
[
] 1 3 2
1
.
=
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 y
z
x y
z
Trang 9(c) SE/FIT/HUT 2002 7
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎣
⎡
−
=
1 0 0 0
0 1 0 0
0 0 cos sin ]
Tz
Phép biến dạng
(secondary translation)
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
=
1 0 0 0
0 1 0 1 0 1
1 1
i g f d c b z
y x z y
[
] 1
=
Phép lấy đối xứng
( reflections-secondary translation)
Quay quanh một trục bất kỳ song song với các
trục tọa độ
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
=
1 0
0 1
0
0
0 0
1
0
0 0
0
1
]
[
z
y
Tr
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
−
=
1 0 0 0
0 0
0 0
0 0 0 1
φ φ φ φ φ
cos sin sin cos )]
(
[T
,
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
−
−
=
−
1 0
0 1 0 0
0 0 1 0
0 0 0 1
1
z y
Tr]
[
⎥
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎢
⎣
⎡
−
− +
−
−
=
1 sin ) cos 1 ( sin
)
cos
1
(
0
0 cos sin
0
0 sin cos
0
0 0 0
1
]
[
φ φ φ
φ
φ φ
φ φ
y z z
y
Tth
Quay quanh một trục bất kỳ
Trang 10(c) SE/FIT/HUT 2002 13
Solution
Chuyển P1 về gốc tọa độ
Quay quanh trục y sao cho P1P2 nằm trên mặt phẳng (y, z)
Quay quanh trục x sao cho P1P2 trùng với trục z
Quay quanh trục z sao cho P1P3 nằm trên mặt phẳ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.
Bước 1: Chuyển P1 về gốc tọa độ
⎥
⎥
⎥
⎥
⎦
⎤
⎢
⎢
⎢
⎢
⎣
⎡
−
−
−
=
−
−
−
1 1 1 1
0 1 0 0
0 0 1 0
0 0 0 1 1 1 1
z y x
z y x
[
y
P3
P2
z x
y
P3
P2 p P1
z x
Bước 2: Quay quanh trục y
cos( - 90 + φ) = sinφ = z'2/L = ( z2 - z1)/L
sin( - 90 + φ) = - cosφ = x'2/L = ( x2 - x1)/L
[ P''2 ] = [ P'2 ][ T(φ-90) ]
y
P 3 P' 2 (x' 2 ,y' 2 ,z' 2 ) P' 1
L φ z(x' 2 ,0,z' 2 )x
2 2 2 2 2
2
2
'
Bước 3: Quay quanh trục x
Với N = | P''1P''2| là độ dài của đoạn P''1P''2
y
P'' 2
N P'' 1
ϕ x
z
Bước 4: Quay quanh trục z
[P ''' ]= [P3][T(-x1,-y1,-z1 ][T(φ-90)][T(ϕ)]
Với góc quay dương ψ trên trục z
cos ψ = y3/M; sin ψ = x3/M;
Ma trận tổng hợp của các phép biến đổi [ T ] có
dạng sau đáp ứng toàn bộ quá trình biến đổi quay
đối tượng quanh một trục bất kỳ.
[ T ] = [T(-x1,-y1,-z1)][T(φ-90)][T(ϕ)][T(ψ)]
y y''' 3
P''' 3
ψ M x''' 3
P''' 2 P''' 1
x
Kết quả sau biến đổi cần phải đưa về 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)]