GEOMETRY - - Texas A&M University Corpus Christi Slides modified from Angel book 6e and from Anthony...
Transcript of GEOMETRY - - Texas A&M University Corpus Christi Slides modified from Angel book 6e and from Anthony...
GEOMETRY Slides modified from Angel book 6e and from
Anthony Steed and Pam Perlich and cs4160
(Columbia U)
2 COSC4328/5327 Computer Graphics
Objectives
• Introduce the elements of geometry
• Scalars
• Vectors
• Points
• Develop mathematical operations among them in a
coordinate-free manner
• Define basic primitives
• Line segments
• Polygons
3 COSC4328/5327 Computer Graphics
Basic Elements
• Geometry is the study of the relationships among
objects in an n-dimensional space • In computer graphics, we are interested in objects that exist in
three dimensions
• Want a minimum set of primitives from which we
can build more sophisticated objects
• We will need three basic elements • Scalars
• Vectors
• Points
4 COSC4328/5327 Computer Graphics
Coordinate-Free Geometry
• When we learned simple geometry, most of us started with a Cartesian approach
• Points were at locations in space p=(x,y,z)
• We derived results by algebraic manipulations involving these coordinates
• This approach was nonphysical
• Physically, points exist regardless of the location of an arbitrary coordinate system
• Most geometric results are independent of the coordinate system
• Example Euclidean geometry: two triangles are identical if two corresponding sides and the angle between them are identical
5 COSC4328/5327 Computer Graphics
Scalars • Need three basic elements in geometry
• Scalars, Vectors, Points
• Scalars can be defined as members of sets which can be combined by two operations (addition and multiplication) obeying some fundamental axioms (associativity, commutivity, inverses)
• Examples include the real and complex number systems under the ordinary rules with which we are familiar
• Scalars alone have no geometric properties
6 COSC4328/5327 Computer Graphics
Vectors
• Physical definition: a vector is a quantity with two attributes • Direction
• Magnitude
• Examples include • Force
• Velocity
• Directed line segments
• Most important example for graphics
• Can map to other types
v
7 COSC4328/5327 Computer Graphics
Vector Operations
• Every vector has an inverse • Same magnitude but points in opposite direction
• Every vector can be multiplied by a scalar
• There is a zero vector • Zero magnitude, undefined orientation
• The sum of any two vectors is a vector • Use head-to-tail axiom
v -v v v
u
w
Vectors
Length and direction. Absolute position not important
Use to store offsets, displacements, locations But strictly speaking, positions are not vectors and cannot be added: a
location implicitly involves an origin, while an offset does not.
=
Usually written as or in bold. Magnitude written as a a
9
Vectors, V (x, y, z)
v
w
v + w
Vector addition
sum v + w
v 2v
(-1)v (1/2)V
Scalar multiplication of
vectors (they remain parallel)
v
w
Vector difference
v - w = v + (-w)
v
-w
v - w
x
y
Vector OP
P
O
10
Vectors V
n Length (modulus) of a vector V (x, y,
z)
|V| =
n A unit vector: a vector can be
normalised such that it retains its
direction, but is scaled to have unit
length:
||V of modulus
Vvector ^
V
VV
11
Dot Product
u · v = xu ·xv + yu ·yv + zu ·zv
u · v = |u| |v| cos
cos = u · v/ |u| |v|
n This is purely a scalar number not a vector.
n What happens when the vectors are unit
n What does it mean if dot product == 0 or == 1?
Dot (scalar) product
a
b
?a b b a
1
cos
cos
a b a b
a b
a b
( )
( ) ( ) ( )
a b c a b a c
ka b a kb k a b
Dot product: some applications in CG
Find angle between two vectors (e.g. cosine of angle between
light source and surface for shading)
Finding projection of one vector on another (e.g. coordinates of
point in arbitrary coordinate system)
Advantage: can be computed easily in cartesian components
Projections (of b on a)
a
b
?
?
b a
b a
cosa b
b a ba
2
a a bb a b a a
a a
Dot product in Cartesian components
?a b
a b
x xa b
y y
a b
a b a b
a b
x xa b x x y y
y y
16
Cross Product
n The result is not a scalar but a vector which is
normal to the plane of the other 2
n Can be computed using the determinant of:
u x v = i(yvzu -zvyu), -j(xvzu - zvxu), k(xvyu - yvxu)
n Size is u x v = |u||v|sin
n Cross product of vector with it self is null
uuu
vvv
zyx
zyx
kji
uuu
vvv
zyx
zyx
kji
uuu
vvv
zyx
zyx
kji
uuu
vvv
zyx
zyx
kji
Cross (vector) product
Cross product orthogonal to two initial vectors
Direction determined by right-hand rule
Useful in constructing coordinate systems (later)
a
b
a b b a
sina b a b
Cross product: Properties
0
( )
( ) ( )
a b b a
a a
a b c a b a c
a kb k a b
x y z
y x z
y z x
z y x
z x y
x z y
Cross product: Cartesian formula?
a b b a
a a a a b a b
b b b a b a b
x y z y z y z
a b x y z z x x z
x y z x y y x
*
0
0
0
a a b
a a b
a a b
z y x
a b A b z x y
y x z
Dual matrix of vector a
20 COSC4328/5327 Computer Graphics
Linear Vector Spaces
• Mathematical system for manipulating vectors
• Operations
• Scalar-vector multiplication u=v
• Vector-vector addition: w=u+v
• Expressions such as
v=u+2w-3r
Make sense in a vector space
21 COSC4328/5327 Computer Graphics
Vectors Lack Position
• These vectors are identical • Same length and magnitude
• Vectors spaces insufficient for geometry • Need points
22 COSC4328/5327 Computer Graphics
Points
• Location in space
• Operations allowed between points and vectors
• Point-point subtraction yields a vector
• Equivalent to point-vector addition
P=v+Q
v=P-Q
23
Vectors, V (x, y, z)
n Represent a direction (and magnitude) in 3D space
n Points != Vectors Vector +Vector = Vector
Point – Point = Vector
Point + Vector = Point
Point + Point = ?
24 COSC4328/5327 Computer Graphics
Affine Spaces
• Point + a vector space
• Operations
• Vector-vector addition
• Scalar-vector multiplication
• Point-vector addition
• Scalar-scalar operations
• For any point define • 1 • P = P
• 0 • P = 0 (zero vector)
25 COSC4328/5327 Computer Graphics
Lines
• Consider all points of the form
• P()=P0 + d
• Set of all points that pass through P0 in the direction of the vector d
26 COSC4328/5327 Computer Graphics
Parametric Form
• This form is known as the parametric form of the line • More robust and general than other forms
• Extends to curves and surfaces
• Two-dimensional forms • Explicit: y = mx +h
• Implicit: ax + by +c =0
• Parametric:
x() = x0 + (1-)x1
y() = y0 + (1-)y1
27 COSC4328/5327 Computer Graphics
Rays and Line Segments
• If >= 0, then P() is the ray leaving P0 in the direction d
If we use two points to define v, then
P( ) = Q + (R-Q)=Q+v
=R + (1-)Q
For 0<=<=1 we get all the
points on the line segment
joining R and Q
28
Parametric equation of a line
(ray)
Given two points P0 = (x0, y0, z0) and
P1 = (x1, y1, z1) the line passing through
them can be expressed as:
P(t) = P0 + t(P1 - P0)
x(t) = x0 + t(x1 - x0)
y(t) = y0 + t(y1 - y0)
z(t) = z0 + t(z1 - z0)
=
With - < t <
29 COSC4328/5327 Computer Graphics
Convexity
• An object is convex iff for any two points in the object all
points on the line segment between these points are also
in the object
P
Q Q
P
convex not convex
30 COSC4328/5327 Computer Graphics
Affine Sums
• Consider the “sum”
P=1P1+2P2+…..+nPn
Can show by induction that this sum makes sense iff
1+2+…..n=1
in which case we have the affine sum of the points P1,P2,…..Pn
• If, in addition, i>=0, we have the convex hull of P1,P2,…..Pn
31 COSC4328/5327 Computer Graphics
Convex Hull
• Smallest convex object containing P1,P2,…..Pn
• Formed by “shrink wrapping” points
32 COSC4328/5327 Computer Graphics
Curves and Surfaces
• Curves are one parameter entities of the form P() where
the function is nonlinear
• Surfaces are formed from two-parameter functions P(, b)
• Linear functions give planes and polygons
P() P(, b)
33 COSC4328/5327 Computer Graphics
Planes
• A plane can be defined by a point and two vectors or by
three points
P(,b)=R+u+bv P(,b)=R+(Q-R)+b(P-Q)
u
v
R
P
R
Q
34 COSC4328/5327 Computer Graphics
Triangles
convex sum of P and Q
convex sum of S() and R
for 0<=,b<=1, we get all points in triangle
Barycentric Coordinates
Triangle is convex so any point inside can be represented as
an affine sum
P(1, 2, 3)=1P+2Q+3R
where
1 +2 +3 = 1
i>=0
The representation is called the barycentric coordinate
representation of P
35 COSC4328/5327 Computer Graphics 36 COSC4328/5327 Computer Graphics
Normals
• Every plane has a vector n normal
(perpendicular, orthogonal) to it
• From point-two vector form P(,b)=R+u+bv, we
know we can use the cross product to find n =
u v and the equivalent form
(P()-P) n=0
u
v
P
REPRESENTATION
COSC4328/5327 Computer Graphics 37 38 COSC4328/5327 Computer Graphics
Objectives
• Introduce concepts such as dimension and basis
• Introduce coordinate systems for representing vectors
spaces and frames for representing affine spaces
• Discuss change of frames and bases
• Introduce homogeneous coordinates
39
Vectors and Matrices
n Matrix is an array of numbers with
dimensions M (rows) by N (columns)
• 3 by 6 matrix
• element 2,3
is (3)
n Vector can be considered a 1 x M matrix
•
zyxv
100025114311212003
40
Types of Matrix
n Identity matrices - I
n Diagonal
1001
1000010000100001
n Symmetric
• Diagonal matrices
are (of course)
symmetric
• Identity matrices are
(of course) diagonal
4000010000200001
fecedbcba
41
Operation on Matrices
n Addition
• Done elementwise
n Transpose • “Flip” (M by N becomes N by M)
sdrc
qbpa
sr
qp
dc
ba
389
724
651
376
825
941T
42
Operations on Matrices
n Multiplication
• Only possible to multiply of dimensions
– x1 by y1 and x2 by y2 iff y1 = x2
• resulting matrix is x1 by y2
– e.g. Matrix A is 2 by 3 and Matrix by 3 by 4
• resulting matrix is 2 by 4
– Just because A x B is possible doesn’t mean
B x A is possible!
43
Matrix Multiplication Order
n A is n by k , B is k by m
n C = A x B defined by
n BxA not necessarily
equal to AxB
k
l
ljilij bac1
*
*
*
*
*
*
*****
*.
*.
*.
*.
*.
*.
*****
*.
..
*.
*.
*.
*.
*.
*****.....
44
Example Multiplications
____
____
1011
12
101132
______
______
______
010001100
111013322
Computation: A x B = C
A a11 a12
a21 a22
B b11 b12 b13
b21 b22 b23
232213212222122121221121
2312131122121211 21121111
babababababa
babababababaC
[2 x 2]
[2 x 3]
[2 x 3]
Computation: A x B = C
A
2 3
1 1
1 0
and B 1 1 1
1 0 2
[3 x 2] [2 x 3] A and B can be multiplied
1 1 1
3 1 2
8 2 5
12*01*1 10*01*1 11*01*1
32*11*1 10*11*1 21*11*1
82*31*2 20*31*2 51*31*2
C
[3 x 3]
Computation: A x B = C
1 1 1
3 1 2
8 2 5
12*01*1 10*01*1 11*01*1
32*11*1 10*11*1 21*11*1
82*31*2 20*31*2 51*31*2
C
A
2 3
1 1
1 0
and B 1 1 1
1 0 2
[3 x 2] [2 x 3]
[3 x 3]
Result is 3 x 3
What is a matrix
Array of numbers (m×n = m rows, n columns)
Addition, multiplication by a scalar simple: element
by element
1 3
5 2
0 4
Matrix-matrix multiplication
Number of columns in first must = rows in second
Element (i,j) in product is dot product of row i of first
matrix and column j of second matrix
1 33 6 9 4
5 22 7 8 3
0 4
Matrix-matrix multiplication
Number of columns in first must = rows in second
Element (i,j) in product is dot product of row i of first
matrix and column j of second matrix
9 21 3 7 3
5 2
0 4
1339
618
3
319
28 2
426
3
644
17
28 2
Matrix-matrix multiplication
Number of columns in first must = rows in second
Element (i,j) in product is dot product of row i of first
matrix and column j of second matrix
9 21 3 7 3
5 2
0 4
1339
618
3
319
28 2
426
3
644
17
28 2
Matrix-matrix multiplication
Number of columns in first must = rows in second
Element (i,j) in product is dot product of row i of first
matrix and column j of second matrix
9 21 3 7 3
5 2
0 4
1339
618
3
319
28 2
426
3
644
17
28 2
Matrix-matrix multiplication
Number of columns in first must = rows in second
Non-commutative (AB and BA are different in general)
Associative and distributive
A(B+C) = AB + AC
(A+B)C = AC + BC
1 33 6 9 4
5 2 NOT EVEN LEGAL!!2 7 8 3
0 4
Matrix-Vector Multiplication
Key for transforming points (next lecture)
Treat vector as a column matrix (m×1)
E.g. 2D reflection about y-axis (from textbook)
1 0
0 1
x x
y y
55
Inverse
n If A x B = I and B x A = I then
A = B-1 and B = A-1
Matrix Inversion
B1
B BB1
I
Like a reciprocal
in scalar math Like the number one
in scalar math
Transpose of a Matrix (or vector?)
1 21 3 5
3 42 4 6
5 6
T
( )T T TAB B A
Identity Matrix and Inverses
1 1
1 1 1( )
AA A A I
AB B A
3 3
1 0 0
0 1 0
0 0 1
I
Vector multiplication in Matrix form
Dot product?
Cross product?
T
b
a a a b a b a b a b
b
a b a b
x
x y z y x x y y z z
z
*
0
0
0
a a b
a a b
a a b
z y x
a b A b z x y
y x z
Dual matrix of vector a
60 COSC4328/5327 Computer Graphics
Linear Independence
• A set of vectors v1, v2, …, vn is linearly independent if
1v1+2v2+.. nvn=0 iff 1=2=…=0
• If a set of vectors is linearly independent, we cannot
represent one in terms of the others
• If a set of vectors is linearly dependent, as least one can
be written in terms of the others
• AB ≠ BA
• ABC ≠ CBA
• So order matters!
COSC4328/5327 Computer Graphics 61 62 COSC4328/5327 Computer Graphics
Dimension
• In a vector space, the maximum number of
linearly independent vectors is fixed and is called
the dimension of the space
• In an n-dimensional space, any set of n linearly
independent vectors form a basis for the space
• Given a basis v1, v2,…., vn, any vector v can be
written as
v=1v1+ 2v2 +….+nvn
where the {i} are unique
63 COSC4328/5327 Computer Graphics
Representation
• Until now we have been able to work with geometric
entities without using any frame of reference, such as a
coordinate system
• Need a frame of reference to relate points and objects to
our physical world.
• For example, where is a point? Can’t answer without a reference
system
• World coordinates
• Camera coordinates
64 COSC4328/5327 Computer Graphics
Coordinate Systems
• Consider a basis v1, v2,…., vn
• A vector is written v=1v1+ 2v2 +….+nvn
• The list of scalars {1, 2, …. n}is the
representation of v with respect to the given basis
• We can write the representation as a row or
column array of scalars
a=[1 2 …. n]T
=
n
2
1
.
65 COSC4328/5327 Computer Graphics
Example
• v=2v1+3v2-4v3
• a=[2 3 –4]T
• Note that this representation is with respect to a particular
basis
• For example, in OpenGL we start by representing vectors
using the object basis but later the system needs a
representation in terms of the camera or eye basis
66 COSC4328/5327 Computer Graphics
Coordinate Systems
• Which is correct?
• Both are because vectors have no fixed location
v
v
67
Co-ordinate Systems
X
Y
Z
Right-Handed System
(Z comes out of the screen)
X
Y
Z
Left-Handed System
(Z goes in to the screen)
68 COSC4328/5327 Computer Graphics
Frames
• A coordinate system is insufficient to represent points
• If we work in an affine space we can add a single point, the
origin, to the basis vectors to form a frame
P0
v1
v2
v3
69 COSC4328/5327 Computer Graphics
Representation in a Frame
• Frame determined by (P0, v1, v2, v3)
• Within this frame, every vector can be written as
v=1v1+ 2v2 +….+nvn
• Every point can be written as
P = P0 + b1v1+ b2v2 +….+bnvn
70 COSC4328/5327 Computer Graphics
Confusing Points and Vectors
Consider the point and the vector
P = P0 + b1v1+ b2v2 +….+bnvn
v=1v1+ 2v2 +….+nvn
They appear to have the similar representations
p=[b1 b2 b3] v=[1 2 3]
which confuses the point with the vector
A vector has no position v
p
v
Vector can be placed anywhere
point: fixed
71 COSC4328/5327 Computer Graphics
A Single Representation
If we define 0•P = 0 and 1•P =P then we can write
v=1v1+ 2v2 +3v3 = [1 2 3 0 ] [v1 v2 v3 P0]
T
P = P0 + b1v1+ b2v2 +b3v3= [b1 b2 b3 1 ] [v1 v2 v3 P0]
T
Thus we obtain the four-dimensional
homogeneous coordinate representation
v = [1 2 3 0 ] T
p = [b1 b2 b3 1 ] T
72 COSC4328/5327 Computer Graphics
Homogeneous Coordinates
The homogeneous coordinates form for a three dimensional point [x y z] is given as
p =[x’ y’ z’ w] T =[wx wy wz w] T
We return to a three dimensional point (for w0) by
xx’/w
yy’/w
zz’/w
If w=0, the representation is that of a vector
Note that homogeneous coordinates replaces points in three dimensions by lines through the origin in four dimensions
For w=1, the representation of a point is [x y z 1]
73 COSC4328/5327 Computer Graphics
Homogeneous Coordinates and
Computer Graphics
• Homogeneous coordinates are key to all computer
graphics systems
• All standard transformations (rotation, translation, scaling) can be
implemented with matrix multiplications using 4 x 4 matrices
• Hardware pipeline works with 4 dimensional representations
• For orthographic viewing, we can maintain w=0 for vectors and w=1
for points
• For perspective we need a perspective division
74 COSC4328/5327 Computer Graphics
Change of Coordinate Systems
• Consider two representations of a the same vector with
respect to two different bases. The representations are
v=1v1+ 2v2 +3v3 = [1 2 3] [v1 v2 v3]
T
=b1u1+ b2u2 +b3u3 = [b1 b2 b3] [u1 u2 u3]
T
a=[1 2 3 ]
b=[b1 b2 b3]
where
75 COSC4328/5327 Computer Graphics
Representing second basis in terms of
first Each of the basis vectors, u1,u2, u3, are vectors
that can be represented in terms of the first basis
u1 = g11v1+g12v2+g13v3
u2 = g21v1+g22v2+g23v3
u3 = g31v1+g32v2+g33v3
v
76 COSC4328/5327 Computer Graphics
Matrix Form
The coefficients define a 3 x 3 matrix
and the bases can be related by
see text for numerical examples
a=MTb
ggg
ggg
ggg
33
M =
77 COSC4328/5327 Computer Graphics
Change of Frames
• We can apply a similar process in homogeneous coordinates to the representations of both points and vectors
• Any point or vector can be represented in either frame
• We can represent Q0, u1, u2, u3 in terms of P0, v1, v2, v3
Consider two frames:
(P0, v1, v2, v3)
(Q0, u1, u2, u3) P0 v1
v2
v3
Q0
u1 u2
u3
78 COSC4328/5327 Computer Graphics
Representing One Frame in Terms of the Other
u1 = g11v1+g12v2+g13v3
u2 = g21v1+g22v2+g23v3
u3 = g31v1+g32v2+g33v3
Q0 = g41v1+g42v2+g43v3 +g44P0
Extending what we did with change of bases
defining a 4 x 4 matrix
ggg
ggg
ggg
ggg
M =
79 COSC4328/5327 Computer Graphics
Working with Representations
Within the two frames any point or vector has a representation of the same form
a=[1 2 3 4 ] in the first frame b=[b1 b2 b3 b4 ] in the second frame
where 4 b4 for points and 4 b4 for vectors and
The matrix M is 4 x 4 and specifies an affine transformation in homogeneous coordinates
a=MTb
80 COSC4328/5327 Computer Graphics
Affine Transformations
• Every linear transformation is equivalent to a change in
frames
• Every affine transformation preserves lines
• However, an affine transformation has only 12 degrees of
freedom because 4 of the elements in the matrix are fixed
and are a subset of all possible 4 x 4 linear transformations
81 COSC4328/5327 Computer Graphics
The World and Camera Frames
• When we work with representations, we work
with n-tuples or arrays of scalars
• Changes in frame are then defined by 4 x 4
matrices
• In OpenGL, the base frame that we start with is
the world frame
• Eventually we represent entities in the camera
frame by changing the world representation using
the model-view matrix
• Initially these frames are the same (M=I)
82 COSC4328/5327 Computer Graphics
Moving the Camera
If objects are on both sides of z=0, we must move
camera frame
1000
d100
0010
0001
M =