Upload
ashlynn-tate
View
218
Download
0
Embed Size (px)
Citation preview
Simple Mathematics Simple Mathematics for graphicsfor graphics
Simple Mathematics Simple Mathematics for graphicsfor graphics
Volumes from Silhouettes
• Start with collection of “calibrated” images
Volumes from Silhouettes
Cup on turntable example
3D Curves from Edges• “Feature-based” stereo matching
3D Curves from Edges• Extract extremal and internal
edges
3D Curves from Edges• Match curves along epipolar lines
viewing rayviewing rayepipolar planeepipolar plane
epipolar lineepipolar line
What is Computer Graphics?
• Using a computer to generate an image from a representation.
Model Imagecomputer
Representations• How do we represent an object?
– Points • p = [x y z]
– Mathematical Functions• X2 + Y2=R2
– Polygons (most commonly used)• Points• Connectivity
Representing Points and Vectors
• A 3D point p = [ x y z ]– Represents a location with respect to
some coordinate system
• A 3D vector v = [ x y z]– Represents a displacement from a
position
Linear Algebra• Why study Linear Algebra?
– Deals with the representation and operations commonly used in Computer Graphics
Vector Spaces• Consists of a set of elements,
called vectors and two operations that are defined on them, addition and multiplication
Vector Addition• Given V = [X Y Z] and W = [A B C]
– V+W = [X+A Y+B Z+C]
• Properties of Vector addition– Commutative: V+W=W+V– Associative (U+V)+W = U+(V+W)– Additive Identity: V+0 = V– Additive Inverse: V+W = 0, W=-V
Parallelogram Rule• To visualize what a vector addition
is doing, here is a 2D example:
V
W
V+W
Vector Multiplication• Given V = [X Y Z] and a Scalar s and t
– sV = [sX sY sZ]
• Properties of Vector multiplication• Associative: (st)V = s(tV)• Multiplicative Identity: 1V = V• Scalar Distribution: (s+t)V = sV+tV• Vector Distribution: s(V+W) = sV+sW
Dot Product and Distances
• Given u = [x y z] and v = [a b c]– v•u = ax+by+cz
• The Euclidean distance of u from the origin is sqrt( x2+y2+z2) and is denoted by ||u||– Notice that ||u|| = sqrt(u•u)
• The Euclidean distance between u and v is sqrt( (x-a) 2+(y-b) 2+(z-c) 2) ) and is denoted by || u-v ||
Properties of the Dot Product
• Given a vector u, v, w and scalar s– The result of a dot product is a
SCALAR value– Commutative: v•w = w•v– Non-degenerate: v•v=0 only when
v=0– Bilinear: v•(u+sw)=v•u+s(v•w)
Angles and Projection• Alternative view of the dot product• v•w=||v|| ||w|| cos() where is the
angle between v and w• If v is a unit vector (||v|| = 1) then if
we perpendicularly project w onto v can call this newly projected vector u then ||u|| = v•w
w
vu
Matrices• A compact way of representing operations on
points and vectors• 3x3 Matrix A looks like
• a(i,j) refers to the element of matrix A
333231
232221
131211
aaa
aaa
aaa
Matrix Multiplication• If A is an nk matrix and B is a kp then
AB is a np matrix with entries c(i,j)where– c(i,j)=a(i,s)b(s,j)
• Alternatively, if we took the rows of A and columns of B as individual vectors then
• c(i,j)=Ai•Bj where the subscript refers to the row and column, respectively
Matrix Multiplication Properties
• Associative: (AB)C = A(BC)• Distributive: A(B+C) = AB+AC• Multiplicative Identity: I= diag(1)
(square matrix• NOT commutative: ABBA
Determinant of a Matrix
• Defined on a square matrix (nxn)
• Where A1i determinant of (n-1)x(n-1) submatrix A gotten by deleting the first row and the ith column
n
i ii AAA
1 11)1(det
Recursive Definition!!• The basis case• det of a 2x2 matrix is defined to
be ad-bc where
db
ca
Uses of the Determinant?
• Linear Independence of columns in a matrix
• Cross Product– Given 2 vectors v=[v1 v2 v3], w=[w1
w2 w3], the cross product is defined to be the determinant of
1221
3113
2332
321
321
wvwv
wvwv
wvwv
www
vvv
kji
Cross Product Properties
• The Cross Product of v and w is denoted by vw
• Is a VECTOR, perpendicular to the plane defined by v and w
• ||vw||=||v|| ||w|| |sin| is the angle between v and w
• vw=-(wv)v
wvw
Matrix Transpose and Inverse
• The Transpose of a matrix A, denoted by AT is defined as aij=aji (exchanging the rows and columns)
• If A and B are nxn matrices and AB=BA=I then B is the inverse of A, denoted by A-1
• (AB)-1=B-1A-1 same applies for transposeT
MM T 11
Methods for finding the Inverse
• Explicit Methods– Gaussian-Jordan Elimination
• Create the Augmented matrix [A|I] and reduce the left side to the identity using elementary row operations and the right hand side will be the inverse. ie. [I|A-1]
– Cramer’s Rule• Solve for A’ where a’
ij=det(submatrix(Aij))
• A-1=(1/det(A))(A’)T
Implicit Methods• Instead of explicitly calculating A-1, there
are techniques that solve equations of the form Ax=b (system of linear equations).
• Clearly x=A-1b but we do not need to explicitly calculate A-1 to calculate x.– LU Decomposition– QR Factorization– Singular Value Decomposition (SVD)– Conjugate Gradient if sparse…
Transformations• Why use transformations?
Create object in convenient coordinates
Reuse basic shape multiple times Hierarchical modeling System independent Virtual cameras
Translation
z
y
x
z
y
x
t
t
t
= +
z
y
x
tttT zyx ),,(
z
y
x
z
y
x
t
t
t
= +
'
'
'
z
y
x
Properties of Translation
v=v)0,0,0(T
=v),,(),,( zyxzyx tttTsssT
=
=v),,(1zyx tttT
v),,(),,( zyxzyx tttTsssT v),,(),,( zyxzyx sssTtttT
v),,( zzyyxx tststsT
v),,( zyx tttT
Rotations (2D)
sin
cos
ry
rx
cos)sin(sin)cos('
sin)sin(cos)cos('
rry
rrx
)sin('
)cos('
ry
rx
cossinsincos)sin(
sinsincoscos)cos(
cossin'
sincos'
yxy
yxx
yx,
',' yx
x
y
Rotations 2D• So in matrix notation
y
x
y
x
cossin
sincos'
'
Rotations (3D)
100
0cossin
0sincos
)(
cos0sin
010
sin0cos
)(
cossin0
sincos0
001
)(
z
y
x
R
R
R
Properties of RotationsIRa )0(
)()()()( aaaa RRRR
)()()( aaa RRR
)()()(1 Taaa RRR
)()()()( abba RRRR order matters!
Combining Translation & Rotation
)1,1(T
)45( R
)45( R
)1,1(T
Combining Translation & Rotation
Tvv'
RTR
TR
R
vv
vv
vv
''
)(''
'''
vv R'
TR
T
vv
vv
''
'''
Scaling
zs
ys
xs
z
y
x
z
y
x
'
'
'
z
y
x
zyx
s
s
s
sssS
00
00
00
),,(
Uniform scaling iff zyx sss
Homogeneous Coordinates
z
y
x
w
Z
Y
X
can be represented as
wherew
Zz
w
Yy
w
Xx ,,
Translation Revisited
11000
100
010
001
),,(z
y
x
t
t
t
z
y
x
tttTz
y
x
zyx
Rotation & Scaling Revisited
11000
000
000
000
),,(z
y
x
s
s
s
z
y
x
sssSz
y
x
zyx
11000
0cossin0
0sincos0
0001
)(z
y
x
z
y
x
Rx
Combining Transformations
vv
vvvv
vvv
vv
M
TRSTRT
RSR
S
'''
''''''
'''
'
where TRSM
Transforming Tangents
t
qp
qp
qpt
qpt
M
M
MM
)(
'''
Transforming Normals
nnn
nn
nn
tntn
tn
tn
tn
TT
T
TT
TT
T
T
T
MM
M
M
M
M
11'
'
'
'
0'
0''
0
Rotations about an arbitrary axis
Rotate by around a unit axis r
r
• We can view the rotation around an arbitrary axis as a set of simpler steps
• We know how to rotate and translate around the world coordinate system
• Can we use this knowledge to perform the rotation?
Rotation about an arbitrary axis
• Translate the space so that the origin of the unit vector is on the world origin
• Rotate such that the extremity of the vector now lies in the xz plane (x-axis rotation)
• Rotate such that the point lies in the z-axis (y-axis rotation)
• Perform the rotation around the z-axis• Undo the previous transformations
Rotation about an arbitrary axis
• Step 1Rotate x-axis
x
y
z
(a,b,c)
x
Closer Look at Y-Z Plane
• Need to rotate degrees around the x-axis
y
z
Equations for
||)1,0,0(||||),,0(||
)1,0,0(),,0()cos(
||),,0(||||)1,0,0(||
||),,0()1,0,0(||)sin(
cb
cb
cb
cb
Rotation about the Y-axis
• Using the same analysis as before, we need to rotate degrees around the Y-axis
y
z
x
Rotation about the Z-axis
• Now, it is aligned with the Z-axis, thus we can simply rotate degrees around the Z-axis.
• Then undo all the transformations we just did
Equation summary
c
b
a
TRRRRRT
c
b
a
rot xyzyxaxis )()()()()(
'
'
'
)( 111
Deformations
Transformations that do not preserve shape Non-uniform scaling Shearing Tapering Twisting Bending
Shearing
11000
01
01
01
1
'
'
'
z
y
x
ss
ss
ss
z
y
x
zyzx
yzyx
xzxy
0
0
0
1
zyzx
yzyx
xz
xy
ss
ss
s
s
x
y
x
y
Tapering
11000
0)(00
00)(0
0001
1
'
'
'
z
y
x
xf
xf
z
y
x
Image courtesy of Watt, 3D Computer Graphics
Twisting
11000
0))(cos(0))(sin(
0010
0))(sin(0))(cos(
1
'
'
'
z
y
x
yy
yy
z
y
x
Image courtesy of Watt, 3D Computer Graphics
Bending
11000
0)()(0
0)()(0
0001
1
'
'
'
z
y
x
ykyh
ygyf
z
y
x
Image courtesy of Watt, 3D Computer Graphics
What have we seen so far?
• Basic representations (point, vector)• Basic operations on points and vectors
(dot product, cross products, etc.)• Transformation – manipulative
operators on the basic representation (translate, rotate, deformations) – 4x4 matrices to “encode” all these.
Why do we need this?• In order to generate a picture from
a model, we need to be able to not only specify a model (representation) but also manipulate the model in order to create more interesting images.
Overview• The next set of slides will deal with
the other half of the process (at least in a simplistic fashion)
• From a model, how do we generate an image
Model Imagecomputer
Accumulation of Transformations
baseT
)()()(
)()(
RRTRMM
RTRMM
TM
basearmhand
basebasearm
basebase
base
arm hand
Example: Robot arm
Orthonormal Coordinates
Iff u and v are orthonormal:
Tyx
yx
Mvv
uu
M
100
0
01
100100
10
01
100
0
011 vr
ur
yx
yx
y
x
yx
yx
vv
uu
r
r
vv
uu
TM
Coordinate Systems Object coordinates World coordinates Camera coordinates Normalized device coordinates Window coordinates
Object CoordinatesConvenient place to model the
object
O
World CoordinatesCommon coordinates for the scene
O
O
W
Camera CoordinatesCoordinate system with the camera
in a convenient pose
u
v
n
1000
nr
vr
ur
zyx
zyx
zyx
nnn
vvv
uuu
M
Normalized Device Coordinates
Device independent coordinatesVisible coordinate usually range
from:
11
11
11
z
y
x
Window CoordinatesAdjusting the NDC to fit the window
0
0
2)1(
2)1(
yheight
yy
xwidth
xx
ndw
ndw
),( 00 yx is the lower left of the window
width
height
Perspective ProjectionTaking the camera coordinates to
NDC
z
x
near
Perspective Projection
z
x
near'p
p
z
xnearx
z
x
near
x
'
'
Rasterization
Array of pixels
0
0 1 2 3 4
1
2
3
Rasterizing LinesGiven two endpoints, find the pixels that make up the line.
),(),,( 1100 yxyx
Rasterizing LinesRequirements
1. No gaps
2. Minimize error (distance to line)
Rasterizing Lines
Line(int x0, int y0, int x1, int y1) float dx = x1 – x0; float dy = y1 – y0; float m = dy/dx; float x = x0, y= y0;
for(x = x0; x <= x1; x++) setPixel(x,round(y)); y = y+m;
Assume –1 < m < 1, x0 < x1
Rasterizing LinesProblems with previous algorithm
1. round takes time2. uses floating point arithmetic
Midpoint Algorithm
NE
E
P=(x,y)
MQ
If Q <= M, choose E. If Q > M, choose NE
Implicit Form of a Line
dxBcdxbdya
dxBydxxdy
Bxdx
dyycbyax
0
0
Implicit form Explicit form
Positive below the lineNegative above the lineZero on the line
Decision Function
cybxayxFd
cybxayxFd
)()1(),1(
),(
2
1
2
1
Choose NE if d > 0Choose E if d <= 0
Incrementing d
cybxayxFdnew )()2(),2(2
1
2
1
If choosing E:
But:
cybxayxFdold )()1(),1(2
1
2
1
So:
Eaddd oldnewinc
Incrementing d
cybxayxFdnew )()2(),2(2
3
2
3
If choosing NE:
But:
cybxayxFdold )()1(),1(2
1
2
1
So:
NEbaddd oldnewinc
Initializing d
2
1
2
1
2
1
2
1
00
0000 )()1(),1(
ba
bacybxa
cybxayxFd
Multiply everything by 2 to remove fractions (doesn’t change the sign)
Midpoint AlgorithmLine(int x0, int y0, int x1, int y1) int dx = x1 – x0, dy = y1 – y0; int d = 2*dy-dx; int delE = 2*dy, delNE = 2*(dy-dx); int x = x0, y = y0; setPixel(x,y);
while(x < x1) if(d<=0) d += delE; x = x+1; else d += delNE; x = x+1; y = y+1; setPixel(x,y);
Assume 0 < m < 1, x0 < x1
See: Bresenham Algorithm
Limitations?• The midpoint line algorithm
assumes that the slope (m) is between 0 and 1
• This implies that this algorithm only applies to lines in region 1
• Extending to other regions left as an a programming assignment
1
23
4
5
67
8
Anti-aliasing LinesLines appear jaggy
Sampling is inadequate
Anti-aliasing LinesTrade intensity resolution for spatial resolution
Anti-aliasing LinesLine(int x0, int y0, int x1, int y1) float dx = x1 – x0; float dy = y1 – y0; float m = dy/dx; float x = x0, y= y0;
for(x = x0; x <= x1; x++) int yi = floor(y); float f = y – yi; setPixel(x,yi, 1-f); setPixel(x,yi+1, f); y = y+m;
Assume 0 < m < 1, x0 < x1
Putting it all together!!• Take your representation (points) and
transform it from Object Space to World Space• Take your World Space point and transform it
to Camera Space• Perform the remapping and projection onto the
image plane in Normalized Device Coordinates• Perform this set of transformations on each
point of the polygonal object• “Connect the dots” through line rasterization
IntuitivelyObjectSpace
WorldSpace
CameraSpace
Rasterization