Curves CGT 511 Curves Bedřich Beneš, Ph.D. Purdue University Department of Computer Graphics Technology What is a curve? Mathematical ldefinition i i is a bit complex 1) The continuous o image of an interval in an nd space 2) A continuous map of a 1D space to an nd space Curves What is a curve? Physical definition i i curve is a trajectory of a moving point This definition is usually good enough in CG Curves Any point has two neighbors (no branching) Endpoints have one Some curves do not have endpoints Infinite curves Closed curves Periodic curves (except for space filling curves they are 2D )
Curve representation Explicit: y=f(x) cannot represent closed curves Implicit: f(x,y)=0 many good properties usually must be calculated numerically f(x,y) is a scalar function (one number) Curve representation Parametric: Q ( t ) = [ x ( t ), y ( t ), z ( t )] curve is a function of one parameter a trajectory of a moving point the meaning of the parameter t is the time It returns two or more values (vector) Curve representation trajectory (curve) is thus represented as a set of points in space Parametric Representation Example Sinusoidal curve Q(t)=[t, sin(t)] x=x(t) y=y(t) z=z(t) Circle Q(t)=[r sin(t), r cos(t)] Parabola Q(t)=[t, t2]
Parametric Representation The parameter t usually goes from 0 to 1 ( 0 t 1 ) Q(0)=[x(0),y(0),z(0)] the starting point of the curve Q(1)=[x(1),y(1),z(1)] (1)] the end point of the curve Points and Vectors Let s have two points A and B The vector P is the P=B A Example: A=[1,2,0], B=[2,1,0] P= B A = [2,1,0] [1,2,0] = (1, 1,0) the same vector can be obtained from an infinite number of different pairs! e.g., from A=[0,0,0], B=[1, 1,0] Points and Vectors A vector represents a set of oriented lines In other words a vector can be translated! A vector added to a point yields to a new point Example: A=[1,2,0], P=(1, 1,0) A+P=[1,2,0] + (1, 1,0) = [2,1,0] Parametric Equation of a Line Defined by two points A and B its equation is Q(t) = [x(t), y(t), z(t)] = A + t(b A); < t < there are special cases: Q(0) = A Q(1) = B Note also: Point + t Vector >point
Parametric Equation of a Line Example: A=[1,2,3], B=[0,1,6] Q(t)=[x(t), y(t), z(t)] = = [1,2,3] + t ([0,1,6] [1,2,3])= = [1 t, 2 t, 3+3t] try t=0 Q(0) = [1,2,3] t=1 Q(1) = [1 1,2 1,3+3] = [0,1,6] Parametric Equation of a Line Can be also thought of as a blending function x(t) = (1 t) X1 + t X2 y(t) = (1 t) Y1 + t Y2 z(t) = (1 t) Z1 + t Z2 this linear blending is frequently used in CG: P(t) = A+t(B A) A) P(t) = (1 t) A + t B Note:(1 t)+t=1 Parametric Curve Derivative The derivative dq( t) dx( t) dy( t) dz( t) Q'( t) = =,, dt dt dt dt Example: derivatives of the functions from the previous examples: sin. curve Q(t)=[t, sin(t)] Q (t) = (1, cos(t)) circle Q(t)=[r sin(t), r cos(t)] Q (t) (t)=(r(r cos(t), r r sin(t)) parabola Q(t)=[t2,t] Q (t)=(2t,1) Parametric Curve Derivative Note: Curve is a set of points Derivative of a curve is a vector. Why? Take the limit process A dq( t) Q'( t) = = lim dt Δt > 0 Δt B A B Δt A Δt B A B Δt
Parametric Curve Derivative The derivative is the tangent t vector to the function in the given point Tangent is a line passing through the given point in the direction of the tangent vector P(t) Tangent & Tangent Vector Example Example: Having curve Q(t)=[ 4t 3 +6t 2, 6t 3 9t 2 +3t] Evaluate tangent in the point Q(0.5). Solution: 1. Get the point Q(0.5) 2. Evaluate derivative Q (t) 3. Evaluate tangent vector Q (0.5) 4. Tangent is: P(s)=Q(0.5) + s Q (0.5) Tangent & Tangent Vector Example ad 1. Q(0.5)= [ 4*0.53+6*0.52, 6*0.53 9*0.52+3*0.5]=[1,0] ad d2. Q () Q (t) = (12 ( 12t2+12t, 2 18t2 18t+3) ad 3. Q (0.5) = (3, 3/2) ad 4. P(s) = [1,0] + s*(3, 3/2) by the way: P(0)=[1,0]=Q(0.5) 1 2 P(s) Q(t) Parametric curves continuity Motivation: Expression of the entire shape from only one curve is usually impossibleibl Curves are connected together Every part of the curve is called a segment The point where curves meet is a knot (joint) The parametric continuity is denoted by C
Parametric curves continuity knot segment Free form curves continuity C 0 If two segments meet in a point, they are continuously connected or simply connected It is denoted by C 0 Two segments Q 1 (t) and Q 2 (t) are continuously connected iff Q 1 (1) = Q 2 (0) One segment starts where the other ends Free form curves continuity C 1 So called C 1 parametric continuity requires two segments to be C 0 and their derivatives to be equal in the Denoted by C 1 They share tangent in the knot (not only the tangent vectors) If we move from the one curve to the other, speed and the direction is unchanged (continuous) Free form curves continuity C 1 Two segments Q 1 (t) and Q 2 (t) are parametrically continuously connected iff 1) Q 1 (1)=Q 2 (0) Equality in the knot 2) Q 1 (1)=Q 2 (0) Derivatives i in the knot are equal Does C 1 imply C 0?
Continuity C 0 vs. C 1 Continuity C 0 : the knot is equal, but the tangent vectors are different Continuity C 0 vs. C 1 Continuity C 1 : the knot is equal, the tangent as well, and the tangents vectors are nonzero Q 1 (1)=Q 2 (0) Q 1 (1) Q 1 (t) Q 1 (t) Q 2 (0) Q 2 (t) Q 1 (1)=Q 2 (0) Q 1 (1)=Q 2 (0) Q 2 (0) Continuity C 2,C 3,C n Continuity C 2,C 3,C n A curve is C n continuous iff it has continuous nonzero derivatives of order 0, 1,, n in every point Two curves meet C n continuously iff they have continuous nonzero derivatives of order 0, 1,, n in every ypoint Note: polynomial functions are C
Continuity C 2,C 3,,C n Curves continuity (contd.) Example: Q 1 (t)=[sin(π t/2), cos(π t/2)] (0 t 1) Q 2 (t)=[1, ππ t/2] (0 t 1) Are they parametrically continuous? Curves continuity (contd.) Solution: ad 1) Q 1 (1) = [sin(π /2), cos(π /2)] = [1, 0] Q 2 2( (0) = [1, 0] this is OK 2) Q 1 (t)=(π /2 cos(π t/2), π /2sin(π t/2)) Q 2 (t)=(0, π /2) Q 1 (1)=(π /2 cos(π /2), π /2sin(π /2)) = (0, π /2) Curves continuity (contd.) These two segments make curve that is parametrically continuous in the knot that is OK as well
Curves continuity (contd.) Example: Let s have curves: Q(t)=[2t,2t], 2t] (0 t 1) R(t)=[t+2,t+2], (0 t 1) Is the curve formed by them (if any) C 1? Curves continuity (contd.) Solution: 1) Q(1)=[2,2] R(0)=[2,2] 2] 2) Q (t)=(2,2) R (t)=(1,1) ( ) The curves definitively meet smoothly, but they are NOT parametrically continuous. Tangent vector sizes are different! Q (t)=(2,2)r (t)=(1,1) Geometric Continuity G 1 Geometric continuity means visual smoothness. The moving point changes the speed but not the direction. Two curves are geometrically continuous if their derivatives in knot are positively linearly dependent Geometric Continuity G 1 Two segments Q 1 (t) and Q 2 (t) are geometrically continuously connected iff 1) Q1(1)=Q2(0) they meet in the knot 2) Q 1(1)= k Q 2(0), k>0 their derivatives in the knot are linearly l dependentd Does C 1 implies G 1?
Geometric Continuity G 1 Continuity summary Continuity (zero order continuity): C 0 two curves meet in the knot Parametric continuity i (first order continuity): i C 1 the curves meet in knot and their derivatives are equal (the joint is smooth) Geometric continuity: G 1 the curves meet in knot and their derivatives are linearly positively dependent (the joint point is visually smooth) Modeling curves The most important curves in CG are polynomial curves i.e., curves that can be represented as the canonical form of a polygon Q(t) = a 0 + a 1 t + a 2 t 2 + + a n t n = a i t i Q(t) = c 0 b 0 (t)+c 1 b 1 (t)+ +c n b n (t)= c i b i (t) the b(t) are the basis function or the blending functions Basis Function Example: Line segment can be expressed as: Q(t)=b 0 (t) p 0 +b 1 (t)p 1 Where: b 0 (t)=1 t b 1 (t)=t
Modeling curves Why polynomials? they can be derived easily they are C they provide many shapes they can be easily evaluated using Horner s scheme i.e., p(t) = a 0 + t (a 1 + t (a 2 + t (a 3 + +a n ) ) that is using only addition and multiplication! Modeling curves The most important are so called cubics (for 99% applications) Cubics are the lowest degree curves that can describe a S shaped curved (it has the inflection point) that is polynomials of order three that is: P[x(t), y(t), z(t)] = = [x 0 +x 1 t+x 2 t 2 +x 3 t 3,y 0 +y 1 t+y 2 t 2 +y 3 t 3,z 0 +z 1 t+z 2 t 2 +z 3 t 3 ] Modeling curves P[x(t), y(t), z(t)] = = [x 0 +x 1 t+x 2 t 2 +x 3 t 3,y 0 +y 1 t+y 2 t 2 +y 3 t 3,z 0 +z 1 t+z 2 t 2 +z 3 t 3 ] the coefficients define the shape of a segment it is almost impossible to enter them by hand Curves are modeled using so called control points or control vertices Modeling curves Control points can form the curve in two ways 1) approximation (at least one point is not passed through) 2) interpolation (passing through)
Modeling curves Bézier cubic Bézier curves (namely Bézier cubics) are the most popular curves used in CG Used for fonts Pierre Etienne Bézier (Sep 1 1910- Oct 25 1999) One segment of Bézier cubic is given by four control points These points form so called control polygon of the segment Bézier cubic Bézier cubic The control points are denoted by P o, P 1, P 2, P 3 Properties: Convex hull property As any of these points move the entire segment changes its shape
Bézier cubic Bézier cubic Bézier cubic is given by the control points and Q(t)=[x(t),y(t),z(t)]=P 0 B 0 (t)+p 1 B 1 (t)+p 2 B 2 (t)+p 3 B 3 (t)= 3 = Pi B i ( t) parameter t is 0 t 1 i= 0 B 0 (t)= (1 t) 3 B 1 (t) = 3t (1 t) 2 B 2 (t) = 3t 2 (1 t) B 3 (t) = t 3 Bernstein polynomials or Bézier basis functions P i are points, so we have three equations in 3D Bézier cubic Properties of the Bernstein polynomials B 0 + B 1 + B 2 + B 3 = 1 (they sum to unity) (...homework) 0 B i (t) 1; for 0 t 1 (nonzero property) we cannot represent conic sections using Bézier cubics (so we cannot draw a circle!) Bézier cubic Properties of Bézier curves Curve interpolates the first and the last control point i.e., Q(0) = P 0 and Q(1)=P 3 Convex hull property any point of the curve lies inside the convex hull defined by the control points
Bézier cubic Properties of Bézier curves (contd.) Invariance under affine transformations (scale, rotate, translate, shear) 3 i=0 A(Q(t)) = A(P i )B i (t) () in other words: rotating the curve can be done by rotating its control points This is NOT true for projections! Bézier cubic rasterization Bezier(PointT P0,PointT P1, PointT P2, PointT P3, int n){ //P0,,P3 are the control points //n is the # of lines used for approximation double t, deltat; PointT a, b; int i; deltat = 1.0/n;//curve will be approximated by n lines a = Q(0, PO, P1, P2, P3); //the first point of the curve t = 0; for (i=0;i<n;i++){ //for all lines do b = Q(t+deltat, t PO, P1, P2, P3);//endpoint of line Line(a,b); //render line a=b; //end becomes the start now t += deltat; //increase the t variable } } Bézier cubic rasterization PointT Q(double t, PointT P0,PointT P1,PointT P2, PointT P3){ double B0, B1, B2, B3; static PointT hlp; B0=(1-t)*(1-t)*(1-t); B1=3*t*(1-t)*(1-t); t)*(1 t) B2=3*t*t*(1-t); B3=t*t*t; hlp.x = P0.x*B0 0 + P1.x*B1 1 + P2.x*B2 2 + P3.x*B3; 3 hlp.y = P0.y*B0 + P1.y*B1 + P2.y*B2 + P3.y*B3; return hlp; } Connecting Bézier cubic Provides a piecewise Bézier curve the parameter of the entire curve is 0 t n, where n is the number of the segments each segment is defined for 0 t 1 we have to perform reparametrization first t t t ' = t t 2 1 1 this scales the interval to [0,1] for each segment
Bézier cubic derivative Bézier cubic derivative The curve is C Derivative in knots: Q(0) and Q(1) it can be easily shown that: Q (0)=3(P 1 P 0 ) Q (1)=3(P 3 P 2 ) Bézier cubic derivative Let s have two Bézier cubic segments consisting Let s connect them 1) continuously 2) parametrically continuously (smooth) 3) geometrically continuously (visually smooth) Bézier cubic connection C 0 Bézier curves interpolate the first and last control point i.e., Q(0) = P 0 and Q(1)=P 3 last control point of the first segment must be the first control point of the next segment
Bézier cubic connection C 0 P2 P1 Q1 Bézier cubic connection C 1 two segments meet C 1 iff the last two control points of the first segment and the first two points of the second curve form line with the common point in the middle P0 P =Q 3 0 Q3 Q Q2 The tangent vectors are equal, i.e., 3(P 3 P 2 )=3(Q 1 Q 0 ) 3 2 1 0 Bézier cubic connection C 1 Bézier cubic connection G 1 two segments meet G 1 iff last two control points of the first segment and first two points of the second curve form line with common point somewhere between them The tangent vectors are parallel and equally oriented, i.e., (P 3 P 2 )=k(q 1 Q 0 ) k>0
Bézier cubic connection G 1 Connecting Bézier Curves Moving one point affects max two segments We can add/erase just a piece of the curve It is standard in SW de Casteljau algorithm The naïve algorithm for rasterization Bézier curves is not efficient It produces equal number of fline segments for entire segment de Casteljau algorithm de Casteljau algorithm is an adaptive solution providing higher subdivision on parts with high curvature and vice versa
de Casteljau algorithm Recursive formula for Bernstein polynomials Provides one point on the curve de Casteljau algorithm Pi,j (t) = (1 t) Pj 1,i (t) + t Pj,i 1 (t) where i,j=1,2,,n The initial values are control points P0,i=Pi and j coefficient corresponds j the to the level of recursion The last point is thus Pn,n de Casteljau algorithm Geometric interpretation de Casteljau algorithm The most important is: P0, P1,1, P2,2, Q(t) and Q(t), P3,2, P3,1, P3 are control points of a sub segmentssegments
de Casteljau algorithm de Casteljau algorithm The criterion to stop the subdivision may be: 1) Control points are in one pixel 2) Control polygon is smaller than a pixel (antialiasing) 3) Control polygon is lineal 4) Area of the control polygon is small etc. Hermite Cubic Interpolation A segment of Hermite cubic interpolation curve is given by two control points P 0, P 1, and the two tangent vectors P 0, P 1, in them Charles Hermite (Dec 24 1822- Jan 14 1901)) P 0 P 0 P 1 P 1 Hermite Cubic Interpolation given by the equation: Q(t)=P 0 F 0 (t) + P 1 F 1 (t) + P 0 F 2 (t) + P 1 F 3 (t) where: F 0 (t) = 2t 3 3t 2 + 1 F = 2t 3 +3t 2 1 (t) F 2 (t) = t 3 2t 2 + t F 3 (t) = t 3 t 2 setting Q(0) we get P 0 setting Q(1) we get P 1 It is interpolation
Hermite Cubic Interpolation Hermite Cubic Interpolation P'0 P0 P1 P'0 P0 P 1 P'1 P1 Advantage having two segments given by P 0, P 1, P 0, P 1,and Q 0, Q 1, Q 0, Q 1, C 0 simply equality of the points P 1 =Q 0 P'1 P0 P'1 P1 C 1 simply equality of the vectors P 1 =Q 0 G 1 first vector is k times equal to the other P 1 =k*q 0 ; k>0 P'0 Hermite Cubic Interpolation Disadvantage: they are not easy to edit By the way Hermite s cubics are a different representation of Bézier cubics and vice versa Conclusion This was a very gentle intro to curves What is missing: Splines the most important ones NUBS even more important Subdivision curves
Summary Parametric equation of curve Derivative, tangent, tangent vector Continuity Modeling curves (interpolation, approximation) Bézier cubic Hermite cubic Readings Interactive Computer Graphics 5th edition, Ed. Angel pp 597 652 Real Time Rendering 2nd edition, Moller, T.A., Haines, E., 481 555 Geometric Modeling, 2nd edition, i M.E.Mortenson