alice-sdf 5.0.0

ALICE-SDF: 3D/Spatial Data Specialist — Store laws, not polygons. GLSL/WGSL/HLSL transpile, SVO, GPU marching cubes, texture-fit, procedural CSG. Optional bridges to alice-physics / alice-codec / libasp / alice-cache behind features.
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
//! AABB (Axis-Aligned Bounding Box) for BVH pruning
//!
//! This module provides efficient AABB computation and distance functions
//! for accelerating SDF evaluation through spatial pruning.
//!
//! Author: Moroya Sakamoto

use glam::{Quat, Vec3};

/// SIMD-friendly AABB structure (32 bytes, cache-aligned)
///
/// Stores the minimum and maximum corners of an axis-aligned bounding box.
/// Designed for efficient SIMD operations and cache-friendly memory access.
#[derive(Clone, Copy, Debug)]
#[repr(C, align(32))]
pub struct AabbPacked {
    /// Minimum X coordinate
    pub min_x: f32,
    /// Minimum Y coordinate
    pub min_y: f32,
    /// Minimum Z coordinate
    pub min_z: f32,
    /// Padding for alignment
    pub _pad1: f32,
    /// Maximum X coordinate
    pub max_x: f32,
    /// Maximum Y coordinate
    pub max_y: f32,
    /// Maximum Z coordinate
    pub max_z: f32,
    /// Padding for alignment
    pub _pad2: f32,
}

impl AabbPacked {
    /// Create an empty (invalid) AABB
    #[inline]
    pub const fn empty() -> Self {
        Self {
            min_x: f32::MAX,
            min_y: f32::MAX,
            min_z: f32::MAX,
            _pad1: 0.0,
            max_x: f32::MIN,
            max_y: f32::MIN,
            max_z: f32::MIN,
            _pad2: 0.0,
        }
    }

    /// Create an AABB that contains everything (infinite)
    #[inline]
    pub const fn infinite() -> Self {
        Self {
            min_x: f32::MIN,
            min_y: f32::MIN,
            min_z: f32::MIN,
            _pad1: 0.0,
            max_x: f32::MAX,
            max_y: f32::MAX,
            max_z: f32::MAX,
            _pad2: 0.0,
        }
    }

    /// Create an AABB from min and max corners
    #[inline]
    pub const fn new(min: Vec3, max: Vec3) -> Self {
        Self {
            min_x: min.x,
            min_y: min.y,
            min_z: min.z,
            _pad1: 0.0,
            max_x: max.x,
            max_y: max.y,
            max_z: max.z,
            _pad2: 0.0,
        }
    }

    /// Create an AABB centered at origin with given half-size
    #[inline]
    pub fn from_half_size(half_size: Vec3) -> Self {
        Self::new(-half_size, half_size)
    }

    /// Create an AABB for a sphere
    #[inline]
    pub fn from_sphere(center: Vec3, radius: f32) -> Self {
        let r = Vec3::splat(radius);
        Self::new(center - r, center + r)
    }

    /// Get minimum corner as Vec3
    #[inline]
    pub const fn min(&self) -> Vec3 {
        Vec3::new(self.min_x, self.min_y, self.min_z)
    }

    /// Get maximum corner as Vec3
    #[inline]
    pub const fn max(&self) -> Vec3 {
        Vec3::new(self.max_x, self.max_y, self.max_z)
    }

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

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

    /// Check if the AABB is valid (non-empty)
    #[inline]
    pub fn is_valid(&self) -> bool {
        self.min_x <= self.max_x && self.min_y <= self.max_y && self.min_z <= self.max_z
    }

    /// Compute signed distance from a point to the AABB surface
    ///
    /// Returns:
    /// - Negative if point is inside
    /// - Zero if point is on surface
    /// - Positive if point is outside
    #[inline]
    pub fn distance_to_point(&self, p: Vec3) -> f32 {
        let center = self.center();
        let half = self.half_size();

        // q = abs(p - center) - half_size
        let q = (p - center).abs() - half;

        // Same formula as box SDF
        let outside = q.max(Vec3::ZERO).length();
        let inside = q.x.max(q.y).max(q.z).min(0.0);

        outside + inside
    }

    /// Fast conservative distance estimate (may underestimate)
    ///
    /// Faster than exact distance but may return smaller values.
    /// Safe for BVH pruning (won't skip valid nodes).
    #[inline]
    pub fn distance_to_point_fast(&self, p: Vec3) -> f32 {
        let dx = (self.min_x - p.x).max(p.x - self.max_x).max(0.0);
        let dy = (self.min_y - p.y).max(p.y - self.max_y).max(0.0);
        let dz = (self.min_z - p.z).max(p.z - self.max_z).max(0.0);

        // Return L-infinity norm (max component) - conservative underestimate
        dx.max(dy).max(dz)
    }

    /// Union of two AABBs (smallest AABB containing both)
    #[inline]
    pub const fn union(&self, other: &Self) -> Self {
        Self {
            min_x: self.min_x.min(other.min_x),
            min_y: self.min_y.min(other.min_y),
            min_z: self.min_z.min(other.min_z),
            _pad1: 0.0,
            max_x: self.max_x.max(other.max_x),
            max_y: self.max_y.max(other.max_y),
            max_z: self.max_z.max(other.max_z),
            _pad2: 0.0,
        }
    }

    /// Intersection of two AABBs
    #[inline]
    pub const fn intersection(&self, other: &Self) -> Self {
        Self {
            min_x: self.min_x.max(other.min_x),
            min_y: self.min_y.max(other.min_y),
            min_z: self.min_z.max(other.min_z),
            _pad1: 0.0,
            max_x: self.max_x.min(other.max_x),
            max_y: self.max_y.min(other.max_y),
            max_z: self.max_z.min(other.max_z),
            _pad2: 0.0,
        }
    }

    /// Expand AABB by a scalar amount in all directions
    #[inline]
    pub fn expand(&self, amount: f32) -> Self {
        Self {
            min_x: self.min_x - amount,
            min_y: self.min_y - amount,
            min_z: self.min_z - amount,
            _pad1: 0.0,
            max_x: self.max_x + amount,
            max_y: self.max_y + amount,
            max_z: self.max_z + amount,
            _pad2: 0.0,
        }
    }

    /// Translate the AABB
    #[inline]
    pub fn translate(&self, offset: Vec3) -> Self {
        Self {
            min_x: self.min_x + offset.x,
            min_y: self.min_y + offset.y,
            min_z: self.min_z + offset.z,
            _pad1: 0.0,
            max_x: self.max_x + offset.x,
            max_y: self.max_y + offset.y,
            max_z: self.max_z + offset.z,
            _pad2: 0.0,
        }
    }

    /// Scale the AABB uniformly from origin
    #[inline]
    pub fn scale(&self, factor: f32) -> Self {
        Self {
            min_x: self.min_x * factor,
            min_y: self.min_y * factor,
            min_z: self.min_z * factor,
            _pad1: 0.0,
            max_x: self.max_x * factor,
            max_y: self.max_y * factor,
            max_z: self.max_z * factor,
            _pad2: 0.0,
        }
    }

    /// Scale the AABB non-uniformly from origin
    #[inline]
    pub fn scale_nonuniform(&self, factors: Vec3) -> Self {
        let min = self.min() * factors;
        let max = self.max() * factors;
        // Handle negative scaling
        Self::new(min.min(max), min.max(max))
    }

    /// Transform AABB by rotation (conservative - may be larger than necessary)
    ///
    /// Computes the AABB of the rotated AABB, which is always larger or equal
    /// to the actual rotated bounding box.
    #[inline]
    pub fn rotate(&self, rotation: Quat) -> Self {
        let center = self.center();
        let half = self.half_size();

        // Rotate all 8 corners and find new AABB
        let corners = [
            Vec3::new(-half.x, -half.y, -half.z),
            Vec3::new(half.x, -half.y, -half.z),
            Vec3::new(-half.x, half.y, -half.z),
            Vec3::new(half.x, half.y, -half.z),
            Vec3::new(-half.x, -half.y, half.z),
            Vec3::new(half.x, -half.y, half.z),
            Vec3::new(-half.x, half.y, half.z),
            Vec3::new(half.x, half.y, half.z),
        ];

        let mut new_min = Vec3::splat(f32::MAX);
        let mut new_max = Vec3::splat(f32::MIN);

        for corner in corners {
            let rotated = rotation * corner + center;
            new_min = new_min.min(rotated);
            new_max = new_max.max(rotated);
        }

        Self::new(new_min, new_max)
    }
}

impl Default for AabbPacked {
    fn default() -> Self {
        Self::empty()
    }
}

/// Compute AABB for primitive SDF nodes
pub mod primitives {
    use super::*;

    /// AABB for a sphere centered at origin
    #[inline]
    pub fn sphere_aabb(radius: f32) -> AabbPacked {
        AabbPacked::from_sphere(Vec3::ZERO, radius)
    }

    /// AABB for a box centered at origin
    #[inline]
    pub fn box_aabb(half_extents: Vec3) -> AabbPacked {
        AabbPacked::new(-half_extents, half_extents)
    }

    /// AABB for a cylinder along Y axis
    #[inline]
    pub fn cylinder_aabb(radius: f32, half_height: f32) -> AabbPacked {
        AabbPacked::new(
            Vec3::new(-radius, -half_height, -radius),
            Vec3::new(radius, half_height, radius),
        )
    }

    /// AABB for a torus in XZ plane
    #[inline]
    pub fn torus_aabb(major_radius: f32, minor_radius: f32) -> AabbPacked {
        let r = major_radius + minor_radius;
        AabbPacked::new(
            Vec3::new(-r, -minor_radius, -r),
            Vec3::new(r, minor_radius, r),
        )
    }

    /// AABB for an infinite plane (returns infinite AABB)
    #[inline]
    pub const fn plane_aabb() -> AabbPacked {
        AabbPacked::infinite()
    }

    /// AABB for a capsule
    #[inline]
    pub fn capsule_aabb(point_a: Vec3, point_b: Vec3, radius: f32) -> AabbPacked {
        let min = point_a.min(point_b) - Vec3::splat(radius);
        let max = point_a.max(point_b) + Vec3::splat(radius);
        AabbPacked::new(min, max)
    }

    /// AABB for a rounded cone along Y axis
    #[inline]
    pub fn rounded_cone_aabb(r1: f32, r2: f32, half_height: f32) -> AabbPacked {
        let max_r = r1.max(r2);
        AabbPacked::new(
            Vec3::new(-max_r, -half_height - r1, -max_r),
            Vec3::new(max_r, half_height + r2, max_r),
        )
    }

    /// AABB for a 4-sided pyramid along Y axis (unit base, as `sdf_pyramid`)
    #[inline]
    pub fn pyramid_aabb(half_height: f32) -> AabbPacked {
        AabbPacked::new(
            Vec3::new(-0.5, -half_height, -0.5),
            Vec3::new(0.5, half_height, 0.5),
        )
    }

    /// AABB for a regular octahedron (vertices at `±size` on each axis)
    #[inline]
    pub fn octahedron_aabb(size: f32) -> AabbPacked {
        let s = size.abs();
        AabbPacked::new(Vec3::splat(-s), Vec3::splat(s))
    }

    /// AABB for a hexagonal prism (hex in XY, extruded along Z)
    ///
    /// `hex_radius` is the apothem (`sdf_hex_prism`: flat edges at
    /// `y = ±hex_radius`), so the vertices sit at `x = ±hex_radius · 2/√3`.
    #[inline]
    pub fn hex_prism_aabb(hex_radius: f32, half_height: f32) -> AabbPacked {
        // 2/√3 rounded up so the box stays conservative in f32
        let vx = hex_radius * 1.154_700_6;
        AabbPacked::new(
            Vec3::new(-vx, -hex_radius, -half_height),
            Vec3::new(vx, hex_radius, half_height),
        )
    }

    /// AABB for a chain link shape (`sdf_link`: a torus of radii r1 / r2 in
    /// XY stretched by `half_length` along Y, so the Y extent is
    /// `half_length + r1 + r2`)
    #[inline]
    pub fn link_aabb(half_length: f32, r1: f32, r2: f32) -> AabbPacked {
        let r = r1 + r2;
        AabbPacked::new(
            Vec3::new(-r, -(half_length + r), -r2),
            Vec3::new(r, half_length + r, r2),
        )
    }
}

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

    #[test]
    fn test_aabb_size_and_alignment() {
        assert_eq!(mem::size_of::<AabbPacked>(), 32);
        assert_eq!(mem::align_of::<AabbPacked>(), 32);
    }

    #[test]
    fn test_aabb_from_sphere() {
        let aabb = AabbPacked::from_sphere(Vec3::ZERO, 1.0);
        assert_eq!(aabb.min(), Vec3::new(-1.0, -1.0, -1.0));
        assert_eq!(aabb.max(), Vec3::new(1.0, 1.0, 1.0));
    }

    #[test]
    fn test_aabb_distance_inside() {
        let aabb = AabbPacked::new(Vec3::new(-1.0, -1.0, -1.0), Vec3::new(1.0, 1.0, 1.0));
        let d = aabb.distance_to_point(Vec3::ZERO);
        assert!(d < 0.0); // Inside
    }

    #[test]
    fn test_aabb_distance_surface() {
        let aabb = AabbPacked::new(Vec3::new(-1.0, -1.0, -1.0), Vec3::new(1.0, 1.0, 1.0));
        let d = aabb.distance_to_point(Vec3::new(1.0, 0.0, 0.0));
        assert!(d.abs() < 0.0001); // On surface
    }

    #[test]
    fn test_aabb_distance_outside() {
        let aabb = AabbPacked::new(Vec3::new(-1.0, -1.0, -1.0), Vec3::new(1.0, 1.0, 1.0));
        let d = aabb.distance_to_point(Vec3::new(2.0, 0.0, 0.0));
        assert!((d - 1.0).abs() < 0.0001); // 1 unit outside
    }

    #[test]
    fn test_aabb_union() {
        let a = AabbPacked::new(Vec3::new(-1.0, -1.0, -1.0), Vec3::new(1.0, 1.0, 1.0));
        let b = AabbPacked::new(Vec3::new(0.0, 0.0, 0.0), Vec3::new(2.0, 2.0, 2.0));
        let u = a.union(&b);
        assert_eq!(u.min(), Vec3::new(-1.0, -1.0, -1.0));
        assert_eq!(u.max(), Vec3::new(2.0, 2.0, 2.0));
    }

    #[test]
    fn test_aabb_translate() {
        let aabb = AabbPacked::new(Vec3::new(-1.0, -1.0, -1.0), Vec3::new(1.0, 1.0, 1.0));
        let translated = aabb.translate(Vec3::new(2.0, 0.0, 0.0));
        assert_eq!(translated.min(), Vec3::new(1.0, -1.0, -1.0));
        assert_eq!(translated.max(), Vec3::new(3.0, 1.0, 1.0));
    }

    #[test]
    fn test_aabb_scale() {
        let aabb = AabbPacked::new(Vec3::new(-1.0, -1.0, -1.0), Vec3::new(1.0, 1.0, 1.0));
        let scaled = aabb.scale(2.0);
        assert_eq!(scaled.min(), Vec3::new(-2.0, -2.0, -2.0));
        assert_eq!(scaled.max(), Vec3::new(2.0, 2.0, 2.0));
    }

    #[test]
    fn test_primitive_aabbs() {
        let sphere = primitives::sphere_aabb(1.0);
        assert_eq!(sphere.center(), Vec3::ZERO);
        assert_eq!(sphere.half_size(), Vec3::splat(1.0));

        let cylinder = primitives::cylinder_aabb(0.5, 1.0);
        assert_eq!(cylinder.half_size(), Vec3::new(0.5, 1.0, 0.5));
    }
}