alice-sdf 1.9.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 available via GitHub path dep only in 1.7.7 (scheduled restoration in 1.8.0 once transitive deps publish).
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
//! SVO Streaming: Chunk-based loading with LRU cache (Deep Fried Edition)
//!
//! For large SVO scenes that don't fit in memory, this module provides:
//!
//! - **Chunking**: Split SVO into spatial chunks at a configurable depth
//! - **LRU Cache**: Keep recently-accessed chunks in memory
//! - **On-demand Loading**: Load chunks from disk/network as needed
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────┐
//! │  SvoStreamingCache          │
//! │  ┌──────┐ ┌──────┐ ┌──────┐│
//! │  │Chunk0│ │Chunk3│ │Chunk7││  ← LRU cache (hot chunks)
//! │  └──────┘ └──────┘ └──────┘│
//! │  capacity: N chunks         │
//! └─────────────┬───────────────┘
//!               │ load/evict
//! ┌─────────────▼───────────────┐
//! │  Storage (disk/network)     │
//! │  chunk_0.svo chunk_1.svo ...│
//! └─────────────────────────────┘
//! ```
//!
//! Author: Moroya Sakamoto

use glam::Vec3;

use super::{SparseVoxelOctree, SvoNode};
use crate::compiled::AabbPacked;

/// A spatial chunk of the SVO
#[derive(Clone)]
pub struct SvoChunk {
    /// Chunk identifier (spatial index)
    pub chunk_id: u32,
    /// Nodes in this chunk (linearized)
    pub nodes: Vec<SvoNode>,
    /// World-space bounds of this chunk
    pub bounds: AabbPacked,
    /// Depth in the tree where this chunk starts
    pub start_depth: u32,
}

impl SvoChunk {
    /// Create a new chunk
    pub const fn new(
        chunk_id: u32,
        nodes: Vec<SvoNode>,
        bounds: AabbPacked,
        start_depth: u32,
    ) -> Self {
        Self {
            chunk_id,
            nodes,
            bounds,
            start_depth,
        }
    }

    /// Memory usage in bytes
    pub fn memory_bytes(&self) -> usize {
        self.nodes.len() * std::mem::size_of::<SvoNode>()
    }

    /// Serialize chunk to bytes
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut bytes = Vec::new();

        // Header: chunk_id, node_count, start_depth, bounds
        bytes.extend_from_slice(&self.chunk_id.to_le_bytes());
        bytes.extend_from_slice(&(self.nodes.len() as u32).to_le_bytes());
        bytes.extend_from_slice(&self.start_depth.to_le_bytes());
        bytes.extend_from_slice(&self.bounds.min_x.to_le_bytes());
        bytes.extend_from_slice(&self.bounds.min_y.to_le_bytes());
        bytes.extend_from_slice(&self.bounds.min_z.to_le_bytes());
        bytes.extend_from_slice(&self.bounds.max_x.to_le_bytes());
        bytes.extend_from_slice(&self.bounds.max_y.to_le_bytes());
        bytes.extend_from_slice(&self.bounds.max_z.to_le_bytes());

        // Node data
        for node in &self.nodes {
            bytes.extend_from_slice(&node.distance.to_le_bytes());
            bytes.extend_from_slice(&node.nx.to_le_bytes());
            bytes.extend_from_slice(&node.ny.to_le_bytes());
            bytes.extend_from_slice(&node.nz.to_le_bytes());
            bytes.push(node.child_mask);
            bytes.push(node.is_leaf);
            bytes.extend_from_slice(&node.material_id.to_le_bytes());
            bytes.extend_from_slice(&node.first_child.to_le_bytes());
            bytes.extend_from_slice(&node._pad[0].to_le_bytes());
            bytes.extend_from_slice(&node._pad[1].to_le_bytes());
        }

        bytes
    }

    /// Deserialize chunk from bytes
    pub fn from_bytes(data: &[u8]) -> Option<Self> {
        if data.len() < 36 {
            return None;
        }

        let chunk_id = u32::from_le_bytes(data[0..4].try_into().ok()?);
        let node_count = u32::from_le_bytes(data[4..8].try_into().ok()?) as usize;
        let start_depth = u32::from_le_bytes(data[8..12].try_into().ok()?);
        let min_x = f32::from_le_bytes(data[12..16].try_into().ok()?);
        let min_y = f32::from_le_bytes(data[16..20].try_into().ok()?);
        let min_z = f32::from_le_bytes(data[20..24].try_into().ok()?);
        let max_x = f32::from_le_bytes(data[24..28].try_into().ok()?);
        let max_y = f32::from_le_bytes(data[28..32].try_into().ok()?);
        let max_z = f32::from_le_bytes(data[32..36].try_into().ok()?);

        let bounds = AabbPacked::new(
            Vec3::new(min_x, min_y, min_z),
            Vec3::new(max_x, max_y, max_z),
        );

        let node_data = &data[36..];
        let node_byte_size = 32; // SvoNode is 32 bytes
        if node_data.len() < node_count * node_byte_size {
            return None;
        }

        let mut nodes = Vec::with_capacity(node_count);
        for i in 0..node_count {
            let offset = i * node_byte_size;
            let d = &node_data[offset..];

            let distance = f32::from_le_bytes(d[0..4].try_into().ok()?);
            let nx = f32::from_le_bytes(d[4..8].try_into().ok()?);
            let ny = f32::from_le_bytes(d[8..12].try_into().ok()?);
            let nz = f32::from_le_bytes(d[12..16].try_into().ok()?);
            let child_mask = d[16];
            let is_leaf = d[17];
            let material_id = u16::from_le_bytes(d[18..20].try_into().ok()?);
            let first_child = u32::from_le_bytes(d[20..24].try_into().ok()?);
            let pad0 = f32::from_le_bytes(d[24..28].try_into().ok()?);
            let pad1 = f32::from_le_bytes(d[28..32].try_into().ok()?);

            nodes.push(SvoNode {
                distance,
                nx,
                ny,
                nz,
                child_mask,
                is_leaf,
                material_id,
                first_child,
                _pad: [pad0, pad1],
            });
        }

        Some(Self {
            chunk_id,
            nodes,
            bounds,
            start_depth,
        })
    }
}

/// LRU entry for the cache
struct CacheEntry {
    chunk: SvoChunk,
    last_access: u64,
}

/// Streaming SVO cache with LRU eviction
///
/// Stores a fixed number of SVO chunks in memory. When the cache
/// is full and a new chunk is needed, the least recently used chunk
/// is evicted.
pub struct SvoStreamingCache {
    /// Maximum number of chunks to keep in memory
    capacity: usize,
    /// Cached chunks (chunk_id → entry)
    entries: std::collections::HashMap<u32, CacheEntry>,
    /// Access counter for LRU ordering
    access_counter: u64,
    /// Total memory used by cached chunks
    memory_used: usize,
    /// Maximum memory in bytes (0 = unlimited, use capacity-based eviction)
    max_memory: usize,
    /// Statistics
    hits: u64,
    misses: u64,
}

impl SvoStreamingCache {
    /// Create a new streaming cache
    ///
    /// # Arguments
    /// * `capacity` - Maximum number of chunks to keep in memory
    pub fn new(capacity: usize) -> Self {
        Self {
            capacity,
            entries: std::collections::HashMap::new(),
            access_counter: 0,
            memory_used: 0,
            max_memory: 0,
            hits: 0,
            misses: 0,
        }
    }

    /// Create a cache with a memory budget
    pub fn with_memory_budget(max_memory_bytes: usize) -> Self {
        Self {
            capacity: usize::MAX,
            entries: std::collections::HashMap::new(),
            access_counter: 0,
            memory_used: 0,
            max_memory: max_memory_bytes,
            hits: 0,
            misses: 0,
        }
    }

    /// Get a chunk from the cache
    pub fn get(&mut self, chunk_id: u32) -> Option<&SvoChunk> {
        self.access_counter += 1;
        if let Some(entry) = self.entries.get_mut(&chunk_id) {
            entry.last_access = self.access_counter;
            self.hits += 1;
            Some(&entry.chunk)
        } else {
            self.misses += 1;
            None
        }
    }

    /// Insert a chunk into the cache, evicting LRU if needed
    pub fn insert(&mut self, chunk: SvoChunk) {
        let chunk_id = chunk.chunk_id;
        let chunk_mem = chunk.memory_bytes();

        // Evict until we have room
        while self.needs_eviction(chunk_mem) {
            if !self.evict_lru() {
                break; // No more entries to evict
            }
        }

        self.access_counter += 1;
        self.memory_used += chunk_mem;
        self.entries.insert(
            chunk_id,
            CacheEntry {
                chunk,
                last_access: self.access_counter,
            },
        );
    }

    /// Check if eviction is needed
    fn needs_eviction(&self, additional_bytes: usize) -> bool {
        if self.entries.len() >= self.capacity {
            return true;
        }
        if self.max_memory > 0 && self.memory_used + additional_bytes > self.max_memory {
            return true;
        }
        false
    }

    /// Evict the least recently used chunk
    fn evict_lru(&mut self) -> bool {
        if self.entries.is_empty() {
            return false;
        }

        let lru_id = self
            .entries
            .iter()
            .min_by_key(|(_, e)| e.last_access)
            .map(|(&id, _)| id);

        if let Some(id) = lru_id {
            if let Some(entry) = self.entries.remove(&id) {
                self.memory_used = self.memory_used.saturating_sub(entry.chunk.memory_bytes());
            }
            true
        } else {
            false
        }
    }

    /// Remove a specific chunk
    pub fn remove(&mut self, chunk_id: u32) -> Option<SvoChunk> {
        if let Some(entry) = self.entries.remove(&chunk_id) {
            self.memory_used = self.memory_used.saturating_sub(entry.chunk.memory_bytes());
            Some(entry.chunk)
        } else {
            None
        }
    }

    /// Clear the cache
    pub fn clear(&mut self) {
        self.entries.clear();
        self.memory_used = 0;
    }

    /// Number of chunks in cache
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if cache is empty
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Total memory used by cached chunks
    pub const fn memory_used(&self) -> usize {
        self.memory_used
    }

    /// Cache hit rate (0.0 to 1.0)
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            self.hits as f64 / total as f64
        }
    }
}

/// Split an SVO into spatial chunks at a given depth
///
/// Each chunk contains the subtree rooted at the given depth.
/// The top-level nodes (above split_depth) are stored in a "root chunk".
pub fn split_into_chunks(svo: &SparseVoxelOctree, split_depth: u32) -> Vec<SvoChunk> {
    if svo.nodes.is_empty() {
        return Vec::new();
    }

    let mut chunks = Vec::new();
    let mut chunk_id = 0u32;

    // BFS to find nodes at split_depth
    struct BfsEntry {
        node_idx: usize,
        depth: u32,
        center: Vec3,
        half_size: Vec3,
    }

    let center = svo.bounds.center();
    let half_size = svo.bounds.half_size();

    let mut queue: std::collections::VecDeque<BfsEntry> = std::collections::VecDeque::new();
    queue.push_back(BfsEntry {
        node_idx: 0,
        depth: 0,
        center,
        half_size,
    });

    // Collect root-level nodes (above split_depth) as first chunk
    let mut root_nodes = Vec::new();

    while let Some(entry) = queue.pop_front() {
        if entry.node_idx >= svo.nodes.len() {
            continue;
        }

        let node = &svo.nodes[entry.node_idx];

        if entry.depth < split_depth && node.is_leaf == 0 {
            // Above split depth: collect as root chunk
            root_nodes.push(*node);

            // Enqueue children
            for octant in 0..8u8 {
                if let Some(child_idx) = node.child_index(octant) {
                    let child_c = super::child_center(entry.center, entry.half_size, octant);
                    queue.push_back(BfsEntry {
                        node_idx: child_idx as usize,
                        depth: entry.depth + 1,
                        center: child_c,
                        half_size: entry.half_size * 0.5,
                    });
                }
            }
        } else {
            // At or below split depth: create a chunk from this subtree
            let subtree = extract_subtree(svo, entry.node_idx);
            let bounds = AabbPacked::new(
                entry.center - entry.half_size,
                entry.center + entry.half_size,
            );

            chunks.push(SvoChunk::new(chunk_id, subtree, bounds, entry.depth));
            chunk_id += 1;
        }
    }

    // Insert root chunk at the beginning
    if !root_nodes.is_empty() {
        chunks.insert(
            0,
            SvoChunk::new(
                u32::MAX, // Special ID for root chunk
                root_nodes,
                svo.bounds,
                0,
            ),
        );
    }

    chunks
}

/// Extract a subtree starting at the given node index
fn extract_subtree(svo: &SparseVoxelOctree, root_idx: usize) -> Vec<SvoNode> {
    if root_idx >= svo.nodes.len() {
        return Vec::new();
    }

    let mut result = Vec::new();
    let mut queue: std::collections::VecDeque<usize> = std::collections::VecDeque::new();
    let mut old_to_new: std::collections::HashMap<usize, u32> = std::collections::HashMap::new();

    queue.push_back(root_idx);
    old_to_new.insert(root_idx, 0);
    let mut next_new_idx = 1u32;

    // First pass: assign new indices
    let mut bfs_order: Vec<usize> = Vec::new();
    let mut temp_queue = queue.clone();
    while let Some(idx) = temp_queue.pop_front() {
        bfs_order.push(idx);
        if idx < svo.nodes.len() {
            let node = &svo.nodes[idx];
            if node.is_leaf == 0 {
                for octant in 0..8u8 {
                    if let Some(child_idx) = node.child_index(octant) {
                        let ci = child_idx as usize;
                        if let std::collections::hash_map::Entry::Vacant(e) = old_to_new.entry(ci) {
                            e.insert(next_new_idx);
                            next_new_idx += 1;
                            temp_queue.push_back(ci);
                        }
                    }
                }
            }
        }
    }

    // Second pass: build remapped nodes
    for &old_idx in &bfs_order {
        if old_idx >= svo.nodes.len() {
            continue;
        }
        let mut node = svo.nodes[old_idx];

        if node.is_leaf == 0 {
            // Remap first_child
            let old_first = node.first_child as usize;
            if let Some(&new_first) = old_to_new.get(&old_first) {
                node.first_child = new_first;
            }
        }

        result.push(node);
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::svo::SvoBuildConfig;
    use crate::types::SdfNode;

    fn make_test_svo() -> SparseVoxelOctree {
        let sphere = SdfNode::sphere(1.0);
        let config = SvoBuildConfig {
            max_depth: 4,
            bounds_min: Vec3::splat(-2.0),
            bounds_max: Vec3::splat(2.0),
            use_compiled: false,
            ..Default::default()
        };
        super::super::build::build_svo(&sphere, &config)
    }

    #[test]
    fn test_cache_basic() {
        let mut cache = SvoStreamingCache::new(4);

        let chunk = SvoChunk::new(0, vec![SvoNode::default()], AabbPacked::empty(), 0);
        cache.insert(chunk);

        assert_eq!(cache.len(), 1);
        assert!(cache.get(0).is_some());
        assert!(cache.get(1).is_none());
    }

    #[test]
    fn test_cache_eviction() {
        let mut cache = SvoStreamingCache::new(2);

        for i in 0..3 {
            let chunk = SvoChunk::new(i, vec![SvoNode::default()], AabbPacked::empty(), 0);
            cache.insert(chunk);
        }

        // Capacity is 2, so oldest (chunk 0) should be evicted
        assert_eq!(cache.len(), 2);
        assert!(cache.get(0).is_none(), "Chunk 0 should have been evicted");
        assert!(cache.get(1).is_some());
        assert!(cache.get(2).is_some());
    }

    #[test]
    fn test_cache_lru_order() {
        let mut cache = SvoStreamingCache::new(2);

        let chunk0 = SvoChunk::new(0, vec![SvoNode::default()], AabbPacked::empty(), 0);
        let chunk1 = SvoChunk::new(1, vec![SvoNode::default()], AabbPacked::empty(), 0);
        cache.insert(chunk0);
        cache.insert(chunk1);

        // Access chunk 0 to make it most recent
        cache.get(0);

        // Insert chunk 2 → should evict chunk 1 (LRU)
        let chunk2 = SvoChunk::new(2, vec![SvoNode::default()], AabbPacked::empty(), 0);
        cache.insert(chunk2);

        assert!(
            cache.get(0).is_some(),
            "Chunk 0 was recently accessed, should still be cached"
        );
        assert!(cache.get(1).is_none(), "Chunk 1 should have been evicted");
        assert!(cache.get(2).is_some());
    }

    #[test]
    fn test_cache_hit_rate() {
        let mut cache = SvoStreamingCache::new(4);

        let chunk = SvoChunk::new(0, vec![SvoNode::default()], AabbPacked::empty(), 0);
        cache.insert(chunk);

        cache.get(0); // hit
        cache.get(0); // hit
        cache.get(1); // miss

        assert!((cache.hit_rate() - 2.0 / 3.0).abs() < 0.01);
    }

    #[test]
    fn test_chunk_serialization() {
        let nodes = vec![
            SvoNode::leaf(1.5, Vec3::new(0.0, 1.0, 0.0)),
            SvoNode::interior(0.5, 0b00110101, 42),
        ];
        let chunk = SvoChunk::new(7, nodes, AabbPacked::new(Vec3::ZERO, Vec3::ONE), 3);

        let bytes = chunk.to_bytes();
        let restored = SvoChunk::from_bytes(&bytes).expect("Should deserialize");

        assert_eq!(restored.chunk_id, 7);
        assert_eq!(restored.start_depth, 3);
        assert_eq!(restored.nodes.len(), 2);
        assert_eq!(restored.nodes[0].distance, 1.5);
        assert_eq!(restored.nodes[0].ny, 1.0);
        assert_eq!(restored.nodes[1].child_mask, 0b00110101);
        assert_eq!(restored.nodes[1].first_child, 42);
    }

    #[test]
    fn test_split_into_chunks() {
        let svo = make_test_svo();
        let chunks = split_into_chunks(&svo, 2);

        assert!(chunks.len() > 1, "Should produce multiple chunks");

        // Total nodes across chunks should account for all SVO nodes
        let total_chunk_nodes: usize = chunks.iter().map(|c| c.nodes.len()).sum();
        assert!(total_chunk_nodes > 0);
    }

    #[test]
    fn test_cache_memory_budget() {
        let mut cache = SvoStreamingCache::with_memory_budget(128);

        // Each default SvoNode is 32 bytes, so 1 node = 32 bytes per chunk
        for i in 0..10 {
            let chunk = SvoChunk::new(i, vec![SvoNode::default()], AabbPacked::empty(), 0);
            cache.insert(chunk);
        }

        // Should have evicted some chunks to stay under 128 bytes
        assert!(
            cache.memory_used() <= 128 + 32,
            "Memory should be near budget"
        );
    }

    #[test]
    fn test_cache_clear() {
        let mut cache = SvoStreamingCache::new(4);

        for i in 0..3 {
            cache.insert(SvoChunk::new(
                i,
                vec![SvoNode::default()],
                AabbPacked::empty(),
                0,
            ));
        }

        cache.clear();
        assert_eq!(cache.len(), 0);
        assert_eq!(cache.memory_used(), 0);
    }
}