3D: no Inflate graphic handle where a nearer facet covers it

A graphic's handle on Inflate's front or back was placed through the nearest piece of its own face only, so a nearer piece of the other face, a side or a back cap could hide a handle that still showed and could be dragged. The handle and the offset read from the page are now left out where a facet drawn over that point of the page covers it: one clearly nearer there, or one drawn last (a cap turned toward the viewer), unless the piece is itself drawn last after it.
This commit is contained in:
dotCooCoo committed 2026-11-16 00:29:18 -08:00
1 parent f2376b05f7
commit 8f47eaa23f
2 files changed
+123 -17

No files matched your search

+71 -16
View File
@@ -1018,10 +1018,41 @@ 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, its depth (larger is nearer)
/// and its plane, for its depth at a point of the page.
/// and its plane, for its depth at a point of the page. The other facets the viewer sees (the
/// sides, and pieces that show the surface's inside) are kept as `covers`, for where they hide
/// a piece.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MembraneFrame {
pieces: Vec<MembranePiece>,
covers: Vec<Cover>,
}
/// A facet that can hide a piece of Inflate's front or back: its contours on the page (nonzero
/// winding), its depth, its plane and its place among the solid's facets.
#[derive(Clone, Debug, PartialEq)]
struct Cover {
contours: Vec<Vec<Point>>,
depth: f64,
plane: Option<DepthPlane>,
index: usize,
}
impl Cover {
/// Does the facet hold page point `p`?
fn holds(&self, p: Point) -> bool {
let mut winding = 0i32;
for c in &self.contours {
for (a, b) in c.iter().zip(c.iter().cycle().skip(1)).take(c.len()) {
let side = (*b - *a).cross(p - *a);
if a.y <= p.y && b.y > p.y && side > 0.0 {
winding += 1;
} else if a.y > p.y && b.y <= p.y && side < 0.0 {
winding -= 1;
}
}
}
winding != 0
}
}
#[derive(Clone, Debug, PartialEq)]
@@ -1031,6 +1062,9 @@ struct MembranePiece {
page: [Point; 3],
depth: f64,
plane: Option<DepthPlane>,
/// The piece's place among the solid's facets: of two facets drawn last, the later one is drawn
/// over the other.
index: usize,
}
impl MembranePiece {
@@ -1043,31 +1077,35 @@ impl MembranePiece {
impl MembraneFrame {
/// The pieces of `facets` that carry where they lie in the art's plane and show the outside of
/// Inflate's front or back.
/// Inflate's front or back, and the other facets not turned away from the viewer as covers.
pub(super) fn of<'a>(facets: impl IntoIterator<Item = &'a Facet>) -> Self {
let pieces = facets
.into_iter()
.filter_map(|f| {
let face = f.membrane?;
let cell = f.surface.as_ref().filter(|c| c.outward)?;
let (mut pieces, mut covers) = (Vec::new(), Vec::new());
for (index, f) in facets.into_iter().enumerate() {
let piece = f.membrane.zip(f.surface.as_ref().filter(|c| c.outward)).and_then(|(face, cell)| {
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, plane: f.plane })
})
.collect();
Self { pieces }
Some(MembranePiece { face, uv, page, depth: f.depth, plane: f.plane, index })
});
match piece {
Some(piece) => pieces.push(piece),
None if !f.away => covers.push(Cover { contours: f.contours.clone(), depth: f.depth, plane: f.plane, index }),
None => {}
}
}
Self { pieces, covers }
}
/// 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 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.
/// that point of the page. `None` when no piece the viewer sees holds it, or when a nearer piece
/// of the other face or a nearer cover hides that point of the page.
fn map(&self, face: PlanarFace, p: Point, from_page: bool) -> Option<Point> {
let inside = |t: &[Point; 3], q: Point| {
let side = |a: Point, b: Point| (b - a).cross(q - a);
let s = [side(t[0], t[1]), side(t[1], t[2]), side(t[2], t[0])];
s.iter().all(|v| *v >= -1e-9) || s.iter().all(|v| *v <= 1e-9)
};
let mut best: Option<(Point, f64)> = None;
let mut best: Option<(Point, f64, &MembranePiece)> = 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) {
@@ -1075,11 +1113,28 @@ impl MembraneFrame {
}
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));
if best.is_none_or(|(_, nearest, _)| depth > nearest) {
best = Some((q, depth, piece));
}
}
best.map(|(q, _)| q)
let (q, depth, shown) = best?;
let at = if from_page { p } else { q };
// A facet drawn last (depth `f64::MAX`) is drawn over every other one, and over another
// drawn last before it. Otherwise, where the front, the back and the sides meet, they give
// the same depth there, and only a facet clearly nearer covers.
let hides = |raw: f64, there: f64, index: usize| match (raw == f64::MAX, shown.depth == f64::MAX) {
(true, true) => index > shown.index,
(true, false) => true,
(false, true) => false,
(false, false) => there > depth + 1e-6 * depth.abs().max(1.0),
};
let covered = self
.pieces
.iter()
.filter(|piece| piece.face != face)
.any(|piece| inside(&piece.page, at) && hides(piece.depth, piece.depth_at(at), piece.index))
|| self.covers.iter().any(|c| c.holds(at) && hides(c.depth, c.plane.and_then(|plane| plane.depth_at(at)).unwrap_or(c.depth), c.index));
(!covered).then_some(q)
}
}
+52 -1
View File
@@ -1044,15 +1044,66 @@ fn overlapping_inflate_pieces_map_a_point_through_the_one_nearer_there() {
page,
depth,
plane: Some(DepthPlane { normal, offset, center: Point::ORIGIN, eye: None }),
index: 0,
};
// 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 frame = GraphicFrame::inflated(Rect::new(0.0, 0.0, 20.0, 20.0), MembraneFrame { pieces: vec![tilted, flat], covers: vec![] });
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}");
}
/// A graphic on Inflate's back has no handle where a nearer piece of the front covers its center
/// on the page, and the back has no offset there; where the back is nearer, it has both.
#[test]
fn an_inflate_back_graphic_covered_by_a_nearer_front_piece_has_no_handle() {
let page = [Point::new(0.0, 0.0), Point::new(20.0, 0.0), Point::new(10.0, 20.0)];
let piece = |face: PlanarFace, depth: f64| MembranePiece { face, uv: page, page, depth, plane: None, index: 0 };
let frame = |front: f64, back: f64| {
let pieces = vec![piece(PlanarFace::Front, front), piece(PlanarFace::Back, back)];
GraphicFrame::inflated(Rect::new(0.0, 0.0, 20.0, 20.0), MembraneFrame { pieces, covers: vec![] })
};
let g = MappedGraphic::from_value(&json!({"symbol": "Mark", "face": "back", "x": -10.0, "y": 5.0})).unwrap();
let covered = frame(10.0, 5.0);
assert!(covered.handle(&g).is_none());
assert!(covered.offset_at(PlanarFace::Back, Point::new(10.0, 5.0)).is_none());
assert!(covered.offset_at(PlanarFace::Front, Point::new(10.0, 5.0)).is_some());
let seen = frame(5.0, 10.0);
let (at, _) = seen.handle(&g).unwrap();
assert!((at.x - 10.0).abs() < 1e-6 && (at.y - 5.0).abs() < 1e-6, "{at:?}");
assert!(seen.offset_at(PlanarFace::Back, Point::new(10.0, 5.0)).is_some());
}
/// A nearer side facet that covers a graphic's center on Inflate's front hides its handle; one
/// behind the front, or beside the center, leaves it. A facet drawn last (a back cap) hides it
/// whatever its plane's depth, unless the front piece is drawn last too, after it.
#[test]
fn an_inflate_graphic_covered_by_a_nearer_side_has_no_handle() {
let page = [Point::new(0.0, 0.0), Point::new(20.0, 0.0), Point::new(10.0, 20.0)];
let front = |depth: f64, index: usize| MembranePiece { face: PlanarFace::Front, uv: page, page, depth, plane: None, index };
let side = |x0: f64, depth: f64, index: usize| Cover {
contours: vec![vec![Point::new(x0, 0.0), Point::new(x0 + 4.0, 0.0), Point::new(x0 + 4.0, 20.0), Point::new(x0, 20.0)]],
depth,
// A plane behind the front, as a back cap's is.
plane: Some(DepthPlane { normal: [0.0, 0.0, 1.0], offset: 1.0, center: Point::ORIGIN, eye: None }),
index,
};
let frame = |piece: MembranePiece, cover: Cover| {
GraphicFrame::inflated(Rect::new(0.0, 0.0, 20.0, 20.0), MembraneFrame { pieces: vec![piece], covers: vec![cover] })
};
let g = MappedGraphic::from_value(&json!({"symbol": "Mark", "face": "front", "x": 10.0, "y": 5.0})).unwrap();
let mut nearer = side(8.0, 10.0, 1);
nearer.plane = None;
assert!(frame(front(5.0, 0), nearer.clone()).handle(&g).is_none());
assert!(frame(front(5.0, 0), nearer).offset_at(PlanarFace::Front, Point::new(10.0, 5.0)).is_none());
assert!(frame(front(5.0, 0), side(8.0, 10.0, 1)).handle(&g).is_some());
assert!(frame(front(5.0, 0), side(14.0, 10.0, 1)).handle(&g).is_some());
assert!(frame(front(5.0, 0), side(8.0, f64::MAX, 1)).handle(&g).is_none());
assert!(frame(front(f64::MAX, 0), side(8.0, f64::MAX, 1)).handle(&g).is_none());
assert!(frame(front(f64::MAX, 2), side(8.0, f64::MAX, 1)).handle(&g).is_some());
}