Skip to main content

strata_gpu_path/
sdf.rs

1//! Analytical signed distance fields (SDF) and winding numbers for quadratic Béziers.
2
3use crate::curve::{Point2D, QuadraticBezier};
4use crate::path::FillRule;
5
6/// Computes the exact minimum Euclidean distance from query point `p` to quadratic Bézier `curve`.
7pub fn distance_to_quadratic(p: Point2D, curve: &QuadraticBezier) -> f32 {
8    // Express curve in polynomial form: B(t) = a * t^2 + b * t + c
9    // B(t) = (1-t)^2 P0 + 2t(1-t) P1 + t^2 P2
10    //      = (P0 - 2P1 + P2) t^2 + 2(P1 - P0) t + P0
11    let a_x = curve.p0.x - 2.0 * curve.p1.x + curve.p2.x;
12    let a_y = curve.p0.y - 2.0 * curve.p1.y + curve.p2.y;
13
14    let b_x = 2.0 * (curve.p1.x - curve.p0.x);
15    let b_y = 2.0 * (curve.p1.y - curve.p0.y);
16
17    let c_x = curve.p0.x - p.x;
18    let c_y = curve.p0.y - p.y;
19
20    // We seek t minimizing || B(t) - P ||^2
21    // d/dt || B(t) - P ||^2 = 2 * (B(t) - P) . B'(t) = 0
22    // (a t^2 + b t + c) . (2 a t + b) = 0
23    // 2 (a.a) t^3 + 3 (a.b) t^2 + (2 a.c + b.b) t + (b.c) = 0
24    let dot_aa = a_x * a_x + a_y * a_y;
25    let dot_ab = a_x * b_x + a_y * b_y;
26    let dot_ac = a_x * c_x + a_y * c_y;
27    let dot_bb = b_x * b_x + b_y * b_y;
28    let dot_bc = b_x * c_x + b_y * c_y;
29
30    let k3 = 2.0 * dot_aa;
31    let k2 = 3.0 * dot_ab;
32    let k1 = 2.0 * dot_ac + dot_bb;
33    let k0 = dot_bc;
34
35    let mut min_dist_sq = p
36        .distance_squared(curve.p0)
37        .min(p.distance_squared(curve.p2));
38
39    let roots = solve_cubic_roots(k3, k2, k1, k0);
40    for t in roots {
41        if (0.0..=1.0).contains(&t) {
42            let pt_on_curve = curve.evaluate(t);
43            let d2 = p.distance_squared(pt_on_curve);
44            if d2 < min_dist_sq {
45                min_dist_sq = d2;
46            }
47        }
48    }
49
50    min_dist_sq.sqrt()
51}
52
53/// Solves real roots of cubic equation: a * t^3 + b * t^2 + c * t + d = 0.
54pub fn solve_cubic_roots(a: f32, b: f32, c: f32, d: f32) -> Vec<f32> {
55    let mut roots = Vec::new();
56
57    if a.abs() < 1e-7 {
58        // Quadratic: b * t^2 + c * t + d = 0
59        if b.abs() < 1e-7 {
60            // Linear: c * t + d = 0
61            if c.abs() > 1e-7 {
62                roots.push(-d / c);
63            }
64        } else {
65            let disc = c * c - 4.0 * b * d;
66            if disc >= 0.0 {
67                let sqrt_disc = disc.sqrt();
68                roots.push((-c + sqrt_disc) / (2.0 * b));
69                roots.push((-c - sqrt_disc) / (2.0 * b));
70            }
71        }
72        return roots;
73    }
74
75    // Depressed cubic substitution: t = x - b / (3a)
76    let inv_a = 1.0 / a;
77    let p = (3.0 * a * c - b * b) / (3.0 * a * a);
78    let q = (2.0 * b * b * b - 9.0 * a * b * c + 27.0 * a * a * d) / (27.0 * a * a * a);
79
80    let offset = b * inv_a / 3.0;
81    let disc = (q * q / 4.0) + (p * p * p / 27.0);
82
83    if disc > 1e-7 {
84        // One real root
85        let sqrt_disc = disc.sqrt();
86        let u = (-q / 2.0 + sqrt_disc).cbrt();
87        let v = (-q / 2.0 - sqrt_disc).cbrt();
88        roots.push(u + v - offset);
89    } else if disc < -1e-7 {
90        // Three real roots (Casus irreducibilis)
91        let r = (-p * p * p / 27.0).sqrt();
92        let phi = (-q / (2.0 * r)).clamp(-1.0, 1.0).acos();
93        let cbrt_r = 2.0 * (-p / 3.0).sqrt();
94
95        roots.push(cbrt_r * (phi / 3.0).cos() - offset);
96        roots.push(cbrt_r * ((phi + 2.0 * std::f32::consts::PI) / 3.0).cos() - offset);
97        roots.push(cbrt_r * ((phi + 4.0 * std::f32::consts::PI) / 3.0).cos() - offset);
98    } else {
99        // Multiple real roots
100        let u = (-q / 2.0).cbrt();
101        roots.push(2.0 * u - offset);
102        roots.push(-u - offset);
103    }
104
105    roots
106}
107
108/// Evaluates whether point `p` is inside a closed path composed of quadratic Béziers
109/// according to the specified fill rule (NonZero or EvenOdd).
110pub fn is_point_inside_closed_path_rule(
111    p: Point2D,
112    curves: &[QuadraticBezier],
113    rule: FillRule,
114) -> bool {
115    let mut winding = 0;
116    let mut ray_crossings = 0;
117
118    for curve in curves {
119        // Count crossings of a horizontal ray from p towards +X
120        let p0 = curve.p0;
121        let p2 = curve.p2;
122
123        let crosses_y = (p0.y <= p.y && p2.y > p.y) || (p2.y <= p.y && p0.y > p.y);
124        if crosses_y {
125            // Solve curve y(t) = p.y
126            // a_y t^2 + b_y t + c_y = 0
127            let a_y = p0.y - 2.0 * curve.p1.y + p2.y;
128            let b_y = 2.0 * (curve.p1.y - p0.y);
129            let c_y = p0.y - p.y;
130
131            let mut t_candidates = Vec::new();
132            if a_y.abs() < 1e-6 {
133                if b_y.abs() > 1e-6 {
134                    t_candidates.push(-c_y / b_y);
135                }
136            } else {
137                let disc = b_y * b_y - 4.0 * a_y * c_y;
138                if disc >= 0.0 {
139                    let s = disc.sqrt();
140                    t_candidates.push((-b_y + s) / (2.0 * a_y));
141                    t_candidates.push((-b_y - s) / (2.0 * a_y));
142                }
143            }
144
145            for t in t_candidates {
146                if (0.0..=1.0).contains(&t) {
147                    let x_intersect = curve.evaluate(t).x;
148                    if x_intersect >= p.x {
149                        ray_crossings += 1;
150                        if p2.y > p0.y {
151                            winding += 1;
152                        } else {
153                            winding -= 1;
154                        }
155                    }
156                }
157            }
158        }
159    }
160
161    match rule {
162        FillRule::NonZero => winding != 0,
163        FillRule::EvenOdd => ray_crossings % 2 != 0,
164    }
165}
166
167/// Evaluates whether point `p` is inside a closed path composed of quadratic Béziers using standard NonZero winding number.
168#[inline]
169pub fn is_point_inside_closed_path(p: Point2D, curves: &[QuadraticBezier]) -> bool {
170    is_point_inside_closed_path_rule(p, curves, FillRule::NonZero)
171}