52
Interpolation and Curve Fitting De Casteljau construction of Bezier curve From www.wikipedia.org Pierre Bézier

Interpolation and Curve Fitting - CAIG Labcaig.cs.nctu.edu.tw/course/NM07S/slides/chap3_3.pdf · Interpolation and Curve Fitting De Casteljau construction of Bezier curve From Pierre

  • Upload
    ledat

  • View
    228

  • Download
    1

Embed Size (px)

Citation preview

Interpolation andCurve Fitting

De Casteljau construction of Bezier curveFrom www.wikipedia.org

Pierre Bézier

Numerical Methods © Wen-Chieh Lin 2

Bezier Curves and B-Spline Curves

Not really “interpolating”curves as theynormally do not pass through data points

Curves stay within the polygon defined by thegiven points

Local control: effect of changing a knot islimited to a local region in Bezier and b-spline

Global control: moving a knot in a cubic splineaffects the whole curve

Numerical Methods © Wen-Chieh Lin 3

Global vs. Local Control

Does a small change affect the whole curve orjust a small segment?

Local is usually more intuitive and providedby composite curves

Local

Global

Numerical Methods © Wen-Chieh Lin 4

Parametric Form

Explicit function

Parametric form

Example: unit circle

)(xfy

)(2 uFy )(1 uFx

)cos(ux )sin(uy

Numerical Methods © Wen-Chieh Lin 5

Interpolation in Parametric Form

Given n+1 control points,

Compute interpolation functions

nizyx

p

i

i

i

i ,...,0,

10,)()()(

)(

u

uzuyux

uPj

j: index for each segment of curve

Numerical Methods © Wen-Chieh Lin 6

Composite Segments

Divide a curve into multiple segmentsRepresent each in a parametric formMaintain continuity between segments

position tangent curvature

P1(u) P2(u) P3(u) Pn(u)

Numerical Methods © Wen-Chieh Lin 7

Cubic 3D Curves

Three cubic polynomials, one for each coordinate

In matrix notation

xxxx ducubuaux 23)(

yyyy ducubuauy 23)(

zzzz ducubuauz 23)(

zyx

zyx

zyx

zyx

dcdcccbbbaaa

uuuuzuyux 1)()()( 23

Numerical Methods © Wen-Chieh Lin 8

Hermite interpolation requires values at endpoints derivatives at endpoints

To create a composite curve, use the end ofone as the beginning of the other and share thetangent vector

Hermite Interpolation

)0(P

)0('P )1('P

)1(P

control points/knots

xxxx ducubuaux 23)(Needs 4 equations tosolve 4 unknowns

Numerical Methods © Wen-Chieh Lin 9

Hermite Curve Formation—consider x coordinate first

Cubic polynomial and its derivative

Given xi, xi+1, x’i, x’i+1, solve for a, b, c, d 4 equations are given for 4 unknowns

xxxx ducubuaux 23)(

xxx cubuaux 23)(' 2

xi dxx )0(xxxxi dcbaxx 1)1(

xi cxx ')0('

xxxi cbaxx 23')1(' 1

Numerical Methods © Wen-Chieh Lin 10

Hermite Curve Formation (cont.) Problem: solve for a, b, c, d

Solution:

ix

ix

iiiix

iiiix

xd

xcxxxxb

xxxxa

'''2)(3

'')(2

11

11

xi dxx )0(xxxxi dcbaxx 1)1(

xi cxx ')0('

xxxi cbaxx 23')1(' 1

Numerical Methods © Wen-Chieh Lin 11

Hermite Curve Formation (cont.) The coefficients can be expressed as linear

combination of geometric information

ix

ix

iiiix

iiiix

xd

xcxxxxb

xxxxa

'''2)(3

'')(2

11

11

1

1

''

000101001233

1122

i

i

i

i

x

x

x

x

xxxx

dcba

Numerical Methods © Wen-Chieh Lin 12

Hermite Curve Formation (cont.) x component of Hermite curve is then represented as

1

123

23

''

000101001233

1122

1

1)(

i

i

i

i

x

x

x

x

xxxx

uuu

dcba

uuuux

Numerical Methods © Wen-Chieh Lin 13

Hermite Curve Formation (cont.) Including y and z components

111

11123

23

''''''

000101001233

1122

1

1)()()(

iii

iii

iii

iii

zyx

zyx

zyx

zyx

zyxzyxzyxzyx

uuu

dddcccbbbaaa

uuuuzuyux

P(u)

uT M p

Numerical Methods © Wen-Chieh Lin 14

Hermite Interpolation in Matrix Form

MpuP T)(u

parametertheis]1,,,[ 23 uuuT umatrixcofficienttheisM

ninformatiogeometrictheisp

000101001233

1122

M

)1(')0(')1()0(

''

1

1

PPPP

pppp

i

i

i

i

p

ith segment incomposite curves

)0(P

)0('P )1('P

)1(P

control points/knots

Numerical Methods © Wen-Chieh Lin 15

Blending Functions of Hermite Splines

Each cubic Hermite spline is a linear combination of4 blending functions

1

1

23

23

23

23

''2

32

132

)(

i

i

i

i

T

pppp

uu

uuu

uu

uu

uP

MpuP Tu )(

geometric information

Numerical Methods © Wen-Chieh Lin 16

Bezier CurvesAnother variant of the same game Instead of endpoints and tangents, four control

points points p0 and p3 are on the curve points p1 and p2 are off the curve P(0) = p0, P(1) = p3

P’(0) = 3(p1 - p0), P’(1) = 3(p3 - p2)p0

p1

p2

p3

)1(')0(')1()0(

00010100

12331122

1)( 23

PPPP

uuuuP

3

2

1

0

23

3300003310000001

00010100

12331122

1)(

pppp

uuuuP

Numerical Methods © Wen-Chieh Lin 17

Bezier Curves (cont.)Variant of the Hermite spline

basis matrix derived from the Hermite basis

Gives more uniform control knobs (series ofpoints) than Hermite

The slope at u = 0 is the slope of the secantline between p0 and p1

)(3 01 xxdudx )(3 01 yydudy

)()( 0101 xxyydxdy

)(3)0(' 01 ppP

Numerical Methods © Wen-Chieh Lin 18

Blending Functions of Bezier Curves Also known as degree 3 Bernstein polynomials Weighted sum of Bernstein polynomials (infinite

series) converges uniformly to any continuousfunction on the interval [0,1]

3

2

1

3

2

2

3

)1(3

)1(3

)1(

)(

i

i

i

i

T

pppp

u

uu

uu

u

uP

Numerical Methods © Wen-Chieh Lin 19

Composing Bezier Curves

Control points at consecutive segments needbe collinear to avoid a discontinuity of slope

Numerical Methods © Wen-Chieh Lin 20

Interpolation, Continuity, andLocal Control

Hermite/Bezier splines Interpolate control points Local control No C2 continuity for cubics

Cubic (Natural) splines Interpolate control points No local control - moving one control point affects whole

curve C2 continuity for cubics

Can’t get C2, interpolation and local control with cubics

Numerical Methods © Wen-Chieh Lin 21

Cubic B-Splines

Basis-spline (B-spline)

Give up interpolation the curve passes near the control points best generated with interactive placement (it’s hard

to guess where the curve will go)

Curve obeys the convex hull property C2 continuity and local control are good

compensation for loss of interpolation

Numerical Methods © Wen-Chieh Lin 22

Cubic B-splines (cont.) Any evaluation of conditions of Bi(1) cannot use pi+3,

because pi+3 does not appear in Bi(u) Evaluation conditions of Bi+1(0) cannot use pi-1

Two conditions satisfy this symmetric condition

64

)0()1( 211

iii

iippp

BB

2)0(')1(' 2

1

iiii

ppBBiB 1iB

2iB

1ip

4ip

3ip

1ip

ip

2ip

Numerical Methods © Wen-Chieh Lin 23

Solving for B-spline Coefficients From

We can obtain

We can solve a set of 4 equations for a, b, c, d

iiiii ducubuauB 23)(

iiii cubuauB 23)(' 2

ii dB )0(

iiiii dcbaB )1(

ii cB )0('

64

)0()1( 211

iii

iippp

BB

2)0(')1(' 2

1

iiii

ppBB

64

)0( 11 iii

ippp

B

2)0(' 11 ii

ipp

Biiii cbaB 23)1('

Then plug in the followings

Numerical Methods © Wen-Chieh Lin 24

Cubic B-splines in Matrix Form

2

1

1

23

0141030303631331

161

)(

i

i

i

i

Ti

ppp

p

uuuuB Mpu

iB 1iB

2iB

1ip

4ip

3ip

1ip

ip

2ip

)(0 ub

)(1 ub

)(1 ub )(2 ub

Numerical Methods © Wen-Chieh Lin 25

Blending Functions of Cubic B-Splines

Represent cubic b-spline as

Blending functions

)(0 ub

)(1 ub

)(1 ub )(2 ub

2211011)( iiiii pbpbpbpbuB

6)1( 3

1u

b

32

22

3

0 uu

b

61

222

23

1 uuu

b

6

3

1u

b

Numerical Methods © Wen-Chieh Lin 26

Continuity in Cubic B-splines

Continuity conditions similar to cubic spline

64

)0()1( 211

iii

iippp

BB

2)0(')1(' 2

1

iiii

ppBB

211 2)0(")1(" iiiii pppBB

iB 1iB

2iB

1ip

4ip

3ip

1ip

ip

2ip

Prove that this conditionis automatically satisfiedif the first two hold!

Numerical Methods © Wen-Chieh Lin 27

Composing B-splinesGiven points p0, …, pn, we can construct B-

splines B1 through Bn-2

We need additional points outside the domain ofthe given points to construct B0 and Bn-1

Add fictitious points p-2 , p-1 , pn+1 , pn+2

Set p-2 = p-1 = p0 and pn+2 = pn+1 = pn so that thecurve starts and ends at the given extreme points

Numerical Methods © Wen-Chieh Lin 28

Comparison of Basic Cubic Splines

Type Control points Continuity Interpolation

Natural n points, shared C2 YHermite 2 points, 2 slopes C1 YBezier 4 points C1 YB-Splines 4 points, shared C2 N

Numerical Methods © Wen-Chieh Lin 29

Interpolating on a Surface

Bezier surfacesB-Spline surfaces

Numerical Methods © Wen-Chieh Lin 30

Bicubic Surfaces To represent surfaces use bicubic functions

x(u,v), y(u,v), z(u,v) are cubic polyomials both in uand v

16 terms for combination of powers of u and v

A bicubic surface can be represented by a 4x4matrix A

Also known as tensor surfaces

4423

1233

11),( avuavuavux

AvuT),( vux 123T uuuu

Tvvv 123v

Numerical Methods © Wen-Chieh Lin 31

Cubic B-Spline Surfaces

Recall B-spline curve:

vMMXuAvu Tjiij vux ,

TT),(

2

1

1

23

0141030303631331

161

)(

i

i

i

i

Ti

ppp

p

uuuuB Mpu

2,21,2,21,2

2,11,1,11,1

2,1,,1,

2,11,1,11,1

jijijiji

jijijiji

jijijiji

jijijiji

ij

xxxxxxxxxxxx

xxxx

X

Numerical Methods © Wen-Chieh Lin 32

2

1

1

23

0001003303631331

1)(

i

i

i

i

Ti

ppp

p

uuuu MpuP

Bezier Surfaces

Recall Bezier curve:

vMMXuAvu Tjiij vux ,

TT),(

2,21,2,21,2

2,11,1,11,1

2,1,,1,

2,11,1,11,1

jijijiji

jijijiji

jijijiji

jijijiji

ij

xxxxxxxxxxxx

xxxx

X

Numerical Methods © Wen-Chieh Lin 33

Bezier Surfaces

00)0,0( pp )(3)0,0( 0010 ppu

p

)(3)0,0( 0001 ppv

p

vu

Numerical Methods © Wen-Chieh Lin 34

Least Square Approximation

Measurement errors are inevitable inobservational and experimental sciences

Errors can be smoothed out by averaging overmany cases, i.e., taking more measurementsthan are strictly necessary to determineparameters of system

Resulting system is overdetermined, so usuallythere is no exact solution

Numerical Methods © Wen-Chieh Lin 35

Least Square Approximation

Higher dimensional data are projected intolower dimensional space to suppress irrelevantdetails

Such projection is most convenientlyaccomplished by least square method

Linear projection from 2D to 1D space:Original data distributed in a 2D spaceThey can be approximated as a 1D line

Numerical Methods © Wen-Chieh Lin 36

Linear Least Squares

For linear problems, we obtain overdeterminedlinear system Aa = b with m×n matrix A, m > n

System is better written Aa ≈b, since equalityis usually not exactly satisfied when m > n

Least squares solution a minimizes squaredEuclidean norm of residual vector r = b - Aa

2

2

2

2minmin Aabr

aa

Numerical Methods © Wen-Chieh Lin 37

Curve Fitting

Given m data points (xi, yi), find n-vector a ofparameters that gives “best fit”to modelfunction y = f(x, a),

Problem is linear if function f is linear incomponents of a

Problem can be written in matrix form asAa≈b with Aij =φj(xi) and bi=yi

)()()(),( 2211 xaxaxaxf nn a

m

ii xfy

1

2)),((min aa

Numerical Methods © Wen-Chieh Lin 38

Curve Fitting (cont.) Polynomial fitting

is linear, since polynomial linear in coefficients,though nonlinear in independent variable x

Fitting sum of exponentials

is example of nonlinear problemWe will only consider linear least squares

problems for now

121),( n

n xaxaaxf a

xan

xa neaeaxf 112),( a

Numerical Methods © Wen-Chieh Lin 39

Example: curve fitting

Fitting quadratic polynomial to five data pointsgives linear least squares problem

5

4

3

2

1

3

2

1

255

244

233

222

211

11111

yyyyy

aaa

xxxxxxxxxx

Aa

Numerical Methods © Wen-Chieh Lin 40

Example: curve fitting (cont.) For data x -1.0 -0.5 0.0 0.5 1.0

y 1.0 0.5 0.0 0.5 2.0overdetermined linear system is

Solution, which we will see later how to compute,is

0.25.00.05.00.1

0.10.1125.05.010.00.0125.05.010.10.11

3

2

1

aaa

Aa

T]4.140.0086.0[a

Numerical Methods © Wen-Chieh Lin 41

Example (cont.) The computed approximation polynomial is

Resulting curve and original data points areshown in graph

24.14.0086.0)( xxxp

Numerical Methods © Wen-Chieh Lin 42

Normal Equations

To minimize squared Euclidean norm ofresidual vector

Take derivative with respect to a and set it to 0

which reduces to n×n linear system of normalequations

)()(2

2AabAabrrr TT

AaAabAabb TTTTT 2

022 bAAaA TT

bAAaA TT

Numerical Methods © Wen-Chieh Lin 43

Orthogonality Space spanned by columns of m × n matrix A,

, is of dimension at most n If m > n, b generally does not lie in span(A),

so there is no exact solution to Aa = bVector y = Aa in span(A) closest to b in 2-

norm occurs when residual r = b - Aa isorthogonal to span(A),

again giving system of

}:{)( nRspan aAaA

Aa)(bArA TT 0

bAAaA TT

Numerical Methods © Wen-Chieh Lin 44

Orthogonality (cont.)

Geometric relationships among b, r, andspan(A) are shown in diagram

Aay

b Aabr

)(Aspan

Numerical Methods © Wen-Chieh Lin 45

Pseudoinverse and Condition Number

Nonsquare m × n matrix A has no inverse inusual sense

If rank(A) = n, pseudoinverse is defined by

and condition number by

By convention, cond(A) = ∞ if rank(A) < n

TT AAAA 1)(

AAA)(cond

Numerical Methods © Wen-Chieh Lin 46

Pseudoinverse and Condition Number

Just as condition number of square matrixmeasure closeness to singularity, conditionnumber of rectangular matrix measurescloseness to rank deficiency

Least squares solution of Aa≈b is given bya=A+b

Numerical Methods © Wen-Chieh Lin 47

Sensitivity and Conditioning Sensitivity of least squares solution to Aa≈b

depends on b as well as ADefine angle θbetween b and y = Aa by

Bound on perturbation ∆x in solution x due toperturbation ∆b in b is given by

2

2

2

2)cos(b

Aa

b

y

2

2

2

2

)cos(1

)(b

b

x

x

Acond

Numerical Methods © Wen-Chieh Lin 48

Sensitivity and Conditioning

Similarly, for perturbation E in matrix A,

Condition number of least squares solution isabout cond(A) if residual is small, but can besquared or arbitrarily worse for large residual

2

2

2

2 )1)())(tan((A

EAA

x

x

condcond

Numerical Methods © Wen-Chieh Lin 49

Normal Equation Method If m × n matrix A has rank n, then symmetric n ×

n matrix is positive definite, so its Choleskyfactorization

can be used to obtain solution a to system ofnormal quations

which has same solution as linear least squaresproblem Aa≈b

Normal equations method involvestransformations:

rectangular square triangular

TT LLAA

bAAaA TT

Numerical Methods © Wen-Chieh Lin 50

Example: Normal Equation Method For polynomial data-fitting on

x -1.0 -0.5 0.0 0.5 1.0y 1.0 0.5 0.0 0.5 2.0

Normal equations method gives

125.20.05.20.05.20.05.20.00.5

0.10.1125.05.010.00.0125.05.010.10.11

0.125.00.025.00.10.15.00.05.00.1

11111AAT

Numerical Methods © Wen-Chieh Lin 51

Example: Normal Equation Method For polynomial data-fitting on

x -1.0 -0.5 0.0 0.5 1.0y 1.0 0.5 0.0 0.5 2.0

Normal equations method gives

25.30.10.4

0.25.00.05.00.1

0.125.00.025.00.10.15.00.05.00.1

11111bAT

Numerical Methods © Wen-Chieh Lin 52

Example (cont.)Choleskey factorization of symmetric positive

definite matrix ATA gives

Solving lower triangular system Lz = ATb byforward substitution gives z = [1.789 0.632 1.336]T

Solving lower triangular system La = z by forwardsubstitution gives a = [0.086 0.400 1.429]T

935.00.00.00.0581.10.0

118.10.0236.2

935.00.0118.10.0581.10.00.00.0236.2

125.20.05.20.05.20.05.20.00.5

AAT