Files
makeCAD/crates/geom/src/vec.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

127 lines
3.8 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.
//! The geometry of vectors and points ([`Vec2`], [`Vec3`], plain data in the record): dot and
//! cross products, lengths, distances, directions and interpolation.
use solidcore_record::{Vec2, Vec3};
use crate::UnitVec3;
/// The geometry of a 2D vector or point.
// `len` is the vector's length, not a count.
#[allow(clippy::len_without_is_empty)]
pub trait Vec2Geom {
fn dot(self, o: Vec2) -> f64;
/// z component of the 3D cross product.
fn cross(self, o: Vec2) -> f64;
fn len(self) -> f64;
fn len2(self) -> f64;
fn dist(self, o: Vec2) -> f64;
fn normalized(self) -> Option<Vec2>;
/// Rotated +90°.
fn perp(self) -> Vec2;
fn angle(self) -> f64;
fn from_angle(a: f64) -> Vec2;
fn lerp(self, o: Vec2, t: f64) -> Vec2;
}
impl Vec2Geom for Vec2 {
fn dot(self, o: Vec2) -> f64 {
self.x * o.x + self.y * o.y
}
fn cross(self, o: Vec2) -> f64 {
self.x * o.y - self.y * o.x
}
fn len(self) -> f64 {
self.x.hypot(self.y)
}
fn len2(self) -> f64 {
self.dot(self)
}
fn dist(self, o: Vec2) -> f64 {
(self - o).len()
}
fn normalized(self) -> Option<Vec2> {
let l = self.len();
(l > crate::EPS && l.is_finite()).then(|| self / l)
}
fn perp(self) -> Vec2 {
Vec2::new(-self.y, self.x)
}
fn angle(self) -> f64 {
self.y.atan2(self.x)
}
fn from_angle(a: f64) -> Vec2 {
Vec2::new(a.cos(), a.sin())
}
fn lerp(self, o: Vec2, t: f64) -> Vec2 {
self + (o - self) * t
}
}
/// The geometry of a 3D vector or point.
// `len` is the vector's length, not a count.
#[allow(clippy::len_without_is_empty)]
pub trait Vec3Geom {
/// Takes a [`Vec3`] or a [`UnitVec3`](crate::UnitVec3).
fn dot(self, o: impl Into<Vec3>) -> f64;
/// Takes a [`Vec3`] or a [`UnitVec3`](crate::UnitVec3).
fn cross(self, o: impl Into<Vec3>) -> Vec3;
fn len(self) -> f64;
fn len2(self) -> f64;
fn dist(self, o: Vec3) -> f64;
fn normalized(self) -> Option<Vec3>;
fn lerp(self, o: Vec3, t: f64) -> Vec3;
fn min(self, o: Vec3) -> Vec3;
fn max(self, o: Vec3) -> Vec3;
/// Any unit vector perpendicular to `self` (which should be non-zero).
fn any_perp(self) -> Vec3;
/// Distance from this point to the segment `a`–`b`.
fn dist_to_segment(self, a: Vec3, b: Vec3) -> f64;
/// This vector scaled to length 1 ([`UnitVec3::new`]).
fn unit(self) -> Option<UnitVec3>;
}
impl Vec3Geom for Vec3 {
fn dot(self, o: impl Into<Vec3>) -> f64 {
let o = o.into();
self.x * o.x + self.y * o.y + self.z * o.z
}
fn cross(self, o: impl Into<Vec3>) -> Vec3 {
let o = o.into();
Vec3::new(self.y * o.z - self.z * o.y, self.z * o.x - self.x * o.z, self.x * o.y - self.y * o.x)
}
fn len(self) -> f64 {
self.len2().sqrt()
}
fn len2(self) -> f64 {
self.dot(self)
}
fn dist(self, o: Vec3) -> f64 {
(self - o).len()
}
fn normalized(self) -> Option<Vec3> {
let l = self.len();
(l > crate::EPS && l.is_finite()).then(|| self / l)
}
fn lerp(self, o: Vec3, t: f64) -> Vec3 {
self + (o - self) * t
}
fn min(self, o: Vec3) -> Vec3 {
Vec3::new(self.x.min(o.x), self.y.min(o.y), self.z.min(o.z))
}
fn max(self, o: Vec3) -> Vec3 {
Vec3::new(self.x.max(o.x), self.y.max(o.y), self.z.max(o.z))
}
fn any_perp(self) -> Vec3 {
let a = if self.x.abs() < 0.9 { Vec3::X } else { Vec3::Y };
self.cross(a).normalized().unwrap_or(Vec3::Z)
}
fn dist_to_segment(self, a: Vec3, b: Vec3) -> f64 {
let ab = b - a;
let l2 = ab.len2();
let t = if l2 > 0.0 { ((self - a).dot(ab) / l2).clamp(0.0, 1.0) } else { 0.0 };
self.dist(a + ab * t)
}
fn unit(self) -> Option<UnitVec3> {
UnitVec3::new(self)
}
}