meshoptimizer-rs 0.2.3

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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// Port of meshoptimizer 1.3 partition.cpp (MIT, Arseny Kapoulkine).
use crate::processing::{dot, finite, point, sub, Context};
use crate::{Error, Positions, Workspace};
use alloc::vec::Vec;
/// Cluster-to-partition assignments in upstream order.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Partitions {
    /// Partition id for each input cluster.
    pub assignments: Vec<u32>,
    /// Number of partitions.
    pub count: usize,
}
#[derive(Clone, Copy, Default)]
struct Group {
    group: i32,
    next: i32,
    size: u32,
    vertices: u32,
    center: [f32; 3],
    radius: f32,
}
#[derive(Clone, Copy, Default)]
struct Order {
    id: u32,
    order: i32,
}
fn push(heap: &mut [Order], n: &mut usize, item: Order) {
    let mut i = *n;
    heap[i] = item;
    *n += 1;
    while i > 0 && heap[i].order < heap[(i - 1) / 2].order {
        let p = (i - 1) / 2;
        heap.swap(i, p);
        i = p;
    }
}
fn pop(heap: &mut [Order], n: &mut usize) -> Order {
    let top = heap[0];
    *n -= 1;
    heap[0] = heap[*n];
    let mut i = 0;
    while i * 2 + 1 < *n {
        let mut j = i * 2 + 1;
        if j + 1 < *n && heap[j + 1].order < heap[j].order {
            j += 1;
        }
        if heap[j].order >= heap[i].order {
            break;
        }
        heap.swap(i, j);
        i = j;
    }
    top
}
fn score(a: Group, b: Group, ctx: &mut Context<'_>) -> Result<f32, Error> {
    let dp = sub(b.center, a.center);
    let d = ctx.sqrt(finite(dot(dp, dp))?);
    let mr = if d + a.radius < b.radius {
        b.radius
    } else if d + b.radius < a.radius {
        a.radius
    } else {
        (d + b.radius + a.radius) / 2.
    };
    finite(if mr > 0. { a.radius / mr } else { 0. })
}
fn merge(a: &mut Group, b: Group, ctx: &mut Context<'_>) -> Result<(), Error> {
    let dp = sub(b.center, a.center);
    let d = ctx.sqrt(finite(dot(dp, dp))?);
    if d + a.radius < b.radius {
        a.center = b.center;
        a.radius = b.radius;
    } else if d + b.radius > a.radius {
        let k = if d > 0. {
            (d + b.radius - a.radius) / (2. * d)
        } else {
            0.
        };
        for (j, &v) in dp.iter().enumerate() {
            a.center[j] = finite(a.center[j] + v * k)?;
        }
        a.radius = finite((d + b.radius + a.radius) / 2.)?;
    }
    Ok(())
}
fn leaf(
    groups: &mut [Group],
    order: &[u32],
    target: usize,
    max: usize,
    ctx: &mut Context<'_>,
) -> Result<(), Error> {
    for &id in order {
        let id = id as usize;
        if groups[id].size == 0 || groups[id].size as usize >= target {
            continue;
        }
        let mut best = None;
        let mut bestscore = -1.;
        for &other in order {
            ctx.tick(1)?;
            let other = other as usize;
            if id == other
                || groups[other].size == 0
                || (groups[id].size as usize + groups[other].size as usize) > max
            {
                continue;
            }
            let sc = score(groups[id], groups[other], ctx)?;
            if sc > bestscore {
                best = Some(other);
                bestscore = sc;
            }
        }
        if let Some(other) = best {
            let mut tail = other;
            while groups[tail].next >= 0 {
                ctx.tick(1)?;
                tail = groups[tail].next as usize;
            }
            groups[tail].next = id as i32;
            groups[other].size += groups[id].size;
            groups[id].size = 0;
            let g = groups[id];
            merge(&mut groups[other], g, ctx)?;
            groups[id].radius = 0.;
        }
    }
    Ok(())
}
fn spatial(
    groups: &mut [Group],
    order: &mut [u32],
    target: usize,
    max: usize,
    depth: usize,
    ctx: &mut Context<'_>,
) -> Result<(), Error> {
    ctx.tick(order.len())?;
    let total = order
        .iter()
        .map(|&v| groups[v as usize].size as usize)
        .sum::<usize>();
    if total <= max || order.len() <= 8 {
        return leaf(groups, order, target, max, ctx);
    }
    let mut mean = [0.; 3];
    let mut vars = [0.; 3];
    let mut runc = 1.;
    let mut runs = 1.;
    ctx.tick(order.len() * 2)?;
    for &v in order.iter() {
        let p = groups[v as usize].center;
        for k in 0..3 {
            let delta = p[k] - mean[k];
            mean[k] += delta * runs;
            vars[k] += delta * (p[k] - mean[k]);
        }
        runc += 1.;
        runs = 1. / runc;
    }
    let axis = if vars[0] >= vars[1] && vars[0] >= vars[2] {
        0
    } else if vars[1] >= vars[2] {
        1
    } else {
        2
    };
    let mut middle = 0;
    for i in 0..order.len() {
        let v = groups[order[i] as usize].center[axis];
        order.swap(middle, i);
        middle += usize::from(v < mean[axis]);
    }
    if middle <= 4 || order.len() - middle <= 4 || depth >= 40 {
        middle = order.len() / 2;
    }
    let (left, right) = order.split_at_mut(middle);
    spatial(groups, left, target, max, depth + 1, ctx)?;
    spatial(groups, right, target, max, depth + 1, ctx)
}
struct Adj {
    offsets: Vec<u32>,
    clusters: Vec<u32>,
    shared: Vec<u32>,
}
fn adjacent(
    indices: &[u32],
    offsets: &[u32],
    n: usize,
    vertices: usize,
    ctx: &mut Context<'_>,
) -> Result<Adj, Error> {
    let mut refs = ctx.alloc::<u32>(vertices + 1)?;
    ctx.tick(indices.len())?;
    for &v in indices {
        refs[v as usize] += 1;
    }
    let mut total = 0usize;
    for i in 0..n {
        let mut count = 0usize;
        // Batched visit charges (ctx.scan) keep each per-visit exhaustion point.
        ctx.scan(
            indices[offsets[i] as usize..offsets[i + 1] as usize].iter(),
            |_, &v| {
                count = count
                    .checked_add(refs[v as usize] as usize - 1)
                    .ok_or(Error::SizeOverflow)?;
                Ok(())
            },
        )?;
        total = total
            .checked_add(count.min(n - 1))
            .ok_or(Error::SizeOverflow)?;
    }
    if total > u32::MAX as usize {
        return Err(Error::SizeOverflow);
    }
    let mut adj = Adj {
        offsets: ctx.alloc(n + 1)?,
        clusters: ctx.alloc(total)?,
        shared: ctx.alloc(total)?,
    };
    let mut nr = 0;
    for r in &mut refs[..vertices] {
        let n = *r;
        *r = nr;
        nr += n;
    }
    let mut data = ctx.alloc::<u32>(nr as usize)?;
    for i in 0..n {
        ctx.scan(
            indices[offsets[i] as usize..offsets[i + 1] as usize].iter(),
            |_, &v| {
                let r = &mut refs[v as usize];
                data[*r as usize] = i as u32;
                *r += 1;
                Ok(())
            },
        )?;
    }
    refs.copy_within(0..vertices, 1);
    refs[0] = 0;
    let mut slots = ctx.alloc::<u32>(n)?;
    slots.fill(u32::MAX);
    for i in 0..n {
        let start = adj.offsets[i] as usize;
        let mut count = 0;
        for &v in &indices[offsets[i] as usize..offsets[i + 1] as usize] {
            let others = &data[refs[v as usize] as usize..refs[v as usize + 1] as usize];
            // These visits cannot fail: charge a covered list once, otherwise
            // keep the per-visit exhaustion point.
            let batched = ctx.work.covers(others.len())?;
            for &c in others {
                if !batched {
                    ctx.tick(1)?;
                }
                if c as usize == i {
                    continue;
                }
                let slot = slots[c as usize] as usize;
                if slot < count && adj.clusters[start + slot] == c {
                    adj.shared[start + slot] += 1;
                } else {
                    slots[c as usize] = count as u32;
                    adj.clusters[start + count] = c;
                    adj.shared[start + count] = 1;
                    count += 1;
                }
            }
            if batched {
                ctx.tick(others.len())?;
            }
        }
        adj.offsets[i + 1] = (start + count) as u32;
    }
    ctx.release(&refs)?;
    ctx.release(&data)?;
    ctx.release(&slots)?;
    Ok(adj)
}
fn pick(
    groups: &[Group],
    id: usize,
    adj: &Adj,
    max: usize,
    bounds: bool,
    acc: &mut [u32],
    ctx: &mut Context<'_>,
) -> Result<Option<(usize, u32)>, Error> {
    let r = 1. / ctx.sqrt(groups[id].vertices as i32 as f32);
    let mut ci = id as i32;
    while ci >= 0 {
        let c = ci as usize;
        ctx.tick((adj.offsets[c + 1] - adj.offsets[c]) as usize)?;
        for i in adj.offsets[c] as usize..adj.offsets[c + 1] as usize {
            let other = groups[adj.clusters[i] as usize].group;
            if other >= 0 {
                acc[other as usize] = acc[other as usize]
                    .checked_add(adj.shared[i])
                    .ok_or(Error::SizeOverflow)?;
            }
        }
        ci = groups[c].next;
    }
    let mut best = None;
    let mut bestscore = 0.;
    ci = id as i32;
    while ci >= 0 {
        let c = ci as usize;
        ctx.tick((adj.offsets[c + 1] - adj.offsets[c]) as usize)?;
        for i in adj.offsets[c] as usize..adj.offsets[c + 1] as usize {
            let other = groups[adj.clusters[i] as usize].group;
            if other < 0 || acc[other as usize] == 0 {
                continue;
            }
            let other = other as usize;
            let shared = acc[other];
            acc[other] = 0;
            if groups[id].size as usize + groups[other].size as usize > max {
                continue;
            }
            let mut sc =
                shared as i32 as f32 * (r + 1. / ctx.sqrt(groups[other].vertices as i32 as f32));
            if bounds {
                sc *= 1. + 0.4 * score(groups[id], groups[other], ctx)?;
            }
            finite(sc)?;
            if sc > bestscore {
                best = Some((other, shared));
                bestscore = sc;
            }
        }
        ci = groups[c].next;
    }
    Ok(best)
}
#[inline(always)]
fn checked<const CHECK: bool>(x: f32) -> Result<f32, Error> {
    if CHECK {
        finite(x)
    } else {
        Ok(x)
    }
}
// Cluster centroid and squared radius, in upstream operation order.
#[inline(always)]
fn group_sphere<const CHECK: bool>(
    p: Positions<'_>,
    verts: &[u32],
) -> Result<([f32; 3], f32), Error> {
    let mut center = [0f32; 3];
    for &v in verts {
        let pp = point(p, v);
        for (k, &x) in pp.iter().enumerate() {
            center[k] = checked::<CHECK>(center[k] + x)?;
        }
    }
    for x in &mut center {
        *x /= verts.len() as f32;
    }
    let mut r = 0.;
    for &v in verts {
        let dp = sub(point(p, v), center);
        let d = checked::<CHECK>(dot(dp, dp))?;
        if r < d {
            r = d;
        }
    }
    Ok((center, r))
}
fn partition(
    out: &mut [u32],
    indices: &[u32],
    counts: &[u32],
    vertices: usize,
    p: Option<Positions<'_>>,
    target: usize,
    ctx: &mut Context<'_>,
) -> Result<usize, Error> {
    if target == 0 {
        return Err(Error::InvalidParameter);
    }
    let max = target.checked_add(target / 3).ok_or(Error::SizeOverflow)?;
    let n = counts.len();
    ctx.large_memo();
    if n > i32::MAX as usize || vertices > u32::MAX as usize || indices.len() > u32::MAX as usize {
        return Err(Error::SizeOverflow);
    }
    ctx.tick(counts.len() + indices.len())?;
    let mut sum = 0usize;
    for &n in counts {
        if n == 0 {
            return Err(Error::InvalidParameter);
        }
        sum = sum.checked_add(n as usize).ok_or(Error::SizeOverflow)?;
    }
    if sum != indices.len() {
        return Err(Error::InvalidLayout);
    }
    if indices.iter().any(|&v| v as usize >= vertices) {
        return Err(Error::IndexOutOfBounds);
    }
    if let Some(p) = p {
        if p.len() != vertices {
            return Err(Error::InvalidLayout);
        }
        ctx.positions(p)?;
    }
    let mut used = ctx.alloc::<u8>(vertices)?;
    let mut filtered = ctx.alloc::<u32>(indices.len())?;
    let mut offsets = ctx.alloc::<u32>(n + 1)?;
    let (mut start, mut write) = (0, 0);
    for (i, &count) in counts.iter().enumerate() {
        offsets[i] = write as u32;
        ctx.tick(count as usize)?;
        for &v in &indices[start..start + count as usize] {
            if used[v as usize] == 0 {
                filtered[write] = v;
                write += 1;
                used[v as usize] = 1;
            }
        }
        for &v in &filtered[offsets[i] as usize..write] {
            used[v as usize] = 0;
        }
        start += count as usize;
    }
    offsets[n] = write as u32;
    filtered.truncate(write);
    let adj = adjacent(&filtered, &offsets, n, vertices, ctx)?;
    let mut groups = ctx.alloc::<Group>(n)?;
    let mut heap = ctx.alloc::<Order>(n)?;
    let mut acc = ctx.alloc(n)?;
    let mut pending = 0;
    for i in 0..n {
        let verts = &filtered[offsets[i] as usize..offsets[i + 1] as usize];
        let mut g = Group {
            group: i as i32,
            next: -1,
            size: 1,
            vertices: verts.len() as u32,
            ..Group::default()
        };
        if let Some(p) = p {
            ctx.tick(verts.len() * 2)?;
            // Moderate coordinates (|x| <= 1e8) bound every partial sum by
            // 1e8 * 2^32 and every squared offset by 3 * (2e8)^2, so these
            // finiteness checks cannot fail there (as in the meshlet modules).
            let (center, r) = if ctx.moderate {
                group_sphere::<false>(p, verts)?
            } else {
                group_sphere::<true>(p, verts)?
            };
            g.center = center;
            g.radius = ctx.sqrt(r);
        }
        groups[i] = g;
        push(
            &mut heap,
            &mut pending,
            Order {
                id: i as u32,
                order: g.vertices as i32,
            },
        );
    }
    while pending != 0 {
        ctx.tick(1)?;
        let mut top = pop(&mut heap, &mut pending);
        let id = top.id as usize;
        if groups[id].size == 0 {
            continue;
        }
        let mut ci = id as i32;
        while ci >= 0 {
            ctx.tick(1)?;
            groups[ci as usize].group = -1;
            ci = groups[ci as usize].next;
        }
        if groups[id].size as usize >= target {
            continue;
        }
        let Some((best, shared)) = pick(&groups, id, &adj, max, p.is_some(), &mut acc, ctx)? else {
            continue;
        };
        let mut tail = id;
        while groups[tail].next >= 0 {
            ctx.tick(1)?;
            tail = groups[tail].next as usize;
        }
        groups[tail].next = best as i32;
        groups[id].size += groups[best].size;
        let vertices = groups[id]
            .vertices
            .checked_add(groups[best].vertices)
            .ok_or(Error::SizeOverflow)?;
        groups[id].vertices = if vertices > shared {
            vertices - shared
        } else {
            1
        };
        groups[best].size = 0;
        groups[best].vertices = 0;
        if p.is_some() {
            let b = groups[best];
            merge(&mut groups[id], b, ctx)?;
            groups[best].radius = 0.;
        }
        ci = id as i32;
        while ci >= 0 {
            ctx.tick(1)?;
            groups[ci as usize].group = id as i32;
            ci = groups[ci as usize].next;
        }
        top.order = groups[id].vertices as i32;
        push(&mut heap, &mut pending, top);
    }
    if p.is_some() {
        let mut order = ctx.alloc::<u32>(n)?;
        let mut count = 0;
        for (i, g) in groups.iter().enumerate() {
            if g.size != 0 {
                order[count] = i as u32;
                count += 1;
            }
        }
        spatial(&mut groups, &mut order[..count], target, max, 0, ctx)?;
    }
    let mut next = 0;
    for i in 0..n {
        if groups[i].size == 0 {
            continue;
        }
        let mut j = i as i32;
        while j >= 0 {
            ctx.tick(1)?;
            out[j as usize] = next as u32;
            j = groups[j as usize].next;
        }
        next += 1;
    }
    Ok(next)
}
/// `meshopt_partitionClusters`. Clusters may contain arbitrary index lists; empty clusters are invalid.
/// Positions are optional; without them only connected clusters merge.
pub fn partition_clusters(
    indices: &[u32],
    counts: &[u32],
    vertex_count: usize,
    p: Option<Positions<'_>>,
    target: usize,
    workspace: &mut Workspace,
) -> Result<Partitions, Error> {
    let mut ctx = Context::new(workspace)?;
    let mut assignments = ctx.alloc(counts.len())?;
    let count = partition(
        &mut assignments,
        indices,
        counts,
        vertex_count,
        p,
        target,
        &mut ctx,
    )?;
    Ok(Partitions { assignments, count })
}
/// Caller-buffer cluster partitioning; a late failure may modify the used prefix.
pub fn partition_clusters_into(
    out: &mut [u32],
    indices: &[u32],
    counts: &[u32],
    vertex_count: usize,
    p: Option<Positions<'_>>,
    target: usize,
    workspace: &mut Workspace,
) -> Result<usize, Error> {
    let mut ctx = Context::new(workspace)?;
    if out.len() < counts.len() {
        return Err(Error::BufferTooSmall);
    }
    partition(
        &mut out[..counts.len()],
        indices,
        counts,
        vertex_count,
        p,
        target,
        &mut ctx,
    )
}