You are on page 1of 17

on ric

Lecture 2 Geometric

Lecture 2 Geometric
Transformations

Computer

ati et
Transformations

s
rm eom
ns ric
Le Tr

tio et
ct an

ma om
ur sf

or e
e or

sf G

sfo 2 G
2 ma
Graphics

an e 2
Ge ti

Tr tur
om on

c
et s

Le
ric

Tr ture
an
c
Le
Geometric
Transformations by
Brian Wyvill
Lecture 2 Geometric
Transformations ns ic
io tr
University of Calgary
at me
rm eo
fo G
ns 2
ra e
T ur
ct
Le

ENEL/CPSC 1 1.
2D Transformations TRANSLATION

2,3

2,1

0,0 4,0 0,0

Object Description: Object Description:


00 01
40 41
23 24
21 22

x new = x + d x y new = y + d y

Using Vector Notation

x x new dx
P = P new = Y T = d so P new = P + T
y new y
F&VD.2 p168
ENEL/CPSC 2 .
2D Transformations SCALING

2,3

2,1

0,0 4,0 0,0

Object Description: Object Description:


00 00
Scale by 0.5 0.6667
40 20
23 12
21 1 0.6667

x new = x * S x y new = y * S y

Using Matrices S is the scaling matrix


x new Sx 0 x
y new = * or P new = S * P
0 Sy y
ENEL/CPSC 3 .
2D Transformations ROTATION

2,3

2,1

0,0 4,0 0,0

Object Description: Object Description:


00 00
Rotate by 15 degrees
40 3.863703 1.035276
23 1.155395 3.415416
21 1.673033 1.483564

x new = x * cosθ − y * sinθ


y new = x * sinθ + y * cosθ

Using Matrices: R is the rotation matrix


x new cosθ −sinθ x
= * or P new = R * P
y new sinθ cosθ y
ENEL/CPSC 4 .
Rotation

Positive angles are measured anti−clockwise from x towards y.

y P new(x new y new)

x new = x * cosθ − y * sinθ


P(x,y)
y new = x * sinθ + y * cosθ −−−−−−− (2.1)
r
r
θ
φ
0,0 x
From the diagram:

x = r cosφ, y = r sinφ −−−−−−− (2.2)


x new = r cos(θ + φ) = r cosφ cosθ − r sinφ sinθ
y new = r sin(θ + φ) = r cosφ sinθ + r sinφ cosθ

Substituting for x and y from 2.2 we get 2.1

ENEL/CPSC 5 .
Homogeneous Coordinates
We have : P new = P + T Translation
P new = S * P Scaling
P new = R * P Rotation

Note the addition for Translation.

By expressing points as homogeneous coordinates all three


affine transformations can be treated the same.

x x
In homogeneous coordinates becomes:
y y
W
x,y,W represents the same point as x’,y’,W if one is a multiple of the other.
Thus (2,3,6) is the same as (4,6,12) since
2/6 = 4/12 and 3/6 = 6/12 (W != 0)

In particular: (x,y,W) is the same point as (x/W, y/W, 1) (W != 0)

Dividing the homogeneous point by W gives the Cartesian point (x/W, y/W)
Note the dimension change.

ENEL/CPSC 6 .
Homogeneous Representation of 2D transforms
W
3 space Line representing all the triples
of the form (tx, ty, tW) t!=0 each 2D
homogeneous point represents a line
of points in 3 space.
x
W=1 plane

The triples found by dividing


by W: (x,y,1) represent points in 2 space.
y These homogenized points form
a plane in (x,y,W) space where W=1

ENEL/CPSC 7 .
Homogeneous Representation of 2D transforms
continued
Points are now 3 element column vectors so we need 3x3 matrices
to represent tranformations. The translation equation becomes:

xnew 1 0 dx x
ynew = 0 1 dy * y
1 0 0 1 1

CAUTION!! My earlier notes (some are still unconverted) and some


text books use row vectors and premultilication rather than
postmultiplication. (New convention conforms to current text book).

Matrices can be transposed to go from one convention to the other:


(P * M)T = MT * PT

ENEL/CPSC 8 .
Column Vector vs. Row Vector
Column vectors take less space on the page in book format. However
for the assignment premultiplying matrics as they are read from the
user is easier than stacking the matrics. Use whichever convention you
feel most comfortable with but be consistent.

Row Vector Representation − e.g. rotation around arbitrary point:


1 0 0 cosθ sinθ 0 1 0 0
|xnew y new 1| = 0 1 0 * −sinθ cosθ 0 * 0 1 0 * |x y 1|
dx dy 1 0 0 1 −dx −dy 1

Column Vector Representation − e.g. rotation around arbitrary point:


x new 1 0 −dx cosθ −sinθ 0 1 0 dx x
y new = 0 1 −dy * sinθ cosθ 0 * 0 1 dy * y
1 0 0 1 0 0 1 0 0 1 1

ENEL/CPSC 9 .
Homogeneous Representation of 2D transforms
continued

xnew 1 0 dx x
ynew = 0 1 dy * y Translation
1 0 0 1 1

xnew Sx 0 0 x
ynew = 0 Sy 0 * y Scaling
1 0 0 1 1

xnew cosθ −sinθ 0 x


ynew = sinθ cosθ 0 * y Rotation
1 0 0 1 1
ENEL/CPSC 1 0.
Composing Geometric Transformations
2,3

2,1

0,0 4,0 0,0 0,0

E.g. Translate by (0,1) then translate by (1,1)


1 0 0
First translation T0 = 0 1 1
0 0 1

1 0 1
Next translation T1 = 0 1 1
0 0 1

P’ = T0 * P
P’’ = T1 * P’ so P’’ = T1 * T0 * P 1 0 1
0 1 2
Matrix product T1 * T0 is 0 0 1

ENEL/CPSC 1 1.
Composing Geometric Transformations
continued
Matrix product also known as concatanation, compounding or composition
of matrices. Consider a set of n=1000000 points P representing the following
piano mesh. The points are scaled by matrix S and rotated by matrix R and
translated by matrix T.

Number of vector matrix multiplies = n*S followed by n*R then n*T = 3n


Alternatively M = R*S*T and then n*M matrix vector calculations = 1n

ENEL/CPSC 1 2.
Order of Concatanation
2

2 1

1
0,0 0,0

Start with a unit square at the origin. Translate to 3,2 rotate


Rotatate about the origin Rotate about the origin
translate to 3,2

2
1 2
1
0,0 0,0

Scale about the origin by 2,1 Translate to 2,1


translate to 3,2 Scale about the origin by 2,1

ENEL/CPSC 1 3.
Order of Concatanation

0,0 0,0

Rotate by 45, Scale by 2,1 translate to 2,3


scale by 2,1 translate to 2,3 rotate by 45

0,0 0,0

Rotate by 45, scale 2,1, The square starts off at 2,3


rotate by −45. What transformations are applied
translate to 2,3 to produce the rotated version above?
ENEL/CPSC 1 4.
Vector Library
A vector library exists in ~blob/453/src/v3d_lib
Types
typedef struct vtx { double x,y,z; } V T X , *REFVTX;
typedef struct wvect { double x,y,z,w; } W V T X , *REFWVTX;
typedef struct { VTX pnt, normal; double d; } PLANE, *REFPLANE;
typedef struct { PLANE pln; int num; REFVTX pv; } POLY, *REFPOLY;
typedef double v3dMATRIX[16], *REFv3dMATRIX;

Functions returning a vector


VTX v3dAdd(VTX v1, VTX v2); /* a+b */
VTX v3dSub(VTX v1, VTX v2); /* vector from v1 to v2 */
VTX v3dDiff(VTX v1,VTX v2);
VTX v3dUnit(VTX v);
VTX v3dMul(VTX vec, double m);
VTX v3dDiv(VTX vec, double m);
VTX v3dCross( VTX v1, VTX v2);
VTX v3dNegate( VTX v);
VTX v3dInterp (double t, VTX v1, VTX v2);
VTX v3dZero();
VTX v3dPointOnPlane(VTX p, REFPLANE pln);

ENEL/CPSC 1 5.
Vector Library continued

Functions returning an int


int nearZero(double x);
int v3dNearZero(VTX v);
int v3dSign(double x);
int v3dSameDir(VTX a, VTX b);
int v3dEqual(VTX a, VTX b);
int v3dColinear(VTX a, VTX b, VTX c);

Functions returning a double


double v3dAcos(double a);
double v3dSigUp( double t);
double v3dLength( VTX v);
double v3dSigDown( double t);
double v3dDot( VTX v1, VTX v2);
double v3dSqLength(VTX v); /* returns square of length */
double v3dSqDist(VTX p1, VTX p2);
double v3dAngle( VTX v1, VTX v2);
double v3dSAngle( VTX v1, VTX v2, VTX v3);
double v3dDist( VTX p1, VTX p2);
double v3dBell( double t);
double v3dDeg( double a);

ENEL/CPSC 1 6.
Vector Library continued

Matrix Functions
void matMake(int xdesc, REFVTX value, double rotation,
REFv3dMATRIX matrix);
void matAssign(REFv3dMATRIX a, REFv3dMATRIX b);
WVTX hmatPointMult(REFv3dMATRIX matrix, WVTX p);
VTX matPointMult( REFv3dMATRIX matrix, VTX p);
void matClear(REFv3dMATRIX matrix);
void matPrint(FILE *outfile, char *buf, REFv3dMATRIX mat);

REFv3dMATRIX matCreate();
REFv3dMATRIX matIdentity( register REFv3dMATRIX matrix);
REFv3dMATRIX matCopy( register REFv3dMATRIX m);
REFv3dMATRIX matNth(REFv3dMATRIX mat, int n);
REFv3dMATRIX matPremult(REFv3dMATRIX a,
REFv3dMATRIX b, REFv3dMATRIX c);

Print Functions
/* print routines n= no newline f = full precision */
void v3dPVec( FILE *fp, char *buf, VTX v);
void v3dPVecn(FILE *fp, char *buf, VTX v);
void v3dPfVec( FILE *fp, char *buf, VTX v);
void v3dPfVecn(FILE *fp, char *buf, VTX v);
void v3dPPlane(FILE *fp, char *buf, REFPLANE p);
void v3dPPoly(FILE *fp, char *buf, REFPOLY p);
void v3d2gl(VTX v, double *gv); /* convert to gl double [3] */

ENEL/CPSC 1 7.

You might also like