use std::collections::HashSet;
use nalgebra::{Matrix3, Vector3};
use visloc_core::types::Camera;
use super::fundamental::{fundamental_ransac, FundamentalRansacConfig};
use super::homography::{homography_ransac, pose_from_homography_matrix, HomographyRansacConfig};
use super::{
EssentialRansac, EssentialRansacConfig, FivePointEssentialMatrixEstimator,
TwoViewCorrespondence,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ConfigurationType {
Undefined,
Degenerate,
Uncalibrated,
Calibrated,
Planar,
Panoramic,
PlanarOrPanoramic,
Watermark,
Multiple,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TwoViewGeometryOptions {
pub min_num_inliers: usize,
pub min_inlier_ratio: f64,
pub min_e_f_inlier_ratio: f64,
pub max_h_inlier_ratio: f64,
pub watermark_min_inlier_ratio: f64,
pub watermark_border_size: f64,
pub detect_watermark: bool,
pub watermark_detection_max_error_px: f64,
pub max_error_px: f64,
pub essential_sampson_threshold: f64,
pub ransac_iterations: usize,
pub seed: u64,
pub multiple_models: bool,
pub calibrated_prefer_essential: bool,
}
impl Default for TwoViewGeometryOptions {
fn default() -> Self {
Self {
min_num_inliers: 15,
min_inlier_ratio: 0.0,
min_e_f_inlier_ratio: 0.95,
max_h_inlier_ratio: 0.8,
watermark_min_inlier_ratio: 0.7,
watermark_border_size: 0.1,
detect_watermark: true,
watermark_detection_max_error_px: 4.0,
max_error_px: 4.0,
essential_sampson_threshold: 5.0e-3,
ransac_iterations: 256,
seed: 7,
multiple_models: false,
calibrated_prefer_essential: false,
}
}
}
impl TwoViewGeometryOptions {
pub fn for_camera(camera: &Camera, max_error_px: f64) -> Self {
let mut options = Self {
max_error_px,
watermark_detection_max_error_px: max_error_px,
..Self::default()
};
if let Some((fx, fy, _, _)) = camera.intrinsics() {
let focal = 0.5 * (fx + fy);
if focal > 0.0 {
options.essential_sampson_threshold = max_error_px / focal;
}
}
options
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct TwoViewGeometryReport {
pub config: ConfigurationType,
pub inliers: Vec<usize>,
pub essential: Option<Matrix3<f64>>,
pub fundamental: Option<Matrix3<f64>>,
pub homography: Option<Matrix3<f64>>,
pub relative_pose: Option<(Matrix3<f64>, Vector3<f64>)>,
pub essential_inliers: Vec<usize>,
pub e_inlier_count: usize,
pub f_inlier_count: usize,
pub h_inlier_count: usize,
}
const fn degenerate_report() -> TwoViewGeometryReport {
TwoViewGeometryReport {
config: ConfigurationType::Degenerate,
inliers: Vec::new(),
essential: None,
fundamental: None,
homography: None,
relative_pose: None,
essential_inliers: Vec::new(),
e_inlier_count: 0,
f_inlier_count: 0,
h_inlier_count: 0,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct TwoViewGeometryVerifier {
pub options: TwoViewGeometryOptions,
}
impl TwoViewGeometryVerifier {
pub const fn new(options: TwoViewGeometryOptions) -> Self {
Self { options }
}
pub fn classify(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> TwoViewGeometryReport {
if self.options.multiple_models {
self.classify_multiple(correspondences, camera)
} else {
self.classify_single(correspondences, camera)
}
}
fn classify_single(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> TwoViewGeometryReport {
let opts = &self.options;
if correspondences.len() < opts.min_num_inliers {
return degenerate_report();
}
let essential_ransac = EssentialRansac {
estimator: FivePointEssentialMatrixEstimator::default(),
config: EssentialRansacConfig {
iterations: opts.ransac_iterations,
sampson_threshold: opts.essential_sampson_threshold,
seed: opts.seed,
},
};
let e_report = essential_ransac.estimate(correspondences, camera);
let f_report = fundamental_ransac(
correspondences,
&FundamentalRansacConfig {
iterations: opts.ransac_iterations,
max_error_px: opts.max_error_px,
seed: opts.seed,
},
);
let h_report = homography_ransac(
correspondences,
&HomographyRansacConfig {
iterations: opts.ransac_iterations,
max_error_px: opts.max_error_px,
seed: opts.seed,
},
);
let e_inliers = e_report.as_ref().map_or(0, |r| r.inliers.len());
let f_inliers = f_report.as_ref().map_or(0, |r| r.inliers.len());
let h_inliers = h_report.as_ref().map_or(0, |r| r.inliers.len());
if (e_report.is_none() && f_report.is_none() && h_report.is_none())
|| (e_inliers < opts.min_num_inliers
&& f_inliers < opts.min_num_inliers
&& h_inliers < opts.min_num_inliers)
{
return degenerate_report();
}
let ratio = |num: usize, den: usize| -> f64 {
if den == 0 {
0.0
} else {
num as f64 / den as f64
}
};
let e_f_inlier_ratio = ratio(e_inliers, f_inliers);
let h_f_inlier_ratio = ratio(h_inliers, f_inliers);
let h_e_inlier_ratio = ratio(h_inliers, e_inliers);
let e_inlier_indices = e_report.as_ref().map(|r| r.inliers.clone());
let f_inlier_indices = f_report.as_ref().map(|r| r.inliers.clone());
let h_inlier_indices = h_report.as_ref().map(|r| r.inliers.clone());
let mut config;
let mut chosen_inliers: Vec<usize>;
if let Some(e) = e_inlier_indices.as_ref().filter(|_| {
e_f_inlier_ratio > opts.min_e_f_inlier_ratio && e_inliers >= opts.min_num_inliers
}) {
let mut num_inliers = e_inliers;
chosen_inliers = e.clone();
if !opts.calibrated_prefer_essential && f_inliers > e_inliers {
if let Some(f) = &f_inlier_indices {
num_inliers = f_inliers;
chosen_inliers = f.clone();
}
}
if h_e_inlier_ratio > opts.max_h_inlier_ratio {
config = ConfigurationType::PlanarOrPanoramic;
if h_inliers > num_inliers {
if let Some(h) = &h_inlier_indices {
chosen_inliers = h.clone();
}
}
} else {
config = ConfigurationType::Calibrated;
}
} else if f_inliers >= opts.min_num_inliers {
let Some(f) = &f_inlier_indices else {
return degenerate_report();
};
let num_inliers = f_inliers;
chosen_inliers = f.clone();
if h_f_inlier_ratio > opts.max_h_inlier_ratio {
config = ConfigurationType::PlanarOrPanoramic;
if h_inliers > num_inliers {
if let Some(h) = &h_inlier_indices {
chosen_inliers = h.clone();
}
}
} else {
config = ConfigurationType::Uncalibrated;
}
} else if h_inliers >= opts.min_num_inliers {
let Some(h) = &h_inlier_indices else {
return degenerate_report();
};
chosen_inliers = h.clone();
config = ConfigurationType::PlanarOrPanoramic;
} else {
return degenerate_report();
}
if opts.min_inlier_ratio > 0.0 {
let inlier_ratio = chosen_inliers.len() as f64 / correspondences.len() as f64;
if inlier_ratio < opts.min_inlier_ratio {
return degenerate_report();
}
}
if opts.detect_watermark && detect_watermark(camera, correspondences, &chosen_inliers, opts)
{
config = ConfigurationType::Watermark;
}
let essential_inliers = e_inlier_indices.unwrap_or_default();
let mut report = TwoViewGeometryReport {
config,
inliers: chosen_inliers,
essential: e_report.map(|r| r.essential),
fundamental: f_report.map(|r| r.fundamental),
homography: h_report.map(|r| r.homography),
relative_pose: None,
essential_inliers,
e_inlier_count: e_inliers,
f_inlier_count: f_inliers,
h_inlier_count: h_inliers,
};
if report.config == ConfigurationType::PlanarOrPanoramic {
self.resolve_planar_or_panoramic(&mut report, correspondences, camera);
}
report
}
fn resolve_planar_or_panoramic(
&self,
report: &mut TwoViewGeometryReport,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) {
let Some(h) = report.homography else {
return;
};
let Some((fx, fy, cx, cy)) = camera.intrinsics() else {
return;
};
let k = Matrix3::new(fx, 0.0, cx, 0.0, fy, cy, 0.0, 0.0, 1.0);
let inlier_correspondences: Vec<TwoViewCorrespondence> =
report.inliers.iter().map(|&i| correspondences[i]).collect();
let Some((rotation, translation, _normal)) =
pose_from_homography_matrix(&h, &k, &k, &inlier_correspondences, camera)
else {
return;
};
report.config = if translation.norm_squared() < 1e-12 {
ConfigurationType::Panoramic
} else {
ConfigurationType::Planar
};
report.relative_pose = Some((rotation, translation));
}
fn classify_multiple(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> TwoViewGeometryReport {
let mut remaining: Vec<usize> = (0..correspondences.len()).collect();
let mut geometries: Vec<TwoViewGeometryReport> = Vec::new();
let mut single_options = self.options;
single_options.multiple_models = false;
let single_verifier = TwoViewGeometryVerifier::new(single_options);
loop {
let remaining_correspondences: Vec<TwoViewCorrespondence> =
remaining.iter().map(|&i| correspondences[i]).collect();
let sub = single_verifier.classify_single(&remaining_correspondences, camera);
if sub.config == ConfigurationType::Degenerate {
break;
}
let global_inliers: Vec<usize> = sub.inliers.iter().map(|&i| remaining[i]).collect();
let inlier_set: HashSet<usize> = global_inliers.iter().copied().collect();
remaining.retain(|i| !inlier_set.contains(i));
if sub.config != ConfigurationType::Watermark {
let mut sub_global = sub;
sub_global.inliers = global_inliers;
geometries.push(sub_global);
}
if remaining.len() < self.options.min_num_inliers {
break;
}
}
if geometries.is_empty() {
degenerate_report()
} else if geometries.len() == 1 {
geometries.into_iter().next().unwrap()
} else {
let best_idx = geometries
.iter()
.enumerate()
.max_by_key(|(_, g)| {
let calibrated_bonus = if matches!(g.config, ConfigurationType::Calibrated) {
1_000_000usize
} else {
0
};
calibrated_bonus + g.inliers.len()
})
.map(|(i, _)| i)
.expect("geometries non-empty");
let mut best = geometries.swap_remove(best_idx);
best.config = ConfigurationType::Multiple;
best
}
}
}
fn detect_watermark(
camera: &Camera,
correspondences: &[TwoViewCorrespondence],
inliers: &[usize],
opts: &TwoViewGeometryOptions,
) -> bool {
if inliers.is_empty() {
return false;
}
let diagonal = ((camera.width as f64).powi(2) + (camera.height as f64).powi(2)).sqrt();
let border = opts.watermark_border_size * diagonal;
let in_interior = |p: &nalgebra::Point2<f64>| {
p.x >= border
&& p.x <= camera.width as f64 - border
&& p.y >= border
&& p.y <= camera.height as f64 - border
};
let mut inlier_points1 = Vec::with_capacity(inliers.len());
let mut inlier_points2 = Vec::with_capacity(inliers.len());
let mut num_in_border = 0usize;
for &i in inliers {
let c = &correspondences[i];
inlier_points1.push(c.previous_xy);
inlier_points2.push(c.current_xy);
if !in_interior(&c.previous_xy) && !in_interior(&c.current_xy) {
num_in_border += 1;
}
}
let border_ratio = num_in_border as f64 / inliers.len() as f64;
if border_ratio < opts.watermark_min_inlier_ratio {
return false;
}
let n = inlier_points1.len();
let threshold_sq =
opts.watermark_detection_max_error_px * opts.watermark_detection_max_error_px;
let mut best_count = 0usize;
for i in 0..n {
let t = inlier_points2[i] - inlier_points1[i];
let count = (0..n)
.filter(|&j| {
let d = inlier_points2[j] - (inlier_points1[j] + t);
d.norm_squared() <= threshold_sq
})
.count();
if count > best_count {
best_count = count;
}
}
let translation_inlier_ratio = best_count as f64 / n as f64;
translation_inlier_ratio >= opts.watermark_min_inlier_ratio
}
pub fn two_view_triangulation_angle(
report: &TwoViewGeometryReport,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> Option<f64> {
two_view_pose_and_triangulation_angle(report, correspondences, camera)
.map(|(_, _, angle)| angle)
}
pub fn two_view_pose_and_triangulation_angle(
report: &TwoViewGeometryReport,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> Option<(Matrix3<f64>, Vector3<f64>, f64)> {
match report.config {
ConfigurationType::Calibrated => {
let essential = report.essential?;
essential_pose_and_angle(&essential, report, correspondences, camera)
}
ConfigurationType::Uncalibrated => {
let fundamental = report.fundamental?;
let (fx, fy, cx, cy) = camera.intrinsics()?;
let k = Matrix3::new(fx, 0.0, cx, 0.0, fy, cy, 0.0, 0.0, 1.0);
let essential = k.transpose() * fundamental * k;
essential_pose_and_angle(&essential, report, correspondences, camera)
}
ConfigurationType::Planar
| ConfigurationType::Panoramic
| ConfigurationType::PlanarOrPanoramic => {
planar_pose_and_angle(report, correspondences, camera)
}
_ => None,
}
}
fn median(values: &mut [f64]) -> Option<f64> {
if values.is_empty() {
return None;
}
values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
Some(values[values.len() / 2])
}
fn essential_pose_and_angle(
essential: &Matrix3<f64>,
report: &TwoViewGeometryReport,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> Option<(Matrix3<f64>, Vector3<f64>, f64)> {
let mut rays1 = Vec::with_capacity(report.inliers.len());
let mut rays2 = Vec::with_capacity(report.inliers.len());
for &index in &report.inliers {
rays1.push(camera.unit_ray_from_pixel(&correspondences[index].previous_xy)?);
rays2.push(camera.unit_ray_from_pixel(&correspondences[index].current_xy)?);
}
let (rotation, translation, valid) = pose_from_essential_matrix(essential, &rays1, &rays2);
if valid.is_empty() {
return None;
}
let cam1_from_cam2_rotation = rotation.transpose();
let mut angles = Vec::with_capacity(valid.len());
for &index in &valid {
let ray1 = rays1[index];
let ray2 = cam1_from_cam2_rotation * rays2[index];
let denominator = ray1.norm() * ray2.norm();
if denominator < 1e-18 {
continue;
}
let angle = (ray1.dot(&ray2) / denominator).clamp(-1.0, 1.0).acos();
angles.push(angle.min(std::f64::consts::PI - angle));
}
Some((rotation, translation, median(&mut angles)?))
}
pub(crate) fn pose_from_essential_matrix(
essential: &Matrix3<f64>,
rays1: &[Vector3<f64>],
rays2: &[Vector3<f64>],
) -> (Matrix3<f64>, Vector3<f64>, Vec<usize>) {
let Some(svd) = essential.try_svd(true, true, f64::EPSILON * 5.0, 10_000) else {
return (Matrix3::identity(), Vector3::zeros(), Vec::new());
};
let (Some(u), Some(v_t)) = (svd.u, svd.v_t) else {
return (Matrix3::identity(), Vector3::zeros(), Vec::new());
};
let w = Matrix3::new(0.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0);
let mut r1 = u * w * v_t;
let mut r2 = u * w.transpose() * v_t;
if r1.determinant() < 0.0 {
r1 = -r1;
}
if r2.determinant() < 0.0 {
r2 = -r2;
}
let t = u.column(2).into_owned();
let candidates = [(r1, t), (r2, t), (r1, -t), (r2, -t)];
let mut best = (r1, t, Vec::new());
for (rotation, translation) in candidates {
let valid: Vec<usize> = (0..rays1.len())
.filter(|&index| {
let ray1_in_cam2 = rotation * rays1[index];
let a = -ray1_in_cam2.dot(&rays2[index]);
let b1 = -ray1_in_cam2.dot(&translation);
let b2 = rays2[index].dot(&translation);
b1 - a * b2 > 0.0 && b2 - a * b1 > 0.0
})
.collect();
if valid.len() >= best.2.len() {
best = (rotation, translation, valid);
}
}
best
}
fn planar_pose_and_angle(
report: &TwoViewGeometryReport,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> Option<(Matrix3<f64>, Vector3<f64>, f64)> {
if report.config == ConfigurationType::Panoramic {
let (rotation, translation) = report.relative_pose?;
return Some((rotation, translation, 0.0));
}
let homography = report.homography?;
let (fx, fy, cx, cy) = camera.intrinsics()?;
let k = Matrix3::new(fx, 0.0, cx, 0.0, fy, cy, 0.0, 0.0, 1.0);
let (rotation, translation, _normal) =
pose_from_homography_matrix(&homography, &k, &k, correspondences, camera)?;
let cam1_center = nalgebra::Point3::origin();
let cam2_center = nalgebra::Point3::from(-(rotation.transpose() * translation));
let mut angles = Vec::new();
for &index in &report.inliers {
let correspondence = &correspondences[index];
let previous = camera.normalize_pixel(&correspondence.previous_xy)?;
let current = camera.normalize_pixel(&correspondence.current_xy)?;
let Some(point3d) = triangulate_rays(&rotation, &translation, previous, current) else {
continue;
};
let ray1 = point3d - cam1_center;
let ray2 = point3d - cam2_center;
let denominator = ray1.norm() * ray2.norm();
if denominator < 1e-18 {
continue;
}
angles.push((ray1.dot(&ray2) / denominator).clamp(-1.0, 1.0).acos());
}
Some((rotation, translation, median(&mut angles).unwrap_or(0.0)))
}
fn triangulate_rays(
rotation: &Matrix3<f64>,
translation: &Vector3<f64>,
previous: nalgebra::Point2<f64>,
current: nalgebra::Point2<f64>,
) -> Option<nalgebra::Point3<f64>> {
let p1 = Matrix3::new(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
let mut a = nalgebra::DMatrix::<f64>::zeros(4, 4);
for column in 0..3 {
a[(0, column)] = previous.x * p1[(2, column)] - p1[(0, column)];
a[(1, column)] = previous.y * p1[(2, column)] - p1[(1, column)];
}
for column in 0..3 {
a[(2, column)] = current.x * rotation[(2, column)] - rotation[(0, column)];
a[(3, column)] = current.y * rotation[(2, column)] - rotation[(1, column)];
}
a[(2, 3)] = current.x * translation.z - translation.x;
a[(3, 3)] = current.y * translation.z - translation.y;
let svd = a.try_svd(true, true, f64::EPSILON * 5.0, 10_000)?;
let v_t = svd.v_t?;
let solution = v_t.row(v_t.nrows() - 1);
let w = solution[3];
if w.abs() < 1e-12 {
return None;
}
Some(nalgebra::Point3::new(
solution[0] / w,
solution[1] / w,
solution[2] / w,
))
}
#[cfg(test)]
mod tests {
use super::*;
use nalgebra::{Point2, Point3, UnitQuaternion, Vector3};
use visloc_core::geometry::{Pose, SE3};
fn synthetic_camera() -> Camera {
Camera::pinhole(1, 640, 480, 500.0, 500.0, 320.0, 240.0)
}
fn project(pose: &Pose, camera: &Camera, point: &Point3<f64>) -> Option<Point2<f64>> {
camera.project(&pose.transform_world_point(point))
}
fn correspondences_for(
previous: &Pose,
current: &Pose,
camera: &Camera,
points: &[Point3<f64>],
) -> Vec<TwoViewCorrespondence> {
points
.iter()
.filter_map(|point| {
let p1 = project(previous, camera, point)?;
let p2 = project(current, camera, point)?;
Some(TwoViewCorrespondence::new(p1, p2))
})
.collect()
}
fn general_scene_points() -> Vec<Point3<f64>> {
let mut points = Vec::new();
for i in 0..6 {
for j in 0..4 {
let x = -1.5 + 0.6 * i as f64;
let y = -1.0 + 0.7 * j as f64;
let z = 3.0 + 0.8 * ((i + j) % 5) as f64;
points.push(Point3::new(x, y, z));
}
}
points
}
fn planar_scene_points() -> Vec<Point3<f64>> {
let mut points = Vec::new();
for i in 0..6 {
for j in 0..4 {
let x = -1.5 + 0.6 * i as f64;
let y = -1.0 + 0.7 * j as f64;
points.push(Point3::new(x, y, 5.0));
}
}
points
}
#[test]
fn general_scene_classifies_calibrated_or_uncalibrated() {
let camera = synthetic_camera();
let previous =
Pose::from_world_to_camera(UnitQuaternion::identity(), Vector3::new(0.0, 0.0, 0.0));
let yaw = UnitQuaternion::from_axis_angle(&Vector3::y_axis(), 0.08);
let current = Pose {
world_to_camera: SE3::new(yaw, Vector3::new(-0.6, 0.05, -0.1)),
};
let correspondences =
correspondences_for(&previous, ¤t, &camera, &general_scene_points());
assert!(correspondences.len() >= 20);
let verifier =
TwoViewGeometryVerifier::new(TwoViewGeometryOptions::for_camera(&camera, 4.0));
let report = verifier.classify(&correspondences, &camera);
assert_eq!(
report.config,
ConfigurationType::Calibrated,
"general 3D parallax with known intrinsics should classify CALIBRATED when E/F agree"
);
assert!(!report.inliers.is_empty());
assert!(
!report.essential_inliers.is_empty() && report.e_inlier_count > 0,
"essential RANSAC must expose its inlier set for prefer-essential-inliers"
);
}
#[test]
fn planar_scene_with_baseline_classifies_planar() {
let camera = synthetic_camera();
let previous =
Pose::from_world_to_camera(UnitQuaternion::identity(), Vector3::new(0.0, 0.0, 0.0));
let yaw = UnitQuaternion::from_axis_angle(&Vector3::y_axis(), 0.06);
let current = Pose {
world_to_camera: SE3::new(yaw, Vector3::new(-0.5, 0.0, 0.0)),
};
let correspondences =
correspondences_for(&previous, ¤t, &camera, &planar_scene_points());
assert!(correspondences.len() >= 20);
let verifier =
TwoViewGeometryVerifier::new(TwoViewGeometryOptions::for_camera(&camera, 4.0));
let report = verifier.classify(&correspondences, &camera);
assert_eq!(report.config, ConfigurationType::Planar);
let (_, translation) = report.relative_pose.expect("planar pose must resolve");
assert!(translation.norm() > 1.0e-6);
}
#[test]
fn pure_rotation_classifies_panoramic() {
let camera = synthetic_camera();
let previous =
Pose::from_world_to_camera(UnitQuaternion::identity(), Vector3::new(0.0, 0.0, 0.0));
let yaw = UnitQuaternion::from_axis_angle(&Vector3::y_axis(), 0.12);
let current = Pose::from_world_to_camera(yaw, Vector3::new(0.0, 0.0, 0.0));
let correspondences =
correspondences_for(&previous, ¤t, &camera, &general_scene_points());
assert!(correspondences.len() >= 20);
let verifier =
TwoViewGeometryVerifier::new(TwoViewGeometryOptions::for_camera(&camera, 4.0));
let report = verifier.classify(&correspondences, &camera);
assert_eq!(report.config, ConfigurationType::Panoramic);
let (_, translation) = report.relative_pose.expect("panoramic pose must resolve");
assert!(translation.norm_squared() < 1.0e-9);
}
#[test]
fn too_few_correspondences_is_degenerate() {
let camera = synthetic_camera();
let previous =
Pose::from_world_to_camera(UnitQuaternion::identity(), Vector3::new(0.0, 0.0, 0.0));
let current =
Pose::from_world_to_camera(UnitQuaternion::identity(), Vector3::new(-0.3, 0.0, 0.0));
let mut points = general_scene_points();
points.truncate(10); let correspondences = correspondences_for(&previous, ¤t, &camera, &points);
let verifier = TwoViewGeometryVerifier::default();
let report = verifier.classify(&correspondences, &camera);
assert_eq!(report.config, ConfigurationType::Degenerate);
assert!(report.inliers.is_empty());
}
#[test]
fn border_translation_detected_as_watermark() {
let camera = synthetic_camera();
let opts = TwoViewGeometryOptions::default();
let translation = Vector3::new(6.0, -4.0, 0.0);
let mut correspondences = Vec::new();
for i in 0..20 {
let x = 5.0 + (i as f64 * 3.0) % 60.0;
let y = 5.0 + (i as f64 * 7.0) % 60.0;
let p1 = Point2::new(x, y);
let p2 = Point2::new(x + translation.x, y + translation.y);
correspondences.push(TwoViewCorrespondence::new(p1, p2));
}
let all_inliers: Vec<usize> = (0..correspondences.len()).collect();
assert!(detect_watermark(
&camera,
&correspondences,
&all_inliers,
&opts
));
}
#[test]
fn interior_matches_are_not_flagged_as_watermark() {
let camera = synthetic_camera();
let opts = TwoViewGeometryOptions::default();
let translation = Vector3::new(6.0, -4.0, 0.0);
let mut correspondences = Vec::new();
for i in 0..20 {
let x = 300.0 + (i as f64 * 3.0) % 40.0;
let y = 220.0 + (i as f64 * 5.0) % 40.0;
let p1 = Point2::new(x, y);
let p2 = Point2::new(x + translation.x, y + translation.y);
correspondences.push(TwoViewCorrespondence::new(p1, p2));
}
let all_inliers: Vec<usize> = (0..correspondences.len()).collect();
assert!(!detect_watermark(
&camera,
&correspondences,
&all_inliers,
&opts
));
}
#[test]
fn legacy_essential_only_path_is_unaffected_by_new_module() {
use super::super::RelativePoseEstimator;
let camera = synthetic_camera();
let previous =
Pose::from_world_to_camera(UnitQuaternion::identity(), Vector3::new(0.0, 0.0, 0.0));
let current =
Pose::from_world_to_camera(UnitQuaternion::identity(), Vector3::new(-0.3, 0.0, 0.0));
let correspondences =
correspondences_for(&previous, ¤t, &camera, &general_scene_points());
let legacy = RelativePoseEstimator::default();
let legacy_pose = legacy
.estimate(&correspondences, &camera)
.expect("legacy essential-only path must still recover a pose");
assert!(legacy_pose.inliers.len() >= 8);
let verifier =
TwoViewGeometryVerifier::new(TwoViewGeometryOptions::for_camera(&camera, 4.0));
let _ = verifier.classify(&correspondences, &camera);
let legacy_pose_again = legacy
.estimate(&correspondences, &camera)
.expect("legacy essential-only path must still recover a pose");
assert_eq!(legacy_pose.inliers, legacy_pose_again.inliers);
}
}