# Transformations

Document Sample

```					                                                     Transforming Pictures
Sometimes objects exhibit certain symmetries, so only a part of it
needs to be described, and the rest constructed by reflecting,
rotating and translating the original part
A designer may want to view and object from different vantage
5. Geometric                     points, by rotating the object, or by moving a “synthetic camera”
viewpoint.
Transformations                  In animation, one or more objects must move relative to one
another, so that their local co-ordinate systems must be shifted and
rotated as the animation proceeds.
Modelling Transforms
Viewing + Projection

1

Example 1                                            Example 2
Object parts defined in a local
co-ordinate system:

etc...

Larger objects are then
“assembled” by duplicating
and transforming each of the
constituent parts:

4

1
Example 3                                                                     Transformations
A transformation on an object is an operation that changes how the
object is finally drawn to screen

5 steps of a “rotating cube” animation                               There are two ways of understanding a transformation

At each frame of the animation, the object is transformed, in this case          An Object Transformation alters the coordinates of each
by a rotation.                                                                   point according to some rule, leaving the underlying coordinate
system unchanged
It could also be transformed by changing its size (scaling), or its shape
(deformation), or its location (translation).
Further animation effects can be achieved by not changing the object,            A Coordinate Transformation produces a different
but the way it is viewed zooming and panning the viewing window                  coordinate system, and then represents all original points in this
new system

Example: OBJECT TRANSFORMATION

2D Object Transformations
{.4, 2}                         A 2D object transformation alters each point P into
P = {Px, Py}
a new point Q using certain rules

It therefore alters the co-ordinates of P {Px, Py}              ???
{1,1}                                                                                                                          DO SOMETHING
into new values which specify point Q {Qx,Qy}                   ???

This can be expressed using some function T, that           Q = {Qx, Qy}
Example: COORDINATE TRANSFORMATION                                             maps co-ordinate pairs into new co-ordinate pairs:
T ({Px,Py}) = {Qx,Qy}
May simply be denoted as T(P) = Q
P = {2, 2}

e.g: P is the point {2, 2}, T is a transform that scales
{1,1}                           by a factor of 2. Then T(P) = T({2, 2}) = {4,4}
Scale by 2

{1,1}                                                More on this
Q = {4, 4}
later…

2
Transformations                                                                                 Useful Properties of Affine Transformations
Whole collections of points may be
transformed by the same transformation T,
e.g. lines or circles                                      P1 {0, 2}   P2 {2, 2}

Preservation of lines:
The image of a line, L, under T, is the set of all
images of the individual points of L.                     P0 {0, 0}    P3 {2, 0}                Preservation of parallelism
For most mappings of interest, this image is
still a connected curve of some shape
For some mappings, the result of transforming                                                   Preservation of proportional distances
a line may no longer be a line
Q1 {0, 4}               Q2 {4, 4}

Affine Transformations, however, do
preserve lines, and are the most commonly-
used transformations in computer graphics

Q3 {4, 0}
Q0 {0, 0}

Why are they useful?                                                                            Elementary Transformations
They preserve lines, so the image of a straight line is another straight                        Affine transformations are usually combinations of four elementary
line.                                                                                           transformations:
This vastly simplifies drawing transformed line segments.
We need only compute the image of the two endpoints of the                                      1: Translation
original line and then draw a straight line between them
2: Scaling
Preservation of co-linearity guarantees that polygons will transform
into polygons                                                                                   3: Rotation

Preservation of parallelism guarantees that parallelograms will                                 4: Shearing
transform into parallelograms

Preservation of proportional distances means that mid-points of
lines remain mid-points

3
Translation                                                                     Scaling
A translation moves an object into a different position in a scene                 A scaling changes the size of an object with two scale factors, Sx and
Sy (if Sx != Sy, then we have a non-uniform scale)
 tx 
This is achieved by adding an offset/translation vector t =  
 ty 
In Vector notation:              Q x   Px   t x                                                               Q x   Px × S x 
Q  =  P  + t                                                                 Q  =  P × S 
 y  y   y                                                                     y  y         y

t                                                                                   Sx, Sy

Scale by
Translate by t
Sx, Sy
Original points                                    Transformed points             Original points                                                Transformed points

Rotation                                                                        Rotation - derivation
Using the trigonometric relations, a point rotated by an angle θ about                                                       Q X = Rcos( θ + φ )              [1]
the origin is given by the following equations:
Q y = Rsin( θ + φ )              [2]

So       Qx = Px cos θ − Py sin θ       Qy = Px sin θ + Py cos θ                                                             PX = R cos (φ )                  [3]
P y = R sin (φ )                 [4]
Q
 Q x   Px cos θ − Py sin θ 
 Q  =  P sin θ + P cos θ                                                                                cos (θ + φ) = cos (θ) cos (φ) − sin (θ) sin (φ)
 y  x              y                                                         θ                          sin (θ + φ) = sin (θ) cos (φ) + cos (θ) sin (φ)
PY                          P
θ                                                                    φ             [1]        Qx = Rcos (θ ) cos (φ ) − Rsin (θ ) sin (φ )

PX        Substituting from [3] and [4]…
R
Qx = Pxcos (θ ) − Py sin (θ )
Rotate by θ                                                                             Similarly from [2]…
Original points                                                                                                                 Qy = Pycos (θ ) + Px sin (θ )
Transformed points   Background info

4
Shearing                                                                Shearing
A shearing affects an object in a particular direction (in 2D, it’s                                                           g =0, h=0.5
either in the x or in the y direction)
A shear in the x direction would be as follows:

 Q x   P x + hP y                                                                              Shear by {g,h}
Q  =       Py     
 y                                           Original points                                                            Transformed points

g=0.5, h=0.5
The quantity h specifies what fraction of the y-coordinate should be
added to the x-coordinate, and may be positive or negative
More generally: a simultaneous shear in both the x and y directions
would be
Shear by {g,h}

 Q x   P x + hP y                            Original points                                                        Transformed points
 Q  =  P + gP 
 y  y           x 

18

Matrix Representation                                                   Transformations Summary
1: Translation                                                     T
All affine transformations in 2D can be generically described in
Qx  1 0  Px  t x 
terms of a generic equation as follows:                                        Q  =      +  
 y  0 1  Py  t y 
T
Q x  a         b     Px   t x                       2: Scaling
Q  =                 P  + t                               Q x   S x     0   Px  0
i.e.          y  c          d    y  y                                  Q  =  0
 y            S y   Py  0
   
+

3: Rotation                                                        T

Q = MP + t                                                 Qx  cos θ
  =
− sin θ   Px   0
 +
Qy   sin θ         cos θ   Py   0
         

T
4: Shearing
Qx   1       h   Px  0
Q  =              +
 y  g        1   Py  0
         
20

5
Matrix Representation                                                                          Problem
An affine transformation is composed of a linear combination
All affine transformations in 2D can be generically described in                            followed by a translation
terms of a generic equation as follows:
Unfortunately, the translation portion is not a matrix multiplication
but must instead be added as an extra term, or vector – this is
Q x  a           b   Px   t x                                      inconvenient
Q  =                     +  
i.e.              y  c            d   Py   t y 


Q = MP + t

Homogeneous Coordinates                                                                        Multiple Transformations
The “trick” we use is to add an additional component 1 to both P and Q, and
also a third row and column to M, consisting of zeros and a 1                               It is rare that we want to perform just one elementary
i.e.                                                                                        transformation.
Qx  a b t x   Px                                             Usually an application requires that we build a complex
Q  =  c d t   P                                              transformation out of several elementary ones
 y         y  y 
e.g. translate an object, rotate it, and scale it, all in one move
 1  0 0 1   1 
              
These individual transformations combine into one overall
Rotate by θ:        cosθ    − sin θ   0    transformation
Translation by {tx, ty}       1 0 t x 
M = 0 1 t y                            M = sin θ
        cosθ      0

        
0 0 1                                   0
          0       1
    This is called the composition of transformations.
        
The composition of two or more affine transformations is also an
Scale by Sx, Sy           S x   0    0       Shear by g, h:
 1 h 0             affine transformation
M =0     Sy   0                            M =  g 1 0
      
            
0     0    1                                 0 0 1
      
            

6
Rotation and Scaling                                                     Pivotal points
The simple versions of rotation and scaling have been based around     Often we wish to rotate or scale with respect to some pivotal point,
the origin.                                                            not the origin
This means that when we rotate or scale, the object will also move,    Most significantly, we often wish to rotate or scale an object about
with respect to the origin                                             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
Not only is the object                                                   original location
rotated, but it also
moves around the origin

Rotation about a pivot point
{x1,y1}

1 0       x1  cos θ     − sin θ    0 1 0 − x1 
0 1       y1   sin θ    cos θ      0 0 1 − y1 
{0,0}                                                                                                          
0 0
          1  0
              0       1  0 0 1 
         

To rotate the square by angle:            θ   about the point   {x1,               cos θ    − sin θ     x1 (1 − cos θ ) + y1 sin θ 
=  sin θ               y1 (1 − cos θ ) − x1 sin θ 
y1}
• Translate all points through {-x1,-y1}                                              cos θ                                 
• Rotate all points about the origin by            θ                         0
             0                    1             

• Translate all points back through {x1 y2}

28

7
Why composition?                                                                                                                                   Local vs. Global Co-ordinates
One of the main reasons for composing transformations is                                                                                              In practice we find that it is cumbersome in most applications to
computational efficiency
have to recalculate the individual points that make up an object
Suppose you want to apply two affine transformations
to the N vertices of a polygonal object                                         a b t x  a1 b1 t x1  a2         b2 t x 2 
c d t  = c d t c                 d 2 ty2                  It is more common to define the object with reference to some local
It requires 18 multiplications* to compose the                                        y   1    1  y1   2                    
transformations, and 6 multiplications to apply a                              0 0 1   0 0 1  0
                                 0 1                     co-ordinate origin (or reference point) and axis.
transformation to a point                                                                      Qx   a b            t x   Px 
So, “compose-then-apply” requires 6N+18                                                   and Qy  =  c d
                    t y   Py 
 
multiplications                                                                                 1  0 0
                    1  1 
                  We then specify through a transformation matrix how this reference
frame relates to the Global reference frame.

12N multiplications are required if each                                                   P ' x  a1 b1 t x1   Px 
transformation is applied separately (and possibly                                         P'  =  c d t   P 
 y  1       1   y1   y 

 1   0 0 1  1 
                    
For models with any significant number of vertices                                         Qx   a2 b2 t x 2   P ' x 
Q  =  c d t   P ' 
{which will be most}, 6N+18 is much less than 12N                                            y     2  2   y 2     y

 1   0 0 1  1 
                     
e.g. if N=10000, 6N+18 = 60018, 12N = 12000
i.e. about half the effort

* : clever graphics systems will take into account that the bottom row doesn’t need to be                                                                                                                                          30
multiplied out. But even in this case Compositions takes about half the time (9N+27 vs. 18N)

Modelling Scenes                                                                                                                                   Scene Graphs

TRINITY

Modelling complex scenes made up many objects is a two step process of modelling                                                                               Individual object geometries are first modelled. These are then
individual objects and then placing these in the scene (using transforms) 31                                                                               linked using positional/orientational relationships (transforms)
32

8
A Scene Graph
Body
+
Hood                                       +
Trunk
Front Bumpe r
Rear Bumpe r
Left Door
Usually we model individual objects based on a local coordinate
Left Door Glass
system
Right Door
Right Door Glass
Left Front Wheel
Right Front Wheel          This of course shouldn’t
Left Rear Wheel
=
Right Rear Wheel
mean all objects need to
share the same
transformations
There can be multiple levels of transforms e.g. the “door glass”
is linked to the “door” which is linked to the “body” 33                                                                                        34

Current Transformation Matrix

+                                                                One way to do this is by using a single object to represent the
CURRENT TRANSFORMATION MATRIX {CTM}

+                     +                         +                         All transformation functions affect only the CTM
transform             transform                 transform
When a draw procedure is finally called it draws based on the CTM

The CTM might be seen as representing the “current coordinate
Obviously we want                                                             frame” for drawing
something more
versatile           =

35                                                                            36

9
Coordinate Transforms                                                                        Identity
x                                           x
x
v                                              v’
v
v’                                                             {1,1}                                       {1,1}
{1,1}

{1
*,1                                                u           y                                 u’       y
*}

u                           u’                                         1 0 0
Object defined in Local                                                                     Q =  0 1 0 P
      
Coordinate System                                                 y
Object after transformation in                             0 0 1 
      
Global Coordinate System
37                                                                                             38

Translation                                                                                  Rotation
x                                                                                              x
v                     1 0 tx    x                        v’                                 v                                           x
0 1 ty  P
Q=                                                                                                          v’
{1,1}                                                         {1+tx,1+ty}
0 0 1 
       
{1,1}
u’

u      y                                                      u’
u            y
y                                                                                        y
1 0 tx   0 tx        1 0 tx  1  1 + tx          1 0 tx  0  tx 
0 1 ty   0 = ty      0 1 ty  0 =  ty            0 1 ty  1 = 1 + ty              cos θ     − sin θ         0             cos θ              − sin θ 
v =  sin θ         u =  cos θ 
                                                                   
0 0 1  1   1 
                     0 0 1  1   1 
                           0 0 1  1  1 
                            Q =  sin θ
          cos θ           0 P
                                        
 0          0             1              1 
                    1 
        
u {0, 1, 1}                                                  
origin                         v
39                                                                                             40

10
Scaling                                                                          Composite Transformations
v
x                                                                            x                                x                                         x1 (1 − cosθ ) + y1 sin θ 
u                                                                      y (1 − cosθ ) − y sin θ 
v                                                                                                                                          O=     1                 1       
x                                                                                                                   
             1             

{1,1}
{sx*1,sy*1}                                             v’
{1,1}                                                                                                                   u’

cosθ + x1(1 − cosθ ) + y1 sinθ 
u        y                                                 sinθ + y (1− cosθ ) − y sinθ 
y   v=           1               1      

              1                

u       y
cos θ        − sin θ   x1 (1 − cos θ ) + y1 sin θ 
y      u           M =  sin θ       cos θ     y1 (1 − cos θ ) − x1 sin θ                   − sinθ + x1(1− cosθ ) + y1 sinθ 
 sx 0 0                       0          sx                0            
 0              0                   1


u=     cosθ + y (1− cosθ ) − y sinθ 
                                
u =  sy 
                                                  
1              1

Q =  0 sy 0 P                O =  0       v= 0

              1                 

                                                            
 0 0 1
                              1          1                 1
 
             
41                                                                                                       42

Transforming Images                                                              Transformations on Pixels
Alternatively we could apply a rotation transfrom for each pixel.

This works in some cases but there are problems when the angle of
rotation is not a multiple of 90 degrees

There often isn’t a 1-to-1 correspondence between number of
original and transformed pixels.
In Addition, rounding to the nearest discrete block can create errors
Simply juggling array positions can achieve simple 90 degree rotations              that lead to gaps in the transformed image.

for (int x=0; x<width; x++)
for (int y=0; y<height; y++)
{
set(x, y, get (y, x) );
}

43                                                                                                       44

11
Transforming Images
The solution is to look at it from the           ROTATE IMAGE by θ :
point of view of rasterizing a
transformed polygon which is                     Find rotated “frame” of the image
textured with the original image
For each scanline (y)
Using interpolation find startx and
start_x            end_x
endx
v                                  For every pixel x (between startx and
endx)
Find {u,v} corresponding to {x, y}
This is done by rotating {x,y} by -θ
set ( {x,y}, get {u,v})

This ensures that every pixel in the rotated
image is dealt with and no gaps are left

N.B. In most cases there will be some distortion in the
u                      image. So rotating images by arbitrary angles can cause
degradation.

45                                                                                                  46

3D Object Transformations                Scale
all vectors are scaled from the origin:
scaling

translation                                     Original                        scale all axes                   scale Y axis

rotation

47      offset from origin           distance from origin also scales                               48

12
Scale                                                                                               Rotation
Rotations are anti-clockwise about the origin:
 x′   s x x   s x        0         0  x
 y ′ =  s y  =  0      sy          0   y  ⇒ q = Sp
   y                                   
 z′   sz z   0
                       0           sz   z 
 

Or in 3D homogeneous coordinates                                                                rotation of 45o about the Z axis

 x′   s x x   s x        0        0      0  x 
 y′   s y   0            sy       0      0  y 
 = y =                                      
 z′   sz z   0           0        sz     0  z 
                                            
 w′  w   0               0        0      1   w

49                        offset from origin rotation                             50

Rotation                                                                                            Translation
2D rotation of θ about origin:                       x′  cos θ      − sin θ   x            Translation only applies to points, we never translate vectors.
 y ′ =  sin θ    cos θ   y 
Remember: points have homogeneous co-ordinate w = 1
                         

x′ = x + a                x′  x + a 1            0 0 a  x
3D homogeneous rotations:                                                                                                       y′  y + b 0            1 0 b  y
y′ = y + b                =         =                  
z′ = z + c                z′  z + c  0           0 1 c  z 
                              
1   0        0      0         cos θ         0   sinθ     0     cos θ   − sinθ      0 0                                   1   1  0               0 0 1 1
0 cos θ                                                                               0 0
           − sinθ   0

 0             1   0         
0 R =  sinθ   cos θ          
Rx =                           Ry =                                 z
 0                  1 0
0 sinθ     cos θ    0         − sinθ        0 cos θ      0                0
                                                                                      
0   0         0     1         0             0   0        1      0        0         0 1

translate along y
51                                                                                52

13

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 43 posted: 1/26/2011 language: English pages: 13
Description: computer graphics and its applicationa
About if any file u wil find copyright contact me it will be remove in 3 to 4 buisnees days. add me on sanjaydudeja007@gmail.com or visit http://www.ohotech.com/