oximedia-virtual 0.1.7

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
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
//! Camera tracking for virtual production.
//!
//! Provides low-pass filtering and pose interpolation for camera tracking
//! data used in LED volume and in-camera VFX workflows.

#![allow(dead_code)]

// ---------------------------------------------------------------------------
// Tracked pose
// ---------------------------------------------------------------------------

/// A single tracked camera pose sample.
#[derive(Debug, Clone, PartialEq)]
pub struct TrackedPose {
    /// X position in metres.
    pub x: f64,
    /// Y position in metres.
    pub y: f64,
    /// Z position in metres.
    pub z: f64,
    /// Rotation around X axis (pitch) in degrees.
    pub rx: f64,
    /// Rotation around Y axis (yaw) in degrees.
    pub ry: f64,
    /// Rotation around Z axis (roll) in degrees.
    pub rz: f64,
    /// Timestamp in milliseconds.
    pub timestamp_ms: u64,
}

impl TrackedPose {
    /// Create a new pose at position `(x, y, z)` with zero rotation.
    #[must_use]
    pub fn new(x: f64, y: f64, z: f64) -> Self {
        Self {
            x,
            y,
            z,
            rx: 0.0,
            ry: 0.0,
            rz: 0.0,
            timestamp_ms: 0,
        }
    }

    /// Euclidean distance to another pose (translation only).
    #[must_use]
    pub fn distance_to(&self, other: &TrackedPose) -> f64 {
        let dx = self.x - other.x;
        let dy = self.y - other.y;
        let dz = self.z - other.z;
        (dx * dx + dy * dy + dz * dz).sqrt()
    }

    /// Linearly interpolate between `self` and `other`.
    ///
    /// `t = 0.0` returns a copy of `self`; `t = 1.0` returns a copy of `other`.
    #[must_use]
    pub fn interpolate(&self, other: &TrackedPose, t: f64) -> TrackedPose {
        let lerp = |a: f64, b: f64| a + (b - a) * t;
        TrackedPose {
            x: lerp(self.x, other.x),
            y: lerp(self.y, other.y),
            z: lerp(self.z, other.z),
            rx: lerp(self.rx, other.rx),
            ry: lerp(self.ry, other.ry),
            rz: lerp(self.rz, other.rz),
            timestamp_ms: lerp(self.timestamp_ms as f64, other.timestamp_ms as f64) as u64,
        }
    }
}

// ---------------------------------------------------------------------------
// Low-pass filter helper
// ---------------------------------------------------------------------------

/// Single-pole low-pass filter.
///
/// `alpha` is in `[0, 1]`: 0 = no update, 1 = no filtering.
#[must_use]
pub fn low_pass_filter(current: f64, prev: f64, alpha: f64) -> f64 {
    alpha * current + (1.0 - alpha) * prev
}

// ---------------------------------------------------------------------------
// Camera tracker
// ---------------------------------------------------------------------------

/// Real-time camera tracker with low-pass filtering.
#[derive(Debug)]
pub struct CameraTracker {
    /// History of received poses (most recent last).
    pub poses: Vec<TrackedPose>,
    /// Low-pass filter coefficient in `[0, 1]`.
    pub filter_alpha: f64,
}

impl CameraTracker {
    /// Create a new tracker with the given filter coefficient.
    #[must_use]
    pub fn new(filter_alpha: f64) -> Self {
        Self {
            poses: Vec::new(),
            filter_alpha,
        }
    }

    /// Submit a new raw pose, apply smoothing, store, and return the filtered pose.
    pub fn update(&mut self, pose: TrackedPose) -> TrackedPose {
        let smoothed = self.smooth_pose(&pose);
        self.poses.push(smoothed.clone());
        smoothed
    }

    /// Return a smoothed version of `raw` based on the previous pose (if any).
    #[must_use]
    pub fn smooth_pose(&self, raw: &TrackedPose) -> TrackedPose {
        let alpha = self.filter_alpha;
        match self.poses.last() {
            None => raw.clone(),
            Some(prev) => TrackedPose {
                x: low_pass_filter(raw.x, prev.x, alpha),
                y: low_pass_filter(raw.y, prev.y, alpha),
                z: low_pass_filter(raw.z, prev.z, alpha),
                rx: low_pass_filter(raw.rx, prev.rx, alpha),
                ry: low_pass_filter(raw.ry, prev.ry, alpha),
                rz: low_pass_filter(raw.rz, prev.rz, alpha),
                timestamp_ms: raw.timestamp_ms,
            },
        }
    }

    /// Estimate instantaneous velocity `(vx, vy, vz)` in m/ms from the last two poses.
    ///
    /// Returns `None` if fewer than two poses have been recorded, or if the
    /// timestamps are identical.
    #[must_use]
    pub fn velocity(&self) -> Option<(f64, f64, f64)> {
        if self.poses.len() < 2 {
            return None;
        }
        let len = self.poses.len();
        let a = &self.poses[len - 2];
        let b = &self.poses[len - 1];
        let dt = b.timestamp_ms as f64 - a.timestamp_ms as f64;
        if dt == 0.0 {
            return None;
        }
        Some(((b.x - a.x) / dt, (b.y - a.y) / dt, (b.z - a.z) / dt))
    }

    /// Return a reference to the most recently recorded (filtered) pose.
    #[must_use]
    pub fn latest(&self) -> Option<&TrackedPose> {
        self.poses.last()
    }
}

// ---------------------------------------------------------------------------
// Frame prediction / motion extrapolation
// ---------------------------------------------------------------------------

/// Prediction model used for extrapolating camera motion forward in time.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PredictionModel {
    /// Constant velocity: assumes the camera continues at its current velocity.
    ConstantVelocity,
    /// Constant acceleration: uses acceleration estimated from the last 3 poses.
    ConstantAcceleration,
    /// Quadratic extrapolation using a 3-point polynomial fit.
    Quadratic,
}

/// Configuration for the frame predictor.
#[derive(Debug, Clone)]
pub struct FramePredictorConfig {
    /// Prediction model to use.
    pub model: PredictionModel,
    /// Maximum number of poses to retain for prediction history.
    pub max_history: usize,
    /// Maximum prediction horizon in milliseconds. Predictions beyond this
    /// are clamped to avoid runaway extrapolation.
    pub max_prediction_ms: f64,
    /// Low-pass filter alpha applied to predicted velocity/acceleration.
    pub smoothing_alpha: f64,
}

impl Default for FramePredictorConfig {
    fn default() -> Self {
        Self {
            model: PredictionModel::ConstantVelocity,
            max_history: 32,
            max_prediction_ms: 50.0,
            smoothing_alpha: 0.7,
        }
    }
}

/// Frame predictor that extrapolates camera tracking data forward in time
/// to compensate for pipeline latency.
///
/// Uses recent pose history to estimate velocity (and optionally acceleration),
/// then projects the camera pose forward by the requested time offset.
#[derive(Debug)]
pub struct FramePredictor {
    config: FramePredictorConfig,
    /// Pose history (most recent last), kept trimmed to `max_history`.
    history: Vec<TrackedPose>,
    /// Smoothed velocity estimate (m/ms) for x, y, z.
    velocity: (f64, f64, f64),
    /// Smoothed acceleration estimate (m/ms^2) for x, y, z.
    acceleration: (f64, f64, f64),
    /// Smoothed angular velocity (deg/ms) for rx, ry, rz.
    angular_velocity: (f64, f64, f64),
}

impl FramePredictor {
    /// Create a new frame predictor.
    #[must_use]
    pub fn new(config: FramePredictorConfig) -> Self {
        Self {
            config,
            history: Vec::new(),
            velocity: (0.0, 0.0, 0.0),
            acceleration: (0.0, 0.0, 0.0),
            angular_velocity: (0.0, 0.0, 0.0),
        }
    }

    /// Feed a new tracked pose. Updates internal velocity/acceleration estimates.
    pub fn feed(&mut self, pose: TrackedPose) {
        self.history.push(pose);
        if self.history.len() > self.config.max_history {
            self.history.remove(0);
        }
        self.update_estimates();
    }

    /// Predict the pose `dt_ms` milliseconds into the future from the latest sample.
    ///
    /// Returns `None` if no poses have been fed yet.
    #[must_use]
    pub fn predict(&self, dt_ms: f64) -> Option<TrackedPose> {
        let latest = self.history.last()?;
        let dt = dt_ms.min(self.config.max_prediction_ms);

        let (vx, vy, vz) = self.velocity;
        let (avx, avy, avz) = self.angular_velocity;

        match self.config.model {
            PredictionModel::ConstantVelocity => Some(TrackedPose {
                x: latest.x + vx * dt,
                y: latest.y + vy * dt,
                z: latest.z + vz * dt,
                rx: latest.rx + avx * dt,
                ry: latest.ry + avy * dt,
                rz: latest.rz + avz * dt,
                timestamp_ms: latest.timestamp_ms + dt as u64,
            }),
            PredictionModel::ConstantAcceleration => {
                let (ax, ay, az) = self.acceleration;
                Some(TrackedPose {
                    x: latest.x + vx * dt + 0.5 * ax * dt * dt,
                    y: latest.y + vy * dt + 0.5 * ay * dt * dt,
                    z: latest.z + vz * dt + 0.5 * az * dt * dt,
                    rx: latest.rx + avx * dt,
                    ry: latest.ry + avy * dt,
                    rz: latest.rz + avz * dt,
                    timestamp_ms: latest.timestamp_ms + dt as u64,
                })
            }
            PredictionModel::Quadratic => self.predict_quadratic(dt),
        }
    }

    /// Get the current smoothed velocity estimate in m/ms.
    #[must_use]
    pub fn velocity(&self) -> (f64, f64, f64) {
        self.velocity
    }

    /// Get the current smoothed acceleration estimate in m/ms^2.
    #[must_use]
    pub fn acceleration(&self) -> (f64, f64, f64) {
        self.acceleration
    }

    /// Get the current smoothed angular velocity estimate in deg/ms.
    #[must_use]
    pub fn angular_velocity(&self) -> (f64, f64, f64) {
        self.angular_velocity
    }

    /// Number of poses in the history buffer.
    #[must_use]
    pub fn history_len(&self) -> usize {
        self.history.len()
    }

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

    /// Compute prediction quality: ratio of prediction error to actual
    /// displacement over the last few frames. Returns `None` if insufficient data.
    ///
    /// A value close to 0.0 means excellent prediction quality.
    #[must_use]
    pub fn prediction_quality(&self) -> Option<f64> {
        if self.history.len() < 4 {
            return None;
        }

        // Use the second-to-last pose to predict the last pose
        let n = self.history.len();
        let p_prev = &self.history[n - 2];
        let p_actual = &self.history[n - 1];
        let dt = p_actual.timestamp_ms as f64 - p_prev.timestamp_ms as f64;
        if dt <= 0.0 {
            return None;
        }

        // Simple constant-velocity prediction from n-2
        let (vx, vy, vz) = self.velocity;
        let pred_x = p_prev.x + vx * dt;
        let pred_y = p_prev.y + vy * dt;
        let pred_z = p_prev.z + vz * dt;

        let error = ((pred_x - p_actual.x).powi(2)
            + (pred_y - p_actual.y).powi(2)
            + (pred_z - p_actual.z).powi(2))
        .sqrt();

        let displacement = p_prev.distance_to(p_actual);
        if displacement < 1e-12 {
            return Some(0.0);
        }

        Some(error / displacement)
    }

    // -----------------------------------------------------------------------
    // Internal estimation
    // -----------------------------------------------------------------------

    fn update_estimates(&mut self) {
        let alpha = self.config.smoothing_alpha;
        let n = self.history.len();

        // Velocity from last two poses
        if n >= 2 {
            let a = &self.history[n - 2];
            let b = &self.history[n - 1];
            let dt = b.timestamp_ms as f64 - a.timestamp_ms as f64;
            if dt > 0.0 {
                let raw_vx = (b.x - a.x) / dt;
                let raw_vy = (b.y - a.y) / dt;
                let raw_vz = (b.z - a.z) / dt;

                self.velocity.0 = low_pass_filter(raw_vx, self.velocity.0, alpha);
                self.velocity.1 = low_pass_filter(raw_vy, self.velocity.1, alpha);
                self.velocity.2 = low_pass_filter(raw_vz, self.velocity.2, alpha);

                let raw_avx = (b.rx - a.rx) / dt;
                let raw_avy = (b.ry - a.ry) / dt;
                let raw_avz = (b.rz - a.rz) / dt;

                self.angular_velocity.0 = low_pass_filter(raw_avx, self.angular_velocity.0, alpha);
                self.angular_velocity.1 = low_pass_filter(raw_avy, self.angular_velocity.1, alpha);
                self.angular_velocity.2 = low_pass_filter(raw_avz, self.angular_velocity.2, alpha);
            }
        }

        // Acceleration from last three poses (finite difference of velocity)
        if n >= 3 {
            let a = &self.history[n - 3];
            let b = &self.history[n - 2];
            let c = &self.history[n - 1];
            let dt1 = b.timestamp_ms as f64 - a.timestamp_ms as f64;
            let dt2 = c.timestamp_ms as f64 - b.timestamp_ms as f64;
            if dt1 > 0.0 && dt2 > 0.0 {
                let v1x = (b.x - a.x) / dt1;
                let v2x = (c.x - b.x) / dt2;
                let v1y = (b.y - a.y) / dt1;
                let v2y = (c.y - b.y) / dt2;
                let v1z = (b.z - a.z) / dt1;
                let v2z = (c.z - b.z) / dt2;

                let dt_avg = (dt1 + dt2) * 0.5;
                let raw_ax = (v2x - v1x) / dt_avg;
                let raw_ay = (v2y - v1y) / dt_avg;
                let raw_az = (v2z - v1z) / dt_avg;

                self.acceleration.0 = low_pass_filter(raw_ax, self.acceleration.0, alpha);
                self.acceleration.1 = low_pass_filter(raw_ay, self.acceleration.1, alpha);
                self.acceleration.2 = low_pass_filter(raw_az, self.acceleration.2, alpha);
            }
        }
    }

    /// Quadratic extrapolation using the last 3 poses.
    fn predict_quadratic(&self, dt_ms: f64) -> Option<TrackedPose> {
        let n = self.history.len();
        if n < 3 {
            // Fall back to constant velocity
            let latest = self.history.last()?;
            let (vx, vy, vz) = self.velocity;
            let (avx, avy, avz) = self.angular_velocity;
            let dt = dt_ms.min(self.config.max_prediction_ms);
            return Some(TrackedPose {
                x: latest.x + vx * dt,
                y: latest.y + vy * dt,
                z: latest.z + vz * dt,
                rx: latest.rx + avx * dt,
                ry: latest.ry + avy * dt,
                rz: latest.rz + avz * dt,
                timestamp_ms: latest.timestamp_ms + dt as u64,
            });
        }

        let p0 = &self.history[n - 3];
        let p1 = &self.history[n - 2];
        let p2 = &self.history[n - 1];

        // Use p2 as the reference point (t=0)
        let t0 = p0.timestamp_ms as f64 - p2.timestamp_ms as f64;
        let t1 = p1.timestamp_ms as f64 - p2.timestamp_ms as f64;
        let dt = dt_ms.min(self.config.max_prediction_ms);

        let extrapolate = |y0: f64, y1: f64, y2: f64| -> f64 {
            // Lagrange interpolation through (t0, y0), (t1, y1), (0, y2)
            // evaluated at t = dt
            let denom01 = t0 - t1;
            let denom02 = t0; // t0 - 0
            let denom12 = t1; // t1 - 0

            // Guard against zero denominators
            if denom01.abs() < 1e-15 || denom02.abs() < 1e-15 || denom12.abs() < 1e-15 {
                return y2; // degenerate: just return latest
            }

            let l0 = (dt - t1) * dt / (denom01 * denom02);
            let l1 = (dt - t0) * dt / ((-denom01) * denom12);
            let l2 = (dt - t0) * (dt - t1) / (denom02 * denom12);

            l0 * y0 + l1 * y1 + l2 * y2
        };

        Some(TrackedPose {
            x: extrapolate(p0.x, p1.x, p2.x),
            y: extrapolate(p0.y, p1.y, p2.y),
            z: extrapolate(p0.z, p1.z, p2.z),
            rx: extrapolate(p0.rx, p1.rx, p2.rx),
            ry: extrapolate(p0.ry, p1.ry, p2.ry),
            rz: extrapolate(p0.rz, p1.rz, p2.rz),
            timestamp_ms: p2.timestamp_ms + dt as u64,
        })
    }
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_tracked_pose_new() {
        let p = TrackedPose::new(1.0, 2.0, 3.0);
        assert_eq!(p.x, 1.0);
        assert_eq!(p.y, 2.0);
        assert_eq!(p.z, 3.0);
        assert_eq!(p.rx, 0.0);
        assert_eq!(p.timestamp_ms, 0);
    }

    #[test]
    fn test_tracked_pose_distance_to_same() {
        let p = TrackedPose::new(0.0, 0.0, 0.0);
        assert_eq!(p.distance_to(&p), 0.0);
    }

    #[test]
    fn test_tracked_pose_distance_to_unit() {
        let a = TrackedPose::new(0.0, 0.0, 0.0);
        let b = TrackedPose::new(1.0, 0.0, 0.0);
        assert!((a.distance_to(&b) - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_tracked_pose_distance_to_3d() {
        let a = TrackedPose::new(0.0, 0.0, 0.0);
        let b = TrackedPose::new(3.0, 4.0, 0.0);
        assert!((a.distance_to(&b) - 5.0).abs() < 1e-10);
    }

    #[test]
    fn test_tracked_pose_interpolate_t0() {
        let a = TrackedPose::new(0.0, 0.0, 0.0);
        let b = TrackedPose::new(10.0, 10.0, 10.0);
        let mid = a.interpolate(&b, 0.0);
        assert!((mid.x - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_tracked_pose_interpolate_t1() {
        let a = TrackedPose::new(0.0, 0.0, 0.0);
        let b = TrackedPose::new(10.0, 10.0, 10.0);
        let mid = a.interpolate(&b, 1.0);
        assert!((mid.x - 10.0).abs() < 1e-10);
    }

    #[test]
    fn test_tracked_pose_interpolate_midpoint() {
        let a = TrackedPose::new(0.0, 0.0, 0.0);
        let b = TrackedPose::new(10.0, 20.0, 30.0);
        let mid = a.interpolate(&b, 0.5);
        assert!((mid.x - 5.0).abs() < 1e-10);
        assert!((mid.y - 10.0).abs() < 1e-10);
        assert!((mid.z - 15.0).abs() < 1e-10);
    }

    #[test]
    fn test_low_pass_filter_alpha_one() {
        // alpha=1 means no filtering
        assert_eq!(low_pass_filter(5.0, 0.0, 1.0), 5.0);
    }

    #[test]
    fn test_low_pass_filter_alpha_zero() {
        // alpha=0 means no update
        assert_eq!(low_pass_filter(5.0, 3.0, 0.0), 3.0);
    }

    #[test]
    fn test_low_pass_filter_half() {
        let v = low_pass_filter(10.0, 0.0, 0.5);
        assert!((v - 5.0).abs() < 1e-10);
    }

    #[test]
    fn test_camera_tracker_no_poses_initially() {
        let t = CameraTracker::new(0.9);
        assert!(t.latest().is_none());
        assert!(t.velocity().is_none());
    }

    #[test]
    fn test_camera_tracker_update_stores_pose() {
        let mut t = CameraTracker::new(1.0); // alpha=1 → no filtering
        let p = TrackedPose::new(1.0, 2.0, 3.0);
        t.update(p);
        let latest = t.latest().expect("should succeed in test");
        assert!((latest.x - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_camera_tracker_velocity() {
        let mut t = CameraTracker::new(1.0);
        let mut p1 = TrackedPose::new(0.0, 0.0, 0.0);
        p1.timestamp_ms = 0;
        let mut p2 = TrackedPose::new(1.0, 0.0, 0.0);
        p2.timestamp_ms = 1000;
        t.update(p1);
        t.update(p2);
        let (vx, vy, vz) = t.velocity().expect("should succeed in test");
        assert!((vx - 0.001).abs() < 1e-10); // 1m / 1000ms
        assert_eq!(vy, 0.0);
        assert_eq!(vz, 0.0);
    }

    // --- Frame predictor tests ---

    fn make_linear_poses(
        start_x: f64,
        velocity: f64,
        count: usize,
        dt_ms: u64,
    ) -> Vec<TrackedPose> {
        (0..count)
            .map(|i| {
                let mut p =
                    TrackedPose::new(start_x + velocity * (i as f64 * dt_ms as f64), 0.0, 0.0);
                p.timestamp_ms = i as u64 * dt_ms;
                p
            })
            .collect()
    }

    #[test]
    fn test_frame_predictor_creation() {
        let fp = FramePredictor::new(FramePredictorConfig::default());
        assert_eq!(fp.history_len(), 0);
        assert!(fp.predict(10.0).is_none());
    }

    #[test]
    fn test_frame_predictor_constant_velocity_linear_motion() {
        let mut fp = FramePredictor::new(FramePredictorConfig {
            model: PredictionModel::ConstantVelocity,
            smoothing_alpha: 1.0, // no smoothing for clarity
            ..FramePredictorConfig::default()
        });

        // Camera moving at 0.001 m/ms (1 m/s) along X
        let poses = make_linear_poses(0.0, 0.001, 5, 10);
        for p in poses {
            fp.feed(p);
        }

        // Predict 10ms ahead: latest x = 0.04, velocity = 0.001 m/ms
        // => predicted x = 0.04 + 0.001 * 10 = 0.05
        let predicted = fp.predict(10.0).expect("should succeed in test");
        assert!(
            (predicted.x - 0.05).abs() < 1e-6,
            "predicted x: {}",
            predicted.x
        );
    }

    #[test]
    fn test_frame_predictor_constant_acceleration() {
        let mut fp = FramePredictor::new(FramePredictorConfig {
            model: PredictionModel::ConstantAcceleration,
            smoothing_alpha: 1.0,
            ..FramePredictorConfig::default()
        });

        // Accelerating motion: x = 0.5 * a * t^2, a = 0.0001 m/ms^2
        // t=0: x=0, t=10: x=0.005, t=20: x=0.02, t=30: x=0.045, t=40: x=0.08
        let a = 0.0001;
        for i in 0..5 {
            let t = i as f64 * 10.0;
            let mut p = TrackedPose::new(0.5 * a * t * t, 0.0, 0.0);
            p.timestamp_ms = t as u64;
            fp.feed(p);
        }

        // Predict 10ms from t=40 (x=0.08)
        // Velocity at t=40 ≈ a*40 = 0.004 m/ms
        // Predicted x ≈ 0.08 + 0.004*10 + 0.5*0.0001*100 = 0.08 + 0.04 + 0.005 = 0.125
        // (exact: 0.5 * 0.0001 * 50^2 = 0.125)
        let predicted = fp.predict(10.0).expect("should succeed in test");
        assert!(
            (predicted.x - 0.125).abs() < 0.02,
            "acceleration predicted x: {}",
            predicted.x
        );
    }

    #[test]
    fn test_frame_predictor_quadratic_model() {
        let mut fp = FramePredictor::new(FramePredictorConfig {
            model: PredictionModel::Quadratic,
            smoothing_alpha: 1.0,
            ..FramePredictorConfig::default()
        });

        // Quadratic motion: x(t) = 0.001 * t^2
        for i in 0..5 {
            let t = i as f64 * 10.0;
            let mut p = TrackedPose::new(0.001 * t * t, 0.0, 0.0);
            p.timestamp_ms = t as u64;
            fp.feed(p);
        }

        // Predict 10ms ahead from t=40 (x=1.6)
        // True x(50) = 0.001 * 2500 = 2.5
        let predicted = fp.predict(10.0).expect("should succeed in test");
        assert!(
            (predicted.x - 2.5).abs() < 0.1,
            "quadratic predicted x: {}",
            predicted.x
        );
    }

    #[test]
    fn test_frame_predictor_max_prediction_clamped() {
        let mut fp = FramePredictor::new(FramePredictorConfig {
            model: PredictionModel::ConstantVelocity,
            max_prediction_ms: 20.0,
            smoothing_alpha: 1.0,
            ..FramePredictorConfig::default()
        });

        let poses = make_linear_poses(0.0, 0.001, 3, 10);
        for p in poses {
            fp.feed(p);
        }

        // Request 100ms prediction, but should be clamped to 20ms
        let predicted = fp.predict(100.0).expect("should succeed in test");
        let expected = 0.02 + 0.001 * 20.0; // latest x + v * 20ms
        assert!(
            (predicted.x - expected).abs() < 1e-6,
            "clamped prediction: {} vs {}",
            predicted.x,
            expected
        );
    }

    #[test]
    fn test_frame_predictor_rotation_prediction() {
        let mut fp = FramePredictor::new(FramePredictorConfig {
            model: PredictionModel::ConstantVelocity,
            smoothing_alpha: 1.0,
            ..FramePredictorConfig::default()
        });

        // Camera rotating at 0.1 deg/ms around Y
        for i in 0..5 {
            let t = i as f64 * 10.0;
            let mut p = TrackedPose::new(0.0, 0.0, 0.0);
            p.ry = 0.1 * t;
            p.timestamp_ms = t as u64;
            fp.feed(p);
        }

        let predicted = fp.predict(10.0).expect("should succeed in test");
        // latest ry = 4.0, angular vel = 0.1 deg/ms => predicted ry = 5.0
        assert!(
            (predicted.ry - 5.0).abs() < 0.01,
            "predicted ry: {}",
            predicted.ry
        );
    }

    #[test]
    fn test_frame_predictor_velocity_estimation() {
        let mut fp = FramePredictor::new(FramePredictorConfig {
            smoothing_alpha: 1.0,
            ..FramePredictorConfig::default()
        });

        let poses = make_linear_poses(0.0, 0.002, 5, 10);
        for p in poses {
            fp.feed(p);
        }

        let (vx, vy, vz) = fp.velocity();
        assert!((vx - 0.002).abs() < 1e-6, "velocity x: {}", vx);
        assert!(vy.abs() < 1e-10);
        assert!(vz.abs() < 1e-10);
    }

    #[test]
    fn test_frame_predictor_prediction_quality() {
        let mut fp = FramePredictor::new(FramePredictorConfig {
            smoothing_alpha: 1.0,
            ..FramePredictorConfig::default()
        });

        // Perfect linear motion should have near-zero prediction error
        let poses = make_linear_poses(0.0, 0.001, 10, 10);
        for p in poses {
            fp.feed(p);
        }

        let quality = fp.prediction_quality().expect("should have enough data");
        assert!(
            quality < 0.01,
            "linear motion prediction quality should be near zero: {}",
            quality
        );
    }

    #[test]
    fn test_frame_predictor_no_data() {
        let fp = FramePredictor::new(FramePredictorConfig::default());
        assert!(fp.predict(10.0).is_none());
        assert!(fp.prediction_quality().is_none());
    }

    #[test]
    fn test_frame_predictor_single_pose() {
        let mut fp = FramePredictor::new(FramePredictorConfig::default());
        fp.feed(TrackedPose::new(1.0, 2.0, 3.0));

        // With only one pose, velocity is zero, so prediction = latest
        let predicted = fp.predict(10.0).expect("should succeed in test");
        assert!((predicted.x - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_frame_predictor_history_trimming() {
        let mut fp = FramePredictor::new(FramePredictorConfig {
            max_history: 4,
            ..FramePredictorConfig::default()
        });

        for i in 0..10 {
            let mut p = TrackedPose::new(i as f64, 0.0, 0.0);
            p.timestamp_ms = i * 100;
            fp.feed(p);
        }

        assert_eq!(fp.history_len(), 4);
    }

    #[test]
    fn test_frame_predictor_quadratic_fallback_with_few_poses() {
        let mut fp = FramePredictor::new(FramePredictorConfig {
            model: PredictionModel::Quadratic,
            smoothing_alpha: 1.0,
            ..FramePredictorConfig::default()
        });

        // Only 2 poses: should fall back to constant velocity
        let mut p1 = TrackedPose::new(0.0, 0.0, 0.0);
        p1.timestamp_ms = 0;
        let mut p2 = TrackedPose::new(1.0, 0.0, 0.0);
        p2.timestamp_ms = 10;
        fp.feed(p1);
        fp.feed(p2);

        let predicted = fp.predict(10.0).expect("should succeed in test");
        assert!(
            (predicted.x - 2.0).abs() < 0.01,
            "fallback prediction: {}",
            predicted.x
        );
    }

    #[test]
    fn test_frame_predictor_stationary_camera() {
        let mut fp = FramePredictor::new(FramePredictorConfig {
            smoothing_alpha: 1.0,
            ..FramePredictorConfig::default()
        });

        for i in 0..5 {
            let mut p = TrackedPose::new(5.0, 3.0, -2.0);
            p.timestamp_ms = i * 10;
            fp.feed(p);
        }

        let predicted = fp.predict(10.0).expect("should succeed in test");
        assert!((predicted.x - 5.0).abs() < 1e-10);
        assert!((predicted.y - 3.0).abs() < 1e-10);
        assert!((predicted.z - (-2.0)).abs() < 1e-10);
    }
}