<<

Introduction Vectors and matrices Affine transformations

COMP30019 Graphics and Interaction Transformation geometry and homogeneous coordinates

Adrian Pearce

Department of Computing and Information Systems University of Melbourne

The University of Melbourne

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Lecture outline

Introduction

Vectors and matrices

Translation

Rotation

Homogeneous coordinates

Affine transformations

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Transformation geometry How are geometric transformations represented conveniently and efficiently? Aim: understand two-dimensional (2D) transformations Reading: I Akenine-Möller Section 4.1 Basic Transforms Section 4.2 Special Matrix Transforms and Operations I Foley Sections 5.3 Homogeneous coordinates and matrix representation of 2D transformations, 5.4 Composition of 2D transformations and 5.6 Efficiency. I Angel Sections 3.2 Three-Dimensional Primatives; 3.3 Coordinate Systems and Frames; 3.7 Affine Transformations; 3.8 Translation, Rotation, and ; 3.9 Trnasofmations in Homogenoeous Coordinates and 3.10 Concatenation of Transformations Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Review of two-dimensional (2D) geometry

Points are represented either in absolute position, relative to origin in Cartesian coordinates: (x, y) ∈ R2 or polar coordinates: (ρ, θ). v  Vectors represent relative displacement, v = x and are vy subject to vector addition, subtraction, scalar product (dot product) or norm (magnitude) operations.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Curves can be modelled in either explicit functional form: y = F(x) or implicit equational form: G(x, y) = 0 or parametric form:

x = x(t) y = y(t)

e.g. some straight lines are:

I y = mx + b (explicit functional form)

I ax + by + c = 0 (implicit equational form)

I x cos θ + y sin θ = ρ (implicit equational form)

I x = x0 + x1p(t), y = y0 + y1p(t) (parametric form) Special forms include a through two points or line segments such as endpoints or parameter bounds.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Geometric transformations

General geometric transformations concern object points rather than transformation of the entire coordinate frame,

x0 = g(x, y), y 0 = h(x, y).

More specific geometric transformations involve 0 0 I translations, where x = x + xt , and y = y + yt and 0 0 I linear transformations v = Mv (where v is a vector and M is a matrix) such as scaling, shear, rotation, reflection.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Vector and matrix conventions

There are two conventions for vectors and matrices where vectors are written as columns: x v = y

Matrices multiply on the left:

a b x Mv = c d y ax + by = cx + dy

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

However, watch out for other forms where vectors are written as rows: T x v = x y = y Matrices multiply on the right:

a b vM = x y c d ax + cy = bx + dy

Really, you should understand the concepts, and be able to work with either notation.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Translation Translation is also known as shifting or displacement, 0 0 where x = x + xt and y = y + yt . Translation in two-dimensional vector notation (by addition):

 0     x x xt 0 = + y y yt

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Rotation

Rotation of an object about the origin

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Rotation as a matrix operation (by multiplication):

cos θ − sin θ x x cos θ − y sin θ = sin θ cos θ y x sin θ + y cos θ

Convenient, as can combine a number of geometric by multiplication,

x0 cos θ − sin θ x = y 0 sin θ cos θ y

Unfortunately translation is different to rotation, therefore can’t treat as multiplication (also true for scaling). This means translations and rotations cannot be combined easily.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

How are rotation matrices derived? ()00, Increasing x f

()xy, Increasing q

y ()xy¢¢,

Hint: Consider the point above with polar coordinates (ρ, θ) (Rowe Fig 2.1). If rotated by φ around the origin, its polar coordinates are (ρ, θ + φ), converting to Cartesian coordinates gives, ρ cos(θ + φ) ρ sin(θ + φ)

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

We can expand and re-arrange this in terms of the cosine and sine angle-addition formulas, and the Cartesian coordinates of the original point x = ρ cos φ y = ρ sin φ to obtain a matrix expression for a rotation by angle θ.

x0 = ρ cos(φ + θ) = ρ cos φ cos θ − ρ sin φ sin θ y 0 = ρ sin(φ + θ) = ρ cos φ sin θ + ρ sin φ cos θ

x0 = ρ cos(φ + θ) =ρ cos φcos θ−ρ sin φsin θ y 0 = ρ sin(φ + θ) =ρ cos φsin θ+ρ sin φcos θ

x0 = xcos θ−ysin θ Adrian Pearce y 0 = xsin θ+ycos θ University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Rigid body transformations

Rotation, translation, and reflection preserve distances (rigid motions).

That is, if two points are a certain distance apart, they will still be the same distance apart after undergoing one of these transformations (or indeed any combination of them).

Mathematically, they are the only transformations with this property of preserving distance.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Reflection

Reflection in a line—gives mirror image Move point to position at equal distance on opposite side of the line. Reflection in the x-axis:

x0 = x y 0 = −y

Reflection in the y-axis:

x0 = −x y 0 = y

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Reflection in the line y = x:

x0 = y y 0 = x

All these can be expressed as matrix operations (details left to you). Reflections in an arbitrary line can be achieved by transforming line (by rotation and translation) to one of the coordinate axes, reflecting in that axis, and transforming back.

I Reflection reverses handedness—an object goes to its mirror image.

I Rotation and translation preserve handedness—they correspond to motions of rigid (solid) objects.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Combination of transformations?

Unfortunately, translation is treated differently (via addition) from scaling and rotation (via multiplication).

We would like to be able to treat all three transformations in a consistent way, so that they can be combine easily

i.e. using the same GPU unit to simplify the pipeline

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Homogeneous coordinates

Homogeneous coordinates introduces a third dummy coordinate w, represented as (x, y, w).

You imagine a third z or (x/z, y/z), where a ray from the origin to point (x, y, z) in three- pierces the at z = 1.

I This paradoxically suggests that there is some ideal point (x, y, 0).

I More importantly it allows for many different homogeneous coordinate representations of the same point, e.g. (2, 3, 6) is the same as (4, 6, 12), etc.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

In homogeneous coordinates this third dimension is often named w or (wx, wy, w) where w 6= 0. You homogenise by dividing by w to get (x, y, 1) (Foley Figure 5.07). W P

X

W = 1 plane Y

This imaginary dimension can also be considered as (x/z, y/z) where a ray from the origin to point (x, y, z) in three-dimensions pierces the plane at z = 1.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

In Figure 5.7 (Foley, page 171):

I Each point subsequently has many different homogeneous coordinate representations, e.g. (2, 3, 6) is the same as (4, 6, 12), etc.

I Homogenise by dividing by w to get (x, y, 1).

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Translation in homogeneous coordinates:       1 0 xt wx w(x + xt ) 0 1 yt  wy = w(y + yt ) 0 0 1 w w

The additional (redundant) dimension w allows translation to become linear and allows the translation to be written as a matrix product rather than a sum.

Translation in homogeneous coordinates (with w = 1):

 0       x 1 0 xt x x + xt 0 y  = 0 1 yt  y = y + yt  1 0 0 1 1 1

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Rotation in homogeneous coordinates

cos θ − sin θ 0 R =  sin θ cos θ 0 0 0 1

inverse rotation, R-1 = RT,

 cos θ sin θ 0 R = − sin θ cos θ 0 0 0 1

How does one rotate about an arbitrary point?

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

In general, with these transforms (except translation of course), we get the 3-by-3 homogeneous version by putting the 2-by-2 matrix down in the upper-left 2-by-2 block of what is otherwise a 3-by-3 identity matrix.

I Inverse rotation is rotation by negative angle −θ. Since cos(−θ) = cos(θ) and sin(−θ) = − sin(θ), the structure of the rotation matrix means that its inverse is its transpose. (Actually, this is a property common to all orthogonal matrices, of which rotation is a special case.)

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Non-rigid body transformations

Scaling is also known as dilatation, magnification or shrinking, 0 0 where x = xsx and y = ysy.

Scaling is an affine transformation (affine transformations preserve of lines, or curvature).

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Affine transformations

An affine transformation preserves

I collinearity between points (ie. three points which lie on a line continue to be collinear after the transformation), and

I ratios of distances between any two points. Therefore translation, rotation, scaling and shear are all affine transformations.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Scaling as a matrix operation:

 0     x xs 0 x 0 = y 0 ys y

Scaling in homogeneous coordinates:

 0       x xs 0 0 x xsx 0 y  =  0 ys 0 y = ysy 1 0 0 1 1 1

This shows how scaling can be achieved as a matrix operation.

I If xs = ys, get isotropic scaling. I Scaling is inverted by using reciprocals of scale factors, 1/xs and 1/ys. I Using scale factors of 1 gives an identity transformation.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Shear Horizontal shear:

0 x = x + xhy y 0 = y Everything keeps its vertical position, but is dragged sideways in proportion to its vertical position. A square is dragged out into a with the same height.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Horizontal shear in homogeneous coordinates:

 0       x 1 xh 0 x (x + xh ∗ y) y 0 = 0 1 0 y =  (y)  1 0 0 1 1 1

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Vertical shear:

x0 = x 0 y = y + yhx

Everything keeps its horizontal position, but is dragged up or down in proportion to its horizontal position. A square is dragged out into a parallelogram with the same width.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Vertical shear in homogeneous coordinates:

x0  1 0 0 x  (x)  0 y  = yh 1 0 y = (yh ∗ x + y) 1 0 0 1 1 1

Shears are elementary transforms

I many useful transforms can be made by appropriate combinations of shears

I this can have theoretical and computational benefits

I for example, totations can be decomposed into shears and used for producing fast method of rotating, without need for using trogonometric calls

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Combining transformation matrices

Any transformation (or combination of transformations) can be expressed in homogeneous coordinates by means of the matrix equation:  0     x m00 m01 m02 x 0 y  = m10 m11 m12 y 1 0 0 1 1

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Affine transformations in 2D

Any affine transformation (or combination of transformations) can be expressed in homogeneous coordinates by means of the matrix equation:

 0      x m00 m01 m02 x 0  y  =  m10 m11 m12   y  1 0 0 1 1

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

  1 0 xt I 0 1 yt  (translation) 0 0 1   xs 0 0 I  0 ys 0 (scaling) 0 0 1 cos θ − sin θ 0  I  sin θ cos θ 0  (clockwise rotation by theta) 0 0 1]  cos θ sin θ 0 I − sin θ cos θ 0 (anti-clockwise rotation by theta) 0 0 1

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Example: rotation around an arbitrary 2D point

y y y y

P1

P1 q x x x x Original house After translation After rotation After translation of P1 to origin to original P1 (Foley Figure 5.10)

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Rotating a point (x, y) about an arbitrary fixed point (rather than the origin), to obtain a new point (xr , yr ).   xr  yr  1         1 0 cx cos θ − sin θ 0 1 0 −cx x =  0 1 cy   sin θ cos θ 0   0 1 −cy   y  0 0 1 0 0 1 0 0 1 1     cos θ − sin θ cx (1 − cos θ) + cy sin θ x =  sin θ cos θ cy (1 − cos θ) − cx sin θ   y  0 0 1 1 Homogeneous coordinates simplify process of combining transformation, similarly for scaling an object about an arbitrary point.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Example: rotation, scaling and translation

P2

P1

Original Translate P1 Scale Rotate Translate to house to origin final position P2

T (x2, y2) · R(θ) · S(sx , sy ) · T (−x1, −y1) (Foley Figure 5.11)

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Commutivity of transformation matrices If M1 and M2 each represent a fundamental translation, scaling or rotation, when is M1 · M2 = M2 · M1? In general, for two matrices is not commutative, that is

M1 · M2 6= M2 · M1 However, commutivity holds in the following special cases when

M1 M2 Translate Translate Scale Scale Rotate Rotate Scale (with sx = sy ) Rotate

When sx = sy , termed isotropic scaling. Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Example: drawing circles and If a point is rotated incrementally by some (small) angle δ, then the trajectory it traces out is a circle. If we draw (short) line segments connecting successive points, this leads to an important fast method for drawing circles or parts of circles—circular arcs (if δ is small enough).   x0 More specifically, if x0 = is our starting point on the circle, y0 then we compute the sequence

xk+1 = Rδxk

where Rδ is a rotation (note this draws a circle centred on the origin). How would the circle-drawing methods be adapted to draw ellipses with arbitrary centres?

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

That is, we want a matrix E such that the sequence of points generated by xk+1 = Exk lies along the .

However, Rδ only steps us along successive points that lie on a circle, incremented by angle δ. What about an ellipse? Our method for drawing circles should be able to be adapted for drawing ellipses quickly, using a combination of matrix transformations. Note that an ellipse at arbitrary orientation can be created by stretching and rotating a unit circle. Initially, let’s consider only ellipses centred at the origin.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

We can obtain an ellipse by stretching and rotating the points of a unit circle. This can be achieved by 1. multiplying by a scaling matrix to achieve the s 0  S = x 0 sy (note different scaling in x and y directions for ellipse), followed by

2. multiplying by a rotation Rθ to orient the point along the path of the ellipse. thus

SRθ

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

If we have a point on an ellipse, we can move it back to the corresponding point on the unit circle, by multiplying it by the -1 inverse of these transforms (SRθ) . Once we get it on the unit circle, we can step it to the next point by multiplying by Rδ. Then we can get that point back on the ellipse by multiplying by (SRθ). Thus

-1 E = (SRθ) Rδ (SRθ) -1 -1 = Rθ S RδSRθ -1 = R(−θ)S RδSRθ

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

I Note that, in general, scaling and rotation don’t commute, so there’s no obvious way to simplify this formula further. I The main computational property of this method is that E can be computed just once, so that each point generated along the ellipse requires just a matrix multiply. I Note that although can calculate sin(δ) and cos(δ) will need to calculate coefficients containing sin(θ) and cos(θ) at each step, since the transformation of a point at each step through different of the ellipse changes. I A nice property is that the steps are smaller where the curvature of the ellipse is higher (at the ends), and bigger where the curvature of the ellipse is lower (along the sides). The method adapts to distribute the number of points used along the ellipse to represent it most accurately when straight line segments are drawn between points.

Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates Introduction Vectors and matrices Translation Rotation Homogeneous coordinates Affine transformations

Summary

I Geometric transformations can be conveniently encoded as operations in matrix algebra. I Rigid body transformations (rotation, translation and reflection) preserve distances and correspond to rigid that is typically modelled in . I However, translations and rotations cannot be conveniently combined using only multiplication. I Representing geometric transformations as matrices in homogeneous coordinates allows the combination of multiple transformations using matrix multiplication. I Translation, rotation and reflection are affine transformation (they preserve distances between points). I Scaling and shear are not affine and do not preserve distances, however rotations can be decomposed into shears with computational benefits. Adrian Pearce University of Melbourne COMP30019 Graphics and InteractionTransformation geometry and homogeneous coordinates