Home

Slightly better interpolating spline.

Today I'm introducing a new interpolating spline. Its name is S9. Without further demagoguery, here it is -

You can click on knots and drag to see how this thing works.

For comparison with other splines, here is a demo. And here you can find the implementation source.

Why automatic spline at all?

For decades people were used to manually clicking and dragging cubic Bézier points until they developed Stockholm syndrome for it. Sometimes experienced designers will even tell you about having some magic rules like "keeping control arm lengths to about a third of chord length" which is good advice, but only if no better option is available. The fact is, humans cannot manually come up with good looking sequences of curves. The problem is inability to see subtle variations in the curvature of polynomial curves.

So what is this curvature anyway? The best way to explain is using a related concept called osculating circle. It is an approximation of how the curve is bending at any point on it. Take a look at this demo -

If you play with this for long enough, you will see a pattern. If a circle size along the curve changes very little, the curve looks better. If it changes a lot, the curve looks worse. This smoothness of how the circle changes along the curve is called curvature variation. Less variation is better.

We can visualise this in a different way. If we record the reciprocal of osculating circle radius on every step of the curve, we get curvature profile of a curve.

Now you can see another pattern. If this plot is smooth, the curve also looks smooth.

However, it can be very difficult to see how smooth or not smooth this blue function is. Especially in absence of large spikes and multiple turns. Instead, let me show you another related plot. Here, in red, is a plot of a function representing curvature variation of the curve. And the rule is simple - thinner this plot gets, less curvature variation the curve has. We want the red part to be as close to the horizontal grey line as possible. For example, a section of a circle will have an integral (red number next to function plot) of this function equal to zero.

As you can see, it can be quite hard to get this red function to relax. But it can still be worse though. This is just one segment. How about we connect two curves?

See now you have to juggle more than two balls - you need to make individual curves smooth, but also keep the connection between the curves continuous too. In other words, the blue function must not have any gaps and the red function must stay as flat as possible.

This gets worse with every additional curve because polynomial curves have this property where changing one side of it will change curvature on the other side as well.

At this point you should see that trying to get harmony out of 20 or 30 connected curves has the same entertainment value as colonoscopy. This is what splines solve. Automatic smoothness of individual segments and continuity between curves.

The goal.

The goal for S9 is to look good and to adapt to changes smoothly so it is useful in interactive CAD applications. This requires the following -

The primary goal is roundness. I want a spline which feels natural for forming fundamentally round and smooth objects.

What are the current best splines?

At the moment there are very few splines that are optimized for the same goal as S9. In my opinion, the best published G2-continuous interpolating spline is this one. In fact, it is very good, only using one cubic curve to connect a pair of knots. I doubt you can do much better than this with a single cubic. Second best is κ-Curves, which is implemented in Adobe Illustrator as a "Curvature" tool. Using quadratic curves, but has a few fundamental problems where the solver can fail without special case handling. Then there is Hobby spline. A very old one. It does not guarantee G2 continuity, but the solver is much simpler. Apple Keynote and some other applications use it.

There are some other, more famous splines like Catmull-Rom. I'm sure they have use cases even today, but they have no use in any even remotely serious CAD application. In fact, I would argue that any locally solved spline would have a flaw somewhere that cannot be fixed.

Some facts about the spline.

I decided to call this spline S9 because it is my 9th attempt to come up with a decent spline. I still have the first 8.

How does it work?

Fundamentally this spline works in a similar way to Hobby spline. It has two separate elements to it -

The solver part is not a problem. The issue is how to come up with a curve parametrized by two angles. And it is harder than it looks.

The curve.

The final and the most important part is the curve this spline is made of. After years of attempts, the best I was able to do is to describe the curve as a function of arc length.

For greek letter connoisseurs, here is an implementation of the curve in Desmos graphing calculator. That thing is powerful.

Curve math is explained in detail in comments for S9 curve. Weight computation rules are sprinkled around in the solver.

The curve solver is given two tangent angles and two weight parameters which are somewhat abstract, but the idea is as follows -

For two weights of 1, the curve is identical to the Euler spiral. This formula also has extra knobs to control curvature change near ends when weights decrease.

Weights are calculated from start and end angles. Which, like velocity function for Hobby spline, reduces a number of free parameters to two.

So what does this curve look like?

Input is two tangent angles you can adjust with sliders.

This curve, when used with automatic weight computation, will form high curvature spikes at the ends for large angles. This is a requirement for this type of spline. But for low to medium angles, it does have very little curvature variation which was the goal.

Final remarks.

The source repository includes implementation of both curve solver and spline solver. However, it is not meant to be a full implementation of path construction solution for CAD. For example, it does not include G2-continuous transitions from straight to curved segments, sharp corners or angle locking. These features would be independent additions.

Further reading.

Here is a minimal list of resources for anyone interested in curves and splines -