Bézier Algorithms & Properties Glenn G. Chappell [email protected] U. of Alaska Fairbanks CS...

21
Bézier Algorithms & Properties Glenn G. Chappell [email protected] U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004

Transcript of Bézier Algorithms & Properties Glenn G. Chappell [email protected] U. of Alaska Fairbanks CS...

Page 1: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

Bézier Algorithms & Properties

Glenn G. [email protected]

U. of Alaska Fairbanks

CS 481/681 Lecture NotesWednesday, March 3, 2004

Page 2: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 2

Review:Bézier Curves & Surfaces [1/2] Our first type of spline is the Bézier curve/surface.

Bézier curves & surfaces were developed around 1960, for use in CAD/CAM.

First application: automobile design. PostScript and TrueType both describe characters with Bézier curves.

A Bézier curve is a parametric curve described by polynomials based on control points.

Any number of control points may be used. 3 & 4 are common. Degree of polynomials = number of points – 1. Several Bézier curves can easily be glued together in a way that

makes the curve as a whole smooth. Bézier curves are approximating curves. A Bézier curve passes

through its first and last control points, but, in general, no others. A Bézier surface patch can be formed by starting with a grid of

control points, drawing curves in one direction, and then in the perpendicular direction, using points on the already drawn curves as control points.

Page 3: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 3

Review:Bézier Curves & Surfaces [2/2] A Bézier curve can be defined using repeated lirping. Example: 3 control points (P0, P1, P2).

f(0) = P0 (first control pt.); f(1) = P2 (last control pt.). To find other function values (for a given value of t):

• Lirp between P0 & P1.

• Lirp between P1 & P2 (same t).

• Lirp between above two values (same t) to get point on curve.

P0

P1

P2

P0

P1

P2

Page 4: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 4

Review:OpenGL Evaluators OpenGL evaluators compute Bézier curves & surfaces.

When using an evaluator, you specify control points. Set the usual graphics state options any way you like. OpenGL does the glVertex* calls for you.

Once an evaluator is initialized and enabled, you can: Use the evaluator to compute a single point on the curve/surface

(and, typically, do a glVertex* with it).• Use glEvalCoord*.

Use the evaluator to draw the whole curve/surface.• Set up an evaluator grid with glMapGrid*.• Draw with glEvalMesh*.

When drawing Bézier surfaces, OpenGL can compute vertex normals for you.

Enable “GL_AUTO_NORMAL”. Evaluators can also be used to set the color, as well as other

vertex-related states.

Page 5: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 5

Bézier Algorithms:Overview

Now we discuss how Bézier curves are drawn.

We will cover two algorithms: The de Casteljau Algorithm

• Uses the repeated-lirping description of the curve.

Using Bernstein Polynomials• Compute the polynomials that describe the

curve.• This is generally how OpenGL evaluators

are implemented.

Page 6: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 6

Bézier Algorithms:De Casteljau Alg. [1/4] Recall that a Bézier curve can be described in terms of repeated

linear interpolation. Example: 4 control points (P0, P1, P2, P3).

f(0) = P0 (first control pt.); f(1) = P3 (last control pt.). To find other function values (for a given value of t):

• Lirp between P0 & P1, P1 & P2, and P2 & P3.

• Lirp between 1st & 2nd point above and between 2nd & 3rd.• Lirp between the above two values to get the point on the curve.

P0 P1

P2

P3

Page 7: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 7

Bézier Algorithms: De Casteljau Alg. [2/4] The de Casteljau Algorithm computes a point on a Bézier curve

using this repeated lirping procedure.

Each lirp is done using the same value of t. x, y, and z are computed separately.

lirp lirp lirp lirp

lirp lirp lirp

lirp lirp

lirp

P0 P1 P2 P3 P4

Point on curve

Page 8: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 8

Bézier Algorithms: De Casteljau Alg. [3/4] To draw a curve using the de Casteljau

Algorithm: Set up the control points in an array as usual. Have a loop to draw the curve.

• This should probably be inside a glBegin/glEnd.• The parameter (t) should go from 0 to 1.

• The step size will depend on the desired output quality.• It is generally best to have an integer loop counter, then, in

each iteration, find t by multiplying.• For each value of the parameter, do the repeated lirping

procedure to calculate the point on the curve. If the curve does not change, pre-computing points on

the curve is a good idea.• Use a display list, maybe?

Next: Sample code to do the repeated lirping.

Page 9: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 9

Bézier Algorithms: De Casteljau Alg. [4/4] Set-Up for Sample Code

Number of control points: const int ncp. Array: double v[ncp][ncp][3];

• Last index above indicates x, y or z. (Make the “3” a “2” for 2-D.)• Control points given in v[0][0…ncp-1][0…2].

Parameter: double t. Already written: double lirp(double t, double a, double b).

Code

for (int c=0; c<3; ++c) // Calc x, y, z independently

for (int i=1; i<ncp; ++i)

for (int k=0; k<ncp-i; ++k)

v[i][k][c] = lirp(t, v[i-1][k][c], v[i-1][k+1][c]);

Point on curve is now in v[ncp-1][0][0…2].

Page 10: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 10

Bézier Algorithms:Bernstein Polynomials [1/3] Another way to draw a Bézier curve is to use the

polynomial that describes it to compute coordinates of points. If there are n control points (P0, P1, …, Pn–1), then a

Bézier curve is parameterized by a polynomial of degree n–1.

For two control points (P0, P1), the polynomial describing the Bézier Curve is P0·(1–t) + P1·t.

• This is just the lirping formula (right?).• This is really two polynomials: one for x, and one for y.

For three control points (P0, P1 , P2), the polynomial isP0·(1–t)2 + P1·2(1–t)t + P2·t2.

For four control points the polynomial isP0·(1–t)3 + P1·3(1–t)2t + P2·3(1–t)t2 + P3·t3.

Page 11: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 11

Bézier Algorithms:Bernstein Polynomials [2/3] Again, for four control points the polynomial is

P0·(1–t)3 + P1·3(1–t)2t + P2·3(1–t)t2 + P3·t3. This is made up of pieces called Bernstein

polynomials. B3

0(t) = (1–t)3. B3

1(t) = 3(1–t)2t. B3

2(t) = 3(1–t)t2. B3

3(t) = t3. The pattern:

Descending powers of (1–t). Ascending powers of t. Coefficients are binomial coefficients.

• Found in Pascal’s Triangle.

Page 12: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 12

Bézier Algorithms:Bernstein Polynomials [3/3] Using a polynomial to draw a Bézier curve in a program:

Have a loop in which t starts at 0 and goes up to 1, as usual. For each value of t, find the point on the curve:

• Compute (1-t), since you probably need to use it several times.• Compute the x and y polynomial functions, using t and (1-t).

Pro’s & Con’s of Bernstein Polynomials vs. de C’s Alg. In practice, using the Bernstein Polynomials is faster

than the de Casteljau Algorithm. But it is tricky to generalize the Bernstein-polynomial

method to an arbitrary number of control points.• The code we looked at for the de Casteljau Algorithm works for

any number of control points.• However, that code slows down rapidly as the number of control

points becomes large.

Page 13: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 13

Bézier Algorithms:Surfaces [1/2] The way we described Bézier surfaces last time suggests how to

extend curve-drawing algorithms to surfaces. Say we have a grid of 9 control points, as shown. The horizontal curves have the following formulas:

P0,0·(1–t)2 + P0,1·2(1–t)t + P0,2·t2

P1,0·(1–t)2 + P1,1·2(1–t)t + P1,2·t2

P2,0·(1–t)2 + P2,1·2(1–t)t + P2,2·t2

Now we use points on these curves as control points. Let our vertical parameter be s. We obtain the following formula for a Bézier patch:

[ P0,0·(1–t)2 + P0,1·2(1–t)t + P0,2·t2 ]·(1–s)2 +[ P1,0·(1–t)2 + P1,1·2(1–t)t + P1,2·t2 ]·2(1–s)s +

[ P2,0·(1–t)2 + P2,1·2(1–t)t + P2,2·t2 ]·s2

P0,0 P0,1 P0,2

P1,0 P1,1 P1,2

P2,0 P2,1 P2,2

Page 14: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 14

Bézier Algorithms:Surfaces [2/2]

Given the formulas on the previous slide, we can draw a Bézier patch using two for-loops. Probably using GL_TRIANGLE_STRIP. So each row has to be given twice (right?).

A similar generalization applies to the de Casteljau Algorithm. However, that algorithm is not terribly

efficient.

Page 15: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 15

Bézier Properties:Overview

We will now look at some properties of Bézier curves. Generally “Good” Properties

• Endpoint Interpolation• Smooth Joining• Affine Invariance• Convex-Hull Property

Generally “Bad” Properties • Not Interpolating• No Local Control

Page 16: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 16

Bézier Properties: Endpoint Interpolation Bézier curve generally do not pass through all control points. However, they do pass through the first & last control points.

So, to make two Bézier curves join, make the first control point of one equal to the last control point of the other.

This may not result in a smooth curve overall.

P1

P0 P2

P1

P0Q0

Q1

P2 Q2

Page 17: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 17

Bézier Properties: Easy Smooth Joining At its start, the velocity vector (first derivative) of a Bézier curve

points from the first control point, towards the second. At its end, the velocity vector points from the last control point

away from the next-to-last.

To make two Bézier curves join smoothly put the last two control points of one and the first two of the other in a line, as shown:

P1

P0 P2

P1

P0 Q0

Q1

P2 Q2

Page 18: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 18

Bézier Properties: Affine Invariance An affine transformation is a transformation that can be

produced by doing a linear transformation followed by a translation.

All of the transformations we have dealt with, except for perspective projection, are affine. These include: Translation. Rotation. Scaling. Shearing. Reflection.

Bézier curves have the property that applying an affine transformation to each control points results in the same transformation being applied to every point on the curve. For example, to rotate a Bézier curve, apply a rotation to the

control points. In short: Transformations act the way you want them to.

Page 19: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 19

The convex hull of a set of points is the smallest convex region containing them.

Informally speaking, “lasso” (or shrink-wrap) the points; the region inside the lasso is the convex hull.

A Bézier curve lies entirely in the convex hull of its control points. This property makes it easy to specify where a curve will not go. Smooth interpolating splines never have this property.

Bézier Properties: Convex-Hull Property

P1

P0 P2

P1

P0 P2

Points Convex hull

P1

P0 P2

Page 20: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 20

Bézier Properties: Not-So-Good Stuff Again Bézier curves do not interpolate all their

control points. So we can easily specify where it does not go, but not

where it does go. Bézier curves also do not have “local control”.

A curve has local control if moving a single control point only changes a small part of the curve (the part near the control point).

Moving any control point on a Bézier curve changes the whole curve.

This is the main reason we do not use Bézier curves with a large number of control points.

• Instead, we piece together several 3- or 4-point Bézier curves in a smooth way.

• This multiple-Bézier curve does have local control.

Page 21: Bézier Algorithms & Properties Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, March 3, 2004.

3 Mar 2004 CS 481/681 21

Building Better Curves:How to Improve on Bézier? The biggest problems with Bézier curves are:

Lack of local control. High degree, if there are a large number of control points. Lack of interpolation (sometimes a problem, sometimes not).

Better curve #1: B-spline. Parametric curve described by polynomials based on control points. Has affine invariance & local control. Has same degree regardless of number of control points. Approximating, has convex-hull property.

Better curve #2: Catmull-Rom spline. Parametric curve described by polynomials based on control points. Has affine invariance & local control. Has same degree (3) regardless of number of control points. Interpolating, no convex-hull property, (and a bit tough to get “just right”).

Better curve #3: NURBS (Non-Uniform Rational B-Spline). Parametric curve described by rational functions based on control points. Has affine invariance & local control. Very general & adaptable, but thus somewhat tougher to use. Built into GLU.