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
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
//! Gaussian initialization from a mesh surface.
//!
//! This module places 3D Gaussians on a triangle mesh surface using area-weighted
//! random sampling. It is designed to be used at the start of training to initialize
//! Gaussians on the FLAME face mesh before optimization.
//!
//! ## Design
//!
//! - No dependency on `oxigaf-flame` — takes raw geometry arrays.
//! - Scale is stored in log-space: `actual_scale = exp(stored_scale)`.
//! - Opacity is stored in logit-space: `actual_opacity = sigmoid(stored_opacity)`.
//! - Rotation is stored as quaternion `[x, y, z, w]`.
//! - SH coefficients: degree-0 DC term encodes color via `color = 0.5 + SH_C0 * sh_dc`.
use crate::{
deform::{build_tbn, mat3_cols_to_quat},
gaussian::{GaussianAttributes, GaussianModel},
RenderError,
};
/// The zeroth-degree spherical harmonics constant.
const SH_C0: f32 = 0.28209_f32;
/// Configuration for Gaussian initialization from a mesh surface.
#[derive(Debug, Clone)]
pub struct GaussianInitConfig {
/// Target number of Gaussians to place on the mesh surface.
pub num_gaussians: usize,
/// Scale factor multiplied with `sqrt(face_area)` to obtain actual scale.
/// Default: 0.05.
pub scale_factor: f32,
/// Minimum log-scale value (clamp from below). Default: -6.0.
pub min_scale_log: f32,
/// Maximum log-scale value (clamp from above). Default: 0.0.
pub max_scale_log: f32,
/// Initial opacity stored in logit-space. Default: `ln(0.1/0.9) ≈ -2.197`.
pub initial_opacity_logit: f32,
/// Spherical harmonics degree (0 = DC term only). Default: 0.
pub sh_degree: u32,
/// Mean color used to initialize SH DC coefficients. Default: `[0.5, 0.5, 0.5]`.
pub mean_color: [f32; 3],
/// RNG seed for reproducibility.
pub seed: u64,
}
impl Default for GaussianInitConfig {
fn default() -> Self {
Self {
num_gaussians: 10_000,
scale_factor: 0.05,
min_scale_log: -6.0,
max_scale_log: 0.0,
// ln(0.1 / 0.9)
initial_opacity_logit: (0.1_f32 / 0.9_f32).ln(),
sh_degree: 0,
mean_color: [0.5, 0.5, 0.5],
seed: 42,
}
}
}
/// A minimal xorshift64 PRNG for deterministic, dependency-free random sampling.
struct Xorshift64(u64);
impl Xorshift64 {
/// Create a new `Xorshift64` with the given seed.
///
/// If `seed` is zero, substitutes `0xDEAD_BEEF` to avoid the all-zero fixed point.
fn new(seed: u64) -> Self {
let state = if seed == 0 { 0xDEAD_BEEF_u64 } else { seed };
Self(state)
}
/// Advance the state and return the next pseudo-random `u64`.
fn next(&mut self) -> u64 {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
self.0
}
/// Return a pseudo-random `f32` in `[0, 1)`.
fn next_f32(&mut self) -> f32 {
// Build a 24-bit mantissa directly (f32 has exactly 24 bits of
// precision, including the implicit leading bit), so the integer
// numerator is always exactly representable and the quotient is
// always strictly < 1.0.
//
// The previous `(self.next() >> 11) as f32 / (1u64 << 53) as f32`
// construction produced a 53-bit numerator, and casting that to
// f32 rounds to the nearest *f32* — any value in
// `[2^53 - 2^28, 2^53)` rounds up to exactly `2^53`, making the
// quotient exactly `1.0` (violating the documented `[0, 1)` range
// roughly once every 2^25 draws).
((self.next() >> 40) as f32) / (1u32 << 24) as f32
}
}
/// Initializes a [`GaussianModel`] by sampling Gaussians on a triangle mesh surface.
///
/// Call [`GaussianInitializer::new`] once (precomputes areas), then call
/// [`GaussianInitializer::initialize`] with a [`GaussianInitConfig`] to produce a model.
pub struct GaussianInitializer {
/// Triangle face indices stored as owned data.
faces: Vec<[u32; 3]>,
/// Vertex positions stored as owned data.
vertices: Vec<[f32; 3]>,
/// Per-vertex normals (optional).
normals: Option<Vec<[f32; 3]>>,
/// Area of each triangle. Entries for degenerate (zero-area) faces are 0.0.
face_areas: Vec<f32>,
/// Prefix sum of areas (length = num_faces + 1). `prefix[i+1] = prefix[i] + area[i]`.
area_prefix: Vec<f64>,
/// Total surface area (sum of all face areas).
total_area: f64,
}
impl GaussianInitializer {
/// Build a `GaussianInitializer` from raw mesh geometry.
///
/// # Arguments
/// - `vertices`: positions of mesh vertices.
/// - `faces`: triangle face indices (each entry references three vertices).
/// - `normals`: optional per-vertex normals; if `None`, normals are computed from the triangle.
///
/// # Errors
/// Returns [`RenderError::Rasterize`] if the mesh has no faces or zero total area.
pub fn new(
vertices: &[[f32; 3]],
faces: &[[u32; 3]],
normals: Option<&[[f32; 3]]>,
) -> Result<Self, RenderError> {
if faces.is_empty() {
return Err(RenderError::Rasterize(
"Cannot initialize Gaussians from an empty mesh (no faces)".to_string(),
));
}
let num_faces = faces.len();
let mut face_areas = Vec::with_capacity(num_faces);
let mut area_prefix = Vec::with_capacity(num_faces + 1);
area_prefix.push(0.0_f64);
for (face_idx, &[i0, i1, i2]) in faces.iter().enumerate() {
let v0 = get_vertex(vertices, i0, face_idx)?;
let v1 = get_vertex(vertices, i1, face_idx)?;
let v2 = get_vertex(vertices, i2, face_idx)?;
let area = triangle_area(v0, v1, v2);
face_areas.push(area);
let prev = area_prefix[face_idx];
area_prefix.push(prev + area as f64);
}
let total_area = *area_prefix.last().unwrap_or(&0.0);
if total_area <= 0.0 {
return Err(RenderError::Rasterize(
"Mesh has zero total surface area; cannot sample Gaussians".to_string(),
));
}
Ok(Self {
faces: faces.to_vec(),
vertices: vertices.to_vec(),
normals: normals.map(|n| n.to_vec()),
face_areas,
area_prefix,
total_area,
})
}
/// Initialize a [`GaussianModel`] by placing `config.num_gaussians` Gaussians
/// on the mesh surface using area-weighted random triangle sampling.
///
/// # Errors
/// Returns [`RenderError::Rasterize`] on invalid configuration.
pub fn initialize(&self, config: &GaussianInitConfig) -> Result<GaussianModel, RenderError> {
if config.num_gaussians == 0 {
return Err(RenderError::Rasterize(
"num_gaussians must be greater than zero".to_string(),
));
}
let n = config.num_gaussians;
let mut rng = Xorshift64::new(config.seed);
// Determine SH layout.
let sh_total = ((config.sh_degree + 1) * (config.sh_degree + 1) * 3) as usize;
// Precompute SH DC values from mean_color.
// sh_dc[c] = (mean_color[c] - 0.5) / SH_C0
let sh_dc_r = (config.mean_color[0] - 0.5) / SH_C0;
let sh_dc_g = (config.mean_color[1] - 0.5) / SH_C0;
let sh_dc_b = (config.mean_color[2] - 0.5) / SH_C0;
let mut gaussians = Vec::with_capacity(n);
let mut sh_coeffs = Vec::with_capacity(n * sh_total);
let mut face_indices = Vec::with_capacity(n);
let mut barycentric = Vec::with_capacity(n);
let mut local_offsets = Vec::with_capacity(n);
let mut is_rigid = Vec::with_capacity(n);
for _ in 0..n {
// --- Area-weighted triangle selection ---
let face_idx = self.sample_face(&mut rng);
let [i0, i1, i2] = self.faces[face_idx];
let v0 = self.vertices[i0 as usize];
let v1 = self.vertices[i1 as usize];
let v2 = self.vertices[i2 as usize];
let face_area = self.face_areas[face_idx];
// --- Barycentric coordinates ---
let (bary_u, bary_v, bary_w) = sample_barycentric(&mut rng);
// --- Position via barycentric interpolation ---
let position = [
bary_u * v0[0] + bary_v * v1[0] + bary_w * v2[0],
bary_u * v0[1] + bary_v * v1[1] + bary_w * v2[1],
bary_u * v0[2] + bary_v * v1[2] + bary_w * v2[2],
];
// --- Log-scale: ln(sqrt(face_area) * scale_factor), clamped ---
let log_scale = if face_area > 0.0 {
let s = (face_area.sqrt() * config.scale_factor).ln();
s.clamp(config.min_scale_log, config.max_scale_log)
} else {
config.min_scale_log
};
// --- Rotation from surface normal → TBN → quaternion ---
let rotation = compute_rotation(FaceGeom {
i0,
i1,
i2,
v0: &v0,
v1: &v1,
v2: &v2,
bary: [bary_u, bary_v, bary_w],
normals: self.normals.as_deref(),
});
// --- Gaussian attributes ---
gaussians.push(GaussianAttributes {
position,
_pad0: 0.0,
rotation,
scale: [log_scale, log_scale, log_scale],
opacity: config.initial_opacity_logit,
});
// --- SH coefficients ---
// DC terms (first 3 floats per Gaussian, one per channel).
sh_coeffs.push(sh_dc_r);
sh_coeffs.push(sh_dc_g);
sh_coeffs.push(sh_dc_b);
// Higher-degree SH coefficients initialized to 0.
let higher_count = sh_total.saturating_sub(3);
sh_coeffs.extend(std::iter::repeat_n(0.0_f32, higher_count));
// --- Binding fields ---
face_indices.push(face_idx as u32);
barycentric.push([bary_u, bary_v, bary_w]);
local_offsets.push([0.0_f32, 0.0_f32, 0.0_f32]);
is_rigid.push(false);
}
Ok(GaussianModel {
gaussians,
sh_coeffs,
sh_degree: config.sh_degree,
face_indices,
barycentric,
local_offsets,
is_rigid,
})
}
/// Sample a face index using area-weighted probability via binary search on the prefix sum.
fn sample_face(&self, rng: &mut Xorshift64) -> usize {
let r = rng.next_f32() as f64 * self.total_area;
// Binary search for the first prefix entry > r.
let mut lo = 0usize;
let mut hi = self.face_areas.len();
while lo < hi {
let mid = lo + (hi - lo) / 2;
if self.area_prefix[mid + 1] <= r {
lo = mid + 1;
} else {
hi = mid;
}
}
// Clamp to valid range (handles floating-point edge at total_area boundary).
lo.min(self.face_areas.len() - 1)
}
}
// ---------------------------------------------------------------------------
// Geometry helpers
// ---------------------------------------------------------------------------
/// Compute the area of a triangle given its three vertex positions.
fn triangle_area(v0: [f32; 3], v1: [f32; 3], v2: [f32; 3]) -> f32 {
let e1 = sub3(v1, v0);
let e2 = sub3(v2, v0);
let cross = cross3(e1, e2);
0.5 * len3(cross)
}
/// Retrieve a vertex by index, returning an error on out-of-bounds access.
fn get_vertex(vertices: &[[f32; 3]], idx: u32, face_idx: usize) -> Result<[f32; 3], RenderError> {
vertices.get(idx as usize).copied().ok_or_else(|| {
RenderError::Rasterize(format!(
"Face {face_idx}: vertex index {idx} is out of bounds (mesh has {} vertices)",
vertices.len()
))
})
}
/// Sample barycentric coordinates `(u, v, w)` uniformly on a triangle.
///
/// Uses the mirror-fold construction: draw `r1, r2` uniformly in `[0, 1)`
/// (as a point in the unit square); if `r1 + r2 > 1` that point falls
/// outside the unit triangle, so reflect both through `1 - r`, which maps
/// it back inside while preserving a uniform distribution. Then
/// `u = r1', v = r2', w = 1 - u - v` (clamped to `>= 0` to absorb
/// floating-point noise right at the `w = 0` edge).
fn sample_barycentric(rng: &mut Xorshift64) -> (f32, f32, f32) {
let r1 = rng.next_f32();
let r2 = rng.next_f32();
let (u, v) = if r1 + r2 > 1.0 {
(1.0 - r1, 1.0 - r2)
} else {
(r1, r2)
};
let w = 1.0 - u - v;
// Clamp to ensure non-negative w due to floating-point noise.
let w = w.max(0.0);
(u, v, w)
}
/// Face geometry used for rotation computation, bundled to avoid a wide function signature.
struct FaceGeom<'a> {
i0: u32,
i1: u32,
i2: u32,
v0: &'a [f32; 3],
v1: &'a [f32; 3],
v2: &'a [f32; 3],
bary: [f32; 3],
normals: Option<&'a [[f32; 3]]>,
}
/// Compute the rotation quaternion `[x, y, z, w]` for a Gaussian placed on a face.
///
/// Builds a genuinely orthonormal TBN frame — the tangent is Gram-Schmidt
/// projected against the surface normal via [`build_tbn`] rather than used
/// raw — then converts it directly to a *normalized* unit quaternion via
/// [`mat3_cols_to_quat`]. This is the same shared construction
/// `deform::deform_cpu` uses for the post-deformation rotation (see
/// `deform.rs`), so the initial and deformed rotations stay geometrically
/// consistent, and both are guaranteed unit quaternions built from a true
/// rotation matrix (unlike a bare `edge` tangent, which is generally *not*
/// perpendicular to an interpolated vertex normal).
fn compute_rotation(geom: FaceGeom<'_>) -> [f32; 4] {
let FaceGeom {
i0,
i1,
i2,
v0,
v1,
v2,
bary: [bary_u, bary_v, bary_w],
normals,
} = geom;
// Interpolated vertex normal. Left as the zero vector when no
// per-vertex normals were supplied: `build_tbn` treats a near-zero
// `interp_n` as "not supplied" and falls back to the triangle's
// geometric normal, exactly matching the previous `normals.is_none()`
// branch.
let interp_n = if let Some(nrm) = normals {
let n0 = nrm.get(i0 as usize).copied().unwrap_or([0.0, 0.0, 1.0]);
let n1 = nrm.get(i1 as usize).copied().unwrap_or([0.0, 0.0, 1.0]);
let n2 = nrm.get(i2 as usize).copied().unwrap_or([0.0, 0.0, 1.0]);
[
bary_u * n0[0] + bary_v * n1[0] + bary_w * n2[0],
bary_u * n0[1] + bary_v * n1[1] + bary_w * n2[1],
bary_u * n0[2] + bary_v * n1[2] + bary_w * n2[2],
]
} else {
[0.0, 0.0, 0.0]
};
let (t, bt, n) = build_tbn(*v0, *v1, *v2, interp_n);
mat3_cols_to_quat(t, bt, n)
}
// ---------------------------------------------------------------------------
// Vector math helpers (pure f32, no external dependencies)
// ---------------------------------------------------------------------------
#[inline]
fn sub3(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
[a[0] - b[0], a[1] - b[1], a[2] - b[2]]
}
#[inline]
fn cross3(a: [f32; 3], b: [f32; 3]) -> [f32; 3] {
[
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
]
}
#[inline]
fn len3(a: [f32; 3]) -> f32 {
(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]).sqrt()
}
// Note: this module previously defined its own `normalize3_safe` /
// `normalize3` / `rotation_matrix_to_quaternion` helpers for building the
// per-Gaussian rotation. They were removed when `compute_rotation` was
// switched to the shared, Gram-Schmidt-corrected `deform::build_tbn` +
// `deform::mat3_cols_to_quat` (see `compute_rotation` above) — the bare
// `edge` tangent those helpers combined was not generally orthogonal to an
// interpolated vertex normal, and the resulting quaternion was never
// normalized.
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
/// A simple unit triangle in the XY plane.
fn unit_triangle_mesh() -> (Vec<[f32; 3]>, Vec<[u32; 3]>) {
let vertices = vec![[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
let faces = vec![[0u32, 1, 2]];
(vertices, faces)
}
/// A simple mesh with two triangles of different areas.
fn two_triangle_mesh() -> (Vec<[f32; 3]>, Vec<[u32; 3]>) {
// Triangle 0: area = 0.5 (unit right triangle)
// Triangle 1: area = 2.0 (base=2, height=2)
let vertices = vec![
[0.0, 0.0, 0.0], // 0
[1.0, 0.0, 0.0], // 1
[0.0, 1.0, 0.0], // 2
[2.0, 0.0, 0.0], // 3
[0.0, 2.0, 0.0], // 4
];
let faces = vec![[0u32, 1, 2], [0, 3, 4]];
(vertices, faces)
}
// -----------------------------------------------------------------------
// test_config_defaults
// -----------------------------------------------------------------------
#[test]
fn test_config_defaults() {
let cfg = GaussianInitConfig::default();
assert_eq!(cfg.num_gaussians, 10_000);
assert!((cfg.scale_factor - 0.05).abs() < 1e-6);
assert!((cfg.min_scale_log - (-6.0)).abs() < 1e-6);
assert!((cfg.max_scale_log - 0.0).abs() < 1e-6);
assert!((cfg.initial_opacity_logit - (0.1_f32 / 0.9_f32).ln()).abs() < 1e-4);
assert_eq!(cfg.sh_degree, 0);
assert_eq!(cfg.mean_color, [0.5, 0.5, 0.5]);
assert_eq!(cfg.seed, 42);
}
// -----------------------------------------------------------------------
// test_empty_mesh_error
// -----------------------------------------------------------------------
#[test]
fn test_empty_mesh_error() {
let vertices = vec![[0.0_f32, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
let faces: Vec<[u32; 3]> = vec![];
let result = GaussianInitializer::new(&vertices, &faces, None);
assert!(result.is_err(), "Empty mesh must produce an error");
let err = result.err().unwrap();
let msg = err.to_string();
assert!(
msg.contains("empty") || msg.contains("no faces"),
"Error should mention empty mesh, got: {msg}"
);
}
// -----------------------------------------------------------------------
// test_single_triangle_init
// -----------------------------------------------------------------------
#[test]
fn test_single_triangle_init() {
let (vertices, faces) = unit_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 10,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
assert_eq!(model.gaussians.len(), 10);
}
// -----------------------------------------------------------------------
// test_gaussian_count_matches_config
// -----------------------------------------------------------------------
#[test]
fn test_gaussian_count_matches_config() {
let (vertices, faces) = unit_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
for &n in &[1usize, 50, 100, 500] {
let cfg = GaussianInitConfig {
num_gaussians: n,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
assert_eq!(
model.gaussians.len(),
n,
"Expected {n} Gaussians, got {}",
model.gaussians.len()
);
assert_eq!(model.face_indices.len(), n);
assert_eq!(model.barycentric.len(), n);
assert_eq!(model.local_offsets.len(), n);
assert_eq!(model.is_rigid.len(), n);
}
}
// -----------------------------------------------------------------------
// test_barycentric_valid_sum_to_one
// -----------------------------------------------------------------------
#[test]
fn test_barycentric_valid_sum_to_one() {
let (vertices, faces) = unit_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 200,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
for (i, &[u, v, w]) in model.barycentric.iter().enumerate() {
assert!((0.0..=1.0).contains(&u), "Gaussian {i}: u={u} out of [0,1]");
assert!((0.0..=1.0).contains(&v), "Gaussian {i}: v={v} out of [0,1]");
assert!((0.0..=1.0).contains(&w), "Gaussian {i}: w={w} out of [0,1]");
let sum = u + v + w;
assert!(
(sum - 1.0).abs() < 1e-5,
"Gaussian {i}: barycentric sum = {sum}, expected 1.0"
);
}
}
// -----------------------------------------------------------------------
// test_scale_within_bounds
// -----------------------------------------------------------------------
#[test]
fn test_scale_within_bounds() {
let (vertices, faces) = unit_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 100,
min_scale_log: -4.0,
max_scale_log: -0.5,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
for (i, g) in model.gaussians.iter().enumerate() {
for (dim, &s) in g.scale.iter().enumerate() {
assert!(
s >= cfg.min_scale_log - 1e-6 && s <= cfg.max_scale_log + 1e-6,
"Gaussian {i} dim {dim}: log-scale {s} outside [{}, {}]",
cfg.min_scale_log,
cfg.max_scale_log
);
}
}
}
// -----------------------------------------------------------------------
// test_opacity_initial_value
// -----------------------------------------------------------------------
#[test]
fn test_opacity_initial_value() {
let (vertices, faces) = unit_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 50,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
let expected_logit = (0.1_f32 / 0.9_f32).ln();
// sigmoid(logit) should be ≈ 0.1
let expected_opacity = 1.0 / (1.0 + (-expected_logit).exp());
for (i, g) in model.gaussians.iter().enumerate() {
assert!(
(g.opacity - expected_logit).abs() < 1e-5,
"Gaussian {i}: opacity logit = {}, expected {}",
g.opacity,
expected_logit
);
let decoded = 1.0 / (1.0 + (-g.opacity).exp());
assert!(
(decoded - expected_opacity).abs() < 1e-4,
"Gaussian {i}: sigmoid(opacity) = {decoded}, expected {expected_opacity}"
);
}
}
// -----------------------------------------------------------------------
// test_sh_dc_from_mean_color
// -----------------------------------------------------------------------
#[test]
fn test_sh_dc_from_mean_color() {
let (vertices, faces) = unit_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let mean_color = [0.8_f32, 0.3, 0.5];
let cfg = GaussianInitConfig {
num_gaussians: 10,
mean_color,
sh_degree: 0,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
// sh_coeffs layout for degree 0: [r_dc, g_dc, b_dc] * N
assert_eq!(model.sh_coeffs.len(), 10 * 3);
for i in 0..10 {
let sh_r = model.sh_coeffs[i * 3];
let sh_g = model.sh_coeffs[i * 3 + 1];
let sh_b = model.sh_coeffs[i * 3 + 2];
// Decoded: color = 0.5 + SH_C0 * sh_dc
let decoded_r = 0.5 + SH_C0 * sh_r;
let decoded_g = 0.5 + SH_C0 * sh_g;
let decoded_b = 0.5 + SH_C0 * sh_b;
assert!(
(decoded_r - mean_color[0]).abs() < 1e-5,
"Gaussian {i}: decoded R = {decoded_r}, expected {}",
mean_color[0]
);
assert!(
(decoded_g - mean_color[1]).abs() < 1e-5,
"Gaussian {i}: decoded G = {decoded_g}, expected {}",
mean_color[1]
);
assert!(
(decoded_b - mean_color[2]).abs() < 1e-5,
"Gaussian {i}: decoded B = {decoded_b}, expected {}",
mean_color[2]
);
}
}
// -----------------------------------------------------------------------
// test_rotation_is_unit_quaternion
// -----------------------------------------------------------------------
#[test]
fn test_rotation_is_unit_quaternion() {
let (vertices, faces) = unit_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 100,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
for (i, g) in model.gaussians.iter().enumerate() {
let [x, y, z, w] = g.rotation;
let norm = (x * x + y * y + z * z + w * w).sqrt();
assert!(
(norm - 1.0).abs() < 1e-4,
"Gaussian {i}: quaternion norm = {norm}, expected 1.0"
);
}
}
// -----------------------------------------------------------------------
// test_all_positions_on_mesh_surface
// -----------------------------------------------------------------------
#[test]
fn test_all_positions_on_mesh_surface() {
let (vertices, faces) = unit_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 100,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
for (i, (g, (&face_idx, &[bu, bv, bw]))) in model
.gaussians
.iter()
.zip(model.face_indices.iter().zip(model.barycentric.iter()))
.enumerate()
{
let [fi0, fi1, fi2] = faces[face_idx as usize];
let v0 = vertices[fi0 as usize];
let v1 = vertices[fi1 as usize];
let v2 = vertices[fi2 as usize];
let expected_pos = [
bu * v0[0] + bv * v1[0] + bw * v2[0],
bu * v0[1] + bv * v1[1] + bw * v2[1],
bu * v0[2] + bv * v1[2] + bw * v2[2],
];
for (dim, (&pos_val, &exp_val)) in
g.position.iter().zip(expected_pos.iter()).enumerate()
{
assert!(
(pos_val - exp_val).abs() < 1e-5,
"Gaussian {i} dim {dim}: position {} != expected {}",
pos_val,
exp_val
);
}
}
}
// -----------------------------------------------------------------------
// test_deterministic_with_same_seed
// -----------------------------------------------------------------------
#[test]
fn test_deterministic_with_same_seed() {
let (vertices, faces) = two_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 50,
seed: 12345,
..Default::default()
};
let model_a = init.initialize(&cfg).expect("First init");
let model_b = init.initialize(&cfg).expect("Second init");
for (i, (ga, gb)) in model_a
.gaussians
.iter()
.zip(model_b.gaussians.iter())
.enumerate()
{
for dim in 0..3 {
assert_eq!(
ga.position[dim], gb.position[dim],
"Gaussian {i} position differs between identical seeds"
);
}
}
assert_eq!(model_a.face_indices, model_b.face_indices);
}
// -----------------------------------------------------------------------
// test_different_seeds_give_different_results
// -----------------------------------------------------------------------
#[test]
fn test_different_seeds_give_different_results() {
let (vertices, faces) = two_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg_a = GaussianInitConfig {
num_gaussians: 100,
seed: 1,
..Default::default()
};
let cfg_b = GaussianInitConfig {
num_gaussians: 100,
seed: 2,
..Default::default()
};
let model_a = init.initialize(&cfg_a).expect("First init");
let model_b = init.initialize(&cfg_b).expect("Second init");
let any_different = model_a
.gaussians
.iter()
.zip(model_b.gaussians.iter())
.any(|(ga, gb)| ga.position[0] != gb.position[0]);
assert!(
any_different,
"Different seeds should produce different Gaussian positions"
);
}
// -----------------------------------------------------------------------
// test_area_weighted_bias
// -----------------------------------------------------------------------
#[test]
fn test_area_weighted_bias() {
// Two triangles: T0 area=0.5, T1 area=2.0 → ratio 1:4
// With many samples, T1 should receive ~4x more Gaussians.
let (vertices, faces) = two_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 5000,
seed: 99,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
let count_t0 = model.face_indices.iter().filter(|&&f| f == 0).count();
let count_t1 = model.face_indices.iter().filter(|&&f| f == 1).count();
// Expected ratio: T1/T0 ≈ 4.0. Allow ±30% tolerance.
let ratio = count_t1 as f64 / count_t0.max(1) as f64;
assert!(
ratio > 2.8 && ratio < 5.2,
"Expected T1/T0 ratio ≈ 4.0, got {ratio:.2} (T0={count_t0}, T1={count_t1})"
);
}
// -----------------------------------------------------------------------
// test_face_indices_in_range
// -----------------------------------------------------------------------
#[test]
fn test_face_indices_in_range() {
let (vertices, faces) = two_triangle_mesh();
let num_faces = faces.len();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 200,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
for (i, &fi) in model.face_indices.iter().enumerate() {
assert!(
(fi as usize) < num_faces,
"Gaussian {i}: face_index {fi} >= num_faces {num_faces}"
);
}
}
// -----------------------------------------------------------------------
// test_local_offsets_are_zero
// -----------------------------------------------------------------------
#[test]
fn test_local_offsets_are_zero() {
let (vertices, faces) = unit_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 50,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
for (i, &offset) in model.local_offsets.iter().enumerate() {
assert_eq!(
offset,
[0.0_f32, 0.0, 0.0],
"Gaussian {i}: local_offset is not zero, got {offset:?}"
);
}
}
// -----------------------------------------------------------------------
// test_is_rigid_all_false
// -----------------------------------------------------------------------
#[test]
fn test_is_rigid_all_false() {
let (vertices, faces) = unit_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 50,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
for (i, &rigid) in model.is_rigid.iter().enumerate() {
assert!(!rigid, "Gaussian {i}: is_rigid should be false");
}
}
// -----------------------------------------------------------------------
// test_large_mesh_init
// -----------------------------------------------------------------------
#[test]
fn test_large_mesh_init() {
// Build a grid mesh with 1000 triangles.
let grid_size = 23usize; // 23×23 grid → 22×22 quads → 968 triangles ≈ 1000
let mut vertices = Vec::new();
let mut faces = Vec::new();
for row in 0..=grid_size {
for col in 0..=grid_size {
vertices.push([col as f32, row as f32, 0.0_f32]);
}
}
let stride = grid_size + 1;
for row in 0..grid_size {
for col in 0..grid_size {
let tl = (row * stride + col) as u32;
let tr = tl + 1;
let bl = tl + stride as u32;
let br = bl + 1;
faces.push([tl, tr, bl]);
faces.push([tr, br, bl]);
}
}
assert!(
faces.len() >= 900,
"Expected ~1000 triangles, got {}",
faces.len()
);
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid large mesh");
let cfg = GaussianInitConfig {
num_gaussians: 5000,
..Default::default()
};
let model = init
.initialize(&cfg)
.expect("Should initialize model from large mesh");
assert_eq!(model.gaussians.len(), 5000);
// All face indices must be in range.
for &fi in &model.face_indices {
assert!((fi as usize) < faces.len());
}
}
// -----------------------------------------------------------------------
// test_sh_higher_degree_zero
// -----------------------------------------------------------------------
#[test]
fn test_sh_higher_degree_zero() {
let (vertices, faces) = unit_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 5,
sh_degree: 1,
mean_color: [0.5, 0.5, 0.5],
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
// sh_degree=1 → (1+1)^2 * 3 = 12 coeffs per Gaussian.
assert_eq!(model.sh_coeffs.len(), 5 * 12);
for i in 0..5 {
// First 3 are DC (may be nonzero), rest (indices 3..12) must be 0.
for k in 3..12 {
let val = model.sh_coeffs[i * 12 + k];
assert_eq!(val, 0.0, "Gaussian {i} SH coeff {k} should be 0, got {val}");
}
}
}
// -----------------------------------------------------------------------
// test_rotation_with_normals
// -----------------------------------------------------------------------
#[test]
fn test_rotation_with_normals() {
let (vertices, faces) = unit_triangle_mesh();
let normals = vec![
[0.0_f32, 0.0, 1.0],
[0.0_f32, 0.0, 1.0],
[0.0_f32, 0.0, 1.0],
];
let init = GaussianInitializer::new(&vertices, &faces, Some(&normals))
.expect("Should construct from valid mesh with normals");
let cfg = GaussianInitConfig {
num_gaussians: 20,
..Default::default()
};
let model = init.initialize(&cfg).expect("Should initialize model");
// All rotations must be unit quaternions.
for (i, g) in model.gaussians.iter().enumerate() {
let [x, y, z, w] = g.rotation;
let norm = (x * x + y * y + z * z + w * w).sqrt();
assert!(
(norm - 1.0).abs() < 1e-4,
"Gaussian {i} with normals: quaternion norm = {norm}"
);
}
}
// -----------------------------------------------------------------------
// test_zero_seed_handled
// -----------------------------------------------------------------------
#[test]
fn test_zero_seed_handled() {
let (vertices, faces) = unit_triangle_mesh();
let init = GaussianInitializer::new(&vertices, &faces, None)
.expect("Should construct from valid mesh");
let cfg = GaussianInitConfig {
num_gaussians: 10,
seed: 0, // zero seed should be handled gracefully
..Default::default()
};
let model = init.initialize(&cfg).expect("Zero seed should not panic");
assert_eq!(model.gaussians.len(), 10);
}
// -----------------------------------------------------------------------
// test_next_f32_always_in_zero_one
// -----------------------------------------------------------------------
#[test]
fn test_next_f32_always_in_zero_one() {
// Regression: the old `(x >> 11) as f32 / (1u64 << 53) as f32`
// construction could round up to exactly 1.0 (a 53-bit numerator
// cast to f32's 24-bit mantissa rounds up for values in
// `[2^53 - 2^28, 2^53)`), violating the documented `[0, 1)` range.
// Draw heavily across several seeds/stream positions.
for seed in [1u64, 2, 42, 12345, u64::MAX, 0xDEAD_BEEF] {
let mut rng = Xorshift64::new(seed);
for _ in 0..100_000 {
let v = rng.next_f32();
assert!(v < 1.0, "next_f32 returned {v} >= 1.0 (seed={seed})");
assert!(v >= 0.0, "next_f32 returned {v} < 0.0 (seed={seed})");
}
}
}
// -----------------------------------------------------------------------
// test_rotation_orthonormal_with_skewed_normal
// -----------------------------------------------------------------------
#[test]
fn test_rotation_orthonormal_with_skewed_normal() {
// A vertex normal deliberately *not* perpendicular to the
// triangle's v0->v1 edge (the tangent's starting point before
// Gram-Schmidt correction). Regression for: the previous
// implementation used that raw edge as the tangent with no
// projection against the normal, producing a non-orthonormal TBN
// and an un-normalized quaternion.
let v0 = [0.0_f32, 0.0, 0.0];
let v1 = [1.0_f32, 0.0, 0.0];
let v2 = [0.0_f32, 1.0, 0.0];
// Not perpendicular to edge v1-v0 = [1,0,0] (dot = 0.6), and far
// from this triangle's own geometric normal [0,0,1].
let skewed_normal = [0.6_f32, 0.6, 0.529_150_3];
let normals = [skewed_normal, skewed_normal, skewed_normal];
let geom = FaceGeom {
i0: 0,
i1: 1,
i2: 2,
v0: &v0,
v1: &v1,
v2: &v2,
bary: [1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0],
normals: Some(&normals[..]),
};
let q = compute_rotation(geom);
let [x, y, z, w] = q;
let qnorm = (x * x + y * y + z * z + w * w).sqrt();
assert!(
(qnorm - 1.0).abs() < 1e-5,
"quaternion not unit length: {qnorm}"
);
// Reconstruct the TBN directly (the same call `compute_rotation`
// makes internally) and check pairwise orthogonality + unit length.
let (t, bt, n) = build_tbn(v0, v1, v2, skewed_normal);
let dot = |a: [f32; 3], b: [f32; 3]| a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
let len = |a: [f32; 3]| dot(a, a).sqrt();
assert!(dot(t, bt).abs() < 1e-5, "t·bt = {}", dot(t, bt));
assert!(dot(t, n).abs() < 1e-5, "t·n = {}", dot(t, n));
assert!(dot(bt, n).abs() < 1e-5, "bt·n = {}", dot(bt, n));
assert!((len(t) - 1.0).abs() < 1e-5, "|t| = {}", len(t));
assert!((len(bt) - 1.0).abs() < 1e-5, "|bt| = {}", len(bt));
assert!((len(n) - 1.0).abs() < 1e-5, "|n| = {}", len(n));
}
}