1 2D Transformations 几何变换. 2 What is a transformation? A transformation is an operation that...

Preview:

Citation preview

1

2D Transformations

几何变换

2

What is a transformation?

A transformation is an operation that transforms or changes a shape (line, drawing etc.)

There are several basic ways you can change a shape:

translation (moving it)

rotation (turning it round)

scaling (making it bigger or smaller).

There are others, but we’ll worry about them later.

3

2D Geometrical Transformations

Translate

Rotate Scale

Shear

4

Translation

Consider the arrow below. We may want to move it from position A to position B.

A

B

dx

dy

x

y

5

Translate Points

We can translate points in the (x, y) plane to new positions by adding translation amounts to the coordinates of the points. For each point P(x, y) to be moved by dx units parallel to the x axis and by dy

units parallel to the y axis, to the new point P’(x’, y’ ). The translation has the following form:

y

x

dyy

dxx

'

'P(x,y)

P’(x’,y’)

dx

dy

6

Translating a line or shape

To translate a line, simply translate both of its end points.

Translating a shape therefore, simply translate all of the points.

The same applies for all transformations.

7

Scale Points

Points can be scaled (stretched) by sx along the x axis and by sy along the y axis into the new points by the multiplications:

We can specify how much bigger or smaller by means of a “scale factor”

To double the size of an object we use a scale factor of 2, to half the size of an obejct we use a scale factor of 0.5

ysy

xsx

y

x

'

'P(x,y)

P’(x’,y’)

xsx x

sy y

y

8

We first review the idea of sin and cos for a given angle :

Useful formulas:

Rotate Points

sin

cos

/sin

/cos

|| 22

ly

lx

ly

lx

yxOPl

sin)sin(cos)cos(

sinsincoscos)cos(

sincoscossin)sin(

P

x

y

l

O

9

Rotate Points (cont.)

Points can be rotated through an angle about the origin:

cossin

cossinsincos

)sin()sin(|'|'

sincos

sinsincoscos

)cos()cos(|'|'

|||'|

yx

ll

lOPy

yx

ll

lOPx

lOPOP

P(x,y)

P’(x’,y’)

xx’

y’

y

l

O

10

Identity transformations

Some transformations lead to no change in the shape/drawing Translate(0,0) Rotate(0) Scale(1,1)

11

Order of transformations

The order in which transformations are applied to a shape is important.

Performing a translation followed by a rotation, will give an entirely different drawing to a performing the rotation followed by

the same translation.

12

Order of transformations

x

y

rotation by 45° followed by translation (100,0)

13

Order of transformations

x

translation (100,0) followed by rotation by 45°

y

14

Rotation around local origin

At the moment, our rotation is centred around the origin. What is often required is to spin the shape in its original location (in-situ)

x

y

local origin

15

Rotation around local origin

This can be achieved by combining two of our existing transformations: Translate the shape to the origin, rotate it the required amount about the origin and translate it back to where it was.

16

Local rotation

x

y

dy

dx

17

Local rotation

x

y

18

Local rotation

y

x

y

19

Local rotation

x

+dy

+dx

20

Summary

So we have looked at simple transformation of points/lines etc.

Next we will look at transformations using matrices.

21

2D Transformations with Matrices

22

2D Geometrical Transformations

Translate

Rotate Scale

Shear

23

Translate Points

Recall.. We can translate points in the (x, y) plane to new positions by adding translation amounts to the coordinates of the points. For each point P(x, y) to be moved by dx units parallel to the x axis and by dy

units parallel to the y axis, to the new point P’(x’, y’ ). The translation has the following form:

y

x

dyy

dxx

'

'

P(x,y)

P’(x’,y’)

dx

dy

In matrix format:

y

x

d

d

y

x

y

x

'

'

If we define the translation matrix , then we have P’ =P + T.

y

x

d

dT

24

Scale Points

Points can be scaled (stretched) by sx along the x axis and by sy along the y axis into the new points by the multiplications:

We can specify how much bigger or smaller by means of a “scale factor”

To double the size of an object we use a scale factor of 2, to half the size of an obejct we use a scale factor of 0.5

ysy

xsx

y

x

'

'

P(x,y)

P’(x’,y’)

xsx x

sy y

y

y

x

s

s

y

x

y

x

0

0

'

'

If we define , then we have P’ =SP

y

x

s

sS

0

0

25

Rotate Points (cont.)

Points can be rotated through an angle about the origin:

cossin

cossinsincos

)sin()sin(|'|'

sincos

sinsincoscos

)cos()cos(|'|'

|||'|

yx

ll

lOPy

yx

ll

lOPx

lOPOP

P(x,y)

P’(x’,y’)

xx’

y’

y

l

O

y

x

y

x

cossin

sincos

'

'

P’ =RP

26

Review…

Translate: P’ = P+T Scale: P’ = SP Rotate: P’ = RP

Spot the odd one out… Multiplying versus adding matrix… Ideally, all transformations would be the same..

easier to code Solution: Homogeneous Coordinates

27

Homogeneous Coordinates

For a given 2D coordinates (x, y), we introduce a third dimension:

[x, y, 1]

In general, a homogeneous coordinates for a 2D point has the form:

[x, y, W]

Two homogeneous coordinates [x, y, W] and [x’, y’, W’] are said to be of the same (or equivalent) if

x = kx’ eg: [2, 3, 6] = [4, 6, 12]y = ky’ for some k ≠ 0 where k=2W = kW’

Therefore any [x, y, W] can be normalised by dividing each element by W:[x/W, y/W, 1]

28

Homogeneous Transformations

Now, redefine the translation by using homogeneous coordinates:

Similarly, we have:

y

x

d

d

y

x

y

x

'

'

1100

10

01

1

'

'

y

x

d

d

y

x

y

x

PTP '

1100

00

00

1

'

'

y

x

s

s

y

x

y

x

1100

0cossin

0sincos

1

'

'

y

x

y

x

Scaling Rotation

P’ = S P P’ = R P

29

Composition of 2D Transformations

1. Additivity of successive translations

We want to translate a point P to P’ by T(dx1, dy1) and then to P’’ by another T(dx2, dy2)

On the other hand, we can define T21= T(dx1, dy1) T(dx2, dy2) first, then apply T21 to P:

where

]),()[,('),('' 112222 PddTddTPddTP yxyxyx

PTP 21''

100

10

01

100

10

01

100

10

01

),(),(

21

21

1

1

2

2

112221

yy

xx

y

x

y

x

yxyx

dd

dd

d

d

d

d

ddTddTT

30

T(-1,2) T(1,-1)

(2,1)

(1,3)

(2,2)

100

110

001

100

210

101

100

110

101

21T

Examples of Composite 2D Transformations

31

Composition of 2D Transformations (cont.)

2. Multiplicativity of successive scalings

where

PS

PssSssS

PssSssSP

yxyx

yxyx

21

1122

1122

)],(),([

]),()[,(''

100

0*0

00*

100

00

00

100

00

00

),(),(

12

12

1

1

2

2

112221

yy

xx

y

x

y

x

yxyx

ss

ss

s

s

s

s

ssSssSS

32

Composition of 2D Transformations (cont.)

3. Additivity of successive rotations

where

PR

PRR

PRRP

21

12

12

)]()([

])()[(''

100

0)cos()sin(

0)sin()cos(

100

0cossin

0sincos

100

0cossin

0sincos

)()(

1212

1212

11

11

22

22

1221

RRR

33

Composition of 2D Transformations (cont.)

4. Different types of elementary transformations discussed above can be concatenated as well.

where

MP

PddTR

PddTRP

yx

yx

)],()([

]),()[('

),()( yx ddTRM

34

Consider the following two questions:

1) translate a line segment P1 P2, say, by -1 units in the x direction and -2 units in the y direction.

2). Rotate a line segment P1 P2, say by degrees counter clockwise, about P1.

P1(1,2)

P2(3,3)

P’2

P’1

P1(1,2)

P2(3,3)P’2

P’1

35

Other Than Point Transformations…

Translate Lines: translate both endpoints, then join them.

Scale or Rotate Lines: More complex. For example, consider to rotate an arbitrary line about a point P1, three steps are needed:1). Translate such that P1 is at the origin;2). Rotate;3). Translate such that the point at the origin returns to P1.

T(-1,-2) R()

P1(1,2)

P2(3,3)

P2(2,1)T(1,2)P2

P2

P1

P1P1

36

Another Example.

Scale

Translate

Rotate

Translate

37

Rigid-Body vs. Affine Transformations

A transformation matrix of the form

where the upper 22 sub-matrix is orthogonal, preserves angles and lengths. Such transforms are called rigid-body transformations, because the body or object being transformed is not distorted in any way. An arbitrary sequence of rotation and translation matrices creates a matrix of this form.

The product of an arbitrary sequence of rotation, translations, and scale matrices will cause an affine transformation, which have the property of preserving parallelism of lines, but not of lengths and angles.

1002221

1211

y

x

trr

trr

38

Rigid-Body vs. Affine Transformations (cont.)

Shear transformation is also affine.

Unit cube45º

Scale in x, not in y

Rigid- bodyTransformation

AffineTransformation

Shear in the x direction Shear in the y direction

100

010

01 a

SH x

100

01

001

bSH y

39

40

41

42

P1

P3P2

1 2

1

2P2'

P1'P3'

1 2

1

2

3

Original Transformed

Translate P2->O, T(-1,-1) Rotate R(-90) Shear sHy(-1) Scale S(2,1) Translate O->P2’ T(1,2) M = T(1,2) * S(2,1)*sHy(-1)*R(-90)*T(-1,-1)=

100

411

120

43

44

45

Prove that we can transform a line by transforming its endpoints and then constructing a new line between the transformed endpoints.

Answer:

S = P + t*(Q- P) , 0<= t <= 1

M can be any transform we discussed,then

S’ = M*S = M*(P + t*(Q-P))

= M*P + t*(M*Q – M*P))

= P’ + t*(Q’-P’)

Q

P

46

3D Transformations

2D coordinates 3D coordinates

x

y

x

y

z

xz

y

Right-handed coordinate system:

47

3D Transformations (cont.)

1. Translation in 3D is a simple extension from that in 2D:

2. Scaling is similarly extended:

1000

100

010

001

),,(z

y

x

zyx d

d

d

dddT

1000

000

000

000

),,(z

y

x

zyx s

s

s

sssS

48

3D Transformations (cont.)

3. The 2D rotation introduced previously is just a 3D rotation

about the z axis.

similarly we have:X

Y

Z

1000

0100

00cossin

00sincos

)(

zR

1000

0cossin0

0sincos0

0001

)(

xR

1000

0cos0sin

0010

0sin0cos

)(

yR

49

More Rotations

We have shown how to rotate about one of the principle axes, i.e. the

axes constituting the coordinate system. There are more we can do,

for example, to perform a rotation about an arbitrary axis:

X

Y

Z

P2(x2, y2 , z2)

P1(x1, y1 , z1)

We want to rotate an object about an

axis in space passing through (x1, y1, z1)

and (x2, y2, z2).

50

Rotating About An Arbitrary AxisY

Z

P2

P1

1). Translate the object by

(-x1, -y1, -z1): T(-x1, -y1, -z1)

X

Y

ZP2

P1

2). Rotate the axis about x so that

it lies on the xz plane: Rx()

X

X

Y

ZP2

P1

3). Rotate the axis about y so

that it lies on z: Ry ()

X

Y

ZP2

P1

4). Rotate object about z by :

Rz()

51

Rotating About An Arbitrary Axis (cont.)After all the efforts, don’t forget to undo the rotations and the translation!

Therefore, the mixed matrix that will perform the required task of rotating an

object about an arbitrary axis is given by:

M = T(x1,y1,z1) Rx(-)Ry(-) Rz() Ry() Rx()T(-x1,-y1,-z1)

Finding is trivial, but what about ?

The angle between the z axis and the

projection of P1 P2 on yz plane is .

X

Y

Z

P2

P1

52

conclusion

snml

rihg

qfed

pcba1) Left_up 3*3 sub matrix:

scale, rotate, shear

2) [p q r]’: translate

3) [l m n] : projection

4) s : Global scale factor

53

Viewing in 3D

54

Projections

Display device (a screen) is 2D… How do we map 3D objects to 2D space?

2D to 2D is straight forward… 2D window to world.. and a viewport on the 2D surface. Clip what won't be shown in the 2D window, and map the

remainder to the viewport.

3D to 2D is more complicated… Solution : Transform 3D objects on to a 2D plane using

projections

55

Projections

In 3D… View volume in the world Projection onto the 2D projection plane A viewport to the view surface

Process… 1… clip against the view volume, 2… project to 2D plane, or window, 3… map to viewport.

56

Projections

Conceptual Model of the 3D viewing process

57

58

59

Projections

Projections: key terms… Projection from 3D to 2D is defined by straight projection

rays (projectors) emanating from the 'center of projection', passing through each point of the object, and intersecting the 'projection plane' to form a projection.

60

Types of projections

2 types of projections perspective and parallel.

Key factor is the center of projection. if distance to center of projection is finite : perspective if infinite : parallel

61

Perspective v Parallel

Perspective: visual effect is similar to human visual system... has 'perspective foreshortening'

size of object varies inversely with distance from the center of projection.

angles only remain intact for faces parallel to projection plane.

Parallel: less realistic view because of no foreshortening however, parallel lines remain parallel. angles only remain intact for faces parallel to projection

plane.

62

Perspective Projections

Any parallel lines not parallel to the projection plane, converge at a vanishing point. There are an infinite number of these, 1 for each of the

infinite amount of directions line can be oriented.

If a set of lines are parallel to one of the three principle axes, the vanishing point is called an axis vanishing point. There are at most 3 such points, corresponding to the

number of axes cut by the projection plane.

63

Perspective Projections

Example: if z projection plane cuts the z axis: normal to it, so only z

has a principle vanishing point, as x and y are parallel and have none.

Can categorise perspective projections by the number of principle vanishing points, and the number of axes the projection plane cuts.

64

Perspective Projections

2 different examples of a one-point perspective projection of a cube.(note: x and y parallel lines do not converge)

65

Perspective Projections

Two-point perspective projection: This is often used in architectural, engineering

and industrial design drawings. Three-point is used less frequently as it adds little

extra realism to that offered by two-point perspective projection.

66

Perspective Projections

Two-point perspective projection:

67

Perspective Projections

p (x,y,z)

z

y

Projection plane

ps(xs,ys)C

d

By similar triangles:

p(x,y,z)

z

x

Projection plane

ps(xs,ys)C

d

d

x

dz

x s

dzx

xs

1

dzy

ys

1

68

Perspective Projections

1

.

1/100

0000

0010

0001

1

0 z

y

x

dd

z

y

x

s

s

1

0

1

1

1

0

dzydz

x

d

z

y

x

s

s

s

s

69

Parallel Projections

2 principle types: orthographic and oblique.

Orthographic : direction of projection = normal to the projection plane.

Oblique : direction of projection != normal to the projection plane.

70

Parallel Projections

Orthographic (or orthogonal) projections: front elevation, top-elevation and side-elevation. all have projection plane perpendicular to a principle axes.

Useful because angle and distance measurements can be made...

However, As only one face of an object is shown, it can be hard to create a mental image of the object, even when several view are available.

71

Parallel Projections

Orthogonal projections:

Recommended