proof-engine 0.2.1

Real-time graphics from math: glyphs and particles moved by ODEs, strange attractors and force fields, drawn with HDR bloom on OpenGL.
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
use glam::{Vec3, Vec4, UVec3, IVec3};
use std::collections::VecDeque;

/// Axis-aligned bounding box.
#[derive(Debug, Clone, Copy)]
pub struct Aabb {
    pub min: Vec3,
    pub max: Vec3,
}

impl Aabb {
    pub fn new(min: Vec3, max: Vec3) -> Self {
        Self { min, max }
    }

    pub fn center(&self) -> Vec3 {
        (self.min + self.max) * 0.5
    }

    pub fn size(&self) -> Vec3 {
        self.max - self.min
    }

    pub fn half_size(&self) -> Vec3 {
        self.size() * 0.5
    }

    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
    }

    pub fn intersects(&self, other: &Aabb) -> bool {
        self.min.x <= other.max.x && self.max.x >= other.min.x
            && self.min.y <= other.max.y && self.max.y >= other.min.y
            && self.min.z <= other.max.z && self.max.z >= other.min.z
    }

    /// Return the child octant bounding box (0..7).
    pub fn subdivide_octant(&self, octant: u8) -> Aabb {
        let c = self.center();
        let min = Vec3::new(
            if octant & 1 == 0 { self.min.x } else { c.x },
            if octant & 2 == 0 { self.min.y } else { c.y },
            if octant & 4 == 0 { self.min.z } else { c.z },
        );
        let max = Vec3::new(
            if octant & 1 == 0 { c.x } else { self.max.x },
            if octant & 2 == 0 { c.y } else { self.max.y },
            if octant & 4 == 0 { c.z } else { self.max.z },
        );
        Aabb { min, max }
    }

    /// Intersect a ray with this AABB, returning (t_enter, t_exit). None if miss.
    pub fn intersect_ray(&self, origin: Vec3, inv_dir: Vec3) -> Option<(f32, f32)> {
        let t1 = (self.min - origin) * inv_dir;
        let t2 = (self.max - origin) * inv_dir;
        let t_min = t1.min(t2);
        let t_max = t1.max(t2);
        let t_enter = t_min.x.max(t_min.y).max(t_min.z);
        let t_exit = t_max.x.min(t_max.y).min(t_max.z);
        if t_enter <= t_exit && t_exit >= 0.0 {
            Some((t_enter.max(0.0), t_exit))
        } else {
            None
        }
    }
}

/// Data stored per voxel.
#[derive(Debug, Clone, Copy)]
pub struct VoxelData {
    pub radiance: Vec4,
    pub normal: Vec3,
    pub opacity: f32,
    pub sh_coeffs: [f32; 9],
}

impl Default for VoxelData {
    fn default() -> Self {
        Self {
            radiance: Vec4::ZERO,
            normal: Vec3::ZERO,
            opacity: 0.0,
            sh_coeffs: [0.0; 9],
        }
    }
}

impl VoxelData {
    pub fn is_empty(&self) -> bool {
        self.opacity <= 0.0
    }

    pub fn average(datas: &[&VoxelData]) -> VoxelData {
        if datas.is_empty() {
            return VoxelData::default();
        }
        let n = datas.len() as f32;
        let mut result = VoxelData::default();
        for d in datas {
            result.radiance += d.radiance;
            result.normal += d.normal;
            result.opacity += d.opacity;
            for i in 0..9 {
                result.sh_coeffs[i] += d.sh_coeffs[i];
            }
        }
        result.radiance /= n;
        result.normal = if result.normal.length_squared() > 1e-8 {
            result.normal.normalize()
        } else {
            Vec3::ZERO
        };
        result.opacity /= n;
        for i in 0..9 {
            result.sh_coeffs[i] /= n;
        }
        result
    }
}

/// A single node in the sparse voxel octree.
#[derive(Debug, Clone)]
pub struct OctreeNode {
    pub children: [Option<u32>; 8],
    pub data: VoxelData,
    pub level: u8,
    pub morton_code: u64,
}

impl OctreeNode {
    pub fn new_leaf(data: VoxelData, level: u8, morton_code: u64) -> Self {
        Self {
            children: [None; 8],
            data,
            level,
            morton_code,
        }
    }

    pub fn new_internal(level: u8, morton_code: u64) -> Self {
        Self {
            children: [None; 8],
            data: VoxelData::default(),
            level,
            morton_code,
        }
    }

    pub fn is_leaf(&self) -> bool {
        self.children.iter().all(|c| c.is_none())
    }

    pub fn has_children(&self) -> bool {
        self.children.iter().any(|c| c.is_some())
    }
}

/// Morton code encoding: interleave bits of x, y, z into a 64-bit code.
pub fn morton_encode_3d(x: u32, y: u32, z: u32) -> u64 {
    fn split_by_3(mut v: u32) -> u64 {
        let mut v = v as u64 & 0x1fffff; // 21 bits
        v = (v | (v << 32)) & 0x1f00000000ffff;
        v = (v | (v << 16)) & 0x1f0000ff0000ff;
        v = (v | (v << 8))  & 0x100f00f00f00f00f;
        v = (v | (v << 4))  & 0x10c30c30c30c30c3;
        v = (v | (v << 2))  & 0x1249249249249249;
        v
    }
    split_by_3(x) | (split_by_3(y) << 1) | (split_by_3(z) << 2)
}

/// Morton code decoding: extract x, y, z from a 64-bit code.
pub fn morton_decode_3d(code: u64) -> (u32, u32, u32) {
    fn compact_by_3(mut v: u64) -> u32 {
        v &= 0x1249249249249249;
        v = (v | (v >> 2))  & 0x10c30c30c30c30c3;
        v = (v | (v >> 4))  & 0x100f00f00f00f00f;
        v = (v | (v >> 8))  & 0x1f0000ff0000ff;
        v = (v | (v >> 16)) & 0x1f00000000ffff;
        v = (v | (v >> 32)) & 0x1fffff;
        v as u32
    }
    (compact_by_3(code), compact_by_3(code >> 1), compact_by_3(code >> 2))
}

/// Dense 3D voxel grid.
#[derive(Debug, Clone)]
pub struct VoxelGrid {
    pub data: Vec<VoxelData>,
    pub resolution: UVec3,
}

impl VoxelGrid {
    pub fn new(resolution: UVec3) -> Self {
        let count = (resolution.x * resolution.y * resolution.z) as usize;
        Self {
            data: vec![VoxelData::default(); count],
            resolution,
        }
    }

    pub fn index(&self, x: u32, y: u32, z: u32) -> usize {
        (z * self.resolution.y * self.resolution.x + y * self.resolution.x + x) as usize
    }

    pub fn get(&self, x: u32, y: u32, z: u32) -> &VoxelData {
        &self.data[self.index(x, y, z)]
    }

    pub fn get_mut(&mut self, x: u32, y: u32, z: u32) -> &mut VoxelData {
        let idx = self.index(x, y, z);
        &mut self.data[idx]
    }

    pub fn set(&mut self, x: u32, y: u32, z: u32, data: VoxelData) {
        let idx = self.index(x, y, z);
        self.data[idx] = data;
    }

    pub fn in_bounds(&self, x: i32, y: i32, z: i32) -> bool {
        x >= 0 && y >= 0 && z >= 0
            && (x as u32) < self.resolution.x
            && (y as u32) < self.resolution.y
            && (z as u32) < self.resolution.z
    }

    pub fn filled_count(&self) -> usize {
        self.data.iter().filter(|v| !v.is_empty()).count()
    }
}

/// The sparse voxel octree.
#[derive(Debug, Clone)]
pub struct SparseVoxelOctree {
    pub nodes: Vec<OctreeNode>,
    pub root: u32,
    pub max_depth: u8,
    pub world_bounds: Aabb,
}

impl SparseVoxelOctree {
    pub fn new(world_bounds: Aabb, max_depth: u8) -> Self {
        let root_node = OctreeNode::new_internal(0, 0);
        Self {
            nodes: vec![root_node],
            root: 0,
            max_depth,
            world_bounds,
        }
    }

    /// Determine which octant a point falls into within the given bounds.
    fn octant_for_point(bounds: &Aabb, point: Vec3) -> u8 {
        let c = bounds.center();
        let mut octant = 0u8;
        if point.x >= c.x { octant |= 1; }
        if point.y >= c.y { octant |= 2; }
        if point.z >= c.z { octant |= 4; }
        octant
    }

    /// Insert a voxel at the given world position, creating intermediate nodes as needed.
    pub fn insert(&mut self, position: Vec3, data: VoxelData) {
        if !self.world_bounds.contains(position) {
            return;
        }
        let max_depth = self.max_depth;
        self.insert_recursive(self.root, self.world_bounds, position, data, 0, max_depth);
    }

    fn insert_recursive(
        &mut self,
        node_idx: u32,
        bounds: Aabb,
        position: Vec3,
        data: VoxelData,
        depth: u8,
        max_depth: u8,
    ) {
        if depth >= max_depth {
            self.nodes[node_idx as usize].data = data;
            return;
        }

        let octant = Self::octant_for_point(&bounds, position);
        let child_bounds = bounds.subdivide_octant(octant);

        let child_idx = if let Some(idx) = self.nodes[node_idx as usize].children[octant as usize] {
            idx
        } else {
            let idx = self.nodes.len() as u32;
            let morton = morton_encode_3d(
                ((position.x - self.world_bounds.min.x) / self.world_bounds.size().x * ((1u32 << max_depth) as f32)) as u32,
                ((position.y - self.world_bounds.min.y) / self.world_bounds.size().y * ((1u32 << max_depth) as f32)) as u32,
                ((position.z - self.world_bounds.min.z) / self.world_bounds.size().z * ((1u32 << max_depth) as f32)) as u32,
            );
            let new_node = OctreeNode::new_internal(depth + 1, morton);
            self.nodes.push(new_node);
            self.nodes[node_idx as usize].children[octant as usize] = Some(idx);
            idx
        };

        self.insert_recursive(child_idx, child_bounds, position, data, depth + 1, max_depth);
    }

    /// Look up voxel data at a world position, at the given LOD level.
    /// Level 0 = root (coarsest), max_depth = leaf (finest).
    pub fn lookup(&self, position: Vec3, level: u8) -> Option<&VoxelData> {
        if !self.world_bounds.contains(position) {
            return None;
        }
        self.lookup_recursive(self.root, self.world_bounds, position, 0, level)
    }

    fn lookup_recursive(
        &self,
        node_idx: u32,
        bounds: Aabb,
        position: Vec3,
        depth: u8,
        target_level: u8,
    ) -> Option<&VoxelData> {
        let node = &self.nodes[node_idx as usize];

        if depth >= target_level || node.is_leaf() {
            if node.data.is_empty() && node.is_leaf() && depth < target_level {
                return None;
            }
            return Some(&node.data);
        }

        let octant = Self::octant_for_point(&bounds, position);
        if let Some(child_idx) = node.children[octant as usize] {
            let child_bounds = bounds.subdivide_octant(octant);
            self.lookup_recursive(child_idx, child_bounds, position, depth + 1, target_level)
        } else {
            // No child at this octant; return this node's data if non-empty
            if !node.data.is_empty() {
                Some(&node.data)
            } else {
                None
            }
        }
    }

    /// Remove a voxel at the given position (clear leaf data).
    pub fn remove(&mut self, position: Vec3) {
        if !self.world_bounds.contains(position) {
            return;
        }
        let max_depth = self.max_depth;
        self.remove_recursive(self.root, self.world_bounds, position, 0, max_depth);
    }

    fn remove_recursive(
        &mut self,
        node_idx: u32,
        bounds: Aabb,
        position: Vec3,
        depth: u8,
        max_depth: u8,
    ) -> bool {
        if depth >= max_depth {
            self.nodes[node_idx as usize].data = VoxelData::default();
            return true; // node is now empty
        }

        let octant = Self::octant_for_point(&bounds, position);
        let child_idx = match self.nodes[node_idx as usize].children[octant as usize] {
            Some(idx) => idx,
            None => return false,
        };

        let child_bounds = bounds.subdivide_octant(octant);
        let child_empty = self.remove_recursive(child_idx, child_bounds, position, depth + 1, max_depth);

        if child_empty {
            let child_node = &self.nodes[child_idx as usize];
            if child_node.is_leaf() && child_node.data.is_empty() {
                self.nodes[node_idx as usize].children[octant as usize] = None;
            }
        }

        // Prune: check if all children are None
        self.nodes[node_idx as usize].children.iter().all(|c| c.is_none())
            && self.nodes[node_idx as usize].data.is_empty()
    }

    /// Traverse a ray through the octree, collecting hits.
    pub fn traverse_ray(&self, origin: Vec3, direction: Vec3, max_t: f32) -> Vec<(Vec3, VoxelData, f32)> {
        let mut results = Vec::new();
        let dir_safe = Vec3::new(
            if direction.x.abs() < 1e-8 { 1e-8 } else { direction.x },
            if direction.y.abs() < 1e-8 { 1e-8 } else { direction.y },
            if direction.z.abs() < 1e-8 { 1e-8 } else { direction.z },
        );
        let inv_dir = Vec3::new(1.0 / dir_safe.x, 1.0 / dir_safe.y, 1.0 / dir_safe.z);

        self.traverse_ray_recursive(
            self.root,
            self.world_bounds,
            origin,
            direction,
            inv_dir,
            max_t,
            &mut results,
        );

        results.sort_by(|a, b| a.2.partial_cmp(&b.2).unwrap_or(std::cmp::Ordering::Equal));
        results
    }

    fn traverse_ray_recursive(
        &self,
        node_idx: u32,
        bounds: Aabb,
        origin: Vec3,
        direction: Vec3,
        inv_dir: Vec3,
        max_t: f32,
        results: &mut Vec<(Vec3, VoxelData, f32)>,
    ) {
        let hit = bounds.intersect_ray(origin, inv_dir);
        let (t_enter, t_exit) = match hit {
            Some((te, tx)) if te <= max_t => (te, tx),
            _ => return,
        };

        let node = &self.nodes[node_idx as usize];

        if node.is_leaf() {
            if !node.data.is_empty() {
                let hit_point = origin + direction * t_enter;
                results.push((hit_point, node.data, t_enter));
            }
            return;
        }

        // Traverse children sorted by t_enter for front-to-back ordering
        let mut child_order: Vec<(u8, f32)> = Vec::new();
        for octant in 0..8u8 {
            if let Some(child_idx) = node.children[octant as usize] {
                let child_bounds = bounds.subdivide_octant(octant);
                if let Some((te, _)) = child_bounds.intersect_ray(origin, inv_dir) {
                    if te <= max_t {
                        child_order.push((octant, te));
                    }
                }
            }
        }
        child_order.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

        for (octant, _) in child_order {
            let child_idx = self.nodes[node_idx as usize].children[octant as usize].unwrap();
            let child_bounds = bounds.subdivide_octant(octant);
            self.traverse_ray_recursive(child_idx, child_bounds, origin, direction, inv_dir, max_t, results);
        }
    }

    /// Average child voxels into parent for LOD (mipmap). Process the given level.
    pub fn mipmap_level(&mut self, level: u8) {
        // Collect nodes at the given level that have children
        let indices: Vec<u32> = (0..self.nodes.len() as u32)
            .filter(|&i| {
                let n = &self.nodes[i as usize];
                n.level == level && n.has_children()
            })
            .collect();

        for idx in indices {
            let children_indices: Vec<u32> = self.nodes[idx as usize]
                .children
                .iter()
                .filter_map(|c| *c)
                .collect();

            if children_indices.is_empty() {
                continue;
            }

            let child_datas: Vec<&VoxelData> = children_indices
                .iter()
                .map(|&ci| &self.nodes[ci as usize].data)
                .filter(|d| !d.is_empty())
                .collect();

            if !child_datas.is_empty() {
                self.nodes[idx as usize].data = VoxelData::average(&child_datas);
            }
        }
    }

    /// Build the full mipmap chain from leaves up to root.
    pub fn build_mipmaps(&mut self) {
        if self.max_depth == 0 {
            return;
        }
        for level in (0..self.max_depth).rev() {
            self.mipmap_level(level);
        }
    }

    /// Build a sparse voxel octree from a dense grid.
    pub fn build_from_voxel_grid(grid: &VoxelGrid, world_bounds: Aabb) -> SparseVoxelOctree {
        let max_dim = grid.resolution.x.max(grid.resolution.y).max(grid.resolution.z);
        let max_depth = (max_dim as f32).log2().ceil() as u8;
        let max_depth = max_depth.max(1);

        let mut octree = SparseVoxelOctree::new(world_bounds, max_depth);
        let voxel_size = world_bounds.size() / Vec3::new(
            grid.resolution.x as f32,
            grid.resolution.y as f32,
            grid.resolution.z as f32,
        );

        for z in 0..grid.resolution.z {
            for y in 0..grid.resolution.y {
                for x in 0..grid.resolution.x {
                    let voxel = grid.get(x, y, z);
                    if !voxel.is_empty() {
                        let pos = world_bounds.min + Vec3::new(
                            (x as f32 + 0.5) * voxel_size.x,
                            (y as f32 + 0.5) * voxel_size.y,
                            (z as f32 + 0.5) * voxel_size.z,
                        );
                        octree.insert(pos, *voxel);
                    }
                }
            }
        }

        octree.build_mipmaps();
        octree
    }

    /// Total memory usage in bytes (approximate).
    pub fn memory_usage(&self) -> usize {
        self.nodes.len() * std::mem::size_of::<OctreeNode>()
    }

    /// Total number of nodes.
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Count of leaf nodes (no children).
    pub fn leaf_count(&self) -> usize {
        self.nodes.iter().filter(|n| n.is_leaf()).count()
    }

    /// Maximum depth actually present.
    pub fn depth(&self) -> u8 {
        self.nodes.iter().map(|n| n.level).max().unwrap_or(0)
    }

    /// Iterate all occupied leaf voxels.
    pub fn iter_leaves(&self) -> OctreeIterator<'_> {
        OctreeIterator {
            octree: self,
            stack: vec![(self.root, self.world_bounds)],
        }
    }

    /// Sample the octree at a world position with fractional LOD.
    /// Interpolates between two integer LOD levels.
    pub fn sample_lod(&self, position: Vec3, lod: f32) -> Option<VoxelData> {
        let lod_low = lod.floor() as u8;
        let lod_high = lod.ceil() as u8;
        let frac = lod - lod.floor();

        let data_low = self.lookup(position, lod_low);
        let data_high = self.lookup(position, lod_high);

        match (data_low, data_high) {
            (Some(a), Some(b)) => {
                let mut result = VoxelData::default();
                result.radiance = a.radiance * (1.0 - frac) + b.radiance * frac;
                result.normal = if (a.normal * (1.0 - frac) + b.normal * frac).length_squared() > 1e-8 {
                    (a.normal * (1.0 - frac) + b.normal * frac).normalize()
                } else {
                    Vec3::ZERO
                };
                result.opacity = a.opacity * (1.0 - frac) + b.opacity * frac;
                for i in 0..9 {
                    result.sh_coeffs[i] = a.sh_coeffs[i] * (1.0 - frac) + b.sh_coeffs[i] * frac;
                }
                Some(result)
            }
            (Some(a), None) => Some(*a),
            (None, Some(b)) => Some(*b),
            (None, None) => None,
        }
    }
}

/// Iterator over all occupied leaf voxels.
pub struct OctreeIterator<'a> {
    octree: &'a SparseVoxelOctree,
    stack: Vec<(u32, Aabb)>,
}

impl<'a> Iterator for OctreeIterator<'a> {
    type Item = (Vec3, &'a VoxelData);

    fn next(&mut self) -> Option<Self::Item> {
        while let Some((node_idx, bounds)) = self.stack.pop() {
            let node = &self.octree.nodes[node_idx as usize];

            if node.is_leaf() {
                if !node.data.is_empty() {
                    return Some((bounds.center(), &node.data));
                }
                continue;
            }

            for octant in (0..8u8).rev() {
                if let Some(child_idx) = node.children[octant as usize] {
                    self.stack.push((child_idx, bounds.subdivide_octant(octant)));
                }
            }
        }
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_aabb_basics() {
        let aabb = Aabb::new(Vec3::ZERO, Vec3::splat(10.0));
        assert_eq!(aabb.center(), Vec3::splat(5.0));
        assert_eq!(aabb.size(), Vec3::splat(10.0));
        assert!(aabb.contains(Vec3::splat(5.0)));
        assert!(!aabb.contains(Vec3::splat(11.0)));
    }

    #[test]
    fn test_aabb_intersects() {
        let a = Aabb::new(Vec3::ZERO, Vec3::splat(5.0));
        let b = Aabb::new(Vec3::splat(3.0), Vec3::splat(8.0));
        let c = Aabb::new(Vec3::splat(6.0), Vec3::splat(10.0));
        assert!(a.intersects(&b));
        assert!(!a.intersects(&c));
    }

    #[test]
    fn test_morton_roundtrip() {
        for x in 0..16u32 {
            for y in 0..16u32 {
                for z in 0..16u32 {
                    let code = morton_encode_3d(x, y, z);
                    let (dx, dy, dz) = morton_decode_3d(code);
                    assert_eq!((x, y, z), (dx, dy, dz), "Morton roundtrip failed for ({x},{y},{z})");
                }
            }
        }
    }

    #[test]
    fn test_morton_ordering() {
        // Morton codes should maintain Z-curve ordering
        let c1 = morton_encode_3d(0, 0, 0);
        let c2 = morton_encode_3d(1, 0, 0);
        let c3 = morton_encode_3d(0, 1, 0);
        assert!(c1 < c2);
        assert!(c1 < c3);
    }

    #[test]
    fn test_insert_and_lookup() {
        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(8.0));
        let mut octree = SparseVoxelOctree::new(bounds, 3);

        let data = VoxelData {
            radiance: Vec4::new(1.0, 0.0, 0.0, 1.0),
            normal: Vec3::Y,
            opacity: 1.0,
            sh_coeffs: [0.0; 9],
        };

        octree.insert(Vec3::new(1.0, 1.0, 1.0), data);

        let result = octree.lookup(Vec3::new(1.0, 1.0, 1.0), 3);
        assert!(result.is_some());
        let r = result.unwrap();
        assert!((r.radiance.x - 1.0).abs() < 1e-5);
        assert!((r.opacity - 1.0).abs() < 1e-5);
    }

    #[test]
    fn test_insert_multiple_and_lookup() {
        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(8.0));
        let mut octree = SparseVoxelOctree::new(bounds, 3);

        let data_red = VoxelData {
            radiance: Vec4::new(1.0, 0.0, 0.0, 1.0),
            normal: Vec3::Y,
            opacity: 1.0,
            sh_coeffs: [0.0; 9],
        };
        let data_blue = VoxelData {
            radiance: Vec4::new(0.0, 0.0, 1.0, 1.0),
            normal: Vec3::X,
            opacity: 0.5,
            sh_coeffs: [0.0; 9],
        };

        octree.insert(Vec3::new(1.0, 1.0, 1.0), data_red);
        octree.insert(Vec3::new(6.0, 6.0, 6.0), data_blue);

        let r1 = octree.lookup(Vec3::new(1.0, 1.0, 1.0), 3).unwrap();
        assert!((r1.radiance.x - 1.0).abs() < 1e-5);

        let r2 = octree.lookup(Vec3::new(6.0, 6.0, 6.0), 3).unwrap();
        assert!((r2.radiance.z - 1.0).abs() < 1e-5);
    }

    #[test]
    fn test_remove() {
        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(8.0));
        let mut octree = SparseVoxelOctree::new(bounds, 3);

        let data = VoxelData {
            radiance: Vec4::ONE,
            normal: Vec3::Y,
            opacity: 1.0,
            sh_coeffs: [0.0; 9],
        };

        octree.insert(Vec3::new(2.0, 2.0, 2.0), data);
        assert!(octree.lookup(Vec3::new(2.0, 2.0, 2.0), 3).is_some());

        octree.remove(Vec3::new(2.0, 2.0, 2.0));
        // After remove, the data should be empty
        let r = octree.lookup(Vec3::new(2.0, 2.0, 2.0), 3);
        match r {
            Some(d) => assert!(d.is_empty()),
            None => {} // also acceptable
        }
    }

    #[test]
    fn test_ray_traversal() {
        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(8.0));
        let mut octree = SparseVoxelOctree::new(bounds, 3);

        let data = VoxelData {
            radiance: Vec4::new(1.0, 0.0, 0.0, 1.0),
            normal: Vec3::Z,
            opacity: 1.0,
            sh_coeffs: [0.0; 9],
        };

        // Place a voxel at roughly (4, 4, 4)
        octree.insert(Vec3::new(4.0, 4.0, 4.0), data);

        // Shoot ray along Z towards it
        let hits = octree.traverse_ray(
            Vec3::new(4.0, 4.0, 0.0),
            Vec3::new(0.0, 0.0, 1.0),
            20.0,
        );
        assert!(!hits.is_empty(), "Ray should hit the voxel");
        assert!((hits[0].1.radiance.x - 1.0).abs() < 1e-5);
    }

    #[test]
    fn test_ray_miss() {
        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(8.0));
        let mut octree = SparseVoxelOctree::new(bounds, 3);

        let data = VoxelData {
            radiance: Vec4::ONE,
            normal: Vec3::Y,
            opacity: 1.0,
            sh_coeffs: [0.0; 9],
        };
        octree.insert(Vec3::new(1.0, 1.0, 1.0), data);

        // Shoot ray that misses entirely
        let hits = octree.traverse_ray(
            Vec3::new(7.0, 7.0, 0.0),
            Vec3::new(0.0, 0.0, 1.0),
            20.0,
        );
        // The ray goes through a different octant, should not hit
        // (it may or may not depending on resolution, but at least check it runs)
        let _ = hits;
    }

    #[test]
    fn test_mipmap_averaging() {
        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(8.0));
        let mut octree = SparseVoxelOctree::new(bounds, 2);

        let data = VoxelData {
            radiance: Vec4::new(1.0, 0.0, 0.0, 1.0),
            normal: Vec3::Y,
            opacity: 1.0,
            sh_coeffs: [0.0; 9],
        };

        octree.insert(Vec3::new(1.0, 1.0, 1.0), data);
        octree.insert(Vec3::new(3.0, 1.0, 1.0), data);

        octree.build_mipmaps();

        // Parent should have averaged radiance
        let parent = octree.lookup(Vec3::new(2.0, 1.0, 1.0), 1);
        assert!(parent.is_some());
    }

    #[test]
    fn test_build_from_grid() {
        let mut grid = VoxelGrid::new(UVec3::new(4, 4, 4));
        grid.set(1, 1, 1, VoxelData {
            radiance: Vec4::new(0.5, 0.5, 0.0, 1.0),
            normal: Vec3::Y,
            opacity: 1.0,
            sh_coeffs: [0.0; 9],
        });

        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(4.0));
        let octree = SparseVoxelOctree::build_from_voxel_grid(&grid, bounds);

        assert!(octree.node_count() > 1);
        assert!(octree.leaf_count() > 0);
    }

    #[test]
    fn test_iterator() {
        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(8.0));
        let mut octree = SparseVoxelOctree::new(bounds, 3);

        let data = VoxelData {
            radiance: Vec4::ONE,
            normal: Vec3::Y,
            opacity: 1.0,
            sh_coeffs: [0.0; 9],
        };

        octree.insert(Vec3::new(1.0, 1.0, 1.0), data);
        octree.insert(Vec3::new(6.0, 6.0, 6.0), data);

        let leaves: Vec<_> = octree.iter_leaves().collect();
        assert_eq!(leaves.len(), 2);
    }

    #[test]
    fn test_voxel_grid() {
        let mut grid = VoxelGrid::new(UVec3::new(4, 4, 4));
        assert_eq!(grid.filled_count(), 0);

        grid.set(0, 0, 0, VoxelData {
            radiance: Vec4::ONE,
            normal: Vec3::Y,
            opacity: 1.0,
            sh_coeffs: [0.0; 9],
        });
        assert_eq!(grid.filled_count(), 1);
        assert!(grid.in_bounds(3, 3, 3));
        assert!(!grid.in_bounds(4, 0, 0));
    }

    #[test]
    fn test_aabb_ray_intersection() {
        let aabb = Aabb::new(Vec3::ZERO, Vec3::splat(2.0));
        let origin = Vec3::new(1.0, 1.0, -5.0);
        let dir = Vec3::new(0.0, 0.0, 1.0);
        let inv_dir = Vec3::new(f32::INFINITY, f32::INFINITY, 1.0);
        // Use safe inv_dir
        let inv_dir = Vec3::new(1.0 / 1e-8, 1.0 / 1e-8, 1.0);
        let hit = aabb.intersect_ray(origin, Vec3::new(0.0, 0.0, 1.0).recip());
        // direction is (0,0,1), recip may be inf, that's fine for the algorithm
        // Just test with non-degenerate ray
        let origin2 = Vec3::new(1.0, 1.0, -2.0);
        let hit2 = aabb.intersect_ray(origin2, Vec3::new(1.0, 1.0, 1.0).recip());
        assert!(hit2.is_some());
    }

    #[test]
    fn test_memory_usage() {
        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(8.0));
        let octree = SparseVoxelOctree::new(bounds, 3);
        assert!(octree.memory_usage() > 0);
    }

    #[test]
    fn test_subdivide_octant_covers_full_volume() {
        let aabb = Aabb::new(Vec3::ZERO, Vec3::splat(8.0));
        for octant in 0..8u8 {
            let child = aabb.subdivide_octant(octant);
            assert!(child.size().x > 0.0);
            assert!(child.size().y > 0.0);
            assert!(child.size().z > 0.0);
            // Child should be half the size
            assert!((child.size().x - 4.0).abs() < 1e-5);
        }
    }

    #[test]
    fn test_sample_lod() {
        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(8.0));
        let mut octree = SparseVoxelOctree::new(bounds, 3);
        let data = VoxelData {
            radiance: Vec4::new(1.0, 0.0, 0.0, 1.0),
            normal: Vec3::Y,
            opacity: 1.0,
            sh_coeffs: [0.0; 9],
        };
        octree.insert(Vec3::new(1.0, 1.0, 1.0), data);
        octree.build_mipmaps();

        let sampled = octree.sample_lod(Vec3::new(1.0, 1.0, 1.0), 2.5);
        assert!(sampled.is_some());
    }

    #[test]
    fn test_lookup_out_of_bounds_returns_none() {
        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(8.0));
        let octree = SparseVoxelOctree::new(bounds, 3);
        assert!(octree.lookup(Vec3::splat(100.0), 3).is_none());
    }

    #[test]
    fn test_insert_out_of_bounds_noop() {
        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(8.0));
        let mut octree = SparseVoxelOctree::new(bounds, 3);
        let initial_count = octree.node_count();
        octree.insert(Vec3::splat(-5.0), VoxelData {
            radiance: Vec4::ONE,
            normal: Vec3::Y,
            opacity: 1.0,
            sh_coeffs: [0.0; 9],
        });
        assert_eq!(octree.node_count(), initial_count);
    }

    #[test]
    fn test_depth_tracking() {
        let bounds = Aabb::new(Vec3::ZERO, Vec3::splat(16.0));
        let mut octree = SparseVoxelOctree::new(bounds, 4);
        octree.insert(Vec3::splat(1.0), VoxelData {
            radiance: Vec4::ONE,
            normal: Vec3::Y,
            opacity: 1.0,
            sh_coeffs: [0.0; 9],
        });
        assert!(octree.depth() >= 1);
        assert!(octree.depth() <= 4);
    }

    #[test]
    fn test_voxel_data_average() {
        let a = VoxelData {
            radiance: Vec4::new(1.0, 0.0, 0.0, 1.0),
            normal: Vec3::Y,
            opacity: 1.0,
            sh_coeffs: [1.0; 9],
        };
        let b = VoxelData {
            radiance: Vec4::new(0.0, 1.0, 0.0, 1.0),
            normal: Vec3::Y,
            opacity: 0.5,
            sh_coeffs: [0.0; 9],
        };
        let avg = VoxelData::average(&[&a, &b]);
        assert!((avg.radiance.x - 0.5).abs() < 1e-5);
        assert!((avg.radiance.y - 0.5).abs() < 1e-5);
        assert!((avg.opacity - 0.75).abs() < 1e-5);
    }

    #[test]
    fn test_morton_large_values() {
        let code = morton_encode_3d(1000, 2000, 3000);
        let (x, y, z) = morton_decode_3d(code);
        assert_eq!((x, y, z), (1000, 2000, 3000));
    }

    #[test]
    fn test_grid_index_consistency() {
        let grid = VoxelGrid::new(UVec3::new(8, 8, 8));
        let idx1 = grid.index(0, 0, 0);
        let idx2 = grid.index(7, 7, 7);
        assert_eq!(idx1, 0);
        assert_eq!(idx2, 511);
    }
}