hephaestus 0.2.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
454
455
456
457
//! End-to-end visual sanity for `BSplineGeom` — clamped uniform-knot
//! B-spline curves, one per mark. Mirrors `examples/line.rs` and
//! `examples/ribbon_geom.rs` for layout.
//!
//! Renders, all 1200×500 with full plotting chrome:
//!
//! - `bspline_1_cubic_vs_bezier.png` — single 4-point degree-3 spline.
//!   The clamped knot vector pins the curve to the first/last control
//!   points; interior points pull without passing through. Math
//!   equivalence to a cubic Bezier is verified separately in the unit
//!   tests.
//! - `bspline_2_parallel_coords.png` — five categorical x positions
//!   threading several smoothed curves through them.
//! - `bspline_3_polar_interpolation.png` — same control polygon
//!   under polar projection, one curve in `"domain"` mode and one in
//!   `"panel"` mode; demonstrates the `"interpolation"` channel.
//! - `bspline_4_endpoint_arrows.png` — spline with arrow-closed
//!   markers at both endpoints, confirming the tangent points along
//!   the first knot interval.
//! - `bspline_5_ribbon_gradient.png` — per-row stroke gradient + per-
//!   row linewidth taper along a spline, triggering the ribbon-mode
//!   variance-detect dispatch.
//! - `bspline_6_dashed_markers.png` — dashed pattern with embedded
//!   circle markers walked along the curve via the same arc-length
//!   walker LineGeom uses.

use hephaestus::backend::vello::VelloRenderer;
use hephaestus::color::{rgb8, Color};
use hephaestus::composition::{Composition, Patch, Span};
use hephaestus::geometry::Size;
use hephaestus::plot::chrome::axis::{Axis, AxisPlacement, PolarRing};
use hephaestus::plot::projection::Projection;
use hephaestus::plot::{linetype, scale, BSplineGeom, Plot, PlotComposition, Value};
use hephaestus::scales::chrome::AxisSide;
use hephaestus::scene::SceneBuilder;
use hephaestus::Renderer;

fn cell_comp() -> Composition {
    Composition::empty(1, 1).place(1, 1, Span::cell(), Patch::new("panel"))
}

fn main() {
    let (w, h) = (1200u32, 500u32);
    let dpi = 96.0;

    let mut renderer = VelloRenderer::new().expect("vello renderer init");
    let bg: Color = rgb8(248, 248, 252);

    cubic_vs_bezier(&mut renderer, w, h, dpi, bg);
    parallel_coordinates(&mut renderer, w, h, dpi, bg);
    polar_interpolation(&mut renderer, w, h, dpi, bg);
    endpoint_arrows(&mut renderer, w, h, dpi, bg);
    ribbon_gradient(&mut renderer, w, h, dpi, bg);
    dashed_markers(&mut renderer, w, h, dpi, bg);
}

// ── Render 1: clamped endpoints + interior pull ──
//
// Single 4-point degree-3 spline. The clamped knot vector forces the
// curve to pass exactly through the first and last control points
// while pulling toward — not through — the interior two. For a
// 4-point group this curve is mathematically a cubic Bezier
// (verified in the unit tests via `de_boor_4pt_cubic_matches_bezier_at_half`).
fn cubic_vs_bezier(renderer: &mut VelloRenderer, w: u32, h: u32, dpi: f64, bg: Color) {
    let xs = vec![1.0_f64, 4.0, 7.0, 9.0];
    let ys = vec![1.0_f64, 8.5, 1.0, 6.0];

    let mut plot = Plot::new(&cell_comp(), "panel")
        .title("Clamped 4-point B-spline (cubic)")
        .subtitle("First/last control points lie on the curve; interior points pull the curve toward them")
        .bind("x", "x")
        .bind("y", "y");
    plot.add_geom(
        BSplineGeom::builder()
            .set("x", xs.clone())
            .set("y", ys.clone())
            .set("stroke", rgb8(40, 60, 140))
            .set("linewidth", 3.0_f64)
            .build(),
    );
    plot.add_geom(
        hephaestus::plot::PointGeom::builder()
            .set("x", xs)
            .set("y", ys)
            .set("shape", "circle")
            .set("size", 6.0_f64)
            .set("fill", rgb8(220, 60, 90))
            .set("stroke", rgb8(40, 40, 40))
            .build(),
    );
    plot.add_axis(Axis::rail("x", AxisPlacement::Cartesian(AxisSide::Bottom)).title("x"));
    plot.add_axis(Axis::rail("y", AxisPlacement::Cartesian(AxisSide::Left)).title("y"));

    let mut view = PlotComposition::new(&cell_comp())
        .add_scale("x", scale::continuous(0.0..=10.0))
        .add_scale("y", scale::continuous(0.0..=10.0))
        .with_plot(plot);
    panic_on_issues(view.validate());
    render_to(
        renderer,
        &mut view,
        w,
        h,
        dpi,
        bg,
        "examples/bspline_1_cubic_vs_bezier.png",
    );
}

// ── Render 2: parallel coordinates ──
//
// Five categorical x positions, several smoothed curves threading
// through them. Each curve is one mark with five control points; the
// degree-3 spline pulls the curve toward each interior point without
// passing through it.
fn parallel_coordinates(renderer: &mut VelloRenderer, w: u32, h: u32, dpi: f64, bg: Color) {
    let n_lines = 6;
    let n_axes = 5;
    let xs: Vec<f64> = (0..(n_lines * n_axes))
        .map(|i| (i % n_axes) as f64)
        .collect();
    let ys: Vec<f64> = (0..(n_lines * n_axes))
        .map(|i| {
            let line = i / n_axes;
            let axis = i % n_axes;
            let phase = line as f64 * 0.45;
            0.5 + 0.4 * ((axis as f64 + phase) * 1.3).sin()
        })
        .collect();
    let keys: Vec<i64> = (0..(n_lines * n_axes))
        .map(|i| (i / n_axes) as i64)
        .collect();

    let mut plot = Plot::new(&cell_comp(), "panel")
        .title("Parallel coordinates with B-spline smoothing")
        .subtitle("Each curve is one mark; control points are the axis values")
        .bind("x", "axis")
        .bind("y", "value")
        .bind("stroke", "line_id");
    plot.add_geom(
        BSplineGeom::builder()
            .keys(keys.clone())
            .set("x", xs)
            .set("y", ys)
            .set("stroke", keys)
            .set("linewidth", 2.0_f64)
            .set("stroke_opacity", 0.85_f64)
            .build(),
    );
    plot.add_axis(Axis::rail("axis", AxisPlacement::Cartesian(AxisSide::Bottom)).title("Axis"));
    plot.add_axis(Axis::rail("value", AxisPlacement::Cartesian(AxisSide::Left)).title("Value"));

    let palette: Vec<Color> = vec![
        rgb8(220, 60, 90),
        rgb8(60, 130, 200),
        rgb8(80, 170, 100),
        rgb8(200, 130, 40),
        rgb8(140, 90, 200),
        rgb8(40, 160, 170),
    ];
    let mut view = PlotComposition::new(&cell_comp())
        .add_scale("axis", scale::continuous(0.0..=(n_axes as f64 - 1.0)))
        .add_scale("value", scale::continuous(0.0..=1.0))
        .add_scale(
            "line_id",
            scale::ordinal((0..(n_lines as i64)).collect::<Vec<_>>()).range_colors(palette),
        )
        .with_plot(plot);
    panic_on_issues(view.validate());
    render_to(
        renderer,
        &mut view,
        w,
        h,
        dpi,
        bg,
        "examples/bspline_2_parallel_coords.png",
    );
}

// ── Render 3: polar interpolation modes side by side ──
//
// Two curves sharing a control polygon: one with "domain"
// interpolation (built in channel-fraction space; polar curvature
// bends the curve between knots), one with "panel" (control points
// projected first; the curve smooths between the projected pixel
// positions). Cartesian users see no difference; this is purely a
// non-Cartesian display.
fn polar_interpolation(renderer: &mut VelloRenderer, w: u32, h: u32, dpi: f64, bg: Color) {
    let thetas = [0.1_f64, 0.25, 0.5, 0.75, 0.9];
    let radii = [0.85_f64, 0.45, 0.85, 0.45, 0.85];
    let n = thetas.len();

    // Two marks sharing one control polygon: the first carries
    // "domain" interpolation, the second "panel". The mark id picks
    // both the colour binding ("mode" scale) and the per-mark
    // "interpolation" channel.
    let mut xs: Vec<f64> = Vec::with_capacity(2 * n);
    let mut ys: Vec<f64> = Vec::with_capacity(2 * n);
    let mut keys: Vec<&'static str> = Vec::with_capacity(2 * n);
    let mut interps: Vec<&'static str> = Vec::with_capacity(2 * n);
    for (mode, _) in [("domain", 0), ("panel", 1)] {
        for i in 0..n {
            xs.push(thetas[i] * std::f64::consts::TAU);
            ys.push(radii[i]);
            keys.push(mode);
            interps.push(mode);
        }
    }

    let mut plot = Plot::new(&cell_comp(), "panel")
        .projection(Projection::polar())
        .title("Polar B-spline — interpolation modes")
        .subtitle("Red: \"domain\" (curve faithful in data space). Blue: \"panel\" (curve smooths in pixel space).")
        .bind("x", "theta")
        .bind("y", "radius")
        .bind("stroke", "mode");
    plot.add_geom(
        BSplineGeom::builder()
            .keys(keys)
            .set("x", xs.clone())
            .set("y", ys.clone())
            .set("stroke", interps.clone())
            .set("interpolation", interps)
            .set("linewidth", 2.5_f64)
            .build(),
    );
    // Mark the five control points so the curves' relationship to the
    // polygon is visible.
    let ctrl_theta: Vec<f64> = thetas.iter().map(|t| t * std::f64::consts::TAU).collect();
    let ctrl_radius: Vec<f64> = radii.to_vec();
    plot.add_geom(
        hephaestus::plot::PointGeom::builder()
            .set("x", ctrl_theta)
            .set("y", ctrl_radius)
            .set("shape", "circle")
            .set("size", 6.0_f64)
            .set("fill", rgb8(40, 40, 40))
            .set("stroke", rgb8(40, 40, 40))
            .build(),
    );
    plot.add_axis(
        Axis::rail("theta", AxisPlacement::PolarAngular(PolarRing::Outer)).title("Theta"),
    );
    plot.add_axis(
        Axis::rail("radius", AxisPlacement::PolarRadius { theta_frac: 0.0 }).title("Radius"),
    );

    let mut view = PlotComposition::new(&cell_comp())
        .add_scale("theta", scale::continuous(0.0..=std::f64::consts::TAU))
        .add_scale("radius", scale::continuous(0.0..=1.0))
        .add_scale(
            "mode",
            scale::ordinal(["domain", "panel"])
                .range_colors([rgb8(220, 60, 90), rgb8(60, 130, 200)]),
        )
        .with_plot(plot);
    panic_on_issues(view.validate());
    render_to(
        renderer,
        &mut view,
        w,
        h,
        dpi,
        bg,
        "examples/bspline_3_polar_interpolation.png",
    );
}

// ── Render 4: endpoint arrows ──
fn endpoint_arrows(renderer: &mut VelloRenderer, w: u32, h: u32, dpi: f64, bg: Color) {
    let xs = vec![1.0_f64, 2.0, 6.0, 8.0, 9.0];
    let ys = vec![1.5_f64, 8.0, 2.0, 8.5, 4.0];

    let mut plot = Plot::new(&cell_comp(), "panel")
        .title("B-spline with endpoint markers")
        .subtitle("Stroke is trimmed automatically so the arrow tips land on the data endpoints")
        .bind("x", "x")
        .bind("y", "y");
    plot.add_geom(
        BSplineGeom::builder()
            .set("x", xs.clone())
            .set("y", ys.clone())
            .set("stroke", rgb8(40, 80, 170))
            .set("linewidth", 3.0_f64)
            .set("start_marker", "arrow-closed")
            .set("end_marker", "arrow-closed")
            .set("start_marker_size", 14.0_f64)
            .set("end_marker_size", 14.0_f64)
            .build(),
    );
    plot.add_geom(
        hephaestus::plot::PointGeom::builder()
            .set("x", xs)
            .set("y", ys)
            .set("shape", "circle")
            .set("size", 5.0_f64)
            .set("fill", rgb8(180, 180, 200))
            .set("stroke", rgb8(60, 60, 80))
            .build(),
    );
    plot.add_axis(Axis::rail("x", AxisPlacement::Cartesian(AxisSide::Bottom)).title("x"));
    plot.add_axis(Axis::rail("y", AxisPlacement::Cartesian(AxisSide::Left)).title("y"));

    let mut view = PlotComposition::new(&cell_comp())
        .add_scale("x", scale::continuous(0.0..=10.0))
        .add_scale("y", scale::continuous(0.0..=10.0))
        .with_plot(plot);
    panic_on_issues(view.validate());
    render_to(
        renderer,
        &mut view,
        w,
        h,
        dpi,
        bg,
        "examples/bspline_4_endpoint_arrows.png",
    );
}

// ── Render 5: per-row gradient + linewidth taper (ribbon-mode) ──
fn ribbon_gradient(renderer: &mut VelloRenderer, w: u32, h: u32, dpi: f64, bg: Color) {
    let n = 12;
    let xs: Vec<f64> = (0..n)
        .map(|i| i as f64 * (10.0 / (n as f64 - 1.0)))
        .collect();
    let ys: Vec<f64> = (0..n)
        .map(|i| {
            let t = i as f64 / (n as f64 - 1.0);
            5.0 + 3.5 * (t * std::f64::consts::TAU).sin()
        })
        .collect();
    let widths: Vec<f64> = (0..n)
        .map(|i| {
            let t = i as f64 / (n as f64 - 1.0);
            2.0 + 12.0 * t
        })
        .collect();
    let strokes: Vec<i64> = (0..n as i64).collect();

    let mut plot = Plot::new(&cell_comp(), "panel")
        .title("Ribbon-mode B-spline — per-row colour and width")
        .subtitle("Variance-detect upgrades to polyline_ribbon_full")
        .bind("x", "x")
        .bind("y", "y")
        .bind("stroke", "row");
    plot.add_geom(
        BSplineGeom::builder()
            .set("x", xs)
            .set("y", ys)
            .set("linewidth", widths)
            .set("stroke", strokes)
            .build(),
    );
    plot.add_axis(Axis::rail("x", AxisPlacement::Cartesian(AxisSide::Bottom)).title("x"));
    plot.add_axis(Axis::rail("y", AxisPlacement::Cartesian(AxisSide::Left)).title("y"));

    let mut view = PlotComposition::new(&cell_comp())
        .add_scale("x", scale::continuous(0.0..=10.0))
        .add_scale("y", scale::continuous(0.0..=10.0))
        .add_scale(
            "row",
            scale::continuous(0.0..=(n as f64 - 1.0))
                .range_colors([rgb8(240, 180, 60), rgb8(190, 50, 130)]),
        )
        .with_plot(plot);
    panic_on_issues(view.validate());
    render_to(
        renderer,
        &mut view,
        w,
        h,
        dpi,
        bg,
        "examples/bspline_5_ribbon_gradient.png",
    );
}

// ── Render 6: dashed + marker linetype ──
fn dashed_markers(renderer: &mut VelloRenderer, w: u32, h: u32, dpi: f64, bg: Color) {
    let xs = vec![1.0_f64, 3.0, 5.0, 7.0, 9.0];
    let ys = vec![2.0_f64, 7.0, 3.5, 8.0, 4.0];

    let pat = linetype::pattern([
        linetype::dash(6.0),
        linetype::gap(3.0),
        linetype::marker("circle"),
        linetype::gap(3.0),
    ]);

    let mut plot = Plot::new(&cell_comp(), "panel")
        .title("B-spline with dashed-plus-marker linetype")
        .subtitle("Pattern walked along the arc length of the flattened curve")
        .bind("x", "x")
        .bind("y", "y");
    plot.add_geom(
        BSplineGeom::builder()
            .set("x", xs)
            .set("y", ys)
            .set("stroke", rgb8(40, 60, 140))
            .set("linewidth", 3.0_f64)
            .set("linetype", Value::Linetype(pat))
            .build(),
    );
    plot.add_axis(Axis::rail("x", AxisPlacement::Cartesian(AxisSide::Bottom)).title("x"));
    plot.add_axis(Axis::rail("y", AxisPlacement::Cartesian(AxisSide::Left)).title("y"));

    let mut view = PlotComposition::new(&cell_comp())
        .add_scale("x", scale::continuous(0.0..=10.0))
        .add_scale("y", scale::continuous(0.0..=10.0))
        .with_plot(plot);
    panic_on_issues(view.validate());
    render_to(
        renderer,
        &mut view,
        w,
        h,
        dpi,
        bg,
        "examples/bspline_6_dashed_markers.png",
    );
}

// ── Helpers ──

fn panic_on_issues<T: std::fmt::Debug>(issues: Vec<T>) {
    if !issues.is_empty() {
        panic!("validate() reported issues: {issues:?}");
    }
}

fn render_to(
    renderer: &mut VelloRenderer,
    view: &mut PlotComposition,
    w: u32,
    h: u32,
    dpi: f64,
    bg: Color,
    out_relative: &str,
) {
    {
        let scene = renderer.scene();
        scene.clear();
        view.render(scene, Size::new(w as f64, h as f64), dpi);
    }
    write_buffer(renderer, w, h, bg, out_relative);
}

fn write_buffer(renderer: &mut VelloRenderer, w: u32, h: u32, bg: Color, out_relative: &str) {
    let mut pixels = vec![0u8; (w * h * 4) as usize];
    renderer
        .render_to_buffer(w, h, bg, &mut pixels)
        .expect("render");
    let path = std::env::current_dir().unwrap().join(out_relative);
    hephaestus::image::write_png(&path, w, h, &pixels).expect("write png");
    println!("wrote {}", path.display());
}