Files
solidcraft/crates/kernel/src/planar_faces_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

134 lines
5.6 KiB
Rust

#![allow(clippy::unwrap_used, clippy::panic)]
//! A wall swept along a spiral has a flat top and bottom that are B-spline faces: a join of two
//! such walls, whose tops and bottoms coincide.
use solidcraft_geom::prelude::*;
use std::f64::consts::TAU;
use solidcore_record::{Loop2, Plane, Region2, Vec2, Vec3};
use crate::*;
const WALL: f64 = 12.0;
const HEIGHT: f64 = 60.0;
/// How far the spiral moves out in one turn.
const PITCH: f64 = 22.0;
/// The centreline of the spiral that starts `start` radians round from +X at `radius`, `t` radians along.
fn centre(radius: f64, start: f64, t: f64) -> Vec3 {
let (r, a) = (radius + PITCH / TAU * t, start + t);
Vec3::new(r * a.cos(), r * a.sin(), 0.0)
}
/// A wall `WALL` thick and `HEIGHT` tall along that spiral for `turn` radians, in `stations` steps,
/// the profile square to the path.
fn spiral_wall(radius: f64, start: f64, turn: f64, stations: usize) -> Body {
let path: Vec<Vec3> = (0..=stations).map(|i| centre(radius, start, turn * i as f64 / stations as f64)).collect();
let tangent = (path[1] - path[0]).normalized().unwrap();
// x up the wall, y across it: x cross y runs along the path.
let plane = Plane { origin: path[0], x: Vec3::Z, y: tangent.cross(Vec3::Z) };
let profile = Region2 {
outer: Loop2::polygon(&[
Vec2::new(0.0, -WALL / 2.0),
Vec2::new(HEIGHT, -WALL / 2.0),
Vec2::new(HEIGHT, WALL / 2.0),
Vec2::new(0.0, WALL / 2.0),
]),
holes: vec![],
};
crate::sweep_path(&plane, &profile, &path).unwrap()
}
/// Two such walls whose centrelines are 11 mm apart, so that they overlap by 1 mm; the second
/// starts a little further round, so that no end face lies in the other's.
fn two_walls(turn: f64, stations: usize) -> (Body, Body) {
(spiral_wall(60.0, 0.0, turn, stations), spiral_wall(71.0, 0.26, turn, stations))
}
/// The plan area of the union of the walls (each given as `(radius, start)`), by sampling every
/// 0.4 mm: a point is in a wall if it is within half its thickness of the wall's centreline and
/// not beyond either end (flat ends).
fn union_plan_area(walls: &[(f64, f64)], turn: f64) -> f64 {
let lines: Vec<Vec<Vec3>> = walls.iter().map(|(r, s)| (0..=400).map(|i| centre(*r, *s, turn * i as f64 / 400.0)).collect()).collect();
let inside = |line: &[Vec3], p: Vec3| -> bool {
let mut best = (f64::INFINITY, 0.0, 0);
for (j, w) in line.windows(2).enumerate() {
let d = w[1] - w[0];
let s = ((p - w[0]).dot(d) / d.dot(d)).clamp(0.0, 1.0);
let dist = (p - (w[0] + d * s)).len();
if dist < best.0 {
best = (dist, (p - w[0]).dot(d) / d.dot(d), j);
}
}
let beyond = (best.2 == 0 && best.1 < 0.0) || (best.2 + 2 == line.len() && best.1 > 1.0);
best.0 <= WALL / 2.0 && !beyond
};
let h = 0.4;
let n = (2.0 * 100.0 / h) as usize;
let mut cells = 0usize;
for i in 0..n {
for j in 0..n {
let p = Vec3::new(-100.0 + (i as f64 + 0.5) * h, -100.0 + (j as f64 + 0.5) * h, 0.0);
if lines.iter().any(|l| inside(l, p)) {
cells += 1;
}
}
}
cells as f64 * h * h
}
/// The faces of `b` that are neither planes nor cylinders (the face types `measure` reports).
fn free_form_faces(b: &Body) -> usize {
crate::measure(b).unwrap().merged.face_types.get("other").copied().unwrap_or(0)
}
fn join_checked(a: &Body, b: &Body, walls: &[(f64, f64)], turn: f64) {
let joined = boolean(a, b, BoolOp::Union).unwrap().unwrap();
assert_eq!(joined.shell_count(), 1);
assert!(joined.validity().is_empty(), "{:?}", joined.validity());
// Decided in 2D: the walls come back as arcs, not as splines.
assert_eq!(free_form_faces(&joined), 0);
let want = HEIGHT * union_plan_area(walls, turn);
let got = joined.mesh(&crate::MeshTolerance::chord(1e-2)).unwrap().measure().volume;
assert!((got - want).abs() < 5e-3 * want, "{got} vs {want}");
}
/// Two walls whose tops and bottoms coincide: the join is one solid with the walls as arcs.
#[test]
fn a_join_of_swept_walls_is_decided_in_2d() {
let turn = 0.6;
let (a, b) = two_walls(turn, 16);
join_checked(&a, &b, &[(60.0, 0.0), (71.0, 0.26)], turn);
}
/// Two interleaved spirals, 0.75 turn, as in the snail: the second is the first turned half a turn.
#[test]
fn interleaved_spiral_walls_join() {
let turn = 0.75 * TAU;
let half = std::f64::consts::PI;
let (a, b) = (spiral_wall(60.0, 0.0, turn, 36), spiral_wall(60.0, half, turn, 36));
join_checked(&a, &b, &[(60.0, 0.0), (60.0, half)], turn);
}
/// An extrusion of a spline outline has plane caps already: a cut of it by a cylinder keeps the
/// exact spline wall, whatever the 2D route does for swept walls.
#[test]
fn a_cut_of_an_extruded_spline_keeps_its_spline_wall() {
let p = Vec2::new;
let line = |a: Vec2, b: Vec2| solidcore_record::Seg2::Line { a, b };
let outline = Loop2 {
segs: vec![
line(p(0.0, 0.0), p(40.0, 0.0)),
line(p(40.0, 0.0), p(40.0, 20.0)),
solidcore_record::Seg2::Cubic { p0: p(40.0, 20.0), p1: p(30.0, 35.0), p2: p(10.0, 5.0), p3: p(0.0, 20.0) },
line(p(0.0, 20.0), p(0.0, 0.0)),
],
};
let plate = crate::extrude(&Plane::XY, &[Region2 { outer: outline, holes: vec![] }], 0.0, 10.0).unwrap().remove(0);
assert_eq!(free_form_faces(&plate), 1);
let hole = crate::cylinder(Vec3::new(20.0, 10.0, -1.0), Vec3::Z, 3.0, 12.0).unwrap();
let cut = boolean(&plate, &hole, BoolOp::Cut).unwrap().unwrap();
assert_eq!(free_form_faces(&cut), 1);
assert!(cut.face_count() < 12, "{}", cut.face_count());
}