1use gpui_kit::{PathBuilder, Pixels, StrokeOptions, point, px};
6
7#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct PathPoint {
10 pub x: f32,
11 pub y: f32,
12}
13
14impl PathPoint {
15 pub const fn new(x: f32, y: f32) -> Self {
16 Self { x, y }
17 }
18
19 pub fn lerp(self, other: Self, t: f32) -> Self {
20 Self {
21 x: self.x + (other.x - self.x) * t,
22 y: self.y + (other.y - self.y) * t,
23 }
24 }
25
26 pub fn distance(self, other: Self) -> f32 {
27 (other.x - self.x).hypot(other.y - self.y)
28 }
29}
30
31#[derive(Clone, Debug, PartialEq)]
33pub struct PolylinePath {
34 pub points: Vec<PathPoint>,
35}
36
37impl PolylinePath {
38 pub fn new(points: Vec<PathPoint>) -> Self {
39 Self { points }
40 }
41
42 pub fn total_length(&self) -> f32 {
44 self.points.windows(2).map(|w| w[0].distance(w[1])).sum()
45 }
46
47 pub fn point_at(&self, u: f32) -> Option<PathPoint> {
49 if self.points.is_empty() {
50 return None;
51 }
52 if self.points.len() == 1 {
53 return Some(self.points[0]);
54 }
55 let total = self.total_length();
56 if total <= f32::EPSILON {
57 return Some(self.points[0]);
58 }
59 let target = u.clamp(0.0, 1.0) * total;
60 let mut acc = 0.0_f32;
61 for w in self.points.windows(2) {
62 let seg = w[0].distance(w[1]);
63 if acc + seg >= target && seg > f32::EPSILON {
64 let local = ((target - acc) / seg).clamp(0.0, 1.0);
65 return Some(w[0].lerp(w[1], local));
66 }
67 acc += seg;
68 }
69 self.points.last().copied()
70 }
71
72 pub fn slice(&self, start: f32, end: f32) -> Self {
75 let start = start.clamp(0.0, 1.0);
76 let end = end.clamp(0.0, 1.0);
77 if self.points.len() < 2 || end <= start {
78 return Self { points: Vec::new() };
79 }
80 let total = self.total_length();
81 if total <= f32::EPSILON {
82 return Self {
83 points: self.points.clone(),
84 };
85 }
86
87 let start_dist = start * total;
88 let end_dist = end * total;
89 let mut out = Vec::new();
90 let mut acc = 0.0_f32;
91
92 for w in self.points.windows(2) {
93 let seg = w[0].distance(w[1]);
94 let next_acc = acc + seg;
95 if next_acc >= start_dist && acc <= end_dist && seg > f32::EPSILON {
96 let t0 = ((start_dist - acc) / seg).clamp(0.0, 1.0);
97 let t1 = ((end_dist - acc) / seg).clamp(0.0, 1.0);
98 let p0 = w[0].lerp(w[1], t0);
99 let p1 = w[0].lerp(w[1], t1);
100 if out.is_empty() {
101 out.push(p0);
102 }
103 out.push(p1);
104 }
105 acc = next_acc;
106 }
107
108 Self { points: out }
109 }
110
111 pub fn resample(&self, count: usize) -> Self {
114 if count == 0 || self.points.is_empty() {
115 return Self { points: Vec::new() };
116 }
117 if count == 1 {
118 return Self {
119 points: vec![self.points[0]],
120 };
121 }
122 let points = (0..count)
123 .filter_map(|i| self.point_at(i as f32 / (count - 1) as f32))
124 .collect();
125 Self { points }
126 }
127
128 pub fn morph(&self, other: &Self, t: f32, sample_count: usize) -> Self {
131 let n = sample_count
132 .max(self.points.len())
133 .max(other.points.len())
134 .max(2);
135 let a = self.resample(n);
136 let b = other.resample(n);
137 let t = t.clamp(0.0, 1.0);
138 let points = a
139 .points
140 .iter()
141 .zip(b.points.iter())
142 .map(|(&pa, &pb)| pa.lerp(pb, t))
143 .collect();
144 Self { points }
145 }
146
147 pub fn to_stroke_builder(
149 &self,
150 offset_x: Pixels,
151 offset_y: Pixels,
152 stroke_width: Pixels,
153 ) -> Option<PathBuilder> {
154 if self.points.len() < 2 {
155 return None;
156 }
157 let mut builder = PathBuilder::stroke(stroke_width);
158 let ox = f32::from(offset_x);
159 let oy = f32::from(offset_y);
160 builder.move_to(point(px(ox + self.points[0].x), px(oy + self.points[0].y)));
161 for pt in &self.points[1..] {
162 builder.line_to(point(px(ox + pt.x), px(oy + pt.y)));
163 }
164 let _ = StrokeOptions::default();
165 Some(builder)
166 }
167}
168
169#[cfg(test)]
170mod tests {
171 use super::*;
172
173 #[test]
174 fn path_slice_truncates_proportionally_to_length() {
175 let check = PolylinePath::new(vec![
176 PathPoint::new(0.0, 0.0),
177 PathPoint::new(10.0, 0.0),
178 PathPoint::new(10.0, 10.0),
179 ]);
180 assert!((check.total_length() - 20.0).abs() < 1e-4);
181
182 let half = check.slice(0.0, 0.5);
183 assert!((half.total_length() - 10.0).abs() < 1e-4);
184 assert_eq!(half.points.last().copied(), Some(PathPoint::new(10.0, 0.0)));
185
186 let quarter = check.slice(0.25, 0.75);
187 assert!((quarter.total_length() - 10.0).abs() < 1e-4);
188 }
189
190 #[test]
191 fn path_morph_aligns_mismatched_vertex_counts() {
192 let line = PolylinePath::new(vec![PathPoint::new(0.0, 0.0), PathPoint::new(20.0, 0.0)]);
193 let v_shape = PolylinePath::new(vec![
194 PathPoint::new(0.0, 10.0),
195 PathPoint::new(10.0, 20.0),
196 PathPoint::new(20.0, 10.0),
197 ]);
198 let mid = line.morph(&v_shape, 0.5, 16);
199 assert_eq!(mid.points.len(), 16);
200 assert!((mid.points[0].y - 5.0).abs() < 1e-3);
202 assert!((mid.points[15].y - 5.0).abs() < 1e-3);
203 }
204}