From d00e3b6ecd70da260f6f5fbd6b1a346864a8b721 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 11:26:11 -0800 Subject: [PATCH] Packaging Tools: Fold Preview picks the print level by a pixel's longer side and draws the board opaque A print tipped away from the view shrinks up and down but not across, so the level of the print raster is now chosen by the longer of a pixel's two sides in flat terms, not its width alone, and fine print lines no longer alias when the view is tipped. The board's inside and outside colors were premultiplied by their alpha and then drawn with full alpha, so a see-through color drew darker instead. The depth buffer treats every panel as opaque, so the board is now drawn opaque in its colors' RGB and their alpha is not used. The tray test now checks that each wall's print faces away from the base on its own side and that the walls fold below the base. --- .../src/cmd/addons/packaging/fold/tests.rs | 20 +++++++-- crates/three-d/src/fold/raster.rs | 15 ++++--- crates/three-d/src/fold/raster/tests.rs | 43 +++++++++++++++++++ 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/cmd/addons/packaging/fold/tests.rs b/crates/engine/src/cmd/addons/packaging/fold/tests.rs index da7cd6a84e..4c423021f5 100644 --- a/crates/engine/src/cmd/addons/packaging/fold/tests.rs +++ b/crates/engine/src/cmd/addons/packaging/fold/tests.rs @@ -38,6 +38,14 @@ fn vector(v: &Value) -> [f64; 3] { [a[0].as_f64().unwrap(), a[1].as_f64().unwrap(), a[2].as_f64().unwrap()] } +/// The middle of the bounds of the points of SVG path data `d` (absolute moves and lines only). +fn middle(d: &str) -> [f64; 2] { + let n: Vec = d.split(|c: char| !(c.is_ascii_digit() || c == '.' || c == '-')).filter_map(|t| t.parse().ok()).collect(); + let span = |k: usize| n.iter().skip(k).step_by(2).fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), &t| (lo.min(t), hi.max(t))); + let ((x0, x1), (y0, y1)) = (span(0), span(1)); + [(x0 + x1) / 2.0, (y0 + y1) / 2.0] +} + fn near(a: [f64; 3], b: [f64; 3]) -> bool { a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-9) } @@ -61,9 +69,15 @@ fn a_tray_folds_its_walls_up_round_the_base() { assert!(panels[base]["outline"].as_str().unwrap().starts_with('M'), "SVG path data"); for (i, p) in panels.iter().enumerate().filter(|(i, _)| *i != base) { assert_eq!((p["parent"].as_u64(), p["depth"].as_u64()), (Some(base as u64), Some(1)), "{p}"); - // Each wall stands square to the base, its print facing out. - let z = vector(&p["frame"]["z"]); - assert!(z[2].abs() < 1e-9 && (z[0].hypot(z[1]) - 1.0).abs() < 1e-9, "wall {i}: {z:?}"); + // Each wall stands square to the base, below it (away from the print), its print facing out: + // away from the base's middle (190, 190), toward the side the wall lies on. + let [cx, cy] = middle(p["outline"].as_str().unwrap()); + let (x, y, o) = (vector(&p["frame"]["x"]), vector(&p["frame"]["y"]), vector(&p["frame"]["origin"])); + let placed: [f64; 3] = std::array::from_fn(|k| o[k] + x[k] * cx - y[k] * cy); + let (dx, dy) = (cx - 190.0, 190.0 - cy); + let out = if dx.abs() > dy.abs() { [dx.signum(), 0.0, 0.0] } else { [0.0, dy.signum(), 0.0] }; + assert!(near(vector(&p["frame"]["z"]), out), "wall {i}: {p}"); + assert!((placed[2] + 20.0).abs() < 1e-9, "wall {i}'s middle is 20 pt below the base: {placed:?}"); } let hinges = r["hinges"].as_array().unwrap(); assert_eq!(hinges.len(), 4); diff --git a/crates/three-d/src/fold/raster.rs b/crates/three-d/src/fold/raster.rs index 012a9621de..e481eee15d 100644 --- a/crates/three-d/src/fold/raster.rs +++ b/crates/three-d/src/fold/raster.rs @@ -179,7 +179,8 @@ impl Print { } /// What [`render`] draws: a folded blank, its print (if any), the color of the board's back and of -/// its printed side where the print has no art (straight RGBA), and the view. +/// its printed side where the print has no art (straight RGBA; the board is opaque, so their alpha +/// is not used), and the view. pub struct Scene<'a> { pub blank: &'a Blank, pub folded: &'a Folded, @@ -356,7 +357,9 @@ impl Fill<'_> { if !wins { continue; } - let footprint = self.flat_at(x + 1.0, y).map_or(0.0, |(u2, v2)| (u2 - u).hypot(v2 - v)); + // The print's footprint is the longer of a pixel's two sides in flat terms. + let across = |dx: f64, dy: f64| self.flat_at(x + dx, y + dy).map_or(0.0, |(u2, v2)| (u2 - u).hypot(v2 - v)); + let footprint = across(1.0, 0.0).max(across(0.0, 1.0)); let color = self.color(u, v, footprint); if let Some(px) = target.color.get_mut(i * 4..i * 4 + 4) { for (o, c) in px.iter_mut().zip(color) { @@ -408,9 +411,9 @@ impl Fill<'_> { } } -fn rgba(c: [u8; 4]) -> [f32; 4] { - let a = f32::from(c[3]) / 255.0; - [f32::from(c[0]) * a, f32::from(c[1]) * a, f32::from(c[2]) * a, f32::from(c[3])] +/// The board color `c` as drawn: its RGB, opaque. +fn opaque(c: [u8; 4]) -> [f32; 4] { + [f32::from(c[0]), f32::from(c[1]), f32::from(c[2]), 255.0] } /// The outline rings of `panel` in flat terms (u, v), flattened finely enough for `scale` pixels a @@ -433,7 +436,7 @@ pub fn render(scene: &Scene) -> Option { let pixels = width * height; let mut target = Target { width, height, color: vec![0; pixels * 4], depth: vec![f64::NEG_INFINITY; pixels], rank: vec![0; pixels], panel: vec![0; pixels] }; - let (inside, outside) = (rgba(scene.inside), rgba(scene.outside)); + let (inside, outside) = (opaque(scene.inside), opaque(scene.outside)); let near = camera.eye().map(|(eye, _)| eye * NEAREST); for (rank, &pi) in scene.folded.order.iter().enumerate() { let (Some(panel), Some(f)) = (scene.blank.panels.get(pi), scene.folded.panels.get(pi)) else { continue }; diff --git a/crates/three-d/src/fold/raster/tests.rs b/crates/three-d/src/fold/raster/tests.rs index 477040b571..551b671625 100644 --- a/crates/three-d/src/fold/raster/tests.rs +++ b/crates/three-d/src/fold/raster/tests.rs @@ -64,6 +64,49 @@ fn a_flat_panel_takes_the_print_s_color_under_each_pixel() { assert_eq!(pixel(&img, 50, 50), [lit(200.0), lit(100.0), lit(50.0), 255]); } +#[test] +fn the_board_is_opaque_whatever_the_alpha_of_its_colors() { + let shade = 0.6 + 0.4 * 0.74; + let lit = |c: f64| (c * shade).round() as u8; + let (b, f) = scene_of(vec![panel(vec![square(0.0, 0.0, 100.0, 100.0)])], vec![Pose::IDENTITY]); + for a in [0, 128] { + let img = render(&Scene { blank: &b, folded: &f, print: None, inside: [0, 0, 0, 255], outside: [200, 100, 50, a], camera: front() }) + .expect("drawn"); + assert_eq!(pixel(&img, 50, 50), [lit(200.0), lit(100.0), lit(50.0), 255], "outside alpha {a}"); + } + // A transparent inside color draws opaque too, on the nearer of two panels seen from the back. + let whole = || panel(vec![square(0.0, 0.0, 100.0, 100.0)]); + let (b, f) = scene_of(vec![whole(), whole()], vec![Pose::IDENTITY, moved(5.0)]); + let camera = Camera { rotate_y: 180.0, ..front() }; + let img = render(&Scene { blank: &b, folded: &f, print: None, inside: [0, 200, 0, 0], outside: WHITE, camera }).expect("drawn"); + let [r, g, bl, a] = pixel(&img, 50, 50); + assert!(r == 0 && bl == 0 && g > 100 && a == 255, "{:?}", [r, g, bl, a]); +} + +#[test] +fn a_print_squeezed_only_up_and_down_on_screen_samples_a_halved_level() { + // 64 × 64, rows alternating black and white: halved, they average to gray. + let mut px = vec![]; + for y in 0..64 { + for _ in 0..64 { + px.extend(if y % 2 == 0 { [0, 0, 0, 255] } else { [255, 255, 255, 255] }); + } + } + let print = Print::new(Rect::new(0.0, 0.0, 100.0, 100.0), 64, 64, px).expect("a print"); + let (b, f) = scene_of(vec![panel(vec![square(0.0, 0.0, 100.0, 100.0)])], vec![Pose::IDENTITY]); + // Tipped 80°: about 5.8 points a pixel up and down, 1 across. + let camera = Camera { rotate_x: 80.0, ..front() }; + let img = render(&Scene { blank: &b, folded: &f, print: Some(&print), inside: [0; 4], outside: WHITE, camera }).expect("drawn"); + let reds: Vec = (0..img.height) + .flat_map(|y| (0..img.width).map(move |x| (x, y))) + .filter(|&(x, y)| img.panel_at(x, y).is_some()) + .map(|(x, y)| pixel(&img, x, y)[0]) + .collect(); + assert!(!reds.is_empty()); + let (lo, hi) = (reds.iter().min().copied().unwrap_or(0), reds.iter().max().copied().unwrap_or(0)); + assert!(hi - lo <= 2, "an even gray, not black and white rows: {lo} to {hi}"); +} + #[test] fn the_back_of_a_panel_shows_the_inside_color() { let (b, f) = scene_of(vec![panel(vec![square(0.0, 0.0, 100.0, 100.0)])], vec![Pose::IDENTITY]);