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
//! Per-cell isotopy certificate, from normal variation.
//!
//! Ticket: T-015. Plantinga & Vegter, *Isotopic approximation of implicit curves
//! and surfaces*, SGP 2004 (`10.1145/1057432.1057465`).
//!
//! # Hausdorff distance does not certify topology, and cannot
//!
//! Two surfaces can be arbitrarily Hausdorff-close and not homeomorphic — a
//! sphere and a sphere with a hairline handle differ by as little as you like in
//! distance and are different objects. So every geometric error this crate
//! reports, however small, says nothing about whether the mesh has the right
//! topology. Every real theorem in this space adds a second hypothesis, and the
//! isosurface-specific ones make it a bound on how much the **normal** turns.
//!
//! # The condition, transcribed
//!
//! Plantinga & Vegter, verbatim: a grid `G` such that for each cell `C`
//!
//! ```text
//! 0 ∉ □F(C) ∨ ⟨□∇F(C), □∇F(C)⟩ > 0
//! ```
//!
//! where `□` is an interval bound over the cell. Their reasoning for why the
//! second clause is what matters: *"Due to the inner product constraint, S is
//! parametrizable in the direction of one of the axes. Therefore we cannot have
//! alternating signs of F at the vertices of C, since F would have to increase
//! along one edge, and decrease along the other parallel edge."* A cell that
//! passes has at most one surface component in it, isotopic to a disc, and the
//! single facet the mesher emits is the right answer *topologically* and not
//! merely nearby.
//!
//! # Why this is exact here rather than an interval-arithmetic approximation
//!
//! The general form needs interval arithmetic over an arbitrary `F`, which this
//! crate has no way to do — an [`Sdf`](crate::Sdf) hands back point values. A
//! sampled hull of the gradient would be a *lower* bound on its variation, so
//! the predicate could pass where the truth fails, which is the one direction a
//! certificate must never err in.
//!
//! But the surface Marching Cubes actually approximates is not `F`. It is the
//! **trilinear interpolant** of the eight corner values — that is the whole
//! subject of the `trilinear` module and of Grosso's papers — and for a
//! trilinear function the interval bounds are exact and closed-form:
//!
//! - `∂F/∂x` is *bilinear* in `(y, z)`, so over the cell it is a convex
//! combination of the four `x`-edge differences. Its exact range is their min
//! and max. Likewise for `y` and `z`.
//! - `F` itself is a convex combination of the eight corner values, so
//! `0 ∉ □F(C)` is exactly *"all eight corners share a sign"* — an inactive
//! cell. **The first clause is therefore free**, and for active cells the
//! predicate is the inner product alone.
//!
//! So this is a genuine certificate against the surface the extractor is
//! approximating, with no interval library and no sampling. What it does not
//! certify is the analytic field against its trilinear interpolant — that is a
//! separate question, and it is the one the sampling error already measures.
//!
//! # The cell size cancels
//!
//! `∂F/∂x` is a corner difference divided by `h`, and so are the other two. The
//! predicate tests the **sign** of a sum of three such squares, so `h²` factors
//! out and the arithmetic runs on raw corner differences. That is only true for
//! **isotropic** cells; an anisotropic grid would need each axis divided by its
//! own spacing before the sum.
use crateReal;
use crateShape3;
/// The four corner index pairs whose differences give `∂F/∂x` over a cell.
///
/// Corner `i` sits at `(i&1, (i>>1)&1, (i>>2)&1)` — `cube.rs`'s numbering, bit 0
/// being `x`. So an `x` difference pairs corners differing only in bit 0.
const X_PAIRS: = ;
/// Likewise for `∂F/∂y`: corners differing only in bit 1.
const Y_PAIRS: = ;
/// Likewise for `∂F/∂z`: corners differing only in bit 2.
const Z_PAIRS: = ;
/// Exact range of one partial derivative over the cell, in corner-difference
/// units.
/// Lower bound of the interval `[a, b] · [a, b]`.
///
/// Interval multiplication takes the minimum of the four corner products. For a
/// self-product those are `a²`, `ab` (twice) and `b²`, so the minimum is `a²` or
/// `b²` when the interval keeps a sign and `ab ≤ 0` when it straddles zero.
/// **That straddle is the whole test**: an axis whose derivative changes sign
/// inside the cell contributes a negative term and can only be rescued by
/// another axis dominating it.
/// Does this cell satisfy Plantinga & Vegter's condition?
///
/// `corner` is the eight corner values in `cube.rs`'s numbering. `true` means
/// the trilinear surface inside the cell is isotopic to what a single-facet
/// approximation produces — one component, no handle, no second sheet.
///
/// Inactive cells pass trivially by the first clause, since `0 ∉ □F(C)` is
/// exactly "all eight corners share a sign" for a trilinear interpolant.
/// What the certificate found over a whole grid.
/// Evaluate the certificate over a sampled grid.
///
/// # Errors
///
/// [`Error::GridTooSmall`](crate::Error::GridTooSmall) for a grid under 2×2×2,
/// [`Error::ShapeOverflow`](crate::Error::ShapeOverflow) if `values` is not one
/// entry per sample.