use inlier::*;
use nalgebra::DMatrix;
#[test]
fn test_estimate_homography_synthetic() {
let n_points = 20;
let mut points1 = DMatrix::<f64>::zeros(n_points, 2);
let mut points2 = DMatrix::<f64>::zeros(n_points, 2);
for i in 0..n_points {
let x = (i as f64) * 10.0;
let y = (i as f64) * 5.0;
points1[(i, 0)] = x;
points1[(i, 1)] = y;
points2[(i, 0)] = x + 10.0; points2[(i, 1)] = y + 5.0; }
let threshold = 1.0;
let result = estimate_homography(&points1, &points2, threshold, None);
if let Ok(result) = result {
assert!(
!result.inliers.is_empty(),
"Should find some inliers if estimation succeeds"
);
}
}
#[test]
fn test_estimate_fundamental_matrix_synthetic() {
let n_points = 15;
let mut points1 = DMatrix::<f64>::zeros(n_points, 2);
let mut points2 = DMatrix::<f64>::zeros(n_points, 2);
for i in 0..n_points {
let angle = (i as f64) * 2.0 * std::f64::consts::PI / (n_points as f64);
let radius = 100.0;
points1[(i, 0)] = radius * angle.cos() + 200.0;
points1[(i, 1)] = radius * angle.sin() + 200.0;
points2[(i, 0)] = radius * (angle + 0.1).cos() + 210.0;
points2[(i, 1)] = radius * (angle + 0.1).sin() + 210.0;
}
let threshold = 2.0;
let result = estimate_fundamental_matrix(&points1, &points2, threshold, None);
assert!(
result.is_ok(),
"Fundamental matrix estimation should succeed"
);
let result = result.unwrap();
assert!(!result.inliers.is_empty(), "Should find some inliers");
}
#[test]
fn test_estimate_essential_matrix_synthetic() {
let n_points = 15;
let mut points1 = DMatrix::<f64>::zeros(n_points, 2);
let mut points2 = DMatrix::<f64>::zeros(n_points, 2);
for i in 0..n_points {
let angle = (i as f64) * 2.0 * std::f64::consts::PI / (n_points as f64);
let radius = 50.0;
points1[(i, 0)] = radius * angle.cos();
points1[(i, 1)] = radius * angle.sin();
points2[(i, 0)] = radius * (angle + 0.1).cos();
points2[(i, 1)] = radius * (angle + 0.1).sin();
}
let threshold = 1.0;
let result = estimate_essential_matrix(&points1, &points2, threshold, None);
if let Ok(result) = result {
assert!(
!result.inliers.is_empty(),
"Should find some inliers if estimation succeeds"
);
}
}
#[test]
fn test_estimate_absolute_pose_synthetic() {
let n_points = 10;
let mut points_3d = DMatrix::<f64>::zeros(n_points, 3);
let mut points_2d = DMatrix::<f64>::zeros(n_points, 2);
for i in 0..n_points {
let x = (i as f64) * 10.0 - 50.0;
let y = (i as f64) * 5.0 - 25.0;
points_3d[(i, 0)] = x;
points_3d[(i, 1)] = y;
points_3d[(i, 2)] = 5.0;
points_2d[(i, 0)] = x / 5.0; points_2d[(i, 1)] = y / 5.0; }
let threshold = 1.0;
let result = estimate_absolute_pose(&points_3d, &points_2d, threshold, None);
assert!(result.is_ok(), "Absolute pose estimation should succeed");
let result = result.unwrap();
assert!(!result.inliers.is_empty(), "Should find some inliers");
}
#[test]
fn test_estimate_rigid_transform_synthetic() {
let n_points = 10;
let mut points1 = DMatrix::<f64>::zeros(n_points, 3);
let mut points2 = DMatrix::<f64>::zeros(n_points, 3);
for i in 0..n_points {
let x = (i as f64) * 10.0;
let y = (i as f64) * 5.0;
let z = (i as f64) * 2.0;
points1[(i, 0)] = x;
points1[(i, 1)] = y;
points1[(i, 2)] = z;
points2[(i, 0)] = x + 10.0;
points2[(i, 1)] = y + 5.0;
points2[(i, 2)] = z + 2.0;
}
let threshold = 0.1;
let result = estimate_rigid_transform(&points1, &points2, threshold, None);
assert!(result.is_ok(), "Rigid transform estimation should succeed");
let result = result.unwrap();
assert!(!result.inliers.is_empty(), "Should find some inliers");
assert!(
result.inliers.len() >= n_points / 2,
"Should find at least half the points as inliers"
);
}
#[test]
fn test_estimate_homography_with_outliers() {
let n_points = 20;
let mut points1 = DMatrix::<f64>::zeros(n_points, 2);
let mut points2 = DMatrix::<f64>::zeros(n_points, 2);
for i in 0..15 {
let x = (i as f64) * 10.0;
let y = (i as f64) * 5.0;
points1[(i, 0)] = x;
points1[(i, 1)] = y;
points2[(i, 0)] = x + 10.0;
points2[(i, 1)] = y + 5.0;
}
for i in 15..n_points {
points1[(i, 0)] = (i as f64) * 100.0;
points1[(i, 1)] = (i as f64) * 200.0;
points2[(i, 0)] = (i as f64) * 300.0;
points2[(i, 1)] = (i as f64) * 400.0;
}
let threshold = 1.0;
let result = estimate_homography(&points1, &points2, threshold, None);
match result {
Ok(result) => {
assert!(
!result.inliers.is_empty(),
"Result should be valid with at least one inlier"
);
}
Err(_) => {
}
}
}
#[test]
fn test_estimate_fundamental_matrix_insufficient_points() {
let n_points = 5;
let mut points1 = DMatrix::<f64>::zeros(n_points, 2);
let mut points2 = DMatrix::<f64>::zeros(n_points, 2);
for i in 0..n_points {
points1[(i, 0)] = (i as f64) * 10.0;
points1[(i, 1)] = (i as f64) * 5.0;
points2[(i, 0)] = (i as f64) * 10.0 + 1.0;
points2[(i, 1)] = (i as f64) * 5.0 + 1.0;
}
let threshold = 1.0;
let result = estimate_fundamental_matrix(&points1, &points2, threshold, None);
if let Ok(result) = result {
assert!(
!result.inliers.is_empty() || result.inliers.is_empty(),
"Result should be valid"
);
}
}