ifc_lite_geometry/math/tol.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! The tolerance catalogue (F2c, #7774, charter #7670): every named numeric
6//! tolerance the geometry stack shares, with the unit it is measured in and
7//! why it has that value.
8//!
9//! # Contract
10//!
11//! - **Values here are the values the code shipped with.** F2c is a refactor:
12//! each constant is the literal it replaced, bit for bit. Where two sites
13//! used different values for what looks like one concept, there are two
14//! constants (see "Open policy questions" below), not a silent unification.
15//! - **Every constant states its unit.** The unit tags are:
16//! - *model units*: the IFC file's length unit (mm in a mm-authored file,
17//! m in a metre file). A constant in model units means a different
18//! physical distance per file. These are the scale-dependent tolerances
19//! C3 reviews.
20//! - *metres*: a physical distance, applied to data already scaled to
21//! metres.
22//! - *dimensionless*: a sine, cosine, ratio or parameter fraction.
23//! - *radians*, *relative* (a fraction of a magnitude), *ulps* (a multiple
24//! of f32 or f64 rounding error).
25//! - *length^n*: a power of model units (squared distance, determinant).
26//! - **Thresholds are still arguments in [`super::v3`] and [`super::mat4`].**
27//! This module supplies the names callers pass.
28//!
29//! # Open policy questions (for C3, no behaviour change here)
30//!
31//! - [`POINT_COINCIDENT_EPS`] (1e-9) and [`CURVE_JUNCTION_EPS`] (1e-6) both
32//! answer "are these two profile or curve points the same point", in model
33//! units. A 1e-9 model-unit gap is 1 nm in a metre file and 1 pm in a mm
34//! file, a few ulps of f64 at 1 km coordinates (about 0.2 pm), so
35//! large-coordinate files can miss coincidences. C3 decides whether
36//! these become metres (scaled by `length_unit_scale`) or relative to the
37//! coordinate magnitude.
38//! - [`PARAM_END_MARGIN`] is an absolute curve-parameter clamp, so a span
39//! narrower than 1e-6 collapses to its start. C3 decides whether it
40//! becomes relative to `t_max - t_min`.
41//! - [`PARTIAL_VOID_EPS`] and [`MIN_PROFILE_DIMENSION`] are 1e-3 *model
42//! units* but are written (and were commented) as 1 mm: a micrometre in a
43//! mm file. C3 decides whether they take `length_unit_scale`.
44//! - The `clash` crate keeps its own copies of [`F32_ULP_SCALE`] and
45//! [`MIN_SIN_THETA`] (D21: clash does not depend on geometry). A test in
46//! `tol_tests` reads clash's source and fails if either value moves.
47//! - Deliberately not here: tolerances local to one algorithm that are already
48//! named and documented where they apply (`plane_eps`, `near_band`, the
49//! boolean kernel's exact-arithmetic thresholds). Moving them would
50//! separate a value from the proof that justifies it.
51
52// ---------------------------------------------------------------------
53// Linear algebra
54// ---------------------------------------------------------------------
55
56/// A 3x3 affine determinant below this is singular. Unit: dimensionless
57/// (placement chains carry the unit scale in the translation only, so the
58/// linear block is a rotation times an `IfcCartesianTransformationOperator`
59/// scale). 1e-18 is a uniform operator scale of 1e-6 per axis; the corpus
60/// minimum is 1e-3 (det 1e-9). D22: one value for glTF, the demesher and
61/// every `affine_inverse`.
62pub const SINGULAR_DET_EPS: f64 = 1e-18;
63
64/// `normalize(v, min_len)` with this threshold refuses only an exactly zero
65/// (or non-finite) vector. Unit: the input vector's own units. Used where the
66/// caller has already established the vector is a difference of distinct
67/// points and wants every non-zero direction.
68pub const NORMALIZE_NONZERO: f64 = 0.0;
69
70/// Smallest vector treated as a direction. Unit: the input's units (a unit
71/// direction, a point difference in model units, or a cross product of two
72/// unit axes). 1e-12 sits about four orders above the f64 rounding error of
73/// a unit-scale cross product, so a refused vector really is degenerate.
74pub const DIRECTION_MIN_LEN: f64 = 1e-12;
75
76/// Smallest placement axis (`IfcAxis2Placement` Axis/RefDirection after
77/// projection) accepted as a direction. Unit: dimensionless (axes are unit
78/// vectors, possibly authored with a few decimals). Coarser than
79/// [`DIRECTION_MIN_LEN`] because authored axes carry authoring noise.
80pub const AXIS_MIN_LEN: f64 = 1e-9;
81
82/// Smallest vector accepted as a facet or edge normal: the cross product of
83/// two polygon edges, or a 2D edge or radial vector in profile units. Unit:
84/// model units (length^2 for the edge cross product).
85pub const EDGE_CROSS_MIN_LEN: f64 = 1e-10;
86
87/// Smallest accumulated direction accepted as a unit vector: an axis left
88/// after projecting it onto a plane, or a vertex normal summed from adjacent
89/// face normals. Unit: dimensionless (sums of unit vectors). Coarse on
90/// purpose: a near-cancelling sum has a direction that is mostly rounding
91/// noise.
92pub const COARSE_DIRECTION_MIN_LEN: f64 = 1e-6;
93
94/// `|a - b|` below this in every element means "same matrix entry" when
95/// testing a placement for identity. Unit: dimensionless for rotation
96/// entries, model units for the translation column.
97pub const IDENTITY_EPS: f64 = 1e-9;
98
99/// The strict variant used for representation-map origins, where an
100/// identity result drops the transform entirely. Unit: as [`IDENTITY_EPS`].
101pub const IDENTITY_EPS_STRICT: f64 = 1e-12;
102
103/// An `IfcAxis2Placement2D` profile placement whose location components and
104/// direction deviate from the identity by less than this is skipped, and a
105/// direction-ratio vector shorter than this falls back to +X. Unit: model
106/// units for the location, dimensionless for the direction.
107pub const PLACEMENT_2D_EPS: f64 = 1e-10;
108
109/// Two directions whose cross product is shorter than this are parallel.
110/// Unit: dimensionless for the unit directions of `structural_edge` (the sine
111/// of the angle); length^2 for the unnormalised 2D edge vectors of
112/// `geom2d::line_intersection`, whose determinant is compared directly.
113pub const PARALLEL_EPS: f64 = 1e-9;
114
115/// Maximum component error when comparing two composed wall and opening
116/// frames to a signed axis permutation. Unit: dimensionless.
117pub(crate) const WALL_OPENING_AXIS_EPS: f64 = 1.0e-10;
118
119/// Largest placement roundoff for deciding that an aligned recess misses a
120/// wall body's interior. Above this uncertainty the ordinary cut decides.
121/// Unit: metres, after unit scaling.
122pub(crate) const WALL_OPENING_MAX_ROUNDOFF_M: f64 = 1e-9;
123
124/// Two unit axes are treated as orthogonal when `|dot|` is at most this, and
125/// as the same axis when `dot > 1 - this`. Unit: dimensionless (a cosine,
126/// 1e-4 is about 0.6 degrees).
127pub const ORTHO_EPS: f64 = 1.0e-4;
128
129/// Two edge vectors with `sin(theta)` below this are collinear, so their
130/// cross product defines no plane. Unit: dimensionless (a sine, 1e-3 is
131/// about 0.06 degrees). Scale-free, unlike an absolute cross-product floor.
132pub const MIN_SIN_THETA: f64 = 1.0e-3;
133
134// ---------------------------------------------------------------------
135// f32 rounding
136// ---------------------------------------------------------------------
137
138/// Half a unit in the last place of an f32, relative: `2^-24`. Unit:
139/// relative (ulps). The worst-case error of rounding one coordinate to f32.
140pub const F32_HALF_ULP_REL: f64 = 1.0 / 16_777_216.0;
141
142/// One f32 ULP at magnitude 1, relative: `2^-23` (`f32::EPSILON`). Unit:
143/// relative (ulps).
144pub const F32_ULP_REL: f64 = 1.0 / 8_388_608.0;
145
146/// The worst-case f32 ULP for a coordinate of magnitude 2 or more: `2^-22`
147/// (the true ULP in `[2, 4)`, growing for larger magnitudes). Unit: relative
148/// (ulps). The multiplier of an `extent` or `|v|` that gives an absolute
149/// noise tolerance in the same units as that magnitude, for quantities
150/// derived from f32-stored vertices.
151pub const F32_ULP_SCALE: f64 = 1.0 / 4_194_304.0;
152
153/// Eight f32 ULPs, relative: `2^-20`. Unit: relative (ulps). The budget for
154/// a world-space position recomposed through a handful of float ops
155/// (`instancing::verify`), where drift beyond the stored precision is a few
156/// ULPs.
157pub const F32_ULP_8: f64 = F32_ULP_REL * 8.0;
158
159// ---------------------------------------------------------------------
160// Curves and profiles (all in model units unless stated)
161// ---------------------------------------------------------------------
162
163/// Two consecutive profile or curve points closer than this (per axis, or in
164/// norm) are the same point and the second is dropped. Unit: model units.
165pub const POINT_COINCIDENT_EPS: f64 = 1e-9;
166
167/// A radius, segment or span at or below this is zero length. Unit: model
168/// units.
169pub const LENGTH_ZERO_EPS: f64 = 1e-9;
170
171/// First and last point of a swept curve closer than this per axis close the
172/// loop. Unit: model units. Coarser than [`POINT_COINCIDENT_EPS`] because the
173/// two ends come from independently evaluated parametrisations.
174pub const CURVE_JUNCTION_EPS: f64 = 1e-6;
175
176/// Two consecutive composite-curve segment end points closer than this (per
177/// axis) are the same joint and the repeat is dropped. Unit: model units.
178pub const SEAM_EPS: f64 = 1e-9;
179
180/// A curve parameter within this of a span end is at that end. Unit:
181/// dimensionless (a fraction of the span, or radians on a circle).
182pub const PARAM_EPS: f64 = 1e-9;
183
184/// Squared distance below which two sampled curve points coincide
185/// (1e-6 model units). Unit: length^2.
186pub const SAMPLE_COINCIDENT_SQ: f64 = 1e-12;
187
188/// Squared distance below which a sampled curve's last point already is its
189/// end (1e-9 model units). Unit: length^2.
190pub const SAMPLE_END_COINCIDENT_SQ: f64 = 1e-18;
191
192/// A trim span of at most this is empty and falls back to the full domain.
193/// Unit: curve parameter units.
194pub const TRIM_SPAN_MIN: f64 = 1e-12;
195
196/// The last sampled parameter stays this far inside the span end so the
197/// evaluator never reads on the closing knot. Unit: absolute curve parameter
198/// units (not relative to the span; see the open policy questions).
199pub const PARAM_END_MARGIN: f64 = 1e-6;
200
201/// A B-spline basis value or knot-span denominator below this is zero. Unit:
202/// dimensionless.
203pub const BASIS_EPS: f64 = 1e-10;
204
205/// Angles closer than this are the same angle when deduplicating arc
206/// breakpoints, and a span within this of 0 or tau is degenerate or full.
207/// Unit: radians.
208pub const ANGLE_EPS: f64 = 1e-6;
209
210/// Arc end points closer than this fraction of `max(radius, 1)` are
211/// coincident, so the arc is a full circle. Unit: relative.
212pub const ARC_COINCIDENT_REL: f64 = 1e-6;
213
214/// A counter-clockwise arc sweep below this is zero (and promoted to a full
215/// circle when the end points coincide). Unit: radians.
216pub const ARC_SWEEP_MIN: f64 = 1e-9;
217
218/// Absolute chord deviation budget when tessellating an arc: 0.5 mm. Unit:
219/// metres. It is a physical budget, so it is only correct on data already
220/// in metres.
221pub const CHORD_TOL_M: f64 = 5.0e-4;
222
223/// A void extending to within this of an extrusion's start or end face is a
224/// through-void. Unit: model units, written as 1 mm (see the open policy
225/// questions).
226pub const PARTIAL_VOID_EPS: f64 = 0.001;
227
228/// Smallest profile width or height that extrudes. Unit: model units,
229/// commented as 1 mm (see the open policy questions).
230pub const MIN_PROFILE_DIMENSION: f64 = 0.001;
231
232/// An extrusion direction component below this is zero (the extrusion is
233/// along local Z). Unit: dimensionless.
234pub const EXTRUSION_AXIS_COMPONENT_EPS: f64 = 0.001;
235
236// Retained numerical edge (already scaled to metres; not raw IFC units)
237// ---------------------------------------------------------------------
238
239/// Smallest admitted retained line length, circle radius or sweep thickness
240/// normal to the profile. Unit: metres. Floor of the retained request domain.
241pub const RETAINED_MIN_LENGTH_M: f64 = 1e-12;
242
243/// Maximum component mismatch between opposite parameterized rectangle edges.
244/// Unit: metres. Kept separate from minimum feature size: this checks closure
245/// of the derived rectangle that selects the rectangle qualification.
246pub const RETAINED_RECTANGLE_EDGE_EPS_M: f64 = 1e-12;
247
248/// Absolute join distance for consecutive derived retained boundary endpoints.
249/// Unit: metres. Independent trigonometric evaluation may introduce roundoff.
250pub const RETAINED_BOUNDARY_JOIN_EPS_M: f64 = 1e-10;
251
252/// Absolute floor in twice-area checks and their source/derived agreement
253/// budget. Unit: metres squared (on twice signed area, not ordinary area).
254pub const RETAINED_TWICE_AREA_EPS_M2: f64 = 1e-18;
255
256/// Relative source/derived twice-area agreement budget. Unit: relative.
257/// Added to the separate absolute twice-area floor.
258pub const RETAINED_TWICE_AREA_REL: f64 = 1e-9;
259
260/// Circular-interval endpoint margin at zero and one full turn. Unit: radians.
261pub const RETAINED_ARC_INTERVAL_EPS_RAD: f64 = 1e-12;
262
263/// Circular X-axis length deviation from unity. Unit: dimensionless.
264pub const RETAINED_CIRCLE_BASIS_EPS: f64 = 1e-12;
265
266/// Dot-product deviation of source placement axes from an orthonormal frame.
267/// Unit: dimensionless. Retained requests accept rigid placements only.
268pub const RETAINED_RIGID_DOT_EPS: f64 = 1e-10;
269
270/// Orthonormal dot-product and right-handed cross-product deviation at the
271/// retained dependency boundary. Unit: dimensionless. Matches the adapter's
272/// existing proper-rigid admission at 1e-12; this is stricter than the authored
273/// source-placement tolerance and does not change the native/browser policy.
274#[cfg(any(all(test, any(not(target_arch = "wasm32"), feature = "wasm_source_capture")), feature = "retained-runtime"))]
275#[cfg_attr(not(test), allow(dead_code))]
276pub(crate) const RETAINED_DEPENDENCY_RIGID_EPS: f64 = 1e-12;