Wave 396/509: Kernel: a mitred sweep carries a slanted section all the way to each mitre (and 3 more)
4 commits, squashed; the originals stay reachable from tag archive/merge-main-develop. - b9d0a3308 jyviko: Mesh memo test: SMLib's booleans mesh neither operand - b6c3bf3c5 jyviko: Face capture test: an Offset Face's top is anchored by its exact history name - 1ee5f1af3 jyviko: Kernel: a mitred sweep carries a slanted section all the way to each mitre - 1e4b64402 jyviko: Provenance: a sweep's origins are the ones it recorded Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ
This commit is contained in:
@@ -137,20 +137,23 @@ fn generated_profiles_and_press_pull_convert_rotated_occurrence_picks_once() {
|
||||
}
|
||||
|
||||
/// A 40 x 30 x 20 box whose top is split at x = 20 and its left half raised 5 by Offset Face:
|
||||
/// the raised top has no construction certificate and no exact history name yet (#494).
|
||||
/// the raised top has no construction certificate, and an exact history name from SMLib's
|
||||
/// offset history.
|
||||
fn raised_half(s: &mut Session, body: &str) {
|
||||
run(s, "solid.box", json!({"length":40,"width":30,"height":20,"body_name":body}));
|
||||
run(s, "solid.split_face", json!({"faces":[[10,15,20]],"plane":{"origin":[20,0,0],"normal":[1,0,0]}}));
|
||||
run(s, "solid.offset_face", json!({"faces":[[10,15,20]],"distance":5}));
|
||||
}
|
||||
|
||||
/// Internal capture of a face in another component whose occurrence has an uncaptured move:
|
||||
/// the raised top has no construction certificate, but its exact history name keeps it, so the
|
||||
/// capture is an exact (lineage) anchor on that occurrence's path, whose pending position is
|
||||
/// captured when the attachment is made (`capture_positions`). Capturing changes nothing.
|
||||
#[test]
|
||||
fn legacy_internal_capture_refuses_an_uncaptured_source_in_another_component() {
|
||||
fn internal_capture_in_another_component_anchors_an_offset_face_by_its_history_name() {
|
||||
let mut s = Session::default();
|
||||
let component = run(&mut s, "component.create", json!({"name":"Source","grounded":false}));
|
||||
let occurrence = component["occurrence"].as_u64().unwrap();
|
||||
// A face Offset Face raised has no construction certificate or exact history name yet:
|
||||
// the legacy path.
|
||||
raised_half(&mut s, "Stock");
|
||||
run(&mut s, "component.activate", json!({"component":0}));
|
||||
run(&mut s, "occurrence.move", json!({"occurrence":occurrence,"translate":[30,0,0],"capture":false}));
|
||||
@@ -165,15 +168,20 @@ fn legacy_internal_capture_refuses_an_uncaptured_source_in_another_component() {
|
||||
.into_iter()
|
||||
.find(|face| face.plane_normal.is_some_and(|normal| normal.z > 0.999) && (face.centroid.z - 25.0).abs() < 1e-6)
|
||||
.unwrap();
|
||||
assert!(state.body("Stock").unwrap().body.face_plane(end.index).is_none(), "the raised top has no construction certificate");
|
||||
let before = s.doc.to_json();
|
||||
let error = super::super::sketch::face::capture_internal(
|
||||
let pending = s.pending_moves.clone();
|
||||
let plane = super::super::sketch::face::capture_internal(
|
||||
&s,
|
||||
&json!({"face":[40,15,25],"body":"Stock","index":end.index,"path":[occurrence]}),
|
||||
"internal",
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(error.to_string().contains("capture the source occurrence position"), "{error}");
|
||||
.unwrap();
|
||||
let anchor = super::super::sketch::face::anchor(&plane).expect("an exact attachment");
|
||||
assert_eq!(anchor.target.certificate, Some(solidcraft_doc::config::FaceCertificate::Lineage));
|
||||
assert_eq!(anchor.path, vec![occurrence]);
|
||||
assert_eq!(s.doc.to_json(), before);
|
||||
assert_eq!(s.pending_moves, pending);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -14,8 +14,9 @@ fn plate() -> Body {
|
||||
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.
|
||||
/// Regions cut from a body that keeps its meshes come out as from the body itself, to the bit.
|
||||
/// SMLib's booleans read the exact record and mesh neither operand, so they leave the memo
|
||||
/// empty; the body's own meshing then fills it once per tolerance and reuses it.
|
||||
#[test]
|
||||
fn regions_cut_from_a_body_keeping_its_meshes_are_unchanged() {
|
||||
let plate = plate();
|
||||
@@ -35,10 +36,11 @@ fn regions_cut_from_a_body_keeping_its_meshes_are_unchanged() {
|
||||
);
|
||||
}
|
||||
let memo = kept.meshes.as_ref().unwrap();
|
||||
assert_eq!(memo.made.lock().unwrap().len(), 1, "one tolerance, one meshing");
|
||||
assert_eq!(memo.made.lock().unwrap().len(), 0, "a boolean meshes neither operand");
|
||||
// A mesh from the memo is the mesh made afresh.
|
||||
let (a, b) = (kept.mesh(&crate::MeshTolerance::chord(0.05)).unwrap(), plate.mesh(&crate::MeshTolerance::chord(0.05)).unwrap());
|
||||
let (a2, _) = (kept.mesh(&crate::MeshTolerance::chord(0.05)).unwrap(), ());
|
||||
assert_eq!(memo.made.lock().unwrap().len(), 1, "one tolerance, one meshing");
|
||||
for m in [&a, &a2] {
|
||||
assert_eq!(m.triangles, b.triangles);
|
||||
assert!(
|
||||
|
||||
@@ -309,7 +309,10 @@ pub fn offset_origins(result: &Body, input: &Body, thickness: f64, prov: &mut Hi
|
||||
|
||||
/// Origins of any swept tool's faces (sweep, loft, pipe; extrude and revolve too) from where
|
||||
/// they start: the face lying in the profile's plane is the start cap, a face with an edge in
|
||||
/// that plane sweeps the profile segment under the edge, and the rest are end caps.
|
||||
/// that plane sweeps the profile segment under the edge, and the rest are end caps. That holds
|
||||
/// for a tool whose every wall runs the whole path; one swept piece by piece along its path
|
||||
/// (SMLib's sweep along lines and arcs, mitred at each corner) has walls starting further on,
|
||||
/// which only the tool's own history names ([`sweep_origins`]).
|
||||
pub fn swept_origins(tool: &Body, plane: &Plane, regions: &[Region2]) -> Result<Vec<FaceOrigin>> {
|
||||
let size = tool.size().max(1e-9);
|
||||
let m = tool.mesh(&crate::MeshTolerance::chord(size * 2e-4))?;
|
||||
@@ -339,32 +342,20 @@ pub fn swept_origins(tool: &Body, plane: &Plane, regions: &[Region2]) -> Result<
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Origins of a [`crate::sweep_path`] tool's faces. Its walls come in profile order (once for
|
||||
/// smooth walls, once per station for ruled ones) followed by the start and end caps; checked
|
||||
/// against the start cap lying in the profile's plane, else found as [`swept_origins`] does.
|
||||
/// Origins of a [`crate::sweep_path`] tool's faces: the ones the sweep recorded as it built them
|
||||
/// ([`Body::history`]: the start and end caps, each wall from the profile segment it sweeps,
|
||||
/// along every piece of the path), the first of each face's; a face the tool names no origin
|
||||
/// for (or a tool without a history) found as [`swept_origins`] does.
|
||||
pub fn sweep_origins(tool: &Body, plane: &Plane, regions: &[Region2]) -> Result<Vec<FaceOrigin>> {
|
||||
let n = tool.face_count();
|
||||
let k = segments(regions).len();
|
||||
let size = tool.size().max(1e-9);
|
||||
if k > 0 && n >= 2 && (n - 2).is_multiple_of(k) {
|
||||
let m = tool.mesh(&crate::MeshTolerance::chord(size * 2e-4))?;
|
||||
let faces = face_tris(&m);
|
||||
let start_flat = faces.get(n - 2).is_some_and(|f| f.tris.iter().all(|(t, _)| t.iter().all(|p| plane.height(*p).abs() <= size * 1e-6)));
|
||||
if start_flat {
|
||||
return Ok((0..n)
|
||||
.map(|i| {
|
||||
if i + 2 == n {
|
||||
FaceOrigin::Cap { end: false }
|
||||
} else if i + 1 == n {
|
||||
FaceOrigin::Cap { end: true }
|
||||
} else {
|
||||
FaceOrigin::Profile { segment: i % k }
|
||||
}
|
||||
})
|
||||
.collect());
|
||||
}
|
||||
let recorded: Vec<Option<FaceOrigin>> = match tool.history() {
|
||||
Some(h) if h.faces.len() == tool.face_count() => h.faces.iter().map(|f| f.origins.first().copied()).collect(),
|
||||
_ => vec![None; tool.face_count()],
|
||||
};
|
||||
if recorded.iter().all(Option::is_some) {
|
||||
return Ok(recorded.into_iter().flatten().collect());
|
||||
}
|
||||
swept_origins(tool, plane, regions)
|
||||
let found = swept_origins(tool, plane, regions)?;
|
||||
Ok(recorded.into_iter().zip(found).map(|(r, f)| r.unwrap_or(f)).collect())
|
||||
}
|
||||
|
||||
/// Replace origins on operand `operand` (a tool made by the operation) with that tool's own
|
||||
|
||||
@@ -765,6 +765,7 @@ mod imp {
|
||||
s.and_then(crate::PathSeg::tangents).ok_or_else(|| KernelError::Invalid("a path segment has no direction".into()))
|
||||
};
|
||||
let (first_t, _) = tangent(path.first())?;
|
||||
let offsets = section_offsets(plane, pieces, start, first_t, 1.0);
|
||||
let (_, last_t) = tangent(path.last())?;
|
||||
let end = path.last().map(seg_ends).transpose()?.map(|(_, b)| b).ok_or_else(|| KernelError::Invalid("empty path".into()))?;
|
||||
// A closed path ends where it starts, with no caps; at a corner there the last run and the
|
||||
@@ -803,12 +804,10 @@ mod imp {
|
||||
inside.extend(seeds(&pose, pieces, |q| apply(&middle, q)));
|
||||
(piece, placed(&pose, &last))
|
||||
} else {
|
||||
// On past each mitre: the profile's reach times the tangent of half the turn takes
|
||||
// every point of it to the mitre's plane; a little further, so the cut back runs
|
||||
// through the walls, never along the run's own end.
|
||||
let over = |turn: f64| reach * (turn * 0.5).tan() * 1.1 + reach * 1e-3 + 1e-6;
|
||||
let pre = entry.map_or(0.0, |(_, _, turn)| over(turn));
|
||||
let post = exit.map(|(_, turn)| over(turn)).unwrap_or(0.0);
|
||||
// On past each mitre, far enough to take every point of the section to the
|
||||
// mitre's plane ([`mitre_lead`]).
|
||||
let pre = entry.map_or(0.0, |(_, _, turn)| mitre_lead(&offsets, reach, turn, false));
|
||||
let post = exit.map(|(_, turn)| mitre_lead(&offsets, reach, turn, true)).unwrap_or(0.0);
|
||||
let mut route = Vec::with_capacity(segs.len() + 2);
|
||||
if pre > 0.0 {
|
||||
route.push(crate::PathSeg::Line { a: a0 - t0 * pre, b: a0 });
|
||||
@@ -819,7 +818,18 @@ mod imp {
|
||||
}
|
||||
let from = moved_plane(&pose, t0 * -pre);
|
||||
let (mut piece, [_, middle, last]) = swept(&from, pieces, &route, false)?;
|
||||
inside.extend(seeds(&from, pieces, |q| apply(&middle, q)));
|
||||
// Each wall is known by its piece's middle half way along the stretch of the run
|
||||
// that point keeps between the mitres: a point standing `s` ahead of the path
|
||||
// keeps the stretch `s` back from the run's (a profile at a slant to a run shorter
|
||||
// than its slant reaches past the mitres from the run's middle section).
|
||||
let run_length: f64 = segs.iter().map(seg_len).sum();
|
||||
let origin = a0 - t0 * pre;
|
||||
inside.extend(pieces.iter().enumerate().map(|(piece, x)| {
|
||||
let q = from.to_world(x.seg.point_at(0.5));
|
||||
let ahead = (q - origin).dot(t0);
|
||||
let at = carried_along(&route, pre + 0.5 * run_length - ahead).map_or_else(|| apply(&middle, q), |m| apply(&m, q));
|
||||
Seed { at, piece }
|
||||
}));
|
||||
if let Some((at, toward, _)) = entry {
|
||||
piece = combined(&piece, &half_space(at, -toward, size)?, solidcraft_smlib::BooleanOp::Subtract, "sweep mitre")?;
|
||||
}
|
||||
@@ -847,6 +857,31 @@ mod imp {
|
||||
with_history(body, &roles, pieces)
|
||||
}
|
||||
|
||||
/// The rigid motion carrying a section from the start of `route` to `along` (clamped to the
|
||||
/// route) along it, as SMLib's sweep turning with a path of lines and arcs carries it: moved
|
||||
/// along each line, turned about each arc's axis. None for a route with another curve before
|
||||
/// that point, whose frame only the sweep knows.
|
||||
fn carried_along(route: &[crate::PathSeg], along: f64) -> Option<Affine> {
|
||||
let mut m = IDENTITY;
|
||||
let mut left = along.max(0.0);
|
||||
for s in route {
|
||||
if left <= 0.0 {
|
||||
break;
|
||||
}
|
||||
let length = seg_len(s);
|
||||
let d = left.min(length);
|
||||
m = match *s {
|
||||
crate::PathSeg::Line { a, b } => affine_moved(&m, (b - a).normalized()? * d),
|
||||
crate::PathSeg::Arc { center, axis, angle, .. } if length > 0.0 => {
|
||||
affine_after(&affine_turned(center, axis.normalized()?, angle * d / length), &m)
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
left -= d;
|
||||
}
|
||||
Some(m)
|
||||
}
|
||||
|
||||
/// The length of a path segment.
|
||||
fn seg_len(s: &crate::PathSeg) -> f64 {
|
||||
match *s {
|
||||
@@ -1074,6 +1109,7 @@ mod imp {
|
||||
.flat_map(|x| (0..=8).map(move |i| x.seg.point_at(f64::from(i) / 8.0)))
|
||||
.map(|q| plane.to_world(q).dist(start) * ctl.scale.max(1.0))
|
||||
.fold(0.0f64, f64::max);
|
||||
let offsets = section_offsets(plane, &pieces, start, first_t, ctl.scale.max(1.0));
|
||||
let size = 4.0 * (total + reach) + 1.0;
|
||||
let law = |u: f64| 1.0 + (ctl.scale - 1.0) * u;
|
||||
let n0 = plane.normal().normalized().ok_or_else(|| KernelError::Invalid("profile plane".into()))?;
|
||||
@@ -1129,8 +1165,18 @@ mod imp {
|
||||
})
|
||||
.collect();
|
||||
let (Some(&first_map), Some(&end_map)) = (maps.first(), maps.last()) else { return Err(KernelError::Invalid("sweep path".into())) };
|
||||
let mid = maps.get(maps.len() / 2).copied().unwrap_or(end_map);
|
||||
inside.extend(seeds(plane, &pieces, |q| affine_apply(&mid, q)));
|
||||
// Each wall is known by its piece's middle at the station half way along the stretch
|
||||
// of the run that point keeps between the mitres (the stations are evenly spaced by
|
||||
// length): a point standing `s` ahead of the path keeps the stretch `s` back from the
|
||||
// run's, as in the plain sweep ([`cornered`]).
|
||||
let last_station = maps.len().saturating_sub(1);
|
||||
inside.extend(pieces.iter().enumerate().map(|(piece, x)| {
|
||||
let q = plane.to_world(x.seg.point_at(0.5));
|
||||
let ahead = (affine_apply(&first_map, q) - a0).dot(t0);
|
||||
let share = if translate || !(length > 0.0) { 0.5 } else { (0.5 - ahead / length).clamp(0.0, 1.0) };
|
||||
let station = ((share * last_station as f64).round() as usize).min(last_station);
|
||||
Seed { at: affine_apply(maps.get(station).unwrap_or(&end_map), q), piece }
|
||||
}));
|
||||
let mut piece = Brep::sweep_stations(&c, &maps, Ends::Capped).map_err(|e| failed("sweep", e))?;
|
||||
let next = match runs.get(ri + 1).and_then(|r| path.get(r.start)) {
|
||||
Some(s) => Some(tangent(Some(s))?.0),
|
||||
@@ -1138,18 +1184,19 @@ mod imp {
|
||||
};
|
||||
let exit = next.map(|t2| mitre_of(t1, t2)).transpose()?;
|
||||
if !translate {
|
||||
// On past each mitre, the run's section there held still.
|
||||
let over = |turn: f64| reach * (turn * 0.5).tan() * 1.1 + reach * 1e-3 + 1e-6;
|
||||
// On past each mitre, the run's section there held still, far enough to take
|
||||
// every point of it to the mitre's plane ([`mitre_lead`]).
|
||||
let prism = |map: Affine, toward: Vec3, by: f64| -> Result<Brep> {
|
||||
Brep::sweep_stations(&c, &[map, affine_moved(&map, toward * by)], Ends::Capped).map_err(|e| failed("sweep corner", e))
|
||||
};
|
||||
if let Some((at, m, turn)) = entry {
|
||||
let lead = prism(affine_moved(&first_map, t0 * -over(turn)), t0, over(turn))?;
|
||||
let pre = mitre_lead(&offsets, reach, turn, false);
|
||||
let lead = prism(affine_moved(&first_map, t0 * -pre), t0, pre)?;
|
||||
piece = combined(&piece, &lead, solidcraft_smlib::BooleanOp::Union, "sweep corner")?;
|
||||
piece = combined(&piece, &half_space(at, -m, size)?, solidcraft_smlib::BooleanOp::Subtract, "sweep mitre")?;
|
||||
}
|
||||
if let Some((m, turn)) = exit {
|
||||
let tail = prism(end_map, t1, over(turn))?;
|
||||
let tail = prism(end_map, t1, mitre_lead(&offsets, reach, turn, true))?;
|
||||
piece = combined(&piece, &tail, solidcraft_smlib::BooleanOp::Union, "sweep corner")?;
|
||||
piece = combined(&piece, &half_space(b1, m, size)?, solidcraft_smlib::BooleanOp::Subtract, "sweep mitre")?;
|
||||
}
|
||||
@@ -1275,6 +1322,39 @@ mod imp {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Where the profile's points sit from the path, as a section carries them along it: for
|
||||
/// each sampled point of `pieces` on `plane`, its distance ahead of the path's `start` along
|
||||
/// the unit direction of travel `t` there, and its distance square to that direction, both
|
||||
/// times `scale`. A section that turns with the path (along a smooth run, and through a
|
||||
/// corner about the line square to both directions) keeps both, a twist keeps both, and an
|
||||
/// end scale grows them by at most its largest scale.
|
||||
fn section_offsets(plane: &Plane, pieces: &[Piece], start: Vec3, t: Vec3, scale: f64) -> Vec<(f64, f64)> {
|
||||
pieces
|
||||
.iter()
|
||||
.flat_map(|x| (0..=8).map(move |i| x.seg.point_at(f64::from(i) / 8.0)))
|
||||
.map(|q| {
|
||||
let d = plane.to_world(q) - start;
|
||||
let s = d.dot(t);
|
||||
(s * scale, (d - t * s).len() * scale)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// How far a run's section is carried straight on past a corner turning by `turn` (`ahead`),
|
||||
/// or back before one (else), so that each of its points reaches the plane halving the
|
||||
/// corner. A point `s` ahead of the path and `w` from it, the plane's unit normal `m` at half
|
||||
/// the turn from the direction of travel `t`: `m · (s t + w) >= s cos(turn / 2) - w sin(turn /
|
||||
/// 2)`, so carried on `e` along `t` it reaches the plane once `e >= w tan(turn / 2) - s` (and
|
||||
/// back before the corner once `e >= w tan(turn / 2) + s`). A profile square to the path has
|
||||
/// `s = 0`: its reach times the tangent of half the turn; one standing at a slant to the path
|
||||
/// reaches across the mitre by its slant as well. A little further than the farthest point,
|
||||
/// so the cut back runs through the walls, never along the run's own end.
|
||||
fn mitre_lead(offsets: &[(f64, f64)], reach: f64, turn: f64, ahead: bool) -> f64 {
|
||||
let k = (turn * 0.5).tan();
|
||||
let need = offsets.iter().map(|(s, w)| w * k + if ahead { -s } else { *s }).fold(0.0f64, f64::max);
|
||||
need * 1.1 + reach * 1e-3 + 1e-6
|
||||
}
|
||||
|
||||
/// The plane halving the corner from direction `t1` to `t2` (its unit normal) and the turn
|
||||
/// there; a path that turns back on itself has none.
|
||||
fn mitre_of(t1: Vec3, t2: Vec3) -> Result<(Vec3, f64)> {
|
||||
|
||||
@@ -484,6 +484,30 @@ mod tests {
|
||||
assert!(sweep_path(&plane, ®ion, &[Vec3::ZERO, Vec3::new(f64::NAN, 0.0, 0.0)]).is_err());
|
||||
}
|
||||
|
||||
/// A profile at a slant to its path (drawn flat, the path rising steeply from it) swept
|
||||
/// along a polyline whose lines are shorter than the slant: each section reaches past both
|
||||
/// mitres of its line, so each line's walls are carried on far enough past the corner for
|
||||
/// all of it to meet the mitre. The square's centre is on the path, so the volume is the
|
||||
/// section square to the path times the path's length, `16 cos(slant) L`; a line carried
|
||||
/// short of a mitre leaves a notch with a face of its section in it. Every face is named.
|
||||
#[test]
|
||||
fn a_slanted_profile_meets_itself_at_every_mitre() {
|
||||
use crate::history::FaceOrigin;
|
||||
let path: Vec<Vec3> = (0..=20).map(|i| Vec3::new((f64::from(i) * 0.2).sin() * 10.0, 0.0, f64::from(i))).collect();
|
||||
let b = sweep_path(&Plane::XY, &square(2.0), &path).unwrap();
|
||||
let len: f64 = path.windows(2).map(|w| w[0].dist(w[1])).sum();
|
||||
let slant = (path[1] - path[0]).normalized().unwrap().z;
|
||||
let want = 16.0 * slant * len;
|
||||
let v = b.mesh(&crate::MeshTolerance::chord(0.01)).unwrap().measure().volume;
|
||||
assert!((v - want).abs() < 1e-6 * want, "{v} vs {want}");
|
||||
let h = b.history().unwrap();
|
||||
let caps = |end: bool| h.faces.iter().filter(|f| f.origins == [FaceOrigin::Cap { end }]).count();
|
||||
assert_eq!((caps(false), caps(true)), (1, 1));
|
||||
for (i, f) in h.faces.iter().enumerate() {
|
||||
assert!(matches!(f.origins[..], [FaceOrigin::Profile { .. }] | [FaceOrigin::Cap { .. }]), "face {i}: {f:?}");
|
||||
}
|
||||
}
|
||||
|
||||
fn square(h: f64) -> Region2 {
|
||||
Region2 { outer: Loop2::polygon(&[Vec2::new(-h, -h), Vec2::new(h, -h), Vec2::new(h, h), Vec2::new(-h, h)]), holes: Vec::new() }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user