<<

2D and 3D Transformations, Homogeneous Coordinates Lecture 03

Patrick Karlsson [email protected]

Centre for Image Analysis Uppsala University

Computer Graphics November 6 2006

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. 1 / 23

Reading Instructions Chapters 4.1–4.9.

Edward Angel. “Interactive Computer Graphics: A Top-down Approach with OpenGL”, Fourth Edition, Addison-Wesley, 2004.

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 2 / 23 Todays lecture ... in the pipeline

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 3 / 23

Scalars, points, and vectors

Scalars α, β Real (or complex) numbers.

Points P, Q Locations in space (but no size or shape).

Vectors u, v Directions in space (magnitude but no position).

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 4 / 23 Mathematical spaces

Scalar field A set of scalars obeying certain properties. New scalars can be formed through addition and multiplication.

(Linear) Vector space Made up of scalars and vectors. New vectors can be created through scalar-vector multiplication, and vector-vector addition.

Affine space An extended vector space that include points. This gives us additional operators, such as vector-point addition, and point-point subtraction.

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 5 / 23

Data types

Polygon based objects Objects are described using polygons. A polygon is defined by its vertices (i.e., points). Transformations manipulate the vertices, thus manipulates the objects.

Some examples in 2D Scalar α 1 float. Point P(x, y) 2 floats. Vector v(x, y) 2 floats. Matrix M 4 floats.

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 6 / 23 Transformations

To create and move objects we need to be able to transform objects in different ways. There are many classes of transformations.

Transformations Translate (Move around.) Rotate Scale Shear ( and .)

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 7 / 23

Transformations Simply add a translation vector

x0 = x + dx y 0 = y + dy

P’(x’,y’)

P(x,y)

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 8 / 23 Transformations Rotation P(x, y) in polar coordinates

x = r cos(φ) y = r sin(φ) y

P0(x 0, y 0) in polar coordinates P’(x’,y’)

x0 = r cos(θ + φ) = r cos(φ) cos(θ) − r sin(φ) sin(θ) P(x,y) y 0 = r sin(θ + φ) = r cos(φ) sin(θ) − r sin(φ) cos(θ) r

r

θ Substitute x and y φ x

x0 = x cos(θ) − y sin(θ) y 0 = x sin(θ) + y cos(θ)

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 9 / 23

Transformations Rotation Arbitrary rotation Translate the rotation axis to the origin. Rotate. Translate back.

y

x

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 10 / 23 Transformations Scaling around the origin Multiply by a scale factor

0 x = sx x 0 y = sy y

y

P’(x’,y’)

P(x,y) x x x’

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 11 / 23

Transformations Shear Shear in the x direction

x0 = x + y cot(θ) y 0 = y

y

P(x,y) P’(x’,y’)

θ x

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 12 / 23 Affine transforms are linear!

αf (x1 + x2) = αf (x1) + αf (x2)

The linearity implies that in order to move an object we only need to transform the individual vertices making up the object, since the inner points are defined by linear combinations of the vertices.

P1

P0

P(α) = (1 − α)P0 + αP1 α = [0, 1]

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 13 / 23

Vector math short revision

The inner (or dot) product is written as u · v. If u · v = 0, then vectors u and v are orthogonal. The squared magnitude of a vector v is given by the inner product v · v = ||v||2. The angle between two vectors u and v is given by u·v ||u||||v|| = cos(θ). Two nonparallel vectors can generate a third vector that is orthogonal to them both by using the cross product n = u × v. The new vector n can be then be used to create a vector that is orthogonal to both u and n by performing w = u × n. The three vectors u, n, and w are mutually orthogonal. The magnitude of a cross product gives the sine of the angle ||u×v|| between the vectors, | sin(θ)| = ||u||||v|| .

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 14 / 23 Vectors and matrices

Translation „ x0 « „ x « „ dx « = + y 0 y dy

Rotation „ x0 « „ cos(θ) − sin(θ) «„ x « = y 0 sin(θ) cos(θ) y

Scaling

„ 0 « „ «„ « x sx 0 x 0 = y 0 sy y

Shearx „ x0 « „ 1 cot(θ) «„ x « = y 0 0 1 y

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 15 / 23

Translation is different!

Translation in 2D „ x0 « „ x « „ dx « = + y 0 y dy

“Stepping up one dimension”

0 x0 1 0 1 0 dx 1 0 x 1 @ y 0 A = @ 0 1 dy A @ y A W 0 0 1 W

If W = 1, then this called a Homogeneous coordinate

0 x0 1 0 1 0 dx 1 0 x 1 @ y 0 A = @ 0 1 dy A @ y A 1 0 0 1 1

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 16 / 23 Using homogeneous coordinates Translation 0 x0 1 0 1 0 dx 1 0 x 1 0 P = T (dx, dy)P @ y 0 A = @ 0 1 dy A @ y A 1 0 0 1 1

Rotation 0 x0 1 0 cos(θ) − sin(θ) 0 1 0 x 1 0 P = R(θ)P @ y 0 A = @ sin(θ) cos(θ) 0 A @ y A 1 0 0 1 1

Scaling

0 0 1 0 1 0 1 x sx 0 0 x 0 0 P = S(sx , sy )P @ y A = @ 0 sy 0 A @ y A 1 0 0 1 1

Shearx 0 x0 1 0 1 cot(θ) 0 1 0 x 1 0 0 P = Hx (θ)P @ y A = @ 0 1 0 A @ y A 1 0 0 1 1 Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 17 / 23

Concatenation of transformations Observe! The order of the matrices (right to left) is important!

P0 = T −1(S(R(T (P)))) = (T −1SRT )P M = T −1SRT P0 = MP

y

x

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 18 / 23 Homogeneous coordinates What about the last element W ?  x   αx   x/W   x/W   y  ⇒  αy  ⇒  y/W  ⇒  y/W  W αW W /W 1 | {z } This is called to homogenize.

Cartesian coordinates:  x   x/W  c = / yc y W h

If W = 0: A point at infinity = a vector  x   y  0

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 19 / 23

Stepping up to three dimensions

Translation and scaling is the same.

Rotation is a little more tricky, and becomes Rx , Ry , and Rz .

Observe that Rx Ry 6= Ry Rx .

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 20 / 23 3D transformations

Translation 0 x0 1 0 1 0 0 dx 1 0 x 1 0 0 B y C B 0 1 0 dy C B y C P = T (dx, dy, dz)P B C = B C B C @ z0 A @ 0 0 1 dz A @ z A 1 0 0 0 1 1

Scaling

0 s 0 0 0 1 0 x 1 0 x0 1 x 0 s 0 0 y P0 = S(s , s , s )P y 0 = B y C B C x y z @ A B 0 0 s 0 C B z C z0 @ z A @ A 0 0 0 1 1

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 21 / 23

3D transformations

Rotation around the x-axis 0 x0 1 0 1 0 0 0 1 0 x 1 0 0 B y C B 0 cos(θ) − sin(θ) 0 C B y C P = Rx (θ)P B C = B C B C @ z0 A @ 0 sin(θ) cos(θ) 0 A @ z A 1 0 0 0 1 1

Rotation around the y-axis

0 x0 1 0 cos(θ) 0 sin(θ) 0 1 0 x 1 0 0 B y C B 0 1 0 0 C B y C P = Ry (θ)P B C = B C B C @ z0 A @ − sin(θ) 0 cos(θ) 0 A @ z A 1 0 0 0 1 1

Rotation around the z-axis 0 x0 1 0 cos(θ) − sin(θ) 0 0 1 0 x 1 0 0 B y C B sin(θ) cos(θ) 0 0 C B y C P = Rz (θ)P B C = B C B C @ z0 A @ 0 0 1 0 A @ z A 1 0 0 0 1 1

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 22 / 23 Lecture 04

Do not miss tomorrows exiting episode: 3D viewing and projections . Room P2344 at 13:15 - 15:00.

Patrick Karlsson (Uppsala University) Transformations and Homogeneous Coords. Computer Graphics 23 / 23