oximedia-virtual 0.1.8

Virtual production and LED wall tools for OxiMedia
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
//! Multi-camera coordination manager
//!
//! Provides multi-camera management with automatic camera selection
//! based on talent tracking position.

use super::{CameraId, MultiCameraState};
use crate::math::{Point3, Vector3};
use crate::{tracking::CameraPose, Result, VirtualProductionError};
use serde::{Deserialize, Serialize};

/// Multi-camera configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiCameraConfig {
    /// Number of cameras
    pub num_cameras: usize,
    /// Enable auto-switching
    pub auto_switch: bool,
}

impl Default for MultiCameraConfig {
    fn default() -> Self {
        Self {
            num_cameras: 1,
            auto_switch: false,
        }
    }
}

/// Criteria used for automatic camera selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AutoSwitchCriteria {
    /// Select the camera whose optical axis is most aligned with the
    /// talent's position (smallest angle between forward vector and
    /// direction to talent).
    BestAngle,
    /// Select the camera closest to the talent.
    NearestDistance,
    /// Select based on a weighted score combining angle and distance.
    /// The score = (1 - w) * normalized_angle + w * normalized_distance
    /// where w is the `distance_weight` in `AutoSwitchConfig`.
    WeightedScore,
    /// Select the camera that has the talent most centered in its
    /// field of view (closest to optical axis in screen space).
    CenteredFraming,
}

/// Configuration for automatic camera selection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutoSwitchConfig {
    /// Selection criteria.
    pub criteria: AutoSwitchCriteria,
    /// Minimum time between automatic switches (milliseconds).
    /// Prevents rapid ping-ponging between cameras.
    pub min_switch_interval_ms: u64,
    /// Hysteresis threshold: a new camera must score at least this
    /// much better (as a fraction, e.g. 0.1 = 10%) than the current
    /// camera to trigger a switch.
    pub hysteresis: f64,
    /// Distance weight for `WeightedScore` criteria (0.0 to 1.0).
    pub distance_weight: f64,
    /// Camera horizontal field of view in radians (used for `CenteredFraming`).
    pub camera_fov_h: f64,
}

impl Default for AutoSwitchConfig {
    fn default() -> Self {
        Self {
            criteria: AutoSwitchCriteria::BestAngle,
            min_switch_interval_ms: 2000,
            hysteresis: 0.15,
            distance_weight: 0.3,
            camera_fov_h: std::f64::consts::PI / 3.0, // 60 degrees
        }
    }
}

/// Result of evaluating a camera for talent coverage.
#[derive(Debug, Clone, Copy)]
pub struct CameraScore {
    /// Camera identifier.
    pub camera_id: CameraId,
    /// Angle between camera forward and direction to talent (radians).
    pub angle_to_talent: f64,
    /// Distance from camera to talent (meters).
    pub distance_to_talent: f64,
    /// Normalized score (0.0 = best, 1.0 = worst). Lower is better.
    pub score: f64,
    /// Whether the talent is within the camera's field of view.
    pub in_fov: bool,
}

/// Multi-camera manager
pub struct MultiCameraManager {
    config: MultiCameraConfig,
    state: MultiCameraState,
    /// Auto-switch configuration (used when auto_switch is enabled).
    auto_switch_config: AutoSwitchConfig,
    /// Timestamp (ns) of the last auto-switch. `None` if no switch has occurred.
    last_switch_timestamp_ns: Option<u64>,
    /// History of automatic switches for diagnostics.
    switch_history: Vec<SwitchEvent>,
}

/// Record of a camera switch event.
#[derive(Debug, Clone)]
pub struct SwitchEvent {
    /// Timestamp of the switch.
    pub timestamp_ns: u64,
    /// Camera switched from.
    pub from: CameraId,
    /// Camera switched to.
    pub to: CameraId,
    /// Score of the selected camera.
    pub score: f64,
    /// Reason for the switch.
    pub reason: String,
}

impl MultiCameraManager {
    /// Create new multi-camera manager
    pub fn new(config: MultiCameraConfig) -> Result<Self> {
        if config.num_cameras == 0 {
            return Err(VirtualProductionError::MultiCamera(
                "Number of cameras must be > 0".to_string(),
            ));
        }

        Ok(Self {
            config,
            state: MultiCameraState::new(),
            auto_switch_config: AutoSwitchConfig::default(),
            last_switch_timestamp_ns: None,
            switch_history: Vec::new(),
        })
    }

    /// Create with auto-switch configuration.
    pub fn with_auto_switch(
        config: MultiCameraConfig,
        auto_switch_config: AutoSwitchConfig,
    ) -> Result<Self> {
        if config.num_cameras == 0 {
            return Err(VirtualProductionError::MultiCamera(
                "Number of cameras must be > 0".to_string(),
            ));
        }

        Ok(Self {
            config: MultiCameraConfig {
                auto_switch: true,
                ..config
            },
            state: MultiCameraState::new(),
            auto_switch_config,
            last_switch_timestamp_ns: None,
            switch_history: Vec::new(),
        })
    }

    /// Update camera pose
    pub fn update_camera(&mut self, camera_id: CameraId, pose: CameraPose) {
        if let Some(entry) = self.state.poses.iter_mut().find(|(id, _)| *id == camera_id) {
            entry.1 = pose;
        } else {
            self.state.poses.push((camera_id, pose));
        }
    }

    /// Set active camera
    pub fn set_active_camera(&mut self, camera_id: CameraId) {
        self.state.active_camera = camera_id;
    }

    /// Get active camera
    #[must_use]
    pub fn active_camera(&self) -> CameraId {
        self.state.active_camera
    }

    /// Get active camera pose
    #[must_use]
    pub fn active_pose(&self) -> Option<&CameraPose> {
        self.state.active_pose()
    }

    /// Get all camera poses
    #[must_use]
    pub fn all_poses(&self) -> &[(CameraId, CameraPose)] {
        &self.state.poses
    }

    /// Get configuration
    #[must_use]
    pub fn config(&self) -> &MultiCameraConfig {
        &self.config
    }

    /// Get the auto-switch configuration.
    #[must_use]
    pub fn auto_switch_config(&self) -> &AutoSwitchConfig {
        &self.auto_switch_config
    }

    /// Set the auto-switch configuration.
    pub fn set_auto_switch_config(&mut self, config: AutoSwitchConfig) {
        self.auto_switch_config = config;
    }

    /// Evaluate all cameras and score them for a given talent position.
    ///
    /// Returns scores sorted best-first (lowest score first).
    #[must_use]
    pub fn evaluate_cameras(&self, talent_position: &Point3<f64>) -> Vec<CameraScore> {
        let mut scores: Vec<CameraScore> = self
            .state
            .poses
            .iter()
            .map(|(camera_id, pose)| self.score_camera(*camera_id, pose, talent_position))
            .collect();

        scores.sort_by(|a, b| {
            a.score
                .partial_cmp(&b.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        scores
    }

    /// Automatically select the best camera for the given talent position.
    ///
    /// Applies hysteresis to avoid rapid switching. Returns `Some(CameraId)`
    /// if a switch is recommended, `None` if the current camera is still best
    /// (or the switch interval hasn't elapsed).
    pub fn auto_select(
        &mut self,
        talent_position: &Point3<f64>,
        current_timestamp_ns: u64,
    ) -> Option<CameraId> {
        if !self.config.auto_switch {
            return None;
        }

        if self.state.poses.is_empty() {
            return None;
        }

        // Check minimum switch interval (skip check if no switch has occurred yet)
        if let Some(last_ts) = self.last_switch_timestamp_ns {
            let elapsed_ns = current_timestamp_ns.saturating_sub(last_ts);
            let min_interval_ns = self.auto_switch_config.min_switch_interval_ms * 1_000_000;
            if elapsed_ns < min_interval_ns {
                return None;
            }
        }

        let scores = self.evaluate_cameras(talent_position);
        if scores.is_empty() {
            return None;
        }

        let best = &scores[0];
        let current_id = self.state.active_camera;

        // If best is already active, no switch needed
        if best.camera_id == current_id {
            return None;
        }

        // Find current camera's score
        let current_score = scores
            .iter()
            .find(|s| s.camera_id == current_id)
            .map(|s| s.score)
            .unwrap_or(f64::MAX);

        // Hysteresis: only switch if the improvement exceeds the threshold
        let improvement = if current_score > 1e-10 {
            (current_score - best.score) / current_score
        } else {
            1.0
        };

        if improvement < self.auto_switch_config.hysteresis {
            return None;
        }

        // Perform the switch
        let previous_camera = self.state.active_camera;
        self.state.active_camera = best.camera_id;
        self.last_switch_timestamp_ns = Some(current_timestamp_ns);

        self.switch_history.push(SwitchEvent {
            timestamp_ns: current_timestamp_ns,
            from: previous_camera,
            to: best.camera_id,
            score: best.score,
            reason: format!(
                "{:?}: improvement {:.1}%",
                self.auto_switch_config.criteria,
                improvement * 100.0
            ),
        });

        Some(best.camera_id)
    }

    /// Get the switch history.
    #[must_use]
    pub fn switch_history(&self) -> &[SwitchEvent] {
        &self.switch_history
    }

    /// Clear switch history.
    pub fn clear_switch_history(&mut self) {
        self.switch_history.clear();
    }

    // -----------------------------------------------------------------------
    // Internal scoring
    // -----------------------------------------------------------------------

    fn score_camera(
        &self,
        camera_id: CameraId,
        pose: &CameraPose,
        talent_position: &Point3<f64>,
    ) -> CameraScore {
        let cam_pos = pose.position;
        let direction_to_talent = Vector3::new(
            talent_position.x - cam_pos.x,
            talent_position.y - cam_pos.y,
            talent_position.z - cam_pos.z,
        );

        let distance = direction_to_talent.norm();
        let dir_normalized = if distance > 1e-10 {
            Vector3::new(
                direction_to_talent.x / distance,
                direction_to_talent.y / distance,
                direction_to_talent.z / distance,
            )
        } else {
            Vector3::new(0.0, 0.0, -1.0)
        };

        // Camera forward vector
        let forward = pose.forward();

        // Angle between forward and direction to talent
        let cos_angle = forward.x * dir_normalized.x
            + forward.y * dir_normalized.y
            + forward.z * dir_normalized.z;
        let angle = cos_angle.clamp(-1.0, 1.0).acos();

        let in_fov = angle < self.auto_switch_config.camera_fov_h * 0.5;

        let score = match self.auto_switch_config.criteria {
            AutoSwitchCriteria::BestAngle => {
                // Normalize angle to [0, 1] where 0 is best
                angle / std::f64::consts::PI
            }
            AutoSwitchCriteria::NearestDistance => {
                // Normalize distance; assume max reasonable distance is 20m
                (distance / 20.0).min(1.0)
            }
            AutoSwitchCriteria::WeightedScore => {
                let w = self.auto_switch_config.distance_weight;
                let angle_norm = angle / std::f64::consts::PI;
                let dist_norm = (distance / 20.0).min(1.0);
                (1.0 - w) * angle_norm + w * dist_norm
            }
            AutoSwitchCriteria::CenteredFraming => {
                // Score based on how centered the talent is in the FOV
                if !in_fov {
                    1.0 // Worst score if out of FOV
                } else {
                    let half_fov = self.auto_switch_config.camera_fov_h * 0.5;
                    if half_fov > 1e-10 {
                        angle / half_fov
                    } else {
                        0.0
                    }
                }
            }
        };

        CameraScore {
            camera_id,
            angle_to_talent: angle,
            distance_to_talent: distance,
            score,
            in_fov,
        }
    }
}

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

    #[test]
    fn test_multicam_manager() {
        let config = MultiCameraConfig {
            num_cameras: 4,
            auto_switch: false,
        };
        let manager = MultiCameraManager::new(config);
        assert!(manager.is_ok());
    }

    #[test]
    fn test_multicam_update() {
        let config = MultiCameraConfig::default();
        let mut manager = MultiCameraManager::new(config).expect("should succeed in test");

        let pose = CameraPose::new(Point3::origin(), UnitQuaternion::identity(), 0);

        manager.update_camera(CameraId(0), pose);
        assert!(manager.active_pose().is_some());
    }

    #[test]
    fn test_multicam_switch() {
        let config = MultiCameraConfig {
            num_cameras: 2,
            auto_switch: false,
        };
        let mut manager = MultiCameraManager::new(config).expect("should succeed in test");

        manager.set_active_camera(CameraId(1));
        assert_eq!(manager.active_camera(), CameraId(1));
    }

    // --- Auto camera selection tests ---

    fn make_camera_pose(x: f64, y: f64, z: f64, look_z: f64) -> CameraPose {
        // Camera at (x, y, z), looking along -Z by default
        let _ = look_z; // orientation is identity (looks along -Z)
        CameraPose::new(Point3::new(x, y, z), UnitQuaternion::identity(), 0)
    }

    #[test]
    fn test_evaluate_cameras_best_angle() {
        let config = MultiCameraConfig {
            num_cameras: 3,
            auto_switch: true,
        };
        let mut manager = MultiCameraManager::with_auto_switch(
            config,
            AutoSwitchConfig {
                criteria: AutoSwitchCriteria::BestAngle,
                ..AutoSwitchConfig::default()
            },
        )
        .expect("should succeed in test");

        // Camera 0 at origin looking -Z
        manager.update_camera(CameraId(0), make_camera_pose(0.0, 0.0, 0.0, -1.0));
        // Camera 1 offset to the right
        manager.update_camera(CameraId(1), make_camera_pose(5.0, 0.0, 0.0, -1.0));
        // Camera 2 far away
        manager.update_camera(CameraId(2), make_camera_pose(10.0, 0.0, 0.0, -1.0));

        // Talent directly in front of camera 0 at (0, 0, -5)
        let talent_pos = Point3::new(0.0, 0.0, -5.0);
        let scores = manager.evaluate_cameras(&talent_pos);

        assert_eq!(scores.len(), 3);
        // Camera 0 should have the best (lowest) score - talent is on its axis
        assert_eq!(scores[0].camera_id, CameraId(0));
        assert!(
            scores[0].score < scores[1].score,
            "cam0 score {} should be < cam1 score {}",
            scores[0].score,
            scores[1].score
        );
    }

    #[test]
    fn test_evaluate_cameras_nearest_distance() {
        let config = MultiCameraConfig {
            num_cameras: 2,
            auto_switch: true,
        };
        let mut manager = MultiCameraManager::with_auto_switch(
            config,
            AutoSwitchConfig {
                criteria: AutoSwitchCriteria::NearestDistance,
                ..AutoSwitchConfig::default()
            },
        )
        .expect("should succeed in test");

        manager.update_camera(CameraId(0), make_camera_pose(0.0, 0.0, 0.0, -1.0));
        manager.update_camera(CameraId(1), make_camera_pose(0.0, 0.0, -4.0, -1.0));

        // Talent at (0, 0, -5) - closer to camera 1
        let talent_pos = Point3::new(0.0, 0.0, -5.0);
        let scores = manager.evaluate_cameras(&talent_pos);

        assert_eq!(scores[0].camera_id, CameraId(1));
        assert!(
            scores[0].distance_to_talent < scores[1].distance_to_talent,
            "cam1 should be closer"
        );
    }

    #[test]
    fn test_auto_select_switches_camera() {
        let config = MultiCameraConfig {
            num_cameras: 2,
            auto_switch: true,
        };
        let mut manager = MultiCameraManager::with_auto_switch(
            config,
            AutoSwitchConfig {
                criteria: AutoSwitchCriteria::BestAngle,
                min_switch_interval_ms: 0, // allow immediate switching
                hysteresis: 0.05,
                ..AutoSwitchConfig::default()
            },
        )
        .expect("should succeed in test");

        manager.update_camera(CameraId(0), make_camera_pose(0.0, 0.0, 0.0, -1.0));
        manager.update_camera(CameraId(1), make_camera_pose(5.0, 0.0, 0.0, -1.0));

        // Start with camera 0 active
        manager.set_active_camera(CameraId(0));

        // Talent moves to be directly in front of camera 1
        // Camera 1 at (5,0,0) looking -Z, talent at (5, 0, -5)
        let talent_pos = Point3::new(5.0, 0.0, -5.0);
        let result = manager.auto_select(&talent_pos, 1_000_000_000);

        // Should switch to camera 1
        assert_eq!(result, Some(CameraId(1)));
        assert_eq!(manager.active_camera(), CameraId(1));
    }

    #[test]
    fn test_auto_select_respects_min_interval() {
        let config = MultiCameraConfig {
            num_cameras: 2,
            auto_switch: true,
        };
        let mut manager = MultiCameraManager::with_auto_switch(
            config,
            AutoSwitchConfig {
                criteria: AutoSwitchCriteria::NearestDistance,
                min_switch_interval_ms: 2000,
                hysteresis: 0.0,
                ..AutoSwitchConfig::default()
            },
        )
        .expect("should succeed in test");

        // Camera 0 at origin, camera 1 at (10, 0, 0)
        manager.update_camera(CameraId(0), make_camera_pose(0.0, 0.0, 0.0, -1.0));
        manager.update_camera(CameraId(1), make_camera_pose(10.0, 0.0, 0.0, -1.0));
        manager.set_active_camera(CameraId(0));

        // Talent right next to camera 1 => should switch to cam 1
        let talent_near_cam1 = Point3::new(10.0, 0.0, -1.0);
        let r1 = manager.auto_select(&talent_near_cam1, 0);
        assert!(r1.is_some(), "should switch to nearer camera");

        // Now talent moves back near cam 0, but interval not elapsed
        let talent_near_cam0 = Point3::new(0.0, 0.0, -1.0);
        let r2 = manager.auto_select(&talent_near_cam0, 500_000_000); // 0.5s later
        assert!(r2.is_none(), "should respect min switch interval");

        // After interval elapses, should be able to switch again
        let r3 = manager.auto_select(&talent_near_cam0, 3_000_000_000); // 3s later
        assert!(r3.is_some(), "should allow switch after interval");
    }

    #[test]
    fn test_auto_select_hysteresis() {
        let config = MultiCameraConfig {
            num_cameras: 2,
            auto_switch: true,
        };
        let mut manager = MultiCameraManager::with_auto_switch(
            config,
            AutoSwitchConfig {
                criteria: AutoSwitchCriteria::BestAngle,
                min_switch_interval_ms: 0,
                hysteresis: 0.5, // very high hysteresis
                ..AutoSwitchConfig::default()
            },
        )
        .expect("should succeed in test");

        manager.update_camera(CameraId(0), make_camera_pose(0.0, 0.0, 0.0, -1.0));
        manager.update_camera(CameraId(1), make_camera_pose(1.0, 0.0, 0.0, -1.0));
        manager.set_active_camera(CameraId(0));

        // Talent slightly favors camera 1, but not by 50%
        let talent_pos = Point3::new(0.5, 0.0, -5.0);
        let result = manager.auto_select(&talent_pos, 1_000_000_000);

        // High hysteresis should prevent switching for a marginal improvement
        assert!(result.is_none(), "hysteresis should prevent switch");
    }

    #[test]
    fn test_auto_select_disabled() {
        let config = MultiCameraConfig {
            num_cameras: 2,
            auto_switch: false,
        };
        let mut manager = MultiCameraManager::new(config).expect("should succeed in test");
        manager.update_camera(CameraId(0), make_camera_pose(0.0, 0.0, 0.0, -1.0));
        manager.update_camera(CameraId(1), make_camera_pose(5.0, 0.0, 0.0, -1.0));

        let talent_pos = Point3::new(5.0, 0.0, -5.0);
        let result = manager.auto_select(&talent_pos, 1_000_000_000);
        assert!(result.is_none(), "auto_select should be disabled");
    }

    #[test]
    fn test_switch_history() {
        let config = MultiCameraConfig {
            num_cameras: 2,
            auto_switch: true,
        };
        let mut manager = MultiCameraManager::with_auto_switch(
            config,
            AutoSwitchConfig {
                criteria: AutoSwitchCriteria::BestAngle,
                min_switch_interval_ms: 0,
                hysteresis: 0.0,
                ..AutoSwitchConfig::default()
            },
        )
        .expect("should succeed in test");

        manager.update_camera(CameraId(0), make_camera_pose(0.0, 0.0, 0.0, -1.0));
        manager.update_camera(CameraId(1), make_camera_pose(5.0, 0.0, 0.0, -1.0));
        manager.set_active_camera(CameraId(0));

        let talent_pos = Point3::new(5.0, 0.0, -5.0);
        manager.auto_select(&talent_pos, 1_000_000_000);

        assert_eq!(manager.switch_history().len(), 1);
        assert_eq!(manager.switch_history()[0].from, CameraId(0));
        assert_eq!(manager.switch_history()[0].to, CameraId(1));

        manager.clear_switch_history();
        assert!(manager.switch_history().is_empty());
    }

    #[test]
    fn test_camera_score_in_fov() {
        let config = MultiCameraConfig {
            num_cameras: 1,
            auto_switch: true,
        };
        let mut manager = MultiCameraManager::with_auto_switch(
            config,
            AutoSwitchConfig {
                camera_fov_h: std::f64::consts::PI / 3.0, // 60 deg
                ..AutoSwitchConfig::default()
            },
        )
        .expect("should succeed in test");

        manager.update_camera(CameraId(0), make_camera_pose(0.0, 0.0, 0.0, -1.0));

        // Talent directly ahead - should be in FOV
        let scores_ahead = manager.evaluate_cameras(&Point3::new(0.0, 0.0, -5.0));
        assert!(scores_ahead[0].in_fov, "talent ahead should be in FOV");

        // Talent behind camera - should NOT be in FOV
        let scores_behind = manager.evaluate_cameras(&Point3::new(0.0, 0.0, 5.0));
        assert!(
            !scores_behind[0].in_fov,
            "talent behind should not be in FOV"
        );
    }

    #[test]
    fn test_weighted_score_criteria() {
        let config = MultiCameraConfig {
            num_cameras: 2,
            auto_switch: true,
        };
        let mut manager = MultiCameraManager::with_auto_switch(
            config,
            AutoSwitchConfig {
                criteria: AutoSwitchCriteria::WeightedScore,
                distance_weight: 0.5,
                min_switch_interval_ms: 0,
                hysteresis: 0.0,
                ..AutoSwitchConfig::default()
            },
        )
        .expect("should succeed in test");

        manager.update_camera(CameraId(0), make_camera_pose(0.0, 0.0, 0.0, -1.0));
        manager.update_camera(CameraId(1), make_camera_pose(2.0, 0.0, 0.0, -1.0));

        let scores = manager.evaluate_cameras(&Point3::new(1.0, 0.0, -3.0));
        // Both cameras should have valid scores
        assert_eq!(scores.len(), 2);
        for s in &scores {
            assert!(
                s.score >= 0.0 && s.score <= 1.0,
                "score out of range: {}",
                s.score
            );
        }
    }

    #[test]
    fn test_centered_framing_criteria() {
        let config = MultiCameraConfig {
            num_cameras: 2,
            auto_switch: true,
        };
        let mut manager = MultiCameraManager::with_auto_switch(
            config,
            AutoSwitchConfig {
                criteria: AutoSwitchCriteria::CenteredFraming,
                min_switch_interval_ms: 0,
                hysteresis: 0.0,
                camera_fov_h: std::f64::consts::PI / 3.0,
                ..AutoSwitchConfig::default()
            },
        )
        .expect("should succeed in test");

        manager.update_camera(CameraId(0), make_camera_pose(0.0, 0.0, 0.0, -1.0));
        manager.update_camera(CameraId(1), make_camera_pose(5.0, 0.0, 0.0, -1.0));

        // Talent at (0, 0, -5) - perfectly centered for camera 0
        let scores = manager.evaluate_cameras(&Point3::new(0.0, 0.0, -5.0));
        assert_eq!(scores[0].camera_id, CameraId(0));
        assert!(
            scores[0].score < 0.05,
            "perfectly centered should have very low score: {}",
            scores[0].score
        );
    }

    #[test]
    fn test_auto_select_no_cameras() {
        let config = MultiCameraConfig {
            num_cameras: 1,
            auto_switch: true,
        };
        let mut manager = MultiCameraManager::with_auto_switch(config, AutoSwitchConfig::default())
            .expect("should succeed in test");

        // No cameras registered yet
        let talent_pos = Point3::new(0.0, 0.0, -5.0);
        let result = manager.auto_select(&talent_pos, 1_000_000_000);
        assert!(result.is_none());
    }
}