arris_math/lib.rs
1//! Numeric foundation of the Arris kernel: points, vectors and unit vectors
2//! over `nalgebra`, frames, axes and rigid motions, intervals, exact orientation
3//! predicates over `robust`, polynomial and interval-guarded root finding,
4//! and `Precision`, the model-wide tolerance configuration.
5//!
6//! Guarantees: `f64` throughout, no allocation in evaluation, no panic on
7//! any finite input, and no numeric literal standing in for a tolerance —
8//! every tolerance is a `Precision` field, a [`Tolerance`] derived from it,
9//! or a named constant with a comment (`.agents/rules/kernel.md`). Depends
10//! on nothing in the workspace.
11//!
12//! The point and vector types are `nalgebra`'s by alias and `nalgebra` is
13//! re-exported (`docs/adr/0001-nalgebra-types-by-alias.md`), so a caller
14//! reaches every operator and solver `nalgebra` has and a `nalgebra` major
15//! bump is an Arris API change.
16#![forbid(unsafe_code)]
17#![warn(missing_docs)]
18
19mod aabb;
20mod axis;
21mod frame;
22mod interval;
23mod isometry;
24mod precision;
25pub mod predicates;
26pub mod roots;
27mod tolerance;
28
29pub use nalgebra;
30
31pub use aabb::Aabb;
32pub use axis::Axis;
33pub use frame::{Frame, Frame2, FrameError, Handedness};
34pub use interval::{Interval, IntervalError};
35pub use isometry::Isometry;
36pub use precision::Precision;
37pub use tolerance::Tolerance;
38
39/// Relative rounding slack: a magnitude at or below this fraction of its
40/// natural scale is rounding noise, not a value. Eight ulps — what a
41/// handful of multiplications and one trigonometric evaluation leave
42/// behind, and orders of magnitude below any model tolerance. It is not a
43/// geometric tolerance and never decides whether two things are *the
44/// same*; it decides whether a computed quantity is zero *in floating
45/// point*: the radius of a sphere's parallel at the pole, `cos(π/2)`
46/// evaluated in `f64`, is `6e-17`, not `0`.
47pub const RELATIVE_ROUNDING: f64 = 8.0 * f64::EPSILON;
48
49/// The widest gap a file's entities may leave that a read closes by a
50/// tolerance, as a fraction of the part's size — the diameter of the ball
51/// holding its vertices and edges (ADR-0025 §4). A vertex or an edge whose
52/// measured gap is above it, or above `Precision::max_tolerance`, is
53/// refused as a gap: closing it would be sewing, which is healing, and a
54/// solid grown to cover it would be a wrong one.
55///
56/// Evidence: an exact writer leaves gaps at the rounding of its decimal
57/// digits, around `1e-15` of the part (Arris's own files); a kernel's
58/// raised tolerances reach a few `1e-7` on a part of unit size (the
59/// corpus's `boolean/seam-a-tolerance-from-crossing-fuse`, `1.2e-7` on a
60/// part of size 7). Open CASCADE's own STEP of every corpus fixture
61/// (the corpus's read-back stage) measures at most `2.5e-6` of the part's
62/// diagonal on 201 of 202 files, and `8.5e-5` on the last — `7.4e-4` on a
63/// part `8.7` across, `boolean/seam-beside-crossing-fuse`, whose boolean
64/// Open CASCADE itself gets wrong by that much. Converted to B-splines
65/// (step 15), its `closed-form-band` variant measures `1.05e-3` on a part
66/// whose ball is `9.3` across, `1.13e-4` of it — exactly the tolerance
67/// Open CASCADE's own shape carries there, which its checker passes.
68/// Healing tools commonly default their sewing tolerance to `1e-6`–`1e-3`
69/// of the model's size, so `2e-4` admits every file an exporting kernel
70/// called valid by its own tolerances, the largest seen with `1.8×` to
71/// spare, and refuses the gaps a healer would have to sew. A tolerance
72/// policy, not a rounding slack. Over the real-part corpus it refuses
73/// gaps in 7 of NIST's 38 parts, which count for the healing cycle
74/// (ADR-0026 §5, `docs/ROADMAP.md` §C4).
75pub const READ_GAP_FRACTION: f64 = 2e-4;
76
77/// `|x| ≤ RELATIVE_ROUNDING · |scale|`: `x` is zero to rounding at
78/// `scale`. A zero `scale` makes only an exact zero negligible.
79///
80/// ```
81/// use arris_math::is_negligible;
82/// use core::f64::consts::FRAC_PI_2;
83///
84/// assert!(is_negligible(3.0 * FRAC_PI_2.cos(), 3.0));
85/// assert!(!is_negligible(3.0 * (FRAC_PI_2 - 1e-9).cos(), 3.0));
86/// ```
87pub fn is_negligible(x: f64, scale: f64) -> bool {
88 x.abs() <= RELATIVE_ROUNDING * scale.abs()
89}
90
91/// An angle moved into `[0, 2π)`: what a periodic curve's or surface's
92/// parameter is reported in (`docs/DATA-MODEL.md` §Conventions). A
93/// negative angle whose sum with `2π` rounds up to `2π` becomes `0` —
94/// the same point on the circle, and inside the domain. A non-finite
95/// angle comes back unchanged.
96///
97/// ```
98/// use arris_math::wrap_angle;
99/// use core::f64::consts::TAU;
100///
101/// assert_eq!(wrap_angle(0.0), 0.0);
102/// assert_eq!(wrap_angle(-1.0), TAU - 1.0);
103/// assert_eq!(wrap_angle(-1e-300), 0.0);
104/// assert_eq!(wrap_angle(TAU + 1.0), 1.0);
105/// ```
106pub fn wrap_angle(t: f64) -> f64 {
107 if !t.is_finite() {
108 return t;
109 }
110 let t = if (0.0..core::f64::consts::TAU).contains(&t) {
111 t
112 } else {
113 t.rem_euclid(core::f64::consts::TAU)
114 };
115 if t >= core::f64::consts::TAU { 0.0 } else { t }
116}
117
118/// The end of one whole period from `lo`: `lo + period`, stepped down to
119/// the representable value below when that sum rounds up, so that
120/// `end - lo <= period` holds exactly. A closed edge spans one period and
121/// no more (`docs/DATA-MODEL.md` §Invariants, E1), and for a
122/// `lo` that is not a small multiple of the period the sum can round to
123/// one unit in the last place too far; this is the range's construction,
124/// not a tolerance. A non-finite argument, or a `period` that is not
125/// positive, comes back as `lo + period`.
126///
127/// ```
128/// use arris_math::period_end;
129/// use core::f64::consts::TAU;
130///
131/// assert_eq!(period_end(0.0, TAU), TAU);
132/// // A pave one unit in the last place below a full turn: the sum
133/// // rounds up, and the end is stepped back to keep the turn one turn.
134/// let lo = f64::from_bits(TAU.to_bits() - 1);
135/// assert!(lo + TAU - lo > TAU);
136/// assert!(period_end(lo, TAU) - lo <= TAU);
137/// ```
138pub fn period_end(lo: f64, period: f64) -> f64 {
139 let mut end = lo + period;
140 if !(end.is_finite() && period > 0.0) {
141 return end;
142 }
143 while end - lo > period {
144 end = f64::from_bits(end.to_bits() - 1);
145 }
146 end
147}
148
149/// A position in 3D. `nalgebra::Point3<f64>` (ADR-0001).
150pub type Point3 = nalgebra::Point3<f64>;
151/// A displacement or direction in 3D, of any length.
152/// `nalgebra::Vector3<f64>` (ADR-0001).
153pub type Vec3 = nalgebra::Vector3<f64>;
154/// A direction in 3D: a [`Vec3`] of unit length, guaranteed by construction.
155/// `nalgebra::Unit<Vector3<f64>>` (ADR-0001).
156pub type UnitVec3 = nalgebra::Unit<Vec3>;
157/// A position in a surface's (u, v) plane. `nalgebra::Point2<f64>`
158/// (ADR-0001).
159pub type Point2 = nalgebra::Point2<f64>;
160/// A displacement in the (u, v) plane. `nalgebra::Vector2<f64>` (ADR-0001).
161pub type Vec2 = nalgebra::Vector2<f64>;
162/// A direction in the (u, v) plane: a [`Vec2`] of unit length.
163/// `nalgebra::Unit<Vector2<f64>>` (ADR-0001).
164pub type UnitVec2 = nalgebra::Unit<Vec2>;
165/// A 3x3 matrix, column-major: a rotation, or a tensor such as the
166/// inertia of a body. `nalgebra::Matrix3<f64>` (ADR-0001).
167pub type Matrix3 = nalgebra::Matrix3<f64>;
168
169#[cfg(test)]
170mod tests {
171 use super::*;
172 use core::f64::consts::{PI, TAU};
173
174 #[test]
175 fn wrap_angle_lands_in_the_half_open_turn() {
176 assert_eq!(wrap_angle(0.0), 0.0);
177 assert_eq!(wrap_angle(-1e-300), 0.0);
178 assert_eq!(wrap_angle(-1.0), TAU - 1.0);
179 assert_eq!(wrap_angle(PI), PI);
180 assert!(wrap_angle(-f64::EPSILON) < TAU);
181 assert_eq!(wrap_angle(3.0 * TAU + 1.0), 1.0);
182 assert_eq!(wrap_angle(-3.0 * TAU - 1.0), TAU - 1.0);
183 assert!(wrap_angle(f64::NAN).is_nan());
184 assert_eq!(wrap_angle(f64::INFINITY), f64::INFINITY);
185 }
186}