48
Computer Graphics CMU 15-462/15-662, Fall 2016 Lecture 4: Transforms

Lecture 4: Transforms - 15-462/662 Spring 202015462.courses.cs.cmu.edu › fall2016content › lectures › 04_transfor… · CMU 15-462/662, Fall 2016 Brief recap from last class

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Computer Graphics

CMU 15-462/15-662, Fall 2016

Lecture 4:

Transforms

CMU 15-462/662, Fall 2016

Brief recap from last class

▪ How to draw a triangle

- Why focus on triangles, and not quads,

pentagons, etc?

- What was specific to triangles in what we

discussed last class?

CMU 15-462/662, Fall 2016

What in the world is this?

CMU 15-462/662, Fall 2016

Cube

(-1, -1, -1) (1, -1, -1)

(1, -1, 1)(-1, -1, 1)

(1, 1, -1)

(1, 1, 1)

(-1, 1, -1)

(-1, 1, 1)

CMU 15-462/662, Fall 2016

Cube man Stretched out cube

moved up

Squishy cube

moved to the rightSlanty cube

moved down and

to the left

The original cube

CMU 15-462/662, Fall 2016

f transforms x to f(x)

x

f(x)

CMU 15-462/662, Fall 2016

And what is our favorite type of

transformation?

CMU 15-462/662, Fall 2016

Linear transforms

𝒆1

𝒆2

▪ But what does it mean?

CMU 15-462/662, Fall 2016

Linear transforms

f(u + v) = f(u) + f(v)

f(au) = af(u)

CMU 15-462/662, Fall 2016

Linear transforms

If a map can be expressed as

f(u) =σ𝑖=1𝑚 𝑢𝑖𝒂𝑖

with fixed vectors 𝒂𝑖, then it is linear

CMU 15-462/662, Fall 2016

Linear transforms

▪ Do you know…

- what 𝑢1 and 𝑢2 are?

- what 𝒂1 and 𝒂2 are?

𝒆1

𝒆2

CMU 15-462/662, Fall 2016

Linear transforms

- 𝒖 is a linear combination of 𝒆1 and 𝒆2

- 𝒇(𝒖) is that same linear combination of 𝒂1 and 𝒂2

- 𝒂1 and 𝒂2 are 𝒇(𝒆1) and 𝐟(𝒆2)

- by knowing what 𝒆1 and 𝒆2 map to, you know how

to map the entire space!

𝒆1

𝒆2

CMU 15-462/662, Fall 2016

An example: Coordinate transformations

𝒊

𝒋Ƹ𝑖: 𝒊 + 𝒋

Ƹ𝒋: −2𝒊 + 𝒋

meMy friend

My friend says, look at 3 o’clock (in their coordinate

frame that means one “forward” and one to the “right”)!

Where should I look?

𝒇 𝒖 = 𝒇(𝑢1 Ƹ𝒊 + 𝑢2 Ƹ𝒋) = 𝑢1𝒇 Ƹ𝒊 + 𝑢2𝒇 Ƹ𝒋 = 𝑢111

+ 𝑢2−21

Direction in my friend’s coordinate frame

Same direction in my coordinate frame

𝒖

CMU 15-462/662, Fall 2016

Linear maps▪ In graphics we often talk about changing coordinate frames

(go from local to world to camera to screen coordinates)

▪ Equally useful to think about maps transforming a space (and

everything in it!)

CMU 15-462/662, Fall 2016

Let’s look at some transforms

that are important in graphics…

How do you formally tell a computer that this

cube should be squished and slanty?

CMU 15-462/662, Fall 2016

Scale

Uniform scale:

Non-uniform scale

Non axis-aligned scaling??S 𝒙 = 𝑥1𝑎𝒆1 + 𝑥2𝑏𝒆2

CMU 15-462/662, Fall 2016

Is uniform scale a linear transform?

Yes!

CMU 15-462/662, Fall 2016

Rotation

= rotate counter-clockwise by

CMU 15-462/662, Fall 2016

Rotation

= rotate counter-clockwise by

As angle changes, points move along circular trajectories.

CMU 15-462/662, Fall 2016

Rotation

= rotate counter-clockwise by

As angle changes, points move along circular trajectories.

Shape (distance between any two points) does not change!

(Rigid or isometric transformation)

CMU 15-462/662, Fall 2016

Rotation

What does look like?

▪ From x, compute 𝛼 and 𝑟

▪ Write down 𝑅𝜃(𝒙) as a function of 𝛼, 𝜃 and 𝑟(i.e. vector (𝑟,0) rotated by 𝛼 + 𝜃)

▪ Apply sum of angle formulae…

▪ Fine, but remember, we only need to know

how 𝒆1 and 𝒆2 are transformed!

𝛼

𝑟

CMU 15-462/662, Fall 2016

RotationSo, what happens to vectors (1, 0) and (0, 1) after rotation by θ?

Answer:

-𝑠𝑖𝑛 θ

𝑐𝑜𝑠 θ

𝑐𝑜𝑠 θ𝑠𝑖𝑛 θ

𝑅θ 𝒆1 = 𝑐𝑜𝑠 𝜃, 𝑠𝑖𝑛 𝜃 = 𝒂1𝑅θ 𝒆2 = −𝑠𝑖𝑛 𝜃, 𝑐𝑜𝑠 𝜃 = 𝒂2

So:

𝑅θ 𝒙 = 𝑥1𝒂1 + 𝑥2𝒂2

CMU 15-462/662, Fall 2016

Is rotation linear?

Yes!

CMU 15-462/662, Fall 2016

Rotation

▪ Note: all points are rotated about the origin

- By the way, what are we actually transforming here?

▪ What if we want to rotate about another point?

CMU 15-462/662, Fall 2016

Reflection

: reflection about y-axis

Reflections change “handedness”…

𝑅𝑒𝑦 𝒙

𝑅𝑒𝑦 𝒙0𝑅𝑒𝑦 𝒙1

𝑅𝑒𝑦 𝒙2 𝑅𝑒𝑦 𝒙3

Do you know what 𝑅𝑒𝑦 𝒙 looks like?

Do you know how to reflect about an arbitrary axis?

𝒙0 𝒙1

𝒙3 𝒙2

Is reflection a linear transform?

CMU 15-462/662, Fall 2016

Shear (in x direction)

𝐻 𝒙0 𝐻 𝒙1

𝐻 𝒙2𝐻 𝒙3

𝒙0 𝒙1

𝒙3 𝒙2

What does 𝐻 𝒙 look like?

Is shearing a linear transformation?

𝑯𝑎 𝒙 = 𝑥110

+ 𝑥2𝑎1

CMU 15-462/662, Fall 2016

Translation

𝑇𝒃 𝒙0

𝒙0 𝒙1

𝒙3 𝒙2

𝑇𝒃 𝒙1

𝑇𝒃 𝒙2𝑇𝒃 𝒙3

Let’s write 𝑇𝑏 𝒙 in the form

𝑇𝒃 𝒙 = 𝑥1??+ 𝑥2

??

such that 𝑇𝒃 𝒙 = 𝒙 + 𝒃

CMU 15-462/662, Fall 2016

Is translation linear?

No. Translation is affine.

𝒙 𝒙 + 𝒚𝒃

𝑇𝒃 𝒙

𝑇𝒃 𝒙 +𝑇𝒃 𝒚

𝒚

𝑇𝒃 𝒚

𝑇𝒃 𝒙 + 𝒚

𝒃

𝒃

CMU 15-462/662, Fall 2016

When at first you don’t succeed…

▪ We’ll turn affine transformations into linear

ones via

▪ But first, let’s use matrix notation to

represent linear transforms

Homogeneous coordinates

(aka projective coordinates)

CMU 15-462/662, Fall 2016

Linear transforms as matrix-vector products

𝑎11 𝑎12𝑎21 𝑎22

𝑥1𝑥2

=𝑎11𝑥1 + 𝑎12𝑥2𝑎21𝑥1 + 𝑎22𝑥2

= 𝑥1𝑎11𝑎21

+ 𝑥2𝑎12𝑎22

= 𝑥1𝒂𝟏 + 𝑥2𝒂𝟐

f(x) =σ𝑖=1𝑚 𝑥𝑖𝒂𝑖 = 𝑨𝒙

𝑨 ∗ 𝒙

CMU 15-462/662, Fall 2016

Linear transforms as matrix-vector products

Change of coordinate systems

𝑓 𝒙 = 𝑥111

+ 𝑥2−21

=1 −21 1

𝒙

𝒊

𝒋Ƹ𝑖: 𝒊 + 𝒋

Ƹ𝒋: −2𝒊 + 𝒋

meMy friend

𝒖

CMU 15-462/662, Fall 2016

Linear transforms as matrix-vector products

Non-uniform scale

S 𝒙 = 𝑥1𝑎𝒆1 + 𝑥2𝑏𝒆2

=𝑎 00 𝑏

𝒙

CMU 15-462/662, Fall 2016

Linear transforms as matrix-vector products

Rotation

𝑅θ 𝒆1 = 𝑐𝑜𝑠 𝜃, 𝑠𝑖𝑛 𝜃 = 𝒂1𝑅θ 𝒆2 = −𝑠𝑖𝑛 𝜃, 𝑐𝑜𝑠 𝜃 = 𝒂2

𝑅θ 𝒙 = 𝑥1𝒂1 + 𝑥2𝒂2

=cos 𝜃 −sin 𝜃sin 𝜃 cos 𝜃

𝒙

CMU 15-462/662, Fall 2016

Linear transforms as matrix-vector products

Shear

𝐻 𝒙 = 𝑥110

+ 𝑥2𝑎1

=1 𝑎0 1

𝒙

CMU 15-462/662, Fall 2016

Linear transforms as matrix-vector products

Translation

Not a linear map*…

*when we’re using Cartesian coordinates

CMU 15-462/662, Fall 2016

Key idea: lift 2D points to a 3D space

So the point is represented as the 3-vector:

And 2D transforms are represented by 3x3 matrices

For example: 2D rotation in homogeneous coordinates:

2D homogeneous coordinates (2D-H)

Q: how do the transforms we’ve seen so far affect the last coordinate?

(𝑥1, 𝑥2)𝑥1𝑥21

cos 𝜃 −sin 𝜃 0sin 𝜃 cos 𝜃 00 0 1

𝑥1𝑥21

CMU 15-462/662, Fall 2016

Translation in 2D-H coords

Translation expressed as 3x3 matrix multiplication:

In homogeneous coordinates, translation is a linear transformation!

𝑻 𝒙 = 𝒙 + 𝒃 =1 0 𝑏10 1 𝑏20 0 1

𝑥1𝑥21

=𝑥1 + 𝑏1𝑥2 +𝑏2

1

CMU 15-462/662, Fall 2016

Translation in 2D-H coords

What is this magic?

Translation in 2D homogeneous coordinates is equivalent to

shearing along x and y axes – a linear operation.

1 0 𝑏10 1 𝑏20 0 1

𝑥1𝑥2𝑥3

=

𝑥1 + 𝑏1𝑥2 +𝑏2𝑥3

But why is 𝑥3 set to 1? Could it not be 3.4182 instead?

CMU 15-462/662, Fall 2016

Homogeneous coordinates

x

y

(𝑥1, 𝑥2)

w

w = 1

(𝑥1, 𝑥2, 1)

𝑤𝑥1, 𝑤𝑥2, 𝑤 , ∀𝑤 > 0

- Homogenous coordinates are scale invariant

- 𝒙 and 𝑤𝒙 correspond to the same 2D point (divide by 𝑤 to convert 2D-H back to 2D)

- 2D-H points with 𝑤 = 0correspond to 2D vectors (technically, points at infinity)

- In homogenous coordinates, points and vectors are distinguishable from each other!

CMU 15-462/662, Fall 2016

Homogeneous coordinates:

points vs. vectors

x

y

(𝑥1, 𝑥2, 0)

w

w = 1

(𝑥1, 𝑥2, 1)

𝑤𝑥1, 𝑤𝑥2, 𝑤 , ∀𝑤 > 0

1 0 𝑏10 1 𝑏20 0 1

𝑥1𝑥21

vs

1 0 𝑏10 1 𝑏20 0 1

𝑥1𝑥20

CMU 15-462/662, Fall 2016

Visualizing 2D transformations in 2D-H

Original shape in 2D can be viewed as

many copies, uniformly scaled by w.2D rotation ⬌ rotate around w

2D scale ⬌ scale x and y; preserve w2D translate ⬌ shear in xy(Question: what happens to 2D shape

if you scale x, y, and w uniformly?)

CMU 15-462/662, Fall 2016

Summary so far…

▪ We know how to transform (scale, rotate,

reflect, shear, translate) 2D points and vectors

- All these transforms are linear maps

expressed as matrix-vector products when

using (slightly) higher-dimensional

homogenous coordinates

- How about other types of transforms (e.g.

rotate about an arbitrary point)?

- How about 3D transforms?

CMU 15-462/662, Fall 2016

Onto more complex transforms

▪ How would you transform this object such that

it gets twice as large?

- but remains where it is…

CMU 15-462/662, Fall 2016

Composition of basic transforms

Note 1: order of composition matters!

Note 2: common source of bugs!

Translate by (3,1), then scale by 0.5

Scale by 0.5, then translate by (3,1)

CMU 15-462/662, Fall 2016

How do we compose linear transforms?

Compose linear transforms via matrix multiplication.

Enables simple & efficient implementation: reduce complex

chain of transforms to a single matrix.

CMU 15-462/662, Fall 2016

How would you perform these transformations?

CMU 15-462/662, Fall 2016

Common pattern: rotation about point x

Step 1: translate by -x

Step 2: rotate Step 3: translate by x

Q: In homogenous coordinates, what does the corresponding

transformation matrix look like?

CMU 15-462/662, Fall 2016

Exercise

▪ Reflection about an arbitrary line