alice-sdf 1.11.0

ALICE-SDF: 3D/Spatial Data Specialist — Store laws, not polygons. GLSL/WGSL/HLSL transpile, SVO, GPU marching cubes, texture-fit, procedural CSG. Bridges to alice-codec/physics/asp/cache/font are not part of the crates.io release (path / git dependency only).
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
//! Physics collision mesh generation (Deep Fried v2)
//!
//! Generates simplified collision primitives from meshes for physics engines.
//! Compatible with UE5, Unity, Godot, and Bullet/PhysX collision systems.
//!
//! # Deep Fried v2 Optimizations
//!
//! - **Parallel Voxelization**: Triangle-to-voxel mapping via `rayon` + `AtomicU8`.
//! - **Parallel AABB**: Min/max reduction via `rayon` parallel iterator.
//! - **Parallel Hull Simplification**: Nearest-neighbor search parallelized.
//! - **Division Exorcism**: Pre-computed `inv_cell` replaces per-voxel division.
//!
//! # Collision Types
//! - **AABB**: Axis-aligned bounding box (fastest, least accurate)
//! - **Bounding Sphere**: Spherical bound (fast, good for round objects)
//! - **Convex Hull**: Tight convex approximation (medium cost, good accuracy)
//! - **Simplified Mesh**: Decimated triangle mesh (highest accuracy, highest cost)
//!
//! Author: Moroya Sakamoto

use crate::mesh::Mesh;
use glam::Vec3;
use rayon::prelude::*;
use std::sync::atomic::{AtomicU8, Ordering};

/// Axis-aligned bounding box for collision
#[derive(Debug, Clone, Copy)]
pub struct CollisionAabb {
    /// Minimum corner
    pub min: Vec3,
    /// Maximum corner
    pub max: Vec3,
}

impl CollisionAabb {
    /// Get center of the AABB
    #[inline]
    pub fn center(&self) -> Vec3 {
        (self.min + self.max) * 0.5
    }

    /// Get half-extents of the AABB
    #[inline]
    pub fn half_extents(&self) -> Vec3 {
        (self.max - self.min) * 0.5
    }

    /// Get volume of the AABB
    #[inline]
    pub fn volume(&self) -> f32 {
        let ext = self.max - self.min;
        ext.x * ext.y * ext.z
    }

    /// Test if a point is inside the AABB
    #[inline]
    pub fn contains(&self, point: Vec3) -> bool {
        point.x >= self.min.x
            && point.x <= self.max.x
            && point.y >= self.min.y
            && point.y <= self.max.y
            && point.z >= self.min.z
            && point.z <= self.max.z
    }
}

/// Bounding sphere for collision
#[derive(Debug, Clone, Copy)]
pub struct BoundingSphere {
    /// Center of the sphere
    pub center: Vec3,
    /// Radius of the sphere
    pub radius: f32,
}

impl BoundingSphere {
    /// Test if a point is inside the sphere
    #[inline]
    pub fn contains(&self, point: Vec3) -> bool {
        (point - self.center).length_squared() <= self.radius * self.radius
    }
}

/// Convex hull collision mesh
#[derive(Debug, Clone)]
pub struct ConvexHull {
    /// Hull vertices
    pub vertices: Vec<Vec3>,
    /// Hull face indices (triangles)
    pub indices: Vec<u32>,
}

/// Simplified collision mesh
#[derive(Debug, Clone)]
pub struct CollisionMesh {
    /// Simplified vertices
    pub vertices: Vec<Vec3>,
    /// Triangle indices
    pub indices: Vec<u32>,
}

/// Compute axis-aligned bounding box from mesh (Deep Fried v2)
///
/// [Deep Fried v2] Parallel min/max reduction via `rayon`.
pub fn compute_aabb(mesh: &Mesh) -> CollisionAabb {
    if mesh.vertices.is_empty() {
        return CollisionAabb {
            min: Vec3::ZERO,
            max: Vec3::ZERO,
        };
    }

    let (min, max) = mesh
        .vertices
        .par_iter()
        .map(|v| (v.position, v.position))
        .reduce(
            || (Vec3::splat(f32::MAX), Vec3::splat(f32::MIN)),
            |(min_a, max_a), (min_b, max_b)| (min_a.min(min_b), max_a.max(max_b)),
        );

    CollisionAabb { min, max }
}

/// Compute bounding sphere using Ritter's algorithm
///
/// Finds a tight-fitting bounding sphere in O(n) time.
pub fn compute_bounding_sphere(mesh: &Mesh) -> BoundingSphere {
    if mesh.vertices.is_empty() {
        return BoundingSphere {
            center: Vec3::ZERO,
            radius: 0.0,
        };
    }

    // Start with first vertex
    let mut center = mesh.vertices[0].position;
    let mut radius;

    // Find point farthest from first vertex
    let mut farthest_idx = 0;
    let mut max_dist = 0.0f32;
    for (i, v) in mesh.vertices.iter().enumerate() {
        let d = (v.position - center).length_squared();
        if d > max_dist {
            max_dist = d;
            farthest_idx = i;
        }
    }

    // Find point farthest from that point
    let p1 = mesh.vertices[farthest_idx].position;
    let mut p2_idx = 0;
    max_dist = 0.0;
    for (i, v) in mesh.vertices.iter().enumerate() {
        let d = (v.position - p1).length_squared();
        if d > max_dist {
            max_dist = d;
            p2_idx = i;
        }
    }
    let p2 = mesh.vertices[p2_idx].position;

    // Initial sphere from these two extremes
    center = (p1 + p2) * 0.5;
    radius = (p1 - p2).length() * 0.5;

    // Grow sphere to include all points
    for v in &mesh.vertices {
        let dist = (v.position - center).length();
        if dist > radius {
            let new_radius = (radius + dist) * 0.5;
            let delta = dist - radius;
            center += (v.position - center).normalize() * (delta * 0.5);
            radius = new_radius;
        }
    }

    BoundingSphere { center, radius }
}

/// Generate a convex hull from mesh vertices
///
/// Uses an incremental convex hull algorithm. The result is suitable
/// for physics engines that require convex collision shapes.
pub fn compute_convex_hull(mesh: &Mesh) -> ConvexHull {
    let points: Vec<Vec3> = mesh.vertices.iter().map(|v| v.position).collect();
    convex_hull_from_points(&points)
}

/// Generate convex hull from a set of points
pub fn convex_hull_from_points(points: &[Vec3]) -> ConvexHull {
    if points.len() < 4 {
        return ConvexHull {
            vertices: points.to_vec(),
            indices: if points.len() == 3 {
                vec![0, 1, 2]
            } else {
                vec![]
            },
        };
    }

    // Find initial tetrahedron
    let (p0, p1, p2, p3) = find_initial_tetrahedron(points);

    let mut hull_verts = vec![points[p0], points[p1], points[p2], points[p3]];
    let mut hull_faces: Vec<[u32; 3]> = vec![[0, 1, 2], [0, 2, 3], [0, 3, 1], [1, 3, 2]];

    // Ensure faces are oriented outward
    let center = (hull_verts[0] + hull_verts[1] + hull_verts[2] + hull_verts[3]) * 0.25;
    for face in &mut hull_faces {
        let a = hull_verts[face[0] as usize];
        let b = hull_verts[face[1] as usize];
        let c = hull_verts[face[2] as usize];
        let n = (b - a).cross(c - a);
        let face_center = (a + b + c) / 3.0;
        if n.dot(face_center - center) < 0.0 {
            face.swap(1, 2);
        }
    }

    // Add remaining points
    let used: [usize; 4] = (p0, p1, p2, p3).into();
    for (i, &pt) in points.iter().enumerate() {
        if used.contains(&i) {
            continue;
        }

        // Find faces visible from this point
        let mut visible = Vec::new();
        for (fi, face) in hull_faces.iter().enumerate() {
            let a = hull_verts[face[0] as usize];
            let b = hull_verts[face[1] as usize];
            let c = hull_verts[face[2] as usize];
            let n = (b - a).cross(c - a);
            if n.dot(pt - a) > 1e-6 {
                visible.push(fi);
            }
        }

        if visible.is_empty() {
            continue; // Point is inside hull
        }

        // Collect horizon edges (edges shared by exactly one visible face)
        let mut horizon_edges: Vec<(u32, u32)> = Vec::new();
        for &fi in &visible {
            let face = hull_faces[fi];
            for k in 0..3 {
                let e0 = face[k];
                let e1 = face[(k + 1) % 3];
                // Check if the reverse edge exists in another visible face
                let is_shared = visible.iter().any(|&fj| {
                    if fj == fi {
                        return false;
                    }
                    let fj_face = hull_faces[fj];
                    for m in 0..3 {
                        if fj_face[m] == e1 && fj_face[(m + 1) % 3] == e0 {
                            return true;
                        }
                    }
                    false
                });
                if !is_shared {
                    horizon_edges.push((e0, e1));
                }
            }
        }

        // Remove visible faces (in reverse order to maintain indices)
        let mut sorted_visible = visible.clone();
        sorted_visible.sort_unstable_by(|a, b| b.cmp(a));
        for fi in sorted_visible {
            hull_faces.swap_remove(fi);
        }

        // Add new vertex
        let new_vi = hull_verts.len() as u32;
        hull_verts.push(pt);

        // Create new faces from horizon edges to new vertex
        for (e0, e1) in horizon_edges {
            hull_faces.push([e0, e1, new_vi]);
        }
    }

    // Flatten indices
    let indices: Vec<u32> = hull_faces.iter().flat_map(|f| f.iter().copied()).collect();

    ConvexHull {
        vertices: hull_verts,
        indices,
    }
}

fn find_initial_tetrahedron(points: &[Vec3]) -> (usize, usize, usize, usize) {
    let n = points.len();

    // Find two most distant points (approximate)
    // Start from extremes on X axis
    let mut min_x = 0;
    let mut max_x = 0;
    for i in 1..n {
        if points[i].x < points[min_x].x {
            min_x = i;
        }
        if points[i].x > points[max_x].x {
            max_x = i;
        }
    }
    let mut p0 = min_x;
    let mut p1 = max_x;

    if (points[p0] - points[p1]).length_squared() < 1e-10 {
        // Degenerate: try Y axis
        for i in 1..n {
            if points[i].y < points[p0].y {
                p0 = i;
            }
            if points[i].y > points[p1].y {
                p1 = i;
            }
        }
    }

    // Find point farthest from line p0-p1
    let line_dir = (points[p1] - points[p0]).normalize_or_zero();
    let mut p2 = 0;
    let mut max_line_dist = 0.0f32;
    for i in 0..n {
        if i == p0 || i == p1 {
            continue;
        }
        let v = points[i] - points[p0];
        let proj = v.dot(line_dir);
        let perp = v - line_dir * proj;
        let d = perp.length_squared();
        if d > max_line_dist {
            max_line_dist = d;
            p2 = i;
        }
    }

    // Find point farthest from plane p0-p1-p2
    let plane_normal = (points[p1] - points[p0])
        .cross(points[p2] - points[p0])
        .normalize_or_zero();
    let mut p3 = 0;
    let mut max_plane_dist = 0.0f32;
    for i in 0..n {
        if i == p0 || i == p1 || i == p2 {
            continue;
        }
        let d = (points[i] - points[p0]).dot(plane_normal).abs();
        if d > max_plane_dist {
            max_plane_dist = d;
            p3 = i;
        }
    }

    (p0, p1, p2, p3)
}

/// Generate a simplified collision mesh by vertex clustering
///
/// Reduces vertex count by clustering nearby vertices into grid cells.
/// Good for creating low-poly collision proxies.
pub fn simplify_collision(mesh: &Mesh, grid_resolution: u32) -> CollisionMesh {
    if mesh.vertices.is_empty() {
        return CollisionMesh {
            vertices: Vec::new(),
            indices: Vec::new(),
        };
    }

    let aabb = compute_aabb(mesh);
    let extent = aabb.max - aabb.min;
    let cell_size = extent / grid_resolution as f32;

    // Division Exorcism: pre-compute inverse
    let inv_cell = Vec3::new(
        if cell_size.x > 1e-6 {
            1.0 / cell_size.x
        } else {
            0.0
        },
        if cell_size.y > 1e-6 {
            1.0 / cell_size.y
        } else {
            0.0
        },
        if cell_size.z > 1e-6 {
            1.0 / cell_size.z
        } else {
            0.0
        },
    );

    // Cluster vertices into grid cells
    use std::collections::HashMap;
    let mut clusters: HashMap<(u32, u32, u32), (Vec3, u32)> = HashMap::new();
    let mut vertex_remap: Vec<(u32, u32, u32)> = Vec::with_capacity(mesh.vertices.len());

    for v in &mesh.vertices {
        let rel = v.position - aabb.min;
        let cx = ((rel.x * inv_cell.x) as u32).min(grid_resolution - 1);
        let cy = ((rel.y * inv_cell.y) as u32).min(grid_resolution - 1);
        let cz = ((rel.z * inv_cell.z) as u32).min(grid_resolution - 1);
        let key = (cx, cy, cz);

        let entry = clusters.entry(key).or_insert((Vec3::ZERO, 0));
        entry.0 += v.position;
        entry.1 += 1;
        vertex_remap.push(key);
    }

    // Build new vertex list with averaged positions
    let mut new_vertex_map: HashMap<(u32, u32, u32), u32> = HashMap::new();
    let mut new_vertices = Vec::new();

    for (key, (sum, count)) in &clusters {
        let new_idx = new_vertices.len() as u32;
        new_vertex_map.insert(*key, new_idx);
        // Division Exorcism: multiply by reciprocal
        let inv_count = 1.0 / *count as f32;
        new_vertices.push(*sum * inv_count);
    }

    // Remap indices, skip degenerate triangles
    let mut new_indices = Vec::new();
    let tri_count = mesh.indices.len() / 3;

    for t in 0..tri_count {
        let a = vertex_remap[mesh.indices[t * 3] as usize];
        let b = vertex_remap[mesh.indices[t * 3 + 1] as usize];
        let c = vertex_remap[mesh.indices[t * 3 + 2] as usize];

        let na = new_vertex_map[&a];
        let nb = new_vertex_map[&b];
        let nc = new_vertex_map[&c];

        // Skip degenerate triangles (collapsed vertices)
        if na != nb && nb != nc && na != nc {
            new_indices.push(na);
            new_indices.push(nb);
            new_indices.push(nc);
        }
    }

    CollisionMesh {
        vertices: new_vertices,
        indices: new_indices,
    }
}

/// V-HACD configuration
#[derive(Debug, Clone)]
pub struct VhacdConfig {
    /// Maximum number of convex hulls to generate
    pub max_hulls: u32,
    /// Voxel grid resolution for decomposition
    pub resolution: u32,
    /// Maximum vertices per convex hull
    pub max_vertices_per_hull: u32,
    /// Volume error tolerance (0.0-1.0, lower = more accurate)
    pub volume_error_percent: f32,
}

impl Default for VhacdConfig {
    fn default() -> Self {
        Self {
            max_hulls: 16,
            resolution: 32,
            max_vertices_per_hull: 32,
            volume_error_percent: 1.0,
        }
    }
}

impl VhacdConfig {
    /// High quality decomposition
    pub const fn high_quality() -> Self {
        Self {
            max_hulls: 32,
            resolution: 64,
            max_vertices_per_hull: 64,
            volume_error_percent: 0.5,
        }
    }

    /// Fast decomposition for runtime use
    pub const fn fast() -> Self {
        Self {
            max_hulls: 8,
            resolution: 16,
            max_vertices_per_hull: 16,
            volume_error_percent: 5.0,
        }
    }
}

/// Result of V-HACD decomposition
#[derive(Debug, Clone)]
pub struct ConvexDecomposition {
    /// Individual convex hull parts
    pub parts: Vec<ConvexHull>,
}

impl ConvexDecomposition {
    /// Total number of vertices across all parts
    pub fn total_vertices(&self) -> usize {
        self.parts.iter().map(|p| p.vertices.len()).sum()
    }

    /// Total number of triangles across all parts
    pub fn total_triangles(&self) -> usize {
        self.parts.iter().map(|p| p.indices.len() / 3).sum()
    }
}

/// Approximate Convex Decomposition using voxelization (Deep Fried v2)
///
/// Decomposes a concave mesh into multiple convex parts using a
/// voxel-based flood fill approach. Suitable for physics collision.
///
/// [Deep Fried v2] Voxelization is parallelized via `rayon` + `AtomicU8`.
pub fn convex_decomposition(mesh: &Mesh, config: &VhacdConfig) -> ConvexDecomposition {
    if mesh.vertices.is_empty() || mesh.indices.is_empty() {
        return ConvexDecomposition { parts: Vec::new() };
    }

    let aabb = compute_aabb(mesh);
    let extent = aabb.max - aabb.min;
    let res = config.resolution;

    // Pad slightly to avoid edge cases
    let padding = extent * 0.01;
    let vox_min = aabb.min - padding;
    let vox_extent = extent + padding * 2.0;
    let cell_size = vox_extent / res as f32;

    // Step 1: Voxelize the mesh (Deep Fried: parallel)
    let total_voxels = (res * res * res) as usize;
    let voxels_atomic: Vec<AtomicU8> = (0..total_voxels).map(|_| AtomicU8::new(0)).collect();

    voxelize_mesh_parallel(mesh, vox_min, cell_size, res, &voxels_atomic);

    // Convert atomics to bool vec for flood fill
    let mut voxels: Vec<bool> = voxels_atomic
        .iter()
        .map(|a| a.load(Ordering::Relaxed) != 0)
        .collect();

    // Interior fill (serial — ray parity requires sequential scan)
    interior_fill(&mut voxels, res);

    // Step 2: Flood-fill to find connected components (convex regions)
    let mut labels = vec![0u32; total_voxels];
    let mut current_label = 0u32;
    let max_labels = config.max_hulls;

    for idx in 0..total_voxels {
        if voxels[idx] && labels[idx] == 0 {
            current_label += 1;
            if current_label > max_labels {
                flood_fill_3d(&voxels, &mut labels, idx, max_labels, res);
            } else {
                flood_fill_3d(&voxels, &mut labels, idx, current_label, res);
            }
        }
    }

    let num_labels = current_label.min(max_labels);
    if num_labels == 0 {
        return ConvexDecomposition { parts: Vec::new() };
    }

    // Step 3: For each label, collect voxel centers and compute convex hull
    let mut parts = Vec::with_capacity(num_labels as usize);

    for label in 1..=num_labels {
        let points: Vec<Vec3> = (0..total_voxels)
            .into_par_iter()
            .filter(|&idx| labels[idx] == label)
            .map(|idx| {
                let x = idx % res as usize;
                let y = (idx / res as usize) % res as usize;
                let z = idx / (res as usize * res as usize);
                vox_min
                    + Vec3::new(
                        (x as f32 + 0.5) * cell_size.x,
                        (y as f32 + 0.5) * cell_size.y,
                        (z as f32 + 0.5) * cell_size.z,
                    )
            })
            .collect();

        if points.len() < 4 {
            continue;
        }

        // Compute convex hull of voxel centers
        let mut hull = convex_hull_from_points(&points);

        // Limit vertex count per hull
        if hull.vertices.len() > config.max_vertices_per_hull as usize {
            hull = simplify_hull(&hull, config.max_vertices_per_hull);
        }

        if hull.vertices.len() >= 4 && hull.indices.len() >= 12 {
            parts.push(hull);
        }
    }

    ConvexDecomposition { parts }
}

/// [Deep Fried v2] Parallel voxelization using AtomicU8
///
/// Each triangle is processed independently in parallel, marking voxels
/// covered by its bounding box using atomic stores.
fn voxelize_mesh_parallel(
    mesh: &Mesh,
    vox_min: Vec3,
    cell_size: Vec3,
    res: u32,
    voxels: &[AtomicU8],
) {
    // Division Exorcism: pre-compute inverse
    let inv_cell = Vec3::new(
        if cell_size.x > 1e-8 {
            1.0 / cell_size.x
        } else {
            0.0
        },
        if cell_size.y > 1e-8 {
            1.0 / cell_size.y
        } else {
            0.0
        },
        if cell_size.z > 1e-8 {
            1.0 / cell_size.z
        } else {
            0.0
        },
    );

    let tri_count = mesh.indices.len() / 3;
    let indices = &mesh.indices;
    let vertices = &mesh.vertices;

    (0..tri_count).into_par_iter().for_each(|t| {
        let p0 = vertices[indices[t * 3] as usize].position;
        let p1 = vertices[indices[t * 3 + 1] as usize].position;
        let p2 = vertices[indices[t * 3 + 2] as usize].position;

        // Compute triangle AABB in voxel coordinates
        let tri_min = p0.min(p1).min(p2);
        let tri_max = p0.max(p1).max(p2);

        let vmin = ((tri_min - vox_min) * inv_cell).max(Vec3::ZERO);
        let vmax = ((tri_max - vox_min) * inv_cell).min(Vec3::splat(res as f32 - 1.0));

        let ix0 = vmin.x as u32;
        let iy0 = vmin.y as u32;
        let iz0 = vmin.z as u32;
        let ix1 = (vmax.x as u32).min(res - 1);
        let iy1 = (vmax.y as u32).min(res - 1);
        let iz1 = (vmax.z as u32).min(res - 1);

        // Mark voxels covered by triangle bounding box
        for z in iz0..=iz1 {
            for y in iy0..=iy1 {
                for x in ix0..=ix1 {
                    let idx = (x + y * res + z * res * res) as usize;
                    if idx < voxels.len() {
                        voxels[idx].store(1, Ordering::Relaxed);
                    }
                }
            }
        }
    });
}

/// Interior fill: for each Y-Z slice, fill interior using ray parity
fn interior_fill(voxels: &mut [bool], res: u32) {
    for z in 0..res {
        for y in 0..res {
            let mut inside = false;
            let mut last_was_solid = false;
            for x in 0..res {
                let idx = (x + y * res + z * res * res) as usize;
                if voxels[idx] {
                    if !last_was_solid {
                        inside = !inside;
                    }
                    last_was_solid = true;
                } else {
                    last_was_solid = false;
                    if inside {
                        voxels[idx] = true;
                    }
                }
            }
        }
    }
}

/// 3D flood fill for label assignment
fn flood_fill_3d(voxels: &[bool], labels: &mut [u32], start: usize, label: u32, res: u32) {
    let mut stack = vec![start];
    let res_usize = res as usize;

    while let Some(idx) = stack.pop() {
        if idx >= voxels.len() || !voxels[idx] || labels[idx] != 0 {
            continue;
        }

        labels[idx] = label;

        let x = idx % res_usize;
        let y = (idx / res_usize) % res_usize;
        let z = idx / (res_usize * res_usize);

        // 6-connected neighbors
        if x > 0 {
            stack.push(idx - 1);
        }
        if x + 1 < res_usize {
            stack.push(idx + 1);
        }
        if y > 0 {
            stack.push(idx - res_usize);
        }
        if y + 1 < res_usize {
            stack.push(idx + res_usize);
        }
        if z > 0 {
            stack.push(idx - res_usize * res_usize);
        }
        if z + 1 < res_usize {
            stack.push(idx + res_usize * res_usize);
        }
    }
}

/// Simplify a convex hull to have at most max_vertices (Deep Fried v2)
///
/// [Deep Fried v2] Nearest-neighbor distance search parallelized via `rayon`.
fn simplify_hull(hull: &ConvexHull, max_vertices: u32) -> ConvexHull {
    if hull.vertices.len() <= max_vertices as usize {
        return hull.clone();
    }

    // Iteratively remove the vertex closest to its neighbors' centroid
    let mut points = hull.vertices.clone();

    while points.len() > max_vertices as usize && points.len() > 4 {
        // [Deep Fried v2] Parallel nearest-neighbor distance computation
        let nearest_dists: Vec<(usize, f32)> = points
            .par_iter()
            .enumerate()
            .map(|(i, &p)| {
                let mut nearest_dist = f32::MAX;
                for (j, &q) in points.iter().enumerate() {
                    if i != j {
                        let d = (p - q).length_squared();
                        if d < nearest_dist {
                            nearest_dist = d;
                        }
                    }
                }
                (i, nearest_dist)
            })
            .collect();

        // Find vertex with smallest contribution
        let min_idx = nearest_dists
            .iter()
            .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
            .map(|&(i, _)| i)
            .unwrap_or(0);

        points.swap_remove(min_idx);
    }

    convex_hull_from_points(&points)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mesh::{sdf_to_mesh, MarchingCubesConfig};
    use crate::types::SdfNode;

    #[test]
    fn test_compute_aabb() {
        let sphere = SdfNode::sphere(1.0);
        let config = MarchingCubesConfig {
            resolution: 8,
            iso_level: 0.0,
            compute_normals: true,
            ..Default::default()
        };
        let mesh = sdf_to_mesh(&sphere, Vec3::splat(-2.0), Vec3::splat(2.0), &config);

        let aabb = compute_aabb(&mesh);
        assert!(aabb.min.x < 0.0);
        assert!(aabb.max.x > 0.0);
        assert!(aabb.volume() > 0.0);
        assert!(aabb.contains(Vec3::ZERO));
    }

    #[test]
    fn test_bounding_sphere() {
        let sphere = SdfNode::sphere(1.0);
        let config = MarchingCubesConfig {
            resolution: 16,
            iso_level: 0.0,
            compute_normals: true,
            ..Default::default()
        };
        let mesh = sdf_to_mesh(&sphere, Vec3::splat(-2.0), Vec3::splat(2.0), &config);

        let bs = compute_bounding_sphere(&mesh);
        // Center should be near origin
        assert!(bs.center.length() < 0.3);
        // Radius should be near 1.0
        assert!((bs.radius - 1.0).abs() < 0.5);
        assert!(bs.contains(Vec3::ZERO));
    }

    #[test]
    fn test_convex_hull() {
        let sphere = SdfNode::sphere(1.0);
        let config = MarchingCubesConfig {
            resolution: 8,
            iso_level: 0.0,
            compute_normals: true,
            ..Default::default()
        };
        let mesh = sdf_to_mesh(&sphere, Vec3::splat(-2.0), Vec3::splat(2.0), &config);

        let hull = compute_convex_hull(&mesh);
        assert!(hull.vertices.len() >= 4);
        assert!(hull.indices.len() >= 12); // At least 4 triangles
    }

    #[test]
    fn test_simplify_collision() {
        let sphere = SdfNode::sphere(1.0);
        let config = MarchingCubesConfig {
            resolution: 16,
            iso_level: 0.0,
            compute_normals: true,
            ..Default::default()
        };
        let mesh = sdf_to_mesh(&sphere, Vec3::splat(-2.0), Vec3::splat(2.0), &config);

        let simplified = simplify_collision(&mesh, 8);

        // Should have fewer vertices than original
        assert!(simplified.vertices.len() < mesh.vertices.len());
        assert!(!simplified.indices.is_empty());
    }

    #[test]
    fn test_convex_hull_cube_points() {
        let points = vec![
            Vec3::new(-1.0, -1.0, -1.0),
            Vec3::new(1.0, -1.0, -1.0),
            Vec3::new(-1.0, 1.0, -1.0),
            Vec3::new(1.0, 1.0, -1.0),
            Vec3::new(-1.0, -1.0, 1.0),
            Vec3::new(1.0, -1.0, 1.0),
            Vec3::new(-1.0, 1.0, 1.0),
            Vec3::new(1.0, 1.0, 1.0),
            // Interior point (should not appear in hull)
            Vec3::ZERO,
        ];

        let hull = convex_hull_from_points(&points);
        // Hull should have 8 vertices (cube corners)
        assert_eq!(hull.vertices.len(), 8);
        // Hull should have 12 triangles (6 faces * 2)
        assert_eq!(hull.indices.len(), 36);
    }

    #[test]
    fn test_convex_decomposition_basic() {
        // Sphere should decompose to ~1 convex part
        let sphere = SdfNode::sphere(1.0);
        let config = MarchingCubesConfig {
            resolution: 16,
            iso_level: 0.0,
            compute_normals: true,
            ..Default::default()
        };
        let mesh = sdf_to_mesh(&sphere, Vec3::splat(-2.0), Vec3::splat(2.0), &config);

        let decomp = convex_decomposition(&mesh, &VhacdConfig::default());
        assert!(
            !decomp.parts.is_empty(),
            "Should produce at least 1 convex part"
        );
        assert!(decomp.total_vertices() > 0);
        assert!(decomp.total_triangles() > 0);
    }

    #[test]
    fn test_convex_decomposition_concave() {
        // Subtract box from sphere -> concave shape
        let shape = SdfNode::sphere(1.0).subtract(SdfNode::box3d(0.5, 0.5, 0.5));
        let config = MarchingCubesConfig {
            resolution: 16,
            iso_level: 0.0,
            compute_normals: true,
            ..Default::default()
        };
        let mesh = sdf_to_mesh(&shape, Vec3::splat(-2.0), Vec3::splat(2.0), &config);

        let decomp = convex_decomposition(&mesh, &VhacdConfig::default());
        // Concave shape should produce multiple parts
        assert!(
            !decomp.parts.is_empty(),
            "Concave shape should produce convex parts, got {}",
            decomp.parts.len()
        );

        // Each part should have valid convex hull
        for (i, part) in decomp.parts.iter().enumerate() {
            assert!(
                part.vertices.len() >= 4,
                "Part {} has only {} vertices",
                i,
                part.vertices.len()
            );
            assert!(
                part.indices.len() >= 12,
                "Part {} has only {} indices",
                i,
                part.indices.len()
            );
        }
    }

    #[test]
    fn test_convex_decomposition_max_hulls() {
        let shape = SdfNode::sphere(1.0).subtract(SdfNode::box3d(0.5, 0.5, 0.5));
        let config = MarchingCubesConfig {
            resolution: 16,
            iso_level: 0.0,
            compute_normals: true,
            ..Default::default()
        };
        let mesh = sdf_to_mesh(&shape, Vec3::splat(-2.0), Vec3::splat(2.0), &config);

        let vhacd_config = VhacdConfig {
            max_hulls: 4,
            ..Default::default()
        };
        let decomp = convex_decomposition(&mesh, &vhacd_config);
        assert!(
            decomp.parts.len() <= 4,
            "Should respect max_hulls limit: got {}",
            decomp.parts.len()
        );
    }
}