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