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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! MC33's interior rule: Chernyaev's sweeping plane, with Custodio's correction.
//!
//! Chernyaev, E. V., *Marching Cubes 33: Construction of Topologically Correct
//! Isosurfaces*, Technical Report CN/95-17, Institute for High Energy Physics
//! (1995), and Custodio, L., Etiene, T., Pesco, S. and Silva, C., *Practical
//! considerations on Marching Cubes 33 topological correctness*, Computers &
//! Graphics 37(7), pp. 840–850 (`10.1016/j.cag.2013.04.004`), §3.1 and §5.1.
//!
//! # What it decides, and how it differs from the face rule
//!
//! [`super::ambiguity`] resolves an ambiguous *face* by evaluating the bilinear
//! interpolant at that face's saddle point. This resolves an ambiguous *cell*:
//! two opposite faces can each be ambiguous and separately resolved, and the
//! cell's own topology still not be determined, because the regions they carry
//! may or may not be joined through the cell's **interior**. Custodio §3.1
//! states the criterion:
//!
//! > if there is a plane cutting the cube such that its saddle point is
//! > positive, it means that there is a positive area crossing the cube, i.e.
//! > the positive vertices are connected inside the cube.
//!
//! So sweep a plane between the two faces. At height `t` the trilinear
//! interpolant restricts to a bilinear function whose four corner values are
//! linear in `t`, and that function has one saddle, whose value is
//!
//! ```text
//! f(x_c(t)) = (A_t·C_t − B_t·D_t) / (A_t + C_t − B_t − D_t) = F(t) / Δ(t)
//! ```
//!
//! # The correction, which is the whole point of this module
//!
//! `F` is a **quadratic** in `t` and `Δ` is **linear**, and Chernyaev's test
//! tracks the sign of `F` alone. Custodio §5.1:
//!
//! > the polynomial F(t) … used by Chernyaev's MC33 algorithm for tracking the
//! > sign of the saddle point, is a second order equation in t and thus can only
//! > allow for two sign changes. Therefore, the sign tracked by the MC33
//! > algorithm will not match the expected one at some point.
//!
//! The quantity that actually matters is the **quotient**, and a quotient of a
//! quadratic by a linear is a hyperbola: where `Δ` has a root the saddle value
//! has a pole, and the sign jumps across it. That admits **three** sign changes
//! on `(0, 1)`, which no quadratic can track. Their Figure 6 is a case 13.5.2
//! that Chernyaev's test reads as 13.5.1 for exactly this reason.
//!
//! This module therefore evaluates the criterion **exactly**, rather than
//! through any polynomial proxy: `F/Δ` has constant sign on each interval
//! between its own roots and poles, so the breakpoints are enumerated and one
//! interior point of each resulting subinterval is tested. There is no
//! tolerance, no sampling and no iteration count — the answer is a finite sign
//! computation.
//!
//! # The coefficients, derived here rather than transcribed
//!
//! Writing `α = A₁ − A₀` and so on, `A_t = A₀ + tα`, so
//!
//! ```text
//! A_t·C_t = A₀C₀ + t(A₀γ + C₀α) + t²αγ
//! B_t·D_t = B₀D₀ + t(B₀δ + D₀β) + t²βδ
//! ```
//!
//! and subtracting gives `F(t) = a t² + b t + c` with
//!
//! ```text
//! a = αγ − βδ
//! b = C₀α + A₀γ − D₀β − B₀δ
//! c = A₀C₀ − B₀D₀
//! ```
//!
//! Those agree term for term with the three the paper prints, which is why they
//! are written out above: the agreement is the check (V-25). Rule 5 forbids
//! guessing a published formula, and re-deriving one is how you avoid having to.
//!
//! # What is *not* here
//!
//! The triangulation. Knowing that a cell has a tunnel is not the same as
//! meshing one, and MC33's tunnel cases need vertices in the cell **interior**
//! that this crate's grid-edge-keyed vertex cache has no slot for. That is
//! A-002b, and it is a separate ticket for that reason.
use crateReal;
use crate::;
/// The two faces a sweep runs between, as the bilinear corner values on each.
///
/// Each array is `[A, B, C, D]` in the cyclic order Custodio's Figure 2 uses, so
/// that `A` and `C` are one diagonal and `B` and `D` are the other. `lo` is the
/// face at `t = 0` and `hi` the face at `t = 1`; the corners must correspond,
/// meaning `lo[k]` and `hi[k]` are the two ends of a cell edge.
/// What the sweep found.
///
/// **The sign is part of the contract, and it is the opposite of the face
/// rule's word.** Both variants speak of the **positive — outside —** regions,
/// because that is the polarity Custodio's criterion is stated in (*"the
/// positive vertices are connected inside the cube"*) and this crate's inside
/// is negative (`crate::cube::is_inside`). [`super::ambiguity::face_is_joined`]
/// runs the other way: its `true` joins the *inside* corners, which on a face
/// is exactly the **negative**-saddle case. The two rules agree about the
/// geometry and attach "joined" to opposite sign classes —
/// `joined_names_the_positive_regions_and_the_face_rule_names_the_negative`
/// pins the relation, and A-002b must translate between them, not equate them.
/// Chernyaev's numerator-only test, kept purely as a cross-check.
///
/// **This is not the test the crate uses**, in the same way and for the same
/// reason `marching_cubes::reference` is not the table it uses: it exists so that
/// [`SweptFaces::test`] can be checked against the construction it corrects, and
/// so that the disagreement Custodio §5.1 predicts can be *demonstrated* rather
/// than described.
///
/// It reports whether `F` is positive anywhere in `[0, 1]`, which is the sign
/// Chernyaev's three conditions track. Where `Δ > 0` throughout the sweep the
/// two agree, because dividing by a positive number does not change a sign.
///
/// **Public, and not `#[cfg(test)]`, since R-023.** The set of cells on which
/// this and [`SweptFaces::test`] disagree *is* the set where the published
/// algorithms disagree, and that set is the subject of a measurement rather than
/// only of a test. A caller reproducing this crate's comparison needs the thing
/// being compared against.