48
CSE 470: Computer Graphics

CSE 470: Computer Graphics

  • Upload
    veta

  • View
    37

  • Download
    2

Embed Size (px)

DESCRIPTION

CSE 470: Computer Graphics. Geometric Objects. A vector space contains vectors and scalars. A vector has direction and magnitude (but not position). Vectors are denoted by u, v, w (lower case). A scalar is a real number. Scalars are denoted by a, b, g - PowerPoint PPT Presentation

Citation preview

Page 1: CSE 470: Computer Graphics

CSE 470: Computer Graphics

Page 2: CSE 470: Computer Graphics

04/21/23 2

Geometric Objects1. A vector space contains vectors and scalars.

A vector has direction and magnitude (but not position).

Vectors are denoted by u, v, w (lower case).

A scalar is a real number.

Scalars are denoted by 2. An affine space is an extension of the vector space to

include points (positions in space).

Points are denoted by P, Q, R (upper case)

3. A Euclidean space extends the linear vector space to add a measure of size or distance.

Page 3: CSE 470: Computer Graphics

04/21/23 3

Combining entities in affine space1. Vector-scalar multiplication

|v| = || |v|, where |v| is the magnitude of vThe direction of v is the same as the direction of v

v 2v

2. Point-vector additionAdding a vector to a point results in another point.

Q

PQ + v = Pvv = P - Q

Page 4: CSE 470: Computer Graphics

04/21/23 4

Vector additionu = (x1, y1) (x1 is horizontal component of u, y1 is vertical component of u.)

v = (x2, y2)

u+v = (x1+x2, y1+y2)

uu

vv

u+v

Place the tail of v at the head of u. Draw a vector from the tail of u to the head of v.

Page 5: CSE 470: Computer Graphics

04/21/23 5

A parametric line

P() = P0 + v P0

P()

v

A line in space:

Affine sums:P() = Q + v defines a lineThere exists R such thatR = Q + v or v = R - QP = Q + (R-Q) = R + (1-)Q = 1R + 2Q where 1 + 2 = 1

If 0 <= <= 1, then all P lie on the line between Q and R.

Page 6: CSE 470: Computer Graphics

04/21/23 6

A parametric line

P() = P0 + v P0

P()

v

A line in space:

Affine sums:P() = Q + v defines a lineThere exists R such thatR = Q + v or v = R - QP = Q + (R-Q) = R + (1-)Q = 1R + 2Q where 1 + 2 = 1

If 0 <= <= 1, then all P lie on the line between Q and R.

Q

R = 0

= 1

Page 7: CSE 470: Computer Graphics

04/21/23 7

Affine sums for more pointsThe affine sum for three points:

P = 1P1 + 2P2 + 3P3, where 1 + 2 + 3 = 1, i >=0defines all points inside triangle P1P2P3.

P1P2

P3

P = 1P1 + 2P2 + ... + nPn, where 1 + 2 + ...+ n = 1, i >=0defines all points inside convex hull around P1P2...Pn.

The affine sum for n points:

A convex hull is like shrink-wraparound all n points.

Page 8: CSE 470: Computer Graphics

04/21/23 8

The dot product

The dot product (or inner product) of two vectors is defined as follows:

u v u v cos

where represents the angle between the two vectors.

u

v

Projection of u onto v:

w u cos u v

v w

Page 9: CSE 470: Computer Graphics

04/21/23 9

The cross productThe cross product of two linearly independent (non-parallel) vectors is a third vector that is orthogonal to both of them.

uv

n

n u v

n u v sin

The direction of n is defined by the right handed coordinate system.

Page 10: CSE 470: Computer Graphics

04/21/23 10

A parametric plane

A plane in affine space can be defined in terms of two linearly independent vectors as follows:

T(,) P0 u v

u

vP0

uv P

Page 11: CSE 470: Computer Graphics

04/21/23 11

Defining a coordinate system

Any 3D vector, w, can be defined in terms of 3 linearly independent vectors, v1, v2, v3:

w 1v1 2v2 3v3v1

v2

v3

w

1, 2 and 3 are components of w with respect to the basis (v1, v2, v3).

Page 12: CSE 470: Computer Graphics

04/21/23 12

Matrix representation of vectors

We can represent w as a column matrix:

a 1

2

3

w aT

v1

v2

v3

1 2 3

v1

v2

v3

1v1 2v2 3v3

Page 13: CSE 470: Computer Graphics

04/21/23 13

Adding a point of reference

Because vectors have no position, we must add a reference point to specify a coordinate frame.

Choose P0 as reference.

Vectors can be written as:

w 1v1 2v2 3v3

Points can be written as:

P P0 1v1 2v2 3v3v1

v2

v3

P

P0

Page 14: CSE 470: Computer Graphics

04/21/23 14

MatricesA Matrix is a rectangular array of quantities:

5 3

4 7

11.3 5.7 1.5

12.2 6.2 2.1

2x2 Matrix 2x3 Matrix

a11 a12 a1n

a21 a22 a2n

am1 am 2 amn

2

7

1

3x1 Matrix

M x N Matrix

Page 15: CSE 470: Computer Graphics

04/21/23 15

Matrix multiplicationTo multiply two matrices, multiply the rows in the first matrix by the columns of the second matrix.

a11 a12

a21 a22

b11 b12

b21 b22

a11b11 a12b21 a11b12 a12b22

a21b11 a22b21 a21b12 a22b22

Example:

Page 16: CSE 470: Computer Graphics

04/21/23 16

Matrix multiplicationIn general:

If A = a ij is a pq matrix,

B = bij is a q r matrix

and C = cij is a pr matrix then

AB = C if cij a ik bkjk1

q

Page 17: CSE 470: Computer Graphics

04/21/23 17

Matrix terminology

1. The transpose of a matrix, exchanges the rows and columns of that matrix.

A a b

c d

AT a c

b d

2. When a matrix, A, is multiplied by the identity matrix, the result is the same matrix A.

A a b

c d

I 1 0

0 1

3. The a matrix, A, is multiplied by its inverse, A-1, the result is the identity matrix.

AI = A

AA-1 = I

Page 18: CSE 470: Computer Graphics

04/21/23 18

Recall Coordinate systemsCoordinate systems are represented by a set of basis vectors (v1, v2, v3) and a reference point, P0.

w 1v1 2v2 3v3

Points can be written as:

P P0 1v1 2v2 3v3

v1

v2

v3

P

P0

Vectors can be written as:

Page 19: CSE 470: Computer Graphics

04/21/23 19

Changing the basis setWe can move from basis (v1, v2, v3) to basis (u1, u2, u3) using the following set of equations:

u1 11v1 12v2 13v3

u2 21v1 22v2 23v3

u3 31v1 32v2 33v3v1

v2

v3

u1u2

u3In matrix form:

u1

u2

u3

11 12 13

21 22 23

31 32 33

v1

v2

v3

u = Mv

Page 20: CSE 470: Computer Graphics

04/21/23 20

Representing a vector in the new basis set

w 1v1 2v2 3v3 aT

v1

v2

v3

Suppose:

We would like to find b, such that:

w 1u1 2u2 3u3 bT

u1

u2

u3

, b

1

2

3

Page 21: CSE 470: Computer Graphics

04/21/23 21

Solving for b

w bT

u1

u2

u3

bT M

v1

v2

v3

aT

v1

v2

v3

bTM = aT

(aT)T = a = (bTM)T = MTb

b = (MT)-1a

Page 22: CSE 470: Computer Graphics

04/21/23 22

Homogeneous coordinatesTransformation of basis vectors leaves the origin unchanged.Homogeneous coordinates allow us to transform the origin.

Recall representation of a point:

P P0 1v1 2v2 3v3

To represent P with matrices, we add a 4th dimension so we can include position:

P 1 2 3 1

v1

v2

v3

P0

P is represented by:

1

2

3

1

For vectors, the fourth coordinate is zero.

Page 23: CSE 470: Computer Graphics

04/21/23 23

Changing frames in homogeneous coordinates

Changing from (v1, v2, v3, P0) to (u1, u2, u3, Q0):

u1 11v1 12v2 13v3

u2 21v1 22v2 23v3

u3 31v1 32v2 33v3

Q0 41v1 42v2 43v3 P0

v1

v2

v3

u1

u2

u3

P0

Q0

In matrix form:

u1

u2

u3

Q0

11 12 13 0

21 22 23 0

31 32 33 0

41 42 43 1

v1

v2

v3

P0

Page 24: CSE 470: Computer Graphics

04/21/23 24

Transforming a point between coordinate systems

u1

u2

u3

Q0

M

v1

v2

v3

P0

, where M

11 12 13 0

21 22 23 0

31 32 33 0

41 42 43 1

Suppose P is represented by b in the u, Q0 space andby a in the v, P0 space. (bT = [1, 2, 3, 1], aT = [1, 2, 3, 1])

bT

u1

u2

u3

Q0

bT M

v1

v2

v3

P0

aT

v1

v2

v3

P0

a = MTb

b = (MT)-1a

Page 25: CSE 470: Computer Graphics

04/21/23 25

Affine transformationsAffine transformations are linear transformations from a point in one frame to another frame:

f(p + q) = f(p) + f(q)

, are scalars; p, q are vertices.

Affine transformations preserve lines.

We can represent the transformation as: v = Au where

A

11 12 13 14

21 22 23 24

31 32 33 34

0 0 0 1

M T

Page 26: CSE 470: Computer Graphics

04/21/23 26

Recall Affine transformationsAffine transformations are linear transformations from a point in one frame to another frame.

Affine transformations preserve lines.

We can represent the transformation as: v = Au where

A

11 12 13 14

21 22 23 24

31 32 33 34

0 0 0 1

M T

Page 27: CSE 470: Computer Graphics

04/21/23 27

TranslationTranslation: Displace points by a fixed distance in a given direction.

d

p

x

y

z

1

p'

x'

y'

z'

1

d

x

y

z

0

x' = x + x

y' = y + y

z' = z + z

p' = Tp where

T

1 0 0 x

0 1 0 y

0 0 1 z

0 0 0 1

Equations: In matrix form:

Page 28: CSE 470: Computer Graphics

04/21/23 28

Inverse translation

T

1 0 0 x

0 1 0 y

0 0 1 z

0 0 0 1

T is the translation matrix

Inverse translation: Displace by -d

T

1 0 0 x

0 1 0 y

0 0 1 z

0 0 0 1

Page 29: CSE 470: Computer Graphics

04/21/23 29

Rotation about the Z axis

Rotation about the Z axis

(x, y)

(x', y') x = cosy = sin

x' = cos(y' = sin(

x' = cos(cos(sin(sin(xcos(ysin(y' = sin(cos(cos(sin(xsin(ycos(

x

y

Page 30: CSE 470: Computer Graphics

04/21/23 30

The rotation matrix

x' =xcos(ysin(y' =xsin(ycos(z' = z

The equations:

In matrix form: P' = RZP where

RZ ()

cos sin 0 0

sin cos 0 0

0 0 1 0

0 0 0 1

Page 31: CSE 470: Computer Graphics

04/21/23 31

Rotation about the X axis

y' =ycos(zsin(z' =ysin(zcos(x' = x

The equations:

In matrix form: P' = RXP where

RX ( )

(y, z)

(y', z')

y

z

1 0 0 0

0 cos sin 0

0 sin cos 0

0 0 0 1

Page 32: CSE 470: Computer Graphics

04/21/23 32

Rotation about the Y axis

z' =zcos(xsin(x' =zsin(xcos(y' = y

The equations:

In matrix form: P' = RYP where

RY ( )

(z, x)

(z', x')

z

x

cos 0 sin 0

0 1 0 0

sin 0 cos 0

0 0 0 1

Page 33: CSE 470: Computer Graphics

04/21/23 33

Reversing Rotations

We use the facts that cos(-) = cos() sin(-) = -sin()

RZ ( )

cos( ) sin( ) 0 0

sin( ) cos( ) 0 0

0 0 1 0

0 0 0 1

cos( ) sin( ) 0 0

sin( ) cos( ) 0 0

0 0 1 0

0 0 0 1

Page 34: CSE 470: Computer Graphics

04/21/23 34

Scaling

Translation and Rotation are rigid body transformations.Scaling is not rigid.

Equations:x' = xxy' = yyz' = zz

P' = SP

S

x 0 0 0

0 y 0 0

0 0 z 0

0 0 0 1

Inverse scaling: Scale with S(1/x, 1/y, 1/z)

Page 35: CSE 470: Computer Graphics

04/21/23 35

Concatenation of TransformationsSuppose you want to:

1. Scale an object2. Rotate the object3. Translate the object

P1 = SP

P2 = RP1 = RSP

P3 = TP2 = TRSP

For each vertex, we apply a series of transformations:

For efficiency, we create a new matrix, M = TRS.Each new point can be computed directly as P3 = MP

Note: Matrix multiplication is not commutative. Order Matters!

Page 36: CSE 470: Computer Graphics

04/21/23 36

Example

P1

0

3/2

0

1

P2

1/2

0

0

1

P3

1/2

0

0

1

(0, 3 /2)

(1/2,0)

( 1/2,0)

P1

P2P3

Transformations:1. Double the height2. Rotate by 90 deg clockwise about the Z axis3. Translate horizontally by

3

Page 37: CSE 470: Computer Graphics

04/21/23 37

Trigonometry review

x

yr

r x 2 y2

sin y

r y

x 2 y2

cos x

r x

x 2 y2

tan sin cos

y

x

cot 1

tan x

y

tan 1 y

x

sin 1 y

x 2 y2

cos 1 x

x 2 y2

Page 38: CSE 470: Computer Graphics

04/21/23 38

Shear

x

y

x

y(x, y) (x', y')

Equations:x' = x+ycosy' = yz' = z

Matrix:

Hx ( )

1000

0100

0010

00cos1

Page 39: CSE 470: Computer Graphics

04/21/23 39

Rotation about a fixed pointSuppose you want to rotate an object about a fixed point, pf = (xf, yf, zf), about an axis parallel to the Z axis.

pf

x

y

z

1. Translate object so that pf is at the origin.2. Rotate object about the Z axis3. Translate object back to pf

Page 40: CSE 470: Computer Graphics

04/21/23 40

Calculating the Rotation matrix

p'T p f RZ T p f p

RZ T p f

cos sin 0 0

sin cos 0 0

0 0 1 0

0 0 0 1

1 0 0 x f

0 1 0 y f

0 0 1 z f

0 0 0 1

T p f RZ T p f

1 0 0 x f

0 1 0 y f

0 0 1 z f

0 0 0 1

cos sin 0 x f cos y f sinsin cos 0 x f sin y f cos

0 0 1 z f

0 0 0 1

Page 41: CSE 470: Computer Graphics

04/21/23 41

General RotationCan generate any rotation with a combination of RxRyRz = R.Calculating the angles can be tricky.

Rotation about an arbitrary axis through the origin:1. Use rotations to align the axis with the Z axis.2. Rotate about the Z axis3. Undo the rotations from (1).

p'Rx x Ry y Rz Ry y Rx x p

Page 42: CSE 470: Computer Graphics

04/21/23 42

Defining the Rotation Axis

1. Define rotation axis as a vector of length 1 (a unit vector) with x, y and z components: x, y, z

v x

y

z

x

2 y

2 z

2 1

x

y

z

v

where

Page 43: CSE 470: Computer Graphics

04/21/23 43

Calculating x

y

z

vd

z

x

y

1. Project vector v to the y-z plane. (Draw line from tip of v perpendicular to y-z plane).

2. x is the angle of this projection from the z axis.

sinx y

d

d y

2 z

2where

cosx z

d

Page 44: CSE 470: Computer Graphics

04/21/23 44

Rotating about X axis

Rx x

1 0 0 0

0 cos x sin x 0

0 sin x cos x 0

0 0 0 1

1 0 0 0

0z

d

y

d0

0y

d

z

d0

0 0 0 1

Page 45: CSE 470: Computer Graphics

04/21/23 45

Calculating y

When rotating about the X axis, the x component stays the same, but the y and z components change.

rx

z

x

y

1y

r 1 x

2 y

2 z

2 d

cosy = d

siny = x

Note: The rotation is clockwise, so the angle will be negative.

Page 46: CSE 470: Computer Graphics

04/21/23 46

The Rotation Matrix for Ry

Ry y cos y 0 sin y 0

0 1 0 0

sin y 0 cos y 0

0 0 0 1

d 0 x 0

0 1 0 0

x 0 d 0

0 0 0 1

Page 47: CSE 470: Computer Graphics

04/21/23 47

The full rotation

p'Rx x Ry y Rz Ry y Rx x p

Rotation about an arbitrary axis:

Rotation about a fixed point, pf, parallel to an arbitrary axis:

p'T p f Rx x Ry y Rz Ry y Rx x T p f p

Page 48: CSE 470: Computer Graphics

04/21/23 48

Most slides are selected from:

http://mathcs.holycross.edu/~croyden/csci343

Thank You