1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Normal-projected near-coplanar band for the exact kernel.
//!
//! Extracted from `kernel/mesh_bridge.rs` so the band-sizing concern lives in
//! one place, exactly as `csg/plane_eps.rs` (#2598) extracted the clipper's
//! plane epsilon. Deliberately the SAME formulation as that module and as its
//! TypeScript siblings — `epsForPlane` in
//! `packages/clash/src/contact/tri-tri.ts` (#2661) and the LOCAL-coordinate
//! tolerance sizing in `packages/drawing-2d/src/section-cutter.ts` (#2622) —
//! not a fourth one.
//!
//! # What was wrong with the scalar band
//!
//! [`near_band_from_extent`] takes ONE scalar `extent`, the max |coordinate|
//! over ALL THREE axes of both operands, and returns
//! `max(8*SNAP_GRID, extent * 2^-22)`. Its consumers then compare that band
//! against a PERPENDICULAR distance to a specific plane. A signed plane
//! distance is `dot(v - p, n)`, so each coordinate's f32 rounding noise enters
//! it weighted by that axis's normal component: an axis orthogonal to `n`
//! contributes nothing however far it puts the model from the origin.
//!
//! Collapsing the three axes to their max therefore sizes the band from an
//! axis the plane never sees. A model 10 km out in X, cut by a Z-normal
//! plane, got `band = 1e4 * 2^-22 ~= 2.4 mm` from the irrelevant X magnitude
//! where the real f32 rounding step in Z is ~1.2e-4 (the `8*SNAP_GRID`
//! floor). Two surfaces a genuine 2 mm apart then fell inside the band, were
//! reconciled as flush, and a 2 mm recess VANISHED — the same thin-flush-cut
//! collapse #2598 described for the clipper, live in the kernel's shared band
//! (`csg/world_frame_tests.rs::a_2mm_recess_cuts_identically_10km_out_in_x`,
//! far volume 0.3000030517580399 vs expected 0.29968).
//!
//! [`NearBand`] keeps the extent PER AXIS and projects it onto the plane's own
//! normal instead.
//!
//! # Determinism
//!
//! Plain FMA-free f64 (abs/mul/add/max) over already-snapped input
//! coordinates, fixed iteration order, no square root anywhere — the
//! comparison is made in the scaled `|n|`-weighted space precisely so no
//! normalisation is needed. Byte-identical native == wasm, like every other
//! predicate in this kernel.
use Tri;
use SNAP_GRID;
/// f32-ULP scale factor for a "worst-case" single-precision coordinate: for a
/// value with magnitude in `[2, 4)` the true float32 ULP is `2^-22`, and for
/// larger magnitudes the ULP only grows. Same `2^-22` term (and reasoning) as
/// `F32_ULP_SCALE` in `csg/plane_eps.rs` and in
/// `packages/clash/src/contact/narrow-phase.ts`.
const F32_ULP_SCALE: f64 = 1.0 / 4_194_304.0;
/// The band's floor: the per-axis-snap scatter envelope near the origin, for
/// two operands, with margin — `8*SNAP_GRID` in CALLER units, so ~122 µm on a
/// metre-denominated caller and ~122 nm on a millimetre-denominated one
/// (#2684). Unchanged from the scalar formula and
/// deliberately NOT projected: it is a tuned absolute allowance for
/// [`SNAP_GRID`] quantization, not a coordinate-magnitude term, and tightening
/// it is a tolerance change needing its own corpus evidence. Same split as
/// `csg/plane_eps.rs`, which also projects only the magnitude term.
pub const NEAR_BAND_FLOOR: f64 = 8.0 * SNAP_GRID;
/// The near-coplanar band as PER-AXIS coordinate extents, resolved against a
/// plane's own normal by [`NearBand::scaled_band2`].
///
/// Build it over exactly the coordinates whose f32 quantization can move the
/// distance being tested (both operands, plus the probe point where there is
/// one), then ask it per plane.
pub
/// The legacy SCALAR near-coplanar band: `max(8*SNAP_GRID, extent * 2^-22)`
/// from a single max-over-axes `extent`.
///
/// Retained ONLY for consumers that are not testing a distance along a plane
/// normal, where the collapse to a scalar is therefore not the defect
/// described in this module's header:
///
/// - `kernel/arrangement/classify.rs` (`BComponents::new`) pads an AABB in
/// every axis. An isotropic pad is what an axis-aligned box wants, and it is
/// sized from `operand_extent` (`2 * hi + 1`) with a further 4x factor, so it
/// stays a comfortable superset of every projected band inside it.
/// - `clash_solid.rs` gates the trust threshold for `intersection_solid`. That
/// caller IS in the defect class (its own known-failing corpus case,
/// `clash_solid_world_frame_tests.rs::a_5mm_overlap_10km_out_in_x_must_still_be_a_solid`,
/// pins it) but it measures a mesh THICKNESS, not a plane distance, so its
/// fix is a different change with its own evidence and is deliberately not
/// folded in here.
pub