## Slightly Better Iterative Spline Decomposition

My colleague Bart Massey (who is a CS professor at
Portland State University)
reviewed my iterative spline algorithm
article and had an insightful comment — we don't just want any spline
decomposition which is flat enough, what we really want is a
decomposition for which every line segment is *barely* within the
specified flatness value.

My initial approach was to keep halving the length of the spline segment until it was flat enough. This definitely generates a decomposition which is flat enough everywhere, but some of the segments will be shorter than they need to be, by as much as a factor of two.

As we'll be taking the resulting spline and doing a lot more computation with each segment, it makes sense to spend a bit more time finding a decomposition with fewer segments.

### The Initial Search

Here's how the first post searched for a 'flat enough' spline section:

```
t = 1.0f;
/* Iterate until s1 is flat */
do {
t = t/2.0f;
_de_casteljau(s, s1, s2, t);
} while (!_is_flat(s1));
```

### Bisection Method

What we want to do is find an approximate solution for the function:

```
flatness(t) = tolerance
```

We'll use the Bisection method to find the value of t for which the flatness is no larger than our target tolerance, but is at least as large as tolerance - ε, for some reasonably small ε.

```
float hi = 1.0f;
float lo = 0.0f;
/* Search for an initial section of the spline which
* is flat, but not too flat
*/
for (;;) {
/* Average the lo and hi values for our
* next estimate
*/
float t = (hi + lo) / 2.0f;
/* Split the spline at the target location
*/
_de_casteljau(s, s1, s2, t);
/* Compute the flatness and see if s1 is flat
* enough
*/
float flat = _flatness(s1);
if (flat <= SCALE_FLAT(SNEK_DRAW_TOLERANCE)) {
/* Stop looking when s1 is close
* enough to the target tolerance
*/
if (flat >= SCALE_FLAT(SNEK_DRAW_TOLERANCE - SNEK_FLAT_TOLERANCE))
break;
/* Flat: t is the new lower interval bound */
lo = t;
} else {
/* Not flat: t is the new upper interval bound */
hi = t;
}
}
```

This searches for a place to split the spline where the initial
portion is flat but not *too* flat. I set SNEK_FLAT_TOLERANCE to 0.01, so
we'll pick segments which have flatness between 0.49 and 0.50.

The benefit from the search is pretty easy to understand by looking at
the number of points generated compared with the number of
`_de_casteljau`

and `_flatness`

calls:

Search | Calls | Points |
---|---|---|

Simple | 150 | 33 |

Bisect | 229 | 25 |

And here's an image comparing the two:

### A Closed Form Approach?

Bart also suggests attempting to find an analytical solution to decompose the spline. What we need is to is take the flatness function and find the spline which makes it equal to the desired flatness. If the spline control points are a, b, c, and d, then the flatness function is:

```
ux = (3×b.x - 2×a.x - d.x)²
uy = (3×b.y - 2×a.y - d.y)²
vx = (3×c.x - 2×d.x - a.x)²
vy = (3×c.y - 2×d.y - a.y)²
flat = max(ux, vx) + max(uy, vy)
```

When the spline is split into two pieces, all of the control points for the new splines are determined by the original control points and the 't' value which sets where the split happens. What we want is to find the 't' value which makes the flat value equal to the desired tolerance. Given that the binary search runs De Casteljau and the flatness function almost 10 times for each generated point, there's a lot of opportunity to go faster with a closed form solution.

## Update: Fancier Method Found!

Bart points me at two papers:

- Flattening quadratic Béziers by Raph Levien
- Precise Flattening of Cubic Bézier Segments by Thomas F. Hain, Athar L. Ahmad, and David D. Langan

Levien's paper offers a great solution for quadratic Béziers by directly computing the minimum set of line segments necessary to approximate within a specified flatness. However, it doesn't generalize to cubic Béziers.

Hain, Ahmad and Langan *do* provide a directly computed decomposition
of a cubic Bézier. This is done by constructing a parabolic
approximation to the first portion of the spline and finding a 't'
value which produces the desired flatness. There are a pile of special
cases to deal with when there isn't a good enough parabolic
approximation. But, overall computational cost is lower than a
straightforward binary decomposition, plus there's no recursion
required.

This second algorithm has the same characteristics as my Bisection method as the last segment may have any flatness from zero through the specified tolerance; Levien's solution is neater in that it generates line segments of similar flatness across the whole spline.

## Current Implementation

```
/*
* Copyright © 2020 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
typedef float point_t[2];
typedef point_t spline_t[4];
uint64_t num_flats;
uint64_t num_points;
#define SNEK_DRAW_TOLERANCE 0.5f
#define SNEK_FLAT_TOLERANCE 0.01f
/*
* This actually returns flatness² * 16,
* so we need to compare against scaled values
* using the SCALE_FLAT macro
*/
static float
_flatness(spline_t spline)
{
/*
* This computes the maximum deviation of the spline from a
* straight line between the end points.
*
* From https://hcklbrrfnn.files.wordpress.com/2012/08/bez.pdf
*/
float ux = 3.0f * spline[1][0] - 2.0f * spline[0][0] - spline[3][0];
float uy = 3.0f * spline[1][1] - 2.0f * spline[0][1] - spline[3][1];
float vx = 3.0f * spline[2][0] - 2.0f * spline[3][0] - spline[0][0];
float vy = 3.0f * spline[2][1] - 2.0f * spline[3][1] - spline[0][1];
ux *= ux;
uy *= uy;
vx *= vx;
vy *= vy;
if (ux < vx)
ux = vx;
if (uy < vy)
uy = vy;
++num_flats;
/*
*If we wanted to return the true flatness, we'd use:
*
* return sqrtf((ux + uy)/16.0f)
*/
return ux + uy;
}
/* Convert constants to values usable with _flatness() */
#define SCALE_FLAT(f) ((f) * (f) * 16.0f)
/*
* Linear interpolate from a to b using distance t (0 <= t <= 1)
*/
static void
_lerp (point_t a, point_t b, point_t r, float t)
{
int i;
for (i = 0; i < 2; i++)
r[i] = a[i]*(1.0f - t) + b[i]*t;
}
/*
* Split 's' into two splines at distance t (0 <= t <= 1)
*/
static void
_de_casteljau(spline_t s, spline_t s1, spline_t s2, float t)
{
point_t first[3];
point_t second[2];
int i;
for (i = 0; i < 3; i++)
_lerp(s[i], s[i+1], first[i], t);
for (i = 0; i < 2; i++)
_lerp(first[i], first[i+1], second[i], t);
_lerp(second[0], second[1], s1[3], t);
for (i = 0; i < 2; i++) {
s1[0][i] = s[0][i];
s1[1][i] = first[0][i];
s1[2][i] = second[0][i];
s2[0][i] = s1[3][i];
s2[1][i] = second[1][i];
s2[2][i] = first[2][i];
s2[3][i] = s[3][i];
}
}
/*
* Decompose 's' into straight lines which are
* within SNEK_DRAW_TOLERANCE of the spline
*/
static void
_spline_decompose(void (*draw)(float x, float y), spline_t s)
{
/* Start at the beginning of the spline. */
(*draw)(s[0][0], s[0][1]);
/* Split the spline until it is flat enough */
while (_flatness(s) > SCALE_FLAT(SNEK_DRAW_TOLERANCE)) {
spline_t s1, s2;
float hi = 1.0f;
float lo = 0.0f;
/* Search for an initial section of the spline which
* is flat, but not too flat
*/
for (;;) {
/* Average the lo and hi values for our
* next estimate
*/
float t = (hi + lo) / 2.0f;
/* Split the spline at the target location
*/
_de_casteljau(s, s1, s2, t);
/* Compute the flatness and see if s1 is flat
* enough
*/
float flat = _flatness(s1);
if (flat <= SCALE_FLAT(SNEK_DRAW_TOLERANCE)) {
/* Stop looking when s1 is close
* enough to the target tolerance
*/
if (flat >= SCALE_FLAT(SNEK_DRAW_TOLERANCE - SNEK_FLAT_TOLERANCE))
break;
/* Flat: t is the new lower interval bound */
lo = t;
} else {
/* Not flat: t is the new upper interval bound */
hi = t;
}
}
/* Draw to the end of s1 */
(*draw)(s1[3][0], s1[3][1]);
/* Replace s with s2 */
memcpy(&s[0], &s2[0], sizeof (spline_t));
}
/* S is now flat enough, so draw to the end */
(*draw)(s[3][0], s[3][1]);
}
void draw(float x, float y)
{
++num_points;
printf("%8g, %8g\n", x, y);
}
int main(int argc, char **argv)
{
spline_t spline = {
{ 0.0f, 0.0f },
{ 0.0f, 256.0f },
{ 256.0f, -256.0f },
{ 256.0f, 0.0f }
};
_spline_decompose(draw, spline);
fprintf(stderr, "flats %lu points %lu\n", num_flats, num_points);
return 0;
}
```