#2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD,...

40
#2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    223
  • download

    2

Transcript of #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD,...

Page 1: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

#2: Geometry & Homogeneous Coordinates

CSE167: Computer Graphics

Instructor: Ronen Barzel

UCSD, Winter 2006

Page 2: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

2

Outline for Today

More math…n Finish linear algebra: Matrix

composition1. Points, Vectors, and

Coordinate Frames2. Homogeneous Coordinates

Page 3: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

3

Matrix Multiplication

Each entry is dot product of row of M with column of N

M =mxx mxy mxz

myx myy myz

mzx mzy mzz

⎢⎢⎢

⎥⎥⎥      N =

nxx nxy nxz

nyx nyy nyz

nzx nzy nzz

⎢⎢⎢

⎥⎥⎥

L =M N

lxx lxy lxzlyx lyy lyzlzx lzy lzz

⎢⎢⎢

⎥⎥⎥=

mxx mxy mxz

myx myy myz

mzx mzy mzz

⎢⎢⎢

⎥⎥⎥⋅

nxx nxy nxz

nyx nyy nyz

nzx nzy nzz

⎢⎢⎢

⎥⎥⎥

lxy =mxxnxy + mxynyy + mxznzy

Page 4: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

4

Matrix Multiplication

M =mxx mxy mxz

myx myy myz

mzx mzy mzz

⎢⎢⎢

⎥⎥⎥                N =

nxx nxy nxz

nyx nyy nyz

nzx nzy nzz

⎢⎢⎢

⎥⎥⎥     

MN( )ij = miknkj∑

MN =

mxknkx∑ mxknky∑ mxknkz∑myknkx∑ myknky∑ myknkz∑mzknkx∑ mzknky∑ mzknkz∑

⎢⎢⎢

⎥⎥⎥

MN =

mxxnxx + mxynyx + mxznzx( ) mxxnxy + mxynyy + mxznzy( ) mxxnxz + mxynyz + mxznzz( )

myxnxx + myynyx + myznzx( ) myxnxy + myynyy + myznzy( ) myxnxz + myynyz + myznzz( )

mzxnxx + mzynyx + mzznzx( ) mzxnxy + mzynyy + mzznzy( ) mzxnxz + mzynyz + mzznzz( )

⎢⎢⎢⎢

⎥⎥⎥⎥

Page 5: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

5

Multiple Transformations If we have a vector v, and an x-axis rotation

matrix Rx, we can generate a rotated vector v′:

If we wanted to then rotate that vector around the

y-axis, we could multiply by another matrix:

′v = Rx θ( )  v

′′v = Ry φ( )   ′v

′′v = Ry φ( )   Rx θ( )  v( )

Page 6: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

6

We can extend this to the concept of applying any sequence of transformations:

Because matrix algebra obeys the associative law, we can regroup this as:

This allows us to compose them into a single matrix:

Multiple Transformations

′v =M4   M3   M2   M1  v( )( )( )

′v = M4  M3  M2  M1( ) v

Mtotal =M 4  M 3 M 2 M1

′v =M total  v

Page 7: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

7

Order matters! Matrix multiplication does NOT commute:

(unless one or the other is a uniform scale) Try this:

rotate 90 degrees about x then 90 degrees about z, versusrotate 90 degrees about z then 90 degrees about x.

Matrix composition works right-to-left. Compose:

Then apply it to a vector:

It first applies C to v, then applies B to the result, then applies A to the result of that.

M N ≠N M

′v =M v

′v = A B C( ) v

′v = A  B  C v( )( )

M =A B C

Page 8: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

8

Quick Matrix algebra summary

linearity:

M (sb) =s M b( )

M a+b( ) = M a( ) + M b( )

M−1 is inverse of M :  (when it exists)

M−1 M a( ) =a

M−1M =I

MM−1 =I

MPQ( )−1 =Q−1P−1M−1

for a rotation: R−1 =RT

determinant M :    

M >1             scales up

M =1             pure rotation

0< M <1    scales down

M =0             "flattens": singular matrix, no inverse

M < 0            reflects

⎪⎪⎪

⎪⎪⎪

M−1 =1M

Page 9: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

9

What good is this?

Composition of transformations, by matrix multiplication, is a basic technique Used all the time You’ll probably use it for Project 1 All linear operations on vectors can be expressed as composition of rotation and scale (even “shear”)

But there’s a limit to what we can do only having linear operations on vectors….

Page 10: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

10

Outline for Today

1. Finish linear algebra: Matrix composition

2. Points, Vectors and Coordinate Frames

3. Homogeneous Coordinates

Page 11: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

11

Geometric objects

Interesting Objects Points Vectors Transformations Coordinate Frames Also: Lines, Rays, Planes, Normals, …

Page 12: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

12

Points and vectors

You know linear algebra, vector spaces

Why am I talking about this? Emphasize differences:

between a point and a vector between a point or vector and the representation of a point or vector

Page 13: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

13

Points and vectors

in R3, can represent point as 3 numbers

in R3, can represent vector as 3 numbers

Easy to think they’re the same thing… …but they’re not! different operations, different behaviors many program libraries make this mistake easy to have bugs in programs

Page 14: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

14

In 1D, consider time

point in time: a class meets at 2PM duration of time: a class lasts 2 hours operations:

class at 2PM + class at 3PM ≠ class at 5PM !! 2 hour class + 3 hour class = 5 hours of classes

class ends at 5PM – starts at 2PM = 3 hour class

class starts at 2PM + lasts 3 hours = ends at 5PM

2 classes at 3PM ≠ one class at 6PM !! 2 classes last 3 hours = 6 hours of classes Class from 2PM to 10PM, half done at

1

22PM( )+

12

6PM( ) = 4PM

("affine combination")1 24 4 44 34 4 4 4

Page 15: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

15

“Coordinate Systems” for time

Knowing just the hour number doesn’t tell you everything…

AM vs. PM (or use 8h00 vs 20h00) Time zones: same point, many representations

10 (Paris) == 9 (London) to remove ambiguity, often use GMT

If always staying in local time zone, not important if scheduling globally, must be careful. convert from one time zone to another.

(Also, hours only good within one day need to specify date & time UNIX time: seconds since 01/01/1970, 00h00 GMT)

Notice: time durations are unaffected by all this!

Page 16: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

16

Geometry, analogously

Point describes a location in space can subtract points to get a vector can take weighted average of points to get a point

Vector describes a displacement in space has a magnitude and direction can add, subtract, scale vectors can add vector to a point to get a point

To represent them as three numbers, you must specify which coordinate system

Page 17: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

17

Vector and point arithmetic

C++ classes can support these

operations

rw =

ru+

rv           vector+vector →  vector

rv=

rw−

ru           vector-vector →  vector

ru=s 

rv               scale a vector

q=p+rv           point+vector→ point

rv=q−p           point-point →  vector

r =14

p+34

q     weighted average of points → point

Page 18: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

18

Coordinate Frames Origin point, and 3 orthonormal vectors for x,y,z

axes (right-handed)

In CG, often work with many different frames simultaneously

f

g

h

a

bc

x

y

z

O

P

Q

o,rx,

ry,

rz

p,

ra,

rb,

rc

q,

rf,

rg,

rh

Page 19: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

19

Coordinates If you have coordinate triples such as:

Then, with frame such as you can construct a point or vector:

Same coordinates, different frame different point or vector Coordinates have no real meaning without a frame CG programs often have lots of frames--you have to keep track!

• (It’s possible to write C++ classes that keep track of frames. But it’s hard for them to be time- and memory-efficient, so it’s rarely done in practice.)

Typically have “World Coordinates” as implicit base frame

Notice: vectors don’t depend on the origin of the frame, only the axes

3

6

1

⎢⎢⎢

⎥⎥⎥    or     

9−25

⎢⎢⎢

⎥⎥⎥

p =o+ 3rx+ 6

ry+

rz

rv=      9

rx−2

ry+ 5

rz

o,rx,

ry,

rz

Page 20: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

20

Coordinates of a Frame

Suppose you have a frame In world coordinates, might have

But in itself always have coordinates:

p,

ra,

rb,

rc

p =1033

⎢⎢⎢

⎥⎥⎥    

ra=

0−11

⎢⎢⎢

⎥⎥⎥    

rb=

0.7070

0.707

⎢⎢⎢

⎥⎥⎥    

rc=

−0.7070

0.707

⎢⎢⎢

⎥⎥⎥

p,

ra,

rb,

rc

p =000

⎢⎢⎢

⎥⎥⎥    

ra=

100

⎢⎢⎢

⎥⎥⎥    

rb=

010

⎢⎢⎢

⎥⎥⎥    

rc=

001

⎢⎢⎢

⎥⎥⎥

Page 21: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

21

Coordinate equivalences

Given a frame: Can have a point with some coords:

Can have a vector with same coords:

Formally, we have:

Informally, p and v look and act about the same• People often sloppy, don’t distinguish point from vector

• Can only get away with it if you stay in same frame!• And even then need to be careful…

rv =

abc

⎢⎢⎢

⎥⎥⎥

o,rx,

ry,

rz

p =abc

⎢⎢⎢

⎥⎥⎥

p =o+rv

rv=p−o

O rx

ry

rz

rv

p

b

ca

Page 22: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

22

Outline for Today

1. Finish linear algebra: Matrix composition

2. Points, Vectors and Coordinate Frames

3. Homogeneous Coordinates But first: transforming points

Page 23: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

23

Linear TransformationsMatrix-vector multiplicatio

n

Matrix-point multiplicatio

n

M

Rotation matrix Rotates vector direction

Moves point by rotating

about origin of frame

Scale matrix Scales vector magnitude

Moves point towards or away from origin of

frame

Mrv =

mxx mxy mxz

myx myy myz

mzx mzy mzz

⎢⎢⎢

⎥⎥⎥

vx

vy

vz

⎢⎢⎢

⎥⎥⎥

Mp =mxx mxy mxz

myx myy myz

mzx mzy mzz

⎢⎢⎢

⎥⎥⎥

px

py

pz

⎢⎢⎢

⎥⎥⎥

Page 24: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

24

Rotating an object Object defined as collection of points Apply rotation matrix to every point:

Rotates object about origin of frame

Also rotates all vectors in object:

rv =p2 −p1r′v = ′p2 − ′p1 = Rp2( )− Rp1( ) =R(p2 −p1) =R

rv

p1K pN

′pi = R pi , for all i

Page 25: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

25

Scaling an object

Apply scale matrix to every point:

Scale object about origin of frame

Also scales all vectors in object

′pi = S pi , for all i

rv =p2 −p1r′v = ′p2 − ′p1 = Sp2( )− Sp1( ) =S(p2 −p1) =S

rv

Page 26: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

26

Moving an object

Add displacement vector to each point:

Translates the object:

Vectors don’t change:

d

′pi = pi +rd,    for all i

rv =p2 −p1r′v = ′p2 − ′p1 = p2 +

rd( )− p1 +

rd( ) =(p2 −p1) + (

rd−

rd) =

rv+ 0 =

rv

Page 27: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

27

General Object Transformation

Some matrix M and displacement d:

Math note: the transformation for p isn’t linear• It’s an affine transformation• Points and their transforms form an affine space• Not a vector space

′p =M p +rd

r′v =M 

rv

Page 28: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

28

But it’s very inconvenient

Different rule for points vs. vectors

Hard to compose transformations:

Hard to invert, etc.

…so introduce Homogeneous Coordinates

′p =Mp +rd

′′p = N ′p +re = NM( )p + N

rd +

re( )

′′′p =Q ′′p +rf = QNM( )p + QN( )

rd +Q

re +

rf( )

Page 29: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

29

Homogeneous coordinates

Basic: a trick to unify/simplify computations.

Deeper: projective geometry Interesting mathematical properties Good to know, but less immediately practical

We will use some aspect of this when we do perspective projection (in a few weeks)

Page 30: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

30

Homogeneous coordinates Add an extra component. 1 for a point, 0 for a

vector:

combine M and d into single 4x4 matrix:

And see what happens when we multiply…

p =

px

py

pz

1

⎢⎢⎢⎢

⎥⎥⎥⎥

        rv=

vx

vy

vz

0

⎢⎢⎢⎢

⎥⎥⎥⎥

mxx mxy mxz dxmyx myy myz dymzx mzy mzz dz0 0 0 1

⎢⎢⎢⎢

⎥⎥⎥⎥

Page 31: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

31

Homogeneous point transform

Transform a point:

Top three rows are the affine transform! Bottom row stays 1

′px′py′pz

1

⎢⎢⎢⎢

⎥⎥⎥⎥

 =

mxx mxy mxz dxmyx myy myz dymzx mzy mzz dz0 0 0 1

⎢⎢⎢⎢

⎥⎥⎥⎥

pxpypz1

⎢⎢⎢⎢

⎥⎥⎥⎥

=

mxx px + mxy py + mxz pz + dxmyx px + myy py + myz pz + dymzx px + mzy py + mzz pz + dz

0 + 0 + 0 +1

⎢⎢⎢⎢

⎥⎥⎥⎥

                                                         M

pxpypz

⎢⎢⎢

⎥⎥⎥    +    

rd

Page 32: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

32

Homogeneous vector transform

Transform a vector:

Top three rows are the linear transform• Displacement d is properly ignored

Bottom row stays 0

′vx′vy′vz

0

⎢⎢⎢⎢

⎥⎥⎥⎥

 =

mxx mxy mxz dxmyx myy myz dymzx mzy mzz dz0 0 0 1

⎢⎢⎢⎢

⎥⎥⎥⎥

vxvyvz0

⎢⎢⎢⎢

⎥⎥⎥⎥

=

mxxvx + mxyvy + mxzvz + 0

myxvx + myyvy + myzvz + 0

mzxvx + mzyvy + mzzvz + 0

0 + 0 + 0 + 0

⎢⎢⎢⎢

⎥⎥⎥⎥

                                                           M

vxvyvz

⎢⎢⎢

⎥⎥⎥ 

Page 33: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

33

Homogeneous arithmetic

Legal operations always end in 0 or 1!

vector+vector:       M

0

⎣⎢

⎦⎥+

M

0⎡

⎣⎢

⎦⎥⇒

M

0⎡

⎣⎢

⎦⎥

vector-vector:       M

0⎡

⎣⎢

⎦⎥−

M

0⎡

⎣⎢

⎦⎥⇒

M

0⎡

⎣⎢

⎦⎥

scalar*vector:               sM

0⎡

⎣⎢

⎦⎥⇒

M

0⎡

⎣⎢

⎦⎥

point+vector:         M

1⎡

⎣⎢

⎦⎥+

M

0⎡

⎣⎢

⎦⎥⇒

M

1⎡

⎣⎢

⎦⎥

point-point:         M

1⎡

⎣⎢

⎦⎥−

M

1⎡

⎣⎢

⎦⎥⇒

M

0⎡

⎣⎢

⎦⎥

point+point:        M

1⎡

⎣⎢

⎦⎥+

M

1⎡

⎣⎢

⎦⎥⇒

M

2⎡

⎣⎢

⎦⎥

scalar*point:                sM

1⎡

⎣⎢

⎦⎥⇒

M

s⎡

⎣⎢

⎦⎥

weighted average

affine combination

⎧⎨⎩

⎫⎬⎭ of points:   

13

M

1⎡

⎣⎢

⎦⎥+

23

M

1⎡

⎣⎢

⎦⎥⇒

M

1⎡

⎣⎢

⎦⎥

Page 34: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

34

Homogeneous Transforms Rotation, Scale, and Translation of points and vectors unified in a single matrix transformation:

Matrix has the form: Last row always 0,0,0,1

Transforms compose by matrix multiplication! Same caveat: order of operations is important Same note: Transforms operate right-to-left

mxx mxy mxz dxmyx myy myz dymzx mzy mzz dz0 0 0 1

⎢⎢⎢⎢

⎥⎥⎥⎥

′p =M p

Page 35: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

35

Primitive Transforms

I=

1 0 0 00 1 0 00 0 1 00 0 0 1

⎢⎢⎢⎢

⎥⎥⎥⎥

S(sx,sy,sz) =

sx 0 0 00 sy 0 00 0 sz 00 0 0 1

⎢⎢⎢⎢

⎥⎥⎥⎥

Rx(θ) =  

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

⎢⎢⎢⎢

⎥⎥⎥⎥

R y(θ) =

cos(θ) 0 sin(θ) 00 1 0 0

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

⎢⎢⎢⎢

⎥⎥⎥⎥

  R z(θ) =

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

⎢⎢⎢⎢

⎥⎥⎥⎥

T(d) =

1 0 0 dx

0 1 0 dy

0 0 1 dz

0 0 0 1

⎢⎢⎢⎢

⎥⎥⎥⎥

Page 36: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

36

Programming in practice

Everyone uses homogeneous matrices. built into low-level software, hardware

In practice, almost never explicitly use 4th component of vector or point. Waste of memory & computation Instead, keep track of points vs. vectors explicitly• E.g. by C++ classes

Separate matrix methods “transform point” and “transform vector” that implicitly use the 1 or 0.

Page 37: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

37

More Coordinate Equivalences

Translate an object by d Translate the coordinate frame by -d Either way, get the same coordinates out

Rotate object about the frame’s origin Rotate the frame oppositely out its own origin Either way, get the same coordinates out

Duality: Matrix transforms objects vs. changes coordinates Either can be handy (we’ll talk about next class) Can be confusing, make sure you know which you want…

Page 38: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

38

Transformation as coordinate frame

Build matrix from vectors a, b, c, point d

Notice effect on coordinate frame:

Any transform M describes change of frame:

If a,b,c are right-handed orthonormal, transformation is rigid Pure rotation, pure translation, or mix of rotation and

translation No scale

M =

ax bx cx dx

ay by cy dy

az bz cz dz

0 0 0 1

⎢⎢⎢⎢

⎥⎥⎥⎥

ra =M

1000

⎢⎢⎢⎢

⎥⎥⎥⎥

=Mrx      

rb=M

0100

⎢⎢⎢⎢

⎥⎥⎥⎥

 =Mry      

rc=M

0010

⎢⎢⎢⎢

⎥⎥⎥⎥

=Mrz      d=M

0001

⎢⎢⎢⎢

⎥⎥⎥⎥

=M o

d,

ra,

rb,

rc =M o,

rx,

ry,

rz

Page 39: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

39

Useful tidbit: Rotate about a “pivot”

Q: How to rotate about arbitrary pivot? Rotation matrix always rotates about origin!

A: Sequence of operations:

This is a handy “primitive” to implement Food for thought: what’s the structure of the

resulting matrix?

Given desired pivot point p, same coords give vector from origin, rv =p−o

Step 1. Translate object to move pivot point to origin: T(−rv)

Step 2. Rotate object by your rotation matrix: RStep 3. Translate to move origin back to original spot: T(

rv)

Compose these:  Rp =T(rv)RT(−

rv)

Page 40: #2: Geometry & Homogeneous Coordinates CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006.

40

Next class:

Hierarchical Transformations Geometric Calculations