alice-sdf 3.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
//! FFI functions for evaluating SDFs (single point, batch, SoA, gradient, animated).
//!
//! Author: Moroya Sakamoto

use super::guard::ffi_guard;
use super::registry::{get_compiled, get_node};
use super::types::*;
use crate::animation::AnimationParams;
use crate::compiled::{eval_compiled, CompiledSdf};
use std::slice;

// ============================================================================
// Evaluation (Deep Fried)
// ============================================================================

/// Evaluate SDF at a single point
///
/// For bulk evaluation, use `alice_sdf_eval_compiled_batch` instead.
#[no_mangle]
pub extern "C" fn alice_sdf_eval(node: SdfHandle, x: f32, y: f32, z: f32) -> f32 {
    ffi_guard(f32::MAX, || {
        let sdf_node = match get_node(node) {
            Some(n) => n,
            None => return f32::MAX,
        };
        crate::eval(&sdf_node, glam::Vec3::new(x, y, z))
    })
}

/// Evaluate compiled SDF at a single point
#[no_mangle]
pub extern "C" fn alice_sdf_eval_compiled(compiled: CompiledHandle, x: f32, y: f32, z: f32) -> f32 {
    ffi_guard(f32::MAX, || {
        let comp = match get_compiled(compiled) {
            Some(c) => c,
            None => return f32::MAX,
        };
        eval_compiled(&comp, glam::Vec3::new(x, y, z))
    })
}

/// Batch evaluate SDF at multiple points (parallel, zero-copy output)
///
/// # Parameters
/// - `node`: SDF handle (will be compiled internally if not already)
/// - `points`: Pointer to array of floats [x0, y0, z0, x1, y1, z1, ...]
/// - `distances`: Output array (must be pre-allocated with `count` elements)
/// - `count`: Number of points to evaluate
///
/// # Safety
///
/// - `points` must point to a valid array of at least `count * 3` f32 values (AoS layout:
///   [x0, y0, z0, x1, y1, z1, ...]). The pointer must be non-null and properly aligned.
/// - `distances` must point to a writable array of at least `count` f32 values.
/// - Both pointers must remain valid and non-aliasing for the entire duration of the call.
///
/// # Deep Fried Optimization
/// Compiles the SDF internally and uses parallel evaluation.
/// For maximum performance, use `alice_sdf_compile` + `alice_sdf_eval_compiled_batch`.
#[no_mangle]
pub unsafe extern "C" fn alice_sdf_eval_batch(
    node: SdfHandle,
    points: *const f32,
    distances: *mut f32,
    count: u32,
) -> BatchResult {
    ffi_guard(
        BatchResult {
            count: 0,
            result: SdfResult::Unknown,
        },
        || {
            if points.is_null() || distances.is_null() {
                return BatchResult {
                    count: 0,
                    result: SdfResult::NullPointer,
                };
            }

            let sdf_node = match get_node(node) {
                Some(n) => n,
                None => {
                    return BatchResult {
                        count: 0,
                        result: SdfResult::InvalidHandle,
                    }
                }
            };

            let count_usize = count as usize;
            // SAFETY: Caller guarantees points is a non-null, valid pointer to at least count * 3 f32
            // values in AoS layout. Null check performed above. Pointer remains valid for this call.
            let points_slice = slice::from_raw_parts(points, count_usize * 3);
            // SAFETY: Caller guarantees distances is a non-null, writable pointer to at least count f32
            // values. Null check performed above. No other reference aliases this slice during the call.
            let distances_slice = slice::from_raw_parts_mut(distances, count_usize);

            // Compile once
            let compiled = CompiledSdf::compile(&sdf_node);

            // Parallel evaluation with Rayon
            use rayon::prelude::*;

            if count_usize >= 256 {
                // Parallel for large batches
                distances_slice
                    .par_iter_mut()
                    .enumerate()
                    .for_each(|(i, d)| {
                        let p = glam::Vec3::new(
                            points_slice[i * 3],
                            points_slice[i * 3 + 1],
                            points_slice[i * 3 + 2],
                        );
                        *d = eval_compiled(&compiled, p);
                    });
            } else {
                // Sequential for small batches (Rayon overhead not worth it)
                for i in 0..count_usize {
                    let p = glam::Vec3::new(
                        points_slice[i * 3],
                        points_slice[i * 3 + 1],
                        points_slice[i * 3 + 2],
                    );
                    distances_slice[i] = eval_compiled(&compiled, p);
                }
            }

            BatchResult {
                count,
                result: SdfResult::Ok,
            }
        },
    )
}

/// Batch evaluate compiled SDF (fastest path for AoS data)
///
/// # Parameters
/// - `compiled`: Pre-compiled SDF handle from `alice_sdf_compile`
/// - `points`: Pointer to array of floats [x0, y0, z0, x1, y1, z1, ...]
/// - `distances`: Output array (must be pre-allocated with `count` elements)
/// - `count`: Number of points to evaluate
///
/// # Safety
///
/// - `points` must point to a valid array of at least `count * 3` f32 values (AoS layout:
///   [x0, y0, z0, x1, y1, z1, ...]). The pointer must be non-null and properly aligned.
/// - `distances` must point to a writable array of at least `count` f32 values.
/// - Both pointers must remain valid and non-aliasing for the entire duration of the call.
#[no_mangle]
pub unsafe extern "C" fn alice_sdf_eval_compiled_batch(
    compiled: CompiledHandle,
    points: *const f32,
    distances: *mut f32,
    count: u32,
) -> BatchResult {
    ffi_guard(
        BatchResult {
            count: 0,
            result: SdfResult::Unknown,
        },
        || {
            if points.is_null() || distances.is_null() {
                return BatchResult {
                    count: 0,
                    result: SdfResult::NullPointer,
                };
            }

            let comp = match get_compiled(compiled) {
                Some(c) => c,
                None => {
                    return BatchResult {
                        count: 0,
                        result: SdfResult::InvalidHandle,
                    }
                }
            };

            let count_usize = count as usize;
            // SAFETY: Caller guarantees points is a non-null, valid pointer to at least count * 3 f32
            // values in AoS layout. Null check performed above. Pointer remains valid for this call.
            let points_slice = slice::from_raw_parts(points, count_usize * 3);
            // SAFETY: Caller guarantees distances is a non-null, writable pointer to at least count f32
            // values. Null check performed above. No other reference aliases this slice during the call.
            let distances_slice = slice::from_raw_parts_mut(distances, count_usize);

            use rayon::prelude::*;

            if count_usize >= 256 {
                distances_slice
                    .par_iter_mut()
                    .enumerate()
                    .for_each(|(i, d)| {
                        let p = glam::Vec3::new(
                            points_slice[i * 3],
                            points_slice[i * 3 + 1],
                            points_slice[i * 3 + 2],
                        );
                        *d = eval_compiled(&comp, p);
                    });
            } else {
                for i in 0..count_usize {
                    let p = glam::Vec3::new(
                        points_slice[i * 3],
                        points_slice[i * 3 + 1],
                        points_slice[i * 3 + 2],
                    );
                    distances_slice[i] = eval_compiled(&comp, p);
                }
            }

            BatchResult {
                count,
                result: SdfResult::Ok,
            }
        },
    )
}

/// Batch evaluate using SoA (Structure of Arrays) layout - THE FASTEST PATH
///
/// SoA layout enables SIMD vectorization and better cache utilization.
/// X, Y, Z coordinates are stored in separate contiguous arrays.
///
/// # Parameters
/// - `compiled`: Pre-compiled SDF handle
/// - `x`: Pointer to X coordinates array
/// - `y`: Pointer to Y coordinates array
/// - `z`: Pointer to Z coordinates array
/// - `distances`: Output array (caller-allocated)
/// - `count`: Number of points
///
/// # Safety
///
/// - `x`, `y`, and `z` must each point to a valid array of at least `count` f32 values.
/// - `distances` must point to a writable array of at least `count` f32 values.
/// - All four pointers must be non-null and remain valid for the entire duration of the call.
/// - Arrays should be 32-byte aligned for AVX2 (not required, but faster).
///
/// # Performance
/// This is the fastest evaluation path. Use for:
/// - Physics simulations
/// - Particle systems
/// - Real-time SDf tracing
#[no_mangle]
pub unsafe extern "C" fn alice_sdf_eval_soa(
    compiled: CompiledHandle,
    x: *const f32,
    y: *const f32,
    z: *const f32,
    distances: *mut f32,
    count: u32,
) -> BatchResult {
    ffi_guard(
        BatchResult {
            count: 0,
            result: SdfResult::Unknown,
        },
        || {
            if x.is_null() || y.is_null() || z.is_null() || distances.is_null() {
                return BatchResult {
                    count: 0,
                    result: SdfResult::NullPointer,
                };
            }

            let comp = match get_compiled(compiled) {
                Some(c) => c,
                None => {
                    return BatchResult {
                        count: 0,
                        result: SdfResult::InvalidHandle,
                    }
                }
            };

            let count_usize = count as usize;
            // SAFETY: Caller guarantees x, y, z are non-null pointers to valid arrays of at least count
            // f32 values each (SoA layout). Null checks performed above. Pointers remain valid for this
            // call. The three slices do not overlap and are not aliased by dist_slice.
            let x_slice = slice::from_raw_parts(x, count_usize);
            let y_slice = slice::from_raw_parts(y, count_usize);
            let z_slice = slice::from_raw_parts(z, count_usize);
            // SAFETY: Caller guarantees distances is a non-null, writable pointer to at least count f32
            // values. Null check performed above. Does not alias x, y, or z slices.
            let dist_slice = slice::from_raw_parts_mut(distances, count_usize);

            use rayon::prelude::*;

            // Threshold for parallelization
            const PARALLEL_THRESHOLD: usize = 1024;

            // Use true SIMD path: eval_compiled_batch_soa_raw processes 8 points
            // at a time with direct f32x8 loads from SoA arrays
            let _aligned_count = (count_usize + 7) & !7;

            // Ensure we have enough padding for SIMD (read up to aligned_count)
            // The raw function handles the 8-wide alignment internally
            if count_usize >= PARALLEL_THRESHOLD {
                // Parallel: split into chunks, each chunk uses SIMD internally
                const CHUNK_SIZE: usize = 4096;

                dist_slice
                    .par_chunks_mut(CHUNK_SIZE)
                    .enumerate()
                    .for_each(|(chunk_idx, chunk)| {
                        let start = chunk_idx * CHUNK_SIZE;
                        let chunk_len = chunk.len();
                        let _simd_count = (chunk_len + 7) & !7;

                        // Use SIMD for full 8-wide groups
                        let simd_iters = chunk_len / 8;
                        for s in 0..simd_iters {
                            let base = start + s * 8;
                            let local_base = s * 8;

                            let vx = wide::f32x8::from([
                                x_slice[base],
                                x_slice[base + 1],
                                x_slice[base + 2],
                                x_slice[base + 3],
                                x_slice[base + 4],
                                x_slice[base + 5],
                                x_slice[base + 6],
                                x_slice[base + 7],
                            ]);
                            let vy = wide::f32x8::from([
                                y_slice[base],
                                y_slice[base + 1],
                                y_slice[base + 2],
                                y_slice[base + 3],
                                y_slice[base + 4],
                                y_slice[base + 5],
                                y_slice[base + 6],
                                y_slice[base + 7],
                            ]);
                            let vz = wide::f32x8::from([
                                z_slice[base],
                                z_slice[base + 1],
                                z_slice[base + 2],
                                z_slice[base + 3],
                                z_slice[base + 4],
                                z_slice[base + 5],
                                z_slice[base + 6],
                                z_slice[base + 7],
                            ]);

                            let p = crate::compiled::Vec3x8 {
                                x: vx,
                                y: vy,
                                z: vz,
                            };
                            let distances = crate::compiled::eval_compiled_simd(&comp, p);
                            let arr: [f32; 8] = distances.into();
                            chunk[local_base..local_base + 8].copy_from_slice(&arr);
                        }

                        // Handle remainder scalar
                        let remainder_start = simd_iters * 8;
                        for (offset, slot) in
                            chunk[remainder_start..chunk_len].iter_mut().enumerate()
                        {
                            let i = start + remainder_start + offset;
                            let p = glam::Vec3::new(x_slice[i], y_slice[i], z_slice[i]);
                            *slot = eval_compiled(&comp, p);
                        }
                    });
            } else {
                // Sequential: use raw SIMD path for full groups, scalar for remainder
                crate::compiled::eval_compiled_batch_soa_raw(
                    &comp,
                    x,
                    y,
                    z,
                    distances,
                    count_usize,
                );
            }

            BatchResult {
                count,
                result: SdfResult::Ok,
            }
        },
    )
}

// ============================================================================
// Gradient (Normal) Evaluation - SoA Layout (THE DEEP FRIED PATH)
// ============================================================================

/// Batch evaluate Distance AND Gradient using SoA layout - THE ULTIMATE DEEP FRIED PATH
///
/// This function computes both distance and gradient (surface normal direction)
/// for all points in a single call, using SIMD acceleration.
///
/// # Arguments
/// - `compiled`: Compiled SDF handle
/// - `x`, `y`, `z`: Input position arrays (SoA layout)
/// - `nx`, `ny`, `nz`: Output gradient/normal arrays (SoA layout)
/// - `dist`: Output distance array (optional, can be null)
/// - `count`: Number of points
///
/// # Safety
///
/// - `x`, `y`, `z` must each point to a valid array of at least `count` f32 values.
/// - `nx`, `ny`, `nz` must each point to a writable array of at least `count` f32 values.
/// - All six of the above pointers must be non-null and remain valid for the duration of
///   the call. The output slices must not alias each other or the input slices.
/// - `dist` may be null (output is skipped), but if non-null it must point to a writable
///   array of at least `count` f32 values.
/// - Arrays should be 32-byte aligned for AVX2 (recommended).
///
/// # Performance
/// - ~4x slower than distance-only (7 SDF evals vs 1)
/// - Still achieves 150M+ points/sec on modern CPU
/// - For 1M points: ~6ms
#[no_mangle]
pub unsafe extern "C" fn alice_sdf_eval_gradient_soa(
    compiled: CompiledHandle,
    x: *const f32,
    y: *const f32,
    z: *const f32,
    nx: *mut f32,
    ny: *mut f32,
    nz: *mut f32,
    dist: *mut f32,
    count: u32,
) -> BatchResult {
    ffi_guard(
        BatchResult {
            count: 0,
            result: SdfResult::Unknown,
        },
        || {
            // Null pointer checks
            if x.is_null()
                || y.is_null()
                || z.is_null()
                || nx.is_null()
                || ny.is_null()
                || nz.is_null()
            {
                return BatchResult {
                    count: 0,
                    result: SdfResult::NullPointer,
                };
            }

            let comp = match get_compiled(compiled) {
                Some(c) => c,
                None => {
                    return BatchResult {
                        count: 0,
                        result: SdfResult::InvalidHandle,
                    }
                }
            };

            let count_usize = count as usize;
            // SAFETY: Caller guarantees x, y, z are non-null pointers to valid arrays of at least count
            // f32 values each (SoA layout). Null checks performed above for x, y, z. Pointers remain
            // valid for the duration of this call and do not alias the output slices.
            let x_slice = slice::from_raw_parts(x, count_usize);
            let y_slice = slice::from_raw_parts(y, count_usize);
            let z_slice = slice::from_raw_parts(z, count_usize);
            // SAFETY: Caller guarantees nx, ny, nz are non-null, writable pointers to at least count f32
            // values each. Null checks performed above for nx, ny, nz. Pointers remain valid and do not
            // alias each other or the input slices.
            let nx_slice = slice::from_raw_parts_mut(nx, count_usize);
            let ny_slice = slice::from_raw_parts_mut(ny, count_usize);
            let nz_slice = slice::from_raw_parts_mut(nz, count_usize);
            // SAFETY: dist may legitimately be null (optional output). If non-null, caller guarantees it
            // points to a writable array of at least count f32 values that remains valid for this call.
            let mut dist_slice = if dist.is_null() {
                None
            } else {
                Some(slice::from_raw_parts_mut(dist, count_usize))
            };

            use crate::compiled::{eval_distance_and_gradient_simd, Vec3x8};
            use rayon::prelude::*;
            use wide::f32x8;

            const EPSILON: f32 = 0.001;
            const PARALLEL_THRESHOLD: usize = 1024;
            const CHUNK_SIZE: usize = 4096;

            if count_usize >= PARALLEL_THRESHOLD {
                // Parallel processing with safe chunk access
                let chunks: Vec<(usize, usize)> = (0..count_usize)
                    .step_by(CHUNK_SIZE)
                    .map(|start| (start, (start + CHUNK_SIZE).min(count_usize)))
                    .collect();

                chunks.into_par_iter().for_each(|(start, end)| {
                    let chunk_len = end - start;
                    let simd_iters = chunk_len / 8;

                    // SIMD loop (8 points at a time)
                    for s in 0..simd_iters {
                        let base = start + s * 8;

                        // Load 8 points from SoA arrays
                        let vx = f32x8::from([
                            x_slice[base],
                            x_slice[base + 1],
                            x_slice[base + 2],
                            x_slice[base + 3],
                            x_slice[base + 4],
                            x_slice[base + 5],
                            x_slice[base + 6],
                            x_slice[base + 7],
                        ]);
                        let vy = f32x8::from([
                            y_slice[base],
                            y_slice[base + 1],
                            y_slice[base + 2],
                            y_slice[base + 3],
                            y_slice[base + 4],
                            y_slice[base + 5],
                            y_slice[base + 6],
                            y_slice[base + 7],
                        ]);
                        let vz = f32x8::from([
                            z_slice[base],
                            z_slice[base + 1],
                            z_slice[base + 2],
                            z_slice[base + 3],
                            z_slice[base + 4],
                            z_slice[base + 5],
                            z_slice[base + 6],
                            z_slice[base + 7],
                        ]);

                        let p = Vec3x8 {
                            x: vx,
                            y: vy,
                            z: vz,
                        };

                        // Compute distance and gradient in one call
                        let (d, gx, gy, gz) = eval_distance_and_gradient_simd(&comp, p, EPSILON);

                        // Store results
                        let d_arr: [f32; 8] = d.to_array();
                        let gx_arr: [f32; 8] = gx.to_array();
                        let gy_arr: [f32; 8] = gy.to_array();
                        let gz_arr: [f32; 8] = gz.to_array();

                        for i in 0..8 {
                            // Normalize gradient to get unit normal
                            let len = gz_arr[i]
                                .mul_add(
                                    gz_arr[i],
                                    gx_arr[i].mul_add(gx_arr[i], gy_arr[i] * gy_arr[i]),
                                )
                                .sqrt();
                            let inv_len = if len > 1e-8 { 1.0 / len } else { 0.0 };

                            // SAFETY: Output slices were created from raw pointers with validated count.
                            // Loop bounds (base + i < count) ensure we stay within the allocated region.
                            // cast_mut() on a shared-slice pointer is safe here because Rayon chunk
                            // partitioning ensures no two tasks write to the same index concurrently.
                            *nx_slice.as_ptr().add(base + i).cast_mut() = gx_arr[i] * inv_len;
                            *ny_slice.as_ptr().add(base + i).cast_mut() = gy_arr[i] * inv_len;
                            *nz_slice.as_ptr().add(base + i).cast_mut() = gz_arr[i] * inv_len;
                        }

                        if let Some(ref ds) = dist_slice {
                            for (i, &d) in d_arr.iter().enumerate() {
                                // SAFETY: Output slices were created from raw pointers with validated count.
                                // Loop bounds (base + i < count) ensure we stay within the allocated region.
                                *ds.as_ptr().add(base + i).cast_mut() = d;
                            }
                        }
                    }

                    // Handle remainder (non-SIMD)
                    let remainder_start = start + simd_iters * 8;
                    for i in remainder_start..end {
                        let p = glam::Vec3::new(x_slice[i], y_slice[i], z_slice[i]);
                        let d = eval_compiled(&comp, p);

                        // Finite difference gradient
                        let eps = EPSILON;
                        let dx = eval_compiled(&comp, p + glam::Vec3::X * eps)
                            - eval_compiled(&comp, p - glam::Vec3::X * eps);
                        let dy = eval_compiled(&comp, p + glam::Vec3::Y * eps)
                            - eval_compiled(&comp, p - glam::Vec3::Y * eps);
                        let dz = eval_compiled(&comp, p + glam::Vec3::Z * eps)
                            - eval_compiled(&comp, p - glam::Vec3::Z * eps);

                        let len = dz.mul_add(dz, dx.mul_add(dx, dy * dy)).sqrt();
                        let inv_len = if len > 1e-8 { 1.0 / len } else { 0.0 };

                        // SAFETY: Output slices were created from raw pointers with validated count.
                        // Loop bounds (remainder_start <= i < end <= count) ensure we stay within the
                        // allocated region. cast_mut() is safe because Rayon chunk partitioning ensures
                        // no two tasks write to the same index concurrently.
                        *nx_slice.as_ptr().add(i).cast_mut() = dx * inv_len;
                        *ny_slice.as_ptr().add(i).cast_mut() = dy * inv_len;
                        *nz_slice.as_ptr().add(i).cast_mut() = dz * inv_len;

                        if let Some(ref ds) = dist_slice {
                            // SAFETY: Output slices were created from raw pointers with validated count.
                            // Loop bounds (remainder_start <= i < end <= count) ensure we stay in bounds.
                            *ds.as_ptr().add(i).cast_mut() = d;
                        }
                    }
                });
            } else {
                // Sequential for small batches
                for i in 0..count_usize {
                    let p = glam::Vec3::new(x_slice[i], y_slice[i], z_slice[i]);
                    let d = eval_compiled(&comp, p);

                    let eps = EPSILON;
                    let dx = eval_compiled(&comp, p + glam::Vec3::X * eps)
                        - eval_compiled(&comp, p - glam::Vec3::X * eps);
                    let dy = eval_compiled(&comp, p + glam::Vec3::Y * eps)
                        - eval_compiled(&comp, p - glam::Vec3::Y * eps);
                    let dz = eval_compiled(&comp, p + glam::Vec3::Z * eps)
                        - eval_compiled(&comp, p - glam::Vec3::Z * eps);

                    let len = dz.mul_add(dz, dx.mul_add(dx, dy * dy)).sqrt();
                    let inv_len = if len > 1e-8 { 1.0 / len } else { 0.0 };

                    nx_slice[i] = dx * inv_len;
                    ny_slice[i] = dy * inv_len;
                    nz_slice[i] = dz * inv_len;

                    if let Some(ref mut ds) = dist_slice {
                        ds[i] = d;
                    }
                }
            }

            BatchResult {
                count,
                result: SdfResult::Ok,
            }
        },
    )
}

// ============================================================================
// Animated Compiled Evaluation (Zero-Copy)
// ============================================================================

/// Evaluate compiled SDF with animation transform (zero-allocation per frame)
///
/// Instead of rebuilding the SDF tree every frame, this applies the inverse
/// transform to the query point and evaluates against the pre-compiled base shape.
///
/// # Parameters
/// - `compiled`: Pre-compiled base shape
/// - `params`: Pointer to AnimationParams struct (36 bytes)
/// - `x`, `y`, `z`: Query point coordinates
///
/// # Safety
///
/// - `params` must point to a valid, properly initialized `AnimationParams` struct that remains
///   valid for the duration of the call. The pointer must be non-null and correctly aligned.
///
/// # Returns
/// Signed distance at the query point
#[no_mangle]
pub unsafe extern "C" fn alice_sdf_eval_animated_compiled(
    compiled: CompiledHandle,
    params: *const AnimationParams,
    x: f32,
    y: f32,
    z: f32,
) -> f32 {
    ffi_guard(f32::MAX, || {
        if params.is_null() {
            return f32::MAX;
        }

        let comp = match get_compiled(compiled) {
            Some(c) => c,
            None => return f32::MAX,
        };

        // SAFETY: Caller guarantees params is a non-null pointer to a valid, initialized
        // AnimationParams struct. Null check performed above. The struct remains valid for
        // this call. We only read through the reference; no mutation occurs.
        let params_ref = &*params;
        crate::animation::eval_animated_compiled(&comp, params_ref, glam::Vec3::new(x, y, z))
    })
}

/// Batch evaluate compiled SDF with animation transform using SoA layout
///
/// Combines zero-copy animation with SIMD SoA evaluation for maximum
/// throughput on animated particle systems.
///
/// # Parameters
/// - `compiled`: Pre-compiled base shape
/// - `params`: Pointer to AnimationParams struct (shared for all points)
/// - `x`, `y`, `z`: Input position arrays (SoA layout)
/// - `distances`: Output array (caller-allocated)
/// - `count`: Number of points
///
/// # Safety
///
/// - `params` must point to a valid, properly initialized `AnimationParams` struct that remains
///   valid for the duration of the call. Must be non-null and correctly aligned.
/// - `x`, `y`, and `z` must each point to a valid array of at least `count` f32 values.
/// - `distances` must point to a writable array of at least `count` f32 values.
/// - All five pointers must be non-null and remain valid for the entire duration of the call.
#[no_mangle]
pub unsafe extern "C" fn alice_sdf_eval_animated_batch_soa(
    compiled: CompiledHandle,
    params: *const AnimationParams,
    x: *const f32,
    y: *const f32,
    z: *const f32,
    distances: *mut f32,
    count: u32,
) -> BatchResult {
    ffi_guard(
        BatchResult {
            count: 0,
            result: SdfResult::Unknown,
        },
        || {
            if params.is_null() || x.is_null() || y.is_null() || z.is_null() || distances.is_null()
            {
                return BatchResult {
                    count: 0,
                    result: SdfResult::NullPointer,
                };
            }

            let comp = match get_compiled(compiled) {
                Some(c) => c,
                None => {
                    return BatchResult {
                        count: 0,
                        result: SdfResult::InvalidHandle,
                    }
                }
            };

            // SAFETY: Caller guarantees params is a non-null pointer to a valid, initialized
            // AnimationParams struct. Null check performed above. The struct remains valid for
            // this call. We only read through the reference; no mutation occurs.
            let params_ref = &*params;
            let count_usize = count as usize;
            // SAFETY: Caller guarantees x, y, z are non-null pointers to valid arrays of at least count
            // f32 values each (SoA layout). Null checks performed above. Pointers remain valid for this
            // call and do not alias the output distances slice.
            let x_slice = slice::from_raw_parts(x, count_usize);
            let y_slice = slice::from_raw_parts(y, count_usize);
            let z_slice = slice::from_raw_parts(z, count_usize);
            // SAFETY: Caller guarantees distances is a non-null, writable pointer to at least count f32
            // values. Null check performed above. Does not alias x, y, or z slices.
            let dist_slice = slice::from_raw_parts_mut(distances, count_usize);

            // Apply inverse animation transform to each point, then evaluate via SIMD
            let has_transform =
                params_ref.has_translation() || params_ref.has_rotation() || params_ref.has_scale();

            if !has_transform {
                // No animation transform: use direct SIMD SoA path
                crate::compiled::eval_compiled_batch_soa_raw(
                    &comp,
                    x,
                    y,
                    z,
                    distances,
                    count_usize,
                );
            } else {
                // Transform points then evaluate
                // For SIMD efficiency, we transform in 8-wide batches
                let simd_iters = count_usize / 8;

                for s in 0..simd_iters {
                    let base = s * 8;

                    // Load 8 points
                    let mut px = [0.0f32; 8];
                    let mut py = [0.0f32; 8];
                    let mut pz = [0.0f32; 8];
                    let mut scale_corrections = [1.0f32; 8];

                    for i in 0..8 {
                        let point = glam::Vec3::new(
                            x_slice[base + i],
                            y_slice[base + i],
                            z_slice[base + i],
                        );
                        let (tp, sc) = params_ref.transform_point(point);
                        px[i] = tp.x;
                        py[i] = tp.y;
                        pz[i] = tp.z;
                        scale_corrections[i] = sc;
                    }

                    // Evaluate transformed points via SIMD
                    let vx = wide::f32x8::from(px);
                    let vy = wide::f32x8::from(py);
                    let vz = wide::f32x8::from(pz);
                    let p = crate::compiled::Vec3x8 {
                        x: vx,
                        y: vy,
                        z: vz,
                    };
                    let raw_distances = crate::compiled::eval_compiled_simd(&comp, p);
                    let raw_arr: [f32; 8] = raw_distances.into();

                    // Apply scale correction
                    for i in 0..8 {
                        dist_slice[base + i] = raw_arr[i] * scale_corrections[i];
                    }
                }

                // Handle remainder
                let remainder_start = simd_iters * 8;
                for i in remainder_start..count_usize {
                    let point = glam::Vec3::new(x_slice[i], y_slice[i], z_slice[i]);
                    dist_slice[i] =
                        crate::animation::eval_animated_compiled(&comp, params_ref, point);
                }
            }

            BatchResult {
                count,
                result: SdfResult::Ok,
            }
        },
    )
}

// ============================================================================
// Tests
// ============================================================================

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

    /// Every exported function catches panics: a call that trips an internal
    /// panic returns its sentinel and leaves a message for
    /// `alice_sdf_last_error` instead of unwinding into the C caller (which
    /// aborts the host process).
    #[test]
    fn panic_inside_ffi_returns_sentinel_and_message() {
        use super::super::guard::{clear_last_error, take_last_error};
        clear_last_error();
        // Direct guard check with the same sentinel the batch functions use.
        let r = ffi_guard(
            BatchResult {
                count: 0,
                result: SdfResult::Unknown,
            },
            || -> BatchResult { panic!("internal invariant violated") },
        );
        assert_eq!(r.result, SdfResult::Unknown);
        assert_eq!(r.count, 0);
        let msg = take_last_error().expect("panic message recorded");
        assert!(msg.contains("internal invariant violated"), "{msg}");

        // Real exported entry points with hostile input never unwind.
        let node = super::super::primitives::alice_sdf_sphere(1.0);
        let mesh = super::super::mesh::alice_sdf_generate_mesh(node, 0, 1.0);
        // resolution 0: either an empty mesh handle or the sentinel, never an abort
        if !mesh.is_null() {
            super::super::mesh::alice_sdf_free_mesh(mesh);
        }
        let mut dist = [0.0f32; 1];
        let r = unsafe { alice_sdf_eval_batch(node, std::ptr::null(), dist.as_mut_ptr(), 1) };
        assert_ne!(r.result, SdfResult::Ok);
        super::super::memory::alice_sdf_free(node);
        clear_last_error();
    }
    use crate::ffi::compilation::{alice_sdf_compile, alice_sdf_free_compiled};
    use crate::ffi::memory::alice_sdf_free;
    use crate::ffi::primitives::alice_sdf_sphere;

    #[test]
    fn test_compile_and_eval() {
        let sphere = alice_sdf_sphere(1.0);
        assert!(!sphere.is_null());

        let compiled = alice_sdf_compile(sphere);
        assert!(!compiled.is_null());

        // Eval at origin (inside)
        let d = alice_sdf_eval_compiled(compiled, 0.0, 0.0, 0.0);
        assert!((d + 1.0).abs() < 0.001);

        // Eval on surface
        let d = alice_sdf_eval_compiled(compiled, 1.0, 0.0, 0.0);
        assert!(d.abs() < 0.001);

        alice_sdf_free_compiled(compiled);
        alice_sdf_free(sphere);
    }

    #[test]
    fn test_batch_eval() {
        let sphere = alice_sdf_sphere(1.0);
        let compiled = alice_sdf_compile(sphere);

        let points: [f32; 9] = [
            0.0, 0.0, 0.0, // inside
            1.0, 0.0, 0.0, // on surface
            2.0, 0.0, 0.0, // outside
        ];
        let mut distances: [f32; 3] = [0.0; 3];

        // SAFETY: points is a valid local array of 9 f32 (3 points x 3 floats). distances is a
        // valid local array of 3 f32. Both remain valid for the duration of the call. compiled is
        // a valid handle obtained above.
        unsafe {
            let result =
                alice_sdf_eval_compiled_batch(compiled, points.as_ptr(), distances.as_mut_ptr(), 3);
            assert_eq!(result.result, SdfResult::Ok);
            assert_eq!(result.count, 3);
        }

        assert!((distances[0] + 1.0).abs() < 0.001); // inside
        assert!(distances[1].abs() < 0.001); // surface
        assert!((distances[2] - 1.0).abs() < 0.001); // outside

        alice_sdf_free_compiled(compiled);
        alice_sdf_free(sphere);
    }

    #[test]
    fn test_animated_compiled_eval() {
        let sphere = alice_sdf_sphere(1.0);
        let compiled = alice_sdf_compile(sphere);

        // No animation: origin should be inside sphere
        let params = AnimationParams {
            scale: 1.0,
            ..Default::default()
        };
        // SAFETY: params is a valid, initialized AnimationParams local struct. compiled is a
        // valid handle. The reference &params lives for the duration of the call.
        unsafe {
            let d = alice_sdf_eval_animated_compiled(compiled, &raw const params, 0.0, 0.0, 0.0);
            assert!((d + 1.0).abs() < 0.01);
        }

        // Translated by 5 on X: origin should be outside
        let params = AnimationParams {
            translate_x: 5.0,
            scale: 1.0,
            ..Default::default()
        };
        // SAFETY: params is a valid, initialized AnimationParams local struct. compiled is a
        // valid handle. The reference &params lives for the duration of the call.
        unsafe {
            let d = alice_sdf_eval_animated_compiled(compiled, &raw const params, 0.0, 0.0, 0.0);
            assert!(d > 0.0);
        }

        alice_sdf_free_compiled(compiled);
        alice_sdf_free(sphere);
    }

    #[test]
    fn test_animated_batch_soa() {
        let sphere = alice_sdf_sphere(1.0);
        let compiled = alice_sdf_compile(sphere);

        let params = AnimationParams {
            translate_x: 5.0,
            scale: 1.0,
            ..Default::default()
        };

        let x: [f32; 4] = [0.0, 5.0, 10.0, 5.0];
        let y: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
        let z: [f32; 4] = [0.0, 0.0, 0.0, 0.0];
        let mut distances: [f32; 4] = [0.0; 4];

        // SAFETY: params, x, y, z, distances are all valid local arrays/structs. x, y, z each
        // have 4 f32 elements; distances has 4 f32 elements; all are valid for the call duration.
        // compiled is a valid handle obtained above.
        unsafe {
            let result = alice_sdf_eval_animated_batch_soa(
                compiled,
                &raw const params,
                x.as_ptr(),
                y.as_ptr(),
                z.as_ptr(),
                distances.as_mut_ptr(),
                4,
            );
            assert_eq!(result.result, SdfResult::Ok);
        }

        // x=0 with translate_x=5 -> sphere center at 5, query at 0 -> distance ~4
        assert!(distances[0] > 3.0);
        // x=5 with translate_x=5 -> sphere center at 5, query at 5 -> inside (-1)
        assert!((distances[1] + 1.0).abs() < 0.01);

        alice_sdf_free_compiled(compiled);
        alice_sdf_free(sphere);
    }

    #[test]
    fn test_soa_eval() {
        let sphere = alice_sdf_sphere(1.0);
        let compiled = alice_sdf_compile(sphere);

        let x: [f32; 4] = [0.0, 1.0, 2.0, 0.5];
        let y: [f32; 4] = [0.0, 0.0, 0.0, 0.5];
        let z: [f32; 4] = [0.0, 0.0, 0.0, 0.5];
        let mut distances: [f32; 4] = [0.0; 4];

        // SAFETY: x, y, z each have 4 f32 elements (SoA layout). distances has 4 f32 elements.
        // All local arrays are valid and live for the duration of the call. compiled is a valid
        // handle obtained above.
        unsafe {
            let result = alice_sdf_eval_soa(
                compiled,
                x.as_ptr(),
                y.as_ptr(),
                z.as_ptr(),
                distances.as_mut_ptr(),
                4,
            );
            assert_eq!(result.result, SdfResult::Ok);
        }

        assert!((distances[0] + 1.0).abs() < 0.001); // origin
        assert!(distances[1].abs() < 0.001); // surface
        assert!((distances[2] - 1.0).abs() < 0.001); // outside

        alice_sdf_free_compiled(compiled);
        alice_sdf_free(sphere);
    }
}