meshoptimizer-rs 0.2.20

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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
//! Sampled LOD surface quality, an extension not in C++ meshoptimizer.
use crate::{
    budget::Budget, workspace::Work, Error, LodChain, LodChainGeneric, Positions, Workspace,
};
use alloc::vec::Vec;

type V = [f64; 3];

/// A borrowed triangle surface. Only referenced vertices are measured/validated.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct LodQualityMeshGeneric<'a, I: crate::Index = u32> {
    /// Triangle-list indices; at least one triangle is required.
    pub indices: &'a [I],
    /// Positions in the same world units as the reported distances.
    pub positions: Positions<'a>,
    /// Optional per-vertex normals, normalized before interpolation.
    pub normals: Option<&'a [[f32; 3]]>,
}
impl<'a, I: crate::Index> LodQualityMeshGeneric<'a, I> {
    /// Borrow a surface without normals. Set `normals` to compare shading normals.
    pub fn new_generic(indices: &'a [I], positions: Positions<'a>) -> Self {
        Self {
            indices,
            positions,
            normals: None,
        }
    }
}

/// Deterministic sampling settings (not in C++ meshoptimizer).
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct LodQualityOptions {
    /// Interior samples per square world unit, default 1. Must be finite and
    /// nonnegative. Each triangle gets `ceil(area * density)` samples, plus its
    /// three corners (shared corners are repeated). Zero disables interiors.
    /// Degenerate triangles have corners only. More than `u32::MAX` interior
    /// samples on one triangle returns `SizeOverflow`; work limits apply first
    /// to every visit that is actually attempted.
    pub samples_per_unit_area: f64,
}
impl Default for LodQualityOptions {
    fn default() -> Self {
        Self {
            samples_per_unit_area: 1.0,
        }
    }
}

/// Sampled distance statistics in world units.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct LodDistance {
    /// Largest sampled nearest-surface distance, a lower estimate of Hausdorff distance.
    pub max: f64,
    /// Root mean square over samples, with equal weight per sample.
    pub rms: f64,
    /// Number of samples, including repeated triangle corners.
    pub samples: u64,
}
/// Shading-normal angular deviation in radians.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[non_exhaustive]
pub struct LodNormalDeviation {
    /// Largest angle, in `[0, pi]`.
    pub max: f64,
    /// Arithmetic mean angle over samples in both directions.
    pub mean: f64,
}
/// Quality of a level relative to the source; not in C++ meshoptimizer.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct LodQuality {
    /// One-sided source-to-level measurements.
    pub source_to_level: LodDistance,
    /// One-sided level-to-source measurements.
    pub level_to_source: LodDistance,
    /// Maximum and pooled RMS across both directions.
    pub symmetric: LodDistance,
    /// Pooled normal angles, present when both surfaces supply normals.
    pub normals: Option<LodNormalDeviation>,
}

/// Pinhole camera used to turn world-space deviation into switch distance.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct LodCamera {
    /// Vertical field of view in radians, strictly between zero and pi.
    /// All positive finite values below pi are valid, including subnormals.
    pub vertical_fov: f64,
    /// Positive viewport height in pixels.
    pub viewport_height: f64,
    /// Positive allowed projected deviation in pixels.
    pub pixel_tolerance: f64,
}
impl LodCamera {
    /// Construct and validate a camera.
    /// # Errors
    /// `InvalidParameter` for non-finite or out-of-domain parameters.
    pub fn new(
        vertical_fov: f64,
        viewport_height: f64,
        pixel_tolerance: f64,
    ) -> Result<Self, Error> {
        let camera = Self {
            vertical_fov,
            viewport_height,
            pixel_tolerance,
        };
        camera.switch_distance(0.0)?;
        Ok(camera)
    }
    /// Minimum camera-space depth in the pinhole model:
    /// `deviation * viewport_height / (2 * tan(vertical_fov/2) * pixel_tolerance)`.
    /// This is a depth threshold for a transverse displacement, not a raster
    /// error guarantee. Use nearest mesh depth (or add a bounding radius when
    /// measuring camera-to-center distance). Sampled error can miss extrema.
    /// # Errors
    /// `InvalidParameter` for invalid camera or negative/non-finite deviation;
    /// `NumericalFailure` if the result overflows or a positive threshold
    /// underflows to zero. A zero deviation always returns zero.
    pub fn switch_distance(&self, deviation: f64) -> Result<f64, Error> {
        if !self.vertical_fov.is_finite()
            || self.vertical_fov <= 0.0
            || self.vertical_fov >= core::f64::consts::PI
            || !self.viewport_height.is_finite()
            || self.viewport_height <= 0.0
            || !self.pixel_tolerance.is_finite()
            || self.pixel_tolerance <= 0.0
            || !deviation.is_finite()
            || deviation < 0.0
        {
            return Err(Error::InvalidParameter);
        }
        if deviation == 0.0 {
            return Ok(0.0);
        }
        // Logs avoid overflow/underflow in intermediate products of valid inputs.
        // For tiny angles, 2*tan(fov/2) rounds to fov; avoid halving a
        // subnormal before taking its logarithm. The relative approximation
        // error below this cutoff is smaller than f64 precision.
        let log_scale = if self.vertical_fov < 1e-8 {
            libm::log(self.vertical_fov)
        } else {
            libm::log(2.0) + libm::log(libm::tan(self.vertical_fov / 2.0))
        };
        let log = libm::log(deviation) + libm::log(self.viewport_height)
            - log_scale
            - libm::log(self.pixel_tolerance);
        let distance = libm::exp(log);
        if !distance.is_finite() || distance == 0.0 {
            return Err(Error::NumericalFailure);
        }
        Ok(distance)
    }
}
/// One chain level's quality and optional camera threshold.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct LodQualityLevel {
    /// Measured directly against the source, never against the previous level.
    pub quality: LodQuality,
    /// Minimum depth for this level's sampled symmetric maximum.
    pub switch_distance: Option<f64>,
}

/// Measure a source and a simplified surface, with a balanced triangle BVH.
/// This extension is not in C++ meshoptimizer and does not change simplification.
///
/// Each triangle contributes its corners in index order, then area-scaled
/// interiors. For interior ordinal `j` of `n`, let `r=sqrt((j+0.5)/n)` and
/// `t=(reverse_bits(j)+0.5)/2^32`; barycentric weights are `(1-r, r*(1-t), r*t)`.
/// Samples are streamed without allocating a sample array. Degenerate triangles
/// use segment/point distances. Normals are normalized per vertex, interpolated
/// at the sample and closest point, then normalized again. Nearest-point ties
/// choose the lowest original target triangle ordinal. Both or neither surface
/// must supply normals. Opposite normals give pi (no absolute dot product).
///
/// Maxima are sampled lower estimates, not certified continuous Hausdorff
/// bounds. RMS/mean weight repeated corners and interiors equally, not area
/// integrals. Higher density improves coverage but does not certify accuracy.
/// Deterministic for identical inputs on the same build/target.
///
/// Work counts validation corners/triangles, BVH bounding/partition visits,
/// samples and visited BVH nodes. Bounds and distances use `f64` arithmetic.
/// Typical query cost is samples × log(triangles); overlapping/adversarial
/// surfaces can visit more nodes, always bounded by `Limits::max_work`.
/// Heap storage is fallible, charged before allocation, and released on return;
/// existing workspace scratch is accounted for and may be evicted under pressure.
/// # Errors
/// `InvalidTopology`, `IndexOutOfBounds`, `InvalidParameter` (empty surface,
/// density, normal length or zero normals), `NumericalFailure` (non-finite
/// positions/normals or zero interpolated normal), or resource/size errors.
pub fn measure_lod_quality_generic<I: crate::Index>(
    source: LodQualityMeshGeneric<'_, I>,
    level: LodQualityMeshGeneric<'_, I>,
    options: &LodQualityOptions,
    workspace: &mut Workspace,
) -> Result<LodQuality, Error> {
    let mut work = workspace.begin();
    let result = measure(
        source,
        level,
        options,
        &mut Budget::new(workspace),
        &mut work,
    );
    workspace.finish(&work);
    result
}

/// Measure every `build_lod_chain` level directly against `source` using its
/// unchanged vertex/normal buffers. Limits cover the entire call, including
/// the returned metadata. An empty chain returns an empty vector.
/// Not in C++ meshoptimizer. Thresholds follow measured errors and are not
/// forced to increase: independent simplifications can have nonmonotone error.
/// # Errors
/// The errors of [`measure_lod_quality`] or [`LodCamera::switch_distance`].
pub fn measure_lod_chain_quality_generic<I: crate::Index>(
    source: LodQualityMeshGeneric<'_, I>,
    chain: &LodChainGeneric<I>,
    options: &LodQualityOptions,
    camera: Option<LodCamera>,
    workspace: &mut Workspace,
) -> Result<Vec<LodQualityLevel>, Error> {
    let mut work = workspace.begin();
    let result = (|| {
        validate_options(options)?;
        if let Some(camera) = camera {
            camera.switch_distance(0.0)?;
        }
        work.add(chain.levels.len())?;
        let mut budget = Budget::new(workspace);
        let mut out = budget.reserved(chain.levels.len())?;
        for level in &chain.levels {
            let mesh = LodQualityMeshGeneric {
                indices: &level.indices,
                ..source
            };
            let quality = measure(source, mesh, options, &mut budget, &mut work)?;
            out.push(LodQualityLevel {
                quality,
                switch_distance: camera
                    .map(|c| c.switch_distance(quality.symmetric.max))
                    .transpose()?,
            });
        }
        Ok(out)
    })();
    workspace.finish(&work);
    result
}

fn validate_options(options: &LodQualityOptions) -> Result<(), Error> {
    if !options.samples_per_unit_area.is_finite() || options.samples_per_unit_area < 0.0 {
        return Err(Error::InvalidParameter);
    }
    Ok(())
}
fn position<I: crate::Index>(mesh: LodQualityMeshGeneric<'_, I>, index: I) -> Result<V, Error> {
    let i = usize::try_from(index.to_u32()).map_err(|_| Error::SizeOverflow)?;
    let p = mesh
        .positions
        .get(i)
        .ok_or(Error::IndexOutOfBounds)?
        .map(f64::from);
    if !p.iter().all(|v| v.is_finite()) {
        return Err(Error::NumericalFailure);
    }
    Ok(p)
}
fn unit(v: V) -> Result<V, Error> {
    let d = libm::sqrt(dot(v, v));
    if !d.is_finite() {
        return Err(Error::NumericalFailure);
    }
    if d == 0.0 {
        return Err(Error::InvalidParameter);
    }
    Ok(v.map(|x| x / d))
}
fn normal<I: crate::Index>(mesh: LodQualityMeshGeneric<'_, I>, index: I) -> Result<V, Error> {
    match mesh.normals {
        Some(normals) => unit(
            normals[usize::try_from(index.to_u32()).map_err(|_| Error::SizeOverflow)?]
                .map(f64::from),
        ),
        None => Ok([0.0; 3]),
    }
}
fn validate<I: crate::Index>(
    mesh: LodQualityMeshGeneric<'_, I>,
    work: &mut Work,
) -> Result<(), Error> {
    if !mesh.indices.len().is_multiple_of(3) {
        return Err(Error::InvalidTopology);
    }
    if mesh.indices.is_empty()
        || mesh
            .normals
            .is_some_and(|n| n.len() != mesh.positions.len())
    {
        return Err(Error::InvalidParameter);
    }
    for &i in mesh.indices {
        work.add(1)?;
        position(mesh, i)?;
        normal(mesh, i)?;
    }
    Ok(())
}
fn measure<I: crate::Index>(
    source: LodQualityMeshGeneric<'_, I>,
    level: LodQualityMeshGeneric<'_, I>,
    options: &LodQualityOptions,
    budget: &mut Budget<'_>,
    work: &mut Work,
) -> Result<LodQuality, Error> {
    validate_options(options)?;
    if source.normals.is_some() != level.normals.is_some() {
        return Err(Error::InvalidParameter);
    }
    validate(source, work)?;
    validate(level, work)?;
    let a = direction::<I, false, 3, false>(source, level, options, budget, work, 0.0)?;
    let b = direction::<I, false, 3, false>(level, source, options, budget, work, 0.0)?;
    let samples = a.count.checked_add(b.count).ok_or(Error::SizeOverflow)?;
    Ok(LodQuality {
        source_to_level: a.distance(),
        level_to_source: b.distance(),
        symmetric: LodDistance {
            max: libm::sqrt(a.max.max(b.max)),
            rms: libm::sqrt((a.sum + b.sum) / samples as f64),
            samples,
        },
        normals: source.normals.map(|_| LodNormalDeviation {
            max: a.angle_max.max(b.angle_max),
            mean: (a.angle_sum + b.angle_sum) / samples as f64,
        }),
    })
}
// The builder consumes only the symmetric maximum. Keep the public statistics
// path exact, including nearest-triangle ties, RMS and shading normals.
// The private floor is already retained by the builder: return the greater of
// that floor and the sampled maximum without querying irrelevant distances.
#[cfg(feature = "clusterlod")]
pub(crate) fn measure_max(
    source: LodQualityMesh<'_>,
    level: LodQualityMesh<'_>,
    options: &LodQualityOptions,
    floor: f32,
    workspace: &mut Workspace,
) -> Result<f64, Error> {
    let mut work = workspace.begin();
    let result = (|| {
        validate_options(options)?;
        if !floor.is_finite() || floor < 0.0 {
            return Err(Error::InvalidParameter);
        }
        // The caller already retains this floor. Its square is exact in f64
        // for every finite f32, so queries need only find samples above it.
        let floor = if floor == 0.0 { 0.0 } else { f64::from(floor) };
        let cutoff = floor * floor;
        let source: LodQualityMeshGeneric<'_, u32> = source.into();
        let level: LodQualityMeshGeneric<'_, u32> = level.into();
        validate(source, &mut work)?;
        validate(level, &mut work)?;
        let mut budget = Budget::new(workspace);
        let a = direction::<u32, true, 0, false>(
            source,
            level,
            options,
            &mut budget,
            &mut work,
            cutoff,
        )?;
        let b = direction::<u32, true, 0, false>(
            level,
            source,
            options,
            &mut budget,
            &mut work,
            a.max,
        )?;
        // Avoid a sqrt round trip when no sample exceeds the existing floor.
        Ok(if b.max == cutoff {
            floor
        } else {
            libm::sqrt(b.max)
        })
    })();
    workspace.finish(&work);
    result
}

// Both maxima use the same original sample stream and exact nearest queries.
// Unlike the public statistics path, undefined interpolated normals contribute
// the maximum chord (two) rather than failing a valid geometric build.
// Returns geometric distance and unit-normal chord maxima. Chords are exactly
// 2*sin(angle/2); compare their squares without per-sample trigonometry.
#[cfg(feature = "clusterlod")]
pub(crate) fn measure_normal_max(
    source: LodQualityMesh<'_>,
    level: LodQualityMesh<'_>,
    options: &LodQualityOptions,
    workspace: &mut Workspace,
) -> Result<(f64, f64), Error> {
    let mut work = workspace.begin();
    let result = (|| {
        validate_options(options)?;
        if source.normals.is_none() || level.normals.is_none() {
            return Err(Error::InvalidParameter);
        }
        let source: LodQualityMeshGeneric<'_, u32> = source.into();
        let level: LodQualityMeshGeneric<'_, u32> = level.into();
        validate(source, &mut work)?;
        validate(level, &mut work)?;
        let mut budget = Budget::new(workspace);
        let a =
            direction::<u32, false, 3, true>(source, level, options, &mut budget, &mut work, 0.0)?;
        let b =
            direction::<u32, false, 3, true>(level, source, options, &mut budget, &mut work, 0.0)?;
        Ok((
            libm::sqrt(a.max.max(b.max)),
            libm::sqrt(a.normal_chord_squared_max.max(b.normal_chord_squared_max)),
        ))
    })();
    workspace.finish(&work);
    result
}

fn sub(a: V, b: V) -> V {
    core::array::from_fn(|k| a[k] - b[k])
}
fn dot(a: V, b: V) -> f64 {
    a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}
fn cross(a: V, b: V) -> V {
    [
        a[1] * b[2] - a[2] * b[1],
        a[2] * b[0] - a[0] * b[2],
        a[0] * b[1] - a[1] * b[0],
    ]
}
fn blend(p: [V; 3], w: V) -> V {
    core::array::from_fn(|k| p[0][k] * w[0] + p[1][k] * w[1] + p[2][k] * w[2])
}
#[derive(Clone, Copy)]
// Distance-only records omit unused normals; full statistics retain all three.
struct Triangle<const NORMALS: usize = 3> {
    p: [V; 3],
    n: [V; NORMALS],
    id: usize,
}
impl<const NORMALS: usize> Triangle<NORMALS> {
    fn read<I: crate::Index>(mesh: LodQualityMeshGeneric<'_, I>, i: usize) -> Result<Self, Error> {
        let ix = &mesh.indices[i * 3..i * 3 + 3];
        let mut triangle = Self {
            p: [
                position(mesh, ix[0])?,
                position(mesh, ix[1])?,
                position(mesh, ix[2])?,
            ],
            n: [[0.0; 3]; NORMALS],
            id: i,
        };
        for (value, &index) in triangle.n.iter_mut().zip(ix) {
            *value = normal(mesh, index)?;
        }
        Ok(triangle)
    }
    fn normals(&self) -> [V; 3] {
        let mut normals = [[0.0; 3]; 3];
        for (to, &from) in normals.iter_mut().zip(&self.n) {
            *to = from;
        }
        normals
    }
    fn center(&self, axis: usize) -> f64 {
        self.p[0][axis] + self.p[1][axis] + self.p[2][axis]
    }
}
#[derive(Clone, Copy)]
struct Node {
    lo: V,
    hi: V,
    left: usize,
    right: usize,
}
struct Tree<const NORMALS: usize = 3> {
    triangles: Vec<Triangle<NORMALS>>,
    nodes: Vec<Node>,
}
impl<const NORMALS: usize> Tree<NORMALS> {
    fn build<I: crate::Index>(
        mesh: LodQualityMeshGeneric<'_, I>,
        budget: &mut Budget<'_>,
        work: &mut Work,
    ) -> Result<Self, Error> {
        let n = mesh.indices.len() / 3;
        work.add(n)?; // initialization is charged before reserving/reading records
        let mut triangles = budget.reserved(n)?;
        for i in 0..n {
            triangles.push(Triangle::<NORMALS>::read(mesh, i)?);
        }
        let count = n
            .checked_mul(2)
            .and_then(|n| n.checked_sub(1))
            .ok_or(Error::SizeOverflow)?;
        work.add(count)?;
        let nodes = budget.reserved(count)?;
        let mut tree = Self { triangles, nodes };
        tree.build_range(0, n, work)?;
        Ok(tree)
    }
    fn build_range(&mut self, start: usize, end: usize, work: &mut Work) -> Result<usize, Error> {
        let mut lo = [f64::INFINITY; 3];
        let mut hi = [f64::NEG_INFINITY; 3];
        for t in &self.triangles[start..end] {
            work.add(1)?;
            for p in t.p {
                for k in 0..3 {
                    lo[k] = lo[k].min(p[k]);
                    hi[k] = hi[k].max(p[k]);
                }
            }
        }
        let id = self.nodes.len();
        self.nodes.push(Node {
            lo,
            hi,
            left: start,
            right: usize::MAX,
        });
        if end - start > 1 {
            let axis = (0..3)
                .max_by(|&a, &b| (hi[a] - lo[a]).total_cmp(&(hi[b] - lo[b])))
                .unwrap_or(0);
            let mid = start + (end - start) / 2;
            select(&mut self.triangles[start..end], mid - start, axis, work)?;
            let left = self.build_range(start, mid, work)?;
            let right = self.build_range(mid, end, work)?;
            self.nodes[id].left = left;
            self.nodes[id].right = right;
        }
        Ok(id)
    }
    fn query<const MAX_ONLY: bool>(
        &self,
        id: usize,
        p: V,
        best: &mut Hit,
        work: &mut Work,
        cutoff: f64,
    ) -> Result<(), Error> {
        // A witnessed distance below the running maximum cannot raise it.
        // Otherwise finish the exact nearest query with the original arithmetic.
        if MAX_ONLY && best.distance <= cutoff {
            return Ok(());
        }
        work.add(1)?;
        let n = self.nodes[id];
        if box_distance(n, p) > best.distance {
            return Ok(());
        }
        if n.right == usize::MAX {
            let t = self.triangles[n.left];
            let (distance, weights) = closest(t.p, p);
            if distance < best.distance || (distance == best.distance && t.id < best.id) {
                *best = Hit {
                    distance,
                    weights,
                    id: t.id,
                    normals: t.normals(),
                    triangle: n.left,
                };
            }
        } else {
            let (a, b) =
                if box_distance(self.nodes[n.left], p) <= box_distance(self.nodes[n.right], p) {
                    (n.left, n.right)
                } else {
                    (n.right, n.left)
                };
            self.query::<MAX_ONLY>(a, p, best, work, cutoff)?;
            self.query::<MAX_ONLY>(b, p, best, work, cutoff)?;
        }
        Ok(())
    }
}
// Metered median partition. A bounded number of quickselect passes falls back
// to heapsort, preventing adversarial quadratic build work. No hidden sort scans.
fn select<const NORMALS: usize>(
    a: &mut [Triangle<NORMALS>],
    mid: usize,
    axis: usize,
    work: &mut Work,
) -> Result<(), Error> {
    let key = |t: Triangle<NORMALS>| (t.center(axis), t.id);
    let cmp = |a: Triangle<NORMALS>, b: Triangle<NORMALS>| {
        key(a).0.total_cmp(&key(b).0).then(a.id.cmp(&b.id))
    };
    let mut lo = 0;
    let mut hi = a.len();
    let mut passes = 2 * (usize::BITS - a.len().leading_zeros());
    while hi - lo > 1 {
        if passes == 0 {
            heap_sort(&mut a[lo..hi], cmp, work)?;
            break;
        }
        passes -= 1;
        let pivot = a[lo + (hi - lo) / 2];
        let mut store = lo;
        a.swap(lo + (hi - lo) / 2, hi - 1);
        for i in lo..hi - 1 {
            work.add(1)?;
            if cmp(a[i], pivot).is_lt() {
                a.swap(i, store);
                store += 1;
            }
        }
        a.swap(store, hi - 1);
        if store == mid {
            break;
        }
        if store < mid {
            lo = store + 1;
        } else {
            hi = store;
        }
    }
    Ok(())
}
fn heap_sort<const NORMALS: usize>(
    a: &mut [Triangle<NORMALS>],
    cmp: impl Fn(Triangle<NORMALS>, Triangle<NORMALS>) -> core::cmp::Ordering + Copy,
    work: &mut Work,
) -> Result<(), Error> {
    fn sift<const INNER_NORMALS: usize>(
        a: &mut [Triangle<INNER_NORMALS>],
        mut root: usize,
        end: usize,
        cmp: impl Fn(Triangle<INNER_NORMALS>, Triangle<INNER_NORMALS>) -> core::cmp::Ordering + Copy,
        work: &mut Work,
    ) -> Result<(), Error> {
        while root < end / 2 {
            let mut child = root * 2 + 1;
            work.add(1)?;
            if child + 1 < end && cmp(a[child], a[child + 1]).is_lt() {
                child += 1;
            }
            if !cmp(a[root], a[child]).is_lt() {
                break;
            }
            a.swap(root, child);
            root = child;
        }
        Ok(())
    }
    for root in (0..a.len() / 2).rev() {
        sift(a, root, a.len(), cmp, work)?;
    }
    for end in (1..a.len()).rev() {
        a.swap(0, end);
        sift(a, 0, end, cmp, work)?;
    }
    Ok(())
}
fn box_distance(n: Node, p: V) -> f64 {
    (0..3)
        .map(|k| (n.lo[k] - p[k]).max(0.0).max(p[k] - n.hi[k]))
        .map(|d| d * d)
        .sum()
}
struct Hit {
    triangle: usize,
    distance: f64,
    weights: V,
    id: usize,
    normals: [V; 3],
}
// The minimum is on an edge or the orthogonal projection onto the face.
// Edge fallback also handles collinear and point triangles without division by zero.
fn closest(t: [V; 3], p: V) -> (f64, V) {
    let mut best = (f64::INFINITY, [1.0, 0.0, 0.0]);
    for (a, b) in [(0, 1), (1, 2), (2, 0)] {
        let edge = sub(t[b], t[a]);
        let len = dot(edge, edge);
        let s = if len > 0.0 {
            (dot(sub(p, t[a]), edge) / len).clamp(0.0, 1.0)
        } else {
            0.0
        };
        let mut w = [0.0; 3];
        w[a] = 1.0 - s;
        w[b] = s;
        // Measure the residual directly, rather than reconstructing a world
        // point from rounded weights (which can erase small offsets on long
        // segments). Endpoints use their direct distance; interiors use the
        // perpendicular distance to the line, relative to the nearer endpoint.
        let d = if s == 0.0 {
            let delta = sub(p, t[a]);
            dot(delta, delta)
        } else if s == 1.0 {
            let delta = sub(p, t[b]);
            dot(delta, delta)
        } else {
            let offset = sub(p, if s <= 0.5 { t[a] } else { t[b] });
            let perpendicular = cross(offset, edge);
            dot(perpendicular, perpendicular) / len
        };
        if d < best.0 {
            best = (d, w);
        }
    }
    let ab = sub(t[1], t[0]);
    let ac = sub(t[2], t[0]);
    let n = cross(ab, ac);
    let nn = dot(n, n);
    if nn > 0.0 {
        let ap = sub(p, t[0]);
        let v = dot(cross(ap, ac), n) / nn;
        let w = dot(cross(ab, ap), n) / nn;
        if v >= 0.0 && w >= 0.0 && v + w <= 1.0 {
            let h = dot(ap, n);
            let d = h * h / nn;
            if d < best.0 {
                best = (d, [1.0 - v - w, v, w]);
            }
        }
    }
    best
}
// The face candidate is also tested by `closest`. A successful candidate is
// sufficient to bound this sample below the running maximum; a failed witness
// leaves the exact tree query unchanged. Degenerate/outside faces use edges.
fn witness(t: [V; 3], p: V, cutoff: f64) -> Option<(f64, V)> {
    let ab = sub(t[1], t[0]);
    let ac = sub(t[2], t[0]);
    let n = cross(ab, ac);
    let nn = dot(n, n);
    if nn > 0.0 {
        let ap = sub(p, t[0]);
        let v = dot(cross(ap, ac), n) / nn;
        let w = dot(cross(ab, ap), n) / nn;
        if v >= 0.0 && w >= 0.0 && v + w <= 1.0 {
            let h = dot(ap, n);
            let d = h * h / nn;
            return if d <= cutoff {
                Some((d, [1.0 - v - w, v, w]))
            } else {
                None
            };
        }
    }
    let (d, weights) = closest(t, p);
    if d <= cutoff {
        Some((d, weights))
    } else {
        None
    }
}
#[derive(Default)]
struct Stats {
    witness: Option<usize>,
    count: u64,
    max: f64,
    sum: f64,
    angle_max: f64,
    normal_chord_squared_max: f64,
    angle_sum: f64,
}
impl Stats {
    fn distance(&self) -> LodDistance {
        LodDistance {
            max: libm::sqrt(self.max),
            rms: libm::sqrt(self.sum / self.count as f64),
            samples: self.count,
        }
    }
    fn sample<const MAX_ONLY: bool, const NORMALS: usize, const NORMAL_MAX: bool>(
        &mut self,
        t: Triangle<NORMALS>,
        weights: V,
        tree: &Tree<NORMALS>,
        normals: bool,
        work: &mut Work,
    ) -> Result<(), Error> {
        work.add(1)?;
        let p = blend(t.p, weights);
        let mut hit = Hit {
            triangle: usize::MAX,
            distance: f64::INFINITY,
            weights: [0.0; 3],
            id: usize::MAX,
            normals: [[0.0; 3]; 3],
        };
        // Nearby samples often share a useful target triangle. Its distance is
        // an upper bound, so a successful witness needs no BVH traversal.
        if MAX_ONLY {
            if let Some(i) = self.witness {
                work.add(1)?;
                let t = tree.triangles[i];
                if let Some((distance, weights)) = witness(t.p, p, self.max) {
                    hit = Hit {
                        triangle: i,
                        distance,
                        weights,
                        id: t.id,
                        normals: t.normals(),
                    };
                }
            }
        }
        tree.query::<MAX_ONLY>(0, p, &mut hit, work, self.max)?;
        if MAX_ONLY && hit.triangle != usize::MAX {
            self.witness = Some(hit.triangle);
        }
        self.count = self.count.checked_add(1).ok_or(Error::SizeOverflow)?;
        self.max = self.max.max(hit.distance);
        if !MAX_ONLY && !NORMAL_MAX {
            self.sum += hit.distance;
        }
        if !MAX_ONLY && normals {
            let a = unit(blend(t.normals(), weights));
            let b = unit(blend(hit.normals, hit.weights));
            if NORMAL_MAX {
                let squared = match (a, b) {
                    (Ok(a), Ok(b)) => {
                        let delta = sub(a, b);
                        dot(delta, delta).clamp(0.0, 4.0)
                    }
                    // Undefined interpolants mean the largest unit-normal chord.
                    _ => 4.0,
                };
                self.normal_chord_squared_max = self.normal_chord_squared_max.max(squared);
            } else {
                let (a, b) = (
                    a.map_err(|_| Error::NumericalFailure)?,
                    b.map_err(|_| Error::NumericalFailure)?,
                );
                let angle = libm::atan2(
                    libm::sqrt(dot(cross(a, b), cross(a, b))),
                    dot(a, b).clamp(-1.0, 1.0),
                );
                self.angle_max = self.angle_max.max(angle);
                self.angle_sum += angle;
            }
        }
        Ok(())
    }
}
fn direction<
    I: crate::Index,
    const MAX_ONLY: bool,
    const NORMALS: usize,
    const NORMAL_MAX: bool,
>(
    source: LodQualityMeshGeneric<'_, I>,
    target: LodQualityMeshGeneric<'_, I>,
    options: &LodQualityOptions,
    budget: &mut Budget<'_>,
    work: &mut Work,
    initial_max: f64,
) -> Result<Stats, Error> {
    let tree = Tree::<NORMALS>::build(target, budget, work)?;
    // Always release local storage accounting, including after a failed query.
    let mut corners = Vec::<u64>::new();
    let result = (|| {
        if MAX_ONLY || NORMAL_MAX {
            let words = source.positions.len().div_ceil(64);
            work.add(words)?;
            corners = budget.reserved(words)?;
            corners.resize(words, 0);
        }
        let mut stats = Stats {
            max: initial_max,
            ..Stats::default()
        };
        for i in 0..source.indices.len() / 3 {
            work.add(1)?;
            let t = Triangle::<NORMALS>::read(source, i)?;
            let n = cross(sub(t.p[1], t.p[0]), sub(t.p[2], t.p[0]));
            let count = libm::ceil(0.5 * libm::sqrt(dot(n, n)) * options.samples_per_unit_area);
            if !count.is_finite() || count > f64::from(u32::MAX) {
                return Err(Error::SizeOverflow);
            }
            // Finite, nonnegative and checked <= u32::MAX: conversion cannot wrap.
            let count = count as u32;
            for (corner, w) in [[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]
                .into_iter()
                .enumerate()
            {
                let vertex = source.indices[i * 3 + corner].to_u32() as usize;
                let mask = 1u64 << (vertex % 64);
                if (MAX_ONLY || NORMAL_MAX) && corners[vertex / 64] & mask != 0 {
                    // This corner is already bounded by an earlier maximum.
                    // Keep the sample visit/count; its distance cannot raise max.
                    work.add(1)?;
                    stats.count = stats.count.checked_add(1).ok_or(Error::SizeOverflow)?;
                } else {
                    stats.sample::<MAX_ONLY, NORMALS, NORMAL_MAX>(
                        t,
                        w,
                        &tree,
                        source.normals.is_some(),
                        work,
                    )?;
                    if MAX_ONLY || NORMAL_MAX {
                        corners[vertex / 64] |= mask;
                    }
                }
            }
            for j in 0..count {
                let r = libm::sqrt((f64::from(j) + 0.5) / f64::from(count));
                let t2 = (f64::from(j.reverse_bits()) + 0.5) / 4294967296.0;
                stats.sample::<MAX_ONLY, NORMALS, NORMAL_MAX>(
                    t,
                    [1.0 - r, r * (1.0 - t2), r * t2],
                    &tree,
                    source.normals.is_some(),
                    work,
                )?;
            }
        }
        Ok(stats)
    })();
    if MAX_ONLY || NORMAL_MAX {
        budget.release(corners);
    }
    budget.release(tree.nodes);
    budget.release(tree.triangles);
    result
}

/// The source-compatible u32 entry point; see [`measure_lod_quality_generic`] for selectable index widths.
#[inline]
pub fn measure_lod_quality(
    source: LodQualityMesh<'_>,
    level: LodQualityMesh<'_>,
    options: &LodQualityOptions,
    workspace: &mut Workspace,
) -> Result<LodQuality, Error> {
    measure_lod_quality_generic::<u32>(source.into(), level.into(), options, workspace)
}

/// The source-compatible u32 entry point; see [`measure_lod_chain_quality_generic`] for selectable index widths.
#[inline]
pub fn measure_lod_chain_quality(
    source: LodQualityMesh<'_>,
    chain: &LodChain,
    options: &LodQualityOptions,
    camera: Option<LodCamera>,
    workspace: &mut Workspace,
) -> Result<Vec<LodQualityLevel>, Error> {
    let source: LodQualityMeshGeneric<'_, u32> = source.into();
    let mut work = workspace.begin();
    let result = (|| {
        validate_options(options)?;
        if let Some(camera) = camera {
            camera.switch_distance(0.0)?;
        }
        work.add(chain.levels.len())?;
        let mut budget = Budget::new(workspace);
        let mut out = budget.reserved(chain.levels.len())?;
        for level in &chain.levels {
            let mesh = LodQualityMeshGeneric {
                indices: &level.indices,
                ..source
            };
            let quality = measure(source, mesh, options, &mut budget, &mut work)?;
            out.push(LodQualityLevel {
                quality,
                switch_distance: camera
                    .map(|c| c.switch_distance(quality.symmetric.max))
                    .transpose()?,
            });
        }
        Ok(out)
    })();
    workspace.finish(&work);
    result
}

impl<'a> LodQualityMesh<'a> {
    /// Source-compatible u32 operation.
    #[inline]
    pub fn new(indices: &'a [u32], positions: Positions<'a>) -> Self {
        LodQualityMeshGeneric::new_generic(indices, positions).into()
    }
}

/// The source-compatible u32 form of [`LodQualityMeshGeneric`].
/// A borrowed triangle surface. Only referenced vertices are measured/validated.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct LodQualityMesh<'a> {
    /// Triangle-list indices; at least one triangle is required.
    pub indices: &'a [u32],
    /// Positions in the same world units as the reported distances.
    pub positions: Positions<'a>,
    /// Optional per-vertex normals, normalized before interpolation.
    pub normals: Option<&'a [[f32; 3]]>,
}
impl<'a> From<LodQualityMeshGeneric<'a, u32>> for LodQualityMesh<'a> {
    #[inline]
    fn from(value: LodQualityMeshGeneric<'a, u32>) -> Self {
        Self {
            indices: value.indices,
            positions: value.positions,
            normals: value.normals,
        }
    }
}

impl<'a> From<LodQualityMesh<'a>> for LodQualityMeshGeneric<'a, u32> {
    #[inline]
    fn from(value: LodQualityMesh<'a>) -> Self {
        Self {
            indices: value.indices,
            positions: value.positions,
            normals: value.normals,
        }
    }
}

#[cfg(all(test, feature = "clusterlod"))]
mod maximum_tests {
    use super::*;

    #[test]
    fn maximum_only_matches_uneven_surfaces() {
        let mut state = 17u32;
        let mut random = || {
            state = state.wrapping_mul(1664525).wrapping_add(1013904223);
            (state >> 8) as f32 / 16777216.0
        };
        for count in [1, 3, 4, 5, 8, 9, 17] {
            let indices: Vec<u32> = (0..count * 3).collect();
            for scale in [1e-10, 1., 1e10] {
                for _ in 0..16 {
                    let source: Vec<[f32; 3]> = (0..count * 3)
                        .map(|_| core::array::from_fn(|_| random() * scale))
                        .collect();
                    let target: Vec<[f32; 3]> = source
                        .iter()
                        .map(|p| core::array::from_fn(|k| p[k] + (random() - 0.5) * scale))
                        .collect();
                    let options = LodQualityOptions {
                        samples_per_unit_area: 4. / f64::from(scale).powi(2),
                    };
                    let a = LodQualityMesh::new(&indices, Positions::from_packed(&source));
                    let b = LodQualityMesh::new(&indices, Positions::from_packed(&target));
                    let full = measure_lod_quality(a, b, &options, &mut Workspace::default())
                        .expect("full measurement");
                    let rounded = full.symmetric.max as f32;
                    for floor in [
                        0.0,
                        f32::from_bits(rounded.to_bits().saturating_sub(1)),
                        rounded,
                        f32::from_bits(rounded.to_bits() + 1),
                    ] {
                        let maximum = measure_max(a, b, &options, floor, &mut Workspace::default())
                            .expect("maximum measurement");
                        assert_eq!(
                            maximum.to_bits(),
                            full.symmetric.max.max(f64::from(floor)).to_bits()
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn maximum_only_matches_full_queries() {
        // Overlapping faces, repeated corners and degenerate segments exercise
        // witness exits and exact queries that must raise the running maximum.
        let indices = [0, 1, 2, 2, 1, 0, 0, 0, 3, 3, 4, 5];
        let source = [
            [0., 0., 0.],
            [1., 0., 0.],
            [0., 1., 0.],
            [0., 0., 2.],
            [1., 0., 2.],
            [0., 1., 2.],
        ];
        for offset in [0., 1e-12, 0.125, 4.] {
            let target = source.map(|p| [p[0], p[1], p[2] + offset]);
            for density in [0., 1., 100.] {
                let options = LodQualityOptions {
                    samples_per_unit_area: density,
                };
                let a = LodQualityMesh::new(&indices, Positions::from_packed(&source));
                let b = LodQualityMesh::new(&indices, Positions::from_packed(&target));
                let full = measure_lod_quality(a, b, &options, &mut Workspace::default())
                    .expect("full measurement");
                for floor in [0.0, f32::MIN_POSITIVE, 0.03125, 1.0, f32::MAX] {
                    let maximum = measure_max(a, b, &options, floor, &mut Workspace::default())
                        .expect("maximum measurement");
                    assert_eq!(
                        maximum.to_bits(),
                        full.symmetric.max.max(f64::from(floor)).to_bits()
                    );
                }
            }
        }
    }
}