You are on page 1of 86

1

Bezier Curves and Surfaces

© Jeff Parker, Nov 2011


Outline
We often wish to generate curves and surfaces
To create objects
To create movement
e.g. Genesis Project
To provide transitions
We will look at a number of ways to generate curves
We will focus on various parametric cubics
Review
We often wish to generate curves and surfaces
There are a number of (conflicting) goals
Interpolate (include) a set of arbitrary points
Easy and efficient to compute
Smooth
Local Control
Easy to deal with
We may need to compute derivatives
It is hard to get all of these
We will discuss ways to achieve some of these
History
The Bezier curves and surfaces were developed twice:
Pierre Bézier, at Renault
Paul de Casteljau, at Citroën
Used to bend sheet metal or wood to achieve a smooth shape

4
Background: Interpolation
Given a set of n points (xi, yi) we may be able to define a
polynomial that goes through each point.
Solution: Lagrange Polynomial
Assumes no two points with the same x but different y
Thus cannot draw circle

Interpolate set of arbitrary points


Easy and efficient to compute
Smooth
Local Control
Runge Phenomena
Try to approximate the red curve with polynomials
Interpolate (hit curve) at evenly spaced x values

6
Runge Phenomena
Interpolate at evenly spaced points
Difficult to get local control with high order polynomials

Interpolate set of arbitrary points


Easy and efficient to compute
Smooth
Local Control
Runge Phenomena
To avoid this
We don't always interpolate
We break curve into small polynomial pieces

Interpolate set of arbitrary points


Easy and efficient to compute
Smooth
Local Control
Terminology
Interpolation vs. Approximation

9
Functions

Three main ways to define a curve


Function: y = x3 + 3x
A function must be single valued
Cannot draw vertical line or circles with function
Implicit function: x2 + y2 + xy= 25
In general case, can't find all points that satisfy
Parametric Function: (x, y) = (5 cos(t), 5 sin(t))
What we will use tonight
Parametric Functions

We will be exploring Parametric Curves


Parametric Surfaces are similar

Natural parameterization
Moves with curve length
Sample Surface

Parametric map the unit square to the sphere


Linear Bezier Curve
We wrote line segments as weighted sum of endpoints
This is the convex sum of the two end points
The curves below are defined on [0..1], sum to 1

Blending Functions
Linear Bezier Curve
In the expression, the terms p0 and p1 are vectors, and
their weighted sum is a vector
This is a powerful technique, and we will use it all night

Blending Functions
Piece these together
Piece together a curve that connects a set of points

p0
p2

p1
p3

p4
Piece these together
Piece together a curve that connects a set of points

p0
p2

p1
p3

p4
Piece these together
Create series of equations p2
p0

p1
p0 p3

p1 p4
p2

p3

p4

Blending Functions
Piece these together
Create series of equations p2
p0

p1
p0 p3

p1 p4
p2
Interpolate set of arbitrary points
p3
Easy and efficient to compute
p4
Smooth
Blending Functions
Local Control
Piece these together
Create series of equations p2
p0

p1
p0 p3

p1 p4
p2
T Interpolate set of arbitrary points
p3
T Easy and efficient to compute
p4
F Smooth – Nope - sharp "corners"
Blending Functions
T Local Control
Piece these together
Create series of equations p2
p0

p1
p0 p3

p1 p4
p2

p3 What do we want from the blending functions?


p4 Active Blending Functions should sum to 1
Blending Functions
Nice to have all non-negative terms
Terminology

Not continuous at break


Not differentiable at corner

Next: C(1) vs G(1)


Geometric Continuity
Cubics
We will focus on Cubic curves
That is, curves with cubic blending functions

Reasons
These allow C(1) and even C(2) continuity
Allow minimum-curvature interpolants to a set of points
Can define position and curvature at two ends
Cubic Polynomials are in the sweet spot:
Smooth, and easy to compute
Implemented with 4x4 matrices, just like rest of OpenGL

23
Bezier Animation

http://zonalandeducation.com/mmts/curveFitting/
bezierCurves/bca2/Bezier1Animation2.html

24
Note rate of movement

25
Bezier Curve
We will look at Bezier Curves in some detail tonight
There are different ways to think about them:
All paths lead to the same curves
De Castlejau Algorithm (recursive subdivision)
Bernstein Polynomials
Matrix Form
Cubic equations
All have their uses, and you will see all in practice

26
De Casteljau Algorithm
Compute points on the curve without evaluating polynomials
Can use the midpoints, so only need to shift right

27
De Casteljau's Method
Compute points on the curve without evaluating polynomials
Can use the midpoints, so only need to shift right

28
De Casteljau's Method
Gives simple recursive method to draw curve

Keep dividing until each subpiece of the curve is almost straight

29
Recursive Drawing Algorithm

We take original four control points


Recursively subdivide, just using adds and shifts
Get two Beziers, with 7 control points
Curve lies inside the convex hull of control points
Continue until the convex hull is one pixel wide
At each step, subcurves
are shorter
are much less wide
Can make this formal
30
Terminology
The Bezier interpolates the first and fourth control point
That is, the curve passes through them
The middle two control points draw the curve towards them

31
De Casteljau

Let's spell out what this means


Notation differs from figure
Expand one level

Derivation is similar
Find third level
Bezier Curve
There are different ways to think about them:
De Castlejau Algorithm (recursive subdivision)
Bernstein Polynomials
Matrix Form
Cubic equations
All have their uses, and you will see all in practice

35
Bernstein Polynomials
We write a Bezier as the weighted sum of control points
The control points are multiplied by Bernstein polynomials
Convex sum of polynomials with values in [0..1] that sum to 1
Each polynomial pulls curve towards it's control point

36
What does it mean?
We use these blending functions to decide the amount of
contribution from each control point.
Note that the terms add up to one
Why Polynomials?
Easy to evaluate
Continuous and differentiable everywhere
Must worry about continuity at join points including
continuity of derivatives

p(u)

q(u)

join point p(1) = q(0)


but p’(1)  q’(0)
38 E. Angel and D. Shreiner: Interactive Computer
Graphics 6E © Addison-Wesley 2012
Bezier Polynomials sum to one

So each point on the curve is a convex sum of the control points

Thus the curve lies inside the convex hull of the control points
Convex Hull

Check that the curve remains inside the convex hull


of the control points in our examples
Also obvious from De Casteljau's Algorithm

40
Bezier Curve
All paths lead to the same point
De Castlejau Algorithm (recursive subdivision)
Bernstein Polynomials
Matrix Form
Cubic equations
All have their uses, and you will see all in practice

41
How did we pick blending functions?
What do we want from blending functions?
Active Blending Functions
Nice to have all non-negative terms
Functions should be smooth (rather than the pyramids)
Where did we get these blending functions?
Review: Blending function for line
We know p0 and p1. We want vectors a0 and a1 so that
Blending function for the line
We could solve this by hand: a0 = p0, etc.
But let's evolve a general method

Both p and a are vectors


We know p, wish to find a
To solve for p, can invert the Constraint Matrix, C
to find the Blending Matrix, B
Blending function for the line
We know p, wish to find terms for a for blending
Multiply on left by B and vector [1 t]

Blending Functions
Blending functions for Bezier
Know p0, p1, p2, and p3, want vectors a0, a1, a2, and a3
so that our curve has the form
What do we know?
We know that f(t) interpolates p0 and p3 at t=0 and t=1
Constraint Matrix for Bezier
We also know that the slope at the ends is 3(p 1-p0)
Compute slope:
Evaluate at t=0 and t=1
Constraint Matrix for Bezier
Now we have a constraint matrix C: find it’s inverse B
Apply Constraint Matrix
Apply the vector [1 u u2 u3] to matrix B
Bezier Curve
All paths lead to the same point
De Castlejau Algorithm (recursive subdivision)
Bernstein Polynomials
Matrix Form
Cubic equations
All have their uses, and you will see all in practice

51
Derive the values ai
Another way to look at this:
NURBS
Nonuniform Rational B-Spline curves and surfaces
add a fourth variable w to x,y,z
Can interpret as weight to give more importance
to some control data
Can also interpret as moving to homogeneous
coordinate
Requires a perspective division
NURBS act correctly for perspective viewing
Quadrics are a special case of NURBS
53 E. Angel and D. Shreiner: Interactive Computer
Graphics 6E © Addison-Wesley 2012
NURBS
Non-Uniform Rational B-Splines
Note that Rational is usually a restriction
Rational numbers vs all real numbers.
Here it is an extension: Projective Geometry
Example: we wish to draw a unit circle
No exact match with Cubic Beziers – can get close
However, if we use projective space…
Quadratic Bezier

North and south pole and point at (Eastern) infinity


We only need a quadratic Bezier for 3 points
But are these points on the unit circle?

x
Degree Elevation

The control points are weighted


This gives NURBS more degrees of freedom
Interpolating Cubic
p1 p3

p0 p2
Use this technique to find a curve that interpolates
control points
Start by computing constraints: must hit
p0 at 0,
p1 at 1/3,
p2 at 2/3,
Interpolating Cubic
p1 p3

p0 p2
Use this technique to find a curve that interpolates
control points
Start by computing constraints: must hit
p0 at 0,
p1 at 1/3,
p2 at 2/3,
Interpolation Matrix
Solving for c we find the interpolation matrix

 1 0 0 0 
  5. 5 9  4 .5 1 
M I  A   9  22.5 18  4.5
1

 
  4 .5 13 . 5  13 .5 4 . 5 

c=MIp
Note that MI does not depend on input data and
can be used for each segment in x, y, and z

60 E. Angel and D. Shreiner: Interactive Computer


Graphics 6E © Addison-Wesley 2012
Interpolating Multiple Segments

use p = [p3 p4 p5 p6]T


use p = [p0 p1 p2 p3] T

Get continuity at join points but not


continuity of derivatives

61 E. Angel and D. Shreiner: Interactive Computer


Graphics 6E © Addison-Wesley 2012
Blending Functions
Rewriting the equation for p(u)
p(u)=uTc=uTMIp = b(u)Tp
where b(u) = [b0(u) b1(u) b2(u) b3(u)]T is
an array of blending polynomials such that
p(u) = b0(u)p0+ b1(u)p1+ b2(u)p2+ b3(u)p3
b0(u) = -4.5(u-1/3)(u-2/3)(u-1)
b1(u) = 13.5u (u-2/3)(u-1)
b2(u) = -13.5u (u-1/3)(u-1)
b3(u) = 4.5u (u-1/3)(u-2/3)
62 E. Angel and D. Shreiner: Interactive Computer
Graphics 6E © Addison-Wesley 2012
Lagrange Blending Functions
These functions are not smooth
Hence the interpolation polynomial is not smooth
Are not limited to [0, 1] – thus not convex

63 E. Angel and D. Shreiner: Interactive


Computer Graphics 6E © Addison-
Catmull-Rom
We can get a similar effect with Bezier curves
One way of auto-generating the right slopes to a set of
interpolated points was introduced by Catmull and Rom
Idea is to use slope between previous and next point as the
slope at the current point.
If distance between points is uneven, can overshoot

64
Cubic Hermite Spline
Sometimes we want to avoid sharp corners
Can define the slope at the endpoints
Hermite Polynomials use a new blending functions
to define curve

p’(0) p’(1)

p(0) p(1)
Hermite Polynomial
Hermite Polynomial
Analysis
Bezier form has advantages over interpolating form,
derivatives need note be continuous at join points
Can we do better?
Go to higher order Bezier
More work
Derivative continuity still only approximate
Supported by OpenGL
Apply different conditions
Tricky without letting order increase
68 E. Angel and D. Shreiner: Interactive Computer
Graphics 6E © Addison-Wesley 2012
Splines
Approximate rather than interpolate (blue vs red curves)
(Image is of a traditional spline used in boat building)

69
B-Splines
Basis splines: use the data at p=[pi-2 pi-1 pi pi-1]T to define
curve only between pi-1 and pi
We can apply more continuity conditions to each segment
For cubics, we can have continuity of function, first and
second derivatives at join points - C(2)
Cost is 3 times as much work for curves
Add one new point each time rather than three
For surfaces, we do 9 times as much work
E. Angel and D. Shreiner: Interactive Computer
70 Graphics 6E © Addison-Wesley 2012
Cubic B-spline
p(u) = uTMSp = b(u)Tp

1 4 1 0
 3 0 3 0

MS   3  6 3 0
 
 1 3  3 1

E. Angel and D. Shreiner: Interactive Computer


71 Graphics 6E © Addison-Wesley 2012
Blending Functions
 (1  u )
3

 3 
1  4  6 u  3u 
2
b(u ) 
6 1  3u  3 u 2  3 u 2 
 
 u
3


convex hull property

E. Angel and D. Shreiner: Interactive Computer


72 Graphics 6E © Addison-Wesley 2012
Basis Functions
In terms of the blending polynomials
 0 u i2
 (u  2) i  2  u  i 1
b0
 b1 (u  1) i 1  u  i
Bi (u )   (u ) i  u  i 1
 b2
 b3 (u  1) i 1  u  i  2

 0 u i2

E. Angel and D. Shreiner: Interactive Computer


73 Graphics 6E © Addison-Wesley 2012
Basis Functions
B-Spline Patches
3 3
p (u , v)   bi (u ) b j (v) pij  u M S P M v T T
S
i 0 j 0
defined over only 1/9 of region

75 E. Angel and D. Shreiner: Interactive


Computer Graphics 6E © Addison-
Splines and Basis
If we examine the cubic B-spline from the
perspective of each control (data) point, each
interior point contributes (through the blending
functions) to four segments
We can rewrite p(u) in terms of the data points as
defining the basis functions {Bi(u)}

p(u )   Bi (u ) pi

76 E. Angel and D. Shreiner: Interactive Computer


Graphics 6E © Addison-Wesley 2012
Generalizing Splines
We can extend to splines of any degree
Data and conditions do not have to given at equally
spaced values (the knots)
Nonuniform and uniform splines
Can have repeated knots
Can force spline to interpolate points
Cox-deBoor recursion gives method of evaluation

E. Angel and D. Shreiner: Interactive Computer


77 Graphics 6E © Addison-Wesley 2012
Beziers in OpenGL
We could evaluate Beziers by hand, given the formulas above.
It is simpler to use Evaluators, provided by OpenGL
Evaluators provide a way to use polynomial or rational polynomial
mapping to produce vertices, normals, texture coordinates, and
colors. The values produced by an evaluator are sent to further
stages of GL processing just as if they had been presented using
glVertex, glNormal, glTexCoord, and glColor commands, except
that the generated values do not update the current normal, texture
coordinates, or color.
All polynomial or rational polynomial splines of any degree (up to the
maximum degree supported by the GL implementation) can be
described using evaluators. These include almost all splines used in
computer graphics: B-splines, Bezier curves, Hermite splines, and
so on. (From the man page)

78
Bezier Curve
void drawCurve() {
int i;
GLfloat pts[4][3];
/* Copy the coordinates from balls to array */
for (i = 0; i < 4; i++) {
pts[i][0] = (GLfloat)cp[i]->x;
pts[i][1] = (GLfloat)wh - (GLfloat)cp[i]->y;
pts[i][2] = (GLfloat)0.0;
}
// Define the evaluator
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4, &pts[0][0]);
/* type, u_min, u_max, stride, num points, points */
glEnable(GL_MAP1_VERTEX_3);
setLineColor();
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
/* Evaluate the curve when u = i/30 */
glEvalCoord1f((GLfloat) i/ 30.0);
glEnd();
Teapot
bteapot.c
// vertices.h
GLfloat vertices[306][3]={{1.4 , 0.0 , 2.4}, {1.4 , -0.784 , 2.4},
{0.784 , -1.4 , 2.4}, {0.0 , -1.4 , 2.4}, {1.3375 , 0.0 , 2.53125},
{1.3375 , -0.749 , 2.53125}, {0.749 , -1.3375 , 2.53125}, {0.0 , -1.3375 ,
2.53125},
{1.4375 , 0.0 , 2.53125}, {1.4375 , -0.805 , 2.53125}, {0.805 , -1.4375 ,
2.53125},
...
// patches.h
int indices[32][4][4]={{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16},
{4, 17, 18, 19, 8, 20, 21, 22, 12, 23, 24, 25, 16, 26, 27, 28},
{19, 29, 30, 31, 22, 32, 33, 34, 25, 35, 36, 37, 28, 38, 39, 40},
...
bteapot.c
/* 32 patches each defined by 16 vertices, arranged in a 4 x 4 array */
/* NOTE: numbering scheme for teapot has vertices labeled from 1 to 306 */
/* remnent of the days of FORTRAN */
#include "patches.h"

void display(void)
{
int i, j, k;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);

glLoadIdentity();
glTranslatef(0.0, 0.0, -10.0);
glRotatef(-35.26, 1.0, 0.0, 0.0);
glRotatef(-45.0, 0.0, 1.0, 0.0);

/* data aligned along z axis, rotate to align with y axis */


glRotatef(-90.0, 1.0,0.0, 0.0);
bteapot.c
for(k=0;k<32;k++)
{
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4, &data[k][0][0][0]);
for (j = 0; j <= 8; j++)
{
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord2f((GLfloat)i/30.0, (GLfloat)j/8.0);
glEnd();
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord2f((GLfloat)j/8.0, (GLfloat)i/30.0);
glEnd();
}
}
glFlush();
}
Interactive Websites to try
Bill Casselman's Bezier Applet
http://www.math.ubc.ca/people/faculty/cass/gfx/bezier.html
Wikepedia Animation
http://en.wikipedia.org/wiki/Bezier_curve
Edward A. Zobel's Animation
http://id.mind.net/~zona/mmts/curveFitting/bezierCurves/
bezierCurve.html
POV-Ray Cyclopedia Tutorial
http://www.spiritone.com/~english/cyclopedia/bezier.html
Andy Salter's Spline Tutorial
http://www.doc.ic.ac.uk/%7Edfg/AndysSplineTutorial/index.html
Evgeny Demidov's Interactive Tutorial
http://ibiblio.org/e-notes/Splines/Intro.htm
84
Summary

85
Summary
We often wish to generate curves and surfaces
We would like
Interpolate (include) a set of arbitrary points
Easy and efficient to compute
Smooth
Local Control…
By splicing together pieces of curves, we can attain
many of these.
Modern hardware and language support these curves
86

You might also like