In graphics kindergarten, you’re always taught about Bezier curves. We’re taught that we can use the Decastlejau algorithm to evaluate them, and there’s something-or-other in there about the Bernstein basis polynomials. You’re also taught about Hermite curves, B-spline curves, Catmull-Rohm, Kochanek-Bartels, and other stuffs, but what seems to be lesser known is the fact that these guys are all interchangable. Beziers are to curves as triangles are to everything else. They have some useful properties, and you can always convert to them.
In this educational post, I’m going to go through the recipe for converting from one type of (uniform, non-rational) cubic spline to any of the others. Eventually, you will come to understand why you only ever need one curve-drawing routine (unless you’re especially paranoid about precision).
Let’s Review
A Bezier curve is a curve that magically appears between any four points. We have a start point, and end point, and two other points that control the curve’s shape.
The Bezier curve can be elegantly evaluated by using the Decastlejau algorithm, which boils down to repeatedly interpolating between control points. First, we compute interpolated points on each of the three segments, then we construct two segments between the three result points and lerp on those, and repeat, until we’re down to just one point.
A useful fact about the Bezier curve is that it can be recursively split. We can subdivide a bezier into two smaller Beziers by evaluating it at 0.5 and using some of the intermediate results from Decastlejau as the new control points:
One final useful fact about Beziers is that the curve is always fully contained within the convex hull of the four control points. This fact is very useful for culling, and it can also be used to do adaptive drawing by subdividing until the size of the convex hull sinks below a suitable threshold.
If you paid attention during graphics kindergarten, you should have learnt that the Bezier curve can be written as a weighted combination of the Bernstein basis polynomials:
Where:
In case you were wondering, if you take the lerps I wrote above, expand them all out, and then simplify, you will eventually get the above equation. I’ll leave that as an exercise to the reader. I’m going to start referring to the Bernstein basis as the “just lerp it” basis.
A Hermite spline is defined using a different basis, with the added wrinkle that two of the “control points” are really vectors:
Where:
Hermite splines are kindof useless by themselves, but they’re a useful building block. If you string together a series of Hermite splines with consistent tangents at matching endpoints, you have the venerable Catmull-Rohm spline. Kochanek and Bartels took this even further by extra parameters for tension, continuity, and bias. Those extra parameters end up being simple scale factors that get applied to the control points segment by segment.
A (uniform) B-spline is defined using yet another basis:
Where:
These little fellas are special in that piecewise B-splines are always C2 continuous.
They’re All The Same. Really…
I just told you that these are all the same thing, so you might be skeptical given how different they look. Here’s the trick: Let’s look at what happens when we expand everything out in the Bezier case:
Where:
So:
At the end of the day, these things are all just cubic polynomials, and they can be contorted back into the form we all learned about in high school algebra. If we liked, we could pre-compute the coefficients of this cubic polynomial and use these instead of the bezier control points. We’d get exactly the same curve, but we’d no longer have the nice properties that Beziers give us, which is why nobody actually does it that way.
Now for the secret sauce…. If we do all of that algebra and then squint a little, we’ll find that we can rewrite each component of the bezier curve as a matrix equation, like so:
We have a control point vector on the right, a parameter vector on the left, and a basis matrix in the middle. We can do the same thing for the curve types too. For Hermite, we’d get:
And for B-splines:
All of the different cubics all do the same thing. They just use a different basis matrix to convert from control vertices into natural spline coefficients. This means that as long as we can tolerate a little roundoff error, we can always convert a given curve segment to Bezier form.
Suppose we have a set of Hermite control points (endpoints and tangents), and we want to find the corresponding Bezier points. All we have to do is set the curves equal to each other and do some linear algebra:
Here we have the recipe for converting from any sort of cubic curve into a Bezier. Take the basis matrix for whatever type of curve it is (Hermite, B-spline, whatever), multiply on the left by the inverse Bezier basis matrix, which is:
This gives a conversion matrix which we then multiply the control points by, one coordinate at a time. So, there you have it. If you can use a Bezier, you can use everything else.
Wumpf
It is much easier if express all your polynomial curves in the Polar Form. (Has nothing to do with spherical/polar coordinates! It is sometimes also called “Blossoming”)
Sadly there are not many good resources out there. The original work (http://www.hpl.hp.com/techreports/Compaq-DEC/SRC-RR-19.pdf) is quite involved. This might be a better starting point: http://www.cs.columbia.edu/~cs4160/slides/hanspeter.pdf
Simon
Thanks! Great read & useful to understand.