isomesh 0.0.5

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
422
423
//! Where triangles go.

use alloc::vec::Vec;

use crate::Real;

/// Receives the triangles an extraction algorithm produces.
///
/// Implemented by [`MeshBuffer`], by an engine wrapper writing straight into a
/// renderer's attribute arrays, and by anything a CAD consumer wants. The point
/// is that an algorithm never allocates the mesh it produces — see
/// [`MeshBuffer::reset`].
///
/// # Winding
///
/// `a, b, c` in [`triangle`](MeshSink::triangle) are **counter-clockwise when
/// viewed from outside the solid**, in a right-handed coordinate system.
/// Equivalently `(b − a) × (c − a)` points the same way as the surface normal,
/// which points away from the solid — the direction of increasing
/// [`Sdf::sample`](crate::Sdf::sample), since negative is inside.
///
/// # Index contract
///
/// [`vertex`](MeshSink::vertex) returns the index by which that vertex may be
/// referenced from [`triangle`](MeshSink::triangle). **Callers must not assume
/// the returned index equals a running counter.** A sink is permitted to weld:
/// to recognise a vertex it has already seen and return that earlier index
/// instead of appending. A welding sink must document its epsilon and its
/// tie-breaking rule, because weld ordering is the classic determinism leak.
///
/// Indices are valid only on the sink that returned them, and only until that
/// sink is reset. Forward references are not permitted: every index passed to
/// `triangle` must have been returned by an earlier `vertex` call on the same
/// sink.
///
/// [`MeshBuffer`] never welds. It always appends, and its returned index is
/// always `vertex_count() - 1`.
pub trait MeshSink {
    /// The scalar positions and normals are written in.
    ///
    /// `f32` for anything destined for a GPU; `f64` for CAD, where the whole
    /// reason the crate is generic is that narrowing the output would throw away
    /// the precision the solve was done in.
    type Scalar: Real;

    /// Append a vertex and return its index. See the trait's index contract.
    fn vertex(&mut self, position: [Self::Scalar; 3], normal: [Self::Scalar; 3]) -> u32;

    /// Append a triangle. See the trait's winding convention.
    fn triangle(&mut self, a: u32, b: u32, c: u32);

    /// A capacity hint.
    ///
    /// Implementors must never *shrink* in response, and must remain correct if
    /// this is never called.
    fn reserve(&mut self, _vertices: usize, _triangles: usize) {}
}

/// The default sink: three parallel `Vec`s, reusable across chunks.
///
/// Reuse is the entire point. A brush stroke re-meshes thousands of chunks and
/// allocation dominates, so the intended lifecycle is one buffer that is
/// [`reset`](MeshBuffer::reset) between chunks rather than one buffer per chunk.
///
/// # Invariants
///
/// The fields are public so a consumer can write straight into a GPU buffer or a
/// mesh attribute array with no intermediate copy. That also means a caller
/// *can* break the invariants: `positions.len() == normals.len()`,
/// `indices.len() % 3 == 0`, and no index `>= positions.len()`. The validity
/// harness checks all three.
///
/// # Example
///
/// ```
/// use isomesh::{MeshBuffer, MeshSink};
///
/// let mut out = MeshBuffer::<f32>::new();
/// for _chunk in 0..2 {
///     out.reset(); // keeps every allocation made so far
///     let a = out.vertex([0.0, 0.0, 0.0], [0.0, 1.0, 0.0]);
///     let b = out.vertex([1.0, 0.0, 0.0], [0.0, 1.0, 0.0]);
///     let c = out.vertex([0.0, 0.0, 1.0], [0.0, 1.0, 0.0]);
///     out.triangle(a, b, c);
/// }
/// assert_eq!(out.triangle_count(), 1);
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct MeshBuffer<R: Real = f32> {
    /// One entry per vertex.
    pub positions: Vec<[R; 3]>,
    /// One entry per vertex, parallel to `positions`. Unit length.
    pub normals: Vec<[R; 3]>,
    /// Flat triples: `[a0, b0, c0, a1, b1, c1, …]`.
    pub indices: Vec<u32>,
}

impl<R: Real> MeshBuffer<R> {
    /// An empty buffer that has allocated nothing.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            positions: Vec::new(),
            normals: Vec::new(),
            indices: Vec::new(),
        }
    }

    /// An empty buffer with room for `vertices` vertices and `triangles`
    /// triangles.
    #[must_use]
    pub fn with_capacity(vertices: usize, triangles: usize) -> Self {
        Self {
            positions: Vec::with_capacity(vertices),
            normals: Vec::with_capacity(vertices),
            indices: Vec::with_capacity(triangles * 3),
        }
    }

    /// Truncate to zero length **without releasing capacity**.
    ///
    /// Call this between chunks. Afterwards [`vertex_count`](Self::vertex_count)
    /// is zero and every allocation this buffer has ever made is still held: it
    /// does not reallocate, and it does not touch the heap at all.
    pub fn reset(&mut self) {
        self.positions.clear();
        self.normals.clear();
        self.indices.clear();
    }

    /// Number of vertices.
    #[must_use]
    pub fn vertex_count(&self) -> usize {
        self.positions.len()
    }

    /// Number of triangles.
    #[must_use]
    pub fn triangle_count(&self) -> usize {
        self.indices.len() / 3
    }

    /// `true` if no triangles have been written.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.indices.is_empty()
    }

    /// Release unused capacity.
    ///
    /// **The only method on this type that frees memory.** Everything else is
    /// deliberately non-shrinking, so this is the explicit opt-out rather than
    /// something that happens on its own.
    pub fn shrink_to_fit(&mut self) {
        self.positions.shrink_to_fit();
        self.normals.shrink_to_fit();
        self.indices.shrink_to_fit();
    }

    /// Append another mesh, shifting its indices to follow this one's vertices.
    ///
    /// The reason this exists is [`crate::weld`]: a chunk seam only becomes
    /// weldable once both chunks' vertices are in one buffer, and the index
    /// shift is exactly the step a caller would get wrong. Nothing is welded
    /// here — the vertices are concatenated as they are, duplicates and all.
    ///
    /// # Errors
    ///
    /// [`Error::IndexSpaceExhausted`](crate::Error::IndexSpaceExhausted) if
    /// the combined vertex count would leave `other`'s indices unaddressable
    /// in the `u32` index space. Checked here rather than debug-asserted as
    /// [`MeshSink::vertex`] does, because `append` sits outside the
    /// extractors' up-front bound: it is the caller's own assembly step,
    /// repeatable without limit, and the failure it prevents is a silent
    /// index wrap that aliases early vertices.
    pub fn append(&mut self, other: &Self) -> crate::Result<()> {
        let total = self.positions.len() as u64 + other.positions.len() as u64;
        if total > u64::from(u32::MAX) {
            return Err(crate::Error::IndexSpaceExhausted { needed: total });
        }
        let base = self.positions.len() as u32;
        self.positions.extend_from_slice(&other.positions);
        self.normals.extend_from_slice(&other.normals);
        self.indices.extend(other.indices.iter().map(|&i| i + base));
        Ok(())
    }
}

// Hand-written rather than derived: `derive(Default)` would add a spurious
// `R: Default` bound to the impl.
impl<R: Real> Default for MeshBuffer<R> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl<R: Real> MeshSink for MeshBuffer<R> {
    type Scalar = R;

    #[inline]
    fn vertex(&mut self, position: [R; 3], normal: [R; 3]) -> u32 {
        let index = self.positions.len();
        // A `debug_assert!` rather than a check on the hot path: the extractors
        // bound their vertex count against `u32` — up front where an a-priori
        // bound exists, per emission in subgrid, where none does — and return
        // `Error::IndexSpaceExhausted` if it cannot fit. This catches a sink
        // driven directly past the limit, in the builds where that is worth
        // paying for.
        debug_assert!(
            index < u32::MAX as usize,
            "MeshBuffer exceeded the u32 index space at {index} vertices"
        );
        self.positions.push(position);
        self.normals.push(normal);
        index as u32
    }

    #[inline]
    fn triangle(&mut self, a: u32, b: u32, c: u32) {
        self.indices.push(a);
        self.indices.push(b);
        self.indices.push(c);
    }

    #[inline]
    fn reserve(&mut self, vertices: usize, triangles: usize) {
        // `Vec::reserve` never shrinks, so this inherits that property.
        self.positions.reserve(vertices);
        self.normals.reserve(vertices);
        self.indices.reserve(triangles * 3);
    }
}

/// Forwards to the referent, so a sink passed by mutable reference still
/// satisfies the generic `M: MeshSink` bound extraction functions use.
///
/// Note that this forwards [`reserve`](MeshSink::reserve) as well. It must:
/// omitting it would silently substitute the no-op default, so every reservation
/// an algorithm makes would be discarded whenever the sink arrived by reference.
impl<T: MeshSink + ?Sized> MeshSink for &mut T {
    type Scalar = T::Scalar;

    #[inline]
    fn vertex(&mut self, position: [Self::Scalar; 3], normal: [Self::Scalar; 3]) -> u32 {
        T::vertex(self, position, normal)
    }

    #[inline]
    fn triangle(&mut self, a: u32, b: u32, c: u32) {
        T::triangle(self, a, b, c);
    }

    #[inline]
    fn reserve(&mut self, vertices: usize, triangles: usize) {
        T::reserve(self, vertices, triangles);
    }
}

#[cfg(test)]
mod tests {
    // Position and index round-trips are asserted exactly: a sink that stores
    // a vertex must give back that vertex, bit for bit.
    #![allow(clippy::float_cmp)]

    use super::*;

    fn fill(out: &mut MeshBuffer<f32>, vertices: u32) {
        for i in 0..vertices {
            let f = i as f32;
            out.vertex([f, 0.0, 0.0], [0.0, 1.0, 0.0]);
        }
        for i in 0..vertices / 3 {
            out.triangle(3 * i, 3 * i + 1, 3 * i + 2);
        }
    }

    /// I-002's stated acceptance criterion.
    ///
    /// The pointer check is what actually proves "resizes without reallocating":
    /// capacity alone would survive a same-size reallocation to a different
    /// address, which is exactly the thing the reuse contract forbids.
    #[test]
    fn reset_preserves_capacity() {
        let mut out = MeshBuffer::<f32>::new();
        fill(&mut out, 96);

        let caps = (
            out.positions.capacity(),
            out.normals.capacity(),
            out.indices.capacity(),
        );
        let ptrs = (
            out.positions.as_ptr(),
            out.normals.as_ptr(),
            out.indices.as_ptr(),
        );
        // Guard against passing vacuously on a buffer that never allocated.
        assert!(caps.0 > 0 && caps.1 > 0 && caps.2 > 0);

        out.reset();

        assert_eq!(out.vertex_count(), 0);
        assert_eq!(out.triangle_count(), 0);
        assert!(out.is_empty());
        assert_eq!(
            (
                out.positions.capacity(),
                out.normals.capacity(),
                out.indices.capacity()
            ),
            caps
        );
        assert_eq!(
            (
                out.positions.as_ptr(),
                out.normals.as_ptr(),
                out.indices.as_ptr()
            ),
            ptrs
        );
    }

    #[test]
    fn reset_is_idempotent() {
        let mut out = MeshBuffer::<f64>::new();
        for i in 0..12 {
            out.vertex([f64::from(i), 0.0, 0.0], [0.0, 1.0, 0.0]);
        }
        out.reset();
        let cap = out.positions.capacity();
        out.reset();
        assert_eq!(out.positions.capacity(), cap);
        assert_eq!(out.vertex_count(), 0);
    }

    #[test]
    fn reserve_never_shrinks() {
        let mut out = MeshBuffer::<f32>::new();
        out.reserve(1000, 1000);
        let caps = (out.positions.capacity(), out.indices.capacity());
        assert!(caps.0 >= 1000 && caps.1 >= 3000);
        out.reserve(1, 1);
        assert_eq!((out.positions.capacity(), out.indices.capacity()), caps);
    }

    /// Pins the documented non-welding behaviour, which the index contract lets
    /// other sinks vary.
    #[test]
    fn mesh_buffer_never_welds() {
        let mut out = MeshBuffer::<f32>::new();
        let p = [1.0, 2.0, 3.0];
        let n = [0.0, 1.0, 0.0];
        assert_eq!(out.vertex(p, n), 0);
        assert_eq!(out.vertex(p, n), 1);
        assert_eq!(out.vertex_count(), 2);
    }

    #[test]
    fn mesh_sink_is_dyn_compatible() {
        const _: Option<&mut dyn MeshSink<Scalar = f32>> = None;
        let mut out = MeshBuffer::<f32>::new();
        let dynamic: &mut dyn MeshSink<Scalar = f32> = &mut out;
        dynamic.reserve(4, 1);
        let a = dynamic.vertex([0.0; 3], [0.0, 1.0, 0.0]);
        assert_eq!(a, 0);
    }

    /// The second forwarding trap. If `impl MeshSink for &mut T` omitted
    /// `reserve`, this would silently reach the no-op default.
    #[test]
    fn mut_ref_impl_forwards_reserve() {
        struct Counting {
            inner: MeshBuffer<f32>,
            reserves: usize,
        }
        impl MeshSink for Counting {
            type Scalar = f32;
            fn vertex(&mut self, p: [f32; 3], n: [f32; 3]) -> u32 {
                self.inner.vertex(p, n)
            }
            fn triangle(&mut self, a: u32, b: u32, c: u32) {
                self.inner.triangle(a, b, c);
            }
            fn reserve(&mut self, v: usize, t: usize) {
                self.reserves += 1;
                self.inner.reserve(v, t);
            }
        }

        fn through_generic<M: MeshSink>(mut sink: M) {
            sink.reserve(64, 32);
        }

        let mut sink = Counting {
            inner: MeshBuffer::new(),
            reserves: 0,
        };
        through_generic(&mut sink);
        assert_eq!(sink.reserves, 1);
        assert!(sink.inner.positions.capacity() >= 64);
    }

    #[test]
    fn indices_are_flat_triples() {
        let mut out = MeshBuffer::<f32>::new();
        out.triangle(0, 1, 2);
        assert_eq!(out.indices, [0, 1, 2]);
        assert_eq!(out.triangle_count(), 1);
    }

    #[test]
    fn shrink_to_fit_is_the_only_release() {
        let mut out = MeshBuffer::<f32>::new();
        fill(&mut out, 96);
        out.reset();
        assert!(out.positions.capacity() > 0);
        out.shrink_to_fit();
        assert_eq!(out.positions.capacity(), 0);
        assert_eq!(out.normals.capacity(), 0);
        assert_eq!(out.indices.capacity(), 0);
    }
}