diff --git a/crates/effects/src/solids/graphics.rs b/crates/effects/src/solids/graphics.rs index 1d7e4e8d45..03ae75a927 100644 --- a/crates/effects/src/solids/graphics.rs +++ b/crates/effects/src/solids/graphics.rs @@ -28,7 +28,7 @@ use vectorcraft_doc::color::{BlendMode, Paint}; use vectorcraft_doc::{Appearance, AppearanceItem, Document, FillLayer, Node, NodeId, NodeKind, Symbol}; use vectorcraft_geom::kurbo::{CubicBez, ParamCurve, PathEl, Shape}; use vectorcraft_geom::{Affine, BezPath, FillRule, PathData, Point, Rect, SubPath, Vec2}; -use vectorcraft_three_d::{FaceMap, Facet, PlanarFace}; +use vectorcraft_three_d::{DepthPlane, FaceMap, Facet, PlanarFace}; /// The effect parameter that holds the graphics. pub const GRAPHICS: &str = "graphics"; @@ -1017,8 +1017,8 @@ pub struct GraphicFrame { /// The pieces of Inflate's front and puffed back that show their outside to the viewer, as the /// graphics are drawn on them ([`surface_parts`]): each piece's face, its corners in the art's plane -/// (offsets from the center of the object's bounds) and on the page, and its depth (larger is -/// nearer). +/// (offsets from the center of the object's bounds) and on the page, its depth (larger is nearer) +/// and its plane, for its depth at a point of the page. #[derive(Clone, Debug, Default, PartialEq)] pub struct MembraneFrame { pieces: Vec, @@ -1030,6 +1030,15 @@ struct MembranePiece { uv: [Point; 3], page: [Point; 3], depth: f64, + plane: Option, +} + +impl MembranePiece { + /// The piece's depth at page point `p`: its plane's depth there, or its own depth when it has + /// no plane or the plane gives none. + fn depth_at(&self, p: Point) -> f64 { + self.plane.and_then(|plane| plane.depth_at(p)).unwrap_or(self.depth) + } } impl MembraneFrame { @@ -1043,15 +1052,15 @@ impl MembraneFrame { let cell = f.surface.as_ref().filter(|c| c.outward)?; let uv = <[Point; 3]>::try_from(cell.uv.as_slice()).ok()?; let page = <[Point; 3]>::try_from(cell.page.as_slice()).ok()?; - Some(MembranePiece { face, uv, page, depth: f.depth }) + Some(MembranePiece { face, uv, page, depth: f.depth, plane: f.plane }) }) .collect(); Self { pieces } } /// Point `p` of `face` in the art's plane mapped onto the page, or with `from_page` a page point - /// mapped into the art's plane, through the nearest piece of that face that holds it. `None` - /// when no piece the viewer sees holds it. + /// mapped into the art's plane, through the piece of that face that holds it and is nearest at + /// that point of the page. `None` when no piece the viewer sees holds it. fn map(&self, face: PlanarFace, p: Point, from_page: bool) -> Option { let inside = |t: &[Point; 3], q: Point| { let side = |a: Point, b: Point| (b - a).cross(q - a); @@ -1061,11 +1070,13 @@ impl MembraneFrame { let mut best: Option<(Point, f64)> = None; for piece in self.pieces.iter().filter(|piece| piece.face == face) { let (from, to) = if from_page { (&piece.page, &piece.uv) } else { (&piece.uv, &piece.page) }; - if !inside(from, p) || best.is_some_and(|(_, depth)| depth >= piece.depth) { + if !inside(from, p) { continue; } - if let Some(q) = triangle_map(from, to).map(|m| m * p).filter(|q| q.is_finite()) { - best = Some((q, piece.depth)); + let Some(q) = triangle_map(from, to).map(|m| m * p).filter(|q| q.is_finite()) else { continue }; + let depth = piece.depth_at(if from_page { p } else { q }); + if best.is_none_or(|(_, nearest)| depth > nearest) { + best = Some((q, depth)); } } best.map(|(q, _)| q) diff --git a/crates/effects/src/solids/tests_graphics.rs b/crates/effects/src/solids/tests_graphics.rs index 45fbb1c489..2fe175a4aa 100644 --- a/crates/effects/src/solids/tests_graphics.rs +++ b/crates/effects/src/solids/tests_graphics.rs @@ -1032,3 +1032,27 @@ fn an_inflate_frame_puts_the_handle_on_the_puff_and_reads_it_back() { let (x, y) = frame.offset_at(PlanarFace::Back, page).unwrap(); assert!((x - 15.0).abs() < 1e-6 && (y - 5.0).abs() < 1e-6, "{x} {y}"); } + +/// Where two pieces of an Inflate face overlap on the page, a point maps through the piece nearer +/// at that point, though the other is nearer at its center. +#[test] +fn overlapping_inflate_pieces_map_a_point_through_the_one_nearer_there() { + let page = [Point::new(0.0, 0.0), Point::new(20.0, 0.0), Point::new(10.0, 20.0)]; + let piece = |shift: f64, depth: f64, normal: [f64; 3], offset: f64| MembranePiece { + face: PlanarFace::Front, + uv: page.map(|p| p + Vec2::new(shift, 0.0)), + page, + depth, + plane: Some(DepthPlane { normal, offset, center: Point::ORIGIN, eye: None }), + }; + // The tilted piece is nearer at its center (depth 10) but at depth 0 where x is 10; the flat + // one lies at depth 5 everywhere. + let tilted = piece(0.0, 10.0, [1.0, 0.0, 1.0], 10.0); + let flat = piece(100.0, 5.0, [0.0, 0.0, 1.0], 5.0); + let frame = GraphicFrame::inflated(Rect::new(0.0, 0.0, 20.0, 20.0), MembraneFrame { pieces: vec![tilted, flat] }); + let (x, y) = frame.offset_at(PlanarFace::Front, Point::new(10.0, 5.0)).unwrap(); + assert!((x - 110.0).abs() < 1e-6 && (y - 5.0).abs() < 1e-6, "{x} {y}"); + // Near the tilted piece's left corner it is the nearer one. + let (x, _) = frame.offset_at(PlanarFace::Front, Point::new(2.0, 0.5)).unwrap(); + assert!((x - 2.0).abs() < 1e-6, "{x}"); +}