From d5128ee0795e91df6f0244e7e08dbc766ce0bf7f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 07:20:59 -0800 Subject: [PATCH] Fold Preview: compare coplanar groups at one depth --- crates/three-d/src/fold/raster.rs | 45 +++++++++++++------ crates/three-d/src/fold/raster/tests.rs | 58 ++++++++++++++++++++++++- docs/mcp.md | 5 ++- docs/target-app-parity.md | 2 +- 4 files changed, 93 insertions(+), 17 deletions(-) diff --git a/crates/three-d/src/fold/raster.rs b/crates/three-d/src/fold/raster.rs index 6b5099bbab..343c5b893c 100644 --- a/crates/three-d/src/fold/raster.rs +++ b/crates/three-d/src/fold/raster.rs @@ -16,8 +16,6 @@ pub const MAX_SIDE: u32 = 4096; pub const MAX_PIXELS: u32 = 1 << 21; /// The share of the eye's distance a panel is cut at in front of the eye. const NEAREST: f64 = 1e-3; -/// How far apart (points of view depth) two panels in one plane may be and still tie. -const TIE: f64 = 1e-4; /// How far apart (the sine of the angle) two panels' normals may be for the panels to lie in one /// plane. const PARALLEL: f64 = 1e-9; @@ -269,6 +267,13 @@ struct Mapped { shade: f32, } +impl Mapped { + fn flat_at(&self, x: f64, y: f64) -> Option<(f64, f64)> { + let [u, v, w] = mul(&self.inverse, [x, y, 1.0]); + (w > 0.0 && w.is_finite()).then(|| (u / w, v / w)).filter(|(u, v)| u.is_finite() && v.is_finite()) + } +} + fn mul(m: &[[f64; 3]; 3], v: V3) -> V3 { [dot(m[0], v), dot(m[1], v), dot(m[2], v)] } @@ -340,6 +345,8 @@ struct Target { /// [`planes`] gives them), panel number and colors. struct Fill<'a> { mapped: &'a Mapped, + /// The group's comparison plane, or this panel when grouping is bypassed. + plane: &'a Mapped, rank: u16, planes: &'a [u16], panel: u16, @@ -354,8 +361,7 @@ struct Fill<'a> { impl Fill<'_> { /// The flat point (less [`Fill::origin`]) under pixel center `(x, y)`, or `None` behind the eye. fn flat_at(&self, x: f64, y: f64) -> Option<(f64, f64)> { - let [u, v, w] = mul(&self.mapped.inverse, [x, y, 1.0]); - (w > 0.0 && w.is_finite()).then(|| (u / w, v / w)).filter(|(u, v)| u.is_finite() && v.is_finite()) + self.mapped.flat_at(x, y) } /// The color of the panel at flat point `(u, v)`, `footprint` points across a pixel. @@ -382,15 +388,15 @@ impl Fill<'_> { for col in first as usize..=last as usize { let x = col as f64 + 0.5; let Some((u, v)) = self.flat_at(x, y) else { continue }; - let [a, b, c] = self.mapped.depth; - let z = u * a + v * b + c; + let Some((pu, pv)) = self.plane.flat_at(x, y) else { continue }; + let [a, b, c] = self.plane.depth; + let z = pu * a + pv * b + c; let i = row * target.width + col; let (Some(&zb), Some(&rb), Some(&pb)) = (target.depth.get(i), target.rank.get(i), target.panel.get(i)) else { continue }; - // Panels in one plane tie within `TIE` and stack by rank; panels in different planes - // that pass this close go by depth alone. Panel numbers count from 1. + // Members of a plane have the same comparison depth and stack by rank. Distinct + // planes compare by depth, then by their number at an exact crossing. let plane = |p: u16| usize::from(p).checked_sub(1).and_then(|p| self.planes.get(p)); - let one_plane = plane(self.panel).is_some_and(|a| plane(pb) == Some(a)); - let wins = if one_plane { z > zb + TIE || ((z - zb).abs() <= TIE && self.rank > rb) } else { z > zb }; + let wins = z.total_cmp(&zb).then_with(|| plane(self.panel).cmp(&plane(pb))).then(self.rank.cmp(&rb)).then(self.panel.cmp(&pb)).is_gt(); if !wins { continue; } @@ -639,6 +645,17 @@ pub fn render(scene: &Scene) -> Option { for (&pi, &rank) in scene.folded.order.iter().zip(&ranks) { let (Some(panel), Some(f)) = (scene.blank.panels.get(pi), scene.folded.panels.get(pi)) else { continue }; let Some(mapped) = map_panel(&local, &f.local) else { continue }; + let plane = if scene.folded.panels.len() <= super::MAX_PANELS { + planes.get(pi).and_then(|kind| { + planes + .iter() + .enumerate() + .filter(|(_, k)| *k == kind) + .find_map(|(i, _)| scene.folded.panels.get(i).and_then(|f| map_panel(&local, &f.local))) + }) + } else { + None + }; let Some(rings) = flat_rings(panel, camera.scale) else { continue }; let rings: Vec> = rings.into_iter().map(|r| r.into_iter().map(|[u, v]| [u - o[0], v - o[1]]).collect()).collect(); let projected: Vec> = rings @@ -661,6 +678,7 @@ pub fn render(scene: &Scene) -> Option { .collect(); let fill = Fill { mapped: &mapped, + plane: plane.as_ref().unwrap_or(&mapped), rank, planes: &planes, panel: u16::try_from(pi + 1).unwrap_or(u16::MAX), @@ -695,7 +713,7 @@ fn planes(folded: &Folded, blank: &Blank) -> Vec { let separation = |normal: V3, at: V3, points: &[V3; 4]| { points.iter().map(|p| dot(normal, [p[0] - at[0], p[1] - at[1], p[2] - at[2]]).abs()).fold(0.0, f64::max) }; - let mut kinds: Vec = vec![]; + let mut kinds: Vec> = vec![]; folded .panels .iter() @@ -717,10 +735,11 @@ fn planes(folded: &Folded, blank: &Blank) -> Vec { let hinges = f64::from(u32::try_from(f.depth.saturating_add(p.depth).saturating_add(1)).unwrap_or(u32::MAX)); length(c) <= PARALLEL && apart <= COPLANAR * hinges * (1.0 + size.max(p.size)) }; - let k = kinds.iter().position(same).unwrap_or_else(|| { - kinds.push(Plane { normal, at, size, depth: f.depth, corners }); + let k = kinds.iter().position(|group| group.iter().all(&same)).unwrap_or_else(|| { + kinds.push(vec![]); kinds.len() - 1 }); + kinds[k].push(Plane { normal, at, size, depth: f.depth, corners }); u16::try_from(k).unwrap_or(u16::MAX) }) .collect() diff --git a/crates/three-d/src/fold/raster/tests.rs b/crates/three-d/src/fold/raster/tests.rs index f7a419e4cd..5302e07372 100644 --- a/crates/three-d/src/fold/raster/tests.rs +++ b/crates/three-d/src/fold/raster/tests.rs @@ -37,6 +37,62 @@ fn moved(z: f64) -> Pose { Pose { t: [0.0, 0.0, z], ..Pose::IDENTITY } } +#[test] +fn a_middle_plane_does_not_join_two_separate_planes_into_one_stack() { + let whole = || panel(vec![square(0.0, 0.0, 100.0, 100.0)]); + let (b, f) = scene_of(vec![whole(), whole(), whole()], vec![moved(4e-13), moved(8e-13), moved(0.0)]); + let image = render(&Scene { blank: &b, folded: &f, print: None, inside: [0; 4], outside: WHITE, camera: front() }).expect("drawn"); + assert_eq!(image.panel_at(50, 50), Some(1), "the separate farther panel does not cover the near stack"); +} + +#[test] +fn a_caller_supplied_scene_past_the_panel_limit_keeps_actual_depth() { + let n = super::super::MAX_PANELS + 1; + let panels = (0..n).map(|_| panel(vec![square(0.0, 0.0, 100.0, 100.0)])).collect(); + let mut poses = vec![Pose::IDENTITY; n]; + poses[1] = moved(5.0); + let (b, f) = scene_of(panels, poses); + let image = render(&Scene { blank: &b, folded: &f, print: None, inside: [0; 4], outside: WHITE, camera: front() }).expect("drawn"); + assert_eq!(image.panel_at(50, 50), Some(1)); +} + +#[test] +fn crossing_a_stack_selects_the_same_pixel_in_every_draw_order() { + let angle = 1e-4_f64; + let (s, c) = angle.sin_cos(); + let crossing = Pose { m: [[c, 0.0, s], [0.0, 1.0, 0.0], [-s, 0.0, c]], t: [0.0, 0.0, s * 50.5 / c + 2e-13] }; + let maps = [Pose::IDENTITY, crossing, moved(4e-13)].map(|p| map_panel(&front(), &p).expect("mapped")); + let planes = [0, 1, 0]; + let ranks = [3, 2, 1]; + let mut winners = vec![]; + for order in [[0, 1, 2], [2, 1, 0], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1]] { + let mut target = Target { + width: 100, + height: 100, + color: vec![0; 40_000], + depth: vec![f64::NEG_INFINITY; 10_000], + rank: vec![0; 10_000], + panel: vec![0; 10_000], + }; + for i in order { + Fill { + mapped: &maps[i], + plane: &maps[usize::from(planes[i])], + rank: ranks[i], + planes: &planes, + panel: (i + 1) as u16, + print: None, + origin: [0.0; 2], + inside: [0.0; 4], + outside: [0.0; 4], + } + .span(&mut target, 50, 50.0, 51.0); + } + winners.push(target.panel[5050]); + } + assert!(winners.iter().all(|&winner| winner == winners[0]), "{winners:?}"); +} + fn pixel(img: &Image, x: u32, y: u32) -> [u8; 4] { let i = (y * img.width + x) as usize * 4; [img.pixels[i], img.pixels[i + 1], img.pixels[i + 2], img.pixels[i + 3]] @@ -155,7 +211,7 @@ fn nearly_parallel_panels_that_cross_use_depth_on_both_sides() { #[test] fn of_two_parallel_panels_the_nearer_shows_however_close() { - // 2 and 2.0001 points deep: a hair more than `TIE` apart once subtracted, though 2 + `TIE` + // 2 and 2.0001 points deep: a hair more than 0.0001 apart once subtracted, though 2 + 0.0001 // equals 2.0001. The panels lie in different planes, so the nearer shows whichever is drawn // first. let whole = || panel(vec![square(0.0, 0.0, 100.0, 100.0)]); diff --git a/docs/mcp.md b/docs/mcp.md index 6b3e9f1743..cc0461ae3c 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -16354,8 +16354,9 @@ one plane, such as the top flaps of a closed box, are not listed. Without a blan null and the lists are empty; `issues` are those of `addon.packaging.check`. Overlapping panels in one plane stack according to the fold tree from the largest panel. Choosing another base -preserves that order when both trees place the panels in the same positions. Panels in different planes draw by -depth, including when their separation is smaller than the depth tie tolerance. Nearly parallel panels that cross +preserves that order when both trees place the panels in the same positions. Every member of a coplanar group must +match every other member. The group uses one comparison depth, and its panels stack by rank at that depth. Panels in +different planes draw by depth, with the plane number resolving an exact crossing. Nearly parallel panels that cross draw by depth on each side of the intersection. A hinge uses the role of the line with the most total length along it; the lowest line id wins a tie. diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index d40b119d74..2f23ed13cd 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -32,7 +32,7 @@ ## Feature submission evidence -- Fold Preview orders overlapping panels by their fold tree, draws distinct planes and crossing panels by depth, and calculates poses relative to the blank's center. A hinge uses its longest line across all split pieces. Tests cover flat, near-flat, nested and offset folds in `crates/three-d/src/fold/tests.rs` and `crates/three-d/src/fold/raster/tests.rs`. +- Fold Preview orders overlapping coplanar panels by their fold tree, draws distinct planes and crossing panels by depth, and calculates poses relative to the blank's center. Each plane uses one comparison depth, and every member of a plane group must match every other member. A hinge uses its longest line across all split pieces. Tests cover flat, near-flat, nested and offset folds in `crates/three-d/src/fold/tests.rs` and `crates/three-d/src/fold/raster/tests.rs`. Variables panel row context menus (2026-11-20, G12): Right-click highlights the clicked variable without changing the canvas selection and offers Select Bound Object, Variable Options…, Unbind Variable, the four Make Dynamic actions, and Delete Variable. Shift+F10 opens the same menu for the focused row; Escape dismisses it. Row actions target that variable; dynamic actions use the canvas selection. Lock and selection checks disable unavailable actions. Headless pointer/accessibility tests cover opening the menu, highlighting, bound-object selection, deletion in one undo step and restoration, and disabled actions. Scores and estimates unchanged.