b Sp Line Curve Least Squares Fit

Embed Size (px)

Citation preview

  • 8/10/2019 b Sp Line Curve Least Squares Fit

    1/5

    Least-Squares Fitting of Data with B-Spline Curves

    David Eberly

    Geometric Tools, LLChttp://www.geometrictools.com/Copyright c1998-2008. All Rights Reserved.

    Created: December 2, 2005Last Modified: September 13, 2008

    Contents

    1 Definition of B-Spline Curves 2

    2 Least-Squares Fitting 3

    3 Implementation 5

    1

    http://www.geometrictools.com/http://www.geometrictools.com/
  • 8/10/2019 b Sp Line Curve Least Squares Fit

    2/5

    This document describes how to fit a set of data points with a B-spline curve using a least-squares algorithm.The construction allows for any dimension for the data points. A typical application is to fit keyframes foranimation sequences, whether the data is positional (3D) or rotational using quaternions (4D). In the latter

    case, the B-spline curve is not necessarily on the unit hypersphere in 4D, but the curve evaluations may benormalized to force the results onto that hypersphere.

    1 Definition of B-Spline Curves

    A B-spline curveis defined for a collection ofn + 1 control points {Qi}ni=0 by

    X(t) =ni=0

    Ni,d(t)Qi (1)

    The control points can be any dimension, but all of the same dimension. The degreeof the curve is d and

    must satisfy 1 d n. The functions Ni,d(t) are the B-spline basis functions, which are defined recursivelyand require selection of a sequence of scalars ti for 0 i n + d + 1. The sequence is nondecreasing; thatis,ti ti+1. Eachti is referred to as a knot, the total sequence a knot vector. The basis function that startsthe recursive definition is

    Ni,0(t) =

    1, ti t < ti+1

    0, otherwise(2)

    for 0 i n + d. The recursion itself is

    Ni,j(t) = t titi+j ti

    Ni,j1(t) + ti+j+1 t

    ti+j+1 ti+1Ni+1,j1(t) (3)

    for 1 j d and 0 i n + d j. The supportof a function is the smallest closed interval on which thefunction has at least one nonzero value. The support ofN

    i,0(t) is clearly [t

    i, ti+1]. In general, the support of

    Ni,j(t) is [ti, ti+j+1]. This fact means that locally the curve is influenced by only a small number of controlpoints, a property called local control.

    The main classification of the knot vector is that it is either openor periodic. If open, the knots are eitheruniformor nonuniform. Periodic knot vectors have uniformly spaced knots. The use of the term open isperhaps a misnomer since you can construct a closed B-spline curve from an open knot vector. The standardway to construct a closed curve uses periodic knot vectors. Uniform knots are

    ti=

    0 , 0 i d

    idn+1d

    , d + 1 i n

    1 , n + 1 i n + d + 1

    (4)

    Periodic knots are

    ti= i d

    n + 1 d, 0 i n + d + 1 (5)

    Equations(2) and (3) allow you to recursively evaluate the B-spline curve, but there are faster ways basedon the local control. This document does not cover the various evaluation schemes. You may find this topicin any book on B-splines and most likely at online sites.

    2

  • 8/10/2019 b Sp Line Curve Least Squares Fit

    3/5

    2 Least-Squares Fitting

    The data points are {(sk, Pk)}mk=0, wheresk are the sample times andPk are the sample data. The sampletimes are assumed to be increasing: s0 < s1 < .. . < sm. A B-spline curve that fits the data is parameterizedbyt [0, 1], so the sample times need to be mapped to the parameter domain by tk = (sk s0)/(sm s0).

    The fitted B-spline curve is formally presented in equation (1), but the control points Qi are unknownquantities to be determined later. The control points are considered to be column vectors, and the collectionof control points may be arranged into a single column vector

    Q=

    Q0

    Q1...

    Qn

    . (6)

    Similarly, the samples Pk are considered to be column vectors, and the collection written as a single columnvector

    P =

    P0

    P1...

    Pm

    . (7)

    For a specifed set of control points, the least-squares error functionbetween the B-spline curve and samplepoints is the scalar-valued function

    E(Q) =1

    2

    mk=0

    n

    j=0

    Nj,d(tk)Qj Pk

    2

    (8)

    The half term is just for convenience in the calculations. The quantityn

    j=0 Nj,d(tk)Qj is the point onthe B-spline curve at the scaled sample time tk. The term within the summation on the right-hand sideof equation (8) measures the squared distance between the sample point and its corresponding curve point.The error function measures the total accumulation of squared distances. The hope is that we may choosethe control points to make this error as small as possible.

    The minimization is a calculus problem. The function Eis quadratic in the components ofQ, its graph aparaboloid (in high dimensional space), so it must have a global minimum that occurs when all its first-orderpartial derivatives are zero. That is, the vertex of the parabola occurs where the first derivatives are zero.

    The first-order partial derivatives are written in terms of the control points Qi rather than in terms of thecomponents of the control points:

    EQ

    i

    = m

    k=0

    nj=0 Nj,d(tk)Qj Pk

    Ni,d(tk)

    = m

    k=0

    nj=0 Ni,d(tk)Nj,d(tk)Qj

    mk=0 Ni,d(tk)Pk

    = m

    k=0

    nj=0 akiakjQj

    mk=0 akiPk

    (9)

    3

  • 8/10/2019 b Sp Line Curve Least Squares Fit

    4/5

    wherearc= Nc,d(tr), and for 0i n. Setting the partial derivatives equal to the zero vector leads to thesystem of equations

    0=m

    k=0

    n

    j=0

    aik

    ajk

    Qj

    m

    k=0

    aik

    Pk

    = ATAQ ATP (10)

    whereA = [arc] is a matrix with n + 1 rows andm + 1 columns. The matrixAT is the transpose ofA. This

    system of equations is in a familiar form of a least-squares problem. Recall that such problems arise whenwanting to solve Ax = b. If the system does not have a solution, the next best thing is to constructx sothat|Ax b|is as small as possible. The minimization leads to the linear system ATAx= ATb.

    The matrix ATA is symmetric, a property that is desirable in the numerical solution of systems. Moreover,the matrix A is banded. This is a generalization of tridiagonal. A banded matrix has a diagonal with(potentially) nonzero entries. It has a contiguous set ofupper bandsand a contiguous set of lower bands,each band with (potentially) nonzero entries. All other entries in the matrix are zero. In our case, thenumber of upper bands and the number of lower bands are the same, namely d+ 1. The bandedness is aconsequence of the local control for B-spline curves (the supports of the B-spline basis functions are bounded

    intervals).The direct approach to solving the equation (10) is to invert the coefficient matrix. The equation ATAQ=ATP implies

    Q=

    ATA1

    ATP=

    ATA1

    AT

    P =XP (11)

    where the last equality defines the matrix X. The problem, though, is that the matrix inversion can be illconditioned because the matrix has eigenvalues that are nearly zero. The ill conditioning causes a Gaussianelimination, even with full pivoting, to have problems. For the application of fitting keyframe data, thekeyframes tend to be sampled at evenly spaced time intervale. In this case, the ill conditioning is not anissue as long as you choose a B-spline curve with uniform knots. Regardless, an approach different from thedirect inversion is called for, both to minimize the effects of ill conditioning andto take advantage of thebandedness of the matrix. Recall that Gaussian elimination to solve a linear system with an n n matrixis anO(n3) algorithm. The solution to a linear system with a tridiagonal matrix is O(n). The same is truefor a banded matrix with a small number of bands relative to the size of the matrix.

    The numerical method of choice for symmetric, banded matrix systems is the Cholesky decomposition. Thebook Matrix Computations by G. Golub and C. van Loan has an excellent discussion of the topic. Thealgorithm starts with a symmetric matrix and factors it into a lower-triangular matrix, G, times the trans-pose of that lower-triangular matrix, GT, which is necessarily upper triangular. In our case, the Choleskydecomposition is

    ATA= GGT (12)

    A numerically stable LU solver may be used first to invert G, then to invert GT. For the application offitting keyframe data, the choice of uniform knots leads to good stability, but also required is to make certainthat the number of control points is smaller than the number of samples by a half. This is essentially aNyquist-frequency argument. If you have as many control points as samples, the B-spline curve can have

    large oscillations.

    The matrix Xis computed as the solution to ATAX=AT using the Cholesky decomposition to invert thematrixATA. The vector of control points, Q, is then computed from Equation (11).

    4

  • 8/10/2019 b Sp Line Curve Least Squares Fit

    5/5

    3 Implementation

    A sample application to illustrate the fit is in the folder

    GeometricTools/WildMagic4/SampleFoundation/BSplineCurveFitter

    In the application, a spiral curve is generated as a polyline of 1000 sample points. A B-spline curve is createdusing the algorithm of this document. The initial degree is 3 and the initial number of control points is 500.The display shows the current degree and the current number of control points. It also shows the averagedistance error between the original samples and the resample B-spline curve points as well as showing theroot-mean-square error for the distances.

    You can increase or decrease the degree and the number of control points.

    key action

    d decrease the degree by 1

    D increase the degree by 1

    s decrease the number of control points by 1 (s for small)

    S increase the number of control points by 1 (S for small)

    m decrease the number of control points by 10 (m for medium)

    M increase the number of control points by 10 (M for medium)

    l decrease the number of control points by 100 (l for large)

    L increase the number of control points by 100 (L for large)

    ESC terminate the application

    You may rotate the scene using the mouse and the built-in virtual trackball. Of interest is when the numberof control points is between 20 and 30. This is where you will see big differences between the curves. Notethat you get a very good fit with an extremely small number of control points.

    5