canvas-renderer 0.1.4

Custom minimal renderer for Saorsa Canvas built on wgpu. Provides GPU rendering with WebGL2/2D fallbacks.
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
//! Spatial rendering for holographic and 3D displays.
//!
//! This module provides camera and view calculations needed for
//! multi-view rendering on holographic displays like Looking Glass.
//!
//! ## Holographic Rendering Concepts
//!
//! ```text
//!                     Looking Glass Display
//!                    ┌─────────────────────┐
//!                    │  ╱   ╱   ╱   ╱   ╱  │
//!                    │ ╱   ╱   ╱   ╱   ╱   │  Lenticular lens array
//!                    │╱   ╱   ╱   ╱   ╱    │  directs different views
//!                    └─────────────────────┘  to each eye
//!
//!          Camera Arc (45 views)
//!         ╭─────────────────────╮
//!        ╱  ╲                   ╲
//!       ●    ●    ●    ●    ●    ●   ← Camera positions
//!       0    8   16   24   32   44
//!//!         ┌─────────────────┐
//!         │ Scene to render │
//!         └─────────────────┘
//! ```
//!
//! ## Quilt Format
//!
//! Multiple views are rendered into a single "quilt" texture:
//!
//! ```text
//! ┌────┬────┬────┬────┬────┐
//! │ 40 │ 41 │ 42 │ 43 │ 44 │
//! ├────┼────┼────┼────┼────┤
//! │ 35 │ 36 │ 37 │ 38 │ 39 │
//! ├────┼────┼────┼────┼────┤
//! │... │... │... │... │... │
//! ├────┼────┼────┼────┼────┤
//! │  0 │  1 │  2 │  3 │  4 │
//! └────┴────┴────┴────┴────┘
//!        Quilt (5x9 grid = 45 views)
//! ```

use serde::{Deserialize, Serialize};

/// A 3D vector for positions and directions.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Vec3 {
    /// X component.
    pub x: f32,
    /// Y component.
    pub y: f32,
    /// Z component.
    pub z: f32,
}

impl Vec3 {
    /// Create a new vector.
    #[must_use]
    pub const fn new(x: f32, y: f32, z: f32) -> Self {
        Self { x, y, z }
    }

    /// Zero vector.
    #[must_use]
    pub const fn zero() -> Self {
        Self::new(0.0, 0.0, 0.0)
    }

    /// Unit vector pointing up (Y+).
    #[must_use]
    pub const fn up() -> Self {
        Self::new(0.0, 1.0, 0.0)
    }

    /// Unit vector pointing forward (Z-).
    #[must_use]
    pub const fn forward() -> Self {
        Self::new(0.0, 0.0, -1.0)
    }

    /// Calculate the length (magnitude) of the vector.
    #[must_use]
    pub fn length(&self) -> f32 {
        (self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
    }

    /// Normalize the vector to unit length.
    #[must_use]
    pub fn normalize(&self) -> Self {
        let len = self.length();
        if len > 0.0 {
            Self::new(self.x / len, self.y / len, self.z / len)
        } else {
            *self
        }
    }

    /// Cross product of two vectors.
    #[must_use]
    pub fn cross(&self, other: &Self) -> Self {
        Self::new(
            self.y * other.z - self.z * other.y,
            self.z * other.x - self.x * other.z,
            self.x * other.y - self.y * other.x,
        )
    }

    /// Dot product of two vectors.
    #[must_use]
    pub fn dot(&self, other: &Self) -> f32 {
        self.x * other.x + self.y * other.y + self.z * other.z
    }

    /// Subtract two vectors.
    #[must_use]
    pub fn sub(&self, other: &Self) -> Self {
        Self::new(self.x - other.x, self.y - other.y, self.z - other.z)
    }

    /// Add two vectors.
    #[must_use]
    pub fn add(&self, other: &Self) -> Self {
        Self::new(self.x + other.x, self.y + other.y, self.z + other.z)
    }

    /// Scale vector by a scalar.
    #[must_use]
    pub fn scale(&self, s: f32) -> Self {
        Self::new(self.x * s, self.y * s, self.z * s)
    }
}

impl Default for Vec3 {
    fn default() -> Self {
        Self::zero()
    }
}

/// A 4x4 matrix for transformations.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Mat4 {
    /// Matrix data in column-major order.
    pub data: [f32; 16],
}

impl Mat4 {
    /// Create identity matrix.
    #[must_use]
    pub fn identity() -> Self {
        #[rustfmt::skip]
        let data = [
            1.0, 0.0, 0.0, 0.0,
            0.0, 1.0, 0.0, 0.0,
            0.0, 0.0, 1.0, 0.0,
            0.0, 0.0, 0.0, 1.0,
        ];
        Self { data }
    }

    /// Create a look-at view matrix.
    #[must_use]
    pub fn look_at(eye: Vec3, target: Vec3, up: Vec3) -> Self {
        let f = target.sub(&eye).normalize();
        let s = f.cross(&up).normalize();
        let u = s.cross(&f);

        #[rustfmt::skip]
        let data = [
            s.x,  u.x,  -f.x, 0.0,
            s.y,  u.y,  -f.y, 0.0,
            s.z,  u.z,  -f.z, 0.0,
            -s.dot(&eye), -u.dot(&eye), f.dot(&eye), 1.0,
        ];
        Self { data }
    }

    /// Create a perspective projection matrix.
    #[must_use]
    pub fn perspective(fov_y_radians: f32, aspect: f32, near: f32, far: f32) -> Self {
        let f = 1.0 / (fov_y_radians / 2.0).tan();
        let nf = 1.0 / (near - far);

        #[rustfmt::skip]
        let data = [
            f / aspect, 0.0, 0.0, 0.0,
            0.0, f, 0.0, 0.0,
            0.0, 0.0, (far + near) * nf, -1.0,
            0.0, 0.0, 2.0 * far * near * nf, 0.0,
        ];
        Self { data }
    }

    /// Multiply two matrices.
    #[must_use]
    pub fn mul(&self, other: &Self) -> Self {
        let mut result = [0.0f32; 16];

        for row in 0..4 {
            for col in 0..4 {
                for k in 0..4 {
                    result[col * 4 + row] += self.data[k * 4 + row] * other.data[col * 4 + k];
                }
            }
        }

        Self { data: result }
    }
}

impl Default for Mat4 {
    fn default() -> Self {
        Self::identity()
    }
}

/// Camera for 3D rendering.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Camera {
    /// Camera position in world space.
    pub position: Vec3,
    /// Point the camera is looking at.
    pub target: Vec3,
    /// Up direction (usually Y+).
    pub up: Vec3,
    /// Field of view in radians.
    pub fov: f32,
    /// Near clipping plane.
    pub near: f32,
    /// Far clipping plane.
    pub far: f32,
}

impl Camera {
    /// Create a new camera with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self {
            position: Vec3::new(0.0, 0.0, 5.0),
            target: Vec3::zero(),
            up: Vec3::up(),
            fov: std::f32::consts::FRAC_PI_4, // 45 degrees
            near: 0.1,
            far: 100.0,
        }
    }

    /// Create view matrix for this camera.
    #[must_use]
    pub fn view_matrix(&self) -> Mat4 {
        Mat4::look_at(self.position, self.target, self.up)
    }

    /// Create projection matrix for this camera.
    #[must_use]
    pub fn projection_matrix(&self, aspect: f32) -> Mat4 {
        Mat4::perspective(self.fov, aspect, self.near, self.far)
    }
}

impl Default for Camera {
    fn default() -> Self {
        Self::new()
    }
}

/// Configuration for holographic multi-view rendering.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HolographicConfig {
    /// Number of views to render (typically 45 for Looking Glass).
    pub num_views: u32,
    /// Number of columns in the quilt grid.
    pub quilt_columns: u32,
    /// Number of rows in the quilt grid.
    pub quilt_rows: u32,
    /// Width of each view in pixels.
    pub view_width: u32,
    /// Height of each view in pixels.
    pub view_height: u32,
    /// Total viewing angle in radians (how much the camera sweeps).
    pub view_cone: f32,
    /// Distance from camera to focal plane.
    pub focal_distance: f32,
}

impl HolographicConfig {
    /// Standard Looking Glass Portrait configuration (45 views, 5x9 quilt).
    #[must_use]
    pub fn looking_glass_portrait() -> Self {
        Self {
            num_views: 45,
            quilt_columns: 5,
            quilt_rows: 9,
            view_width: 420,
            view_height: 560,
            view_cone: 40.0_f32.to_radians(), // 40 degrees total
            focal_distance: 2.0,
        }
    }

    /// Standard Looking Glass 4K configuration (45 views, 5x9 quilt).
    #[must_use]
    pub fn looking_glass_4k() -> Self {
        Self {
            num_views: 45,
            quilt_columns: 5,
            quilt_rows: 9,
            view_width: 819,
            view_height: 455,
            view_cone: 40.0_f32.to_radians(),
            focal_distance: 2.0,
        }
    }

    /// Calculate the total quilt texture width.
    #[must_use]
    pub fn quilt_width(&self) -> u32 {
        self.quilt_columns * self.view_width
    }

    /// Calculate the total quilt texture height.
    #[must_use]
    pub fn quilt_height(&self) -> u32 {
        self.quilt_rows * self.view_height
    }

    /// Calculate which row and column a view index maps to.
    #[must_use]
    pub fn view_to_grid(&self, view_index: u32) -> (u32, u32) {
        let col = view_index % self.quilt_columns;
        let row = view_index / self.quilt_columns;
        (col, row)
    }

    /// Calculate the pixel offset for a view in the quilt.
    #[must_use]
    pub fn view_offset(&self, view_index: u32) -> (u32, u32) {
        let (col, row) = self.view_to_grid(view_index);
        (col * self.view_width, row * self.view_height)
    }

    /// Calculate the camera position for a specific view.
    ///
    /// The camera moves along a horizontal arc centered on the target.
    #[must_use]
    #[allow(clippy::cast_precision_loss)] // View indices are small, precision loss negligible
    pub fn camera_for_view(&self, base_camera: &Camera, view_index: u32) -> Camera {
        if self.num_views <= 1 {
            return base_camera.clone();
        }

        // Calculate the angle offset for this view
        // View 0 is leftmost, view (num_views-1) is rightmost
        let t = view_index as f32 / (self.num_views - 1) as f32;
        let angle = (t - 0.5) * self.view_cone;

        // Calculate new camera position by rotating around the target
        let dir = base_camera.position.sub(&base_camera.target);
        let distance = dir.length();

        // Rotate the direction vector around Y axis
        let cos_a = angle.cos();
        let sin_a = angle.sin();
        let new_dir = Vec3::new(
            dir.x * cos_a + dir.z * sin_a,
            dir.y,
            -dir.x * sin_a + dir.z * cos_a,
        );

        Camera {
            position: base_camera.target.add(&new_dir.normalize().scale(distance)),
            target: base_camera.target,
            up: base_camera.up,
            fov: base_camera.fov,
            near: base_camera.near,
            far: base_camera.far,
        }
    }
}

impl Default for HolographicConfig {
    fn default() -> Self {
        Self::looking_glass_portrait()
    }
}

/// Result of rendering a quilt.
#[derive(Debug, Clone)]
pub struct QuiltRenderInfo {
    /// Width of the complete quilt texture.
    pub width: u32,
    /// Height of the complete quilt texture.
    pub height: u32,
    /// Number of views rendered.
    pub num_views: u32,
    /// Columns in the quilt grid.
    pub columns: u32,
    /// Rows in the quilt grid.
    pub rows: u32,
}

impl QuiltRenderInfo {
    /// Create render info from holographic config.
    #[must_use]
    pub fn from_config(config: &HolographicConfig) -> Self {
        Self {
            width: config.quilt_width(),
            height: config.quilt_height(),
            num_views: config.num_views,
            columns: config.quilt_columns,
            rows: config.quilt_rows,
        }
    }
}

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

    const EPSILON: f32 = 1e-5;

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

    // ===========================================
    // TDD: Vec3 Tests
    // ===========================================

    #[test]
    fn test_vec3_new() {
        let v = Vec3::new(1.0, 2.0, 3.0);
        assert!(approx_eq(v.x, 1.0));
        assert!(approx_eq(v.y, 2.0));
        assert!(approx_eq(v.z, 3.0));
    }

    #[test]
    fn test_vec3_zero() {
        let v = Vec3::zero();
        assert!(approx_eq(v.x, 0.0));
        assert!(approx_eq(v.y, 0.0));
        assert!(approx_eq(v.z, 0.0));
    }

    #[test]
    fn test_vec3_length() {
        let v = Vec3::new(3.0, 4.0, 0.0);
        assert!(approx_eq(v.length(), 5.0));
    }

    #[test]
    fn test_vec3_normalize() {
        let v = Vec3::new(3.0, 0.0, 0.0);
        let n = v.normalize();
        assert!(approx_eq(n.x, 1.0));
        assert!(approx_eq(n.y, 0.0));
        assert!(approx_eq(n.z, 0.0));
    }

    #[test]
    fn test_vec3_normalize_zero() {
        let v = Vec3::zero();
        let n = v.normalize();
        // Zero vector normalizes to zero
        assert!(approx_eq(n.length(), 0.0));
    }

    #[test]
    fn test_vec3_cross() {
        let x = Vec3::new(1.0, 0.0, 0.0);
        let y = Vec3::new(0.0, 1.0, 0.0);
        let z = x.cross(&y);
        assert!(approx_eq(z.x, 0.0));
        assert!(approx_eq(z.y, 0.0));
        assert!(approx_eq(z.z, 1.0));
    }

    #[test]
    fn test_vec3_dot() {
        let a = Vec3::new(1.0, 2.0, 3.0);
        let b = Vec3::new(4.0, 5.0, 6.0);
        assert!(approx_eq(a.dot(&b), 32.0)); // 1*4 + 2*5 + 3*6 = 32
    }

    #[test]
    fn test_vec3_add_sub() {
        let a = Vec3::new(1.0, 2.0, 3.0);
        let b = Vec3::new(4.0, 5.0, 6.0);
        let sum = a.add(&b);
        let diff = a.sub(&b);

        assert!(approx_eq(sum.x, 5.0));
        assert!(approx_eq(sum.y, 7.0));
        assert!(approx_eq(sum.z, 9.0));

        assert!(approx_eq(diff.x, -3.0));
        assert!(approx_eq(diff.y, -3.0));
        assert!(approx_eq(diff.z, -3.0));
    }

    #[test]
    fn test_vec3_scale() {
        let v = Vec3::new(1.0, 2.0, 3.0);
        let scaled = v.scale(2.0);
        assert!(approx_eq(scaled.x, 2.0));
        assert!(approx_eq(scaled.y, 4.0));
        assert!(approx_eq(scaled.z, 6.0));
    }

    // ===========================================
    // TDD: Mat4 Tests
    // ===========================================

    #[test]
    fn test_mat4_identity() {
        let m = Mat4::identity();
        // Diagonal should be 1s
        assert!(approx_eq(m.data[0], 1.0));
        assert!(approx_eq(m.data[5], 1.0));
        assert!(approx_eq(m.data[10], 1.0));
        assert!(approx_eq(m.data[15], 1.0));
        // Off-diagonal should be 0s
        assert!(approx_eq(m.data[1], 0.0));
        assert!(approx_eq(m.data[4], 0.0));
    }

    #[test]
    fn test_mat4_mul_identity() {
        let m = Mat4::identity();
        let result = m.mul(&m);
        // Identity * Identity = Identity
        assert!(approx_eq(result.data[0], 1.0));
        assert!(approx_eq(result.data[5], 1.0));
        assert!(approx_eq(result.data[10], 1.0));
        assert!(approx_eq(result.data[15], 1.0));
    }

    #[test]
    fn test_mat4_look_at() {
        let eye = Vec3::new(0.0, 0.0, 5.0);
        let target = Vec3::zero();
        let up = Vec3::up();
        let view = Mat4::look_at(eye, target, up);

        // The view matrix should translate -eye when looking at origin
        // The last column should contain the translation
        assert!(view.data[15].abs() - 1.0 < EPSILON);
    }

    #[test]
    fn test_mat4_perspective() {
        let proj = Mat4::perspective(
            std::f32::consts::FRAC_PI_4, // 45 degrees
            1.0,                         // square aspect
            0.1,
            100.0,
        );
        // Perspective matrix should have -1 in [2,3] position (row 3, col 2)
        assert!(approx_eq(proj.data[11], -1.0));
    }

    // ===========================================
    // TDD: Camera Tests
    // ===========================================

    #[test]
    fn test_camera_default() {
        let cam = Camera::new();
        assert!(approx_eq(cam.position.z, 5.0));
        assert!(approx_eq(cam.target.x, 0.0));
        assert!(approx_eq(cam.target.y, 0.0));
        assert!(approx_eq(cam.target.z, 0.0));
    }

    #[test]
    fn test_camera_view_matrix() {
        let cam = Camera::new();
        let view = cam.view_matrix();
        // Should be a valid 4x4 matrix
        assert_eq!(view.data.len(), 16);
    }

    #[test]
    fn test_camera_projection_matrix() {
        let cam = Camera::new();
        let proj = cam.projection_matrix(16.0 / 9.0);
        // Should be a valid 4x4 matrix with perspective division
        assert!(approx_eq(proj.data[11], -1.0));
    }

    // ===========================================
    // TDD: HolographicConfig Tests
    // ===========================================

    #[test]
    fn test_holographic_config_portrait() {
        let config = HolographicConfig::looking_glass_portrait();
        assert_eq!(config.num_views, 45);
        assert_eq!(config.quilt_columns, 5);
        assert_eq!(config.quilt_rows, 9);
        assert_eq!(config.num_views, config.quilt_columns * config.quilt_rows);
    }

    #[test]
    fn test_holographic_config_4k() {
        let config = HolographicConfig::looking_glass_4k();
        assert_eq!(config.num_views, 45);
        assert_eq!(config.quilt_columns, 5);
        assert_eq!(config.quilt_rows, 9);
    }

    #[test]
    fn test_quilt_dimensions() {
        let config = HolographicConfig::looking_glass_portrait();
        let width = config.quilt_width();
        let height = config.quilt_height();

        assert_eq!(width, 5 * 420); // 2100
        assert_eq!(height, 9 * 560); // 5040
    }

    #[test]
    fn test_view_to_grid() {
        let config = HolographicConfig::looking_glass_portrait();

        // View 0 should be at (0, 0)
        let (col, row) = config.view_to_grid(0);
        assert_eq!(col, 0);
        assert_eq!(row, 0);

        // View 4 should be at (4, 0) - last column of first row
        let (col, row) = config.view_to_grid(4);
        assert_eq!(col, 4);
        assert_eq!(row, 0);

        // View 5 should be at (0, 1) - first column of second row
        let (col, row) = config.view_to_grid(5);
        assert_eq!(col, 0);
        assert_eq!(row, 1);

        // View 44 should be at (4, 8) - last view
        let (col, row) = config.view_to_grid(44);
        assert_eq!(col, 4);
        assert_eq!(row, 8);
    }

    #[test]
    fn test_view_offset() {
        let config = HolographicConfig::looking_glass_portrait();

        // View 0 starts at (0, 0)
        let (x, y) = config.view_offset(0);
        assert_eq!(x, 0);
        assert_eq!(y, 0);

        // View 1 starts at (420, 0)
        let (x, y) = config.view_offset(1);
        assert_eq!(x, 420);
        assert_eq!(y, 0);

        // View 5 starts at (0, 560) - second row
        let (x, y) = config.view_offset(5);
        assert_eq!(x, 0);
        assert_eq!(y, 560);
    }

    #[test]
    fn test_camera_for_view_single() {
        let config = HolographicConfig {
            num_views: 1,
            ..Default::default()
        };
        let base = Camera::new();
        let view_cam = config.camera_for_view(&base, 0);

        // Single view should return the same camera
        assert!(approx_eq(view_cam.position.x, base.position.x));
        assert!(approx_eq(view_cam.position.y, base.position.y));
        assert!(approx_eq(view_cam.position.z, base.position.z));
    }

    #[test]
    fn test_camera_for_view_center() {
        let config = HolographicConfig::looking_glass_portrait();
        let base = Camera::new();

        // Middle view (22) should be approximately at the center
        let center_cam = config.camera_for_view(&base, 22);

        // Center camera should have similar position to base
        // (small offset due to discrete view count)
        let distance_from_base = center_cam.position.sub(&base.position).length();
        assert!(distance_from_base < 0.5);
    }

    #[test]
    fn test_camera_for_view_symmetry() {
        let config = HolographicConfig::looking_glass_portrait();
        let base = Camera::new();

        // First and last views should be symmetric about center
        let left_cam = config.camera_for_view(&base, 0);
        let right_cam = config.camera_for_view(&base, 44);

        // X positions should be opposite
        assert!(approx_eq(left_cam.position.x, -right_cam.position.x));
        // Y positions should be the same
        assert!(approx_eq(left_cam.position.y, right_cam.position.y));
    }

    #[test]
    fn test_camera_for_view_maintains_distance() {
        let config = HolographicConfig::looking_glass_portrait();
        let base = Camera::new();
        let base_distance = base.position.sub(&base.target).length();

        // All views should maintain the same distance to target
        for i in 0..config.num_views {
            let view_cam = config.camera_for_view(&base, i);
            let view_distance = view_cam.position.sub(&view_cam.target).length();
            assert!(
                approx_eq(view_distance, base_distance),
                "View {i} distance {view_distance} != base {base_distance}"
            );
        }
    }

    #[test]
    fn test_camera_for_view_progression() {
        let config = HolographicConfig::looking_glass_portrait();
        let base = Camera::new();

        // Camera X position should increase from left to right
        let mut prev_x = f32::NEG_INFINITY;
        for i in 0..config.num_views {
            let view_cam = config.camera_for_view(&base, i);
            assert!(
                view_cam.position.x > prev_x,
                "View {} x {} should be > {}",
                i,
                view_cam.position.x,
                prev_x
            );
            prev_x = view_cam.position.x;
        }
    }

    #[test]
    fn test_camera_for_view_edge_angles() {
        let config = HolographicConfig::looking_glass_portrait();
        let base = Camera::new();

        // With base camera at (0, 0, 5) looking at origin,
        // edge views should be at half the view cone angle
        let left_cam = config.camera_for_view(&base, 0);
        let right_cam = config.camera_for_view(&base, 44);

        // Calculate actual angles from camera positions
        let left_angle = left_cam.position.x.atan2(left_cam.position.z);
        let right_angle = right_cam.position.x.atan2(right_cam.position.z);

        // Total angle span should match view_cone
        let total_span = right_angle - left_angle;
        let expected_span = config.view_cone;
        assert!(
            (total_span - expected_span).abs() < 0.01,
            "View cone span {total_span:.4} should match config {expected_span:.4}"
        );
    }

    #[test]
    fn test_camera_for_view_all_point_to_target() {
        let config = HolographicConfig::looking_glass_portrait();
        let base = Camera::new();

        // Every generated camera should point at the same target
        for i in 0..config.num_views {
            let view_cam = config.camera_for_view(&base, i);
            assert!(
                approx_eq(view_cam.target.x, base.target.x),
                "View {} target.x {} should equal base {}",
                i,
                view_cam.target.x,
                base.target.x
            );
            assert!(
                approx_eq(view_cam.target.y, base.target.y),
                "View {} target.y {} should equal base {}",
                i,
                view_cam.target.y,
                base.target.y
            );
            assert!(
                approx_eq(view_cam.target.z, base.target.z),
                "View {} target.z {} should equal base {}",
                i,
                view_cam.target.z,
                base.target.z
            );
        }
    }

    #[test]
    fn test_camera_for_view_preserves_camera_params() {
        let config = HolographicConfig::looking_glass_portrait();
        let base = Camera {
            position: Vec3::new(0.0, 0.0, 10.0),
            target: Vec3::new(0.0, 0.0, 0.0),
            up: Vec3::up(),
            fov: 0.8,  // Custom FOV
            near: 0.5, // Custom near
            far: 50.0, // Custom far
        };

        // All generated cameras should preserve FOV, near, far, and up
        for i in 0..config.num_views {
            let view_cam = config.camera_for_view(&base, i);
            assert!(approx_eq(view_cam.fov, base.fov), "View {i} FOV mismatch");
            assert!(
                approx_eq(view_cam.near, base.near),
                "View {i} near mismatch"
            );
            assert!(approx_eq(view_cam.far, base.far), "View {i} far mismatch");
            assert!(
                approx_eq(view_cam.up.x, base.up.x)
                    && approx_eq(view_cam.up.y, base.up.y)
                    && approx_eq(view_cam.up.z, base.up.z),
                "View {i} up vector mismatch"
            );
        }
    }

    #[test]
    fn test_camera_for_view_4k_config() {
        let config = HolographicConfig::looking_glass_4k();
        let base = Camera::new();

        // Same tests as portrait - verify 4K config also works
        assert_eq!(config.num_views, 45);

        // Center view should be near base
        let center_cam = config.camera_for_view(&base, 22);
        let distance_from_base = center_cam.position.sub(&base.position).length();
        assert!(
            distance_from_base < 0.5,
            "4K center view should be near base"
        );

        // Symmetry check
        let left_cam = config.camera_for_view(&base, 0);
        let right_cam = config.camera_for_view(&base, 44);
        assert!(
            approx_eq(left_cam.position.x, -right_cam.position.x),
            "4K edge views should be symmetric"
        );

        // View cone should match
        let left_angle = left_cam.position.x.atan2(left_cam.position.z);
        let right_angle = right_cam.position.x.atan2(right_cam.position.z);
        let total_span = right_angle - left_angle;
        assert!(
            (total_span - config.view_cone).abs() < 0.01,
            "4K view cone should match config"
        );
    }

    #[test]
    fn test_camera_for_view_custom_target() {
        let config = HolographicConfig::looking_glass_portrait();
        let base = Camera {
            position: Vec3::new(5.0, 2.0, 8.0),
            target: Vec3::new(5.0, 2.0, 0.0), // Offset target
            up: Vec3::up(),
            fov: std::f32::consts::FRAC_PI_4,
            near: 0.1,
            far: 100.0,
        };

        let base_distance = base.position.sub(&base.target).length();

        // All views should maintain distance to custom target
        for i in 0..config.num_views {
            let view_cam = config.camera_for_view(&base, i);
            let view_distance = view_cam.position.sub(&view_cam.target).length();
            assert!(
                approx_eq(view_distance, base_distance),
                "View {i} distance {view_distance} should match base {base_distance}"
            );
            // Target should be unchanged
            assert!(approx_eq(view_cam.target.x, base.target.x));
            assert!(approx_eq(view_cam.target.y, base.target.y));
            assert!(approx_eq(view_cam.target.z, base.target.z));
        }
    }

    #[test]
    fn test_camera_for_view_zero_views() {
        // Edge case: zero views should return base camera
        let config = HolographicConfig {
            num_views: 0,
            ..Default::default()
        };
        let base = Camera::new();
        let view_cam = config.camera_for_view(&base, 0);

        // With 0 views, should return base (num_views <= 1 branch)
        assert!(approx_eq(view_cam.position.x, base.position.x));
        assert!(approx_eq(view_cam.position.y, base.position.y));
        assert!(approx_eq(view_cam.position.z, base.position.z));
    }

    // ===========================================
    // TDD: QuiltRenderInfo Tests
    // ===========================================

    #[test]
    fn test_quilt_render_info_from_config() {
        let config = HolographicConfig::looking_glass_portrait();
        let info = QuiltRenderInfo::from_config(&config);

        assert_eq!(info.width, 2100);
        assert_eq!(info.height, 5040);
        assert_eq!(info.num_views, 45);
        assert_eq!(info.columns, 5);
        assert_eq!(info.rows, 9);
    }
}