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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//! Deterministic blending and 3D norms — the kernels a distance field needs
//! when the *metric itself* becomes a value.
//!
//! A signed distance field is written against a norm. Change the norm and the
//! same expression describes a different shape: `‖p‖₂ − r` is a sphere,
//! `‖p‖∞ − r` is a cube, `‖p‖₁ − r` an octahedron. Blending between those
//! three, and blending between two *regions* that use different norms, is the
//! whole of that operation — and every port of it (a SIMD kernel, a JIT, a
//! shader) has to agree bit for bit or the field disagrees with itself about
//! which side of a surface a point is on.
//!
//! This module pins that law once:
//!
//! * [`lerp`], [`clamp`], [`smoothstep`] — the blend weights. They use only
//! IEEE basic operations, so they were already deterministic; what was
//! missing is a *single canonical operation order* that every port copies.
//! `smoothstep` in particular is spelled differently by GLSL, HLSL and WGSL
//! implementations, and a shader compiled with fast-math will not reproduce
//! any of them.
//! * [`norm_l1`], [`norm_l2`], [`norm_linf`] and [`MetricWeights`] — the norm
//! and the convex combination of the three bases, with the two closed forms
//! a caller needs to stay safe: how fast the field can change
//! ([`MetricWeights::lipschitz`]) and how far a ball of radius `r` actually
//! reaches ([`MetricWeights::euclidean_radius`]).
//!
//! # Why a convex combination and not a free exponent
//!
//! The obvious parameterisation is the Minkowski `p`-norm
//! `(|x|ᵖ + |y|ᵖ + |z|ᵖ)^(1/p)`, but it calls [`powf`](crate::powf) three
//! times plus a root *per sample*, and `p < 1` silently stops being a norm.
//! A weighted sum `w₁‖·‖₁ + w₂‖·‖₂ + w∞‖·‖∞` covers the same visual range
//! (octahedron ↔ sphere ↔ cube) with three cheap evaluations, is a norm for
//! exactly the non-negative weights — a non-negative combination of norms
//! satisfies the triangle inequality term by term — and the failure case is
//! then a *type* error rather than a field that tears at run time.
//! [`MetricWeights::new`] rejects it.
//!
//! # Closed forms (3D)
//!
//! With `g(h) = w₁‖h‖₁ + w₂‖h‖₂ + w∞‖h‖∞`, positively homogeneous of degree
//! one, the extremes over the Euclidean unit sphere are exact, not estimates.
//! Sign and permutation symmetry let one take `h` descending and
//! non-negative, and then `g − w₂` is the linear functional `⟨u, h⟩` with
//! `u = (w₁+w∞, w₁, w₁)`:
//!
//! ```text
//! max g = ‖u‖₂ + w₂ = √((w₁ + w∞)² + 2w₁²) + w₂ (at u/‖u‖₂)
//! min g = min_{k∈{1,2,3}} (k·w₁ + w∞)/√k + w₂ (on an extreme ray)
//! ```
//!
//! The maximiser `u/‖u‖₂` is itself descending and non-negative, so it lies in
//! the cone and the maximum is attained; the minimiser `−u/‖u‖₂` does not, so
//! the minimum sits on one of the cone's three extreme rays `(1,0,0)`,
//! `(1,1,0)/√2`, `(1,1,1)/√3`. For the pure bases this gives `√3` and `1`
//! for `‖·‖₁`, `1` and `1` for `‖·‖₂`, `1` and `1/√3` for `‖·‖∞` — the last
//! being why a cube-metric ball of radius `r` reaches `√3·r` in Euclidean
//! space, and why an axis-aligned bound computed the Euclidean way misses it.
//!
//! `tests/analytic_metric.rs` checks both closed forms against a brute-force
//! sweep that shares no code with this module.
use cratesqrt;
/// Replaces a NaN produced by arithmetic with the canonical positive quiet
/// NaN.
///
/// `inf/inf` and `0·inf` raise the *default* NaN, whose sign bit is target
/// dependent: `+NaN` (`0x7fc0_0000`) on `aarch64` and `wasm32`, `−NaN`
/// (`0xffc0_0000`) on `x86_64`. Measured, not assumed — a sweep of the
/// special values over [`smoothstep`] differs in 293 of 8000 triples between
/// the two, all of them generated NaNs. Propagating an *input* NaN keeps its
/// payload identically on every target, so the pass-through arms
/// ([`clamp`], and the NaN arm of [`smoothstep`]) need no canonicalisation;
/// only results that arithmetic could have created do. Same policy as
/// [`sqrt`].
/// √2 as `f32`, exact literal (never computed at run time).
const SQRT_2: f32 = 1.414_213_5;
/// √3 as `f32`, exact literal (never computed at run time).
const SQRT_3: f32 = 1.732_050_8;
/// Why a weight triple is not a metric.
///
/// Values are produced by [`MetricWeights::new`] only; downstream code
/// matching on it needs a wildcard arm, as the list can grow.
/// `‖v‖₁` — the octahedral norm.
/// `‖v‖₂` — the Euclidean norm, via the deterministic [`sqrt`].
/// `‖v‖∞` — the cubic (Chebyshev) norm.
/// A norm built as a non-negative combination of `‖·‖₁`, `‖·‖₂` and `‖·‖∞`.
///
/// The weights are private: a triple that is not a metric must not exist, so
/// the only way in is [`MetricWeights::new`], which validates. The three
/// bases are available as [`L1`](Self::L1), [`L2`](Self::L2) and
/// [`LINF`](Self::LINF), and [`L2`](Self::L2) reproduces the Euclidean field
/// bit for bit (weights `0, 1, 0` contribute `0·n` terms, which are exact).
/// `(1 − t)·a + t·b`, exact at both endpoints.
///
/// The other common spelling, `a + t·(b − a)`, is monotone in `t` but returns
/// `a + (b − a)` at `t = 1`, which is not `b` in general. For a blended
/// region that matters more than monotonicity: outside the blend the weight is
/// exactly `1` or exactly `0`, and the field there has to be the untouched
/// original, not the original plus a rounding error.
///
/// `t` outside `[0, 1]` extrapolates. An infinite `a` or `b` gives the
/// canonical NaN at the opposite endpoint (`0 · ∞`).
/// `x` restricted to `[low, high]`.
///
/// NaN passes through unchanged (`f32::clamp` would too, but it also panics on
/// inverted bounds). With `low > high` the low bound wins, which keeps the
/// function total.
/// The cubic Hermite blend `3t² − 2t³` with `t = clamp((x − e0)/(e1 − e0), 0, 1)`.
///
/// Flat at both edges (`s'(e0) = s'(e1) = 0`) and symmetric about the
/// midpoint. Its steepest slope is `1.5/(e1 − e0)` at the midpoint, so a
/// transition of width `W` contributes at most `1.5/W` to the gradient of
/// whatever it blends — the number that decides whether a blended field stays
/// within its Lipschitz budget.
///
/// A degenerate or inverted interval (`e1 ≤ e0`) is treated as a step at
/// `e0`: below it `0`, at or above it `1`, with no division. NaN in `x`
/// propagates with its payload; a NaN the division created is canonicalised.