isomesh 0.0.14

Engine-agnostic isosurface extraction: signed distance field in, triangles out. no_std, f32 and f64, no math library in the public API.
Documentation
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
//! `mark_edit_subgrid` exists to predict one thing: whether a chunk's Subgrid
//! mesh changed. So the tests that matter extract every chunk before and after
//! an edit and hold the prediction to the mesh.

use alloc::boxed::Box;
use alloc::vec;
use alloc::vec::Vec;
use core::cell::Cell;

use super::mark_edit_subgrid;
use crate::chunk::dirty::{DirtySet, EditReport, mark_edit};
use crate::chunk::{ChunkId, ChunkLayout};
use crate::fields::{Difference, Sphere, Union};
use crate::subgrid::extract::SubgridMarchingTetrahedra;
use crate::{Error, MeshBuffer, Sdf};

const SAMPLES: u32 = 4;

/// Four chunks of 8 cells along x, 0.25 apart: chunk `[x, 0, 0]` covers world x
/// in `[2x, 2x + 2)`. A power-of-two spacing, per M-32.
fn layout() -> ChunkLayout<f64> {
    ChunkLayout::new(8, 0.25, [0.0; 3]).expect("a valid layout")
}

const ROW: ([i64; 3], [i64; 3]) = ([0, 0, 0], [31, 7, 7]);

fn row_chunks() -> impl Iterator<Item = ChunkId> {
    (0..4).map(|x| ChunkId::new([x, 0, 0]))
}

/// A slab `2 * half` thick, perpendicular to x, centred at `at`. Negative inside.
struct Plate {
    at: f64,
    half: f64,
}

impl Sdf for Plate {
    type Scalar = f64;
    fn sample(&self, p: [f64; 3]) -> f64 {
        (p[0] - self.at).abs() - self.half
    }
}

/// Solid everywhere, a hair inside.
struct Solid;

impl Sdf for Solid {
    type Scalar = f64;
    fn sample(&self, _: [f64; 3]) -> f64 {
        -0.01
    }
}

/// [`Solid`] with a crack `2 * half` wide at `at`, which is outside.
///
/// At the corners of the cell the crack sits in, `half - |x - at|` is below
/// `-0.01`, so **no corner value differs from `Solid`'s**, and the crack is
/// still there between them.
struct Cracked {
    at: f64,
    half: f64,
}

impl Sdf for Cracked {
    type Scalar = f64;
    fn sample(&self, p: [f64; 3]) -> f64 {
        (-0.01f64).max(self.half - (p[0] - self.at).abs())
    }
}

type Field = Box<dyn Sdf<Scalar = f64>>;

fn sphere(center: [f64; 3], radius: f64) -> Sphere<f64> {
    Sphere { center, radius }
}

fn mesh(field: &Field, l: &ChunkLayout<f64>, id: ChunkId) -> MeshBuffer<f64> {
    let mut out = MeshBuffer::new();
    let shape = l.sample_shape().expect("a sample shape");
    SubgridMarchingTetrahedra::<f64>::new(SAMPLES)
        .expect("a valid resolution")
        .extract(field, &shape, l.sample_origin(id), l.cell_size(), &mut out)
        .expect("the chunk extracts");
    out
}

/// The chunks of the row whose Subgrid mesh differs between the two fields.
fn chunks_that_changed(l: &ChunkLayout<f64>, before: &Field, after: &Field) -> Vec<ChunkId> {
    row_chunks()
        .filter(|id| mesh(before, l, *id) != mesh(after, l, *id))
        .collect()
}

fn marked_by_subgrid(
    l: &ChunkLayout<f64>,
    before: &Field,
    after: &Field,
    samples: u32,
) -> (Vec<ChunkId>, EditReport) {
    let mut dirty = DirtySet::new();
    let report = mark_edit_subgrid(l, before, after, ROW.0, ROW.1, samples, &mut dirty)
        .expect("a region in bounds");
    (dirty.iter().collect(), report)
}

fn marked_by_corners(
    l: &ChunkLayout<f64>,
    before: &Field,
    after: &Field,
) -> (Vec<ChunkId>, EditReport) {
    let mut dirty = DirtySet::new();
    let report = mark_edit(l, before, after, ROW.0, ROW.1, &mut dirty).expect("a region in bounds");
    (dirty.iter().collect(), report)
}

/// Edits of every kind a caller makes, each of which changes some chunk's mesh.
fn edits() -> Vec<(&'static str, Field, Field)> {
    let plate = || Plate {
        at: 4.125,
        half: 0.075,
    };
    let with_plate = || -> Field {
        Box::new(Union {
            a: sphere([1.0, 1.0, 1.0], 0.5),
            b: plate(),
        })
    };
    let alone = || -> Field { Box::new(sphere([1.0, 1.0, 1.0], 0.5)) };
    let crack = || Cracked {
        at: 1.125,
        half: 0.075,
    };
    vec![
        (
            "a sphere grows in place",
            alone(),
            Box::new(sphere([1.0, 1.0, 1.0], 0.6)),
        ),
        (
            "a sphere moves from chunk 0 into chunk 2",
            alone(),
            Box::new(sphere([5.0, 1.0, 1.0], 0.5)),
        ),
        (
            "a plate thinner than a cell is added",
            alone(),
            with_plate(),
        ),
        ("that plate is removed", with_plate(), alone()),
        (
            "a crack no corner sees is cut into solid",
            Box::new(Solid),
            Box::new(crack()),
        ),
        ("that crack is closed", Box::new(crack()), Box::new(Solid)),
        (
            "a plate is added beside a sphere's surface",
            Box::new(sphere([1.0, 1.0, 1.0], 1.1)),
            Box::new(Union {
                a: sphere([1.0, 1.0, 1.0], 1.1),
                b: Plate {
                    at: 2.125,
                    half: 0.075,
                },
            }),
        ),
        (
            "a brush carves across the seam between chunks 0 and 1",
            Box::new(sphere([1.5, 1.0, 1.0], 0.9)),
            Box::new(Difference {
                a: sphere([1.5, 1.0, 1.0], 0.9),
                b: sphere([2.0, 1.0, 1.0], 0.3),
            }),
        ),
    ]
}

/// **The soundness oracle.** Whatever an edit does, every chunk whose Subgrid
/// mesh differs afterwards must have been marked, or the caller keeps drawing
/// and colliding with a mesh the field no longer describes. Over-marking costs a
/// re-mesh; under-marking is a stale world.
///
/// Reports how many chunks were marked without changing, which is the price.
///
/// **And it runs the corner rule through the same oracle**, because a zero that
/// could not have been a number proves nothing: if the oracle never caught
/// `mark_edit` missing a chunk, `mark_edit_subgrid` passing it would say
/// nothing.
#[test]
fn every_chunk_whose_subgrid_mesh_changed_is_marked() {
    let l = layout();
    let (mut changed_total, mut marked_total, mut over, mut corner_missed) = (0, 0, 0, 0);
    for (name, before, after) in edits() {
        let changed = chunks_that_changed(&l, &before, &after);
        assert!(
            !changed.is_empty(),
            "{name}: the edit changes no chunk's mesh, so it proves nothing"
        );
        let (marked, _) = marked_by_subgrid(&l, &before, &after, SAMPLES);
        for id in &changed {
            assert!(
                marked.contains(id),
                "{name}: chunk {id:?} changed and was not marked (marked: {marked:?})"
            );
        }
        let wasted = marked.iter().filter(|id| !changed.contains(id)).count();
        let (corner_marked, _) = marked_by_corners(&l, &before, &after);
        corner_missed += changed
            .iter()
            .filter(|id| !corner_marked.contains(id))
            .count();
        std::println!(
            "{name}: changed {}, marked {}, marked without changing {wasted}",
            changed.len(),
            marked.len()
        );
        changed_total += changed.len();
        marked_total += marked.len();
        over += wasted;
    }
    std::println!(
        "total: changed {changed_total}, marked {marked_total}, over-marked {over}; \
         the corner rule missed {corner_missed} of the {changed_total}"
    );
    assert!(
        corner_missed > 0,
        "the oracle never caught the corner rule, so passing it proves nothing"
    );
}

/// Issue #11's witness: every corner of the cell the plate sits in is outside
/// before and after, and the chunk's mesh changes.
#[test]
fn a_plate_thinner_than_a_cell_dirties_the_chunk_that_meshes_it() {
    let l = layout();
    let all = edits();
    for name in [
        "a plate thinner than a cell is added",
        "that plate is removed",
    ] {
        let (_, before, after) = all.iter().find(|(n, _, _)| *n == name).expect("an edit");
        let (marked, report) = marked_by_subgrid(&l, before, after, SAMPLES);
        assert!(
            marked.contains(&ChunkId::new([2, 0, 0])),
            "{name}: {marked:?}"
        );
        assert!(report.output_changed_cells > 0, "{name}");
    }
}

/// The contrast that makes the new rule necessary: a crack between the corners
/// changes no corner value, so the corner rule sees nothing and the mesh changes.
#[test]
fn a_crack_no_corner_sees_is_missed_by_mark_edit_and_found_by_mark_edit_subgrid() {
    let l = layout();
    let all = edits();
    let (_, before, after) = all
        .iter()
        .find(|(n, _, _)| n.starts_with("a crack no corner sees"))
        .expect("an edit");
    assert_eq!(
        chunks_that_changed(&l, before, after),
        vec![ChunkId::new([0, 0, 0])],
        "the crack must really change the mesh"
    );

    let (corner_marked, corner_report) = marked_by_corners(&l, before, after);
    assert_eq!(
        corner_report.value_changed_cells, 0,
        "no corner value moved"
    );
    assert!(corner_marked.is_empty());

    let (subgrid_marked, _) = marked_by_subgrid(&l, before, after, SAMPLES);
    assert_eq!(subgrid_marked, vec![ChunkId::new([0, 0, 0])]);
}

/// Marking is not "everything". An edit that stays in one chunk leaves the others
/// to keep their meshes.
#[test]
fn an_edit_inside_one_chunk_leaves_the_others_alone() {
    let l = layout();
    let before: Field = Box::new(sphere([1.0, 1.0, 1.0], 0.5));
    let after: Field = Box::new(sphere([1.0, 1.0, 1.0], 0.6));
    let (marked, _) = marked_by_subgrid(&l, &before, &after, SAMPLES);
    assert_eq!(marked, vec![ChunkId::new([0, 0, 0])]);
}

/// At one sample per edge the extractor reads the corners and nothing else, and
/// the rule must collapse to `mark_edit`'s, counter for counter.
#[test]
fn one_sample_per_edge_is_the_corner_rule() {
    let l = layout();
    for (name, before, after) in edits() {
        let (corner_marked, corner_report) = marked_by_corners(&l, &before, &after);
        let (marked, report) = marked_by_subgrid(&l, &before, &after, 1);
        assert_eq!(report, corner_report, "{name}: the reports differ");
        assert_eq!(marked, corner_marked, "{name}: the marked chunks differ");
    }
}

/// Counts the evaluations made of it.
struct Counting<'a>(&'a Cell<u64>);

impl Sdf for Counting<'_> {
    type Scalar = f64;
    fn sample(&self, _: [f64; 3]) -> f64 {
        self.0.set(self.0.get() + 1);
        1.0
    }
}

/// The cost is a claim in the docs, so it is counted: every distinct point once,
/// however many cells share it.
#[test]
fn each_probe_point_is_evaluated_once() {
    let l = layout();
    let [cx, cy, cz] = [3u64, 2, 4];
    for samples in [1u32, 2, 4, 5] {
        let s = u64::from(samples) - 1;
        let corners = (cx + 1) * (cy + 1) * (cz + 1);
        let edges = cx * (cy + 1) * (cz + 1)
            + (cx + 1) * cy * (cz + 1)
            + (cx + 1) * (cy + 1) * cz
            + cx * cy * (cz + 1)
            + cx * (cy + 1) * cz
            + (cx + 1) * cy * cz
            + cx * cy * cz;
        let expected = corners + edges * s;

        let (n_before, n_after) = (Cell::new(0), Cell::new(0));
        let mut dirty = DirtySet::new();
        mark_edit_subgrid(
            &l,
            &Counting(&n_before),
            &Counting(&n_after),
            [0, 0, 0],
            [cx as i64 - 1, cy as i64 - 1, cz as i64 - 1],
            samples,
            &mut dirty,
        )
        .expect("a region in bounds");
        assert_eq!(n_before.get(), expected, "samples {samples}: before");
        assert_eq!(n_after.get(), expected, "samples {samples}: after");
    }
}

#[test]
fn an_identical_field_dirties_nothing() {
    let l = layout();
    let field: Field = Box::new(sphere([1.0, 1.0, 1.0], 0.5));
    let (marked, report) = marked_by_subgrid(&l, &field, &field, SAMPLES);
    assert!(marked.is_empty());
    assert_eq!(report.value_changed_cells, 0);
    assert_eq!(report.dirty_chunks, 0);
    assert!(report.region_cells > 0);
}

/// `dirty_chunks` is this edit's count, however long the caller's queue already
/// is: a backlog is not something the edit did.
#[test]
fn dirty_chunks_counts_this_edit_and_not_the_queue() {
    let l = layout();
    let field: Field = Box::new(sphere([1.0, 1.0, 1.0], 0.5));
    let mut dirty = DirtySet::new();
    dirty.insert(ChunkId::new([0, 0, 0]));
    let report = mark_edit_subgrid(&l, &field, &field, ROW.0, ROW.1, SAMPLES, &mut dirty)
        .expect("a region in bounds");
    assert_eq!(report.dirty_chunks, 0);
    assert_eq!(dirty.len(), 1, "the caller's queue is kept");
}

#[test]
fn bad_arguments_are_refused_rather_than_panicked_on() {
    let l = layout();
    let field: Field = Box::new(sphere([1.0, 1.0, 1.0], 0.5));
    let mut dirty = DirtySet::new();

    // The extractor cannot be built with no samples, whatever the region.
    for (min, max) in [(ROW.0, ROW.1), ([5, 5, 5], [4, 5, 5])] {
        let r = mark_edit_subgrid(&l, &field, &field, min, max, 0, &mut dirty);
        assert!(matches!(r, Err(Error::InvalidCellSize { .. })), "{r:?}");
    }

    // An inverted region is nothing to do.
    let r = mark_edit_subgrid(
        &l,
        &field,
        &field,
        [5, 5, 5],
        [4, 5, 5],
        SAMPLES,
        &mut dirty,
    )
    .expect("an inverted region is empty, not an error");
    assert_eq!(r, EditReport::default());

    // An enormous one is refused before anything is allocated.
    let r = mark_edit_subgrid(
        &l,
        &field,
        &field,
        [i64::MIN, i64::MIN, i64::MIN],
        [i64::MAX, i64::MAX, i64::MAX],
        SAMPLES,
        &mut dirty,
    );
    assert!(matches!(r, Err(Error::IndexSpaceExhausted { .. })), "{r:?}");
    // A region the corner rule accepts, but whose probe grid does not fit.
    let r = mark_edit_subgrid(
        &l,
        &field,
        &field,
        [0, 0, 0],
        [1000, 1000, 1000],
        SAMPLES,
        &mut dirty,
    );
    assert!(matches!(r, Err(Error::IndexSpaceExhausted { .. })), "{r:?}");
    assert!(dirty.is_empty());
}