Files
solidcraft/crates/engine/src/cmd/model_axis_tests.rs
T
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

205 lines
9.6 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.
//! An axis on an edge or round face of the model (`{edge|face: [x,y,z]}`): Revolve, Surface
//! Revolve, Circular Pattern and Plane at Angle turn about a circular edge's or a cylinder's
//! axis, and follow it when a parameter moves the cylinder.
use solidcraft_geom::prelude::*;
use std::f64::consts::PI;
use serde_json::{Value, json};
use solidcore_record::Vec3;
use crate::Session;
use crate::view::construction_planes;
fn run(s: &mut Session, id: &str, p: Value) -> Value {
match s.execute(id, &p) {
Ok(v) => v,
Err(e) => panic!("{id} {p}: {e}"),
}
}
fn volume(s: &mut Session) -> f64 {
run(s, "inspect.measure", json!({}))["total"]["volume_mm3"].as_f64().unwrap_or(f64::NAN)
}
fn rel(a: f64, b: f64) -> f64 {
(a - b).abs() / b.abs().max(1e-12)
}
/// A Ø10 × 10 shaft standing on XY at x = `cx` (a parameter, 40 mm).
fn shaft() -> Session {
let mut s = Session::default();
run(&mut s, "parameters.add", json!({"name": "cx", "expression": "40 mm"}));
run(&mut s, "solid.cylinder", json!({"base": ["cx", 0, 0], "radius": 5, "height": 10, "body_name": "Shaft"}));
s
}
const SHAFT: f64 = PI * 25.0 * 10.0;
/// A 2 × 6 rectangle on XZ, x 46…48, z 2…8: 6 to 8 mm from the shaft's axis.
fn ring_profile(s: &mut Session) {
run(s, "sketch.create", json!({"plane": "XZ", "name": "Profile"}));
run(s, "sketch.rectangle.two_point", json!({"p0": [46, -2], "p1": [48, -8]}));
run(s, "sketch.finish", json!({}));
}
/// Pappus: a 2 × 6 section whose centre is `r` from the axis, turned a full turn.
fn ring(r: f64) -> f64 {
2.0 * PI * r * 12.0
}
#[test]
fn revolve_about_a_circular_edge_follows_the_cylinder() {
let mut s = shaft();
ring_profile(&mut s);
// The shaft's top rim.
run(&mut s, "solid.revolve", json!({"sketch": "Profile", "axis": {"edge": [45, 0, 10]}, "operation": "new", "name": "Turn"}));
assert!(rel(volume(&mut s), SHAFT + ring(7.0)) < 1e-4, "turned about the shaft's axis: {}", volume(&mut s));
run(&mut s, "parameters.change", json!({"name": "cx", "expression": "40.5 mm"}));
assert!(rel(volume(&mut s), SHAFT + ring(6.5)) < 1e-4, "follows the shaft: {}", volume(&mut s));
// Saved and opened again it still follows.
let saved = s.doc.to_json();
let mut reopened = Session::new(solidcraft_doc::Document::from_json(&saved).unwrap());
assert!(rel(volume(&mut reopened), volume(&mut s)) < 1e-9);
run(&mut reopened, "parameters.change", json!({"name": "cx", "expression": "40 mm"}));
assert!(rel(volume(&mut reopened), SHAFT + ring(7.0)) < 1e-4);
}
#[test]
fn revolve_about_a_cylinder_wall() {
let mut s = shaft();
ring_profile(&mut s);
run(&mut s, "solid.revolve", json!({"sketch": "Profile", "axis": {"face": [45, 0, 5]}, "operation": "new"}));
assert!(rel(volume(&mut s), SHAFT + ring(7.0)) < 1e-4, "{}", volume(&mut s));
let Some(solidcraft_doc::Feature { kind: solidcraft_doc::FeatureKind::Revolve { axis, .. }, .. }) = s.doc.features.last() else {
panic!("no revolve")
};
assert!(matches!(axis, solidcraft_doc::AxisRef::Model { .. }), "kept as a reference: {axis:?}");
}
#[test]
fn surface_revolve_about_a_circular_edge() {
let mut s = shaft();
ring_profile(&mut s);
run(&mut s, "surface.revolve", json!({"sketch": "Profile", "axis": {"edge": [45, 0, 0]}, "angle": "180 deg"}));
run(&mut s, "parameters.change", json!({"name": "cx", "expression": "41 mm"}));
let st = s.world_state().unwrap();
// A half turn about x = 41: the far side of the profile ends up at x = 41 − 7 ± 1.
let b = st.bodies.last().map(|b| b.mesh().bounds()).unwrap();
assert!((b.min.x - (41.0 - 7.0)).abs() < 1e-3, "{b:?}");
assert!((b.max.x - 48.0).abs() < 1e-3, "{b:?}");
}
#[test]
fn circular_pattern_about_a_boss_wall_follows_the_boss() {
let mut s = Session::default();
run(&mut s, "parameters.add", json!({"name": "cx", "expression": "100 mm"}));
run(&mut s, "solid.box", json!({"length": 200, "width": 200, "height": 5, "corner": [0, 0, 0], "body_name": "Plate"}));
run(&mut s, "solid.cylinder", json!({"base": ["cx", 100, 0], "radius": 10, "height": 20, "operation": "join", "targets": ["Plate"]}));
run(&mut s, "solid.hole", json!({"position": [130, 100, 5], "diameter": 2, "name": "Hole"}));
run(&mut s, "solid.pattern.circular", json!({"features": ["Hole"], "axis": {"face": [110, 100, 10]}, "count": 4, "name": "Ring"}));
let holes = |s: &Session, cx: f64, r: f64| {
let st = s.world_state().unwrap();
let body = &st.bodies[0].body;
[(r, 0.0), (0.0, r), (-r, 0.0), (0.0, -r)]
.iter()
.all(|(dx, dy)| body.classify_point(Vec3::new(cx + dx, 100.0 + dy, 2.5)).unwrap() == solidcraft_kernel::PointClass::Outside)
};
assert!(holes(&s, 100.0, 30.0), "four holes round the boss");
run(&mut s, "parameters.change", json!({"name": "cx", "expression": "90 mm"}));
assert!(holes(&s, 90.0, 40.0), "the copies turn about the boss where it is now");
let st = s.world_state().unwrap();
assert_eq!(
st.bodies[0].body.classify_point(Vec3::new(100.0, 130.0, 2.5)).unwrap(),
solidcraft_kernel::PointClass::Inside,
"not where the boss was"
);
}
#[test]
fn plane_at_angle_about_a_cylinder_axis_follows_it() {
let mut s = shaft();
run(&mut s, "construct.plane.at_angle", json!({"base": "XZ", "axis": {"face": [45, 0, 5]}, "angle": "90 deg", "name": "Square"}));
let plane = |s: &Session| construction_planes(s).into_iter().find(|p| p.1 == "Square").map(|p| p.2).unwrap();
let pl = plane(&s);
assert!(pl.normal().cross(Vec3::X).len() < 1e-9, "turned square to XZ about Z: {pl:?}");
assert!(pl.height(Vec3::new(40.0, 0.0, 0.0)).abs() < 1e-6, "through the shaft's axis: {pl:?}");
run(&mut s, "parameters.change", json!({"name": "cx", "expression": "25 mm"}));
assert!(plane(&s).height(Vec3::new(25.0, 0.0, 0.0)).abs() < 1e-6, "follows the shaft");
}
#[test]
fn plane_at_angle_on_a_face_about_an_edge_follows_both() {
let mut s = Session::default();
run(&mut s, "parameters.add", json!({"name": "h", "expression": "20 mm"}));
run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": "h", "name": "Base"}));
// The top face, turned 45° about its front edge (y = 0, along X).
run(
&mut s,
"construct.plane.at_angle",
json!({"base": {"face": [5, 5, 20]}, "axis": {"edge": [20, 0, 20]}, "angle": "45 deg", "name": "Slanted"}),
);
let feature = s.doc.find_feature("Slanted").unwrap();
let solidcraft_doc::FeatureKind::ConstructionPlane { plane: solidcraft_doc::PlaneRef::AtAngle { base, line, .. } } = &feature.kind else {
panic!("at_angle plane: {:?}", feature.kind)
};
assert!(matches!(base.as_ref(), solidcraft_doc::PlaneRef::Face { anchor: Some(_), .. }), "{base:?}");
assert!(line.is_some(), "the edge is kept as a model reference");
let plane = |s: &Session| construction_planes(s).into_iter().find(|p| p.1 == "Slanted").map(|p| p.2).unwrap();
let half = 0.5_f64.sqrt();
let pl = plane(&s);
assert!(pl.height(Vec3::new(20.0, 0.0, 20.0)).abs() < 1e-6, "through the edge: {pl:?}");
assert!((pl.normal().z.abs() - half).abs() < 1e-9 && pl.normal().x.abs() < 1e-9, "45° to the top: {pl:?}");
// A taller box: the face and the edge both move up.
run(&mut s, "parameters.change", json!({"name": "h", "expression": "30 mm"}));
let pl = plane(&s);
assert!(pl.height(Vec3::new(20.0, 0.0, 30.0)).abs() < 1e-6, "follows the edge: {pl:?}");
assert!((pl.normal().z.abs() - half).abs() < 1e-9, "{pl:?}");
}
#[test]
fn the_axis_keeps_its_turn_whichever_way_the_edge_runs() {
// The top and bottom rims run opposite ways round the shaft; a quarter turn about either
// lands in the same place.
let quarter = |edge: [f64; 3]| {
let mut s = shaft();
ring_profile(&mut s);
run(&mut s, "solid.revolve", json!({"sketch": "Profile", "axis": {"edge": edge}, "angle": "90 deg", "operation": "new"}));
let st = s.world_state().unwrap();
st.bodies.last().map(|b| b.mesh().bounds()).unwrap()
};
let (top, bottom) = (quarter([45.0, 0.0, 10.0]), quarter([45.0, 0.0, 0.0]));
assert!(top.min.dist(bottom.min) < 1e-6 && top.max.dist(bottom.max) < 1e-6, "{top:?} vs {bottom:?}");
}
#[test]
fn hostile_model_axes_are_refused_without_panicking() {
let mut s = shaft();
ring_profile(&mut s);
let before = s.doc.to_json();
for axis in [
json!({"edge": [1e9, 0, 0]}),
json!({"face": [1e300, -1e300, 0]}),
json!({"edge": "top"}),
json!({"face": [45, 0]}),
// The flat top: no axis.
json!({"face": [40, 0, 10]}),
// The shaft's axis doesn't lie in a sketch on XY.
] {
for (cmd, p) in [
("solid.revolve", json!({"sketch": "Profile", "axis": axis})),
("surface.revolve", json!({"sketch": "Profile", "axis": axis})),
("solid.pattern.circular", json!({"bodies": ["Shaft"], "axis": axis, "count": 3})),
("construct.plane.at_angle", json!({"base": "XZ", "axis": axis, "angle": "10 deg"})),
] {
assert!(s.execute(cmd, &p).is_err(), "{cmd} {p}");
assert_eq!(s.doc.to_json(), before, "{cmd} {p} left the design as it was");
}
}
run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Flat"}));
run(&mut s, "sketch.rectangle.two_point", json!({"p0": [46, 2], "p1": [48, 8]}));
run(&mut s, "sketch.finish", json!({}));
let e = s.execute("solid.revolve", &json!({"sketch": "Flat", "axis": {"face": [45, 0, 5]}})).unwrap_err().to_string();
assert!(e.contains("does not lie in the sketch plane"), "{e}");
}