Keep a zoomed-in profile's hover fill: count the slabs each edge crosses (#336)

fill_triangles bounded the slab triangulator by edges x slabs, as if every
edge crossed every slab. A ring sampled finely at a close zoom has hundreds
of edges and slabs but only four edges in each slab, so the estimate ran a
hundred times over the hover highlight's budget and the fill was dropped,
leaving only the outline. Count the slabs each edge actually spans instead.

Region2::triangulate tested every edge against every slab, so its work was
the overestimate. It now sweeps upward with the edges crossing the current
slab, giving the same triangles in time proportional to them.

Fixes #164

Co-authored-by: Vitali M. <6042958+vitalibr@users.noreply.github.com>
This commit is contained in:
Vitali M.andVitali M. authored and GitHub committed 2026-10-12 13:47:11 -04:00
1 parent e6a95615a0
commit b2fc6d1fac
3 files changed
+108 -8

No files matched your search

+10 -7
View File
@@ -114,23 +114,26 @@ pub fn region_triangles(region: &Region2, plane: &Plane, proj: &Proj, ppp: f32)
display_region(region, plane, proj, ppp).map_or_else(Vec::new, |display| fill_triangles(&display, &mut 65_536))
}
/// Bound the slab triangulator's allocation before calling it. Every slab can emit
/// at most one triangle per edge; complex fills are omitted rather than cut into holes.
/// Bound the slab triangulator's allocation before calling it. Every slab emits one
/// triangle per edge crossing it; complex fills are omitted rather than cut into holes.
pub fn fill_triangles(display: &Region2, budget: &mut usize) -> Vec<[Vec2; 3]> {
let mut heights = Vec::new();
let mut edges = 0usize;
let mut spans = Vec::new();
for loop_ in std::iter::once(&display.outer).chain(&display.holes) {
for segment in &loop_.segs {
let (a, b) = (segment.start(), segment.end());
if (a.y - b.y).abs() > 1e-12 {
edges += 1;
heights.extend([a.y, b.y]);
spans.push((a.y.min(b.y), a.y.max(b.y)));
}
}
}
let mut heights: Vec<f64> = spans.iter().flat_map(|&(lo, hi)| [lo, hi]).collect();
heights.sort_by(f64::total_cmp);
heights.dedup_by(|a, b| (*a - *b).abs() < 1e-12);
if edges.saturating_mul(heights.len().saturating_sub(1)) > *budget {
// An edge crosses the slabs between its end heights. Counting every edge in every slab
// overestimated a zoomed-in ring a hundredfold and dropped its hover fill (#164).
let slab = |y: f64| heights.partition_point(|h| *h < y - 1e-12);
let crossings = spans.iter().fold(0usize, |n, &(lo, hi)| n.saturating_add(slab(hi).saturating_sub(slab(lo))));
if crossings > *budget {
return Vec::new();
}
let triangles = display.triangulate(1.0);
@@ -289,3 +289,37 @@ fn pixel_frame_budget_does_not_cache_unbounded_empty_curve_results() {
assert!(h.app.viewport.display.lock().unwrap().len() <= 4096);
assert_eq!(h.app.session.doc.to_json(), baseline);
}
/// A zoomed-in ring is sampled finely, but each slab of the fill still crosses only its
/// four boundary edges: its fill must fit the hover highlight's budget (#164).
#[test]
fn zoomed_in_ring_fill_fits_the_hover_highlight_budget() {
let mut h = Harness::new();
h.app.ui.show_origin = false;
h.app.ui.perspective = true;
h.app.run("sketch.create", json!({"plane":"XY","name":"Ring"})).unwrap();
h.app.run("sketch.circle.center", json!({"center":[0,0],"radius":40})).unwrap();
h.app.run("sketch.circle.center", json!({"center":[0,0],"radius":45})).unwrap();
h.settle();
h.step(&json!({"call":"ui.view","params":{"view":"iso"}})).unwrap();
h.step(&json!({"call":"ui.view","params":{"view":"fit"}})).unwrap();
h.app.cam.zoom_at(0.15, Some(Vec3::new(42.5, 0.0, 0.0)));
h.settle();
let baseline = h.app.session.doc.to_json();
let sid = h.app.session.active_sketch.unwrap();
let state = h.app.session.world_state();
let ss = state.sketch(sid).unwrap();
let proj = projection(&h.app, h.app.viewport.rect.unwrap());
let ppp = h.app.viewport.pixels_per_point();
let ring = ss.profiles.iter().find(|p| p.region.holes.len() == 1).unwrap();
let display = crate::sketch_display::display_region(&ring.region, &ss.plane, &proj, ppp).unwrap();
let edges = std::iter::once(&display.outer).chain(&display.holes).map(|l| l.segs.len()).sum::<usize>();
assert!(edges * edges / 2 > 65_536, "fixture must sample the ring finely enough to exceed the old estimate: {edges} edges");
let triangles = crate::sketch_display::region_triangles(&ring.region, &ss.plane, &proj, ppp);
assert!(!triangles.is_empty(), "the ring's fill was dropped at this zoom");
for t in &triangles {
let r = ((t[0] + t[1] + t[2]) / 3.0).len();
assert!((39.9..=45.1).contains(&r), "fill triangle outside the ring: r = {r}");
}
assert_eq!(h.app.session.doc.to_json(), baseline);
}