pltrs-core 0.1.0

Geometry, paths, affine transforms, colours and numpy's float semantics shared by every pltrs crate
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
//! matplotlib's `BezierSegment`: a Bézier curve of any degree in any
//! dimension, with its polynomial form and axis-aligned extrema.

use std::fmt;

use crate::Point;
use crate::numeric::pow;

/// Why Bézier control points or a curve split were rejected.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BezierError {
    /// No control points were given (or they have no coordinates).
    Empty,
    /// The control points do not all have the same dimension.
    RaggedControlPoints,
    /// Both ends of the curve lie on the same side of the closed path
    /// (matplotlib's `NonIntersectingPathException`).
    NotIntersecting,
    /// No segment of a path crosses the region's boundary (matplotlib's
    /// `split_path_inout` raises a plain `ValueError` for it).
    PathMissesRegion,
    /// The two lines are parallel, so they have no single intersection.
    ParallelLines,
}

impl fmt::Display for BezierError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Empty => "a Bézier segment needs at least one control point",
            Self::RaggedControlPoints => "all control points must have the same dimension",
            Self::NotIntersecting => "both points are on the same side of the closed path",
            Self::PathMissesRegion => "The path does not intersect with the patch",
            Self::ParallelLines => "the lines do not intersect",
        })
    }
}

impl std::error::Error for BezierError {}

/// A d-dimensional Bézier curve given by its control points (matplotlib's
/// `BezierSegment`).
#[derive(Clone, Debug, PartialEq)]
pub struct BezierSegment {
    dimension: usize,
    /// Control points, row after row.
    coordinates: Vec<f64>,
}

/// The sum of the products `a × b` of `terms`, each added with a fused
/// multiply-add in order, as numpy's matrix products sum them
/// (`OpenBLAS`'s `dgemm` kernels).
fn fused_sum(terms: impl Iterator<Item = (f64, f64)>) -> f64 {
    terms.fold(0.0, |sum, (a, b)| a.mul_add(b, sum))
}

/// `C(n, k)` as a float (exact for the degrees used here).
fn binomial(n: usize, k: usize) -> f64 {
    (0..k).fold(1.0, |acc, i| acc * (n - i) as f64 / (i + 1) as f64)
}

impl BezierSegment {
    /// A curve through control points of `dimension` coordinates each,
    /// stored row after row in `coordinates`.
    pub fn new(dimension: usize, coordinates: Vec<f64>) -> Result<Self, BezierError> {
        if dimension == 0 || coordinates.is_empty() {
            return Err(BezierError::Empty);
        }
        if !coordinates.len().is_multiple_of(dimension) {
            return Err(BezierError::RaggedControlPoints);
        }
        Ok(Self {
            dimension,
            coordinates,
        })
    }

    /// A curve from rows of coordinates (all of the same length).
    pub fn from_rows<R: AsRef<[f64]>>(rows: &[R]) -> Result<Self, BezierError> {
        let dimension = rows.first().ok_or(BezierError::Empty)?.as_ref().len();
        if rows.iter().any(|r| r.as_ref().len() != dimension) {
            return Err(BezierError::RaggedControlPoints);
        }
        Self::new(
            dimension,
            rows.iter()
                .flat_map(|r| r.as_ref().iter().copied())
                .collect(),
        )
    }

    /// A planar curve.
    pub fn from_points(points: &[Point]) -> Result<Self, BezierError> {
        Self::new(2, points.iter().flat_map(|p| [p.x, p.y]).collect())
    }

    /// Number of coordinates per control point.
    pub const fn dimension(&self) -> usize {
        self.dimension
    }

    /// The degree: one less than the number of control points.
    pub fn degree(&self) -> usize {
        self.coordinates.len() / self.dimension - 1
    }

    /// The control points, one slice of coordinates each.
    pub fn control_points(&self) -> impl ExactSizeIterator<Item = &[f64]> {
        self.coordinates.chunks_exact(self.dimension)
    }

    /// The control points of a planar curve (`None` in other dimensions).
    pub fn points(&self) -> Option<Vec<Point>> {
        (self.dimension == 2).then(|| {
            self.control_points()
                .map(|c| Point::new(c[0], c[1]))
                .collect()
        })
    }

    /// The point at parameter `t`, computed as matplotlib's `__call__`
    /// computes the points of its parameters: the Bernstein weights
    /// `(1 - t)^(n - i) tⁱ` by `pow` (numpy's `power.outer`), times the
    /// control points scaled by `C(n, i)`, summed in order with fused
    /// multiply-adds (numpy's matrix product).
    pub fn eval(&self, t: f64) -> Vec<f64> {
        let n = self.degree();
        let weights: Vec<f64> = (0..=n)
            .map(|i| pow(1.0 - t, (n - i) as f64) * pow(t, i as f64))
            .collect();
        let scaled: Vec<Vec<f64>> = self
            .control_points()
            .enumerate()
            .map(|(i, point)| point.iter().map(|c| c * binomial(n, i)).collect())
            .collect();
        (0..self.dimension)
            .map(|d| fused_sum(weights.iter().zip(&scaled).map(|(&w, row)| (w, row[d]))))
            .collect()
    }

    /// The first two coordinates of [`eval`](Self::eval) as a point.
    pub fn point_at(&self, t: f64) -> Point {
        let v = self.eval(t);
        Point::new(v[0], v.get(1).copied().unwrap_or(0.0))
    }

    /// The power-basis coefficients `Cⱼ` with `B(t) = Σ Cⱼ tʲ`, one row per
    /// power (matplotlib's `polynomial_coefficients`: its matrix of
    /// `C(n, j) (-1)^(i + j) C(j, i)` times the control points, summed in
    /// order with fused multiply-adds).
    pub fn polynomial_coefficients(&self) -> Vec<Vec<f64>> {
        let n = self.degree();
        let points: Vec<&[f64]> = self.control_points().collect();
        let factor = |j: usize, i: usize| {
            if i > j {
                return 0.0;
            }
            let sign = if (i + j).is_multiple_of(2) { 1.0 } else { -1.0 };
            binomial(n, j) * sign * binomial(j, i)
        };
        (0..=n)
            .map(|j| {
                (0..self.dimension)
                    .map(|d| {
                        fused_sum(
                            points
                                .iter()
                                .enumerate()
                                .map(|(i, point)| (factor(j, i), point[d])),
                        )
                    })
                    .collect()
            })
            .collect()
    }

    /// The parameters in `[0, 1]` where a coordinate's derivative vanishes,
    /// with the coordinate (dimension) index of each: matplotlib's
    /// `axis_aligned_extrema`, grouped by dimension, ascending within one.
    pub fn axis_aligned_extrema(&self) -> (Vec<usize>, Vec<f64>) {
        let n = self.degree();
        let mut dims = Vec::new();
        let mut roots = Vec::new();
        if n <= 1 {
            return (dims, roots);
        }
        let coefficients = self.polynomial_coefficients();
        for d in 0..self.dimension {
            let derivative: Vec<f64> = coefficients[1..]
                .iter()
                .enumerate()
                .map(|(k, row)| (k + 1) as f64 * row[d])
                .collect();
            for root in real_roots_in_01(&derivative) {
                dims.push(d);
                roots.push(root);
            }
        }
        (dims, roots)
    }

    /// Splits the curve at `t` with de Casteljau's algorithm (matplotlib's
    /// `split_de_casteljau`): the halves before and after `t`.
    pub fn split(&self, t: f64) -> (Self, Self) {
        let d = self.dimension;
        let mut level = self.coordinates.clone();
        let mut left = level[..d].to_vec();
        let mut right_rev = level[level.len() - d..].to_vec();
        while level.len() > d {
            level = level
                .chunks_exact(d)
                .zip(level.chunks_exact(d).skip(1))
                .flat_map(|(a, b)| a.iter().zip(b).map(|(a, b)| a * (1.0 - t) + b * t))
                .collect();
            left.extend_from_slice(&level[..d]);
            right_rev.extend_from_slice(&level[level.len() - d..]);
        }
        let right = right_rev.chunks_exact(d).rev().flatten().copied().collect();
        (
            Self {
                dimension: d,
                coordinates: left,
            },
            Self {
                dimension: d,
                coordinates: right,
            },
        )
    }
}

/// matplotlib's `_real_roots_in_01`: the sorted real roots in `[0, 1]` of
/// `Σ cₖ tᵏ` (ascending coefficients), ignoring near-zero (< 1e-12)
/// leading terms.
///
/// ```
/// use pltrs_core::bezier::real_roots_in_01;
/// // (t - 0.25)(t - 0.75) = t² - t + 0.1875
/// assert_eq!(real_roots_in_01(&[0.1875, -1.0, 1.0]), [0.25, 0.75]);
/// assert!(real_roots_in_01(&[5.0]).is_empty());
/// assert!(real_roots_in_01(&[]).is_empty());
/// assert!(real_roots_in_01(&[-2.0, 1.0]).is_empty(), "t = 2 is outside");
/// ```
pub fn real_roots_in_01(coefficients: &[f64]) -> Vec<f64> {
    let Some(degree) = coefficients.iter().rposition(|c| c.abs() >= 1e-12) else {
        return Vec::new();
    };
    let c = &coefficients[..=degree];
    let mut roots = match degree {
        0 => Vec::new(),
        1 => vec![-c[0] / c[1]],
        2 => quadratic_roots(c[0], c[1], c[2]),
        _ => {
            const EPS: f64 = 1e-10;
            polynomial_roots(c, -EPS, 1.0 + EPS)
                .into_iter()
                .map(|r| r.clamp(0.0, 1.0))
                .collect()
        }
    };
    roots.retain(|r| (0.0..=1.0).contains(r));
    roots.sort_by(f64::total_cmp);
    roots
}

/// matplotlib's `_quadratic_roots_in_01` before the range filter: the
/// numerically stable quadratic formula.
fn quadratic_roots(c0: f64, c1: f64, c2: f64) -> Vec<f64> {
    let disc = c1 * c1 - 4.0 * c2 * c0;
    if disc < 0.0 {
        return Vec::new();
    }
    let sqrt_disc = disc.sqrt();
    let q = if c1 >= 0.0 {
        -0.5 * (c1 + sqrt_disc)
    } else {
        -0.5 * (c1 - sqrt_disc)
    };
    let mut roots = Vec::with_capacity(2);
    if q.abs() > 1e-12 {
        roots.push(c0 / q);
    }
    if c2.abs() > 1e-12 {
        roots.push(q / c2);
    }
    roots
}

fn horner(c: &[f64], t: f64) -> f64 {
    c.iter().rev().fold(0.0, |acc, k| acc * t + k)
}

/// The real roots of `Σ cₖ tᵏ` (degree ≥ 1) in `[lo, hi]`, located between
/// the roots of the derivative and refined by bisection. (matplotlib calls
/// `numpy.roots` here; both agree to ~1e-12 on simple roots.)
fn polynomial_roots(c: &[f64], lo: f64, hi: f64) -> Vec<f64> {
    if c.len() == 2 {
        let r = -c[0] / c[1];
        return if (lo..=hi).contains(&r) {
            vec![r]
        } else {
            Vec::new()
        };
    }
    let derivative: Vec<f64> = c[1..]
        .iter()
        .enumerate()
        .map(|(k, v)| (k + 1) as f64 * v)
        .collect();
    let mut knots = vec![lo];
    knots.extend(polynomial_roots(&derivative, lo, hi));
    knots.push(hi);
    let mut roots = Vec::new();
    for pair in knots.windows(2) {
        let (mut a, mut b) = (pair[0], pair[1]);
        let (fa, fb) = (horner(c, a), horner(c, b));
        if fa == 0.0 {
            roots.push(a);
            continue;
        }
        if fa.signum() == fb.signum() {
            continue;
        }
        while b - a > f64::EPSILON * b.abs().max(1.0) {
            let m = f64::midpoint(a, b);
            if horner(c, m).signum() == fa.signum() {
                a = m;
            } else {
                b = m;
            }
        }
        roots.push(f64::midpoint(a, b));
    }
    if horner(c, hi) == 0.0 {
        roots.push(hi);
    }
    roots.dedup();
    roots
}

#[cfg(test)]
mod tests {
    use super::*;

    fn cubic() -> BezierSegment {
        BezierSegment::from_rows(&[[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0]]).expect("valid")
    }

    #[test]
    fn evaluation_and_properties() {
        let c = cubic();
        assert_eq!((c.degree(), c.dimension()), (3, 2));
        assert_eq!(c.eval(0.5), vec![0.5, 0.75]);
        assert_eq!(c.point_at(0.0), Point::new(0.0, 0.0));
        assert_eq!(
            c.polynomial_coefficients(),
            vec![
                vec![0.0, 0.0],
                vec![0.0, 3.0],
                vec![3.0, -3.0],
                vec![-2.0, 0.0]
            ]
        );
        assert_eq!(
            BezierSegment::from_rows::<[f64; 2]>(&[]),
            Err(BezierError::Empty)
        );
        assert_eq!(
            BezierSegment::from_rows(&[vec![0.0, 0.0], vec![1.0]]),
            Err(BezierError::RaggedControlPoints)
        );
    }

    #[test]
    fn extrema_like_matplotlib() {
        // x' = 6t(1 - t) vanishes at both ends, y' at the middle.
        assert_eq!(
            cubic().axis_aligned_extrema(),
            (vec![0, 0, 1], vec![0.0, 1.0, 0.5])
        );
        let line = BezierSegment::from_rows(&[[0.0, 0.0], [4.0, 2.0]]).expect("valid");
        assert_eq!(line.axis_aligned_extrema(), (vec![], vec![]));
        let quartic = BezierSegment::from_rows(&[
            [0.0, 0.0],
            [1.0, 3.0],
            [2.0, -2.0],
            [3.0, 2.0],
            [4.0, 0.0],
        ])
        .expect("valid");
        let (dims, roots) = quartic.axis_aligned_extrema();
        assert_eq!(dims, vec![1, 1, 1]);
        for (got, want) in roots.iter().zip([
            0.197_868_915_336_342_77,
            0.582_517_508_272_926_4,
            0.813_363_576_390_730_6,
        ]) {
            assert!((got - want).abs() < 1e-12);
        }
    }

    #[test]
    fn evaluation_rounds_as_numpy() {
        // matplotlib 3.11: the far end of a networkx self-loop,
        // `BezierSegment(points)([0, t, 1])` at its x extremum.
        let curve = BezierSegment::from_rows(&[
            [0.499_999_910_593_032_6, -0.692_820_340_394_973_8],
            [0.586_602_464_318_274_8, -0.692_820_340_394_973_8],
            [0.586_602_464_318_274_8, -0.866_025_447_845_459],
            [0.499_999_910_593_032_6, -0.866_025_447_845_459],
        ])
        .expect("valid");
        let (dims, roots) = curve.axis_aligned_extrema();
        assert_eq!(dims, [0, 1, 1]);
        assert_eq!(
            curve.eval(roots[0]),
            [0.564_951_825_886_964_2, -0.779_422_894_120_216_6]
        );
    }

    #[test]
    fn de_casteljau_split() {
        let (left, right) = cubic().split(0.5);
        assert_eq!(left.points().expect("2-D")[3], Point::new(0.5, 0.75));
        assert_eq!(right.points().expect("2-D")[0], Point::new(0.5, 0.75));
        assert_eq!(right.points().expect("2-D")[3], Point::new(1.0, 0.0));
        let point = BezierSegment::from_rows(&[[1.5, -2.0]]).expect("valid");
        assert_eq!(point.split(0.3), (point.clone(), point));
    }
}