VIEWS: 75 PAGES: 66 CATEGORY: Computers & Internet POSTED ON: 2/6/2011 Public Domain
#7: Cubic Curves CSE167: Computer Graphics Instructor: Ronen Barzel UCSD, Winter 2006 Outline for today Inverses of Transforms Curves overview Bézier curves 1 Graphics pipeline transformations Remember the series of transforms in the graphics pipe: M - model: places object in world space C - camera: places camera in world space P - projection: from camera space to normalized view space D - viewport: remaps to image coordinates And remember about C: handy for positioning the camera as a model backwards for the pipeline: • we need to get from world space to camera space So we need to use C-1 You‟ll need it for project 4: OpenGL wants you to load C-1 as the base of the MODELVIEW stack 2 How do we get C-1? Could construct C, and use a matrix-inverse routine Would work. But relatively slow. And we didn‟t give you one :) Instead, let‟s construct C-1 directly based on how we constructed C based on shortcuts and rules for affine transforms 3 Inverse of a translation Translate back, i.e., negate the translation vector 1 0 0 vx 1 0 0 vx 0 1 0 vy 0 1 0 vy T(v) T (v) T(v) 1 0 0 1 vz 0 0 1 vz 0 0 0 1 0 0 0 1 Easy to verify: 1 0 0 vx 1 0 0 vx 1 0 0 vx vx 1 0 0 0 0 1 0 vy 0 1 0 vy 0 1 0 vy vy 0 1 0 0 T(v) T(v) I 0 0 1 vz 0 0 1 vz 0 0 1 vz v z 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 4 Inverse of a scale Scale by the inverses sx 0 0 0 1 sx 0 0 0 0 sy 0 0 0 S(sx , sy , sz ) 1 1 , 1 , 1 ) 0 1 sy S (sx , sy , sz ) S( s s s 0 0 0 sz 0 x y z 0 0 1 sz 0 0 0 0 1 0 0 0 1 Easy to verify: 1 sx 0 0 0 sx 0 0 0 sx sx 0 0 0 1 0 0 0 0 0 sy 0 0 0 0 0 1 0 0 1 , 1 , 1 ) S(s , s , s ) 0 1 sy 0 S( s s s sy sy 0 I x y z x y z 0 0 1 sz 0 0 0 sz 0 0 0 sz sz 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 5 Inverse of a rotation Rotate about the same axis, with the oppose angle: R 1 (a, ) R(a, ) For example: cos( ) sin( ) 0 0 sin( ) cos( ) 0 0 R z ( ) 0 0 1 0 0 0 0 1 cos( ) sin( ) 0 0 cos( ) sin( ) 0 0 sin( ) cos( ) 0 0 sin( ) cos( ) 0 0 R z ( ) R z ( ) 1 R T ( ) 0 0 1 0 0 0 1 0 z 0 0 0 1 0 0 0 1 Inverse of a rotation is the transpose: R 1 (a, ) R T (a, ) • Columns of a rotation matrix are orthonormal • ATA produces all columns‟ dot-product combinations as matrix • Dot product of a column with itself = 1 (on the diagonal) • Dot product of a column with any other column = 0 (off the diagonal) 6 Inverses of composition If you have a series of transforms composed together M A B C D To invert, compose inverses in the reverse order M 1 D 1 C 1 B1 A 1 Easy to verify: M 1 M (D 1 C 1 B1 A 1 )(A B C D) D 1 C 1 B1 A 1 A B C D I D 1 C 1 B1 B C D I D 1 C 1 C D I D 1 D I 7 Composing with inverses, pictorially QuickTime™ and a TIFF (LZW) decomp resso r are need ed to see this picture. To go from one space to another, compose along arrows Backwards along arrow: use inverse transform Lamp in world coords M table1 M top1 Mlamp Plant in Tabletop1 coords M1 M1 M table2 M top2 Mplant top1 table1 8 Model-to-Camera transform r a s so a nd mp re . e™ e c o tu re T im ) d pic ick se d is Qu p res se e th m to n co ed F (U e ed T IF a re n Camera-to-world C Model-to-Camera = C-1M y QuickTime™ and a TIFF (LZW) decomp resso r are neede d to see this picture. Qu i ckTi m e™ a nd a TIFF (Un co mp res se d) de c omp res so r z x a re ne ed ed to se e th is pi c tu re. Camera Space 9 The look-at transformation Remember, we constructed C using the look-at idiom: Given: eye point e, target point t, and up vector u Construct: columns of camera matrix C de et c et uc a uc bca Important: a, b, c are orthonormal 10 C-1 from a,b,c,d columns If we construct a transform using a, b, c, d columns, it's the same as a composition. First rotate/scale using a, b, c, then translate by d: ax bx cx d x 1 0 0 d x ax bx cx 0 a b c d 0 1 0 d a b c 0 C y y y y y y y y T(d) M az bz cz dz 0 0 1 dz az bz cz 0 0 0 0 1 0 0 0 1 0 0 0 1 If a, b, c are orthonormal, they define a pure rotation: C T(d) R To take the inverse: 1 C 1 T(d) R R 1 T 1 (d) C 1 R T T(d) ax ay az 0 1 0 0 d x b b b 0 0 1 0 d C 1 x y z y cx cy cz 0 0 0 1 dz 0 0 0 1 0 0 0 1 Build R T using a, b, c as rows, build T(d), compose them Notice, this does the translation first, then the rotation. Exercise: what does the final matrix look like? 11 Outline for today Inverses of Transforms Curves overview Bézier curves 12 Usefulness of curves in modeling Surface of revolution 13 Usefulness of curves in modeling Extruded/swept surfaces 14 Usefulness of curves in modeling Surface patches QuickTime™ and a QuickTime™ an d a TIFF (Uncompressed) decompressor TIFF (Uncompressed) decompressor are need ed to see this picture. are need ed to see this p icture . QuickTime™ and a QuickTime™ an d a TIFF (Uncompressed) decompressor TIFF (Uncompressed) decompre ssor are need ed to see this p icture . are neede d to see this picture. 15 Usefulness of curves in animation Provide a “track” for objects http://www.f-lohmueller.de/ 16 Usefulness of curves in animation Specify parameter values over time: 2D curve edtior 17 How to represent curves Specify every point along a curve? Used sometimes as “freehand drawing mode” in 2D applications Hard to get precise results Too much data, too hard to work with generally Specify a curve using a small number of “control points” Known as a spline curve or just spline 18 Interpolating Splines Specify points, the curve goes through all the points Seems most intuitive Surprisingly, not usually the best choice. Hard to predict behavior • Overshoots • Wiggles Hard to get “nice-looking” curves 19 Approximating Splines “Influenced” by control points but not necessarily go through them. QuickTime™ an d a TIFF (Uncompressed) decompressor are need ed to see this picture. Various types & techniques Most common: (Piecewise) Polynomial Functions Most common of those: • Bézier • B-spline Each has good properties We‟ll focus on Bézier splines 20 What is a curve, anyway? We draw it, think of it as a thing existing in space But mathematically we treat it as a function, x(t) Given a value of t, computes a point x Can think of the function as moving a point along the curve x(t) z y x(0.0) x(0.5) x(1.0) y x 21 The tangent to the curve Vector points in the direction of movement (Length is the speed in the direction of movement) dx Also a function of t, written x(t) or d dt x(t) or (t) dt x(t) z y x’(0.0) x’(0.5) x’(1.0) y x 22 Polynomial Functions Linear: f t at b (1st order) Quadratic: f t at 2 bt c (2nd order) Cubic: f t at 3 bt 2 ct d (3rd order) 23 Point-valued Polynomials (Curves) Linear: x t at b (1st order) Quadratic: x t at 2 bt c (2nd order) Cubic: x t at 3 bt 2 ct d (3rd order) Each is 3 polynomials “in parallel”: xx (t) ax t bx xy (t) ayt by xz (t) azt bz We usually define the curve for 0 ≤ t ≤ 1 24 How much do you need to specify? Two points define a line (1st order) Three points define a quadratic curve (2nd order) Four points define a cubic curve (3rd order) k+1 points define a k-order curve Let‟s start with a line… 25 Linear Interpolation Linear interpolation, AKA Lerp Generates a value that is somewhere in between two other values A „value‟ could be a number, vector, color, … Consider interpolating between two points p0 and p1 by some parameter t This defines a “curve” that is straight. AKA a first-order spline When t=0, we get p0 When t=1 we get p1 When t=0.5 we get the midpoint p1 . p0 t=1 . 0<t<1 t=0 x(t) Lerp t, p 0 , p1 1 t p 0 t p1 26 Linear interpolation We can write this in three ways All exactly the same equation Just different ways of looking at it Different properties become apparent • As a weighted average of the control points: x(t) (1 t)p 0 (t)p1 • As a polynomial in t : x(t) (p1 p 0 )t p 0 • In a matrix form : 1 1 t x(t) p 0 p1 1 0 1 27 Linear interpolation as weighted average x(t) (1 t)p 0 (t)p1 B0 (t) p 0 B1 (t)p1 , where B0 (t) 1 t and B1 (t) t Each weight is a function of t The sum of the weights is always 1, for any value of t Also known as blending functions B0 (t) B1 (t) QuickTime™ an d a TIFF (Uncompressed) decompressor are need ed to see this picture. 28 Linear interpolation as polynomial x(t) (p1 p 0 ) t p0 vector point Curve is based at point p0 Add the vector, scaled by t p1 - p0 . p0 . . .5(p1-p0) 29 Linear interpolation in matrix form 1 1 t x(t) p 0 p1 G B T 1 0 1 where: G p 0 p1 is the "Geometry matrix" 1 1 B is the "Geometric Basis" 1 0 t T is the "Polynomial basis" 1 Actually, this is shorthand for separate equations for x, y, z 1 1 t 1 1 t 1 1 t x x (t) p0 x p1x 1 x y (t) p0 y p1y 1 0 1 xz (t) p0 z p1z 1 0 1 1 0 Or it can really be put into one matrix p0 x p1x 1 1 t x(t) p0 y p1y p0 z p1z 1 0 1 30 Linear Interpolation: tangent For a straight line, the tangent is constant x(t) p1 p 0 • As a weighted average of the control points: x(t) (1)p 0 (1)p1 • As a (trivial, zero-order) polynomial in t : x(t) 0t (p1 p 0 ) • In a matrix form : 1 1 1 x(t) p 0 p1 1 0 0 31 Outline for today Inverses of Transforms Curves overview Bézier curves 32 Bézier Curves Can be thought of as a higher order extension of linear interpolation p1 p1 p2 p1 p3 p0 p0 p0 p2 Linear Quadratic Cubic 33 Cubic Bézier Curve Most common case 4 points for a cubic Bézier Interpolates the endpoints Midpoints are “handles” that control the tangent at the endpoints Easy and intuitive to use Many demo applets online http://www.cs.unc.edu/~mantler/research/bezier/ http://www.theparticle.com/applets/nyu/BezierApplet/ http://www.sunsite.ubc.ca/LivingMathematics/V001N01/UBCExamples/Bezier/bezier.html Convex Hull property Variation-diminishing property 34 Bézier Curve Formulation Ways to formulate Bézier curves, analogous to linear: Weighted average of control points -- weights are Bernstein polynomials Cubic polynomial function of t Matrix form Also, the de Casteljau algorithm: recursive linear interpolations Aside: Many of the original CG techniques were developed for Computer Aided Design and manufacturing. Before games, before movies, CAD/CAM was the big application for CG. Pierre Bézier worked for Peugot, developed curves in 1962 Paul de Casteljau worked for Citroen, developed the curves in 1959 35 Bézier Curve Find the point x on the p1 curve as a function of parameter t: • p0 x(t) p2 p3 36 de Casteljau Algorithm A recursive series of linear interpolations Works for any order. We‟ll do cubic Not terribly efficient to evaluate this way Other forms more commonly used So why study it? Kinda neat Intuition about the geometry Useful for subdivision (later today) 37 de Casteljau Algorithm p1 Start with the control points p0 And given a value of t In the drawings, t≈0.25 p2 p3 38 de Casteljau Algorithm p1 q1 q0 q0 (t) Lerp t,p 0 ,p1 p0 q1 (t) Lerp t,p1 ,p 2 p2 q2 (t) Lerp t,p 2 ,p 3 q2 p3 39 de Casteljau Algorithm q1 q0 r0 r1 r0 (t) Lerp t, q 0 (t), q1 (t) r1 (t) Lerp t, q1 (t), q 2 (t) q2 40 de Casteljau Algorithm r0 • x r1 x(t) Lerp t, r0 (t), r1 (t) 41 de Casteljau algorihm p1 • p0 x p2 Applets http://www2.mat.dtu.dk/people/J.Gravesen/cagd/decast.html http://www.caffeineowl.com/graphics/2d/vectorial/bezierintro.html p3 42 Recursive Linear Interpolation p0 q0 Lerp t,p 0 ,p1 r0 Lerp t, q 0 , q1 p1 x Lerp t, r0 , r1 q1 Lerp t,p1 ,p 2 r1 Lerp t, q1 , q 2 p2 q2 Lerp t,p 2 ,p 3 p3 p1 q0 r0 p2 x q1 r1 p3 q2 p4 43 Expand the Lerps q 0 (t) Lerp t, p 0 , p1 1 t p 0 tp1 q1 (t) Lerp t, p1 , p 2 1 t p1 tp 2 q 2 (t) Lerp t, p 2 , p 3 1 t p 2 tp 3 r0 (t) Lerp t, q 0 (t), q1 (t) 1 t 1 t p 0 tp1 t 1 t p1 tp 2 r1 (t) Lerp t, q1 (t), q 2 (t) 1 t 1 t p1 tp 2 t 1 t p 2 tp 3 x(t) Lerp t, r0 (t), r1 (t) 1 t 1 t 1 t p 0 tp1 t 1 t p1 tp 2 t 1 t 1 t p 1 tp 2 t 1 t p 2 tp 3 44 Weighted average of control points Group this as a weighted average of the points: x(t) 1 t 1 t 1 t p 0 tp1 t 1 t p1 tp 2 t 1 t 1 t p1 tp 2 t 1 t p 2 tp 3 x(t) 1 t p 0 31 t tp1 31 t t 2 p 2 t 3p 3 3 2 B0 (t ) B1 (t ) x(t) t 3 3t 2 3t 1 p 0 3t 3 6t 2 3t p1 3t 3 3t 2 p t p 2 3 3 B2 (t ) B3 (t ) 45 Bézier using Bernstein Polynomials x(t) B0 t p 0 B1 t p1 B2 t p 2 B3 t p 3 The cubic Bernstein polynomials : B0 t t 3 3t 2 3t 1 B1 t 3t 3 6t 2 3t B2 t 3t 3 3t 2 B0 (t) B1 (t) B2 (t) B3 (t) QuickTime™ and a TIFF (Uncompressed) decompressor are neede d to see this picture. B3 t t 3 B (t) 1 i Notice: Weights always add to 1 B0 and B3 go to 1 -- interpolating the endpoints 46 General Bernstein Polynomials B0 t t 1 1 B0 t t 2 2t 1 2 B0 t t 3 3t 2 3t 1 3 B1 t t 1 B12 t 2t 2 2t B13 t 3t 3 6t 2 3t B2 t t 2 2 B2 t 3t 3 3t 2 3 B3 t t 3 3 QuickTime™ an d a QuickTime™ an d a Quic kTime™ and a TIFF (Uncompressed) decompressor TIFF ( Uncompress ed) decompr essor TIFF (Uncompressed) decompressor ar e neede d to s ee this picture. are need ed to see this picture. are need ed to see this picture. n n n! B t 1 t t n i i i i!n i ! n i i Bin t 1 47 General Bézier using Bernstein Polynomials Bernstein polynomial form of an nth-order Bézier curve: n B t 1 t t n n i i i i n x t Bin t p i i0 48 Convex Hull Property Construct a convex polygon around a set of points The convex hull of the control points Any weighted average of the points, with the weights all between 0 and 1: Known as a convex combination of the points Result always lies within the convex hull (including on the border) Bézier curve is a convex combination of the control points Curve is always inside the convex hull Very important property! • Makes curve predictable p1 p3 • Allows culling • Allows intersection testing • Allows adaptive tessellation p0 p2 49 Cubic Equation Form Start with Bernstein form: x(t) t 3 3t 2 3t 1 p 0 3t 3 6t 2 3t p1 3t 3 3t 2 p 2 t 3 p 3 Regroup into coefficients of t : x(t) p 0 3p1 3p 2 p 3 t 3 3p 0 6p1 3p 2 t 2 3p 0 3p1 t p 0 1 a p 0 3p1 3p 2 p 3 b 3p 0 6p1 3p 2 x(t) at bt ct d 3 2 c 3p 0 3p1 d p 0 Good for fast evaluation: precompute constant coefficients (a,b,c,d) Doesn‟t give much geometric intuition But the geometry can be extracted from the coefficients 50 Aside: linear combinations of points Reminder: we can‟t scale a point or add two points Can subtract two points Can take weighted average of points if the weights add up to one Act on homogeneous points: w component of result must be 1 .25 .75 OK point 1 1 1 Can also take weighted average of points if the weights add up to 0 The result gives w=0, i.e. a vector E.g. p-q is the same as (+1)p + (-1)q Can also do (-1)p0+(3)p1+(-3)p2+(1)p3 1 3 3 1 OK vector 1 1 1 1 0 51 Cubic Equation, vector notation a p 0 3p1 3p 2 p 3 b 3p 0 6p1 3p 2 x(t) at bt ct d 3 2 c 3p 0 3p1 d p 0 Curve is based at p 0 AKA d Increasing t introduces the other vectors: first order: c -- moves towards p1 second order: b -- subtracts off c, pulls towards p 2 third order: a -- substracts off everything, moves towards p 3 52 Cubic Matrix Form t 3 a p 0 3p1 3p 2 p 3 2 b 3p 0 6p1 3p 2 x(t) a b c d t t c 3p 0 3p1 1 d p 0 1 3 3 1 t 3 3 6 3 0 t 2 x(t) p 0 p1 p 2 p 3 3 3 0 0 t 1 0 0 0 1 G Bez B Bez T Other cubic splines use different basis matrix B Hermite, Catmull-Rom, B-Spline, … 53 Cubic Matrix Form 3 parallel equations, in x, y and z: 1 3 3 1 t 3 3 6 3 0 t 2 x x (t) p0 x p1x p2 x p3x 3 3 0 0 t 1 0 0 0 1 1 3 3 1 t 3 3 6 3 0 t 2 x y (t) p0 y p1y p2 y p3y 3 3 0 0 t 1 0 0 0 1 1 3 3 1 t 3 3 6 3 0 t 2 x z (t) p0 z p1z p2 z p3z 3 3 0 0 t 1 0 0 0 1 54 Matrix Form Bundle into a single matrix 1 3 3 1 t 3 p0 x p1x p2 x p3x 2 3 6 3 0 t x(t) p0 y p1y p2 y p3y 3 3 0 0 t p0 z p1z p2 z p3z 1 0 0 0 1 x(t) G Bez B Bez T x(t) C T Evaluate quickly: Precompute C Take advantage of existing 4x4 matrix hardware support 55 Tangent The derivative of a curve represents the tangent vector to the curve at some point dx t dt x t 56 Tangent Computing the tangent of a polynomial curve is easy: dx x(t) at 3 bt 2 ct d x(t) (t) 3at 2 2bt c dt t 3 3t 2 2 x(t) a b c d t x(t) dx (t) a b c d 2t t dt 1 1 0 Notice x’(t) is a vector Doesn‟t depend on d Doesn‟t depend on position of curve 57 Transforming Bézier curves Two ways to transform a Bézier curve Transform the control points, then compute resulting spline point Compute spline point, then transform it Either way, get the same point! Curve is defined via affine combination of points Invariant under affine transformations Convex hull property always remains 58 Drawing Bézier Curves How can you draw a curve? Generally no low-level support for drawing curves Can only draw line segments or individual pixels Approximate the curve as a series of line segments Analogous to tessellation of a surface Methods: • Sample uniformly • Sample adaptively • Recursive Subdivision 59 Uniform Sampling Approximate curve with N straight segments N chosen in advance i Evaluate x i x ti where ti for i 0, 1, , N N i3 i2 i xi a 3 b 2 c d N N N x(t) x4 x2 Connect the points with lines x1 x3 Too few points? x0 Bad approximation “Curve” is faceted Too many points? Slow to draw too many line segments Segments may draw on top of each other 60 Adaptive Sampling Use only as many line segments as you need Fewer segments needed where curve is mostly flat More segments needed where curve bends No need to track bends that are smaller than a pixel x(t) Various schemes for sampling, checking results, deciding whether to sample more Or, use knowledge of curve structure: Adapt by recursive subdivision 61 Recursive Subdivision Any cubic curve segment can be expressed as a Bézier curve Any piece of a cubic curve is itself a cubic curve Therefore: Any Bézier curve can be broken up into smaller Bézier curves But how…? 62 de Casteljau subdivision p1 q0 r0 r1 p0 x p2 de Casteljau construction points are the control points of two Bézier q2 sub-segments p3 63 Adaptive subdivision algorithm: Use de Casteljau construction to split Bézier segment Examine each half: If flat enough: draw line segment Else: recurse To test if curve is flat enough Only need to test if hull is flat enough • Curve is guaranteed to lie within the hull e.g., test how far the handles are from a straight segment • If it‟s about a pixel, the hull is flat 64 Done Next class: Extending to longer curves Extending to curved surfaces 65