Files
makeCAD/crates/engine/src/cmd/offset_topology_tests.rs
T
jyviko dc38349cbf Layer split stage 1: the B-rep record, native body file and Vec/Plane come from solidcore-record (#11496)
* Kernel: the B-rep record and the native body file come from solidcore-record

SolidCraft's portable B-rep record (`solidcraft_geom::brep`: brep.rs, topology, history,
envelope, part, curve2) and the native body file (`solidcraft_kernel::native`: the tagged binary
codec and the container) move to solidcore's `solidcore-record` crate: geometry as data, with
structural validation and no maths (solidcore's docs/layers.md, stage 1). Every caller uses
`solidcore_record::brep` and `solidcore_record::native`; the old copies are deleted.

What computes from coordinates stays with the kernel, in geom's `brep`:
- `brep::check`: the record's geometric checks (orthonormal frames, non-degenerate planes,
  unit revolution axes, non-singular placements). `check::validate` and
  `check::validate_built` are the record's validation and these together, and every caller of
  `Brep::validate` uses them, so what is accepted is unchanged;
- `brep::label`: a surface's natural ranges (a cone's apex), `curve_interval`,
  `edge_curve_interval` and `form_turns`;
- `brep::pcurve::eval`: a pcurve's point;
- `geometry::check`, `placement::moved` and `split_edges` become functions on the record.

The kernel's native module keeps the conversion between a body and its saved record
(metadata.rs) and checks the decoded record's geometry. `native/analytic.rs`, which no module
declared, is deleted.

solidcore-record is pinned to its rev on solidcore's `layer-record` branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NvoxKMbMFSQU8tzRtoJhtT

* Geometry: Vec2, Vec3, Plane and the profile types come from solidcore-record

The plain data of vectors and points, sketch planes and planar profiles (`Seg2`, `Loop2`,
`Region2`) moves to `solidcore-record`, with its constants and component-wise operators only
(owner decision, solidcore layer split). Their geometry stays in geom as traits:
`Vec2Geom`, `Vec3Geom`, `PlaneGeom`, `Seg2Geom`, `Loop2Geom`, `Region2Geom`, `Seg2ArcFit`,
`Loop2ArcFit` and `Region2ArcFit`, in scope with `use solidcraft_geom::prelude::*`. Every caller
names the types from `solidcore_record`; geom no longer defines or re-exports them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NvoxKMbMFSQU8tzRtoJhtT

* Kernel: pin solidcore-record to solidcore main f5e8e9a29

solidcore-record is merged on solidcore main (tangency-labs/solidcore#2519); its crate there is
identical to the rev pinned before but for its clippy configuration. The lock is regenerated
against the merged rev.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NvoxKMbMFSQU8tzRtoJhtT

---------

Co-authored-by: jyviko <2501648+jyviko@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 09:04:26 -05:00

151 lines
8.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#![allow(clippy::unwrap_used, clippy::panic)]
use serde_json::{Value, json};
use solidcore_record::Vec3;
use solidcraft_doc::{FeatureKind, ProfileSel};
use crate::Session;
fn run(session: &mut Session, command: &str, params: Value) -> Value {
session.execute(command, &params).unwrap_or_else(|error| panic!("{command}: {error}"))
}
#[test]
fn breaking_offset_preserves_downstream_material_history_and_source_independence() {
for distance in [0.2, 1.4] {
let (mut session, sketch, radius) = rounded_source();
let offset = run(&mut session, "sketch.offset", json!({"curves":["l1"],"distance":distance,"side":[0,0]}));
let association = offset["association"].as_str().unwrap().to_string();
let curves = session.model.state().sketch(sketch).unwrap().sketch.offset_boundary(&association).unwrap();
run(&mut session, "sketch.finish", json!({}));
run(&mut session, "solid.extrude", json!({"sketch":sketch,"profiles":{"offset_boundaries":[association]},"distance":2,"operation":"new"}));
let before_volume = session.world_state().unwrap().bodies.last().unwrap().mesh().measure().volume.abs();
run(&mut session, "sketch.edit", json!({"sketch":sketch}));
let before = session.doc.to_json();
let history = session.undo.len();
run(&mut session, "sketch.offset.break", json!({"curve":association}));
assert_eq!(session.undo.len(), history + 1);
assert!(session.model.results.iter().all(|result| result.error.is_none()));
assert!(
matches!(&session.doc.features.last().unwrap().kind, FeatureKind::Extrude { profiles: ProfileSel::Curves { loops }, .. } if loops == &vec![curves.clone()])
);
let frozen = session.doc.to_json();
assert!((session.world_state().unwrap().bodies.last().unwrap().mesh().measure().volume.abs() - before_volume).abs() < 1e-6);
run(&mut session, "edit.undo", json!({}));
assert_eq!(session.doc.to_json(), before);
run(&mut session, "edit.redo", json!({}));
assert_eq!(session.doc.to_json(), frozen);
run(&mut session, "parameters.change", json!({"name":radius,"expression":"2 mm"}));
assert!(session.model.results.iter().all(|result| result.error.is_none()));
assert!((session.world_state().unwrap().bodies.last().unwrap().mesh().measure().volume.abs() - before_volume).abs() < 1e-6);
let reopened = Session::new(solidcraft_doc::Document::from_json(&session.doc.to_json()).unwrap());
assert!(reopened.model.results.iter().all(|result| result.error.is_none()));
assert!((reopened.world_state().unwrap().bodies.last().unwrap().mesh().measure().volume.abs() - before_volume).abs() < 1e-6);
}
}
fn rounded_source() -> (Session, u64, String) {
let mut session = Session::default();
run(&mut session, "sketch.create", json!({"plane":"XY","name":"Rounded"}));
let sketch = session.active_sketch.unwrap();
run(&mut session, "sketch.rectangle.two_point", json!({"p0":[-38,-33],"p1":[38,33]}));
let fillet = run(&mut session, "sketch.fillet", json!({"points":["l1.start","l1.end","l2.end","l3.end"],"radius":"1 mm"}));
(session, sketch, fillet["param"].as_str().unwrap().to_string())
}
#[test]
fn inset_past_corner_radius_creates_a_real_boundary_and_saved_profile_identity() {
let (mut session, sketch, _) = rounded_source();
let offset = run(&mut session, "sketch.offset", json!({"curves":["l1"],"distance":1.4,"side":[0,0]}));
assert_eq!(offset["curves"].as_array().unwrap().len(), 4);
let state = session.model.state();
let solved = state.sketch(sketch).unwrap();
let record = solved.sketch.offsets.last().unwrap();
let topology = record.topology.as_ref().unwrap();
let association = topology.id.clone();
assert_eq!(topology.roles.len(), 8);
assert_eq!(topology.roles.iter().filter(|role| role.retired).count(), 4);
assert_eq!(solved.sketch.offset_boundary(&association).unwrap(), record.outputs);
assert!(topology.roles.iter().filter(|role| role.retired).all(|role| solved.sketch.curve_index(&role.output).is_none()));
run(&mut session, "sketch.finish", json!({}));
run(
&mut session,
"solid.extrude",
json!({"sketch":sketch,"profiles":{"offset_boundaries":[association]},"distance":2,"operation":"new","name":"Inner"}),
);
let feature = session.doc.features.last().unwrap();
assert!(matches!(&feature.kind, FeatureKind::Extrude { profiles: ProfileSel::OffsetBoundaries { associations }, .. } if associations.len() == 1));
let check = |session: &Session| {
assert!(session.model.results.iter().all(|result| result.error.is_none()));
let state = session.world_state().unwrap();
let body = state.bodies.last().unwrap();
// The exact inward parallel set is a sharp 73.2 by 63.2 rectangle.
let mesh = body.mesh();
assert!((mesh.measure().volume.abs() - 73.2 * 63.2 * 2.0).abs() < 1e-6);
assert!(mesh.contains(Vec3::new(36.5, 31.5, 1.0)));
assert!(!mesh.contains(Vec3::new(36.7, 31.5, 1.0)));
assert!(!mesh.contains(Vec3::new(36.5, 31.7, 1.0)));
};
check(&session);
let saved = session.doc.to_json();
let reopened = Session::new(solidcraft_doc::Document::from_json(&saved).unwrap());
check(&reopened);
assert_eq!(reopened.doc.to_json(), saved);
let undo = session.undo.len();
let redo = session.redo.len();
for params in [
json!({"profiles":{"offset_boundaries":[]}}),
json!({"profiles":{"offset_boundaries":["missing"]}}),
json!({"profiles":{"offset_boundaries":[association,association]}}),
json!({"profiles":[[record.outputs[0],record.outputs[1],record.outputs[2]]]}),
] {
let mut params = params;
params["sketch"] = json!(sketch);
params["distance"] = json!(1);
assert!(session.execute("solid.extrude", &params).is_err());
assert_eq!(session.doc.to_json(), saved);
assert_eq!(session.undo.len(), undo);
assert_eq!(session.redo.len(), redo);
}
}
/// #10984: a dog bone whose round ends touch at a cusp on each side. Drawn 2 mm outside, the
/// outline meets in each notch outside the bone, where the two ends' copies cross before
/// their ends. One notch ran both copies on to their far crossing instead, a loop into the
/// bone, as both crossings are as far from the notch.
#[test]
fn an_outline_outside_a_dog_bone_meets_outside_both_notches() {
let mut session = Session::default();
run(&mut session, "sketch.create", json!({"plane":"XY","name":"Bone"}));
let sketch = session.active_sketch.unwrap();
run(&mut session, "sketch.line", json!({"points":[[-10,-10],[10,-10]]}));
run(&mut session, "sketch.arc.center_point", json!({"center":[20,-10],"start":[10,-10],"end":[20,0]}));
run(&mut session, "sketch.arc.center_point", json!({"center":[20,10],"start":[20,0],"end":[10,10]}));
run(&mut session, "sketch.line", json!({"points":[[10,10],[-10,10]]}));
run(&mut session, "sketch.arc.center_point", json!({"center":[-20,10],"start":[-10,10],"end":[-20,0]}));
run(&mut session, "sketch.arc.center_point", json!({"center":[-20,-10],"start":[-20,0],"end":[-10,-10]}));
run(&mut session, "sketch.offset", json!({"curves":["l1"],"distance":2,"side":[0,-40]}));
let state = session.model.state();
let sk = &state.sketch(sketch).unwrap().sketch;
let record = sk.offsets.last().unwrap();
assert_eq!(record.outputs.len(), 6);
let mut notches: Vec<f64> = Vec::new();
for id in &record.outputs {
let curve = &sk.curves[sk.curve_index(id).unwrap()];
let (a, b) = match curve.kind {
solidcraft_sketch::CurveKind::Line { a, b } | solidcraft_sketch::CurveKind::Arc { a, b, .. } => (a, b),
_ => panic!("{id} is no line or arc"),
};
for p in [sk.points[a].pos, sk.points[b].pos] {
if p.y.abs() < 1e-6 {
notches.push(p.x);
}
}
}
notches.sort_by(f64::total_cmp);
// The copies have radius 12 about centres 20 apart: they cross 12² − 10² = 44, √44 off
// the notch's line, outside it: x = ±(20 + √44).
let outside = 20.0 + 44f64.sqrt();
assert_eq!(notches.len(), 4, "{notches:?}");
assert!(notches.iter().zip([-outside, -outside, outside, outside]).all(|(x, want)| (x - want).abs() < 1e-6), "{notches:?}");
}