inlier 0.1.0

Robust model fitting primitives for RANSAC-based pipelines in Rust.
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
//! Fundamental matrix estimator using 8-point and 7-point algorithms.

use crate::core::Estimator;
use crate::models::FundamentalMatrix;
use crate::types::DataMatrix;
use crate::utils::solve_cubic_real;
use nalgebra::{DMatrix, DVector, Matrix3, SVD};

/// Fundamental matrix estimator using the 8-point algorithm.
pub struct FundamentalEstimator;

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

impl FundamentalEstimator {
    pub fn new() -> Self {
        Self
    }

    /// Normalize points for numerical stability (Hartley normalization).
    fn normalize_points(
        &self,
        data: &DataMatrix,
        sample: &[usize],
        normalized: &mut DMatrix<f64>,
        t1: &mut Matrix3<f64>,
        t2: &mut Matrix3<f64>,
    ) -> bool {
        let n = sample.len();
        if n == 0 {
            return false;
        }

        // Compute centroids
        let mut cx1 = 0.0;
        let mut cy1 = 0.0;
        let mut cx2 = 0.0;
        let mut cy2 = 0.0;

        for &idx in sample {
            cx1 += data[(idx, 0)];
            cy1 += data[(idx, 1)];
            cx2 += data[(idx, 2)];
            cy2 += data[(idx, 3)];
        }

        cx1 /= n as f64;
        cy1 /= n as f64;
        cx2 /= n as f64;
        cy2 /= n as f64;

        // Compute mean distances
        let mut d1 = 0.0;
        let mut d2 = 0.0;

        for &idx in sample {
            let dx1 = data[(idx, 0)] - cx1;
            let dy1 = data[(idx, 1)] - cy1;
            let dx2 = data[(idx, 2)] - cx2;
            let dy2 = data[(idx, 3)] - cy2;
            d1 += (dx1 * dx1 + dy1 * dy1).sqrt();
            d2 += (dx2 * dx2 + dy2 * dy2).sqrt();
        }

        d1 /= n as f64;
        d2 /= n as f64;

        if d1 < 1e-10 || d2 < 1e-10 {
            return false;
        }

        let s1 = std::f64::consts::SQRT_2 / d1;
        let s2 = std::f64::consts::SQRT_2 / d2;

        // Build normalization transforms
        *t1 = Matrix3::new(s1, 0.0, -s1 * cx1, 0.0, s1, -s1 * cy1, 0.0, 0.0, 1.0);
        *t2 = Matrix3::new(s2, 0.0, -s2 * cx2, 0.0, s2, -s2 * cy2, 0.0, 0.0, 1.0);

        // Normalize points
        normalized.resize_mut(n, 4, 0.0);
        for (i, &idx) in sample.iter().enumerate() {
            normalized[(i, 0)] = (data[(idx, 0)] - cx1) * s1;
            normalized[(i, 1)] = (data[(idx, 1)] - cy1) * s1;
            normalized[(i, 2)] = (data[(idx, 2)] - cx2) * s2;
            normalized[(i, 3)] = (data[(idx, 3)] - cy2) * s2;
        }

        true
    }

    /// Seven-point fundamental matrix solver (matches C++ implementation).
    /// Returns up to 3 solutions by solving a cubic equation.
    fn estimate_seven_point(&self, data: &DataMatrix, sample: &[usize]) -> Vec<FundamentalMatrix> {
        // Build 7x9 coefficient matrix
        let mut coefficients = DMatrix::<f64>::zeros(7, 9);
        for (i, &idx) in sample.iter().enumerate() {
            let x0 = data[(idx, 0)];
            let y0 = data[(idx, 1)];
            let x1 = data[(idx, 2)];
            let y1 = data[(idx, 3)];

            coefficients[(i, 0)] = x1 * x0;
            coefficients[(i, 1)] = x1 * y0;
            coefficients[(i, 2)] = x1;
            coefficients[(i, 3)] = y1 * x0;
            coefficients[(i, 4)] = y1 * y0;
            coefficients[(i, 5)] = y1;
            coefficients[(i, 6)] = x0;
            coefficients[(i, 7)] = y0;
            coefficients[(i, 8)] = 1.0;
        }

        // Find null space using SVD (last 2 columns of V correspond to null space)
        let svd = SVD::new(coefficients.clone(), false, true);
        let vt = match svd.v_t {
            Some(vt) => vt,
            None => return Vec::new(),
        };
        let v = vt.transpose();

        // Last 2 columns are the null space basis
        if v.ncols() < 9 {
            return Vec::new();
        }

        // Extract f1 and f2 from null space (columns 7 and 8)
        let f1 = v.column(7);
        let f2 = v.column(8);

        // Compute determinant coefficients manually (matching C++ implementation)
        // det(F) = F[0]*(F[4]*F[8] - F[5]*F[7]) - F[1]*(F[3]*F[8] - F[5]*F[6]) + F[2]*(F[3]*F[7] - F[4]*F[6])
        // f1 and f2 are stored in column-major order: [f00, f10, f20, f01, f11, f21, f02, f12, f22]
        let idx = |r: usize, c: usize| c * 3 + r;

        let f1_0 = f1[idx(0, 0)];
        let f1_1 = f1[idx(0, 1)];
        let f1_2 = f1[idx(0, 2)];
        let f1_3 = f1[idx(1, 0)];
        let f1_4 = f1[idx(1, 1)];
        let f1_5 = f1[idx(1, 2)];
        let f1_6 = f1[idx(2, 0)];
        let f1_7 = f1[idx(2, 1)];
        let f1_8 = f1[idx(2, 2)];

        let f2_0 = f2[idx(0, 0)];
        let f2_1 = f2[idx(0, 1)];
        let f2_2 = f2[idx(0, 2)];
        let f2_3 = f2[idx(1, 0)];
        let f2_4 = f2[idx(1, 1)];
        let f2_5 = f2[idx(1, 2)];
        let f2_6 = f2[idx(2, 0)];
        let f2_7 = f2[idx(2, 1)];
        let f2_8 = f2[idx(2, 2)];

        // Compute cubic coefficients (matching C++ code exactly)
        let c3 = f1_0 * (f1_4 * f1_8 - f1_5 * f1_7) - f1_1 * (f1_3 * f1_8 - f1_5 * f1_6)
            + f1_2 * (f1_3 * f1_7 - f1_4 * f1_6);

        let c2 = f1_0 * (f1_4 * f2_8 + f2_4 * f1_8 - f1_5 * f2_7 - f2_5 * f1_7)
            + f2_0 * (f1_4 * f1_8 - f1_5 * f1_7)
            - f1_1 * (f1_3 * f2_8 + f2_3 * f1_8 - f1_5 * f2_6 - f2_5 * f1_6)
            - f2_1 * (f1_3 * f1_8 - f1_5 * f1_6)
            + f1_2 * (f1_3 * f2_7 + f2_3 * f1_7 - f1_4 * f2_6 - f2_4 * f1_6)
            + f2_2 * (f1_3 * f1_7 - f1_4 * f1_6);

        let c1 = f1_0 * (f2_4 * f2_8 - f2_5 * f2_7)
            + f2_0 * (f1_4 * f2_8 + f2_4 * f1_8 - f1_5 * f2_7 - f2_5 * f1_7)
            - f1_1 * (f2_3 * f2_8 - f2_5 * f2_6)
            - f2_1 * (f1_3 * f2_8 + f2_3 * f1_8 - f1_5 * f2_6 - f2_5 * f1_6)
            + f1_2 * (f2_3 * f2_7 - f2_4 * f2_6)
            + f2_2 * (f1_3 * f2_7 + f2_3 * f1_7 - f1_4 * f2_6 - f2_4 * f1_6);

        let c0 = f2_0 * (f2_4 * f2_8 - f2_5 * f2_7) - f2_1 * (f2_3 * f2_8 - f2_5 * f2_6)
            + f2_2 * (f2_3 * f2_7 - f2_4 * f2_6);

        // Normalize polynomial (divide by c3 to get monic form)
        if c3.abs() < 1e-10_f64 {
            return Vec::new();
        }
        let inv_c3 = 1.0 / c3;
        let c2_norm = c2 * inv_c3;
        let c1_norm = c1 * inv_c3;
        let c0_norm = c0 * inv_c3;

        // Solve cubic: x^3 + c2*x^2 + c1*x + c0 = 0
        let mut roots = [0.0; 3];
        let n_roots = solve_cubic_real(c2_norm, c1_norm, c0_norm, &mut roots);

        // Build fundamental matrices for each root
        let mut models = Vec::new();
        for root in roots.iter().take(n_roots) {
            let lambda = *root;
            // F = lambda * f1 + f2
            let mut f_vec = DVector::<f64>::zeros(9);
            for j in 0..9 {
                f_vec[j] = lambda * f1[j] + f2[j];
            }

            // Normalize
            let norm = f_vec.norm();
            if norm > 1e-10 {
                f_vec /= norm;
            }

            // Reshape into 3x3 matrix
            let mut f = Matrix3::<f64>::zeros();
            for r in 0..3 {
                for c in 0..3 {
                    f[(r, c)] = f_vec[r * 3 + c];
                }
            }

            models.push(FundamentalMatrix::new(f));
        }

        models
    }
}

impl Estimator for FundamentalEstimator {
    type Model = FundamentalMatrix;

    fn sample_size(&self) -> usize {
        8
    }

    fn is_valid_sample(&self, _data: &DataMatrix, sample: &[usize]) -> bool {
        if sample.len() < self.sample_size() {
            return false;
        }
        // Check for distinct indices
        for i in 0..sample.len() {
            for j in (i + 1)..sample.len() {
                if sample[i] == sample[j] {
                    return false;
                }
            }
        }
        true
    }

    fn estimate_model(&self, data: &DataMatrix, sample: &[usize]) -> Vec<Self::Model> {
        let n = sample.len();
        if n < 7 {
            return Vec::new();
        }

        // Use 7-point solver for exactly 7 points, 8-point for 8+ points
        if n == 7 {
            return self.estimate_seven_point(data, sample);
        }

        // 8-point solver for 8+ points
        // Normalize points
        let mut normalized = DMatrix::<f64>::zeros(0, 0);
        let mut t1 = Matrix3::<f64>::zeros();
        let mut t2 = Matrix3::<f64>::zeros();

        if !self.normalize_points(data, sample, &mut normalized, &mut t1, &mut t2) {
            return Vec::new();
        }

        // Build coefficient matrix A for Af = 0
        let mut a = DMatrix::<f64>::zeros(n, 9);
        for i in 0..n {
            let x1 = normalized[(i, 0)];
            let y1 = normalized[(i, 1)];
            let x2 = normalized[(i, 2)];
            let y2 = normalized[(i, 3)];

            a[(i, 0)] = x2 * x1;
            a[(i, 1)] = x2 * y1;
            a[(i, 2)] = x2;
            a[(i, 3)] = y2 * x1;
            a[(i, 4)] = y2 * y1;
            a[(i, 5)] = y2;
            a[(i, 6)] = x1;
            a[(i, 7)] = y1;
            a[(i, 8)] = 1.0;
        }

        // Solve via SVD: the solution is the right singular vector
        // corresponding to the smallest singular value of A^T A
        let ata = &a.transpose() * &a;
        let svd = SVD::new(ata, false, true);
        let vt = match svd.v_t {
            Some(vt) => vt,
            None => return Vec::new(),
        };
        let v = vt.transpose();

        // Last column corresponds to smallest singular value
        let last_col = v.column(v.ncols() - 1);

        // Check for NaN
        if last_col.iter().any(|&x| x.is_nan()) {
            return Vec::new();
        }

        // Reshape into 3x3 matrix
        let mut f_norm = Matrix3::<f64>::zeros();
        for r in 0..3 {
            for c in 0..3 {
                f_norm[(r, c)] = last_col[3 * r + c];
            }
        }

        // Denormalize: F = T2^T * F_norm * T1
        let f = t2.transpose() * f_norm * t1;

        vec![FundamentalMatrix::new(f)]
    }

    fn estimate_model_nonminimal(
        &self,
        data: &DataMatrix,
        sample: &[usize],
        weights: Option<&[f64]>,
    ) -> Vec<Self::Model> {
        let n = sample.len();
        if n < self.sample_size() {
            return Vec::new();
        }

        // Normalize points
        let mut normalized = DMatrix::<f64>::zeros(0, 0);
        let mut t1 = Matrix3::<f64>::zeros();
        let mut t2 = Matrix3::<f64>::zeros();

        if !self.normalize_points(data, sample, &mut normalized, &mut t1, &mut t2) {
            return Vec::new();
        }

        // Build coefficient matrix A for Af = 0 (with optional weights)
        let mut a = DMatrix::<f64>::zeros(n, 9);
        for (i, &idx) in sample.iter().enumerate() {
            let x1 = normalized[(i, 0)];
            let y1 = normalized[(i, 1)];
            let x2 = normalized[(i, 2)];
            let y2 = normalized[(i, 3)];

            let weight = weights.map(|w| w[idx]).unwrap_or(1.0);
            let w_x0 = weight * x1;
            let w_y0 = weight * y1;
            let w_x1 = weight * x2;
            let w_y1 = weight * y2;

            a[(i, 0)] = w_x1 * x1;
            a[(i, 1)] = w_x1 * y1;
            a[(i, 2)] = w_x1;
            a[(i, 3)] = w_y1 * x1;
            a[(i, 4)] = w_y1 * y1;
            a[(i, 5)] = w_y1;
            a[(i, 6)] = w_x0;
            a[(i, 7)] = w_y0;
            a[(i, 8)] = weight;
        }

        // Solve via SVD: A^T * A * f = 0
        let ata = &a.transpose() * &a;
        let svd = SVD::new(ata, false, true);
        let vt = match svd.v_t {
            Some(vt) => vt,
            None => return Vec::new(),
        };
        let v = vt.transpose();

        // Last column corresponds to smallest singular value
        let last_col = v.column(v.ncols() - 1);

        // Check for NaN
        if last_col.iter().any(|&x| x.is_nan()) {
            return Vec::new();
        }

        // Reshape into 3x3 matrix
        let mut f_norm = Matrix3::<f64>::zeros();
        for r in 0..3 {
            for c in 0..3 {
                f_norm[(r, c)] = last_col[3 * r + c];
            }
        }

        // Denormalize: F = T2^T * F_norm * T1
        let f = t2.transpose() * f_norm * t1;

        vec![FundamentalMatrix::new(f)]
    }

    fn is_valid_model(
        &self,
        model: &Self::Model,
        _data: &DataMatrix,
        _sample: &[usize],
        _threshold: f64,
    ) -> bool {
        // Basic sanity check: determinant should be small (rank-2 constraint)
        let det = model.f.determinant().abs();
        det < 1e-3 * _threshold.max(1.0)
    }
}