CS430 Computer Graphics - Winona State University

Download Report

Transcript CS430 Computer Graphics - Winona State University

CS430
Computer Graphics
Transformations of
Objects – 2D Part I
Chi-Cheng Lin, Winona State University
Topics
Matrix Prelim
 Introduction
 Fundamental Transformations
 Affine Transformations
 More Affine Transformations
 Inverse of Affine Transformations
 Composition of Affine Transformations

2
Matrix Prelim
Matrix: rectangle array of numbers
 A: nm matrix  n rows, m columns

 a11 a12

 a 21
 :

 :

an1

...
a1m 

. 
. 

. 

a nm 
A vector in nD-space  an n1 matrix
 a11 
 
 a 21 
 : 
 
 : 
 
an1 
3
Matrix Multiplication
A: nm matrix
B: mp matrix
m
C = A B : np matrix, where c ij   a is bsj
s 1
 Ai =i th row vector of A
Bj =j th column vector of B
cij = Ai • Bj

A B = B A ??
 A (B + C )= A B + A C

??
4
Identity Matrix

Identity matrix I
 1 0 ...

0 1
I  :
1


:

0
1, i  j
ij  
0, otherwise
0

.
.

.

1
5
Determinant of Matrix
Determinant of a 22 matrix A:
a c 
  det A  ad  bc
A  
b d 
 Determinant of an nn matrix A:

n
det A   a1j (1)1 j A1j
j 1
where A1i = determinant of an (n-1)(n-1)
matrix obtained by deleting the first row
and i th column from A
 Find the determinant of a 33 matrix! (p.824)
6
Transpose and Inverse of Matrix

Transpose of an nm matrix A is an
mn matrix B
B = A T, i.e., bij = aji
Transpose of an n1 column vector is a
1n row vector

Inverse of an nn matrix A is an nn
matrix B
B = A-1
A B = B A = I
7
Introduction
Transformation: cornerstone of CG
 Change the position, size, orientation of
objects
 We will focus on linear transformation
 Like other graphics systems,
transformation is also central to OpenGL

8
OpenGL Graphics Pipeline

OpenGL pipeline
P1, P2, …
P2
CT
Q1, Q2, … Process
Q2 & display
Q1
P1

Example
glBegin(GL_LINES);
glVertex3f(…) // send P1 through the pipeline
glVertex3f(…) // send P2 through the pipeline
glVertex3f(…) // send P3 through the pipeline
glEnd();
9
Transforming Points and Objects

Transformation alters each point P in
space into a new point Q by means of
formula or algorithm
P is mapped to Q
Q is the image of P under the mapping T

Fundamental transformations
Translate
Scale
Rotate
10
Fundamental Transformations Translation

Translation
Change the position of an object
(Qx, Qy) = (Px + dx, Py + dy)
or
Q = P + T, where T = (dx, dy)
y
Q2
P2
P1
Q1
dx
dy
x
11
Translation Example

Example
d x= 3
dy=-4
12

Fundamental Transformations Scaling
Scaling
Change the size of an object
(Qx, Qy) = (Sx Px, Sy Py)
or Q x   S x 0  Px  or Q x 
 
Q   0
 y 
 
S y  Py 
 Px
 S
Q 
P
y
 
 y
Actually it is scaling about the origin




y
Sy=2
X
Sy=2
Sx=3
x
Sx=3
x
13
Scaling Example

Example
Sx=1/2
Sy=1/4
14
Fundamental Transformations Rotation

Rotation
Change the orientation of an object
(Qx, Qy) = (Pxcos -Pysin, Pxsin + Pycos)
or Q x   cos - sin  Px  or Q x 
 Px 
   R 
   
 
Q 
Q 
P 
P
sin

cos

y
y
y
 
  
 
 y

Derivation of rotation mapping?
y
r
Q
 r P

x
15
Fundamental Transformation Rotation

Rotation (cont’d)
Rotation is performed about the origin
y

x
16
Rotation Example

Example

 = 45o
17
Affine Transformations

Questions
How do we scale/rotate about any point?
How do we combine transformations?

Problem of composing transformations
Translation: Q = T + P  addition
Scaling: Q = S P
 multiplication
Rotation: Q = R P
 multiplication

Solution: homogeneous coordinates
Represent a 2D point (x, y) as (x, y, 1)
A vector (a, b) = (a, b, 0)
18
Affine Transformations

Coordinates of point Q are linear
combinations of those of point P:
Q x

Q y
 1

  m11Px  m12Py  m13 
 

   m 21Px  m 22Py  m 23 
 

1
 

Q x   m 11 m 12 m 13  Px 
  
 
 Q y    m 21 m 22 m 23  Py 
 1  0
 1 
0
1
  
 

Translation, scaling, and rotation are affine
transformations (so are their combinations) 19
Affine Transformations
Translation, scaling, and rotation are affine
transformations
 Succession of affine transformation is an
affine transformation
 Affine transformation from vector V to
vector W : W x   m11 m12 m13 V x 


W y
 0


 
   m 21 m 22
  0
0
 

m 23 V y
1  0




The third row of the transformation matrix
is always (0, 0, 1)
20
Fundamental Transformations Revisit

Translation
(Qx, Qy) =
Q x   1
  
Q y    0
 1  0
  
(Px + dx, Py + dy)
0 d x  Px 
 
1 d y  Py 
0 1  1 
1 0 dx 


 Q  T (d x , d y ) P , T (d x , d y )   0 1 d y 
0 0 1 


21
Fundamental Transformations Revisit

Scaling
(Qx, Qy) = (Sx Px, Sy Py)
Q x

Q y
 1

 S x
 
 0
  0
 
0
Sy
0
0  Px 
 
0  Py 
1  1 
S x

 Q  S (S x , S y ) P , S (S x , S y )   0
 0

0
Sx
0
0

0
1 
22
Fundamental Transformations Revisit

Rotation
(Qx, Qy) = (Pxcos -Pysin, Pxsin + Pycos)
Q x

Q y
 1

  cos   sin  0  Px 
 
 
   sin  cos  0  Py 
  0
 1 
0
1
 
 
 cos   sin  0 


 Q  R () P , R ()   sin  cos  0 
 0

0
1


23
More Affine Transformations Reflection

Reflection – special case of scaling
Sx < 0  reflection about the y-axis
Sy < 0  reflection about the x-axis
y
Sy=2
Sy=1
Sx=-0.5
Sx=3
x
Sx=3
Sy=-2
24
More Affine Transformations Reflection

Pure reflection
Each of the scale factors is either +1 or –1
Examples
T(Px, Py) = (-Px, Py) flip horizontally about y-axis
T(Px, Py) = (Px, -Py) flip vertically about x-axis
T(Px, Py) = (-Px, -Py) ?

Uniform scaling
Sx = Sy = S, where |S| is magnification factor

Differential scaling
Sx  Sy
25

More Affine Transformations Shearing
ShearX
x-coordinate is translated by an amount that
increases linearly with y
y-coordinate is unaffected
Can be used to make italic
letters out of regular ones
(Qx, Qy) = (Px + hPy, Py)
Q x

 Q y
 1

  1 h 0  Px 
 
 
   0 1 0  Py   Q  Shx (h ) P
  0 0 1  1 
 
 
26
More Affine Transformations Shearing

ShearY
y-coordinate is translated by an amount that
increases linearly with x
x-coordinate is unaffected
(Qx, Qy) = (Px, gPx + Py)
Q x

 Q y
 1

  1 0 0  Px 
 
 
   g 1 0  Py   Q  Shy (g ) P
  0 0 1  1 
 
 
27
Inverse of Affine Transformations

If Q =MP, then P =M–1Q, where M–1 is
the inverse of M (i.e., M M–1 = I )
 m 11 m 12

M   m 21 m 22
 0
0

 M 1
m 13 

m 23 
1 
 m 22  m12
1 

  m 21 m11
det M 
0
 0
 (m13 m 22  m12 m 23 ) 

 (m 23 m11  m 21m13 ) 

1

where det M = m11m22 - m12m21
 Inverse of an affine transformation is an
affine transformation!
28
Inverse of Affine Transformations
Translation: T -1(dx, dy) = T(-dx, -dy)
 Scaling: S -1(Sx, Sy) = S(1/Sx, 1/Sy)
 Rotation: R -1() = R(-)
 ShearX: ShX -1(h) = ShX (-h)
 ShearY: Shy -1(g) = Shy(-g)

Verify that those matrices are correct
 If M = I, what transformation is it?

29
Composition of Affine Transformations

Composing (concatenating) transformations
Process of applying several transformations in
succession to form one overall transformation
Composition of affine transformations is
affine
 If a sequence of transformations are
represented by matrices M1, M2, …, Mn,
then a point P is transformed to a point
Q = (Mn((M2(M1 P)))
= (Mn  M2M1)P

30
Composition of Affine Transformations

Example: Build a transformation matrix that
Rotates 45 degrees
Scales in x by 1.5 and in y by –2
Translates by (3, 5)
Find the transformed point of P = (1, 2)
Solution:
 1.06 3 
 1 0 3 1.5 0 0  cos(45)  sin( 45) 0   1.06



 

M   0 1 5  0  2 0  sin( 45) cos(45) 0     1.414  1.414 5 
 0 0 1  0

  0

0
1
0
0
1
0
1



 

Q = M P = (1.94, 0.758, 1)
31
Applications of Composition

Rotate about an arbitrary point (x, y)
Translate by (-x, -y)
3
Rotate about the origin through angle 
2
Translate by (x, y)
Transformation matrix
M = T(x, y)R()T(-x, -y)
1
 cos()  sin( )  cos()x  sin( )y  x 


  sin( ) cos()  sin( )x  cos()y  y 
 0

0
1


32
Applications of Composition

Scale about an arbitrary point (x, y)
Translate by (-x, -y)
Scale in x by Sx and in y by Sy
Translate by (x, y)
 Transformation matrix M
= T(x, y)S(Sx, Sy)T(-x, -y) = ??

Window-to-viewport transformation is
also a good example of composition of
affine transformations (why?)
33
Applications of Composition

Reflection about an axis of reflection
that makes an angel of  with the x axis
Rotate by -
Reflect about x-axis
(i.e., scale in x by 1
and in y by –1)
Rotate by 
34
Applications of Composition

Reflection about an axis of reflection
that makes an angel of  with the x axis
Translation matrix M
= R()S(1, -1)R(-)
Let c = cos(-) and s = sin(-),
 c

M   s
 0

s
c
0  1 0 0  c  s


0  0  1 0  s c
0 1  0 0 1  0 0
 cos(2) sin( 2) 0 


  sin( 2)  cos(2) 0 
 0

0
1


0  c 2  s 2
 
0     2cs

1   0
 2cs
s 2 c 2
0
0

0

1
Check it out!
35