1use core::ops::Range;
8
9use alloc::vec::Vec;
10
11use arrayvec::ArrayVec;
12
13use crate::{
14    common::{
15        factor_quartic_inner, solve_cubic, solve_itp_fallible, solve_quadratic,
16        GAUSS_LEGENDRE_COEFFS_16,
17    },
18    Affine, BezPath, CubicBez, Line, ParamCurve, ParamCurveArclen, ParamCurveNearest, Point, Vec2,
19};
20
21#[cfg(not(feature = "std"))]
22use crate::common::FloatFuncs;
23
24pub trait ParamCurveFit {
52    fn sample_pt_tangent(&self, t: f64, sign: f64) -> CurveFitSample;
61
62    fn sample_pt_deriv(&self, t: f64) -> (Point, Vec2);
66
67    fn moment_integrals(&self, range: Range<f64>) -> (f64, f64, f64) {
79        let t0 = 0.5 * (range.start + range.end);
80        let dt = 0.5 * (range.end - range.start);
81        let (a, x, y) = GAUSS_LEGENDRE_COEFFS_16
82            .iter()
83            .map(|(wi, xi)| {
84                let t = t0 + xi * dt;
85                let (p, d) = self.sample_pt_deriv(t);
86                let a = wi * d.x * p.y;
87                let x = p.x * a;
88                let y = p.y * a;
89                (a, x, y)
90            })
91            .fold((0.0, 0.0, 0.0), |(a0, x0, y0), (a1, x1, y1)| {
92                (a0 + a1, x0 + x1, y0 + y1)
93            });
94        (a * dt, x * dt, y * dt)
95    }
96
97    fn break_cusp(&self, range: Range<f64>) -> Option<f64>;
115}
116
117pub struct CurveFitSample {
119    pub p: Point,
121    pub tangent: Vec2,
123}
124
125impl CurveFitSample {
126    fn intersect(&self, c: CubicBez) -> ArrayVec<f64, 3> {
130        let p1 = 3.0 * (c.p1 - c.p0);
131        let p2 = 3.0 * c.p2.to_vec2() - 6.0 * c.p1.to_vec2() + 3.0 * c.p0.to_vec2();
132        let p3 = (c.p3 - c.p0) - 3.0 * (c.p2 - c.p1);
133        let c0 = (c.p0 - self.p).dot(self.tangent);
134        let c1 = p1.dot(self.tangent);
135        let c2 = p2.dot(self.tangent);
136        let c3 = p3.dot(self.tangent);
137        solve_cubic(c0, c1, c2, c3)
138            .into_iter()
139            .filter(|t| (0.0..=1.0).contains(t))
140            .collect()
141    }
142}
143
144pub fn fit_to_bezpath(source: &impl ParamCurveFit, accuracy: f64) -> BezPath {
166    let mut path = BezPath::new();
167    fit_to_bezpath_rec(source, 0.0..1.0, accuracy, &mut path);
168    path
169}
170
171fn fit_to_bezpath_rec(
174    source: &impl ParamCurveFit,
175    range: Range<f64>,
176    accuracy: f64,
177    path: &mut BezPath,
178) {
179    let start = range.start;
180    let end = range.end;
181    let start_p = source.sample_pt_tangent(range.start, 1.0).p;
182    let end_p = source.sample_pt_tangent(range.end, -1.0).p;
183    if start_p.distance_squared(end_p) <= accuracy * accuracy {
184        if let Some((c, _)) = try_fit_line(source, accuracy, range, start_p, end_p) {
185            if path.is_empty() {
186                path.move_to(c.p0);
187            }
188            path.curve_to(c.p1, c.p2, c.p3);
189            return;
190        }
191    }
192    let t = if let Some(t) = source.break_cusp(start..end) {
193        t
194    } else if let Some((c, _)) = fit_to_cubic(source, start..end, accuracy) {
195        if path.is_empty() {
196            path.move_to(c.p0);
197        }
198        path.curve_to(c.p1, c.p2, c.p3);
199        return;
200    } else {
201        0.5 * (start + end)
204    };
205    if t == start || t == end {
206        let p1 = start_p.lerp(end_p, 1.0 / 3.0);
208        let p2 = end_p.lerp(start_p, 1.0 / 3.0);
209        if path.is_empty() {
210            path.move_to(start_p);
211        }
212        path.curve_to(p1, p2, end_p);
213        return;
214    }
215    fit_to_bezpath_rec(source, start..t, accuracy, path);
216    fit_to_bezpath_rec(source, t..end, accuracy, path);
217}
218
219const N_SAMPLE: usize = 20;
220
221struct CurveDist {
223    samples: ArrayVec<CurveFitSample, N_SAMPLE>,
224    arcparams: ArrayVec<f64, N_SAMPLE>,
225    range: Range<f64>,
226    spicy: bool,
229}
230
231impl CurveDist {
232    fn from_curve(source: &impl ParamCurveFit, range: Range<f64>) -> Self {
233        let step = (range.end - range.start) * (1.0 / (N_SAMPLE + 1) as f64);
234        let mut last_tan = None;
235        let mut spicy = false;
236        const SPICY_THRESH: f64 = 0.2;
237        let mut samples = ArrayVec::new();
238        for i in 0..N_SAMPLE + 2 {
239            let sample = source.sample_pt_tangent(range.start + i as f64 * step, 1.0);
240            if let Some(last_tan) = last_tan {
241                let cross = sample.tangent.cross(last_tan);
242                let dot = sample.tangent.dot(last_tan);
243                if cross.abs() > SPICY_THRESH * dot.abs() {
244                    spicy = true;
245                }
246            }
247            last_tan = Some(sample.tangent);
248            if i > 0 && i < N_SAMPLE + 1 {
249                samples.push(sample);
250            }
251        }
252        CurveDist {
253            samples,
254            arcparams: ArrayVec::default(),
255            range,
256            spicy,
257        }
258    }
259
260    fn compute_arc_params(&mut self, source: &impl ParamCurveFit) {
261        const N_SUBSAMPLE: usize = 10;
262        let (start, end) = (self.range.start, self.range.end);
263        let dt = (end - start) * (1.0 / ((N_SAMPLE + 1) * N_SUBSAMPLE) as f64);
264        let mut arclen = 0.0;
265        for i in 0..N_SAMPLE + 1 {
266            for j in 0..N_SUBSAMPLE {
267                let t = start + dt * ((i * N_SUBSAMPLE + j) as f64 + 0.5);
268                let (_, deriv) = source.sample_pt_deriv(t);
269                arclen += deriv.hypot();
270            }
271            if i < N_SAMPLE {
272                self.arcparams.push(arclen);
273            }
274        }
275        let arclen_inv = arclen.recip();
276        for x in &mut self.arcparams {
277            *x *= arclen_inv;
278        }
279    }
280
281    fn eval_arc(&self, c: CubicBez, acc2: f64) -> Option<f64> {
283        const EPS: f64 = 1e-9;
285        let c_arclen = c.arclen(EPS);
286        let mut max_err2 = 0.0;
287        for (sample, s) in self.samples.iter().zip(&self.arcparams) {
288            let t = c.inv_arclen(c_arclen * s, EPS);
289            let err = sample.p.distance_squared(c.eval(t));
290            max_err2 = err.max(max_err2);
291            if max_err2 > acc2 {
292                return None;
293            }
294        }
295        Some(max_err2)
296    }
297
298    fn eval_ray(&self, c: CubicBez, acc2: f64) -> Option<f64> {
306        let mut max_err2 = 0.0;
307        for sample in &self.samples {
308            let mut best = acc2 + 1.0;
309            for t in sample.intersect(c) {
310                let err = sample.p.distance_squared(c.eval(t));
311                best = best.min(err);
312            }
313            max_err2 = best.max(max_err2);
314            if max_err2 > acc2 {
315                return None;
316            }
317        }
318        Some(max_err2)
319    }
320
321    fn eval_dist(&mut self, source: &impl ParamCurveFit, c: CubicBez, acc2: f64) -> Option<f64> {
322        let ray_dist = self.eval_ray(c, acc2)?;
324        if !self.spicy {
325            return Some(ray_dist);
326        }
327        if self.arcparams.is_empty() {
328            self.compute_arc_params(source);
329        }
330        self.eval_arc(c, acc2)
331    }
332}
333
334const D_PENALTY_ELBOW: f64 = 0.65;
345const D_PENALTY_SLOPE: f64 = 2.0;
346
347fn try_fit_line(
356    source: &impl ParamCurveFit,
357    accuracy: f64,
358    range: Range<f64>,
359    start: Point,
360    end: Point,
361) -> Option<(CubicBez, f64)> {
362    let acc2 = accuracy * accuracy;
363    let chord_l = Line::new(start, end);
364    const SHORT_N: usize = 7;
365    let mut max_err2 = 0.0;
366    let dt = (range.end - range.start) / (SHORT_N + 1) as f64;
367    for i in 0..SHORT_N {
368        let t = range.start + (i + 1) as f64 * dt;
369        let p = source.sample_pt_deriv(t).0;
370        let err2 = chord_l.nearest(p, accuracy).distance_sq;
371        if err2 > acc2 {
372            return None;
374        }
375        max_err2 = err2.max(max_err2);
376    }
377    let p1 = start.lerp(end, 1.0 / 3.0);
378    let p2 = end.lerp(start, 1.0 / 3.0);
379    let c = CubicBez::new(start, p1, p2, end);
380    Some((c, max_err2))
381}
382
383pub fn fit_to_cubic(
388    source: &impl ParamCurveFit,
389    range: Range<f64>,
390    accuracy: f64,
391) -> Option<(CubicBez, f64)> {
392    let start = source.sample_pt_tangent(range.start, 1.0);
393    let end = source.sample_pt_tangent(range.end, -1.0);
394    let d = end.p - start.p;
395    let chord2 = d.hypot2();
396    let acc2 = accuracy * accuracy;
397    if chord2 <= acc2 {
398        return try_fit_line(source, accuracy, range, start.p, end.p);
400    }
401    let th = d.atan2();
402    fn mod_2pi(th: f64) -> f64 {
403        let th_scaled = th * core::f64::consts::FRAC_1_PI * 0.5;
404        core::f64::consts::PI * 2.0 * (th_scaled - th_scaled.round())
405    }
406    let th0 = mod_2pi(start.tangent.atan2() - th);
407    let th1 = mod_2pi(th - end.tangent.atan2());
408
409    let (mut area, mut x, mut y) = source.moment_integrals(range.clone());
410    let (x0, y0) = (start.p.x, start.p.y);
411    let (dx, dy) = (d.x, d.y);
412    area -= dx * (y0 + 0.5 * dy);
414    let dy_3 = dy * (1. / 3.);
419    x -= dx * (x0 * y0 + 0.5 * (x0 * dy + y0 * dx) + dy_3 * dx);
420    y -= dx * (y0 * y0 + y0 * dy + dy_3 * dy);
421    x -= x0 * area;
423    y = 0.5 * y - y0 * area;
424    let moment = d.x * x + d.y * y;
426    let chord2_inv = chord2.recip();
431    let unit_area = area * chord2_inv;
432    let mx = moment * chord2_inv.powi(2);
433    let chord = chord2.sqrt();
436    let aff = Affine::translate(start.p.to_vec2()) * Affine::rotate(th) * Affine::scale(chord);
437    let mut curve_dist = CurveDist::from_curve(source, range);
438    let mut best_c = None;
439    let mut best_err2 = None;
440    for (cand, d0, d1) in cubic_fit(th0, th1, unit_area, mx) {
441        let c = aff * cand;
442        if let Some(err2) = curve_dist.eval_dist(source, c, acc2) {
443            fn scale_f(d: f64) -> f64 {
444                1.0 + (d - D_PENALTY_ELBOW).max(0.0) * D_PENALTY_SLOPE
445            }
446            let scale = scale_f(d0).max(scale_f(d1)).powi(2);
447            let err2 = err2 * scale;
448            if err2 < acc2 && best_err2.map(|best| err2 < best).unwrap_or(true) {
449                best_c = Some(c);
450                best_err2 = Some(err2);
451            }
452        }
453    }
454    match (best_c, best_err2) {
455        (Some(c), Some(err2)) => Some((c, err2)),
456        _ => None,
457    }
458}
459
460fn cubic_fit(th0: f64, th1: f64, area: f64, mx: f64) -> ArrayVec<(CubicBez, f64, f64), 4> {
462    let (s0, c0) = th0.sin_cos();
465    let (s1, c1) = th1.sin_cos();
466    let a4 = -9.
467        * c0
468        * (((2. * s1 * c1 * c0 + s0 * (2. * c1 * c1 - 1.)) * c0 - 2. * s1 * c1) * c0
469            - c1 * c1 * s0);
470    let a3 = 12.
471        * ((((c1 * (30. * area * c1 - s1) - 15. * area) * c0 + 2. * s0
472            - c1 * s0 * (c1 + 30. * area * s1))
473            * c0
474            + c1 * (s1 - 15. * area * c1))
475            * c0
476            - s0 * c1 * c1);
477    let a2 = 12.
478        * ((((70. * mx + 15. * area) * s1 * s1 + c1 * (9. * s1 - 70. * c1 * mx - 5. * c1 * area))
479            * c0
480            - 5. * s0 * s1 * (3. * s1 - 4. * c1 * (7. * mx + area)))
481            * c0
482            - c1 * (9. * s1 - 70. * c1 * mx - 5. * c1 * area));
483    let a1 = 16.
484        * (((12. * s0 - 5. * c0 * (42. * mx - 17. * area)) * s1
485            - 70. * c1 * (3. * mx - area) * s0
486            - 75. * c0 * c1 * area * area)
487            * s1
488            - 75. * c1 * c1 * area * area * s0);
489    let a0 = 80. * s1 * (42. * s1 * mx - 25. * area * (s1 - c1 * area));
490    let mut roots = ArrayVec::<f64, 4>::new();
493    const EPS: f64 = 1e-12;
494    if a4.abs() > EPS {
495        let a = a3 / a4;
496        let b = a2 / a4;
497        let c = a1 / a4;
498        let d = a0 / a4;
499        if let Some(quads) = factor_quartic_inner(a, b, c, d, false) {
500            for (qc1, qc0) in quads {
501                let qroots = solve_quadratic(qc0, qc1, 1.0);
502                if qroots.is_empty() {
503                    roots.push(-0.5 * qc1);
505                } else {
506                    roots.extend(qroots);
507                }
508            }
509        }
510    } else if a3.abs() > EPS {
511        roots.extend(solve_cubic(a0, a1, a2, a3));
512    } else if a2.abs() > EPS || a1.abs() > EPS || a0.abs() > EPS {
513        roots.extend(solve_quadratic(a0, a1, a2));
514    } else {
515        return [(
516            CubicBez::new((0.0, 0.0), (1. / 3., 0.0), (2. / 3., 0.0), (1., 0.0)),
517            1f64 / 3.,
518            1f64 / 3.,
519        )]
520        .into_iter()
521        .collect();
522    }
523
524    let s01 = s0 * c1 + s1 * c0;
525    roots
526        .iter()
527        .filter_map(|&d0| {
528            let (d0, d1) = if d0 > 0.0 {
529                let d1 = (d0 * s0 - area * (10. / 3.)) / (0.5 * d0 * s01 - s1);
530                if d1 > 0.0 {
531                    (d0, d1)
532                } else {
533                    (s1 / s01, 0.0)
534                }
535            } else {
536                (0.0, s0 / s01)
537            };
538            if d0 >= 0.0 && d1 >= 0.0 {
540                Some((
541                    CubicBez::new(
542                        (0.0, 0.0),
543                        (d0 * c0, d0 * s0),
544                        (1.0 - d1 * c1, d1 * s1),
545                        (1.0, 0.0),
546                    ),
547                    d0,
548                    d1,
549                ))
550            } else {
551                None
552            }
553        })
554        .collect()
555}
556
557pub fn fit_to_bezpath_opt(source: &impl ParamCurveFit, accuracy: f64) -> BezPath {
569    let mut cusps = Vec::new();
570    let mut path = BezPath::new();
571    let mut t0 = 0.0;
572    loop {
573        let t1 = cusps.last().copied().unwrap_or(1.0);
574        match fit_to_bezpath_opt_inner(source, accuracy, t0..t1, &mut path) {
575            Some(t) => cusps.push(t),
576            None => match cusps.pop() {
577                Some(t) => t0 = t,
578                None => break,
579            },
580        }
581    }
582    path
583}
584
585fn fit_to_bezpath_opt_inner(
590    source: &impl ParamCurveFit,
591    accuracy: f64,
592    range: Range<f64>,
593    path: &mut BezPath,
594) -> Option<f64> {
595    if let Some(t) = source.break_cusp(range.clone()) {
596        return Some(t);
597    }
598    let err;
599    if let Some((c, err2)) = fit_to_cubic(source, range.clone(), accuracy) {
600        err = err2.sqrt();
601        if err < accuracy {
602            if range.start == 0.0 {
603                path.move_to(c.p0);
604            }
605            path.curve_to(c.p1, c.p2, c.p3);
606            return None;
607        }
608    } else {
609        err = 2.0 * accuracy;
610    }
611    let (mut t0, t1) = (range.start, range.end);
612    let mut n = 0;
613    let last_err;
614    loop {
615        n += 1;
616        match fit_opt_segment(source, accuracy, t0..t1) {
617            FitResult::ParamVal(t) => t0 = t,
618            FitResult::SegmentError(err) => {
619                last_err = err;
620                break;
621            }
622            FitResult::CuspFound(t) => return Some(t),
623        }
624    }
625    t0 = range.start;
626    const EPS: f64 = 1e-9;
627    let f = |x| fit_opt_err_delta(source, x, accuracy, t0..t1, n);
628    let k1 = 0.2 / accuracy;
629    let ya = -err;
630    let yb = accuracy - last_err;
631    let (_, x) = match solve_itp_fallible(f, 0.0, accuracy, EPS, 1, k1, ya, yb) {
632        Ok(x) => x,
633        Err(t) => return Some(t),
634    };
635    let path_len = path.elements().len();
637    for i in 0..n {
638        let t1 = if i < n - 1 {
639            match fit_opt_segment(source, x, t0..range.end) {
640                FitResult::ParamVal(t1) => t1,
641                FitResult::SegmentError(_) => range.end,
642                FitResult::CuspFound(t) => {
643                    path.truncate(path_len);
644                    return Some(t);
645                }
646            }
647        } else {
648            range.end
649        };
650        let (c, _) = fit_to_cubic(source, t0..t1, accuracy).unwrap();
651        if i == 0 && range.start == 0.0 {
652            path.move_to(c.p0);
653        }
654        path.curve_to(c.p1, c.p2, c.p3);
655        t0 = t1;
656        if t0 == range.end {
657            break;
659        }
660    }
661    None
662}
663
664fn measure_one_seg(source: &impl ParamCurveFit, range: Range<f64>, limit: f64) -> Option<f64> {
665    fit_to_cubic(source, range, limit).map(|(_, err2)| err2.sqrt())
666}
667
668enum FitResult {
669    ParamVal(f64),
671    SegmentError(f64),
673    CuspFound(f64),
675}
676
677fn fit_opt_segment(source: &impl ParamCurveFit, accuracy: f64, range: Range<f64>) -> FitResult {
678    if let Some(t) = source.break_cusp(range.clone()) {
679        return FitResult::CuspFound(t);
680    }
681    let missing_err = accuracy * 2.0;
682    let err = measure_one_seg(source, range.clone(), accuracy).unwrap_or(missing_err);
683    if err <= accuracy {
684        return FitResult::SegmentError(err);
685    }
686    let (t0, t1) = (range.start, range.end);
687    let f = |x| {
688        if let Some(t) = source.break_cusp(range.clone()) {
689            return Err(t);
690        }
691        let err = measure_one_seg(source, t0..x, accuracy).unwrap_or(missing_err);
692        Ok(err - accuracy)
693    };
694    const EPS: f64 = 1e-9;
695    let k1 = 2.0 / (t1 - t0);
696    match solve_itp_fallible(f, t0, t1, EPS, 1, k1, -accuracy, err - accuracy) {
697        Ok((t1, _)) => FitResult::ParamVal(t1),
698        Err(t) => FitResult::CuspFound(t),
699    }
700}
701
702fn fit_opt_err_delta(
705    source: &impl ParamCurveFit,
706    accuracy: f64,
707    limit: f64,
708    range: Range<f64>,
709    n: usize,
710) -> Result<f64, f64> {
711    let (mut t0, t1) = (range.start, range.end);
712    for _ in 0..n - 1 {
713        t0 = match fit_opt_segment(source, accuracy, t0..t1) {
714            FitResult::ParamVal(t0) => t0,
715            FitResult::SegmentError(_) => return Ok(accuracy),
718            FitResult::CuspFound(t) => return Err(t),
719        }
720    }
721    let err = measure_one_seg(source, t0..t1, limit).unwrap_or(accuracy * 2.0);
722    Ok(accuracy - err)
723}