lyon_bezier 0.4.1

2d quadratic and cubic bezier curve math, featuring an efficient flattening algorithm.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
///! Utilities to flatten cubic bezier curve segments, implmeneted both with callback and
///! iterator based APIs.
///!
///! The algorithm implemented here is based on:
///! http://cis.usouthal.edu/~hain/general/Publications/Bezier/Bezier%20Offset%20Curves.pdf
///! It produces a better approximations than the usual recursive subdivision approach (or
///! in other words, it generates less points for a given tolerance threshold).

use super::{ Point, CubicBezierSegment };

use std::f32;
use std::mem::swap;

/// An iterator over a cubic bezier segment that yields line segments approximating the
/// curve for a given approximation threshold.
///
/// The iterator starts at the first point *after* the origin of the curve and ends at the
/// destination.
pub struct CubicFlatteningIter {
    curve: CubicBezierSegment,
    sub_curve: Option<CubicBezierSegment>,
    next_inflection_start: f32,
    next_inflection_end: f32,
    following_inflection_start: f32,
    following_inflection_end: f32,
    num_inflecions: i32,
    tolerance: f32,
}

fn clamp(a: f32, min: f32, max: f32) -> f32 {
    if a < min { return min; }
    if a > max { return max; }
    return a;
}

impl CubicFlatteningIter {
    /// Creates an iterator that yields points along a cubic bezier segment, useful to build a
    /// flattened approximation of the curve given a certain tolerance.
    pub fn new(bezier: CubicBezierSegment, tolerance: f32) -> Self {
        let (first_inflection, second_inflection) = find_cubic_bezier_inflection_points(&bezier);
        let num_inflections = if first_inflection.is_none() { 0 } else if second_inflection.is_none() { 1 } else { 2 };
        let first_inflection = first_inflection.unwrap_or(-1.0);
        let second_inflection = second_inflection.unwrap_or(-1.0);

        let mut iter = CubicFlatteningIter {
            curve: bezier,
            sub_curve: None,
            next_inflection_start: f32::NAN,
            next_inflection_end: f32::NAN,
            following_inflection_start: f32::NAN,
            following_inflection_end: f32::NAN,
            num_inflecions: num_inflections,
            tolerance: tolerance
        };

        if iter.num_inflecions == 0 {
            iter.sub_curve = Some(bezier);
            return iter;
        }

        // Calulate the range where the inflection can be linearly approximated
        find_cubic_bezier_inflection_approximation_range(
            &bezier, first_inflection, tolerance,
            &mut iter.next_inflection_start,
            &mut iter.next_inflection_end
        );

        iter.next_inflection_start = clamp(iter.next_inflection_start, 0.0, 1.0);
        iter.next_inflection_end = clamp(iter.next_inflection_end, 0.0, 1.0);

        if iter.num_inflecions == 2 {
            find_cubic_bezier_inflection_approximation_range(
                &bezier, second_inflection, tolerance,
                &mut iter.following_inflection_start,
                &mut iter.following_inflection_end
            );
            iter.following_inflection_start = clamp(iter.following_inflection_start, 0.0, 1.0);
            iter.following_inflection_end = clamp(iter.following_inflection_end, 0.0, 1.0);
        }

        if iter.next_inflection_start > 0.0 {
            iter.sub_curve = Some(bezier.before_split(iter.next_inflection_start));
        }

        return iter;
    }
}

impl Iterator for CubicFlatteningIter {
    type Item = Point;
    fn next(&mut self) -> Option<Point> {
        if let Some(sub_curve) = self.sub_curve {
            // We are iterating over a sub-curve that does not have inflections.
            let t = no_inflection_flattening_step(&sub_curve, self.tolerance);
            if t >= 1.0 {
                let to = sub_curve.to;
                self.sub_curve = None;
                return Some(to);
            } else {
                let next_curve = sub_curve.after_split(t);
                self.sub_curve = Some(next_curve);
                return Some(next_curve.from);
            }
        }

        if self.num_inflecions > 0 {
            // We are at the beginning of an inflection range which is approximated with a line
            // segment.
            let current_range_end = self.next_inflection_end;

            // Pop the inflection range.
            self.num_inflecions -= 1;
            self.next_inflection_start = self.following_inflection_start;
            self.next_inflection_end = self.following_inflection_end;

            // If the range doesn't extend all the way to the end of the curve, prepare the next
            // sub-curve portion that we are going to iterator over.
            if current_range_end < 1.0 {
                let mut next_curve = self.curve.after_split(current_range_end);
                if self.num_inflecions > 0 && current_range_end < self.next_inflection_start {
                    next_curve = next_curve.before_split(self.next_inflection_start);
                }
                self.sub_curve = Some(next_curve);
            }

            return Some(self.curve.sample(current_range_end));
        }

        return None;
    }
}

pub fn flatten_cubic_bezier<F: FnMut(Point)>(
    bezier: CubicBezierSegment,
    tolerance: f32,
    call_back: &mut F
) {
    let (first_inflection, second_inflection) = find_cubic_bezier_inflection_points(&bezier);
    let num_inflections = if first_inflection.is_none() { 0 } else if second_inflection.is_none() { 1 } else { 2 };
    let first_inflection = if let Some(t) = first_inflection { t } else { -1.0 };
    let second_inflection = if let Some(t) = second_inflection { t } else { -1.0 };

    if num_inflections == 0 {
        flatten_cubic_no_inflection(bezier, tolerance, call_back);
        return;
    }

    let mut first_inflection_start = first_inflection;
    let mut first_inflection_end = first_inflection;
    let mut second_inflection_start = second_inflection;
    let mut second_inflection_end = second_inflection;

    // For both inflection points, calulate the range where they can be linearly approximated if
    // they are positioned within [0, 1]
    if num_inflections > 0 {
        find_cubic_bezier_inflection_approximation_range(
            &bezier, first_inflection, tolerance,
            &mut first_inflection_start,
            &mut first_inflection_end
        );
    }

    if num_inflections == 2 {
        find_cubic_bezier_inflection_approximation_range(
            &bezier, second_inflection, tolerance,
            &mut second_inflection_start,
            &mut second_inflection_end
        );
    }

    // Process ranges. [first_inflection_start, first_inflection_end] and
    // [second_inflection_start, second_inflection_end] are approximated by line segments.
    if num_inflections == 1 && first_inflection_start <= 0.0 && first_inflection_end >= 1.0 {
        // The whole range can be approximated by a line segment.
        call_back(bezier.to);
        return;
    }

    if first_inflection_start > 0.0 {
        // Flatten the Bezier up until the first inflection point's approximation
        // point.
        flatten_cubic_no_inflection(
            bezier.before_split(first_inflection_start),
            tolerance,
            call_back
        );

        if first_inflection_end >= 1.0 {
            call_back(bezier.to);
            return;
        }
    }

    if first_inflection_end >= 0.0 && first_inflection_end < 1.0
    && (num_inflections == 1 || second_inflection_start > first_inflection_end) {
        // The second inflection point's approximation range begins after the end
        // of the first, approximate the first inflection point by a line and
        // subsequently flatten up until the end or the next inflection point.
        let next_bezier = bezier.after_split(first_inflection_end);

        call_back(next_bezier.from);

        if num_inflections == 1 || (num_inflections > 1 && second_inflection_start >= 1.0) {
            // No more inflection points to deal with, flatten the rest of the curve.
            flatten_cubic_no_inflection(next_bezier, tolerance, call_back);
            return;
        }
    } else if num_inflections > 1 && second_inflection_start > 1.0 {
        // We've already concluded second_inflection_start <= first_inflection_end, so if this is
        // true the approximation range for the first inflection point runs past the end of the
        // curve, draw a line to the end and we're done.
        call_back(bezier.to);
        return;
    }

    if num_inflections > 1 && second_inflection_start < 1.0 && second_inflection_end > 0.0 {
        if second_inflection_start > 0.0 && second_inflection_start < first_inflection_end {
            // In this case the second_inflection approximation range starts inside the
            // first inflection's approximation range.
            call_back(bezier.sample(first_inflection_end));
        } else if second_inflection_start > 0.0 && first_inflection_end > 0.0 {
            let next_bezier = bezier.after_split(first_inflection_end);

            // Find a control points describing the portion of the curve between
            // first_inflection_end and second_inflection_start.
            let second_inflection_start = (second_inflection_start - first_inflection_end) / (1.0 - first_inflection_end);
            flatten_cubic_no_inflection(
                next_bezier.before_split(second_inflection_start),
                tolerance,
                call_back
            );
        } else if second_inflection_start > 0.0 {
            // We have nothing interesting before second_inflection_start, find that bit and
            // flatten it.
            flatten_cubic_no_inflection(
                bezier.before_split(second_inflection_start),
                tolerance,
                call_back
            );
        }

        if second_inflection_end < 1.0 {
            // Flatten the portion of the curve after second_inflection_end
            let next_bezier = bezier.after_split(second_inflection_end);

            // Draw a line to the start, this is the approximation between second_inflection_start and second_inflection_end.
            call_back(next_bezier.from);
            flatten_cubic_no_inflection(next_bezier, tolerance, call_back);
        } else {
            // Our approximation range extends beyond the end of the curve.
            call_back(bezier.to);
        }
    }
}

// The algorithm implemented here is based on:
// http://cis.usouthal.edu/~hain/general/Publications/Bezier/Bezier%20Offset%20Curves.pdf
//
// The basic premise is that for a small t the third order term in the
// equation of a cubic bezier curve is insignificantly small. This can
// then be approximated by a quadratic equation for which the maximum
// difference from a linear approximation can be much more easily determined.
fn flatten_cubic_no_inflection<F: FnMut(Point)>(
    mut bezier: CubicBezierSegment,
    tolerance: f32,
    call_back: &mut F
) {
    let end = bezier.to;

    let mut t = 0.0;
    while t < 1.0 {
        t = no_inflection_flattening_step(&bezier, tolerance);

        if t == 1.0 {
            break
        }
        bezier = bezier.after_split(t);
        call_back(bezier.from);
    }

    call_back(end);
}

fn no_inflection_flattening_step(
    bezier: &CubicBezierSegment,
    tolerance: f32,
) -> f32 {
    let v1 = bezier.ctrl1 - bezier.from;
    let v2 = bezier.ctrl2 - bezier.from;

    // To remove divisions and check for divide-by-zero, this is optimized from:
    // Float s2 = (v2.x * v1.y - v2.y * v1.x) / hypot(v1.x, v1.y);
    // t = 2 * Float(sqrt(tolerance / (3. * abs(s2))));
    let v1_cross_v2 = v2.x * v1.y - v2.y * v1.x;
    let h = v1.x.hypot(v1.y);
    if v1_cross_v2 * h == 0.0 {
        return 1.0;
    }
    let s2inv = h / v1_cross_v2;

    let t = 2.0 * (tolerance * s2inv.abs() / 3.0).sqrt();

    // TODO: We start having floating point precision issues if this constant
    // is closer to 1.0 with a small enough tolerance threshold.
    if t >= 0.995 {
        return 1.0;
    }

    return t;
}

// Find the inflection points of a cubic bezier curve.
fn find_cubic_bezier_inflection_points(
    bezier: &CubicBezierSegment,
) -> (Option<f32>, Option<f32>) {
    // Find inflection points.
    // See www.faculty.idc.ac.il/arik/quality/appendixa.html for an explanation
    // of this approach.
    let pa = bezier.ctrl1 - bezier.from;
    let pb = bezier.ctrl2 - (bezier.ctrl1 * 2.0) + bezier.from;
    let pc = bezier.to - (bezier.ctrl2 * 3.0) + (bezier.ctrl1 * 3.0) - bezier.from;

    let a = pb.x * pc.y - pb.y * pc.x;
    let b = pa.x * pc.y - pa.y * pc.x;
    let c = pa.x * pb.y - pa.y * pb.x;

    if a == 0.0 {
        // Not a quadratic equation.
        if b == 0.0 {
            // Instead of a linear acceleration change we have a constant
            // acceleration change. This means the equation has no solution
            // and there are no inflection points, unless the constant is 0.
            // In that case the curve is a straight line, essentially that means
            // the easiest way to deal with is is by saying there's an inflection
            // point at t == 0. The inflection point approximation range found will
            // automatically extend into infinity.
            if c == 0.0 {
               return (Some(0.0), None);
            }
            return (None, None);
        }
        return (Some(-c / b), None);
    }

    fn in_range(t: f32) -> Option<f32> { if t >= 0.0 && t < 1.0 { Some(t) } else { None } }

    let discriminant = b * b - 4.0 * a * c;

    if discriminant < 0.0 {
        return (None, None);
    }

    if discriminant == 0.0 {
        return (in_range(-b / (2.0 * a)), None);
    }

    let discriminant_sqrt = discriminant.sqrt();
    let q = if b < 0.0 { b - discriminant_sqrt } else { b + discriminant_sqrt } * -0.5;

    let mut first_inflection = q / a;
    let mut second_inflection = c / q;
    if first_inflection > second_inflection {
        swap(&mut first_inflection, &mut second_inflection);
    }

    let first_inflection = in_range(first_inflection);
    let second_inflection = in_range(second_inflection);

    if first_inflection.is_none() {
        return (second_inflection, None);
    }

    return (first_inflection, second_inflection);
}

fn cubic_root(val: f32) -> f32 {
    if val < 0.0 {
        return -cubic_root(-val);
    }

    return val.powf(1.0 / 3.0);
}

// Find the range around a point where the curve can be approximated with a line segment, given
// a tolerance threshold.
fn find_cubic_bezier_inflection_approximation_range(
    bezier_segment: &CubicBezierSegment,
    t: f32, tolerance: f32,
    min: &mut f32, max: &mut f32
) {
    let bezier = bezier_segment.after_split(t);

    let ctrl21 = bezier.ctrl1 - bezier.from;
    let ctrl41 = bezier.to - bezier.from;

    if ctrl21.x == 0.0 && ctrl21.y == 0.0 {
        // In this case s3 becomes lim[n->0] (ctrl41.x * n) / n - (ctrl41.y * n) / n = ctrl41.x - ctrl41.y.
        let s3 = ctrl41.x - ctrl41.y;

        if s3 == 0.0 {
            *min = t;
            *max = t;
            return;
        }
        // Use the absolute value so that Min and Max will correspond with the
        // minimum and maximum of the range.
        let tf = cubic_root((tolerance / s3).abs());
        *min = t - tf;
        *max = t + tf;
        return;
    }

    let s3 = (ctrl41.x * ctrl21.y - ctrl41.y * ctrl21.x) / ctrl21.x.hypot(ctrl21.y);

    if s3 == 0.0 {
        // This means within the precision we have it can be approximated
        // infinitely by a linear segment. Deal with this by specifying the
        // approximation range as extending beyond the entire curve.
        *min = -1.0;
        *max = 2.0;
        return;
    }

    let tf = cubic_root((tolerance / s3).abs());

    *min = t - tf * (1.0 - t);
    *max = t + tf * (1.0 - t);
}

#[cfg(test)]
fn print_arrays(a: &[Point], b: &[Point]) {
    println!("left:  {:?}", a);
    println!("right: {:?}", b);
}

#[cfg(test)]
fn assert_approx_eq(a: &[Point], b: &[Point]) {
    if a.len() != b.len() {
        print_arrays(a, b);
        panic!("Lenths differ ({} != {})", a.len(), b.len());
    }
    for i in 0..a.len() {
        if (a[i].x - b[i].x).abs() > 0.0000001 ||
           (a[i].y - b[i].y).abs() > 0.0000001 {
            print_arrays(a, b);
            panic!("The arrays are not equal");
        }
    }
}

#[test]
fn test_iterator_builder_1() {
    let tolerance = 0.01;
    let c1 = CubicBezierSegment {
        from: Point::new(0.0, 0.0),
        ctrl1: Point::new(1.0, 0.0),
        ctrl2: Point::new(1.0, 1.0),
        to: Point::new(0.0, 1.0),
    };
    let iter_points: Vec<Point> = c1.flattening_iter(tolerance).collect();
    let mut builder_points = Vec::new();
    c1.flattened_for_each(tolerance, &mut|p|{ builder_points.push(p); });

    assert!(iter_points.len() > 2);
    assert_approx_eq(&iter_points[..], &builder_points[..]);
}

#[test]
fn test_iterator_builder_2() {
    let tolerance = 0.01;
    let c1 = CubicBezierSegment {
        from: Point::new(0.0, 0.0),
        ctrl1: Point::new(1.0, 0.0),
        ctrl2: Point::new(0.0, 1.0),
        to: Point::new(1.0, 1.0),
    };
    let iter_points: Vec<Point> = c1.flattening_iter(tolerance).collect();
    let mut builder_points = Vec::new();
    c1.flattened_for_each(tolerance, &mut|p|{ builder_points.push(p); });

    assert!(iter_points.len() > 2);
    assert_approx_eq(&iter_points[..], &builder_points[..]);
}

#[test]
fn test_iterator_builder_3() {
    let tolerance = 0.01;
    let c1 = CubicBezierSegment {
        from: Point::new(141.0, 135.0),
        ctrl1: Point::new(141.0, 130.0),
        ctrl2: Point::new(140.0, 130.0),
        to: Point::new(131.0, 130.0),
    };
    let iter_points: Vec<Point> = c1.flattening_iter(tolerance).collect();
    let mut builder_points = Vec::new();
    c1.flattened_for_each(tolerance, &mut|p|{ builder_points.push(p); });

    assert!(iter_points.len() > 2);
    assert_approx_eq(&iter_points[..], &builder_points[..]);
}

#[test]
fn test_issue_19() {
    let tolerance = 0.15;
    let c1 = CubicBezierSegment {
        from: Point::new(11.71726, 9.07143),
        ctrl1: Point::new(1.889879,13.22917),
        ctrl2: Point::new(18.142855,19.27679),
        to: Point::new(18.142855,19.27679),
    };
    let iter_points: Vec<Point> = c1.flattening_iter(tolerance).collect();
    let mut builder_points = Vec::new();
    c1.flattened_for_each(tolerance, &mut|p|{ builder_points.push(p); });

    assert_approx_eq(&iter_points[..], &builder_points[..]);

    assert!(iter_points.len() > 1);
}