Skip to main content

strata_gpu_path/
curve.rs

1//! Parametric Quadratic and Cubic Bézier mathematical primitives.
2
3/// 2D point representation for vector curves.
4#[derive(Debug, Clone, Copy, PartialEq, Default)]
5pub struct Point2D {
6    pub x: f32,
7    pub y: f32,
8}
9
10impl Point2D {
11    pub const fn new(x: f32, y: f32) -> Self {
12        Self { x, y }
13    }
14
15    pub fn distance_squared(&self, other: Point2D) -> f32 {
16        let dx = self.x - other.x;
17        let dy = self.y - other.y;
18        dx * dx + dy * dy
19    }
20
21    pub fn distance(&self, other: Point2D) -> f32 {
22        self.distance_squared(other).sqrt()
23    }
24
25    pub fn lerp(&self, other: Point2D, t: f32) -> Point2D {
26        Point2D::new(
27            self.x + (other.x - self.x) * t,
28            self.y + (other.y - self.y) * t,
29        )
30    }
31}
32
33/// Axis-aligned 2D bounding box.
34#[derive(Debug, Clone, Copy, PartialEq)]
35pub struct Rect2D {
36    pub min_x: f32,
37    pub min_y: f32,
38    pub max_x: f32,
39    pub max_y: f32,
40}
41
42impl Rect2D {
43    pub fn new(min_x: f32, min_y: f32, max_x: f32, max_y: f32) -> Self {
44        Self {
45            min_x,
46            min_y,
47            max_x,
48            max_y,
49        }
50    }
51
52    pub fn contains(&self, p: Point2D) -> bool {
53        p.x >= self.min_x && p.x <= self.max_x && p.y >= self.min_y && p.y <= self.max_y
54    }
55
56    pub fn width(&self) -> f32 {
57        self.max_x - self.min_x
58    }
59
60    pub fn height(&self) -> f32 {
61        self.max_y - self.min_y
62    }
63}
64
65impl From<strata_primitives::geom::Point<f32>> for Point2D {
66    fn from(p: strata_primitives::geom::Point<f32>) -> Self {
67        Self::new(p.x, p.y)
68    }
69}
70
71impl From<Point2D> for strata_primitives::geom::Point<f32> {
72    fn from(p: Point2D) -> Self {
73        strata_primitives::geom::Point::new(p.x, p.y)
74    }
75}
76
77impl From<strata_primitives::geom::Rect<f32>> for Rect2D {
78    fn from(r: strata_primitives::geom::Rect<f32>) -> Self {
79        Self::new(r.min_x(), r.min_y(), r.max_x(), r.max_y())
80    }
81}
82
83impl From<Rect2D> for strata_primitives::geom::Rect<f32> {
84    fn from(r: Rect2D) -> Self {
85        strata_primitives::geom::Rect::from_xywh(r.min_x, r.min_y, r.width(), r.height())
86    }
87}
88
89/// Quadratic Bézier curve defined by three control points: P0 (start), P1 (control), P2 (end).
90#[derive(Debug, Clone, Copy, PartialEq)]
91pub struct QuadraticBezier {
92    pub p0: Point2D,
93    pub p1: Point2D,
94    pub p2: Point2D,
95}
96
97impl QuadraticBezier {
98    pub const fn new(p0: Point2D, p1: Point2D, p2: Point2D) -> Self {
99        Self { p0, p1, p2 }
100    }
101
102    /// Evaluates curve at parameter t in [0.0, 1.0].
103    /// B(t) = (1-t)^2 * P0 + 2t(1-t) * P1 + t^2 * P2
104    pub fn evaluate(&self, t: f32) -> Point2D {
105        let u = 1.0 - t;
106        let x = u * u * self.p0.x + 2.0 * u * t * self.p1.x + t * t * self.p2.x;
107        let y = u * u * self.p0.y + 2.0 * u * t * self.p1.y + t * t * self.p2.y;
108        Point2D::new(x, y)
109    }
110
111    /// First derivative tangent vector B'(t) = 2(1-t)(P1 - P0) + 2t(P2 - P1).
112    pub fn tangent(&self, t: f32) -> Point2D {
113        let u = 1.0 - t;
114        let dx = 2.0 * u * (self.p1.x - self.p0.x) + 2.0 * t * (self.p2.x - self.p1.x);
115        let dy = 2.0 * u * (self.p1.y - self.p0.y) + 2.0 * t * (self.p2.y - self.p1.y);
116        Point2D::new(dx, dy)
117    }
118
119    /// Subdivides the quadratic curve at parameter t using De Casteljau's algorithm.
120    pub fn split(&self, t: f32) -> (QuadraticBezier, QuadraticBezier) {
121        let p01 = self.p0.lerp(self.p1, t);
122        let p12 = self.p1.lerp(self.p2, t);
123        let p012 = p01.lerp(p12, t);
124
125        (
126            QuadraticBezier::new(self.p0, p01, p012),
127            QuadraticBezier::new(p012, p12, self.p2),
128        )
129    }
130
131    /// Computes tight axis-aligned bounding box including curve extrema.
132    pub fn bounding_box(&self) -> Rect2D {
133        let mut min_x = self.p0.x.min(self.p2.x);
134        let mut max_x = self.p0.x.max(self.p2.x);
135        let mut min_y = self.p0.y.min(self.p2.y);
136        let mut max_y = self.p0.y.max(self.p2.y);
137
138        // Check horizontal extrema: d/dt X(t) = 0
139        let denom_x = self.p0.x - 2.0 * self.p1.x + self.p2.x;
140        if denom_x.abs() > 1e-6 {
141            let tx = (self.p0.x - self.p1.x) / denom_x;
142            if (0.0..=1.0).contains(&tx) {
143                let x = self.evaluate(tx).x;
144                min_x = min_x.min(x);
145                max_x = max_x.max(x);
146            }
147        }
148
149        // Check vertical extrema: d/dt Y(t) = 0
150        let denom_y = self.p0.y - 2.0 * self.p1.y + self.p2.y;
151        if denom_y.abs() > 1e-6 {
152            let ty = (self.p0.y - self.p1.y) / denom_y;
153            if (0.0..=1.0).contains(&ty) {
154                let y = self.evaluate(ty).y;
155                min_y = min_y.min(y);
156                max_y = max_y.max(y);
157            }
158        }
159
160        Rect2D::new(min_x, min_y, max_x, max_y)
161    }
162}
163
164/// Cubic Bézier curve defined by four control points: P0, P1, P2, P3.
165#[derive(Debug, Clone, Copy, PartialEq)]
166pub struct CubicBezier {
167    pub p0: Point2D,
168    pub p1: Point2D,
169    pub p2: Point2D,
170    pub p3: Point2D,
171}
172
173impl CubicBezier {
174    pub const fn new(p0: Point2D, p1: Point2D, p2: Point2D, p3: Point2D) -> Self {
175        Self { p0, p1, p2, p3 }
176    }
177
178    /// Evaluates curve at parameter t in [0.0, 1.0].
179    pub fn evaluate(&self, t: f32) -> Point2D {
180        let u = 1.0 - t;
181        let u2 = u * u;
182        let u3 = u2 * u;
183        let t2 = t * t;
184        let t3 = t2 * t;
185
186        let x =
187            u3 * self.p0.x + 3.0 * u2 * t * self.p1.x + 3.0 * u * t2 * self.p2.x + t3 * self.p3.x;
188        let y =
189            u3 * self.p0.y + 3.0 * u2 * t * self.p1.y + 3.0 * u * t2 * self.p2.y + t3 * self.p3.y;
190        Point2D::new(x, y)
191    }
192
193    /// Approximates a cubic Bézier curve with an adaptive sequence of quadratic Béziers for direct GPU evaluation.
194    pub fn to_quadratics(&self, tolerance: f32) -> Vec<QuadraticBezier> {
195        let mut result = Vec::new();
196        self.subdivide_to_quadratics(tolerance, &mut result);
197        result
198    }
199
200    fn subdivide_to_quadratics(&self, tolerance: f32, output: &mut Vec<QuadraticBezier>) {
201        // Compute midpoint quadratic control point approximation: C = (3 P1 - P0 + 3 P2 - P3) / 4
202        let cx = (3.0 * self.p1.x - self.p0.x + 3.0 * self.p2.x - self.p3.x) / 4.0;
203        let cy = (3.0 * self.p1.y - self.p0.y + 3.0 * self.p2.y - self.p3.y) / 4.0;
204        let quad = QuadraticBezier::new(self.p0, Point2D::new(cx, cy), self.p3);
205
206        // Test error at t = 0.5
207        let cubic_mid = self.evaluate(0.5);
208        let quad_mid = quad.evaluate(0.5);
209        let err = cubic_mid.distance(quad_mid);
210
211        if err <= tolerance {
212            output.push(quad);
213        } else {
214            // Split cubic in half using De Casteljau
215            let p01 = self.p0.lerp(self.p1, 0.5);
216            let p12 = self.p1.lerp(self.p2, 0.5);
217            let p23 = self.p2.lerp(self.p3, 0.5);
218            let p012 = p01.lerp(p12, 0.5);
219            let p123 = p12.lerp(p23, 0.5);
220            let p0123 = p012.lerp(p123, 0.5);
221
222            let left = CubicBezier::new(self.p0, p01, p012, p0123);
223            let right = CubicBezier::new(p0123, p123, p23, self.p3);
224
225            left.subdivide_to_quadratics(tolerance, output);
226            right.subdivide_to_quadratics(tolerance, output);
227        }
228    }
229}