Files
solidcraft/crates/kernel/src/mesh_memo_tests.rs
T
02c77df5f7 inspect.clearance: mesh each body once per check instead of once per pair (#2867)
* inspect.clearance: pairs whose bodies did not change are not measured again

Each clearance check measured every close pair again, though between two
checks usually only a few bodies change or move. Results are now kept per pair,
keyed by both bodies' geometry and placement and the check's settings; a pair
whose two bodies are unchanged is taken from that cache. With 40 overlapping
parts, a check after moving one of them took 0.8 s instead of 23 s (741 of 780
pairs reused). Pairs that could not be worked out are tried again each time.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* inspect.clearance: boxes and placed keys exactly as before the reuse

The pair boxes are taken over the mesh's triangles with finite corners, as a
distance shape's are, so pruning is unchanged. A placed body's identity now
includes its model body's own frame, so two placements are never mistaken for
one another.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* inspect.clearance: a large body is meshed once for all its pairs

Each pair that touches cuts the larger body down to the pair's region with a
kernel boolean, and that boolean meshed the whole body twice: once for its
checks and once inside the intersection. A sheet with 600 faces paid about
2.5 s per pair for meshes that were the same every time.

A body can now keep its meshes for reuse (Body::keeping_meshes); its booleans
take the meshes from there, and the intersection takes its shell meshes
from there too (truck-shapeops: and_with_history_meshed). Clearance uses such
copies for one check. Results are unchanged to the bit. On a 3 x 2 m sheet
with bends and 300 holes, the clip took 0.23 s per pair instead of 2.6 s, and
24 rivets took 5.6 s instead of 16.5 s.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
2026-10-19 16:14:24 -04:00

66 lines
3.1 KiB
Rust

use super::*;
use crate::{BoolOp, boolean, box_solid, cylinder, measure};
/// A plate with a row of holes and a rounded rim along one edge (so that it is no prism the 2D
/// route would take), as a large body many small regions are cut from.
fn plate() -> Body {
let rim = cylinder(Vec3::new(0.0, 0.0, 3.0), Vec3::X, 4.0, 300.0).unwrap();
let mut plate = box_solid(Vec3::ZERO, Vec3::new(300.0, 200.0, 3.0)).unwrap();
plate = boolean(&plate, &rim, BoolOp::Union).unwrap().unwrap();
for k in 0..12 {
let hole = cylinder(Vec3::new(20.0 + 22.0 * k as f64, 100.0, -1.0), Vec3::Z, 2.6, 5.0).unwrap();
plate = boolean(&plate, &hole, BoolOp::Cut).unwrap().unwrap();
}
plate
}
/// Regions cut from a body that keeps its meshes come out as from the body itself, to the bit,
/// and the body is meshed once for all of them.
#[test]
fn regions_cut_from_a_body_keeping_its_meshes_are_unchanged() {
let plate = plate();
let kept = plate.keeping_meshes();
for k in [0.0, 3.0, 7.0] {
let x = 20.0 + 22.0 * k;
let region = box_solid(Vec3::new(x - 8.0, 92.0, -2.0), Vec3::new(x + 8.0, 108.0, 5.0)).unwrap();
let want = boolean(&plate, &region, BoolOp::Intersect).unwrap().unwrap();
let got = boolean(&kept, &region, BoolOp::Intersect).unwrap().unwrap();
let (w, g) = (measure(&want).unwrap(), measure(&got).unwrap());
assert_eq!(got.face_count(), want.face_count(), "region at {x}");
assert_eq!(g.volume.to_bits(), w.volume.to_bits(), "region at {x}");
let cut = boolean(&kept, &region, BoolOp::Cut).unwrap().unwrap();
assert_eq!(
measure(&cut).unwrap().volume.to_bits(),
measure(&boolean(&plate, &region, BoolOp::Cut).unwrap().unwrap()).unwrap().volume.to_bits()
);
}
let memo = kept.meshes.as_ref().unwrap();
assert_eq!(memo.shells.lock().unwrap().len(), 1, "one tolerance, one meshing");
assert!(!memo.made.lock().unwrap().is_empty());
// A mesh from the memo is the mesh made afresh.
let (a, b) = (kept.tessellate_with(0.05, true).unwrap(), plate.tessellate_with(0.05, true).unwrap());
let (a2, _) = (kept.tessellate_with(0.05, true).unwrap(), ());
for m in [&a, &a2] {
assert_eq!(m.triangles, b.triangles);
assert!(
m.positions
.iter()
.zip(&b.positions)
.all(|(p, q)| p.x.to_bits() == q.x.to_bits() && p.y.to_bits() == q.y.to_bits() && p.z.to_bits() == q.z.to_bits())
);
}
}
/// A copy whose solid was replaced does not take meshes of the solid it replaced.
#[test]
fn a_copy_with_another_solid_makes_its_own_meshes() {
let kept = plate().keeping_meshes();
let other = box_solid(Vec3::ZERO, Vec3::new(10.0, 10.0, 10.0)).unwrap();
let mut swapped = kept.clone();
swapped.solid = other.solid.clone();
let _ = kept.tessellate_with(0.5, true).unwrap();
let m = swapped.tessellate_with(0.5, true).unwrap();
assert_eq!(m.triangles.len(), other.tessellate_with(0.5, true).unwrap().triangles.len());
assert!(swapped.boolean_meshes(0.01).is_none());
}