Numerical Computation Lecture 14: Bezier Curves United International College

Preview:

Citation preview

Numerical Computation

Lecture 14: Bezier Curves

United International College

Last Time

• During the last class period we covered:– Cubic Splines– Readings: • Pav, section 6.2 (omit 6.2.1)

Today

• We will cover:– Bezier Curves – Readings: • These Slides !!

Review

• So far we have looked at the question of finding a polynomial (or set of polynomials) to interpolate a set of data points {xk ,yk}

• We have come up with two ways to find a single interpolating polynomial for the data: Lagrange method and Newton’s method.

• We have computed spline curves that interpolate the data over a partition.

Drawbacks to These Methods

• These methods are quite useful for a variety of applications, but they have two major drawbacks:

• 1) They cannot model data that have duplicate y – values

Drawbacks to These Methods

• 2) The final shape of the curve is completely dependent on the data. It would be nice to be able to better control the shape of the interpolating curve without having to move data points.

Bezier Curves

• Bezier Curves can solve both of these problems !• Definition: A Bezier curve is a parametric curve

described by polynomials based on control points.– Any number of control points may be used. (usually 4)– Degree of polynomials = number of points – 1. (usually =3)– Several Bezier curves can easily be glued together in a way

that makes the curve as a whole smooth.– A Bezier curve passes through its first and last control

points, but, in general, no others.

Parametric Curves

• Definition: A parametric curve is a vector function of a single argument (t). The variable t is called the parameter.

• Example: p(t) = (x(t), y(t)) is a parametric curve in the plane.

• Example: p(t) = (x(t), y(t), z(t)) is a parametric curve in space.

• Example: p(t) = (cos(t), sin(t)) for 0 ≤ t ≤ π produces a circle.

Parametric Curves

• Example: x(t) = t2–2t, y(t) = t–1, t in [0,3].

• By using parametric curves we can create curves with multiple y-values for a given x-value. This solves the first problem discussed above as Drawback 1).

x

y

t = 0 (start)

(t2–2t, t–1)

t = 3 (end)

Parametric Curves

• Other Benefits of using Parametric Curves:– Every curve can be described as a parametric

curve.– A parametric curve can be easily computed.– A parametric curve is a useful description of

motion. We think of the parameter as representing time (thus, “t”).

Parametric Curves - Motion

• Given a parametric curve p(t) = (x(t), y(t)) p’(t) = (x’(t), y’(t)) velocity vector points in

direction of motionp’(t)/||p’(t)|| unit tangent (velocity) vector||p’(t)|| speed

t = 0t = 1

p(t) p’(t)

Bezier Curves

• Bezier curves are parametric curves, where each of the parametric functions for x(t) and y(t) are polynomials in t (typically degree 2 or 3).

• Quadratic Bezier Curve: x(t) and y(t) are quadratic polynomials in t. (0 ≤ t ≤ 1)

• Cubic Bezier Curve: x(t) and y(t) are cubic polynomials in t. (0 ≤ t ≤ 1)

x(t) = at3 + bt2 + ct + dy(t) = et3 + ft2 + gt + h

Bezier Curves

• Cubic Bezier Curve: x(t) = at3 + bt2 + ct + d y(t) = et3 + ft2 + gt + h (0 ≤ t ≤ 1)• A Bezier curve passes through its first and last control points, so

x0 = x(0) y0 = y(0) x3 = x(1) y3 = y(1)

How do we find the other points (x(t),y(t)) on the curve?

p0=(x0,y0)(x(t),y(t))

p1=(x1,y1)

p3=(x3,y3)

p2=(x2,y2)

de Casteljau Algorithm

• The de Casteljau algorithm calculates the points on a Bezier Curve as a recursive series of linear interpolations. (“Lerps”)• Step 1:

p0

q0

p1

p2

p3

q2

q1

32322

21211

10100

)1(,,

)1(,,

)1(,,

ppppq

ppppq

ppppq

tttLerp

tttLerp

tttLerp

de Casteljau Algorithm

• Step 2: q0

q2

q1

r1

r0

21211

10100

)1(,,

)1(,,

qqqqr

qqqqr

tttLerp

tttLerp

de Casteljau Algorithm

• Step 3:

r1p(t)=(x(t),y(t))

r0•

1010 )1(,,)( rrrrp tttLerpt

de Casteljau Algorithm

• Expanding this recursive formula gives:

3221

211010

3221211

2110100

32322

21211

10100

111

1111,,)(

111,,

111,,

1,,

1,,

1,,

pppp

pppprrp

ppppqqr

ppppqqr

ppppq

ppppq

ppppq

ttttttt

ttttttttLerpt

tttttttLerp

tttttttLerp

tttLerp

tttLerp

tttLerp

de Casteljau Algorithm

• Collecting powers of t and simplifying, we get

• Note: p(0) = p0 and p(1) = p3, so this curve does go through the first and last control points, as was required.

3

32

23

123

023

33

22

12

03

33

363133)(

,

13131)(

pp

ppp

ppppp

ttt

ttttttt

or

ttttttt

Bezier Curve

p(t)•

p0

p1

p2

p3

Bernstein Polynomials

• The polynomials that appear as coefficients of the powers of t are called Bernstein Polynomials.

3

32

23

123

023

33

22

12

03

33

363133)(

,

13131)(

pp

ppp

ppppp

ttt

ttttttt

or

ttttttt

Bernstein Polynomials

33

3

2332

2331

2330

3332

321

310

30

33

223

123

023

33

363

133

)(

33

363133)(

ttB

tttB

ttttB

ttttB

tBtBtBtBt

ttt

ttttttt

ppppp

pp

ppp

Bernstein Polynomials

B03(t)

B13(t) B2

3(t)

B33(t)

Bernstein Polynomials

!!

!

13

3

12

3)1(333

11

3)1(3363

10

3)1(133

30333

2122332

1222331

0332330

ini

n

i

n

ttttB

tttttttB

ttttttttB

tttttttB

• Definition: The i-th Bernstein polynomial of degree n is defined as

• Note: If we add up all of the Bernstein polynomials of degree n, we always get 1, no matter what t is.

Bernstein Polynomials

iinni tt

i

ntB

1

1 tBni

• Bernstein Formula for a Bezier curve of degree n:

Bezier Curves

n

ii

ni

iinni

tBt

tti

ntB

0

1

pp

• Bernstein Formula for a cubic Bezier curve:

Bezier Curves

3

0

3

iii tBt pp

• So far we have looked at two methods for computing the Bezier curve: de Casteljau’s Algorithm and Bernstein Polynomials. Now, we will consider a third method, which is the most efficient method. This method involves matrix calculations.

• Recall the equations for the cubic Bezier curve:

Matrix Form of Bezier Curves

3

32

23

123

023

33

363133)(

pp

ppp

ttt

ttttttt

• If we re-group the terms, so that we have all multipliers of powers of t, we get:

Matrix Form of Bezier Curves

133

36333)(

010

2210

33210

ppp

pppppppp

t

ttt

Matrix Form of Bezier Curves

0

10

210

3210

23

010

2210

33210

33

363

33

)(

133

36333)(

pd

ppc

pppb

ppppa

dcbap

ppp

pppppppp

tttt

t

ttt

Matrix Form of Bezier Curves

0

10

210

3210

23

33

363

33

)(

pd

ppc

pppb

ppppa

dcbap

tttt

3

2

1

0

23

0001

0033

0363

1331

1)(

p

p

p

p

d

c

b

a

d

c

b

a

p tttt

Matrix Form of Bezier Curves

yx

yx

yx

yx

pp

pp

pp

pp

tttt

tttt

33

22

11

00

23

3

2

1

0

23

0001

0033

0363

1331

1)(

0001

0033

0363

1331

1)(

p

p

p

p

p

p

function p = bezier(t, p0, p1, p2, p3 )% bezier computes the bezier curve value p(t)% defined by control points p0, p1, p2, p3u=[t^3 t^2 t 1];M = [-1, 3,-3, 1; 3,-6, 3, 0; -3, 3, 0, 0; 1, 0, 0, 0]C = [p0;p1;p2;p3]p=u*M*Cend

Matlab Function for Bezier Curves

function plot_bezier(p0,p1,p2,p3)%plot_bezier plots the bezier curve through the control % points p0, p1, p2, p3 tvector = 0.0:0.05:1; pts = zeros(length(tvector),2); for i = 1:length(tvector) pts(i,:) = bezier(tvector(i), p0, p1, p2, p3); end C = [p0;p1;p2;p3]; plot(C(:,1), C(:,2),'o',pts(:,1),pts(:,2),'-');end

Matlab Function for Bezier Curves

Matlab Function for Bezier Curves

• Bezier surfaces are an extension of Bezier curves to 3 dimensions

• Instead of the curve being parameterized by a single variable t, we use two variables, s and t

• By definition, we choose to have s and t range from 0 to 1

Bezier Surfaces

s

t

0,0

1,1

1,0

0,1

• Consider a bicubic Bezier surface (bicubic means that it is a cubic function in both the s and t parameters)

• A cubic curve has 4 control points, and a bicubic surface has a grid of 16 control points, p0 through p15

Control Mesh

p0 p1

p2

p3

p4 p5

p6

p7

p8 p9

p10

p11

p12 p13

p14 p15

s

t

• Recall that for a Bezier curve we had a formula in terms of the Bernstein polynomials:

• It seems reasonable to define the Bezier surface in a similar fashion so that the surface is really a family of Bezier curves:

Bernstein Polynomial

ijjiji

tBsBts pp )()(, 333

0

3

0

3

0

3

iii tBt pp

• Recall that the matrix form for a Bezier curve is

• A parametric surface will have three component functions of s and t – one for each of the three dimensions: p(s,t) = (x(s,t), y(s,t), z(s,t))

Matrix Form

yx

yx

yx

yx

pp

pp

pp

pp

tttt

33

22

11

00

23

0001

0033

0363

1331

1)(p

• To simplify the calculation of the parametric surface, we will consider the matrix equation for each of the x, y, and z components separately

• For example, to evaluate the x component of a Bezier curve, we have:

Matrix Form

x

x

x

x

p

p

p

p

tttx

3

2

1

0

23

0001

0033

0363

1331

1

• If we expand the Bernstein formula

and isolate the x component of p(s,t) we will get (after a lot of arithmetic!)

Matrix Form

ijjiji

tBsBts pp )()(, 333

0

3

0

1

1

,

23

23

ttt

sss

tsx TTBezxBez

t

s

tBGBs

Matrix Form

1

1

,

23

23

ttt

sss

ts

TBezxBezx

Tz

Ty

Tx

t

s

BGBC

tCs

tCs

tCs

x

xxxx

xxxx

xxxx

xxxx

x

TBezBez

pppp

pppp

pppp

pppp

151173

141062

13951

12840

0001

0033

0363

1331

G

BB

• We would have similar formulas for y(s,t) and z(s,t). • Notes:– Cx stores the coefficients of the bicubic equation for x

– Gx stores the geometry (x components of the control points)

– BBez is the basis matrix (Bezier basis)

– s and t are the vectors formed from the exponents of s and t

– The matrix form can also take advantage of 4x4 matrix support which is built into modern computer graphics hardware

Matrix Form

• Your Programming Assignment is to create two Matlab functions:– bezier_surf(s,t,G)

• This will take two parameters s and t and a 4x4x3 matrix G and will return the vector p(s,t) = (x(s,t), y(s,t), z(s,t))

– bezier_surf_plot(G)• This will take one parameter – a 4x4x3 matrix G and will plot the

Bezier surface defined by these points.

Programming Assignment

• To assist you in this work, I have included code on the lecture web page that shows how to input and work with a 4x4x3 matrix and gives an example you should use for the matrix G. It also shows what kind of output you should expect.

• The surface plot should look like the figure on the next page:

Programming Assignment

Programming Assignment

Recommended