Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

24
Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006

Transcript of Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

Page 1: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

Background Mathematics

Aaron Bloomfield

CS 445: Introduction to Graphics

Fall 2006

Page 2: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

2

Mathematical Foundations

A very brief review of some mathematical tools we’ll employ Geometry (2D, 3D) Trigonometry Vector and affine spaces

Points, vectors, and coordinates Dot and cross products Linear transforms and matrices

Page 3: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

3

3D Geometry

To model, animate, and render 3D scenes, we must specify: Location Displacement from arbitrary locations Orientation

We’ll look at two types of spaces: Vector spaces Affine spaces

We will often be sloppy about the distinction

Page 4: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

4

Vector Spaces

Given a basis for a vector space: Each vector in the space is a unique linear combination

of the basis vectors The coordinates of a vector are the scalars from this

linear combination Best-known example: Cartesian coordinates Note that a given vector will have different coordinates

for different bases

Page 5: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

5

Vectors And Points

We commonly use vectors to represent: Direction (i.e., orientation) Points in space (i.e., location) Displacements from point to point

But we want points and directions to behave differently Ex: To translate something means to move it without

changing its orientation Translation of a point = different point Translation of a direction = same direction

Page 6: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

6

Affine spaces vs. Vector spaces

Vector spaces All vectors originate at the origin Thus, can be denoted with (x,y,z) coordinates

The head of the vector The origin is absolute

Affine spaces Vectors can start at an arbitrary point Thus, can be denoted via (x,y,z) and starting point P The origin is relative to the current “context”

Only when the distinction matters will we specify

Page 7: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

7

Affine Spaces

To be more rigorous, we need an explicit notion of position

Affine spaces add a third element to vector spaces: points (P, Q, R, …)

Points support these operations Point-point subtraction: Q - P = v

Result is a vector pointing from P to Q Vector-point addition: P + v = Q

Result is a new point P + 0 = P

Note that the addition of two points is not defined

P

Q

v

Page 8: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

8

Affine Spaces

Points, like vectors, can be expressed in coordinates The definition uses an affine combination Net effect is same: expressing a point in terms of a

basis Thus the common practice of representing points

as vectors with coordinates Be careful to avoid nonsensical operations

Point + point Scalar * point

Page 9: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

9

Affine Lines: An Aside

Parametric representation of a line with a direction vector d and a point P1 on the line:

P() = Porigin + d

Restricting 0 produces a ray Setting d to P - Q and restricting 0 1

produces a line segment between P and Q

Page 10: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

10

Dot Product

The dot product or, more generally, inner product of two vectors is a scalar:

v1 • v2 = x1x2 + y1y2 + z1z2 (in 3D) Useful for many purposes

Computing the length of a vector length(v) = |v| = sqrt(v • v)

Normalizing a vector, making it unit-length Divide each coordinate by the length

Computing the angle between two vectors: u • v = |u| |v| cos(θ)

Checking two vectors for orthogonality Does the angle equal 90º?

Projecting one vector onto anotherθ

u

v

Page 11: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

11

Cross Product

The cross product or vector product of two vectors is a vector:

Cross product of two vectors is orthogonal to both

Right-hand rule dictates direction of cross product

Cross product is handy for finding surface orientation

Lighting Visibility

1221

1221

1221

21 )(

y x y x

z x z x

z y z y

vv

Page 12: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

12

Linear Transformations

A linear transformation: Maps one vector to another Preserves linear combinations

Thus behavior of linear transformation is completely determined by what it does to a basis

Turns out any and all linear transformations can be represented by a matrix

Page 13: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

13

Matrices

By convention, matrix element Mrc is located at row r and column c:

This is called row-major order

By (OpenGL) convention, vectors are columns: And 2-D matrices are in

column-major order!

mnm2m1

2n2221

1n1211

MMM

MMM

MMM

M

3

2

v

v

v

v

1

Page 14: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

14

Matrices

Matrix-vector multiplication applies a linear transformation to a vector:

Recall how to do matrix multiplication

z

y

x

v

v

v

MMM

MMM

MMM

vM

333231

232221

131211

Page 15: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

15

Matrix Transformations

A sequence or composition of linear trans-formations corresponds to the product of the corresponding matrices Note: the matrices to the right affect vector first Note: order of matrices matters!

The identity matrix I has no effect in multiplication Some (not all) matrices have an inverse:

vvMM 1

Page 16: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

16

3D Scene Representation

Scene is usually approximated by 3D primitives Point Line segment Polygon Polyhedron Curved surface Solid object etc.

Page 17: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

17

3D Point

Specifies a location Represented by three coordinates Infinitely small

typedef struct {Coordinate x;Coordinate y;Coordinate z;

} Point;

typedef struct {Coordinate x;Coordinate y;Coordinate z;

} Point;

(x,y,z)

Origin

Page 18: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

18

3D Vector

Specifies a direction and a magnitude Represented by three coordinates Magnitude ||V|| = sqrt(dx dx + dy dy + dz dz)

Which is the square root of the dot product Has no location (in affine spaces!)

Dot product of two 3D vectors V1·V2 = dx1dx2 + dy1dy2 + dz1dz2

V1·V2 = ||V1 || || V2 || cos()

typedef struct {Coordinate dx;Coordinate dy;Coordinate dz;

} Vector;

typedef struct {Coordinate dx;Coordinate dy;Coordinate dz;

} Vector;

(dx,dy,dz)

(dx2,dy2 ,dz2)

Page 19: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

19

3D Line Segment

Use a linear combination of two points Parametric representation:

P = P1 + t (P2 - P1), (0 t 1)

typedef struct {Point P1;Point P2;

} Segment;

typedef struct {Point P1;Point P2;

} Segment;P1

P2

Origin

Page 20: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

20

3D Ray

Line segment with one endpoint at infinity Parametric representation:

P = P1 + t V, (0 <= t < )

typedef struct {Point P1;Vector V;

} Ray;

typedef struct {Point P1;Vector V;

} Ray;

P1

V

Origin

Page 21: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

21

3D Line

Line segment with both endpoints at infinity Parametric representation:

P = P1 + t V, (- < t < )

P1

typedef struct {Point P1;Vector V;

} Line;

typedef struct {Point P1;Vector V;

} Line; V

Origin

Page 22: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

22Origin

3D Plane

A combination of three non-linear points Implicit representation:

N + d = 0, or ax + by + cz + d = 0

N is the plane “normal” Unit-length vector Perpendicular to plane

typedef struct {Vector N;Distance d;

} Plane;

typedef struct {Vector N;Distance d;

} Plane;P1

P3P2

N = (a,b,c)

d

Page 23: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

23

3D Polygon

Area “inside” a sequence of coplanar points Triangle Quadrilateral Convex Star-shaped Concave Self-intersecting

Points are in counter-clockwise order Holes (use > 1 polygon struct)

typedef struct {Point

*points;int npoints;

} Polygon;

typedef struct {Point

*points;int npoints;

} Polygon;

Page 24: Background Mathematics Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006.

24

3D Sphere

All points at distance “r” from point “(cx, cy, cz)” Implicit representation:

(x - cx)2 + (y - cy)2 + (z - cz)2 = r 2

Parametric representation: x = r cos() cos() + cx

y = r cos() sin() + cy

z = r sin() + cz

typedef struct {Point center;Distance radius;

} Sphere;

typedef struct {Point center;Distance radius;

} Sphere;

r

Origin