b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 1 | //! Renders a plan's keyframes side by side as SVG. |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 2 | //! |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 3 | //! `cargo run -p monolith-planner --example filmstrip -- swap > swap.svg` |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 4 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 5 | use monolith_planner::{Config, Rect, Style, plan}; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 6 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 7 | const W: f64 = 320.0; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 8 | const H: f64 = 200.0; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 9 | const G: f64 = 6.0; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 10 | const COLOURS: [&str; 6] = [ |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 11 | "#e0774a", "#4aa3e0", "#7bc96f", "#c77be0", "#e0c24a", "#4ae0c2", |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 12 | ]; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 13 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 14 | fn cols(n: usize) -> Vec<Rect> { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 15 | let w = (W - G * (n + 1) as f64) / n as f64; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 16 | (0..n) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 17 | .map(|i| Rect::new(G + i as f64 * (w + G), G, w, H - 2.0 * G)) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 18 | .collect() |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 19 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 20 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 21 | fn rows(n: usize) -> Vec<Rect> { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 22 | let h = (H - G * (n + 1) as f64) / n as f64; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 23 | (0..n) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 24 | .map(|i| Rect::new(G, G + i as f64 * (h + G), W - 2.0 * G, h)) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 25 | .collect() |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 26 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 27 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 28 | fn scenario(name: &str) -> Option<(Vec<Rect>, Vec<Rect>)> { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 29 | Some(match name { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 30 | "swap" => { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 31 | let c = cols(2); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 32 | (c.clone(), vec![c[1], c[0]]) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 33 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 34 | "reverse" => { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 35 | let c = cols(4); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 36 | (c.clone(), c.iter().rev().copied().collect()) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 37 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 38 | "flip" => (cols(3), rows(3)), |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 39 | _ => return None, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 40 | }) |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 41 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 42 | |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 43 | fn main() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 44 | let name = std::env::args().nth(1).unwrap_or_else(|| "swap".into()); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 45 | let Some((from, to)) = scenario(&name) else { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 46 | eprintln!("scenarios: swap, reverse, flip"); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 47 | std::process::exit(2); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 48 | }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 49 | let cfg = Config { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 50 | style: Style::Orthogonal, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 51 | gap: G, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 52 | ..Config::default() |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 53 | }; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 54 | let p = plan(&from, &to, &cfg); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 55 | let frames = p.keyframes.len() as f64; |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 56 | println!( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 57 | r#"<svg xmlns="http://www.w3.org/2000/svg" width="{}" height="{}" style="background:#111317">"#, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 58 | frames * (W + 20.0), |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 59 | H + 40.0 |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 60 | ); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 61 | for (k, frame) in p.keyframes.iter().enumerate() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 62 | let ox = k as f64 * (W + 20.0); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 63 | println!(r##"<rect x="{ox}" y="0" width="{W}" height="{H}" fill="#1b1e25"/>"##); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 64 | for (i, r) in frame.iter().enumerate() { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 65 | if r.w > 0.5 && r.h > 0.5 { |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 66 | println!( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 67 | r#"<rect x="{}" y="{}" width="{}" height="{}" rx="4" fill="{}"/>"#, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 68 | ox + r.x, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 69 | r.y, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 70 | r.w, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 71 | r.h, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 72 | COLOURS[i % COLOURS.len()] |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 73 | ); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 74 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 75 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 76 | println!( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 77 | r##"<text x="{}" y="{}" fill="#8a909c" font-family="sans-serif" font-size="13">keyframe {k}</text>"##, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 78 | ox + 4.0, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 79 | H + 18.0 |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 80 | ); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 81 | } |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 82 | println!( |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 83 | r##"<text x="4" y="{}" fill="#8a909c" font-family="sans-serif" font-size="13">{name}: {:?}</text>"##, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 84 | H + 34.0, |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 85 | p.groups.iter().map(|g| g.strategy).collect::<Vec<_>>() |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 86 | ); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 87 | println!("</svg>"); |
b563d2da1c80 | atagen | 2026-10-16 08:25 UTC | 88 | } |