11

This works (SplineDegree -> 2)

ParametricPlot[BezierFunction[{{0, 0}, {1, 0.5}, {2, 2}}, SplineDegree -> 2][u], {u, 0, 1}]

BezierFunction SplineDegree 2

but this does not (SplineDegree -> 1)

ParametricPlot[BezierFunction[{{0, 0}, {1, 0.5}, {2, 2}}, SplineDegree -> 1][u], {u, 0, 1}]

BezierFunction::invdeg: Value of option SplineDegree -> 1 should be a positive integer, or a list of positive integers

I found no reference to a possible issue in the documentation for BezierFunction.

SplineDegree -> 1 works fine with BSplineFunction and BezierCurve; shouldn't it also work with BezierFunction?

(Mma 8.0.4 Mac OSX)

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
JxB
  • 5,111
  • 1
  • 23
  • 40
  • 4
    The only value that seems to work for an arbitrary list of values seems to be the length of the list minus 1. – Heike Jul 11 '12 at 16:21
  • 5
    When you look at the definition of Bézier curves you can see why the degree of the polynomial has to be equal to the number of points minus one, like Heike pointed out. The error message could be more helpful here, though. – Thies Heidecke Jul 11 '12 at 18:55
  • 3
    @ThiesHeidecke And yet SplineDegree->1 works for BezierCurve[]? You get straight lines when you put that in a Graphics[]. What am I missing? – JxB Jul 11 '12 at 19:01
  • 3
    @ThiesHeidecke And moreover, the option SplineDegree which is explicitly mentioned in the docs for BezierFunction is then completely misleading because it suggests that the function works just like BezierCurve. So my guess is that they did mean to make these two functions work the same way, but forgot to partition the argument lis of BezierFunction according to the SplineDegree when it's explicitly given. – Jens Jul 11 '12 at 19:01
  • Yes, this could definitely be improved on Wolfram's side. It is confusing as it is at the moment. – Thies Heidecke Jul 11 '12 at 19:06

2 Answers2

12

Reason for incompatibility

Yes, the doc shouldn't mention that it is fully compatible (that's an oversight...). BSplineFunction and BSplineCurve are fully compatible as far as I remember, but not BezierCurve and BezierFunction.

The reason is that what BezierCurve is doing when it has more than d+1 control points for SplineDegree->d case is something called composite Bezier curve. Essentially, the remainder of control points are grouped in d points, and creating degree d spline by carrying the last point from the previous curve.

pts = {{0, 0}, {1, 1}, {2, -1}, {3, 0}, {4, 2}, {6, -1}, {7, 3}, {8, -1}, {9, 1}, {10, -2}};

Graphics[{BezierCurve[pts], Green, Line[pts], Red, Point[pts]}]

composite bezier

It is a convenient hack that graphics / CAD people has been using for a long time, and it makes sense when you are creating multiple continuous curve segments.

not c1

But the problem with it for BezierFunction is that:

  1. it does not guarantee the continuous derivative at the connection points (which are marked with blue circles). It only guarantees $C^0$:

  2. Another problem is parametrization. Should parameters run between 0 to 1 (by uniformly dividing by the number of curve segments), or what... None of this is a problem for graphics primitives.

  3. This idea is not working well in multi-dimensional case (it can be extended, but no one uses it and has no mathematical meaning).

  4. There are few mathematical ways to make the connection $C^d$ when $d$ is the degree, nonetheless (introducing intermediate control points or using smoothing function), but then it is not matching with BezierCurve either.

So, we decided not to support it.

Work around

  1. SplineDegree->1 case: Just use BSplineFunction. It is exactly the same parametrization, running between 0 to 1, uniformly divided by the number of line segments.

  2. SplineDegree->d where d > 1: CompositeBezierFunction code will be provided.

Yu-Sung Chang
  • 7,061
  • 1
  • 39
  • 31
1

I've made a function like what @yu-sung-chang was suggesting.

compositeBezierPoints[bezierCurvePoints_, degree_Integer] :=
Select[
  Partition[bezierCurvePoints, UpTo[degree + 1], 1][[1 ;; -1 ;; degree]],
  Length[#] > 1 &]

compositeBezierPoints[bezierCurvePoints_, 1] := Select[ Partition[bezierCurvePoints, UpTo[2], 1], Length[#] > 1 &]

If you input the points of the BezierCurve, and the degree (Automatic is degree 3), then compositeBezierPoints breaks up the points into a new set of points expected by BezierFunction. You would map BezierFunction over this list to get a list of functions that are each parameterized from 0 to 1. Here's a Manipulate showing the equivalence for a range of degrees:

pts = Table[{i, RandomReal[{-10, 10}]}, {i, 15}];
Manipulate[
  Show[
    Graphics[{Red, Point[pts]}], 
    ParametricPlot[
      Evaluate[Through[(BezierFunction /@ compositeBezier[pts, d])[t]]],
      {t, 0, 1}], 
    Graphics[{Black, Dashed, BezierCurve[pts, SplineDegree -> d]}]],
  {d, 1, 6, 1}]

You can re-parameterize the composite parts into a Piecewise function that itself runs from 0 to 1. I use BezierSymbolicFunction (found somewhere on MSE) to get the arc lengths in order to scale the segments:

BezierSymbolicFunction[pts_?MatrixQ] :=
Function[Evaluate[Sum[pts[[i+1]] BernsteinBasis[Length[pts]-1, i, #], {i, 0, Length[pts]-1}]]]

makeBezierPiecewise[bezierCurvePoints_, degree_Integer] := Module[{symbolicSegments, bezierSegments, lengths, ends}, symbolicSegments = BezierSymbolicFunction /@ compositeBezier[bezierCurvePoints, degree]; bezierSegments = BezierFunction /@ compositeBezier[bezierCurvePoints, degree]; lengths = ArcLength[#[t], {t, 0, 1}]& /@ symbolicSegments; ends = Prepend[Accumulate[lengths/Total[lengths]], 0.]; Function[Evaluate[ Piecewise[ Table[ { bezierSegments[[i]][(#-ends[[i]])/(ends[[i+1]] - ends[[i]])], ends[[i]] < # <= ends[[i+1]]}, {i, 1, Length[ends]-1}]]]]]

kevind
  • 41
  • 2