Tuesday, November 9, 2010

Reticulating Splines

I now have piece-wise Bezier splines that interpolate input points! I ran through many tutorials and equations to try and find a general form the produced these results, but nothing seemed to work.

What I finally ended up doing was based off of a heuristic I developed.

Initial Thought
My initial idea was to specify an "input tangent" (in the base case being the direction vector between the first two points) and an "output tangent" being parallel to the chord stretched between the previous point and the next point (thus making the tangent at the current point this line).

The final "output tangent" is simply defined as the vector from the second to last point and the last point. This takes care of all the necessary constraints to select a single spline from the infinite possibilities.

When these tangents have a positive dot product, you can construct an intersection point (making a triangle) using the previous and current points and the entrance and exit tangents. Then, to pick arbitrary piece-wise Bezier control points, just scale along the triangle's created sides by an arbitrary "smoothing factor". The larger the scalar, the closer the points are to the tip of the triangle (and intersection point), the smaller the scalar, the closer the Bezier curve is to a line.

This runs into a problem with the dot product between the two tangents is negative, the intersection of the points causes cusps in the spline. To solve this, I originally special-cased these segments by scaling along the entrance and exit tangents by the smooth factor, scaled by the distance between the points. I figured this heuristic made as much sense as any, even though it was arbitrary.

Realization
When I was describing my algorithm to Chris Peters (an instructor of mine), he noted that I could just do that simplistic heuristic for all points and avoid the parallel tangents case altogether.

I'm super excited, because my results look awesome, and are easily tweaked with a single magic number (that works best at 0.3)

Interpolating Bezier spline with smoothing = 0.3

As the smoothing approaches 0, the interpolating spline becomes the line segments between the points.

As the smoothing approaches 1 (each tangent being the whole distance between the points), generally undesirable results occur, but I left this possibility in for the user to decide.

The spline from the first picture without the shells.

1 comment: