13_beziercurves (2)

Upload: ayu7kaji

Post on 03-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 13_BezierCurves (2)

    1/58

    CSE 167:

    Introduction to Computer GraphicsLecture 13: Bzier Curves

    Jrgen P. Schulze, Ph.D.University of California, San Diego

    Fall Quarter 2011

  • 7/28/2019 13_BezierCurves (2)

    2/58

    Announcements Homework project #6 due Friday, Nov 18 at 1:30pm

    To be presented in lab 260

    No grading this Friday, Nov 11 (Veterans Day) Last day for late grading project 5: Thursday, Nov 10

    Final project presentations date+time confirmed:

    December 2nd, 1-3pm, CSE 1202

    2

  • 7/28/2019 13_BezierCurves (2)

    3/58

    Lecture Overview Polynomial Curves contd

    Bzier Curves

    Introduction

    Drawing Bzier curves

    Piecewise Bzier curves

    3

  • 7/28/2019 13_BezierCurves (2)

    4/58

    Polynomial Curves Quadratic:

    (2nd order)

    Cubic:(3rd order)

    We usually define the curve for 0 t 1

    4

  • 7/28/2019 13_BezierCurves (2)

    5/58

    Control Points Polynomial coefficients a, b, c, d can be interpreted as

    control points

    Remember: a, b, c, d havex,y,z components each

    Unfortunately, they do not intuitively describe the shape of

    the curve

    Goal: intuitive control points

    5

  • 7/28/2019 13_BezierCurves (2)

    6/58

    Control Points How many control points?

    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

    Lets start with a line

    6

  • 7/28/2019 13_BezierCurves (2)

    7/58

    First Order Curve Based on linear interpolation (LERP)

    Weighted average between two values

    Value could be a number, vector, color, Interpolate between points p0 and p1 with parameter t

    Defines a curve that is straight (first-order spline)

    t= corresponds to p0 t=1 corresponds to p1 t=0.5 corresponds to midpoint

    p0

    p1

    t=1

    .

    . 0

  • 7/28/2019 13_BezierCurves (2)

    8/58

    Linear Interpolation Three equivalent ways to write it

    Expose different properties

    1. Regroup for points p weighted sum of controlpoints

    2. Regroup for t polynomial in t

    3.

    Matrix form

    8

  • 7/28/2019 13_BezierCurves (2)

    9/58

    Weighted Average

    Weights are a function oft

    Sum is always 1, for any value oft

    Also known as blendin unctions

    x(t) = (1 t)p0 + (t)p1

    = B0 (t) p0 + B1(t)p1, whereB0 (t) = 1 tandB1(t) = t

    9

  • 7/28/2019 13_BezierCurves (2)

    10/58

    Curve is based at point p0

    x(t) = (p1 p0 )

    vector124 34

    t+ p0

    point123

    Linear Polynomial

    ,

    .p0.

    p1-p0

    .5(p1-p0)

    .

    10

  • 7/28/2019 13_BezierCurves (2)

    11/58

    Geometry matrix

    Matrix Form

    Polynomial basis

    In components

    11

  • 7/28/2019 13_BezierCurves (2)

    12/58

    Tangent For a straight line, the tangent is constant

    Weighted average

    Polynomial

    Matrix form

    12

  • 7/28/2019 13_BezierCurves (2)

    13/58

    Lecture Overview Polynomial Curves contd

    Bzier Curves

    Introduction

    Drawing Bzier curves

    Piecewise Bzier curves

    13

  • 7/28/2019 13_BezierCurves (2)

    14/58

    Bzier Curves Are a higher order extension of linear interpolation

    p1p2

    p1

    p0 p0p0

    p2

    p3

    Linear Quadratic Cubic

    14

  • 7/28/2019 13_BezierCurves (2)

    15/58

    Bzier Curves Give intuitive control over curve with control points

    Endpoints are interpolated, intermediate points areapproximated

    Convex Hull property

    Variation-Diminishing property

    Man demo a lets online for exam le:

    Demo: http://www.cs.princeton.edu/~min/cs426/jar/bezier.html http://www.theparticle.com/applets/nyu/BezierApplet/

    http://www.sunsite.ubc.ca/LivingMathematics/V001N01/UBCExamples/Bezier/bezier.html

    15

  • 7/28/2019 13_BezierCurves (2)

    16/58

    Cubic Bzier Curve Most common case

    Defined by four control points: Two interpolated endpoints (points are on the curve)

    Two points control the tangents at the endpoints

    Points x on curve defined as function of parameter t

    16

    p0p2

    p3

    x(t)

  • 7/28/2019 13_BezierCurves (2)

    17/58

    Algorithmic Construction Algorithmic construction

    De Casteljau algorithm, developed at Citroen in 1959,

    named after its inventor Paul de Casteljau (pronouncedCast-all-Joe)

    Developed independently from Bziers work:

    z er crea e e ormu a on us ng en ng unc ons,

    Casteljau devised the recursive interpolation algorithm

    17

  • 7/28/2019 13_BezierCurves (2)

    18/58

    De Casteljau Algorithm A recursive series of linear interpolations

    Works for any order Bezier function, not only cubic

    Not very efficient to evaluate Other forms more commonly used

    But:

    Gives intuition about the geometry Useful for subdivision

    18

  • 7/28/2019 13_BezierCurves (2)

    19/58

    De Casteljau Algorithm

    p0

    p1 Given:

    Four control points

    A value oft(here t0.25)

    p3

    19

  • 7/28/2019 13_BezierCurves (2)

    20/58

    De Casteljau Algorithm

    p0

    q0

    p1q1

    q0 (t) = Lerp t,p0 , p1( )

    p3

    q2

    q1(t) = Le p t,p1,p2

    q2 (t) = Lerp t,p2 , p3( )

    20

  • 7/28/2019 13_BezierCurves (2)

    21/58

    De Casteljau Algorithm

    q0

    q1

    r1

    r0

    q2

    0 , 0 , 1

    r1(t) = Lerp t,q1(t),q2 (t)( )

    21

  • 7/28/2019 13_BezierCurves (2)

    22/58

    De Casteljau Algorithm

    r1x

    r0

    x(t) = Lerp t,r0 (t),r1(t)( )

    22

  • 7/28/2019 13_BezierCurves (2)

    23/58

    x

    p0

    p1

    p2

    De Casteljau Algorithm

    p3 Applets Demo: http://www2.mat.dtu.dk/people/J.Gravesen/cagd/decast.html

    http://www.caffeineowl.com/graphics/2d/vectorial/bezierintro.html

    23

  • 7/28/2019 13_BezierCurves (2)

    24/58

    x = Lerp t,r0 ,r1( )r0 = Lerp t,q0 ,q1( )

    r1 = Lerp t,q1,q2( )

    q0 = Lerp t,p0 ,p1( )

    q1 = Lerp t,p1,p2( )q2 = Lerp t,p2 ,p3( )

    p0

    p1

    p2

    Recursive Linear Interpolation

    p1

    q0

    r0 p2

    x q1

    r1 p3

    q2

    p4

    24

  • 7/28/2019 13_BezierCurves (2)

    25/58

    Expand the LERPsq0 (t) = Lerp t,p0 ,p1( )= 1 t( )p0 + tp1q1(t) = Lerp t,p1,p2( )= 1 t( )p1 + tp2q2 (t) = Lerp t,p2 ,p3( )= 1 t( )p2 + tp3

    0 , 0 , 1 0 1 1 2

    r1(t) = Lerp t,q1(t),q2 (t)( )= 1 t( ) 1 t( )p1 + tp2( )+ t 1 t( )p2 + tp3( )

    x(t) = Lerp t,r0

    (t),r1

    (t)( )

    = 1 t( ) 1 t( ) 1 t( )p0 + tp1( )+ t 1 t( )p1 + tp2( )( )

    +t 1 t( ) 1 t( )p1 + tp2( )+ t 1 t( )p2 + tp3( )( )25

  • 7/28/2019 13_BezierCurves (2)

    26/58

    x(t) = 1 t( ) 1 t( ) 1 t( )p0 + tp1( )+ t 1 t( )p1 + tp2( )( )

    +t 1 t( ) 1 t( )p1 + tp2( )+ t 1 t( )p2 + tp3( )( )

    Weighted Average of Control Points Regroup for p:

    x t = t p0

    + t tp1

    + t t p2

    + t p3

    x(t) = t3

    + 3t2

    3t+ 1( )

    B0 (t)6 7444 8444

    p0 + 3t3

    6t2

    + 3t( )

    B1 (t)6 744 844

    p1

    + 3t3 + 3t2( )B2 (t)

    1 24 34p2 + t

    3( )B3 (t)

    {p3

    26

  • 7/28/2019 13_BezierCurves (2)

    27/58

  • 7/28/2019 13_BezierCurves (2)

    28/58

    General Bernstein PolynomialsB0

    1t( ) = t+ 1 B0

    2t( ) = t2 2t+ 1 B0

    3t( ) = t3 + 3t2 3t+ 1

    B11

    t( ) = t B12

    t( ) = 2t2 + 2t B13

    t( ) = 3t3 6t2 + 3t

    B22

    t( ) = t2

    B23

    t( ) = 3t3

    + 3t2

    B33

    t( ) = t3

    Bin t( ) =n

    i

    1 t( )n i

    t( )i n

    i

    =n!

    i! n i( )!

    Bin

    t( ) = 1 n! = factorial of n(n+1)! = n! x (n+1)

    28

  • 7/28/2019 13_BezierCurves (2)

    29/58

  • 7/28/2019 13_BezierCurves (2)

    30/58

    Bzier Curve PropertiesOverview:

    Convex Hull property

    Variation Diminishing property

    Affine Invariance

    30

  • 7/28/2019 13_BezierCurves (2)

    31/58

    Definitions Convex hull of a set of points:

    Polyhedral volume created such that all lines connecting any

    two points lie completely inside it (or on its boundary) Convex combination of a set of points:

    Weighted average of the points, where all weights between 0

    an , sum up to

    Any convex combination always of a set of points lies

    within the convex hull

    31

  • 7/28/2019 13_BezierCurves (2)

    32/58

    Convex Hull Property A Bzier curve is a convex combination of the control points

    (by definition, see Bernstein polynomials)

    Bzier curve is always inside the convex hull Makes curve predictable

    Allows culling, intersection testing, adaptive tessellation

    ~

    p0

    p1

    p2

    p3

    . . . .

    32

  • 7/28/2019 13_BezierCurves (2)

    33/58

    Variation Diminishing Property If the curve is in a plane, this means no straight line

    intersects a Bzier curve more times than it intersects

    the curve's control polyline Curve is not more wiggly than control polyline

    33

  • 7/28/2019 13_BezierCurves (2)

    34/58

    Affine InvarianceTransforming Bzier curves

    Two ways to transform:

    Transform the control points, then compute resulting splinepoints

    Compute spline points then transform them

    Either way, we get the same points Curve is defined via affine combination of points

    Invariant under affine transformations

    Convex hull property remains true

    34

  • 7/28/2019 13_BezierCurves (2)

    35/58

    Start with Bernstein form:

    x(t) = t3 + 3t2 3t+ 1( )p0 + 3t3

    6t2 + 3t( )p1 + 3t3

    + 3t2( )p2 + t3( )p3

    Regroup into coefficients oft:

    x(t) = p0 + 3p1 3p2 + p3( )t3

    + 3p0 6p1 + 3p2( )t2

    + 3p0 + 3p1( )t+ p0( )1

    Cubic Polynomial Form

    Good for fast evaluation Precompute constant coefficients (a,b,c,d)

    Not much geometric intuition

    x(t) = at3

    + bt2

    + ct+ d

    0 1 2 3

    b = 3p0 6p1 + 3p2( )c = 3p0 + 3p1( )

    d = p0( )

    35

  • 7/28/2019 13_BezierCurves (2)

    36/58

    x(t) =rarbrc d

    t3

    t2

    t1

    ra = p0 + 3p1 3p2 + p3( )rb = 3p0 6p1 + 3p2( )r

    c = 3p0 + 3p1( )d = p0( )

    Cubic Matrix Form

    Other types of cubic splines use different basis matrices BBez

    x(t) = p0 p1 p2 p3[ ]

    GBez

    1 2444 3444

    1 3 3 1

    3 6 3 0

    3 3 0 0

    1 0 0 0

    BBez

    1 2444 3444

    t

    t2

    t

    1

    T

    {

    36

  • 7/28/2019 13_BezierCurves (2)

    37/58

    Cubic Matrix Form

    xx (t) = p0x p1x p2x p3x[ ]

    1 3 3 1

    3 6 3 0

    3 3 0 0

    1 0 0 0

    t3

    t2

    t

    1

    In 3D: 3 parallel equations for x, y and z:

    xy (t) = p0y p1y p2y p3y

    1 3

    3 1

    3 6 3 0

    3 3 0 0

    1 0 0 0

    t

    3

    t2

    t

    1

    xz (t) = p0z p1z p2z p3z

    1 3 3 1

    3 6 3 0

    3 3 0 0

    1 0 0 0

    t3

    t2

    t

    1

    37

  • 7/28/2019 13_BezierCurves (2)

    38/58

    Bundle into a single matrix

    x(t) =

    p0x p1x p2x p3x

    p0y p1y p2y p3y

    p0z p1z p2z p3z

    1 3 3 1

    3 6 3 0

    3 3 0 0

    1 0 0 0

    t3

    t2

    t

    1

    Matrix Form

    Efficient evaluation Pre-compute C

    Take advantage of existing 4x4 matrix hardware support

    x(t) = GBezBBezTx(t) = CT

    38

  • 7/28/2019 13_BezierCurves (2)

    39/58

    Lecture Overview Polynomial Curves contd

    Bzier Curves

    Introduction Drawing Bzier curves

    Piecewise Bzier curves

    39

  • 7/28/2019 13_BezierCurves (2)

    40/58

    Drawing Bzier Curves Draw line segments or individual pixels

    Approximate the curve as a series of line segments

    (tessellation) Uniform sampling

    Adaptive sampling

    Recursive subdivision

    40

  • 7/28/2019 13_BezierCurves (2)

    41/58

  • 7/28/2019 13_BezierCurves (2)

    42/58

    Adaptive Sampling Use only as many line segments as you need

    Fewer segments where curve is mostly flat

    More segments where curve bends Segments never smaller than a pixel

    x(t)

    42

  • 7/28/2019 13_BezierCurves (2)

    43/58

  • 7/28/2019 13_BezierCurves (2)

    44/58

    De Casteljau Subdivision

    xp0

    p1

    p2

    q0r0

    r1

    De Casteljau construction pointsare the control points of two Bziersub-segments

    p3

    q2

    44

  • 7/28/2019 13_BezierCurves (2)

    45/58

    Adaptive Subdivision Algorithm Use De Casteljau construction to split Bzier segment

    For each half

    If flat enough: draw line segment Else: recurse

    Curve is flat enough if hull is flat enough

    Test how far the approximating control points are from astraight segment

    If less than one pixel, the hull is flat enough

    45

  • 7/28/2019 13_BezierCurves (2)

    46/58

    Drawing Bzier Curves With OpenGL Indirect OpenGL support for drawing curves:

    Define evaluator map (glMap)

    Draw line strip by evaluating map (glEvalCoord) Optimize by pre-computing coordinate grid (glMapGrid and

    glEvalMesh)

    ore etai s a out pen imp ementation:

    http://www.cs.duke.edu/courses/fall09/cps124/notes/12_curves

    /opengl_nurbs.pdf

    46

  • 7/28/2019 13_BezierCurves (2)

    47/58

    Lecture Overview Polynomial Curves contd

    Bzier Curves

    Introduction Drawing Bzier curves

    Piecewise Bzier curves

    47

  • 7/28/2019 13_BezierCurves (2)

    48/58

    More Control Points Cubic Bzier curve limited to 4 control points

    Cubic curve can only have one inflection (point where curve changesdirection of bending)

    Need more control points for more complex curves k-1 order Bzier curve with kcontrol points

    Hard to control and hard to work with

    Intermediate points dont have obvious effect on shape Changing any control point changes the whole curve

    Want local support: each control point only influences nearby portion ofcurve

    48

  • 7/28/2019 13_BezierCurves (2)

    49/58

    Piecewise Curves Sequence of simple (low-order) curves, end-to-end Known as a piecewise polynomial curve

    Sequence of line segments

    Piecewise linearcurve

    Sequence of cubic curve segments Piecewise cubiccurve (here piecewise Bzier)

    49

  • 7/28/2019 13_BezierCurves (2)

    50/58

    Mathematical Continuity C0 continuity: Curve segments are connected

    C1 continuity: C0 & 1st-order derivatives agree at joints

    Tangents/normals are C0 continuous Important for smooth shading

    C2 continuity: C & 2nd-order derivatives a ree at oints

    Tangents/normals are C1 continuous

    Important for high quality reflections

  • 7/28/2019 13_BezierCurves (2)

    51/58

    Global Parameterization Given N curve segments x0(t), x1(t), , xN-1(t) Each is parameterized for tfrom 0 to 1 Define a piecewise curve

    Global parameter u from 0 to N

    x0 (u), 0 u 1

    Alternate: solution u also goes from 0 to 1

    x(u) =1 ,

    M MxN1(u N 1( )), N 1 u N

    x(u) = xi (u i), where i = u (and x(N) = xN1(1))

    x(u) = xi (Nu i), where i = Nu

    51

  • 7/28/2019 13_BezierCurves (2)

    52/58

    Given N+1 points p0, p1, , pN Define curve

    Piecewise-Linear Curve

    x(u) = Lerp(u i,pi ,pi +1), i u i + 1

    = (1 u + i)pi + (u i)p i +1, i = u

    p1 p3x(1.5) x(2.9)

    N+1 points define N linear segments x(i)=pi C0 continuous by construction C1 at pi when pi-pi-1 = pi+1-pi

    p0

    p2p5

    x(5.25)

    52

  • 7/28/2019 13_BezierCurves (2)

    53/58

    Piecewise Bzier curve Given 3N+ 1 points p0 ,p1,K,p3N Define N Bzier segments:

    x0 (t) = B0 (t)p0 + B1(t)p1 + B2 (t)p2 + B3(t)p3

    x1(t) = B0 (t)p3 + B1(t)p4 + B2 (t)p5 + B3(t)p6

    xN1(t) = B0 (t)p3N 3 + B1(t)p3N2 + B2 (t)p3N1 + B3(t)p3N

    x0(t)

    x1(t)

    x2(t)

    x3(t)

    p0

    p1p2

    p3

    p4p5

    p6

    p7 p8

    p9

    p10 p11

    p12

    53

  • 7/28/2019 13_BezierCurves (2)

    54/58

    Piecewise Bzier Curve

    x(u) =

    x0 (1

    3u), 0 u 3

    x1

    (1

    3

    u 1), 3 u 6

    M M

    xN1(1

    3u (N 1)), 3N 3 u 3N

    Parameter in 0

  • 7/28/2019 13_BezierCurves (2)

    55/58

    3N+1 points defineNBzier segments

    x(3i)=p3i C0 continuous by construction

    C1 continuous at p3i when p3i - p3i-1 = p3i+1 - p3i C2 is harder to achieve

    Piecewise Bzier Curve

    p0

    p1

    p2

    P3p6

    p5

    p4

    C1 continuous

    p0

    P3

    p2

    p1

    p4

    p5

    p6

    C1 discontinuous

    55

  • 7/28/2019 13_BezierCurves (2)

    56/58

    Piecewise Bzier Curves

    Used often in 2D drawing programs

    Inconveniences Must have 4 or 7 or 10 or 13 or (1 plus a multiple of 3)

    control points

    Some points interpolate, others approximate

    Need to im ose constraints on control oints to obtain C1

    continuity

    C2 continuity more difficult

    Solutions User interface using Bzier handles

    Generalization to B-splines or NURBS

    56

  • 7/28/2019 13_BezierCurves (2)

    57/58

    Bzier Handles

    Segment end points

    (interpolating)

    presented as curvecontrol points

    Midpoints

    a roximatin

    points) presented as

    handles

    Can have option to

    enforce C1 continuity

    Adobe Illustrator

    57

  • 7/28/2019 13_BezierCurves (2)

    58/58