2d and 3d transformations, homogeneous coordinates ... · pdf file2d and 3d transformations,...

12
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. Computer Graphics 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

Upload: hoangduong

Post on 09-Mar-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2D and 3D Transformations, Homogeneous Coordinates ... · PDF file2D and 3D Transformations, Homogeneous Coordinates Lecture 03 Patrick Karlsson @cb.uu.se Centre for Image Analysis

2D and 3D Transformations,Homogeneous Coordinates

Lecture 03

Patrick [email protected]

Centre for Image AnalysisUppsala University

Computer GraphicsNovember 6 2006

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

Reading InstructionsChapters 4.1–4.9.

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

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

Page 2: 2D and 3D Transformations, Homogeneous Coordinates ... · PDF file2D and 3D Transformations, Homogeneous Coordinates Lecture 03 Patrick Karlsson @cb.uu.se Centre for Image Analysis

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, QLocations in space (but no size or shape).

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

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

Page 3: 2D and 3D Transformations, Homogeneous Coordinates ... · PDF file2D and 3D Transformations, Homogeneous Coordinates Lecture 03 Patrick Karlsson @cb.uu.se Centre for Image Analysis

Mathematical spaces

Scalar fieldA set of scalars obeying certain properties. New scalars can be formedthrough addition and multiplication.

(Linear) Vector spaceMade up of scalars and vectors. New vectors can be created throughscalar-vector multiplication, and vector-vector addition.

Affine spaceAn extended vector space that include points. This gives us additionaloperators, 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 objectsObjects are described using polygons.

A polygon is defined by its vertices (i.e., points).

Transformations manipulate the vertices, thus manipulates theobjects.

Some examples in 2DScalar α 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

Page 4: 2D and 3D Transformations, Homogeneous Coordinates ... · PDF file2D and 3D Transformations, Homogeneous Coordinates Lecture 03 Patrick Karlsson @cb.uu.se Centre for Image Analysis

Transformations

To create and move objects we need to be able to transformobjects in different ways.

There are many classes of transformations.

TransformationsTranslate (Move around.)

Rotate

Scale

Shear (Scaling and rotation.)

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

TransformationsTranslation

Simply add a translation vector

x ′ = x + dx

y ′ = y + dy

P(x,y)

P’(x’,y’)

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

Page 5: 2D and 3D Transformations, Homogeneous Coordinates ... · PDF file2D and 3D Transformations, Homogeneous Coordinates Lecture 03 Patrick Karlsson @cb.uu.se Centre for Image Analysis

TransformationsRotation

P(x , y) in polar coordinates

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

P ′(x ′, y ′) in polar coordinates

x ′ = r cos(θ + φ) = r cos(φ) cos(θ) − r sin(φ) sin(θ)

y ′ = r sin(θ + φ) = r cos(φ) sin(θ) − r sin(φ) cos(θ)

Substitute x and y

x ′ = x cos(θ) − y sin(θ)

y ′ = x sin(θ) + y cos(θ)

θφ

r

r

P(x,y)

P’(x’,y’)

x

y

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

TransformationsRotation

Arbitrary rotationTranslate the rotation axis to the origin.

Rotate.

Translate back.

y

x

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

Page 6: 2D and 3D Transformations, Homogeneous Coordinates ... · PDF file2D and 3D Transformations, Homogeneous Coordinates Lecture 03 Patrick Karlsson @cb.uu.se Centre for Image Analysis

TransformationsScaling around the origin

Multiply by a scale factor

x ′ = sxx

y ′ = syy

x

y

P(x,y)

P’(x’,y’)

x x’

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

TransformationsShear

Shear in the x direction

x ′ = x + y cot(θ)

y ′ = y

x

y

θ

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

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

Page 7: 2D and 3D Transformations, Homogeneous Coordinates ... · PDF file2D and 3D Transformations, Homogeneous Coordinates Lecture 03 Patrick Karlsson @cb.uu.se Centre for Image Analysis

Affine transforms are linear!

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

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

P0

P1

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

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

Vector mathshort revision

The inner (or dot) product is written as u · v . If u · v = 0, thenvectors u and v are orthogonal.

The squared magnitude of a vector v is given by the inner productv · v = ||v ||2.

The angle between two vectors u and v is given byu·v

||u||||v || = cos(θ).

Two nonparallel vectors can generate a third vector that isorthogonal 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 isorthogonal to both u and n by performing w = u × n. The threevectors u, n, and w are mutually orthogonal.

The magnitude of a cross product gives the sine of the anglebetween the vectors, | sin(θ)| = ||u×v ||

||u||||v || .

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

Page 8: 2D and 3D Transformations, Homogeneous Coordinates ... · PDF file2D and 3D Transformations, Homogeneous Coordinates Lecture 03 Patrick Karlsson @cb.uu.se Centre for Image Analysis

Vectors and matrices

Translation „x ′

y ′

«=

„xy

«+

„dxdy

«

Rotation „x ′

y ′

«=

„cos(θ) − sin(θ)sin(θ) cos(θ)

« „xy

«

Scaling „x ′

y ′

«=

„sx 00 sy

« „xy

«

Shearx „x ′

y ′

«=

„1 cot(θ)0 1

« „xy

«

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

Translation is different!

Translation in 2D „x ′

y ′

«=

„xy

«+

„dxdy

«

“Stepping up one dimension”0@ x ′

y ′

W

1A =

0@ 1 0 dx0 1 dy0 0 1

1A 0@ xyW

1A

If W = 1, then this called a Homogeneous coordinate0@ x ′

y ′

1

1A =

0@ 1 0 dx0 1 dy0 0 1

1A 0@ xy1

1A

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

Page 9: 2D and 3D Transformations, Homogeneous Coordinates ... · PDF file2D and 3D Transformations, Homogeneous Coordinates Lecture 03 Patrick Karlsson @cb.uu.se Centre for Image Analysis

Using homogeneous coordinatesTranslation

P′ = T (dx , dy)P

0@ x ′

y ′

1

1A =

0@ 1 0 dx0 1 dy0 0 1

1A 0@ xy1

1A

Rotation

P′ = R(θ)P

0@ x ′

y ′

1

1A =

0@ cos(θ) − sin(θ) 0sin(θ) cos(θ) 0

0 0 1

1A 0@ xy1

1A

Scaling

P′ = S(sx , sy )P

0@ x ′

y ′

1

1A =

0@ sx 0 00 sy 00 0 1

1A 0@ xy1

1A

Shearx

P′ = Hx (θ)P

0@ x ′

y ′

1

1A =

0@ 1 cot(θ) 00 1 00 0 1

1A 0@ xy1

1APatrick 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!

P′ = T−1(S(R(T (P)))) = (T−1SRT )P

M = T−1SRT

P′ = MP

y

x

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

Page 10: 2D and 3D Transformations, Homogeneous Coordinates ... · PDF file2D and 3D Transformations, Homogeneous Coordinates Lecture 03 Patrick Karlsson @cb.uu.se Centre for Image Analysis

Homogeneous coordinatesWhat about the last element W? x

yW

αxαyαW

x/Wy/W

W/W

x/Wy/W

1

︸ ︷︷ ︸

This is called to homogenize.

Cartesian coordinates:(xc

yc

)=

(x/Wy/W

)h

If W = 0: A point at infinity = a vector xy0

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 RxRy 6= RyRx .

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

Page 11: 2D and 3D Transformations, Homogeneous Coordinates ... · PDF file2D and 3D Transformations, Homogeneous Coordinates Lecture 03 Patrick Karlsson @cb.uu.se Centre for Image Analysis

3D transformations

Translation

P′ = T (dx , dy , dz)P

0BB@x ′

y ′

z′

1

1CCA =

0BB@1 0 0 dx0 1 0 dy0 0 1 dz0 0 0 1

1CCA0BB@

xyz1

1CCA

Scaling

P′ = S(sx , sy , sz)P

0@ x ′

y ′

z′

1A =

0BB@sx 0 0 00 sy 0 00 0 sz 00 0 0 1

1CCA0BB@

xyz1

1CCA

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

3D transformations

Rotation around the x-axis

P′ = Rx (θ)P

0BB@x ′

y ′

z′

1

1CCA =

0BB@1 0 0 00 cos(θ) − sin(θ) 00 sin(θ) cos(θ) 00 0 0 1

1CCA0BB@

xyz1

1CCA

Rotation around the y-axis

P′ = Ry (θ)P

0BB@x ′

y ′

z′

1

1CCA =

0BB@cos(θ) 0 sin(θ) 0

0 1 0 0− sin(θ) 0 cos(θ) 0

0 0 0 1

1CCA0BB@

xyz1

1CCA

Rotation around the z-axis

P′ = Rz(θ)P

0BB@x ′

y ′

z′

1

1CCA =

0BB@cos(θ) − sin(θ) 0 0sin(θ) cos(θ) 0 0

0 0 1 00 0 0 1

1CCA0BB@

xyz1

1CCA

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

Page 12: 2D and 3D Transformations, Homogeneous Coordinates ... · PDF file2D and 3D Transformations, Homogeneous Coordinates Lecture 03 Patrick Karlsson @cb.uu.se Centre for Image Analysis

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