oxigaf-render 0.1.2

Differentiable 3D Gaussian Splatting rasterizer (wgpu)
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
//! CPU-side spherical harmonics utilities for 3D Gaussian Splatting.
//!
//! Implements real SH basis evaluation, coefficient-to-color conversion,
//! Monte Carlo projection, degree conversion, DC operations, Z-axis rotation,
//! and band energy computation — all matching the 3DGS shader convention.
//!
//! ## Coefficient Layout
//!
//! Functions operating on multi-channel SH use **per-channel** layout:
//! `[r0..rN, g0..gN, b0..bN]` where `N = (degree+1)^2` coefficients per channel.
//! Total length = `3 * N`.
//!
//! This is **not** the layout the GPU rasterizer's SH coefficient buffer
//! uses: the shaders (`preprocess.wgsl`, `preprocess_bwd.wgsl`,
//! `preprocess_sh{1,2,3}.wgsl`) read a per-coefficient **interleaved**
//! layout, `sh_offset + 3*k + c` for basis index `k` and channel `c`
//! (i.e. `[r0,g0,b0, r1,g1,b1, ...]`). The two layouts are only
//! basis-value-compatible (see [`sh_eval_degree1`]'s sign convention
//! note), not memory-layout-compatible: converting between them requires
//! an explicit transpose, which this module does not currently provide.

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// L=0 normalisation: 1 / (2√π).
pub const SH_C0: f32 = 0.282_094_8_f32;

/// L=1 normalisation: √(3/(4π)).
pub const SH_C1: f32 = 0.488_602_52_f32;

/// L=2 normalisations (one per m-index: m = −2,−1,0,+1,+2).
///
/// Signs match the 3DGS shader convention (`preprocess.wgsl` /
/// `sh_eval.wgsl`'s `SH_C2_0..SH_C2_4`), not the naive all-positive
/// `√(...)` magnitudes: the `yz` (m=−1) and `xz` (m=+1) terms carry a
/// negative sign here so `sh_eval_degree2`'s output matches what the GPU
/// rasterizer evaluates from the same coefficients.
pub const SH_C2: [f32; 5] = [
    1.092_548_5_f32,  // m=−2 (xy):  +√(15/(4π))
    -1.092_548_5_f32, // m=−1 (yz):  −√(15/(4π))
    0.315_391_57_f32, // m= 0:       +√(5/(16π))
    -1.092_548_5_f32, // m=+1 (xz):  −√(15/(4π))
    0.546_274_24_f32, // m=+2:       +√(15/(16π))
];

/// L=3 normalisations (one per m-index: m = −3..=+3).
///
/// Signs match the 3DGS shader convention (`SH_C3_0..SH_C3_6`); see the
/// note on [`SH_C2`].
pub const SH_C3: [f32; 7] = [
    -0.590_043_6_f32, // m=−3: −√(35/(32π)) * √2
    2.890_611_4_f32,  // m=−2: +√(105/(4π))
    -0.457_045_8_f32, // m=−1: −√(21/(32π)) * √2
    0.373_176_34_f32, // m= 0: +√(7/(16π))
    -0.457_045_8_f32, // m=+1: −√(21/(32π)) * √2
    1.445_305_7_f32,  // m=+2: +√(105/(16π))
    -0.590_043_6_f32, // m=+3: −√(35/(32π)) * √2
];

// ---------------------------------------------------------------------------
// Error type
// ---------------------------------------------------------------------------

/// Errors produced by spherical harmonics operations.
#[derive(Debug, thiserror::Error)]
pub enum ShError {
    /// SH degree > 3 is not supported.
    #[error("Unsupported SH degree: {0}, max is 3")]
    UnsupportedDegree(usize),

    /// Coefficient slice length does not match the expected count.
    #[error("Coefficient count mismatch: expected {expected}, got {got}")]
    CoeffCountMismatch { expected: usize, got: usize },

    /// Supplied direction vector has near-zero length.
    #[error("Direction vector must be non-zero")]
    ZeroDirection,

    /// Rotation matrix supplied to a function is not 3×3 row-major.
    #[error("Rotation matrix must be 3x3 row-major")]
    InvalidRotationMatrix,
}

// ---------------------------------------------------------------------------
// ShBasis descriptor
// ---------------------------------------------------------------------------

/// Descriptor for a particular SH degree.
#[derive(Debug, Clone)]
pub struct ShBasis {
    /// Maximum SH degree (0–3).
    pub degree: usize,
    /// Total number of basis functions: `(degree+1)^2`.
    pub num_coeffs: usize,
}

impl ShBasis {
    /// Construct from degree, or error if > 3.
    pub fn new(degree: usize) -> Result<Self, ShError> {
        if degree > 3 {
            return Err(ShError::UnsupportedDegree(degree));
        }
        Ok(Self {
            degree,
            num_coeffs: sh_num_coeffs(degree),
        })
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Returns the number of real SH basis functions through the given degree: `(degree+1)^2`.
#[inline]
pub fn sh_num_coeffs(degree: usize) -> usize {
    (degree + 1) * (degree + 1)
}

/// Normalize a direction; error if norm < 1e-7.
#[inline]
fn normalize(direction: [f32; 3]) -> Result<[f32; 3], ShError> {
    let [x, y, z] = direction;
    let norm = (x * x + y * y + z * z).sqrt();
    if norm < 1e-7 {
        return Err(ShError::ZeroDirection);
    }
    Ok([x / norm, y / norm, z / norm])
}

// ---------------------------------------------------------------------------
// Per-degree basis evaluation
// ---------------------------------------------------------------------------

/// Evaluate the single L=0 SH basis value.
#[inline]
pub fn sh_eval_degree0(_x: f32, _y: f32, _z: f32) -> [f32; 1] {
    [SH_C0]
}

/// Evaluate L=0 and L=1 SH basis values (4 total).
///
/// Output order: `[Y_0^0, Y_1^{-1}, Y_1^0, Y_1^1]`
///
/// The m=−1 and m=+1 terms carry a negative sign (`-SH_C1 * y`,
/// `-SH_C1 * x`) to match the 3DGS shader convention (`preprocess.wgsl` /
/// `sh_eval.wgsl` evaluate `SH_C1 * vec3(-y, z, -x)`); only `SH_C1` itself
/// (a normalisation magnitude) stays positive.
#[inline]
pub fn sh_eval_degree1(x: f32, y: f32, z: f32) -> [f32; 4] {
    [SH_C0, -SH_C1 * y, SH_C1 * z, -SH_C1 * x]
}

/// Evaluate L=0..=2 SH basis values (9 total).
///
/// Output appends L=2 to `sh_eval_degree1`:
/// `[..., Y_2^{-2}, Y_2^{-1}, Y_2^0, Y_2^1, Y_2^2]`
#[inline]
pub fn sh_eval_degree2(x: f32, y: f32, z: f32) -> [f32; 9] {
    let [b0, b1, b2, b3] = sh_eval_degree1(x, y, z);
    [
        b0,
        b1,
        b2,
        b3,
        SH_C2[0] * x * y,
        SH_C2[1] * y * z,
        SH_C2[2] * (2.0 * z * z - x * x - y * y),
        SH_C2[3] * x * z,
        SH_C2[4] * (x * x - y * y),
    ]
}

/// Evaluate L=0..=3 SH basis values (16 total).
///
/// Output appends L=3 to `sh_eval_degree2`:
/// `[..., Y_3^{-3}, Y_3^{-2}, Y_3^{-1}, Y_3^0, Y_3^1, Y_3^2, Y_3^3]`
#[inline]
pub fn sh_eval_degree3(x: f32, y: f32, z: f32) -> [f32; 16] {
    let [b0, b1, b2, b3, b4, b5, b6, b7, b8] = sh_eval_degree2(x, y, z);
    [
        b0,
        b1,
        b2,
        b3,
        b4,
        b5,
        b6,
        b7,
        b8,
        SH_C3[0] * y * (3.0 * x * x - y * y),
        SH_C3[1] * x * y * z,
        SH_C3[2] * y * (4.0 * z * z - x * x - y * y),
        SH_C3[3] * z * (2.0 * z * z - 3.0 * x * x - 3.0 * y * y),
        SH_C3[4] * x * (4.0 * z * z - x * x - y * y),
        SH_C3[5] * z * (x * x - y * y),
        SH_C3[6] * x * (x * x - 3.0 * y * y),
    ]
}

/// Evaluate real SH basis up to `degree` for `direction` (normalises first).
///
/// Returns a `Vec<f32>` of length `(degree+1)^2`.
pub fn sh_eval(direction: [f32; 3], degree: usize) -> Result<Vec<f32>, ShError> {
    if degree > 3 {
        return Err(ShError::UnsupportedDegree(degree));
    }
    let [x, y, z] = normalize(direction)?;
    let out: Vec<f32> = match degree {
        0 => sh_eval_degree0(x, y, z).to_vec(),
        1 => sh_eval_degree1(x, y, z).to_vec(),
        2 => sh_eval_degree2(x, y, z).to_vec(),
        _ => sh_eval_degree3(x, y, z).to_vec(),
    };
    Ok(out)
}

// ---------------------------------------------------------------------------
// SH coefficients → RGB color
// ---------------------------------------------------------------------------

/// Convert per-channel SH coefficients to a view-dependent RGB colour.
///
/// `sh_coeffs` layout: `[r_0..r_N, g_0..g_N, b_0..b_N]` where `N = (degree+1)^2`.
/// Total required length: `3 * N`. This is the **per-channel** layout (see
/// the module docs); it is not memory-layout-compatible with the GPU
/// rasterizer's interleaved SH buffer, though the basis values themselves
/// (via [`sh_eval`]) now match the shader's sign convention.
///
/// Returns clamped `[0, 1]` RGB: `color = 0.5 + Σ_k (c_k * Y_k)`.
pub fn sh_to_color(
    sh_coeffs: &[f32],
    direction: [f32; 3],
    degree: usize,
) -> Result<[f32; 3], ShError> {
    if degree > 3 {
        return Err(ShError::UnsupportedDegree(degree));
    }
    let n = sh_num_coeffs(degree);
    let expected = 3 * n;
    if sh_coeffs.len() != expected {
        return Err(ShError::CoeffCountMismatch {
            expected,
            got: sh_coeffs.len(),
        });
    }
    let basis = sh_eval(direction, degree)?;
    let mut rgb = [0.5_f32; 3];
    for (ch, rgb_val) in rgb.iter_mut().enumerate() {
        let offset = ch * n;
        for (k, &basis_val) in basis.iter().enumerate().take(n) {
            *rgb_val += sh_coeffs[offset + k] * basis_val;
        }
    }
    Ok([
        rgb[0].clamp(0.0, 1.0),
        rgb[1].clamp(0.0, 1.0),
        rgb[2].clamp(0.0, 1.0),
    ])
}

// ---------------------------------------------------------------------------
// DC helpers
// ---------------------------------------------------------------------------

/// Convert degree-0 SH coefficients to RGB (with 3DGS +0.5 offset), clamped \[0,1\].
#[inline]
pub fn sh_dc_to_color(dc_r: f32, dc_g: f32, dc_b: f32) -> [f32; 3] {
    [
        (0.5 + SH_C0 * dc_r).clamp(0.0, 1.0),
        (0.5 + SH_C0 * dc_g).clamp(0.0, 1.0),
        (0.5 + SH_C0 * dc_b).clamp(0.0, 1.0),
    ]
}

/// Inverse of `sh_dc_to_color`: convert linear RGB to degree-0 SH coefficients.
#[inline]
pub fn color_to_sh_dc(r: f32, g: f32, b: f32) -> [f32; 3] {
    [(r - 0.5) / SH_C0, (g - 0.5) / SH_C0, (b - 0.5) / SH_C0]
}

// ---------------------------------------------------------------------------
// Monte Carlo SH projection
// ---------------------------------------------------------------------------

/// Project a direction-indexed RGB function to SH coefficients via Monte Carlo integration.
///
/// `samples`: slice of `(unit_direction, rgb_color)` pairs (uniform sphere distribution).
///
/// Returns per-channel coefficients: `[r_0..r_N, g_0..g_N, b_0..b_N]`, length `3*N`.
/// Weight per sample: `4π / num_samples`.
pub fn sh_project_monte_carlo(
    samples: &[([f32; 3], [f32; 3])],
    degree: usize,
) -> Result<Vec<f32>, ShError> {
    if degree > 3 {
        return Err(ShError::UnsupportedDegree(degree));
    }
    let n = sh_num_coeffs(degree);
    let num = samples.len();
    let mut coeffs = vec![0.0_f32; 3 * n];
    if num == 0 {
        return Ok(coeffs);
    }
    let weight = 4.0 * std::f32::consts::PI / num as f32;
    for (dir, rgb) in samples {
        let basis = sh_eval(*dir, degree)?;
        for k in 0..n {
            for ch in 0..3 {
                coeffs[ch * n + k] += rgb[ch] * basis[k] * weight;
            }
        }
    }
    // Normalize by 1/N (weight already includes the 4π factor but not the 1/N division)
    // Actually weight = 4π/N already encodes the 1/N factor — accumulation is a sum,
    // so dividing by N again would double-count. The sum itself IS the integral estimate.
    Ok(coeffs)
}

// ---------------------------------------------------------------------------
// Degree conversion
// ---------------------------------------------------------------------------

/// Upsample SH coefficients from `from_degree` to `to_degree` by zero-padding.
pub fn sh_upsample(
    coeffs: &[f32],
    from_degree: usize,
    to_degree: usize,
) -> Result<Vec<f32>, ShError> {
    if from_degree > 3 {
        return Err(ShError::UnsupportedDegree(from_degree));
    }
    if to_degree > 3 {
        return Err(ShError::UnsupportedDegree(to_degree));
    }
    let n_from = sh_num_coeffs(from_degree);
    let expected = 3 * n_from;
    if coeffs.len() != expected {
        return Err(ShError::CoeffCountMismatch {
            expected,
            got: coeffs.len(),
        });
    }
    if to_degree <= from_degree {
        return Ok(coeffs.to_vec());
    }
    let n_to = sh_num_coeffs(to_degree);
    let mut out = vec![0.0_f32; 3 * n_to];
    for ch in 0..3 {
        let src_offset = ch * n_from;
        let dst_offset = ch * n_to;
        out[dst_offset..dst_offset + n_from]
            .copy_from_slice(&coeffs[src_offset..src_offset + n_from]);
    }
    Ok(out)
}

/// Downsample SH coefficients from `from_degree` to `to_degree` by truncation.
pub fn sh_downsample(
    coeffs: &[f32],
    from_degree: usize,
    to_degree: usize,
) -> Result<Vec<f32>, ShError> {
    if from_degree > 3 {
        return Err(ShError::UnsupportedDegree(from_degree));
    }
    if to_degree > 3 {
        return Err(ShError::UnsupportedDegree(to_degree));
    }
    let n_from = sh_num_coeffs(from_degree);
    let expected = 3 * n_from;
    if coeffs.len() != expected {
        return Err(ShError::CoeffCountMismatch {
            expected,
            got: coeffs.len(),
        });
    }
    if to_degree >= from_degree {
        return Ok(coeffs.to_vec());
    }
    let n_to = sh_num_coeffs(to_degree);
    let mut out = vec![0.0_f32; 3 * n_to];
    for ch in 0..3 {
        let src_offset = ch * n_from;
        let dst_offset = ch * n_to;
        out[dst_offset..dst_offset + n_to].copy_from_slice(&coeffs[src_offset..src_offset + n_to]);
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// Z-axis rotation
// ---------------------------------------------------------------------------

/// Rotate SH coefficients around the Z axis by `angle_rad`.
///
/// For each band `l` and signed order `m ≠ 0`:
/// - `new[l^{+|m|}] = old[l^{+|m|}] * cos(m*θ) − old[l^{-|m|}] * sin(m*θ)`
/// - `new[l^{-|m|}] = old[l^{-|m|}] * cos(m*θ) + old[l^{+|m|}] * sin(m*θ)`
///
/// `m=0` coefficients are unchanged. Applied independently per channel.
pub fn rotate_sh_coeffs_z(
    coeffs: &[f32],
    angle_rad: f32,
    degree: usize,
) -> Result<Vec<f32>, ShError> {
    if degree > 3 {
        return Err(ShError::UnsupportedDegree(degree));
    }
    let n = sh_num_coeffs(degree);
    let expected = 3 * n;
    if coeffs.len() != expected {
        return Err(ShError::CoeffCountMismatch {
            expected,
            got: coeffs.len(),
        });
    }
    let mut out = coeffs.to_vec();
    for l in 1..=degree {
        for m in 1..=l {
            let mf = m as f32;
            let c = (mf * angle_rad).cos();
            let s = (mf * angle_rad).sin();
            // Within a channel block, index of Y_l^{-m} is l*l + (l - m),
            // and Y_l^{+m} is l*l + (l + m).
            let idx_neg = l * l + (l - m); // Y_l^{-m}
            let idx_pos = l * l + (l + m); // Y_l^{+m}
            for ch in 0..3 {
                let base = ch * n;
                let old_pos = coeffs[base + idx_pos];
                let old_neg = coeffs[base + idx_neg];
                out[base + idx_pos] = old_pos * c - old_neg * s;
                out[base + idx_neg] = old_neg * c + old_pos * s;
            }
        }
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// Band energy
// ---------------------------------------------------------------------------

/// Compute RMS energy of one SH band across all channels.
///
/// `band`: SH band index (0, 1, 2, or 3).
/// `num_channels`: number of colour channels (normally 3).
pub fn sh_band_energy(coeffs: &[f32], band: usize, num_channels: usize) -> Result<f32, ShError> {
    if band > 3 {
        return Err(ShError::UnsupportedDegree(band));
    }
    let coeffs_per_channel = coeffs.len() / num_channels.max(1);
    let n_band = 2 * band + 1; // number of basis functions in this band
    let band_start = band * band; // l^2
    let band_end = band_start + n_band;
    if band_end > coeffs_per_channel {
        return Err(ShError::CoeffCountMismatch {
            expected: band_end * num_channels,
            got: coeffs.len(),
        });
    }
    let mut sum_sq = 0.0_f32;
    let mut count = 0_usize;
    for ch in 0..num_channels {
        let offset = ch * coeffs_per_channel;
        for k in band_start..band_end {
            let v = coeffs[offset + k];
            sum_sq += v * v;
            count += 1;
        }
    }
    if count == 0 {
        return Ok(0.0);
    }
    Ok((sum_sq / count as f32).sqrt())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    const EPS: f32 = 1e-5;

    fn approx(a: f32, b: f32, tol: f32) -> bool {
        (a - b).abs() < tol
    }

    // 1. sh_num_coeffs
    #[test]
    fn test_sh_num_coeffs() {
        assert_eq!(sh_num_coeffs(0), 1);
        assert_eq!(sh_num_coeffs(1), 4);
        assert_eq!(sh_num_coeffs(2), 9);
        assert_eq!(sh_num_coeffs(3), 16);
    }

    // 2. sh_eval_degree0 constant
    #[test]
    fn test_sh_eval_degree0_constant() {
        let dirs = [[1.0_f32, 0.0, 0.0], [0.0, 1.0, 0.0], [0.5_f32, 0.5, 0.707]];
        for [x, y, z] in dirs {
            let b = sh_eval_degree0(x, y, z);
            assert!(approx(b[0], SH_C0, EPS), "degree0 must equal SH_C0");
        }
    }

    // 3. sh_eval_degree1 for (1,0,0)
    #[test]
    fn test_sh_eval_degree1_x_axis() {
        let b = sh_eval_degree1(1.0, 0.0, 0.0);
        assert!(approx(b[0], SH_C0, EPS));
        assert!(approx(b[1], 0.0, EPS)); // -SH_C1 * y = 0
        assert!(approx(b[2], 0.0, EPS)); // SH_C1 * z = 0
        assert!(approx(b[3], -SH_C1, EPS)); // -SH_C1 * x = -SH_C1 (3DGS shader sign convention)
    }

    // 4. sh_eval normalizes non-unit vector
    #[test]
    fn test_sh_eval_normalizes() {
        let unit = sh_eval([1.0, 0.0, 0.0], 2).expect("unit ok");
        let scaled = sh_eval([3.0, 0.0, 0.0], 2).expect("scaled ok");
        for (a, b) in unit.iter().zip(scaled.iter()) {
            assert!(
                approx(*a, *b, EPS),
                "normalized and un-normalized must match"
            );
        }
    }

    // 5. sh_eval ZeroDirection
    #[test]
    fn test_sh_eval_zero_direction_error() {
        let result = sh_eval([0.0, 0.0, 0.0], 1);
        assert!(matches!(result, Err(ShError::ZeroDirection)));
    }

    // 6. sh_eval degree 4 → UnsupportedDegree
    #[test]
    fn test_sh_eval_unsupported_degree() {
        let result = sh_eval([1.0, 0.0, 0.0], 4);
        assert!(matches!(result, Err(ShError::UnsupportedDegree(4))));
    }

    // 7. sh_to_color degree 0 → constant color
    #[test]
    fn test_sh_to_color_degree0_constant() {
        // With degree 0 only, color = clamp(0.5 + SH_C0 * c_r)
        let dc_r = 0.1_f32;
        let dc_g = -0.5_f32;
        let dc_b = 0.3_f32;
        let coeffs = [dc_r, dc_g, dc_b];
        let dir1 = sh_to_color(&coeffs, [1.0, 0.0, 0.0], 0).expect("ok");
        let dir2 = sh_to_color(&coeffs, [0.0, 1.0, 0.0], 0).expect("ok");
        for ch in 0..3 {
            assert!(
                approx(dir1[ch], dir2[ch], EPS),
                "degree-0 color must be view-independent"
            );
        }
        let expected_r = (0.5 + SH_C0 * dc_r).clamp(0.0, 1.0);
        assert!(approx(dir1[0], expected_r, EPS));
    }

    // 8. sh_to_color length mismatch → error
    #[test]
    fn test_sh_to_color_length_mismatch() {
        let bad = [0.0_f32; 5]; // wrong for any degree
        let result = sh_to_color(&bad, [1.0, 0.0, 0.0], 1); // expects 3*4=12
        assert!(matches!(result, Err(ShError::CoeffCountMismatch { .. })));
    }

    // 9. sh_dc_to_color roundtrip
    #[test]
    fn test_sh_dc_to_color_roundtrip() {
        // color_to_sh_dc then sh_dc_to_color should recover original (within [0,1])
        let r = 0.6_f32;
        let g = 0.4_f32;
        let b = 0.8_f32;
        let [dr, dg, db] = color_to_sh_dc(r, g, b);
        let recovered = sh_dc_to_color(dr, dg, db);
        assert!(approx(recovered[0], r, EPS));
        assert!(approx(recovered[1], g, EPS));
        assert!(approx(recovered[2], b, EPS));
    }

    // 10. sh_dc_to_color clamps
    #[test]
    fn test_sh_dc_to_color_clamps() {
        // Very large coefficient → clamped to 1.0
        let out = sh_dc_to_color(100.0, -100.0, 0.0);
        assert!(approx(out[0], 1.0, EPS));
        assert!(approx(out[1], 0.0, EPS));
    }

    // 11. sh_upsample 0→1 adds zeros
    #[test]
    fn test_sh_upsample_0_to_1() {
        let coeffs = [0.5_f32, 0.3, 0.2]; // 3 * 1 coefficients
        let up = sh_upsample(&coeffs, 0, 1).expect("ok");
        assert_eq!(up.len(), 3 * 4); // degree-1 → 3*4=12
                                     // First element of each channel preserved
        assert!(approx(up[0], 0.5, EPS));
        assert!(approx(up[4], 0.3, EPS));
        assert!(approx(up[8], 0.2, EPS));
        // Higher elements are zero
        for &v in &up[1..4] {
            assert!(approx(v, 0.0, EPS));
        }
    }

    // 12. sh_upsample 0→2 adds zeros
    #[test]
    fn test_sh_upsample_0_to_2() {
        let coeffs = [1.0_f32, 2.0, 3.0];
        let up = sh_upsample(&coeffs, 0, 2).expect("ok");
        assert_eq!(up.len(), 3 * 9);
        assert!(approx(up[0], 1.0, EPS));
        assert!(approx(up[9], 2.0, EPS));
        assert!(approx(up[18], 3.0, EPS));
        for (i, &val) in up.iter().enumerate().take(9).skip(1) {
            assert!(approx(val, 0.0, EPS), "up[{i}] should be 0");
        }
    }

    // 13. sh_downsample 2→0 keeps DC only
    #[test]
    fn test_sh_downsample_2_to_0() {
        let mut coeffs = vec![0.0_f32; 3 * 9];
        coeffs[0] = 0.7; // r DC
        coeffs[9] = 0.5; // g DC
        coeffs[18] = 0.3; // b DC
        coeffs[1] = 99.0; // should be discarded
        let down = sh_downsample(&coeffs, 2, 0).expect("ok");
        assert_eq!(down.len(), 3);
        assert!(approx(down[0], 0.7, EPS));
        assert!(approx(down[1], 0.5, EPS));
        assert!(approx(down[2], 0.3, EPS));
    }

    // 14. sh_downsample 2→1 keeps bands 0,1
    #[test]
    fn test_sh_downsample_2_to_1() {
        let mut coeffs = vec![0.0_f32; 3 * 9];
        for i in 0..4 {
            coeffs[i] = i as f32 + 1.0; // r band 0+1
            coeffs[9 + i] = i as f32 + 5.0; // g
            coeffs[18 + i] = i as f32 + 9.0; // b
        }
        coeffs[4] = 999.0; // L=2 → discarded
        let down = sh_downsample(&coeffs, 2, 1).expect("ok");
        assert_eq!(down.len(), 3 * 4);
        for i in 0..4 {
            assert!(approx(down[i], i as f32 + 1.0, EPS));
            assert!(approx(down[4 + i], i as f32 + 5.0, EPS));
            assert!(approx(down[8 + i], i as f32 + 9.0, EPS));
        }
    }

    // 15. sh_project_monte_carlo constant function → DC coefficient matches theory
    #[test]
    fn test_sh_project_monte_carlo_constant() {
        // For a constant function f over S², the SH projection integral is:
        //   c_0 = ∫_{S²} f * Y_0^0 dΩ = f * SH_C0 * 4π
        // The estimator with weight 4π/N gives c_0 ≈ f * SH_C0 * 4π.
        use std::f32::consts::PI;
        let target = [0.6_f32, 0.4_f32, 0.2_f32];
        let n = 2000_usize;
        let golden = (1.0_f32 + 5.0_f32.sqrt()) / 2.0;
        let samples: Vec<([f32; 3], [f32; 3])> = (0..n)
            .map(|i| {
                let fi = i as f32;
                let theta = (1.0 - 2.0 * (fi + 0.5) / n as f32).clamp(-1.0, 1.0).acos();
                let phi = 2.0 * PI * fi / golden;
                let dir = [
                    theta.sin() * phi.cos(),
                    theta.cos(),
                    theta.sin() * phi.sin(),
                ];
                (dir, target)
            })
            .collect();
        let coeffs = sh_project_monte_carlo(&samples, 0).expect("ok");
        // Expected DC coefficient per channel: f * SH_C0 * 4π
        for ch in 0..3 {
            let expected = target[ch] * SH_C0 * 4.0 * PI;
            assert!(
                approx(coeffs[ch], expected, 0.05),
                "ch {ch} DC coeff: got {}, expected {expected}",
                coeffs[ch]
            );
        }
    }

    // 16. sh_band_energy for known coefficients
    #[test]
    fn test_sh_band_energy_band0() {
        // 3-channel degree-0: coeffs = [r, g, b]
        // Band 0 has 1 coeff per channel: energy = sqrt((r^2 + g^2 + b^2) / 3)
        let coeffs = [2.0_f32, 0.0, 0.0]; // 1 coeff per channel
                                          // But wait — for degree-0, num_channels * 1 = 3 coeffs total, coeffs_per_channel = 1
        let energy = sh_band_energy(&coeffs, 0, 3).expect("ok");
        let expected = ((4.0 + 0.0 + 0.0) / 3.0_f32).sqrt();
        assert!(
            approx(energy, expected, EPS),
            "got {energy}, expected {expected}"
        );
    }

    // 17. rotate_sh_coeffs_z angle=0 → identity
    #[test]
    fn test_rotate_sh_coeffs_z_zero_angle() {
        let coeffs: Vec<f32> = (0..3 * 9).map(|i| i as f32 * 0.1).collect();
        let rotated = rotate_sh_coeffs_z(&coeffs, 0.0, 2).expect("ok");
        for (a, b) in coeffs.iter().zip(rotated.iter()) {
            assert!(approx(*a, *b, EPS));
        }
    }

    // 18. rotate_sh_coeffs_z angle=2π → identity
    #[test]
    fn test_rotate_sh_coeffs_z_full_revolution() {
        let coeffs: Vec<f32> = (0..3 * 16).map(|i| (i as f32 - 24.0) * 0.05).collect();
        let rotated = rotate_sh_coeffs_z(&coeffs, 2.0 * std::f32::consts::PI, 3).expect("ok");
        for (a, b) in coeffs.iter().zip(rotated.iter()) {
            assert!(approx(*a, *b, 1e-4), "original {a} vs rotated {b}");
        }
    }

    // 19. sh_to_color degree 1 with known direction
    #[test]
    fn test_sh_to_color_degree1_known() {
        // degree-1: 4 basis, 12 coefficients
        // Layout [r0,r1,r2,r3, g0,g1,g2,g3, b0,b1,b2,b3]
        let mut coeffs = [0.0_f32; 12];
        // Set only the DC term for R channel:
        coeffs[0] = 0.2; // r_0
        coeffs[4] = 0.3; // g_0
        coeffs[8] = 0.4; // b_0
        let dir = [0.0_f32, 1.0, 0.0];
        let color = sh_to_color(&coeffs, dir, 1).expect("ok");
        let expected_r = (0.5 + SH_C0 * 0.2).clamp(0.0, 1.0);
        let expected_g = (0.5 + SH_C0 * 0.3).clamp(0.0, 1.0);
        let expected_b = (0.5 + SH_C0 * 0.4).clamp(0.0, 1.0);
        assert!(approx(color[0], expected_r, 1e-4));
        assert!(approx(color[1], expected_g, 1e-4));
        assert!(approx(color[2], expected_b, 1e-4));
    }

    // 20. sh_eval_degree2 orthogonality check at 6 cardinal directions
    #[test]
    fn test_sh_eval_degree2_cardinal_sum() {
        // Sum of Y_2^0 over 6 cardinal directions should be zero (alternating sign).
        // Y_2^0 = SH_C2[2] * (2z^2 - x^2 - y^2)
        // +x: 2(0)-1-0 = -1, -x: same, +y: 0-0-1=-1, -y: same, +z: 2-0-0=2, -z: same
        // Sum = 4*(-1)*C + 2*(2)*C = -4C + 4C = 0 ✓ (orthogonality)
        let cards = [
            [1.0_f32, 0.0, 0.0],
            [-1.0, 0.0, 0.0],
            [0.0, 1.0, 0.0],
            [0.0, -1.0, 0.0],
            [0.0, 0.0, 1.0],
            [0.0, 0.0, -1.0],
        ];
        let y20_sum: f32 = cards
            .iter()
            .map(|&[x, y, z]| {
                sh_eval_degree2(x, y, z)[6] // index 6 = Y_2^0
            })
            .sum();
        assert!(approx(y20_sum, 0.0, EPS), "Y_2^0 cardinal sum = {y20_sum}");
    }

    // 21. Regression test: CPU basis signs must match the 3DGS GPU shader
    // convention (preprocess.wgsl / sh_eval.wgsl), at every degree.
    #[test]
    fn test_sh_basis_signs_match_shader_convention() {
        // Cross-checked against the shaders' own literal constants,
        // independently of this module's SH_C1/SH_C2/SH_C3 (so a future
        // accidental sign flip in both places at once would still be
        // caught by this test).
        const SHADER_SH_C1: f32 = 0.488_602_51;
        const SHADER_SH_C2_0: f32 = 1.092_548_4;
        const SHADER_SH_C2_1: f32 = -1.092_548_4;
        const SHADER_SH_C2_2: f32 = 0.315_391_56;
        const SHADER_SH_C2_3: f32 = -1.092_548_4;
        const SHADER_SH_C2_4: f32 = 0.546_274_2;
        const SHADER_SH_C3_0: f32 = -0.590_043_6;
        const SHADER_SH_C3_1: f32 = 2.890_611_4;
        const SHADER_SH_C3_2: f32 = -0.457_045_8;
        const SHADER_SH_C3_3: f32 = 0.373_176_33;
        const SHADER_SH_C3_4: f32 = -0.457_045_8;
        const SHADER_SH_C3_5: f32 = 1.445_305_7;
        const SHADER_SH_C3_6: f32 = -0.590_043_6;

        // A generic, non-axis-aligned direction (normalized (2,1,3)) so
        // every term below is nonzero and every sign is exercised.
        let (x, y, z) = (0.534_522_5_f32, 0.267_261_24_f32, 0.801_783_7_f32);
        let tol = 1e-4;

        // Degree 1: shader computes SH_C1 * vec3(-y, z, -x).
        let b1 = sh_eval_degree1(x, y, z);
        assert!(approx(b1[1], SHADER_SH_C1 * -y, tol), "Y_1^-1: {}", b1[1]);
        assert!(approx(b1[2], SHADER_SH_C1 * z, tol), "Y_1^0: {}", b1[2]);
        assert!(approx(b1[3], SHADER_SH_C1 * -x, tol), "Y_1^1: {}", b1[3]);

        // Degree 2: shader computes SH_C2_k * (basis polynomial)_k.
        let b2 = sh_eval_degree2(x, y, z);
        assert!(approx(b2[4], SHADER_SH_C2_0 * x * y, tol), "Y_2^-2");
        assert!(approx(b2[5], SHADER_SH_C2_1 * y * z, tol), "Y_2^-1");
        assert!(
            approx(b2[6], SHADER_SH_C2_2 * (2.0 * z * z - x * x - y * y), tol),
            "Y_2^0"
        );
        assert!(approx(b2[7], SHADER_SH_C2_3 * x * z, tol), "Y_2^1");
        assert!(
            approx(b2[8], SHADER_SH_C2_4 * (x * x - y * y), tol),
            "Y_2^2"
        );

        // Degree 3: shader computes SH_C3_k * (basis polynomial)_k.
        let b3 = sh_eval_degree3(x, y, z);
        assert!(
            approx(b3[9], SHADER_SH_C3_0 * y * (3.0 * x * x - y * y), tol),
            "Y_3^-3"
        );
        assert!(approx(b3[10], SHADER_SH_C3_1 * x * y * z, tol), "Y_3^-2");
        assert!(
            approx(
                b3[11],
                SHADER_SH_C3_2 * y * (4.0 * z * z - x * x - y * y),
                tol
            ),
            "Y_3^-1"
        );
        assert!(
            approx(
                b3[12],
                SHADER_SH_C3_3 * z * (2.0 * z * z - 3.0 * x * x - 3.0 * y * y),
                tol
            ),
            "Y_3^0"
        );
        assert!(
            approx(
                b3[13],
                SHADER_SH_C3_4 * x * (4.0 * z * z - x * x - y * y),
                tol
            ),
            "Y_3^1"
        );
        assert!(
            approx(b3[14], SHADER_SH_C3_5 * z * (x * x - y * y), tol),
            "Y_3^2"
        );
        assert!(
            approx(b3[15], SHADER_SH_C3_6 * x * (x * x - 3.0 * y * y), tol),
            "Y_3^3"
        );
    }

    // 22. Regression test: this module and `crate::environment` are both
    // re-exported from the crate root, so they MUST agree basis-for-basis.
    // Historically `spherical_harmonics` shipped the naive all-positive
    // convention while `environment` (and the shaders, and `cpu_reference`)
    // used the 3DGS one, so a crate-root caller got view-dependent colour
    // mirrored relative to what the rasterizer produced.
    #[test]
    fn test_sh_eval_degree3_matches_environment_basis() {
        // Absolute tolerance rather than bit equality: the two modules
        // spell a few constants with a different final literal digit
        // (e.g. -0.590_043_6 vs -0.590_043_59) and compute Y_2^0 as
        // `2z²-x²-y²` here but `3z²-1` there — algebraically identical for
        // unit vectors, different float rounding. 1e-6 is still ~7 orders
        // of magnitude tighter than any sign flip.
        let tol = 1e-6;

        // Generic directions: every component nonzero, signs mixed, so a
        // flip in any single m-index term is caught.
        let dirs = [
            [2.0_f32, 1.0, 3.0],
            [-1.0_f32, 2.0, -0.5],
            [0.3_f32, -0.7, 0.6],
            [-0.8_f32, -0.4, -1.3],
        ];

        for dir in dirs {
            // `sh_eval_degree3` does not normalise, `sh_basis_up_to_l3`
            // documents a unit-vector precondition: feed both the exact
            // same normalised triple.
            let unit = normalize(dir).expect("direction is non-zero");
            let [x, y, z] = unit;

            let mine = sh_eval_degree3(x, y, z);
            let theirs = crate::environment::sh_basis_up_to_l3(unit);

            for (k, (a, b)) in mine.iter().zip(theirs.iter()).enumerate() {
                assert!(
                    approx(*a, *b, tol),
                    "basis {k} disagrees for dir {dir:?}: \
                     spherical_harmonics={a}, environment={b}"
                );
            }
        }
    }

    // 23. The same equivalence must hold through the public `sh_eval`
    // entry point (which normalises internally) at every supported degree.
    #[test]
    fn test_sh_eval_matches_environment_basis_all_degrees() {
        let tol = 1e-6;
        let dir = [2.0_f32, 1.0, 3.0];
        let unit = normalize(dir).expect("direction is non-zero");
        let reference = crate::environment::sh_basis_up_to_l3(unit);

        for degree in 0..=3_usize {
            let basis = sh_eval(dir, degree).expect("degree <= 3 and dir non-zero");
            assert_eq!(basis.len(), sh_num_coeffs(degree));
            for (k, value) in basis.iter().enumerate() {
                assert!(
                    approx(*value, reference[k], tol),
                    "degree {degree} basis {k}: sh_eval={value}, environment={}",
                    reference[k]
                );
            }
        }
    }
}