diff --git a/crates/render/src/clip.rs b/crates/render/src/clip.rs index bfc9f013a..e62889e76 100644 --- a/crates/render/src/clip.rs +++ b/crates/render/src/clip.rs @@ -82,16 +82,13 @@ impl Pieces { fn new(cap: Cap) -> Pieces { Pieces { out: Vec::new(), cur: Vec::new(), points: 0, cap, cut: false } } - /// End the current piece: kept when it has two points or more and the cap has room for it. (A - /// piece has fewer only once the cap refused a point, and then nothing more is added.) + /// End the current piece: kept when it has two points or more. (A piece is started only when + /// the cap has room for it, and has fewer points only once the cap refused one, and then + /// nothing more is added.) fn end(&mut self) { let cur = std::mem::take(&mut self.cur); if cur.len() >= 2 { - if self.out.len() < self.cap.pieces { - self.out.push(cur); - } else { - self.cut = true; - } + self.out.push(cur); } } /// Add `p` to the current piece, unless the cap has no room for it (the pieces are then cut). @@ -104,10 +101,15 @@ impl Pieces { } } /// Add the segment `p`→`q`: to the current piece when it starts where that one ends, else as - /// a new piece. + /// a new piece. With no piece left in the cap, the pieces are cut there: the new piece is not + /// started, so its points are not copied only to be dropped. fn segment(&mut self, p: Vec2, q: Vec2) { if !self.cur.last().is_some_and(|l| l.near(p, 1e-12)) { self.end(); + if self.out.len() >= self.cap.pieces { + self.cut = true; + return; + } self.point(p); } self.point(q); @@ -716,6 +718,26 @@ mod tests { assert_eq!(c.clip_triangles_outside_within(&away, 30), (away.clone(), true)); } + #[test] + fn a_piece_the_cap_has_no_room_for_is_not_built() { + // One piece fits the cap; the next one stops the pieces before any of its points are + // copied. + let mut pieces = Pieces::new(Cap { points: 1_000, pieces: 1 }); + pieces.segment(Vec2::new(1.0, 1.0), Vec2::new(10.0, 1.0)); + pieces.end(); + pieces.segment(Vec2::new(10.0, 2.0), Vec2::new(1.0, 2.0)); + assert!(pieces.cut && pieces.cur.is_empty() && pieces.points == 2, "{} points", pieces.points); + // Through the rectangle: out past its right edge, back in, and on inside for 1,000 points. + let back: Vec = [Vec2::new(1.0, 1.0), Vec2::new(11.0, 1.0), Vec2::new(11.0, 2.0), Vec2::new(1.0, 2.0)] + .into_iter() + .chain((0..1000).map(|i| Vec2::new(1.0 + f64::from(i % 2), 3.0 + f64::from(i) * 0.005))) + .collect(); + let first = vec![Vec2::new(1.0, 1.0), Vec2::new(10.0, 1.0)]; + assert_eq!(clip_polyline_within(&back, &r(), Cap { points: usize::MAX, pieces: 1 }), (vec![first.clone()], false)); + // A piece that fits exactly, with nothing visible after it, is whole. + assert_eq!(clip_polyline_within(&back[..3], &r(), Cap { points: 2, pieces: 1 }), (vec![first], true)); + } + #[test] fn triangles() { let t = clip_triangles(&[Vec2::new(-5.0, 0.0), Vec2::new(5.0, 0.0), Vec2::new(5.0, 10.0)], &r()); diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index 94392c8ff..c45f30f9b 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -1919,6 +1919,52 @@ fn runs_seen_through_a_viewport_are_counted_and_spend_the_layout_dash_budget() { assert_eq!(l.undashed, 2); } +/// Viewports after a spent layout build draw nothing, and the list says what they leave out: also +/// when the build was spent by a list filled exactly, before anything was left out. A list that +/// left out only dashes still draws them. +#[test] +fn viewports_after_a_spent_layout_build_say_what_they_leave_out() { + // A point seen through a rectangular viewport and then through a clipped one (which has no + // border), on a layout with a dashed line and the clip outline first: nothing on the layout + // after the viewports marks the list. + let paper = Space::Paper("Layout1".into()); + let layout = |second_on: bool| { + let mut d = dashed_lines(&[]); + let point = EntityKind::Point(cadcraft_doc::Point { p: Vec3::new(5.0, 5.0, 0.0), angle: 0.0 }); + d.add(&Space::Model, Common::default(), point).unwrap(); + let line = EntityKind::Line(cadcraft_doc::Line { a: Vec3::new(0.0, 12.0, 0.0), b: Vec3::new(750.0, 12.0, 0.0) }); + d.add(&paper, Common { linetype: "DASHED".into(), ..Default::default() }, line).unwrap(); + let vertices = [(20.0, 0.0), (30.0, 0.0), (30.0, 10.0), (20.0, 10.0)].map(|(x, y)| PolyVertex::new(Vec2::new(x, y))).to_vec(); + let square = cadcraft_doc::LwPolyline { vertices, closed: true, const_width: 0.0, elevation: 0.0, plinegen: false }; + let outline = d.add(&paper, Common::default(), EntityKind::LwPolyline(square)).unwrap(); + d.add(&paper, Common::default(), EntityKind::Viewport(vp(Vec2::new(5.0, 5.0), 10.0, 10.0, Vec2::new(5.0, 5.0), 10.0, 2))).unwrap(); + let mut clipped = vp(Vec2::new(25.0, 5.0), 10.0, 10.0, Vec2::new(5.0, 5.0), 10.0, 3); + clipped.clip = Some(outline); + clipped.on = second_on; + d.add(&paper, Common::default(), EntityKind::Viewport(clipped)).unwrap(); + d + }; + let build = |d: &Drawing, max_points, max_dashes| { + crate::pipeline::build_space_within(d, &paper, &Options::default(), false, max_points, MAX_DISPLAY_PRIMS, max_dashes) + }; + let shown = |l: &DisplayList| l.prims.iter().filter(|p| p.handle == VIEWPORT_CONTENT).count(); + let (both, first) = (layout(true), layout(false)); + let full = build(&both, MAX_DISPLAY_POINTS, MAX_DASH_ELEMENTS); + assert!(!full.truncated && shown(&full) == 2, "{}", shown(&full)); + // The layout up to the first viewport's point fills the list exactly: whole when the second + // viewport is off, and its point said to be left out when it is on. + let exact = points(&build(&first, MAX_DISPLAY_POINTS, MAX_DASH_ELEMENTS)); + let l = build(&first, exact, MAX_DASH_ELEMENTS); + assert!(!l.truncated && shown(&l) == 1, "{}", shown(&l)); + let l = build(&both, exact, MAX_DASH_ELEMENTS); + assert!(l.truncated && shown(&l) == 1, "{}", shown(&l)); + let l = build(&both, exact - 1, MAX_DASH_ELEMENTS); + assert!(l.truncated && shown(&l) == 0, "{}", shown(&l)); + // The dashed line past the dash budget: drawn continuous, and both viewports drawn. + let l = build(&both, MAX_DISPLAY_POINTS, 100); + assert!(l.truncated && l.undashed == 1 && shown(&l) == 2, "{} {}", l.undashed, shown(&l)); +} + /// A block reference cut by an XCLIP boundary is drawn apart and then joined: a run in it drawn /// continuous is still counted. #[test] diff --git a/crates/render/src/viewports.rs b/crates/render/src/viewports.rs index df7a91ff4..97762b53c 100644 --- a/crates/render/src/viewports.rs +++ b/crates/render/src/viewports.rs @@ -97,6 +97,12 @@ fn contents(b: &mut Builder, d: &Drawing, vp: &cadcraft_doc::Viewport, area: Bou if b.list.expanded >= MAX_BLOCK_EXPANSION || !vp.on { return; } + // A spent build draws nothing more: once its list says so, the model is not scanned (and its + // objects' bounds worked out) for nothing. A build spent by a list filled exactly has left + // nothing out yet: the scan still marks the list for the contents it can't draw. + if b.spent() && b.list.truncated { + return; + } let center = vp.center.xy(); let ok = |v: f64| v.is_finite() && v > 0.0; if !(ok(vp.width) && ok(vp.height) && ok(vp.view_height) && center.is_finite() && vp.view_center.is_finite()) {