oximedia-align 0.1.0

Video alignment and registration tools for multi-camera synchronization in 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
#![allow(dead_code)]
//! Elastic (non-rigid) alignment for deformable media registration.
//!
//! This module implements non-rigid alignment techniques that can handle local deformations
//! such as lens distortion residuals, rolling shutter wobble, and object-level motion.
//!
//! # Features
//!
//! - **Thin-plate spline (TPS) warping** for smooth non-rigid transforms
//! - **Control point management** with automatic correspondence
//! - **Regularized alignment** to prevent overfitting
//! - **Deformation field** representation and analysis

use crate::{AlignError, AlignResult, Point2D};

/// A control point pair used as a landmark for elastic alignment.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ControlPoint {
    /// Source position.
    pub source: Point2D,
    /// Target position (where the source should map to).
    pub target: Point2D,
    /// Weight for this control point (higher means stronger influence).
    pub weight: f64,
}

impl ControlPoint {
    /// Create a new control point pair.
    #[must_use]
    pub fn new(source: Point2D, target: Point2D, weight: f64) -> Self {
        Self {
            source,
            target,
            weight,
        }
    }

    /// Create with default weight 1.0.
    #[must_use]
    pub fn with_unit_weight(source: Point2D, target: Point2D) -> Self {
        Self {
            source,
            target,
            weight: 1.0,
        }
    }

    /// Compute the displacement vector (target - source).
    #[must_use]
    pub fn displacement(&self) -> (f64, f64) {
        (self.target.x - self.source.x, self.target.y - self.source.y)
    }

    /// Compute the displacement magnitude.
    #[must_use]
    pub fn displacement_magnitude(&self) -> f64 {
        let (dx, dy) = self.displacement();
        (dx * dx + dy * dy).sqrt()
    }
}

/// Configuration for elastic alignment.
#[derive(Debug, Clone)]
pub struct ElasticAlignConfig {
    /// Regularization parameter (lambda). Higher values produce smoother warps.
    pub regularization: f64,
    /// Minimum number of control points required.
    pub min_control_points: usize,
    /// Maximum allowed displacement in pixels.
    pub max_displacement: f64,
    /// Grid resolution for deformation field output (pixels per cell).
    pub grid_resolution: u32,
}

impl Default for ElasticAlignConfig {
    fn default() -> Self {
        Self {
            regularization: 0.01,
            min_control_points: 4,
            max_displacement: 100.0,
            grid_resolution: 16,
        }
    }
}

/// Thin-Plate Spline coefficients for one coordinate dimension.
#[derive(Debug, Clone)]
pub struct TpsCoefficients {
    /// Weights for each control point (non-linear part).
    pub weights: Vec<f64>,
    /// Affine part: a0 + a1*x + a2*y.
    pub affine: [f64; 3],
}

/// Result of elastic alignment computation.
#[derive(Debug, Clone)]
pub struct ElasticAlignResult {
    /// TPS coefficients for x-coordinate mapping.
    pub tps_x: TpsCoefficients,
    /// TPS coefficients for y-coordinate mapping.
    pub tps_y: TpsCoefficients,
    /// The control points used.
    pub control_points: Vec<ControlPoint>,
    /// Root-mean-square alignment error in pixels.
    pub rms_error: f64,
    /// Maximum alignment error in pixels.
    pub max_error: f64,
    /// Bending energy of the deformation (lower is smoother).
    pub bending_energy: f64,
}

/// A sampled deformation field on a regular grid.
#[derive(Debug, Clone)]
pub struct DeformationField {
    /// Horizontal displacement for each grid cell.
    pub dx: Vec<f64>,
    /// Vertical displacement for each grid cell.
    pub dy: Vec<f64>,
    /// Number of grid columns.
    pub cols: u32,
    /// Number of grid rows.
    pub rows: u32,
    /// Cell size in pixels.
    pub cell_size: u32,
}

impl DeformationField {
    /// Create a zero deformation field.
    #[must_use]
    pub fn new(width: u32, height: u32, cell_size: u32) -> Self {
        let cols = width.div_ceil(cell_size);
        let rows = height.div_ceil(cell_size);
        let count = (cols * rows) as usize;
        Self {
            dx: vec![0.0; count],
            dy: vec![0.0; count],
            cols,
            rows,
            cell_size,
        }
    }

    /// Get displacement at grid cell (cx, cy).
    #[must_use]
    pub fn get(&self, cx: u32, cy: u32) -> Option<(f64, f64)> {
        if cx < self.cols && cy < self.rows {
            let idx = (cy * self.cols + cx) as usize;
            Some((self.dx[idx], self.dy[idx]))
        } else {
            None
        }
    }

    /// Set displacement at grid cell (cx, cy).
    pub fn set(&mut self, cx: u32, cy: u32, dx: f64, dy: f64) {
        if cx < self.cols && cy < self.rows {
            let idx = (cy * self.cols + cx) as usize;
            self.dx[idx] = dx;
            self.dy[idx] = dy;
        }
    }

    /// Compute the average displacement magnitude across the field.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn average_displacement(&self) -> f64 {
        if self.dx.is_empty() {
            return 0.0;
        }
        let total: f64 = self
            .dx
            .iter()
            .zip(self.dy.iter())
            .map(|(x, y)| (x * x + y * y).sqrt())
            .sum();
        total / self.dx.len() as f64
    }

    /// Compute maximum displacement magnitude in the field.
    #[must_use]
    pub fn max_displacement(&self) -> f64 {
        self.dx
            .iter()
            .zip(self.dy.iter())
            .map(|(x, y)| (x * x + y * y).sqrt())
            .fold(0.0_f64, f64::max)
    }
}

/// The Thin-Plate Spline radial basis function: r^2 * ln(r).
#[allow(clippy::cast_precision_loss)]
fn tps_kernel(r: f64) -> f64 {
    if r < 1e-15 {
        0.0
    } else {
        r * r * r.ln()
    }
}

/// Elastic aligner using thin-plate spline interpolation.
#[derive(Debug, Clone)]
pub struct ElasticAligner {
    /// Configuration.
    config: ElasticAlignConfig,
}

impl ElasticAligner {
    /// Create a new elastic aligner with the given configuration.
    #[must_use]
    pub fn new(config: ElasticAlignConfig) -> Self {
        Self { config }
    }

    /// Create with default configuration.
    #[must_use]
    pub fn with_defaults() -> Self {
        Self {
            config: ElasticAlignConfig::default(),
        }
    }

    /// Compute TPS alignment from control point correspondences.
    pub fn align(&self, control_points: &[ControlPoint]) -> AlignResult<ElasticAlignResult> {
        let n = control_points.len();
        if n < self.config.min_control_points {
            return Err(AlignError::InsufficientData(format!(
                "Need at least {} control points, got {}",
                self.config.min_control_points, n
            )));
        }

        // Check max displacement constraint
        for cp in control_points {
            if cp.displacement_magnitude() > self.config.max_displacement {
                return Err(AlignError::InvalidConfig(format!(
                    "Control point displacement {:.1} exceeds max {:.1}",
                    cp.displacement_magnitude(),
                    self.config.max_displacement
                )));
            }
        }

        // Solve TPS for x and y independently
        let tps_x = self.solve_tps(control_points, true)?;
        let tps_y = self.solve_tps(control_points, false)?;

        // Compute errors
        let (rms_error, max_error) = self.compute_errors(control_points, &tps_x, &tps_y);

        // Compute bending energy
        let bending_energy = self.compute_bending_energy(control_points, &tps_x, &tps_y);

        Ok(ElasticAlignResult {
            tps_x,
            tps_y,
            control_points: control_points.to_vec(),
            rms_error,
            max_error,
            bending_energy,
        })
    }

    /// Transform a point using TPS coefficients.
    #[must_use]
    pub fn transform_point(&self, point: &Point2D, result: &ElasticAlignResult) -> Point2D {
        let new_x = self.evaluate_tps(point, &result.tps_x, &result.control_points);
        let new_y = self.evaluate_tps(point, &result.tps_y, &result.control_points);
        Point2D::new(new_x, new_y)
    }

    /// Generate a sampled deformation field from an alignment result.
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn generate_deformation_field(
        &self,
        result: &ElasticAlignResult,
        width: u32,
        height: u32,
    ) -> DeformationField {
        let cell_size = self.config.grid_resolution;
        let mut field = DeformationField::new(width, height, cell_size);

        for cy in 0..field.rows {
            for cx in 0..field.cols {
                let px = f64::from(cx * cell_size + cell_size / 2);
                let py = f64::from(cy * cell_size + cell_size / 2);
                let src = Point2D::new(px, py);
                let dst = self.transform_point(&src, result);
                field.set(cx, cy, dst.x - px, dst.y - py);
            }
        }

        field
    }

    /// Solve TPS for one coordinate (x if `for_x` is true, y otherwise).
    #[allow(clippy::cast_precision_loss)]
    fn solve_tps(&self, points: &[ControlPoint], for_x: bool) -> AlignResult<TpsCoefficients> {
        let n = points.len();
        // System size: n + 3 (n weights + 3 affine params)
        let size = n + 3;

        // Build the system matrix L and right-hand side v
        // L = | K  P |   v = | target_coords |
        //     | P' 0 |       |       0       |
        let mut l_matrix = vec![0.0f64; size * size];
        let mut rhs = vec![0.0f64; size];

        // Fill K (n x n kernel matrix) + regularization on diagonal
        for i in 0..n {
            for j in 0..n {
                let r = points[i].source.distance(&points[j].source);
                l_matrix[i * size + j] = tps_kernel(r);
            }
            // Regularization
            l_matrix[i * size + i] += self.config.regularization / points[i].weight;
        }

        // Fill P (n x 3) and P^T (3 x n)
        for i in 0..n {
            l_matrix[i * size + n] = 1.0;
            l_matrix[i * size + n + 1] = points[i].source.x;
            l_matrix[i * size + n + 2] = points[i].source.y;

            l_matrix[(n) * size + i] = 1.0;
            l_matrix[(n + 1) * size + i] = points[i].source.x;
            l_matrix[(n + 2) * size + i] = points[i].source.y;
        }

        // Fill rhs
        for i in 0..n {
            rhs[i] = if for_x {
                points[i].target.x
            } else {
                points[i].target.y
            };
        }

        // Solve using Gauss elimination with partial pivoting
        let solution = Self::gauss_solve(&mut l_matrix, &mut rhs, size)?;

        let weights = solution[..n].to_vec();
        let affine = [solution[n], solution[n + 1], solution[n + 2]];

        Ok(TpsCoefficients { weights, affine })
    }

    /// Evaluate TPS at a point.
    fn evaluate_tps(
        &self,
        point: &Point2D,
        tps: &TpsCoefficients,
        control_points: &[ControlPoint],
    ) -> f64 {
        let mut val = tps.affine[0] + tps.affine[1] * point.x + tps.affine[2] * point.y;

        for (i, cp) in control_points.iter().enumerate() {
            let r = point.distance(&cp.source);
            val += tps.weights[i] * tps_kernel(r);
        }

        val
    }

    /// Compute RMS and max error.
    #[allow(clippy::cast_precision_loss)]
    fn compute_errors(
        &self,
        points: &[ControlPoint],
        tps_x: &TpsCoefficients,
        tps_y: &TpsCoefficients,
    ) -> (f64, f64) {
        let mut sum_sq = 0.0;
        let mut max_e = 0.0_f64;

        for cp in points {
            let px = self.evaluate_tps(&cp.source, tps_x, points);
            let py = self.evaluate_tps(&cp.source, tps_y, points);
            let err = ((px - cp.target.x).powi(2) + (py - cp.target.y).powi(2)).sqrt();
            sum_sq += err * err;
            max_e = max_e.max(err);
        }

        let rms = (sum_sq / points.len() as f64).sqrt();
        (rms, max_e)
    }

    /// Compute bending energy.
    fn compute_bending_energy(
        &self,
        points: &[ControlPoint],
        tps_x: &TpsCoefficients,
        tps_y: &TpsCoefficients,
    ) -> f64 {
        let n = points.len();
        let mut energy = 0.0;

        for i in 0..n {
            for j in 0..n {
                let r = points[i].source.distance(&points[j].source);
                let k = tps_kernel(r);
                energy += tps_x.weights[i] * tps_x.weights[j] * k;
                energy += tps_y.weights[i] * tps_y.weights[j] * k;
            }
        }

        energy.abs()
    }

    /// Gaussian elimination with partial pivoting.
    fn gauss_solve(a: &mut [f64], b: &mut [f64], n: usize) -> AlignResult<Vec<f64>> {
        // Forward elimination
        for col in 0..n {
            // Partial pivoting
            let mut max_val = a[col * n + col].abs();
            let mut max_row = col;
            for row in (col + 1)..n {
                let val = a[row * n + col].abs();
                if val > max_val {
                    max_val = val;
                    max_row = row;
                }
            }

            if max_val < 1e-15 {
                return Err(AlignError::NumericalError(
                    "Singular matrix in TPS solve".to_string(),
                ));
            }

            // Swap rows
            if max_row != col {
                for k in 0..n {
                    a.swap(col * n + k, max_row * n + k);
                }
                b.swap(col, max_row);
            }

            // Eliminate below
            let pivot = a[col * n + col];
            for row in (col + 1)..n {
                let factor = a[row * n + col] / pivot;
                for k in col..n {
                    a[row * n + k] -= factor * a[col * n + k];
                }
                b[row] -= factor * b[col];
            }
        }

        // Back substitution
        let mut x = vec![0.0f64; n];
        for col in (0..n).rev() {
            let mut sum = b[col];
            for k in (col + 1)..n {
                sum -= a[col * n + k] * x[k];
            }
            x[col] = sum / a[col * n + col];
        }

        Ok(x)
    }

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

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

    #[test]
    fn test_control_point_creation() {
        let cp = ControlPoint::new(Point2D::new(10.0, 20.0), Point2D::new(12.0, 22.0), 1.0);
        assert!((cp.source.x - 10.0).abs() < f64::EPSILON);
        assert!((cp.target.x - 12.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_control_point_displacement() {
        let cp = ControlPoint::new(Point2D::new(0.0, 0.0), Point2D::new(3.0, 4.0), 1.0);
        let (dx, dy) = cp.displacement();
        assert!((dx - 3.0).abs() < f64::EPSILON);
        assert!((dy - 4.0).abs() < f64::EPSILON);
        assert!((cp.displacement_magnitude() - 5.0).abs() < 1e-10);
    }

    #[test]
    fn test_control_point_unit_weight() {
        let cp = ControlPoint::with_unit_weight(Point2D::new(0.0, 0.0), Point2D::new(1.0, 1.0));
        assert!((cp.weight - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_config_default() {
        let config = ElasticAlignConfig::default();
        assert!((config.regularization - 0.01).abs() < f64::EPSILON);
        assert_eq!(config.min_control_points, 4);
    }

    #[test]
    fn test_deformation_field_creation() {
        let field = DeformationField::new(320, 240, 16);
        assert_eq!(field.cols, 20);
        assert_eq!(field.rows, 15);
        assert_eq!(field.dx.len(), 300);
    }

    #[test]
    fn test_deformation_field_get_set() {
        let mut field = DeformationField::new(64, 64, 16);
        field.set(1, 2, 3.5, -1.5);
        let (dx, dy) = field.get(1, 2).unwrap();
        assert!((dx - 3.5).abs() < f64::EPSILON);
        assert!((dy - (-1.5)).abs() < f64::EPSILON);
    }

    #[test]
    fn test_deformation_field_average() {
        let mut field = DeformationField::new(32, 32, 16);
        field.set(0, 0, 3.0, 4.0); // magnitude 5
        field.set(1, 0, 0.0, 0.0);
        field.set(0, 1, 0.0, 0.0);
        field.set(1, 1, 0.0, 0.0);
        assert!((field.average_displacement() - 1.25).abs() < 1e-10);
    }

    #[test]
    fn test_deformation_field_max() {
        let mut field = DeformationField::new(32, 32, 16);
        field.set(0, 0, 3.0, 4.0);
        field.set(1, 0, 1.0, 0.0);
        assert!((field.max_displacement() - 5.0).abs() < 1e-10);
    }

    #[test]
    fn test_tps_kernel() {
        assert!((tps_kernel(0.0)).abs() < f64::EPSILON);
        // tps_kernel(1.0) = 1^2 * ln(1) = 0
        assert!((tps_kernel(1.0)).abs() < f64::EPSILON);
        // tps_kernel(e) = e^2 * ln(e) = e^2
        let e = std::f64::consts::E;
        assert!((tps_kernel(e) - e * e).abs() < 1e-10);
    }

    #[test]
    fn test_elastic_align_insufficient_points() {
        let aligner = ElasticAligner::with_defaults();
        let points = vec![ControlPoint::with_unit_weight(
            Point2D::new(0.0, 0.0),
            Point2D::new(1.0, 1.0),
        )];
        let result = aligner.align(&points);
        assert!(result.is_err());
    }

    #[test]
    fn test_elastic_align_identity() {
        let aligner = ElasticAligner::new(ElasticAlignConfig {
            regularization: 0.001,
            min_control_points: 4,
            max_displacement: 100.0,
            grid_resolution: 16,
        });

        // Identity mapping: source == target
        let points = vec![
            ControlPoint::with_unit_weight(Point2D::new(0.0, 0.0), Point2D::new(0.0, 0.0)),
            ControlPoint::with_unit_weight(Point2D::new(100.0, 0.0), Point2D::new(100.0, 0.0)),
            ControlPoint::with_unit_weight(Point2D::new(0.0, 100.0), Point2D::new(0.0, 100.0)),
            ControlPoint::with_unit_weight(Point2D::new(100.0, 100.0), Point2D::new(100.0, 100.0)),
        ];

        let result = aligner.align(&points).unwrap();
        // RMS error should be very small for identity
        assert!(result.rms_error < 1.0);
    }

    #[test]
    fn test_elastic_align_translation() {
        let aligner = ElasticAligner::new(ElasticAlignConfig {
            regularization: 0.001,
            min_control_points: 4,
            max_displacement: 100.0,
            grid_resolution: 16,
        });

        // Translation of (5, 3)
        let points = vec![
            ControlPoint::with_unit_weight(Point2D::new(0.0, 0.0), Point2D::new(5.0, 3.0)),
            ControlPoint::with_unit_weight(Point2D::new(100.0, 0.0), Point2D::new(105.0, 3.0)),
            ControlPoint::with_unit_weight(Point2D::new(0.0, 100.0), Point2D::new(5.0, 103.0)),
            ControlPoint::with_unit_weight(Point2D::new(100.0, 100.0), Point2D::new(105.0, 103.0)),
        ];

        let result = aligner.align(&points).unwrap();
        // Transform a test point
        let transformed = aligner.transform_point(&Point2D::new(50.0, 50.0), &result);
        // Should be approximately (55, 53)
        assert!((transformed.x - 55.0).abs() < 2.0);
        assert!((transformed.y - 53.0).abs() < 2.0);
    }

    #[test]
    fn test_elastic_align_max_displacement_exceeded() {
        let aligner = ElasticAligner::new(ElasticAlignConfig {
            max_displacement: 5.0,
            ..ElasticAlignConfig::default()
        });

        let points = vec![
            ControlPoint::with_unit_weight(Point2D::new(0.0, 0.0), Point2D::new(100.0, 100.0)),
            ControlPoint::with_unit_weight(Point2D::new(10.0, 0.0), Point2D::new(110.0, 100.0)),
            ControlPoint::with_unit_weight(Point2D::new(0.0, 10.0), Point2D::new(100.0, 110.0)),
            ControlPoint::with_unit_weight(Point2D::new(10.0, 10.0), Point2D::new(110.0, 110.0)),
        ];

        let result = aligner.align(&points);
        assert!(result.is_err());
    }
}