meshoptimizer-rs 0.2.4

Rust port of meshoptimizer 1.3 with byte-identical output. Covers mesh simplification, meshlets, cluster LOD, vertex/index compression analysis, remeshing and optional parallel batch APIs and audited SIMD codecs.
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Triangle strip conversion, following the pinned `stripifier.cpp` order.
use crate::workspace::{checked_bytes, topology};
use crate::{Error, Workspace};
use alloc::vec::Vec;

trait StripOutput {
    fn limit(&self) -> usize;
    fn count(&self) -> usize;
    fn emit(&mut self, values: &[u32]);
}

impl StripOutput for Vec<u32> {
    #[inline(always)]
    fn limit(&self) -> usize {
        self.capacity()
    }
    #[inline(always)]
    fn count(&self) -> usize {
        self.len()
    }
    #[inline(always)]
    fn emit(&mut self, values: &[u32]) {
        debug_assert!(self.len() + values.len() <= self.capacity());
        self.extend_from_slice(values);
    }
}

struct SliceOutput<'a> {
    data: &'a mut [u32],
    count: usize,
}
impl StripOutput for SliceOutput<'_> {
    #[inline(always)]
    fn limit(&self) -> usize {
        self.data.len()
    }
    #[inline(always)]
    fn count(&self) -> usize {
        self.count
    }
    #[inline(always)]
    fn emit(&mut self, values: &[u32]) {
        self.data[self.count..self.count + values.len()].copy_from_slice(values);
        self.count += values.len();
    }
}

// Triangle emission advances whole checked chunks; it needs no growing
// offset/range addition in the inner caller-buffer decode loop.
struct TriangleOutput<'a> {
    chunks: core::slice::IterMut<'a, [u32; 3]>,
    initial_chunks: usize,
    capacity: usize,
}
impl StripOutput for TriangleOutput<'_> {
    #[inline(always)]
    fn limit(&self) -> usize {
        self.capacity
    }
    #[inline(always)]
    fn count(&self) -> usize {
        (self.initial_chunks - self.chunks.len()) * 3
    }
    #[inline(always)]
    fn emit(&mut self, values: &[u32]) {
        self.chunks
            .next()
            .expect("validated triangle bound")
            .copy_from_slice(values);
    }
}

#[inline(always)]
fn reserve_output(output: &mut Vec<u32>, bound: usize) -> Result<(), Error> {
    output
        .try_reserve_exact(bound)
        .map_err(|_| Error::AllocationFailed)
}

/// Worst-case output index count for `meshopt_stripify`.
#[inline(always)]
pub fn stripify_bound(index_count: usize) -> Result<usize, Error> {
    if !index_count.is_multiple_of(3) {
        return Err(Error::InvalidTopology);
    }
    let triangles = index_count / 3;
    if triangles > isize::MAX as usize / 20 {
        return Err(Error::SizeOverflow);
    }
    Ok(triangles * 5)
}

/// Worst-case output index count for `meshopt_unstripify`.
#[inline(always)]
pub fn unstripify_bound(index_count: usize) -> Result<usize, Error> {
    if index_count == 1 || index_count == 2 {
        return Err(Error::InvalidTopology);
    }
    let triangles = index_count.saturating_sub(2);
    if triangles > isize::MAX as usize / 12 {
        return Err(Error::SizeOverflow);
    }
    Ok(triangles * 3)
}

#[inline(always)]
fn first(buffer: &[[u32; 3]; 8], count: usize, valence: &[u8]) -> usize {
    let mut selected = 0;
    let mut minimum = u32::MAX;
    for (i, t) in buffer[..count].iter().enumerate() {
        let (a, b, c) = (
            valence[t[0] as usize] as u32,
            valence[t[1] as usize] as u32,
            valence[t[2] as usize] as u32,
        );
        let value = if a < b && a < c {
            a
        } else if b < c {
            b
        } else {
            c
        };
        if value < minimum {
            selected = i;
            minimum = value;
        }
    }
    selected
}

#[inline(always)]
fn remove(buffer: &mut [[u32; 3]; 8], index: usize, count: usize) {
    for i in index..count - 1 {
        buffer[i] = buffer[i + 1];
    }
}

#[inline(always)]
fn next(buffer: &[[u32; 3]; 8], count: usize, e0: u32, e1: u32) -> Option<(usize, usize)> {
    for (i, &[a, b, c]) in buffer[..count].iter().enumerate() {
        if e0 == a && e1 == b {
            return Some((i, 2));
        }
        if e0 == b && e1 == c {
            return Some((i, 0));
        }
        if e0 == c && e1 == a {
            return Some((i, 1));
        }
    }
    None
}

#[inline(always)]
fn key(value: Option<(usize, usize)>) -> usize {
    value.map_or(usize::MAX, |(i, corner)| i * 4 + corner)
}

/// Convert triangles to an exact upstream strip. A nonzero `restart_index` enables restarts.
#[inline(always)]
pub fn stripify(
    indices: &[u32],
    vertex_count: usize,
    restart_index: u32,
    workspace: &mut Workspace,
) -> Result<Vec<u32>, Error> {
    let bound = stripify_bound(indices.len())?;
    let output_bytes = checked_bytes(bound, 4)?;
    if output_bytes > workspace.limits().max_bytes {
        return Err(Error::LimitExceeded);
    }
    let mut out = Vec::new();
    reserve_output(&mut out, bound)?;
    out.resize(bound, 0);
    let count = stripify_core(
        &mut SliceOutput {
            data: &mut out,
            count: 0,
        },
        indices,
        vertex_count,
        restart_index,
        workspace,
        output_bytes,
    )?;
    out.truncate(count);
    Ok(out)
}

/// Caller-buffer form of `meshopt_stripify`; destination needs the bound capacity.
pub fn stripify_into(
    destination: &mut [u32],
    indices: &[u32],
    vertex_count: usize,
    restart_index: u32,
    workspace: &mut Workspace,
) -> Result<usize, Error> {
    let mut output = SliceOutput {
        data: destination,
        count: 0,
    };
    stripify_core(
        &mut output,
        indices,
        vertex_count,
        restart_index,
        workspace,
        0,
    )
}

fn stripify_core(
    destination: &mut impl StripOutput,
    indices: &[u32],
    vertex_count: usize,
    restart_index: u32,
    workspace: &mut Workspace,
    output_bytes: usize,
) -> Result<usize, Error> {
    let mut work = workspace.begin();
    let result = (|| {
        let bound = stripify_bound(indices.len())?;
        if destination.limit() < bound {
            return Err(Error::BufferTooSmall);
        }
        topology(indices, vertex_count, &mut work)?;
        let retained = workspace.flags.len().min(vertex_count);
        workspace.prepare([0, 0, vertex_count, 0], output_bytes)?;
        let valence = &mut workspace.flags[..vertex_count];
        valence[..retained].fill(0);
        let bulk_valence = work.covers(indices.len())?;
        if bulk_valence {
            work.add(indices.len())?;
        }
        for &index in indices {
            if !bulk_valence {
                work.add(1)?;
            }
            valence[index as usize] = valence[index as usize].wrapping_add(1);
        }

        let mut buffer = [[0u32; 3]; 8];
        let mut count = 0usize;
        let mut offset = 0usize;
        let mut strip = [0u32; 2];
        let mut parity = 0usize;
        let mut continuation: Option<(usize, usize)> = None;
        let triangles = indices.len() / 3;
        let bulk_strip = work.covers(triangles)?;
        if bulk_strip {
            work.add(triangles)?;
        }
        while count > 0 || offset < indices.len() {
            if !bulk_strip {
                work.add(1)?;
            }
            while count < 8 && offset < indices.len() {
                buffer[count].copy_from_slice(&indices[offset..offset + 3]);
                count += 1;
                offset += 3;
            }
            let size = destination.count();
            if let Some((i, corner)) = continuation {
                let [a, b, c] = buffer[i];
                let v = buffer[i][corner];
                remove(&mut buffer, i, count);
                count -= 1;
                for index in [a, b, c] {
                    valence[index as usize] = valence[index as usize].wrapping_sub(1);
                }
                let cont = if parity != 0 {
                    next(&buffer, count, strip[1], v)
                } else {
                    next(&buffer, count, v, strip[1])
                };
                let swap = if cont.is_none() {
                    if parity != 0 {
                        next(&buffer, count, v, strip[0])
                    } else {
                        next(&buffer, count, strip[0], v)
                    }
                } else {
                    None
                };
                if let Some(swap) = swap {
                    destination.emit(&[strip[0], v]);
                    strip[1] = v;
                    continuation = Some(swap);
                } else {
                    destination.emit(&[v]);
                    strip = [strip[1], v];
                    parity ^= 1;
                    continuation = cont;
                }
            } else {
                let i = first(&buffer, count, valence);
                let [mut a, mut b, mut c] = buffer[i];
                remove(&mut buffer, i, count);
                count -= 1;
                for index in [a, b, c] {
                    valence[index as usize] = valence[index as usize].wrapping_sub(1);
                }
                let ea = next(&buffer, count, c, b);
                let eb = next(&buffer, count, a, c);
                let ec = next(&buffer, count, b, a);
                let minimum = key(ea).min(key(eb)).min(key(ec));
                continuation = if key(ea) == minimum {
                    ea
                } else if key(eb) == minimum {
                    (a, b, c) = (b, c, a);
                    eb
                } else if key(ec) == minimum {
                    (a, b, c) = (c, a, b);
                    ec
                } else {
                    None
                };
                if restart_index != 0 {
                    if size > 0 {
                        destination.emit(&[restart_index]);
                    }
                    destination.emit(&[a, b, c]);
                    strip = [b, c];
                    parity = 1;
                } else {
                    if size > 0 {
                        destination.emit(&[strip[1], a]);
                    }
                    let (e0, e1) = if parity != 0 { (c, b) } else { (b, c) };
                    destination.emit(&[a, e0, e1]);
                    strip = [e0, e1];
                    parity ^= 1;
                }
            }
        }
        Ok(destination.count())
    })();
    workspace.finish(&work);
    result
}

/// Convert a strip to triangles, skipping degenerate triangles as upstream does.
#[inline(always)]
pub fn unstripify(
    indices: &[u32],
    restart_index: u32,
    workspace: &mut Workspace,
) -> Result<Vec<u32>, Error> {
    let bound = unstripify_bound(indices.len())?;
    let output_bytes = checked_bytes(bound, 4)?;
    if output_bytes > workspace.limits().max_bytes {
        return Err(Error::LimitExceeded);
    }
    let mut out = Vec::new();
    reserve_output(&mut out, bound)?;
    // Keep allocation metadata outside the emitting decoder, using validated
    // chunks for every size. Match C++'s initialized requested output storage.
    out.resize(bound, 0);
    let count = {
        let chunks = out.as_chunks_mut::<3>().0;
        let mut output = TriangleOutput {
            initial_chunks: chunks.len(),
            capacity: bound,
            chunks: chunks.iter_mut(),
        };
        unstripify_core(&mut output, indices, restart_index, workspace, output_bytes)?
    };
    out.truncate(count);
    Ok(out)
}

/// Caller-buffer form of `meshopt_unstripify`; destination needs the bound capacity.
pub fn unstripify_into(
    destination: &mut [u32],
    indices: &[u32],
    restart_index: u32,
    workspace: &mut Workspace,
) -> Result<usize, Error> {
    let capacity = destination.len();
    let chunks = destination.as_chunks_mut::<3>().0;
    let mut output = TriangleOutput {
        initial_chunks: chunks.len(),
        capacity,
        chunks: chunks.iter_mut(),
    };
    unstripify_core(&mut output, indices, restart_index, workspace, 0)
}

fn unstripify_core(
    destination: &mut impl StripOutput,
    indices: &[u32],
    restart_index: u32,
    workspace: &mut Workspace,
    output_bytes: usize,
) -> Result<usize, Error> {
    let mut work = workspace.begin();
    let result = unstripify_checked(
        destination,
        indices,
        restart_index,
        workspace,
        output_bytes,
        &mut work,
    );
    workspace.finish(&work);
    result
}

#[inline(always)]
fn unstripify_checked(
    destination: &mut impl StripOutput,
    indices: &[u32],
    restart_index: u32,
    workspace: &mut Workspace,
    output_bytes: usize,
    work: &mut crate::workspace::Work,
) -> Result<usize, Error> {
    let bound = unstripify_bound(indices.len())?;
    if destination.limit() < bound {
        return Err(Error::BufferTooSmall);
    }
    workspace.account_codec(output_bytes)?;
    if work.covers(indices.len())? {
        work.add(indices.len())?;
        unstripify_loop::<false>(destination, indices, restart_index, work)
    } else {
        unstripify_loop::<true>(destination, indices, restart_index, work)
    }
}

#[inline(always)]
fn unstripify_loop<const CHARGE_WORK: bool>(
    destination: &mut impl StripOutput,
    indices: &[u32],
    restart_index: u32,
    work: &mut crate::workspace::Work,
) -> Result<usize, Error> {
    if restart_index == 0 {
        return unstripify_plain::<CHARGE_WORK>(destination, indices, work);
    }
    let mut start = 0;
    let mut previous2 = 0;
    let mut previous1 = 0;
    for (i, &c) in indices.iter().enumerate() {
        if CHARGE_WORK {
            work.add(1)?;
        }
        if restart_index != 0 && c == restart_index {
            start = i + 1;
        } else if i - start >= 2 {
            // Degeneracy is invariant under winding; avoid swapping values
            // or making branch outcomes depend on parity for rejected corners.
            if (previous2 != previous1) & (previous2 != c) & (previous1 != c) {
                let (mut a, mut b) = (previous2, previous1);
                if (i - start) & 1 != 0 {
                    core::mem::swap(&mut a, &mut b);
                }
                destination.emit(&[a, b, c]);
            }
        }
        previous2 = previous1;
        previous1 = c;
    }
    Ok(destination.count())
}

// Without a restart marker, each possible triangle is one adjacent window.
// Charge the first two input visits individually on the tight path, retaining
// the exact failure prefix before any output can be emitted.
#[inline(always)]
fn unstripify_plain<const CHARGE_WORK: bool>(
    destination: &mut impl StripOutput,
    indices: &[u32],
    work: &mut crate::workspace::Work,
) -> Result<usize, Error> {
    if CHARGE_WORK {
        for _ in 0..indices.len().min(2) {
            work.add(1)?;
        }
    }
    for (i, triangle) in indices.windows(3).enumerate() {
        if CHARGE_WORK {
            work.add(1)?;
        }
        let (mut a, mut b, c) = (triangle[0], triangle[1], triangle[2]);
        if (a != b) & (a != c) & (b != c) {
            if i & 1 != 0 {
                core::mem::swap(&mut a, &mut b);
            }
            destination.emit(&[a, b, c]);
        }
    }
    Ok(destination.count())
}