From 0b8794042155a5e6d6f4175025abab4d2e76e65d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 12:26:26 -0800 Subject: [PATCH] Packaging Tools: Fold Preview halves the print down to one pixel and reads #-named swatches The print raster stopped after two halvings, so a 2,048-pixel print seen far out sampled a 512-pixel level and fine art aliased. It is now halved down to one pixel, and each halving weighs an odd last row or column by the part of the print it covers, so an edge color does not spread across a small level. A swatch whose name starts with # could not be the inside color, and one named like a hex color drew that hex color. The inside is now looked up as a swatch first and read as #rrggbb only when no swatch with a plain color has that name. --- crates/three-d/src/fold/raster.rs | 40 ++++++++++++++----- crates/three-d/src/fold/raster/tests.rs | 37 ++++++++++++++++- .../ui-egui/src/addons/packaging/preview.rs | 12 +++--- .../src/addons/packaging/preview/tests.rs | 11 ++++- docs/mcp.md | 2 +- 5 files changed, 81 insertions(+), 21 deletions(-) diff --git a/crates/three-d/src/fold/raster.rs b/crates/three-d/src/fold/raster.rs index e481eee15d..9cccbaf6d9 100644 --- a/crates/three-d/src/fold/raster.rs +++ b/crates/three-d/src/fold/raster.rs @@ -115,19 +115,35 @@ impl Level { out } - /// This level at half its size, rounded up, each pixel the mean of the four it covers (an odd - /// last row or column with itself). - fn half(&self) -> Option { + /// This level at half its size, rounded up, each pixel the mean of the pixels it covers weighted + /// by the area of the full print they cover. A pixel of this level is `step` pixels of the full + /// print (`base` pixels across and down) on a side, and fewer in the last column and row when + /// `base` is not a multiple of `step`. + fn half(&self, base: (u32, u32), step: u32) -> Option { let (w, h) = (self.width.div_ceil(2), self.height.div_ceil(2)); if (w, h) == (self.width, self.height) { return None; } + // The pixels of the full print that column or row `i` of this level covers, or 0 past it. + let covers = |i: u32, side: u32| u64::from(side).saturating_sub(u64::from(i) * u64::from(step)).min(u64::from(step)) as f32; let mut pixels = Vec::with_capacity(w as usize * h as usize * 4); - for y in 0..i64::from(h) { - for x in 0..i64::from(w) { - let px = [self.at(2 * x, 2 * y), self.at(2 * x + 1, 2 * y), self.at(2 * x, 2 * y + 1), self.at(2 * x + 1, 2 * y + 1)]; - for k in 0..4 { - pixels.push((px.iter().map(|p| p[k]).sum::() / 4.0).round() as u8); + for y in 0..h { + for x in 0..w { + let mut sum = [0.0f32; 4]; + let mut area = 0.0; + for (sx, sy) in [(2 * x, 2 * y), (2 * x + 1, 2 * y), (2 * x, 2 * y + 1), (2 * x + 1, 2 * y + 1)] { + if sx >= self.width || sy >= self.height { + continue; + } + let a = covers(sx, base.0) * covers(sy, base.1); + let p = self.at(i64::from(sx), i64::from(sy)); + for (s, c) in sum.iter_mut().zip(p) { + *s += c * a; + } + area += a; + } + for s in sum { + pixels.push(if area > 0.0 { (s / area).round() as u8 } else { 0 }); } } } @@ -135,7 +151,8 @@ impl Level { } } -/// The flat print: the document rect it covers and its raster at full size and at two halvings. +/// The flat print: the document rect it covers and its raster at full size and halved again and +/// again down to one pixel. #[derive(Clone, Debug)] pub struct Print { pub region: Rect, @@ -152,9 +169,10 @@ impl Print { return None; } let mut levels = vec![Level { width, height, pixels }]; - while levels.len() < 3 { - let Some(next) = levels.last().and_then(Level::half) else { break }; + let mut step = 1; + while let Some(next) = levels.last().and_then(|l| l.half((width, height), step)) { levels.push(next); + step *= 2; } Some(Print { region, levels }) } diff --git a/crates/three-d/src/fold/raster/tests.rs b/crates/three-d/src/fold/raster/tests.rs index 551b671625..8639615a53 100644 --- a/crates/three-d/src/fold/raster/tests.rs +++ b/crates/three-d/src/fold/raster/tests.rs @@ -177,7 +177,7 @@ fn unusable_views_and_prints_are_refused() { assert!(Print::new(region, MAX_SIDE + 1, 1, vec![0; (MAX_SIDE as usize + 1) * 4]).is_none()); assert!(Print::new(Rect::new(0.0, 0.0, 0.0, 10.0), 1, 1, vec![0; 4]).is_none(), "an empty region"); let p = Print::new(region, 8, 4, vec![255; 8 * 4 * 4]).expect("a print"); - assert_eq!(p.levels.iter().map(|l| (l.width, l.height)).collect::>(), vec![(8, 4), (4, 2), (2, 1)]); + assert_eq!(p.levels.iter().map(|l| (l.width, l.height)).collect::>(), vec![(8, 4), (4, 2), (2, 1), (1, 1)]); } #[test] @@ -213,6 +213,41 @@ fn a_print_far_smaller_on_screen_samples_a_halved_level() { assert!((80..=150).contains(&r), "gray, not a black or white column: {r}"); } +#[test] +fn a_print_sixteen_times_smaller_on_screen_samples_a_level_halved_four_times() { + // 64 × 64, columns in bands of 4 alternating black and white: halved twice, they still alternate; + // halved four times, they average to gray. + let mut px = vec![]; + for _ in 0..64 { + for x in 0..64 { + px.extend(if (x / 4) % 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"); + assert_eq!(print.levels.len(), 7, "64 down to 1"); + let (b, f) = scene_of(vec![panel(vec![square(0.0, 0.0, 100.0, 100.0)])], vec![Pose::IDENTITY]); + // 100 points in 3 pixels: about 21 print pixels a view pixel. + let camera = Camera { width: 3, height: 3, scale: 0.03, ..front() }; + let img = render(&Scene { blank: &b, folded: &f, print: Some(&print), inside: [0; 4], outside: WHITE, camera }).expect("drawn"); + let [r, ..] = pixel(&img, 0, 1); + assert!((90..=140).contains(&r), "gray, not a black or white band: {r}"); +} + +#[test] +fn a_halved_level_weighs_an_odd_last_column_by_the_area_it_covers() { + // 9 × 9, black but for a white last column: a ninth of the print, so the one-pixel level is + // a ninth white (255 / 9 is about 28), not half. + let mut px = vec![]; + for _ in 0..9 { + for x in 0..9 { + px.extend(if x == 8 { [255, 255, 255, 255] } else { [0, 0, 0, 255] }); + } + } + let print = Print::new(Rect::new(0.0, 0.0, 100.0, 100.0), 9, 9, px).expect("a print"); + assert_eq!(print.levels.iter().map(|l| (l.width, l.height)).collect::>(), vec![(9, 9), (5, 5), (3, 3), (2, 2), (1, 1)]); + assert_eq!(print.levels.last().map(|l| l.pixels.clone()), Some(vec![28, 28, 28, 255])); +} + #[test] fn the_bounds_take_in_every_folded_panel() { let (b, f) = diff --git a/crates/ui-egui/src/addons/packaging/preview.rs b/crates/ui-egui/src/addons/packaging/preview.rs index a85b8650d9..0b0188f44c 100644 --- a/crates/ui-egui/src/addons/packaging/preview.rs +++ b/crates/ui-egui/src/addons/packaging/preview.rs @@ -183,14 +183,12 @@ thread_local! { static RENDERER: std::cell::RefCell = std::cell::RefCell::new(vectorcraft_render::Renderer::new()); } -/// The board's inside color (straight RGBA) for `inside`: `#rrggbb`, or a swatch of `d` with a -/// plain color; `None` for anything else. +/// The board's inside color (straight RGBA) for `inside`: a swatch of `d` with a plain color, or else +/// `#rrggbb`; `None` for anything else. fn inside_color(d: &vectorcraft_engine::doc::Document, inside: &str) -> Option<[u8; 4]> { - if inside.starts_with('#') { - return vectorcraft_color::Color::from_hex(inside).map(|c| c.to_rgba8(1.0)); - } - match &d.swatch(inside)?.paint { - vectorcraft_color::Paint::Solid { color, .. } => Some(color.to_rgba8(1.0)), + match d.swatch(inside).map(|s| &s.paint) { + Some(vectorcraft_color::Paint::Solid { color, .. }) => Some(color.to_rgba8(1.0)), + _ if inside.starts_with('#') => vectorcraft_color::Color::from_hex(inside).map(|c| c.to_rgba8(1.0)), _ => None, } } diff --git a/crates/ui-egui/src/addons/packaging/preview/tests.rs b/crates/ui-egui/src/addons/packaging/preview/tests.rs index 9fdfb1972e..974556032b 100644 --- a/crates/ui-egui/src/addons/packaging/preview/tests.rs +++ b/crates/ui-egui/src/addons/packaging/preview/tests.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use egui::{Event, Modifiers, PointerButton, Pos2, vec2}; -use super::{Cache, ID, PrintKey, Settings, last_image, last_rect, prints_rendered, show, status, work_done}; +use super::{Cache, ID, PrintKey, Settings, inside_color, last_image, last_rect, prints_rendered, show, status, work_done}; use crate::{VectorcraftApp, menus}; /// An app with a tray blank (a 100 pt base at x 140 to 240, y 140 to 240, and a 40 pt wall on each @@ -123,6 +123,15 @@ fn ui_fold_preview_sets_the_window_and_refuses_bad_values() { // A swatch with a plain color names the inside. app.run("ui.foldPreview", json!({"inside": "White"})).unwrap(); assert_eq!(app.ui.fold_preview.inside, "White"); + // So does one whose name starts with #, and a swatch's name comes before a #rrggbb that reads the same. + app.run("swatch.new", json!({"name": "#brand", "color": "#0000ff"})).unwrap(); + app.run("swatch.new", json!({"name": "#00ff00", "color": "#0000ff"})).unwrap(); + app.run("ui.foldPreview", json!({"inside": "#brand"})).unwrap(); + assert_eq!(app.ui.fold_preview.inside, "#brand"); + let doc = &app.session.active().expect("a document").doc; + assert_eq!(inside_color(doc, "#brand"), Some([0, 0, 255, 255])); + assert_eq!(inside_color(doc, "#00ff00"), Some([0, 0, 255, 255])); + assert_eq!(inside_color(doc, "#ff0000"), Some([255, 0, 0, 255])); } #[test] diff --git a/docs/mcp.md b/docs/mcp.md index ac07dd1799..4aa61fb48b 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -13235,7 +13235,7 @@ and the print is a raster whose longest side is 2,048 pixels, rendered again whe perspective?, inside?, showFoldLines?, base?}`) sets the window, shows it and draws it again. A second run leaves the window open. `rotateX` (-180 to 180) tips the top toward the viewer and `rotateY` (-180 to 180) turns the right side toward the viewer, in degrees; `zoom` is 0.1 to 8, where 1 fits the folded blank; `perspective` is the field of view, 0 to -160 degrees, 0 for none; `inside` is `#rrggbb` or the name of a swatch with a plain color; `base` is a point in a panel +160 degrees, 0 for none; `inside` is the name of a swatch with a plain color, or else `#rrggbb`; `base` is a point in a panel of the document's blanks, or null for the largest panel. With perspective, the zoom of 1 keeps the eye at least twice the folded blank's radius from its middle. A value of the wrong kind or out of range is an error and sets nothing. VectorCraft's own defaults are a Three-Quarter view (`rotateX` 25, `rotateY` 35), no perspective, a white inside and the