2d geometric transformations -...
TRANSCRIPT
Sun-Jeong Kim
2D Geometric Transformations
9th Week, 2008
Computer Graphics2
Geometric Transformations
DefinitionTo change the geometric description of an object like its position, orientation, or size
Why to need geometric transformations in CGTo reposition or resize objects
To convert a world-coordinates scene description to a display for an output device
Computer-aided design and computer animation
Modeling transformations
Computer Graphics3
Basic 2D Geometric Transformations
TranslationTranslation
RotationRotation
ScalingScaling
y
x
tyy
txx
+=′+=′
θθθθ
cossin
sincos
yxy
yxx
+=′−=′
y
x
syy
sxx
⋅=′⋅=′
x
y
x
y
x
y
x
y
x
y
x
y
Computer Graphics4
2D Translation
Adding offsets to the coordinates of a pointTranslation vectorTranslation vector (or shift vector): (tx, ty)
Rigid-body transformationMoving objects without deformation
y
x
tyy
txx
+=′+=′
class wcPt2D {public:
float x, y;};
void translatePolygon(wcPt2D *verts, int nVerts, float tx, float ty){
for(int k=0; k<nVerts; k++) {verts[k].x = verts[k].x + tx;verts[k].y = verts[k].y + ty;
}}
Computer Graphics5
2D Rotation (1)
Rotating all points of an objectA rotation axisrotation axis and a rotation anglerotation angle
A rotation point (pivot pointpivot point)
θθθφρθφρθθθφρθφρ
cossincossinsincos
sincossinsincoscos
yxy
yxx
+=+=′−=−=′
( )( )φθρ
φθρφρφρ
+=′+=′
==
sin
cos
sin
cos
y
x
y
x
Computer Graphics6
2D Rotation (2)
Rotating all points of an objectA rotation axisrotation axis and a rotation anglerotation angle
A rotation point (pivot pointpivot point)θθθθ
cossin
sincos
yxy
yxx
+=′−=′
class wcPt2D {public:
float x, y;};
void rotatePolygon(wcPt2D *verts, int nVerts, wcPt2D pivPt, double theta){
for(int k=0; k<nVerts; k++) {float rx = pivPt.x + (verts[k].x - pivPt.x) * cos(theta)
- (verts[k].y - pivPt.y) * sin(theta);float ry = pivPt.y + (verts[k].x - pivPt.x) * sin(theta)
+ (verts[k].y - pivPt.y) * cos(theta);verts[k].x = rx;verts[k].y = ry;
}}
Computer Graphics7
2D Scaling (1)
To alter the size of an objectMultiplying object positions (x, y) by scaling factorsscaling factors sx
and sy
Uniform scalingUniform scaling: sx = sy
Differential scaling (NonuniformNonuniform scalingscaling): sx≠sy
Fixed pointFixed point
y
x
syy
sxx
⋅=′⋅=′
( )( ) yff
xff
syyyy
sxxxx
⋅−+=′
⋅−+=′ ( )( )yfy
xfx
sysyy
sxsxx
−+⋅=′
−+⋅=′
1
1
Constants
Computer Graphics8
2D Scaling (2)
To alter the size of an objectMultiplying object positions (x, y) by scaling factorsscaling factors sx
and sy
Fixed pointFixed point
class wcPt2D {public:
float x, y;};
void scalePolygon(wcPt2D *verts, int nVerts, wcPt2D fixedPt, float sx, float sy)
{for(int k=0; k<nVerts; k++) {
verts[k].x = verts[k].x * sx + fixedPt.x * (1 – sx);verts[k].y = verts[k].y * sy + fixedPt.y * (1 – sy);
}}
( )( )yfy
xfx
sysyy
sxsxx
−+⋅=′
−+⋅=′
1
1
Computer Graphics9
Matrix Representations (1)
Translation
Rotation
Scaling
y
x
tyy
txx
+=′+=′
θθθθ
cossin
sincos
yxy
yxx
+=′−=′
y
x
syy
sxx
⋅=′⋅=′
⎥⎦
⎤⎢⎣
⎡+⎥
⎦
⎤⎢⎣
⎡=⎥
⎦
⎤⎢⎣
⎡′′
y
x
t
t
y
x
y
x
⎥⎦
⎤⎢⎣
⎡⎥⎦
⎤⎢⎣
⎡ −=⎥
⎦
⎤⎢⎣
⎡′′
y
x
y
x
θθθθ
cossin
sincos
⎥⎦
⎤⎢⎣
⎡⎥⎦
⎤⎢⎣
⎡=⎥
⎦
⎤⎢⎣
⎡′′
y
x
s
s
y
x
y
x
0
0
Transformation MatrixTransformation Matrix
Computer Graphics10
Matrix Representations (2)
Convenient and efficient way to represent a sequence of geometric transformations
Mi: transformation matrix
Ex) rotation scaling
⎥⎦
⎤⎢⎣
⎡⎥⎦
⎤⎢⎣
⎡ −⎥⎦
⎤⎢⎣
⎡=⎥
⎦
⎤⎢⎣
⎡′′
y
x
s
s
y
x
y
x
θθθθ
cossin
sincos
0
0
PMMMP 12Ln=′
Computer Graphics11
Matrix Representations (3)
How about ‘translation’?
Ex) rotation scaling translation:
Ex) translation rotation scaling: ???
to eliminate the matrix addition
Homogeneous coordinatesHomogeneous coordinates
21 MPMP +=′
⎥⎦
⎤⎢⎣
⎡+⎥
⎦
⎤⎢⎣
⎡⎥⎦
⎤⎢⎣
⎡ −⎥⎦
⎤⎢⎣
⎡=⎥
⎦
⎤⎢⎣
⎡′′
y
x
y
x
t
t
y
x
s
s
y
x
θθθθ
cossin
sincos
0
0
⎥⎦
⎤⎢⎣
⎡+⎥
⎦
⎤⎢⎣
⎡⎥⎦
⎤⎢⎣
⎡=⎥
⎦
⎤⎢⎣
⎡′′
y
x
t
t
y
x
y
x
10
01
Computer Graphics12
Homogeneous Coordinates (1)
Expanding each 2D coordinate-position representation (x, y) to 3-element representation (xh, yh, h)
Homogeneous parameterHomogeneous parameter: h
(x, y, w) (x/w, y/w, 1)
Ex) (2, 1, 1) = (4, 2, 2) = (6, 3, 3)
Cf. (x, y, 0) a point at infinite = a vector
h
yy
h
xx hh == ,
1 2
1
2
x
y
Computer Graphics13
Homogeneous Coordinates (2)
Translation
Rotation
Scaling
y
x
tyy
txx
+=′+=′
θθθθ
cossin
sincos
yxy
yxx
+=′−=′
y
x
syy
sxx
⋅=′⋅=′
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡=
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡′′
1100
10
01
1
y
x
t
t
y
x
y
x
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡ −=
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡′′
1100
0cossin
0sincos
1
y
x
y
x
θθθθ
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡=
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡′′
1100
00
00
1
y
x
s
s
y
x
y
x
Computer Graphics14
Inverse Transformations
Translation
Rotation
Scaling
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡=
100
10
01
y
x
t
t
T
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡ −=
100
0cossin
0sincos
θθθθ
R
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡=
100
00
00
y
x
s
s
S
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡−−
=−
100
10
011
y
x
t
t
T
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡−=−
100
0cossin
0sincos1 θθ
θθR
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡=−
100
010
0011
y
x
s
s
S
Computer Graphics15
2D Composite Transformations
A sequence of transformations multiplication of transformation matrices
ConcatenationConcatenation (composition)
Efficiency with premultiplication
Ex) translation rotation scaling:MPP
PMMMP
=′=′ 12Ln
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡ −
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡=
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡′′
1100
10
01
100
0cossin
0sincos
100
00
00
1
y
x
t
t
s
s
y
x
y
x
y
x
θθθθ
Computer Graphics16
General 2D Pivot-Point Rotation
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡−−+−−
=⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡−−
⋅⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡ −⋅⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
100
sin)cos1(cossin
sin)cos1(sincos
100
10
01
100
0cossin
0sincos
100
10
01
θθθθθθθθ
θθθθ
rr
rr
r
r
r
r
xy
yx
y
x
y
x
( ) ( ) ( ) ( )θθ ,,,, rrrrrr yxyxyx RTRT =−−⋅⋅
Translate Rotate Translate
(xr,yr) (xr,yr) (xr,yr)(xr,yr)
Computer Graphics17
General 2D Fixed-Point Scaling
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡−−
=⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡−−
⋅⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡⋅
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
100
)1(0
)1(0
100
10
01
100
00
00
100
10
01
yfy
xfx
f
fx
f
f
sys
sxs
y
x
s
s
y
x
y
Translate Scale Translate
(xf,yf) (xf,yf) (xf,yf) (xf,yf)
( ) ( ) ( ) ( )yxffffyxff ssyxyxssyx ,,,, ,, STST =−−⋅⋅
Computer Graphics18
Reflection (1)
Reflection with respect to the axis
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡−
100
010
001
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡−
100
010
001
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡−
−
100
010
001
Reflectionabout the x axis
x
y 1
32
1’
3’2’x
y1
32
1’
3’ 2’
x
y
3
1’
3’ 2’
1
2
Reflectionabout the y axis
Reflectionrelative to the origin
Computer Graphics19
Reflection (2)
Reflection with respect to a line
Clockwise rotation of 45° reflection about the x axis counterclockwise rotation of 45°
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
100
001
010 y=x
x
y
x
y 1
32
1’
3’2’x
y
x
y
Computer Graphics20
Shear (1)
Distorting the shape of an abjectA unit square parallelogram
A unit square shifted parallelogram
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
100
010
01 xh
x-direction shear with hx=2
x
y
x
y
(0,0) (1,0)
(1,1)(0,1)
(0,0) (1,0)
(3,1)(2,1)
yy
yhxx x
=′⋅+=′
Shear with hx=1/2, yref=–1
x
y
x
y
(0,0) (1,0)
(1,1)(0,1)
(1/2,0)
(3/2,0)
(2,1)(1,1)
(0,-1)
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡ ⋅−
100
010
1 refxx yhh ( )yy
yyhxx refx
=′
−+=′