# How to interpolate a set of points with a continuous closed B-spline curve?

I have been learining the NURBS theory by the classical textbook "The NURBS Book" this year. In the chapter 9, the author introduced the method of non-rational B-spline curve interpolation with a open curve.

I have implemented this global B-spline curve interpolation algorithm with the Wolfram Mathematica. About the details, please see here or here

In addition, I know how to generate a closes B-spline curve by the control points. Please see my answer for closes B-spline curve

Now let me give a simple example with Mathematica, I add the option SplineClosed -> True in the BSplineCurve.

searchSpan[knots_, u0_] :=
With[{max = Max[knots]},
If[u0 == max, Position[knots, max][[1, 1]] - 2,
Ordering[UnitStep[u0 - knots], 1][] - 2]
]

Options[BSplineInterpolation] = {SplineDegree -> Automatic};

BSplineInterpolation[pts : {{_, _} ..}, opts : OptionsPattern[]] /;
MatrixQ[pts, NumericQ] :=
Module[{n, md, sd, paras, knots, coeffMat, ctrlpts},
n = Length@pts - 1;
sd = OptionValue[SplineDegree] /. Automatic -> 3 /.
deg_ :> n /; deg > n;
paras =
FoldList[Plus, 0, Normalize[(Norm /@ Differences[pts]), Total]] // N;
(*calculate the knots*)
knots =
Join[ConstantArray[0, sd + 1],
1/sd (Plus @@ (paras[[# + 1 ;; # + sd]]) & /@ Range[1, n - sd]),
ConstantArray[1, sd + 1]] // N;
(*calculate the coefficients of matrix*)
coeffMat = Function[{u0},
With[{i = searchSpan[knots, u0]},
Join[ConstantArray[0, i - sd],
BSplineBasis[{sd, knots}, #, u0] & /@ Range[i - sd, i],
ConstantArray[0, n - i]]]] /@ paras;
(*solve the control points of B-Spline curve*)
ctrlpts = LinearSolve[coeffMat, pts];
(*visualize the result*)
Graphics[
{BSplineCurve[ctrlpts,
SplineClosed -> True, SplineDegree -> sd, SplineKnots -> knots],
Red, PointSize[Medium], Point[pts]}]
]


Test

pts =
{{-1.5, -2}, {-3, -1}, {-2.7, 0.5}, {-1.75, 1.3}, {0.8, -1.5},
{1.5, 0.4}, {0, 2}, {3, 2}};
BSplineInterpolation[pts] Obviously, this curve is not continuous and it doesn't pass all the interpolation points.

another trial(Namely, deleteing the option SplineKnots -> knots) ### Question

I have searched the paper about closed curve interpoaltion by the Google Scholar, however, I didn't discovered any helpful reference.

• How to achieve a continuous closed B-spline curve?
• I have the same issue. Do you find any solution? Apr 25 at 5:41

It looks as though your first example is working, except for the edge-case of connecting the first and last points. An easy solution, if it works, would be to repeat the first point in pts at the end.