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

107 lines
4.9 KiB
Rust

//! Profile segments as exact schema 4 curves: each sketch segment on its plane becomes a 3D edge
//! curve and, on the plane's own surface (`origin + u x + v y`, (u, v) the sketch coordinates), its
//! pcurve, both on t in [0, 1] with the same knots and weights, so the pcurve lifts onto the edge
//! exactly. Lines are lines; arcs rational quadratic B-splines (one span per quarter turn or less);
//! cubic and higher Béziers B-splines on their own control points; conics rational quadratic
//! Béziers (exact ellipses, parabolas and hyperbolas).
use solidcore_record::brep::{Curve, Curve2, Surface};
use solidcore_record::{Plane, Seg2, Vec2, Vec3};
use solidcraft_geom::prelude::*;
use crate::{KernelError, Result};
/// The interval every curve [`segment`] makes runs on.
pub(crate) const INTERVAL: [f64; 2] = [0.0, 1.0];
/// The plane of a sketch as a record surface whose (u, v) are the sketch's coordinates.
pub(crate) fn plane_surface(plane: &Plane) -> Surface {
let a = |v: Vec3| [v.x, v.y, v.z];
Surface::Plane { origin: a(plane.origin), u_axis: a(plane.x), v_axis: a(plane.y) }
}
/// A clamped Bézier knot vector of `degree`.
fn bezier_knots(degree: usize) -> Vec<f64> {
std::iter::repeat_n(0.0, degree + 1).chain(std::iter::repeat_n(1.0, degree + 1)).collect()
}
/// The 2D rational control net of `s` (points, weights, knots), on t in [0, 1].
fn net(s: &Seg2) -> Result<(Vec<Vec2>, Vec<f64>, Vec<f64>)> {
Ok(match *s {
Seg2::Line { a, b } => (vec![a, b], vec![1.0; 2], bezier_knots(1)),
Seg2::Arc { center, radius, start, sweep } => {
if !(radius.is_finite() && radius > 0.0 && sweep.is_finite() && sweep != 0.0 && sweep.abs() <= std::f64::consts::TAU * (1.0 + 1e-9)) {
return Err(KernelError::Invalid("an arc with no radius or turn, or turning more than once".into()));
}
let sweep = sweep.clamp(-std::f64::consts::TAU, std::f64::consts::TAU);
let m = (sweep.abs() / std::f64::consts::FRAC_PI_2 - 1e-9).ceil().max(1.0) as usize;
let d = sweep / m as f64;
let at = |a: f64, r: f64| center + Vec2::new(a.cos(), a.sin()) * r;
let w = (d / 2.0).cos();
let (mut pts, mut weights, mut knots) = (vec![at(start, radius)], vec![1.0], vec![0.0; 3]);
for j in 0..m {
let a0 = start + d * j as f64;
pts.push(at(a0 + d / 2.0, radius / w));
weights.push(w);
pts.push(at(a0 + d, radius));
weights.push(1.0);
let u = (j + 1) as f64 / m as f64;
if j + 1 < m {
knots.extend([u, u]);
} else {
knots.extend([1.0; 3]);
}
}
(pts, weights, knots)
}
Seg2::Cubic { p0, p1, p2, p3 } => (vec![p0, p1, p2, p3], vec![1.0; 4], bezier_knots(3)),
Seg2::Conic { a, apex, b, w } => {
if !(w.is_finite() && w > 0.0) {
return Err(KernelError::Invalid("a conic's weight must be positive".into()));
}
(vec![a, apex, b], vec![1.0, w, 1.0], bezier_knots(2))
}
Seg2::Bezier { n, ref p } => {
let n = usize::from(n);
let pts = p.get(..=n).filter(|_| (1..=solidcore_record::MAX_BEZIER_DEGREE).contains(&n));
let pts = pts.ok_or_else(|| KernelError::Invalid("Bézier degree".into()))?;
(pts.to_vec(), vec![1.0; n + 1], bezier_knots(n))
}
})
}
/// The segment `s` on `plane` as an edge curve and its pcurve on [`plane_surface`], both on
/// [`INTERVAL`].
pub(crate) fn segment(plane: &Plane, s: &Seg2) -> Result<(Curve, Curve2)> {
let (pts, weights, knots) = net(s)?;
if pts.iter().any(|q| !q.is_finite()) {
return Err(KernelError::Invalid("a profile curve is not finite".into()));
}
let w = |q: Vec2| {
let p = plane.to_world(q);
[p.x, p.y, p.z]
};
if let Seg2::Line { a, b } = *s {
if a.dist(b) <= 0.0 {
return Err(KernelError::Invalid("a profile line has no length".into()));
}
let pc = Curve2::Spline { knots, points: vec![[a.x, a.y], [b.x, b.y]] };
return Ok((Curve::Line { start: w(a), end: w(b) }, pc));
}
if weights.iter().all(|x| *x == 1.0) {
let curve = Curve::Spline { knots: knots.clone(), points: pts.iter().map(|q| w(*q)).collect() };
return Ok((curve, Curve2::Spline { knots, points: pts.iter().map(|q| [q.x, q.y]).collect() }));
}
let homogeneous = pts
.iter()
.zip(&weights)
.map(|(q, h)| {
let [x, y, z] = w(*q);
[x * h, y * h, z * h, *h]
})
.collect();
let pcurve =
Curve2::RationalSpline { knots: knots.clone(), homogeneous: pts.iter().zip(&weights).map(|(q, h)| [q.x * h, q.y * h, *h]).collect() };
Ok((Curve::RationalSpline { knots, homogeneous }, pcurve))
}