hephaestus 0.1.0

Backend-agnostic 2D scene renderer for data visualization.
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Polyline-end clipping by a circle, ellipse, or axis-aligned rectangle.
//!
//! Trims the start (or end) of a polyline at the first segment that exits the
//! clip shape, *only* when the polyline's starting (or ending) point lies
//! inside the shape. A polyline that begins outside the shape is left
//! untouched — incidental crossings in the middle are not clipped.

use crate::color::{lerp_color, Color, ColorSpace};
use crate::geometry::{Point, Rect};

/// A shape used to clip a polyline endpoint. All variants are
/// axis-aligned; rotated ellipses or arbitrary kurbo shapes are out of
/// scope.
#[derive(Debug, Clone, Copy)]
pub enum EndClip {
    /// Closed disk centered at `center` with radius `radius`.
    Circle { center: Point, radius: f64 },
    /// Axis-aligned ellipse centered at `center` with x-radius `rx` and
    /// y-radius `ry`.
    Ellipse { center: Point, rx: f64, ry: f64 },
    /// Closed axis-aligned rectangle.
    Rect(Rect),
}

impl EndClip {
    /// Whether the closed shape contains the point.
    fn contains(&self, p: Point) -> bool {
        match *self {
            EndClip::Circle { center, radius } => (p - center).hypot() <= radius,
            EndClip::Ellipse { center, rx, ry } => {
                if rx <= 0.0 || ry <= 0.0 {
                    return false;
                }
                let dx = (p.x - center.x) / rx;
                let dy = (p.y - center.y) / ry;
                dx * dx + dy * dy <= 1.0
            }
            EndClip::Rect(r) => p.x >= r.x0 && p.x <= r.x1 && p.y >= r.y0 && p.y <= r.y1,
        }
    }

    /// For a segment from `p0` (assumed inside) to `p1`, return the smallest
    /// `t ∈ (0, 1]` such that `p0 + t·(p1-p0)` lies on the shape boundary.
    /// Returns `None` when the segment stays inside.
    fn exit_t(&self, p0: Point, p1: Point) -> Option<f64> {
        let d = p1 - p0;
        match *self {
            EndClip::Circle { center, radius } => exit_circle(p0, d, center, radius),
            EndClip::Ellipse { center, rx, ry } => {
                if rx <= 0.0 || ry <= 0.0 {
                    return None;
                }
                let u0 = Point::new((p0.x - center.x) / rx, (p0.y - center.y) / ry);
                let u1 = Point::new((p1.x - center.x) / rx, (p1.y - center.y) / ry);
                exit_circle(u0, u1 - u0, Point::ORIGIN, 1.0)
            }
            EndClip::Rect(r) => exit_rect(p0, d, r),
        }
    }
}

fn exit_circle(p0: Point, d: crate::geometry::Vec2, center: Point, radius: f64) -> Option<f64> {
    let f = p0 - center;
    let a = d.dot(d);
    if a == 0.0 {
        return None;
    }
    let b = 2.0 * f.dot(d);
    let c = f.dot(f) - radius * radius;
    let disc = b * b - 4.0 * a * c;
    if disc < 0.0 {
        return None;
    }
    let sqrt_disc = disc.sqrt();
    // Larger root — the exit for a segment starting inside.
    let t = (-b + sqrt_disc) / (2.0 * a);
    if t > 0.0 && t <= 1.0 {
        Some(t)
    } else {
        None
    }
}

fn exit_rect(p0: Point, d: crate::geometry::Vec2, r: Rect) -> Option<f64> {
    let mut t_exit = f64::INFINITY;
    if d.x > 0.0 {
        t_exit = t_exit.min((r.x1 - p0.x) / d.x);
    } else if d.x < 0.0 {
        t_exit = t_exit.min((r.x0 - p0.x) / d.x);
    }
    if d.y > 0.0 {
        t_exit = t_exit.min((r.y1 - p0.y) / d.y);
    } else if d.y < 0.0 {
        t_exit = t_exit.min((r.y0 - p0.y) / d.y);
    }
    if t_exit.is_finite() && t_exit > 0.0 && t_exit <= 1.0 {
        Some(t_exit)
    } else {
        None
    }
}

/// Apply optional clip-start and clip-end to a polyline, returning the
/// trimmed vertex list. Useful as a standalone preprocessing step before
/// piping the result into [`round_corners`](super::round_corners) or any
/// other vertex-list consumer.
///
/// `clip_start` trims only when `points[0]` is inside the shape — a polyline
/// beginning outside the clip shape is left untouched, even if its middle
/// passes through. Symmetric for `clip_end` against `points[len-1]`. May
/// return an empty `Vec` when the polyline is fully inside one of the clip
/// shapes.
pub fn clip_polyline(
    points: &[Point],
    clip_start: Option<EndClip>,
    clip_end: Option<EndClip>,
) -> Vec<Point> {
    if points.len() < 2 {
        return points.to_vec();
    }
    let mut pts = points.to_vec();
    if let Some(c) = clip_start {
        pts = trim_start(&pts, &c);
        if pts.len() < 2 {
            return pts;
        }
    }
    if let Some(c) = clip_end {
        pts.reverse();
        pts = trim_start(&pts, &c);
        pts.reverse();
    }
    pts
}

fn trim_start(points: &[Point], clip: &EndClip) -> Vec<Point> {
    let n = points.len();
    if n < 2 {
        return points.to_vec();
    }
    if !clip.contains(points[0]) {
        return points.to_vec();
    }
    for i in 0..(n - 1) {
        if let Some(t) = clip.exit_t(points[i], points[i + 1]) {
            let p_exit = points[i] + (points[i + 1] - points[i]) * t;
            let mut out = Vec::with_capacity(n - i);
            out.push(p_exit);
            out.extend_from_slice(&points[(i + 1)..]);
            return out;
        }
    }
    Vec::new()
}

/// Apply optional clip-start and clip-end to a polyline carrying parallel
/// per-vertex `widths` and `colors` arrays. Synthesised intersection
/// vertices receive width and colour linearly interpolated from the two
/// bracketing data vertices at the intersection parameter `t`, the colour
/// through `space`. Used by ribbon-mode geoms so the clipped polyline
/// keeps its per-vertex attributes aligned with the points list.
///
/// `widths` and `colors` must have the same length as `points`.
pub fn clip_polyline_with_attrs(
    points: &[Point],
    widths: &[f64],
    colors: &[Color],
    clip_start: Option<EndClip>,
    clip_end: Option<EndClip>,
    space: ColorSpace,
) -> (Vec<Point>, Vec<f64>, Vec<Color>) {
    debug_assert_eq!(points.len(), widths.len());
    debug_assert_eq!(points.len(), colors.len());
    if points.len() < 2 {
        return (points.to_vec(), widths.to_vec(), colors.to_vec());
    }
    let mut pts = points.to_vec();
    let mut ws = widths.to_vec();
    let mut cs = colors.to_vec();
    if let Some(c) = clip_start {
        let (np, nw, nc) = trim_start_with_attrs(&pts, &ws, &cs, &c, space);
        pts = np;
        ws = nw;
        cs = nc;
        if pts.len() < 2 {
            return (pts, ws, cs);
        }
    }
    if let Some(c) = clip_end {
        pts.reverse();
        ws.reverse();
        cs.reverse();
        let (np, nw, nc) = trim_start_with_attrs(&pts, &ws, &cs, &c, space);
        pts = np;
        ws = nw;
        cs = nc;
        pts.reverse();
        ws.reverse();
        cs.reverse();
    }
    (pts, ws, cs)
}

fn trim_start_with_attrs(
    points: &[Point],
    widths: &[f64],
    colors: &[Color],
    clip: &EndClip,
    space: ColorSpace,
) -> (Vec<Point>, Vec<f64>, Vec<Color>) {
    let n = points.len();
    if n < 2 {
        return (points.to_vec(), widths.to_vec(), colors.to_vec());
    }
    if !clip.contains(points[0]) {
        return (points.to_vec(), widths.to_vec(), colors.to_vec());
    }
    for i in 0..(n - 1) {
        if let Some(t) = clip.exit_t(points[i], points[i + 1]) {
            let p_exit = points[i] + (points[i + 1] - points[i]) * t;
            let w_exit = widths[i] + t * (widths[i + 1] - widths[i]);
            let c_exit = lerp_color(colors[i], colors[i + 1], t, space);
            let mut out_p = Vec::with_capacity(n - i);
            let mut out_w = Vec::with_capacity(n - i);
            let mut out_c = Vec::with_capacity(n - i);
            out_p.push(p_exit);
            out_w.push(w_exit);
            out_c.push(c_exit);
            out_p.extend_from_slice(&points[(i + 1)..]);
            out_w.extend_from_slice(&widths[(i + 1)..]);
            out_c.extend_from_slice(&colors[(i + 1)..]);
            return (out_p, out_w, out_c);
        }
    }
    (Vec::new(), Vec::new(), Vec::new())
}

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

    fn pt(x: f64, y: f64) -> Point {
        Point::new(x, y)
    }

    #[test]
    fn circle_clip_trims_at_entry() {
        // Segment from (-5, 0) into unit circle at origin.
        let pts = [pt(0.0, 0.0), pt(5.0, 0.0)];
        let clip = EndClip::Circle {
            center: Point::ORIGIN,
            radius: 1.0,
        };
        let out = clip_polyline(&pts, Some(clip), None);
        assert_eq!(out.len(), 2);
        assert!((out[0].x - 1.0).abs() < 1e-9);
        assert!(out[0].y.abs() < 1e-9);
        assert_eq!(out[1], pt(5.0, 0.0));
    }

    #[test]
    fn circle_no_clip_when_start_outside() {
        let pts = [pt(-5.0, 0.0), pt(5.0, 0.0)];
        let clip = EndClip::Circle {
            center: Point::ORIGIN,
            radius: 1.0,
        };
        let out = clip_polyline(&pts, Some(clip), None);
        assert_eq!(out, vec![pt(-5.0, 0.0), pt(5.0, 0.0)]);
    }

    #[test]
    fn ellipse_clip_trims_at_boundary() {
        // Ellipse rx=2, ry=1 at origin. Start at origin, head along +x — exit at x=2.
        let pts = [pt(0.0, 0.0), pt(5.0, 0.0)];
        let clip = EndClip::Ellipse {
            center: Point::ORIGIN,
            rx: 2.0,
            ry: 1.0,
        };
        let out = clip_polyline(&pts, Some(clip), None);
        assert_eq!(out.len(), 2);
        assert!((out[0].x - 2.0).abs() < 1e-9);
        assert!(out[0].y.abs() < 1e-9);
    }

    #[test]
    fn rect_clip_trims_at_boundary() {
        let pts = [pt(0.5, 0.5), pt(2.0, 0.5)];
        let clip = EndClip::Rect(Rect::new(0.0, 0.0, 1.0, 1.0));
        let out = clip_polyline(&pts, Some(clip), None);
        assert!((out[0].x - 1.0).abs() < 1e-9);
        assert!((out[0].y - 0.5).abs() < 1e-9);
    }

    #[test]
    fn polyline_fully_inside_returns_empty() {
        let pts = [pt(0.0, 0.0), pt(0.5, 0.0), pt(0.5, 0.5)];
        let clip = EndClip::Circle {
            center: Point::ORIGIN,
            radius: 5.0,
        };
        let out = clip_polyline(&pts, Some(clip), None);
        assert!(out.is_empty());
    }

    #[test]
    fn both_ends_clipped() {
        // Segment from inside-circle-A to inside-circle-B.
        let pts = [pt(0.0, 0.0), pt(10.0, 0.0)];
        let clip_a = EndClip::Circle {
            center: Point::ORIGIN,
            radius: 1.0,
        };
        let clip_b = EndClip::Circle {
            center: pt(10.0, 0.0),
            radius: 1.0,
        };
        let out = clip_polyline(&pts, Some(clip_a), Some(clip_b));
        assert_eq!(out.len(), 2);
        assert!((out[0].x - 1.0).abs() < 1e-9);
        assert!((out[1].x - 9.0).abs() < 1e-9);
    }

    #[test]
    fn attrs_clip_lerps_widths_and_colors_at_cut() {
        // Segment from (0, 0) to (5, 0) — exits unit circle at x = 1, t = 0.2.
        let pts = [pt(0.0, 0.0), pt(5.0, 0.0)];
        let widths = [2.0_f64, 12.0];
        let colors = [
            Color::new([0.0, 0.0, 0.0, 1.0]),
            Color::new([1.0, 1.0, 1.0, 1.0]),
        ];
        let clip = EndClip::Circle {
            center: Point::ORIGIN,
            radius: 1.0,
        };
        let (out_p, out_w, out_c) =
            clip_polyline_with_attrs(&pts, &widths, &colors, Some(clip), None, ColorSpace::Srgb);
        assert_eq!(out_p.len(), 2);
        assert_eq!(out_w.len(), 2);
        assert_eq!(out_c.len(), 2);
        // Cut at t = 1/5.
        assert!((out_p[0].x - 1.0).abs() < 1e-9);
        // width lerp: 2.0 + 0.2 * (12.0 - 2.0) = 4.0
        assert!((out_w[0] - 4.0).abs() < 1e-9);
        // color lerp componentwise at t=0.2.
        assert!((out_c[0].components[0] - 0.2).abs() < 1e-5);
        assert!((out_c[0].components[1] - 0.2).abs() < 1e-5);
        // Original second vertex passes through.
        assert_eq!(out_p[1], pt(5.0, 0.0));
        assert!((out_w[1] - 12.0).abs() < 1e-9);
    }

    #[test]
    fn attrs_clip_end_lerps_at_reverse_walk() {
        // Segment from (-10, 0) to (10, 0) — clip end against unit circle at origin.
        let pts = [pt(-10.0, 0.0), pt(10.0, 0.0)];
        let widths = [2.0_f64, 12.0];
        let colors = [
            Color::new([0.0, 0.0, 0.0, 1.0]),
            Color::new([1.0, 1.0, 1.0, 1.0]),
        ];
        let clip = EndClip::Circle {
            center: Point::ORIGIN,
            radius: 1.0,
        };
        let (out_p, out_w, _out_c) =
            clip_polyline_with_attrs(&pts, &widths, &colors, None, Some(clip), ColorSpace::Srgb);
        // Endpoint at (10, 0) is inside? It's *outside* the unit circle (distance 10),
        // so this should be a no-op. Let me adjust the test.
        assert_eq!(out_p.len(), 2);
        assert_eq!(out_p[0], pt(-10.0, 0.0));
        assert_eq!(out_p[1], pt(10.0, 0.0));
        // unchanged widths.
        assert!((out_w[0] - 2.0).abs() < 1e-9);
        assert!((out_w[1] - 12.0).abs() < 1e-9);
    }

    #[test]
    fn attrs_clip_both_ends() {
        // Polyline from inside-A to inside-B; both clips fire and both
        // synthesised endpoints carry lerped attrs.
        let pts = [pt(0.0, 0.0), pt(10.0, 0.0)];
        let widths = [0.0_f64, 10.0];
        let colors = [
            Color::new([0.0, 0.0, 0.0, 1.0]),
            Color::new([1.0, 0.0, 0.0, 1.0]),
        ];
        let clip_a = EndClip::Circle {
            center: Point::ORIGIN,
            radius: 1.0,
        };
        let clip_b = EndClip::Circle {
            center: pt(10.0, 0.0),
            radius: 1.0,
        };
        let (out_p, out_w, _out_c) = clip_polyline_with_attrs(
            &pts,
            &widths,
            &colors,
            Some(clip_a),
            Some(clip_b),
            ColorSpace::Srgb,
        );
        assert_eq!(out_p.len(), 2);
        // Start cut at t = 0.1 → width = 1.0.
        assert!((out_p[0].x - 1.0).abs() < 1e-9);
        assert!((out_w[0] - 1.0).abs() < 1e-9);
        // End cut at t = 0.9 → width = 9.0.
        assert!((out_p[1].x - 9.0).abs() < 1e-9);
        assert!((out_w[1] - 9.0).abs() < 1e-9);
    }

    #[test]
    fn attrs_clip_no_op_when_start_outside() {
        let pts = [pt(-5.0, 0.0), pt(5.0, 0.0)];
        let widths = [1.0_f64, 2.0];
        let colors = [
            Color::new([0.0, 0.0, 0.0, 1.0]),
            Color::new([1.0, 0.0, 0.0, 1.0]),
        ];
        let clip = EndClip::Circle {
            center: Point::ORIGIN,
            radius: 1.0,
        };
        let (out_p, out_w, _out_c) =
            clip_polyline_with_attrs(&pts, &widths, &colors, Some(clip), None, ColorSpace::Srgb);
        assert_eq!(out_p, vec![pt(-5.0, 0.0), pt(5.0, 0.0)]);
        assert_eq!(out_w, vec![1.0, 2.0]);
    }

    #[test]
    fn multi_segment_polyline_clip_finds_first_exit() {
        // Start at origin (inside unit circle); zig-zag out and back in.
        let pts = [
            pt(0.0, 0.0),
            pt(0.5, 0.0),
            pt(2.0, 0.0), // exits unit circle on this segment
            pt(2.0, 1.0),
        ];
        let clip = EndClip::Circle {
            center: Point::ORIGIN,
            radius: 1.0,
        };
        let out = clip_polyline(&pts, Some(clip), None);
        // Exit on segment (0.5, 0) -> (2.0, 0) at x = 1.
        assert_eq!(out.len(), 3);
        assert!((out[0].x - 1.0).abs() < 1e-9);
        assert_eq!(out[1], pt(2.0, 0.0));
        assert_eq!(out[2], pt(2.0, 1.0));
    }
}