dc38349cbf
* 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>
127 lines
6.5 KiB
Rust
127 lines
6.5 KiB
Rust
//! Offset surfaces (Surface › Offset): faces of a body copied out at a signed distance along
|
||
//! their normals, as a new surface body. Each face goes onto its surface's offset, SMLib's exact
|
||
//! offset (`smlib_offset_sheet`) of the face on its own; the offsets are then stitched where they
|
||
//! meet within the tolerance, so neighbouring faces moved to the same place along their shared
|
||
//! edge (meeting smoothly) keep sharing it, and across a sharp edge they come apart. Faces that
|
||
//! close up make a solid. An offset SMLib cannot make (one that would fold a face over) is its
|
||
//! error, never a fitted surface.
|
||
|
||
use crate::body::Body;
|
||
use crate::{KernelError, Result};
|
||
|
||
/// The picked faces of `body` (indices in its face order) offset by `distance` along their
|
||
/// outward normals (negative: against them) into one surface body. `tolerance`: how far apart
|
||
/// two offsets' edges may be and still be one edge. A distance of zero copies the faces. The
|
||
/// history is over `[body]`: each face is the offset of the one it came from.
|
||
pub fn offset_surface(body: &Body, faces: &[usize], distance: f64, tolerance: f64) -> Result<Body> {
|
||
body.require_brep("offset")?;
|
||
if !(distance.is_finite() && distance.abs() < 1e6) {
|
||
return Err(KernelError::Invalid("the offset distance must be finite".into()));
|
||
}
|
||
if !(tolerance.is_finite() && (1e-6..=10.0).contains(&tolerance)) {
|
||
return Err(KernelError::Invalid("the offset tolerance must be 0.000001…10 mm".into()));
|
||
}
|
||
let count = body.face_count();
|
||
let mut picked: Vec<usize> = Vec::new();
|
||
for &i in faces {
|
||
if i >= count {
|
||
return Err(KernelError::Invalid("no such face".into()));
|
||
}
|
||
if !picked.contains(&i) {
|
||
picked.push(i);
|
||
}
|
||
}
|
||
if picked.is_empty() {
|
||
return Err(KernelError::Invalid("pick the faces to offset".into()));
|
||
}
|
||
crate::surfaces::offset_faces(body, &picked, distance, tolerance)
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
use crate::{box_solid, cylinder, measure, sphere};
|
||
use solidcore_record::Vec3;
|
||
use solidcraft_geom::prelude::*;
|
||
|
||
fn rel(a: f64, b: f64) -> f64 {
|
||
(a - b).abs() / b.abs().max(1e-9)
|
||
}
|
||
fn face_at(b: &Body, p: Vec3) -> usize {
|
||
crate::surfaces::nearest_face(b, p).unwrap()
|
||
}
|
||
fn edges(b: &Body) -> usize {
|
||
b.edge_count()
|
||
}
|
||
|
||
/// Planes move exactly: a box's top 5 up is a 10 × 10 square at z = 15. Two faces across a
|
||
/// sharp edge come apart (8 edges, none shared); a zero distance copies.
|
||
#[test]
|
||
fn planes_move_and_sharp_edges_come_apart() {
|
||
let b = box_solid(Vec3::ZERO, Vec3::new(10.0, 10.0, 10.0)).unwrap();
|
||
let top = face_at(&b, Vec3::new(5.0, 5.0, 10.0));
|
||
let up = offset_surface(&b, &[top], 5.0, 0.01).unwrap();
|
||
let m = measure(&up).unwrap();
|
||
assert!(up.is_surface() && rel(m.area, 100.0) < 1e-9 && (m.bbox.min.z - 15.0).abs() < 1e-9, "{m:?}");
|
||
let side = face_at(&b, Vec3::new(5.0, 0.0, 5.0));
|
||
let two = offset_surface(&b, &[top, side], 1.0, 0.01).unwrap();
|
||
assert!(two.face_count() == 2 && edges(&two) == 8);
|
||
let copy = offset_surface(&b, &[top, side], 0.0, 0.01).unwrap();
|
||
assert!(copy.face_count() == 2 && rel(measure(©).unwrap().area, 200.0) < 1e-9);
|
||
}
|
||
|
||
/// A cylinder's side (one face, closed round its seam) moved out by 2 keeps its seam joined,
|
||
/// with radius 12: area 2π·12·20. Moved in past its axis, refused.
|
||
#[test]
|
||
fn a_cylinder_side_changes_radius() {
|
||
let c = cylinder(Vec3::ZERO, Vec3::Z, 10.0, 20.0).unwrap();
|
||
let d = 10.0 / 2f64.sqrt();
|
||
let side = [face_at(&c, Vec3::new(d, d, 10.0))];
|
||
assert_eq!(face_at(&c, Vec3::new(-d, -d, 10.0)), side[0], "the side is one face");
|
||
let out = offset_surface(&c, &side, 2.0, 0.01).unwrap();
|
||
assert!(out.face_count() == 1);
|
||
let want = 2.0 * std::f64::consts::PI * 12.0 * 20.0;
|
||
assert!(rel(measure(&out).unwrap().area, want) < 1e-3, "{} vs {want}", measure(&out).unwrap().area); // (measured on meshes)
|
||
let joined = offset_surface(&c, &side, 0.0, 0.01).unwrap();
|
||
assert_eq!(edges(&out), edges(&joined), "the side still closes on its seam");
|
||
assert!(offset_surface(&c, &side, -10.5, 0.01).is_err());
|
||
}
|
||
|
||
/// A sphere (fitted faces) moved out by 2: area 4π·12², within the tolerance; moved in to
|
||
/// its centre, refused.
|
||
#[test]
|
||
fn a_sphere_is_fitted_and_refused_past_its_centre() {
|
||
let s = sphere(Vec3::ZERO, 10.0).unwrap();
|
||
let all: Vec<usize> = (0..s.face_count()).collect();
|
||
let out = offset_surface(&s, &all, 2.0, 0.01).unwrap();
|
||
let want = 4.0 * std::f64::consts::PI * 144.0;
|
||
assert!(rel(measure(&out).unwrap().area, want) < 2e-3, "{} vs {want}", measure(&out).unwrap().area); // (measured on meshes)
|
||
// The patches meet smoothly: their offsets still share every edge, and close up.
|
||
assert_eq!(edges(&out), edges(&s));
|
||
assert!(!out.is_surface(), "the offset sphere closes into a solid");
|
||
assert!(rel(measure(&out).unwrap().volume, 4.0 / 3.0 * std::f64::consts::PI * 1728.0) < 2e-3, "outward"); // (measured on meshes)
|
||
assert!(offset_surface(&s, &all, -10.0, 0.01).is_err());
|
||
}
|
||
|
||
/// A doubly curved patch (a saddle, a B-spline height field) moved by 1.5: every point of the
|
||
/// offset lies 1.5 from the patch, within the tolerance.
|
||
#[test]
|
||
fn a_bspline_patch_is_offset_within_the_tolerance() {
|
||
let b = box_solid(Vec3::new(-14.0, -14.0, 0.0), Vec3::new(14.0, 14.0, 20.0)).unwrap();
|
||
let tool = cylinder(Vec3::new(0.0, -60.0, 50.0), Vec3::Y, 40.0, 120.0).unwrap();
|
||
let cup = crate::boolean(&b, &tool, crate::BoolOp::Cut).unwrap().unwrap();
|
||
let z = |x: f64| 50.0 - (1600.0 - x * x).sqrt();
|
||
let rim = [Vec3::new(14.0, 0.0, z(14.0)), Vec3::new(0.0, 14.0, z(0.0)), Vec3::new(-14.0, 0.0, z(-14.0)), Vec3::new(0.0, -14.0, z(0.0))];
|
||
let cover = crate::patch_edges(&cup, &rim).unwrap();
|
||
let record = cover.record();
|
||
let s = solidcraft_geom::brep::eval::SurfaceEval::new(&record.faces[0].surface).unwrap();
|
||
let out = offset_surface(&cover, &[0], 1.5, 0.01).unwrap();
|
||
let mesh = out.mesh(&crate::MeshTolerance::chord(0.05)).unwrap();
|
||
for p in mesh.positions.iter().step_by(7) {
|
||
let uv = s.project(*p, None, f64::INFINITY).unwrap();
|
||
let dist = s.point(uv).unwrap().dist(*p);
|
||
assert!((dist - 1.5).abs() < 0.03, "{dist} at {p:?}");
|
||
}
|
||
}
|
||
}
|