use nalgebra::{DMatrix, Matrix3, Matrix3x4, Point2, UnitQuaternion, Vector3};
use rand::rngs::SmallRng;
use rand::seq::SliceRandom;
use rand::SeedableRng;
use visloc_core::geometry::SE3;
use visloc_core::types::Camera;
pub mod colmap_verification;
pub mod correspondence_graph;
pub mod five_point;
pub mod fundamental;
pub mod homography;
pub mod rescue;
pub mod spherical;
pub use colmap_verification::{
two_view_pose_and_triangulation_angle, two_view_triangulation_angle, ConfigurationType,
TwoViewGeometryOptions, TwoViewGeometryReport, TwoViewGeometryVerifier,
};
pub use correspondence_graph::{
Correspondence, CorrespondenceGraph, CorrespondenceGraphError, EdgeMetadata, IngestStats,
PairMatches,
};
pub use fundamental::{
estimate_fundamental_dlt, fundamental_ransac, fundamental_squared_sampson_error,
FundamentalRansacConfig, FundamentalReport,
};
pub use homography::{
decompose_homography_matrix, estimate_homography_dlt, homography_ransac,
homography_squared_error, pose_from_homography_matrix, HomographyMotion,
HomographyRansacConfig, HomographyReport,
};
pub use rescue::{connected_components, generate_bridge_candidates, BridgeCandidateOptions};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TwoViewCorrespondence {
pub previous_xy: Point2<f64>,
pub current_xy: Point2<f64>,
}
impl TwoViewCorrespondence {
pub const fn new(previous_xy: Point2<f64>, current_xy: Point2<f64>) -> Self {
Self {
previous_xy,
current_xy,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct EssentialRansacReport {
pub essential: Matrix3<f64>,
pub inliers: Vec<usize>,
pub mean_sampson_error: f64,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RelativePose {
pub previous_to_current: SE3,
pub translation_unit: Vector3<f64>,
pub translation_scale: f64,
pub inliers: Vec<usize>,
pub mean_sampson_error: f64,
pub alternate: Option<(UnitQuaternion<f64>, Vector3<f64>)>,
pub chirality_margin: f64,
}
pub trait EssentialMatrixEstimator {
fn estimate(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> Option<Matrix3<f64>>;
fn minimum_correspondences(&self) -> usize;
fn estimate_all(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> Vec<Matrix3<f64>> {
self.estimate(correspondences, camera).into_iter().collect()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EightPointEssentialMatrixEstimator {
pub min_correspondences: usize,
}
impl Default for EightPointEssentialMatrixEstimator {
fn default() -> Self {
Self {
min_correspondences: 8,
}
}
}
impl EssentialMatrixEstimator for EightPointEssentialMatrixEstimator {
fn minimum_correspondences(&self) -> usize {
self.min_correspondences
}
fn estimate(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> Option<Matrix3<f64>> {
if correspondences.len() < self.min_correspondences {
return None;
}
let normalized = normalize_pairs(correspondences, camera)?;
let (previous_normalization, previous_points) =
hartley_normalization(normalized.iter().map(|(p, _)| *p))?;
let (current_normalization, current_points) =
hartley_normalization(normalized.iter().map(|(_, c)| *c))?;
let mut a = DMatrix::<f64>::zeros(normalized.len(), 9);
for row in 0..normalized.len() {
let (x, y) = (previous_points[row].x, previous_points[row].y);
let (xp, yp) = (current_points[row].x, current_points[row].y);
a[(row, 0)] = xp * x;
a[(row, 1)] = xp * y;
a[(row, 2)] = xp;
a[(row, 3)] = yp * x;
a[(row, 4)] = yp * y;
a[(row, 5)] = yp;
a[(row, 6)] = x;
a[(row, 7)] = y;
a[(row, 8)] = 1.0;
}
let ata = a.transpose() * a;
let svd = ata.try_svd(true, true, f64::EPSILON * 5.0, 10_000)?;
let v_t = svd.v_t?;
let last = v_t.row(v_t.nrows() - 1);
let mut essential_normalized = Matrix3::new(
last[0], last[1], last[2], last[3], last[4], last[5], last[6], last[7], last[8],
);
let essential_norm_svd =
essential_normalized.try_svd(true, true, f64::EPSILON * 5.0, 10_000)?;
let u_n = essential_norm_svd.u?;
let v_t_n = essential_norm_svd.v_t?;
let s_n =
(essential_norm_svd.singular_values[0] + essential_norm_svd.singular_values[1]) * 0.5;
let constrained_n = Matrix3::from_diagonal(&Vector3::new(s_n, s_n, 0.0));
essential_normalized = u_n * constrained_n * v_t_n;
let essential_calibrated: Matrix3<f64> =
current_normalization.transpose() * essential_normalized * previous_normalization;
Some(essential_calibrated)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FivePointEssentialMatrixEstimator {
pub min_correspondences: usize,
}
impl Default for FivePointEssentialMatrixEstimator {
fn default() -> Self {
Self {
min_correspondences: 5,
}
}
}
impl EssentialMatrixEstimator for FivePointEssentialMatrixEstimator {
fn minimum_correspondences(&self) -> usize {
self.min_correspondences
}
fn estimate(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> Option<Matrix3<f64>> {
self.estimate_all(correspondences, camera)
.into_iter()
.next()
}
fn estimate_all(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> Vec<Matrix3<f64>> {
if correspondences.len() < self.min_correspondences {
return Vec::new();
}
let rays1: Option<Vec<Vector3<f64>>> = correspondences
.iter()
.map(|c| {
camera
.normalize_pixel(&c.previous_xy)
.map(|p| Vector3::new(p.x, p.y, 1.0))
})
.collect();
let rays2: Option<Vec<Vector3<f64>>> = correspondences
.iter()
.map(|c| {
camera
.normalize_pixel(&c.current_xy)
.map(|p| Vector3::new(p.x, p.y, 1.0))
})
.collect();
let (Some(rays1), Some(rays2)) = (rays1, rays2) else {
return Vec::new();
};
let models = five_point::relpose_5pt(&rays1, &rays2);
if correspondences.len() == self.min_correspondences {
models
.into_iter()
.filter(|essential| all_in_front_for_some_pose(essential, correspondences, camera))
.collect()
} else {
models
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EssentialRansacConfig {
pub iterations: usize,
pub sampson_threshold: f64,
pub seed: u64,
}
impl Default for EssentialRansacConfig {
fn default() -> Self {
Self {
iterations: 256,
sampson_threshold: 5.0e-3,
seed: 7,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EssentialRansac<E = EightPointEssentialMatrixEstimator> {
pub estimator: E,
pub config: EssentialRansacConfig,
}
impl Default for EssentialRansac {
fn default() -> Self {
Self {
estimator: EightPointEssentialMatrixEstimator::default(),
config: EssentialRansacConfig::default(),
}
}
}
impl<E> EssentialRansac<E>
where
E: EssentialMatrixEstimator,
{
pub fn estimate(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> Option<EssentialRansacReport> {
self.estimate_with_optional_weights(correspondences, camera, None)
}
pub fn estimate_with_weights(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
weights: &[f32],
) -> Option<EssentialRansacReport> {
if weights.len() != correspondences.len() {
return self.estimate(correspondences, camera);
}
self.estimate_with_optional_weights(correspondences, camera, Some(weights))
}
fn estimate_with_optional_weights(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
weights: Option<&[f32]>,
) -> Option<EssentialRansacReport> {
let sample_size = self.estimator.minimum_correspondences();
if correspondences.len() < sample_size {
return None;
}
let weighted = weights.filter(|w| w.iter().any(|&v| v.is_finite() && v > 0.0));
let mut sorted_indices: Vec<usize> = (0..correspondences.len()).collect();
if let Some(w) = weighted {
sorted_indices
.sort_by(|&a, &b| w[b].partial_cmp(&w[a]).unwrap_or(std::cmp::Ordering::Equal));
}
let mut rng = SmallRng::seed_from_u64(self.config.seed);
let mut best_inliers: Vec<usize> = Vec::new();
let mut best_essential: Option<Matrix3<f64>> = None;
let threshold_sq = self.config.sampson_threshold * self.config.sampson_threshold;
let n = correspondences.len();
let total_iters = self.config.iterations.max(1);
for iteration in 0..self.config.iterations {
let m_k = if weighted.is_some() {
let progress = iteration as f64 / total_iters as f64;
let m = sample_size as f64 + (n - sample_size) as f64 * progress;
(m.ceil() as usize).clamp(sample_size, n)
} else {
n
};
let mut subset: Vec<usize> = sorted_indices[..m_k].to_vec();
subset.shuffle(&mut rng);
let sample: Vec<TwoViewCorrespondence> = subset[..sample_size]
.iter()
.map(|&i| correspondences[i])
.collect();
for candidate in self.estimator.estimate_all(&sample, camera) {
let Some(inliers) = score_inliers_if_competitive(
&candidate,
correspondences,
camera,
threshold_sq,
best_inliers.len(),
) else {
continue;
};
if inliers.len() > best_inliers.len() {
best_inliers = inliers;
best_essential = Some(candidate);
}
}
}
let essential = best_essential?;
if best_inliers.len() < sample_size {
return None;
}
let inlier_correspondences: Vec<TwoViewCorrespondence> =
best_inliers.iter().map(|&i| correspondences[i]).collect();
let refined = self
.estimator
.estimate_all(&inlier_correspondences, camera)
.into_iter()
.max_by_key(|candidate| {
score_inliers(candidate, correspondences, camera, threshold_sq).len()
})
.unwrap_or(essential);
let final_inliers = score_inliers(&refined, correspondences, camera, threshold_sq);
let final_inliers = if final_inliers.len() >= best_inliers.len() {
final_inliers
} else {
best_inliers
};
let mean = mean_sampson_error(&refined, correspondences, camera, &final_inliers);
Some(EssentialRansacReport {
essential: refined,
inliers: final_inliers,
mean_sampson_error: mean,
})
}
}
fn score_inliers_if_competitive(
essential: &Matrix3<f64>,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
threshold_sq: f64,
incumbent_count: usize,
) -> Option<Vec<usize>> {
let mut inliers = Vec::new();
for (i, correspondence) in correspondences.iter().enumerate() {
if sampson_distance_squared(essential, correspondence, camera)
.is_some_and(|distance_sq| distance_sq <= threshold_sq)
{
inliers.push(i);
}
let remaining = correspondences.len() - i - 1;
if inliers.len() + remaining <= incumbent_count {
return None;
}
}
Some(inliers)
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CheiralityOptions {
pub min_tri_angle_deg: f64,
pub max_ambiguity_ratio: f64,
pub min_positive_depth_fraction: f64,
}
impl Default for CheiralityOptions {
fn default() -> Self {
Self {
min_tri_angle_deg: 0.0,
max_ambiguity_ratio: 1.0,
min_positive_depth_fraction: 0.0,
}
}
}
impl CheiralityOptions {
pub const fn hardened() -> Self {
Self {
min_tri_angle_deg: 1.0,
max_ambiguity_ratio: 0.85,
min_positive_depth_fraction: 0.5,
}
}
pub const fn hardened_keep_ambiguous() -> Self {
Self {
max_ambiguity_ratio: 1.0,
..Self::hardened()
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct RelativePoseRecovery {
pub rotation: UnitQuaternion<f64>,
pub translation_unit: Vector3<f64>,
pub best_score: i64,
pub second_score: i64,
pub alternate: Option<(UnitQuaternion<f64>, Vector3<f64>)>,
}
impl RelativePoseRecovery {
pub fn chirality_margin(&self) -> f64 {
if self.best_score <= 0 {
return 0.0;
}
(self.best_score - self.second_score) as f64 / self.best_score as f64
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RelativePoseEstimator<E = EightPointEssentialMatrixEstimator> {
pub ransac: EssentialRansac<E>,
pub default_translation_scale: f64,
pub cheirality: CheiralityOptions,
}
impl Default for RelativePoseEstimator {
fn default() -> Self {
Self {
ransac: EssentialRansac::default(),
default_translation_scale: 1.0,
cheirality: CheiralityOptions::default(),
}
}
}
impl<E> RelativePoseEstimator<E>
where
E: EssentialMatrixEstimator,
{
pub fn estimate(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> Option<RelativePose> {
self.estimate_with_scale(correspondences, camera, self.default_translation_scale)
}
pub fn estimate_with_scale(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
translation_scale: f64,
) -> Option<RelativePose> {
self.estimate_with_scale_and_optional_weights(
correspondences,
camera,
translation_scale,
None,
)
}
pub fn estimate_with_cameras(
&self,
correspondences: &[TwoViewCorrespondence],
previous_camera: &Camera,
current_camera: &Camera,
) -> Option<RelativePose> {
let normalized = normalize_correspondences_with_cameras(
correspondences,
previous_camera,
current_camera,
)?;
let unit_camera = Camera::pinhole(0, 1, 1, 1.0, 1.0, 0.0, 0.0);
self.estimate(&normalized, &unit_camera)
}
pub fn estimate_with_scale_and_weights(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
translation_scale: f64,
weights: &[f32],
) -> Option<RelativePose> {
self.estimate_with_scale_and_optional_weights(
correspondences,
camera,
translation_scale,
Some(weights),
)
}
fn estimate_with_scale_and_optional_weights(
&self,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
translation_scale: f64,
weights: Option<&[f32]>,
) -> Option<RelativePose> {
let report = match weights {
Some(w) if w.len() == correspondences.len() => {
self.ransac
.estimate_with_weights(correspondences, camera, w)
}
_ => self.ransac.estimate(correspondences, camera),
}?;
let recovered = recover_relative_pose_with_options(
&report.essential,
correspondences,
camera,
&report.inliers,
&self.cheirality,
)?;
let se3 = SE3::new(
recovered.rotation,
recovered.translation_unit * translation_scale,
);
Some(RelativePose {
previous_to_current: se3,
translation_unit: recovered.translation_unit,
translation_scale,
inliers: report.inliers,
mean_sampson_error: report.mean_sampson_error,
alternate: recovered.alternate,
chirality_margin: recovered.chirality_margin(),
})
}
}
pub fn recover_relative_pose(
essential: &Matrix3<f64>,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
inliers: &[usize],
) -> Option<(UnitQuaternion<f64>, Vector3<f64>)> {
let recovered = recover_relative_pose_with_options(
essential,
correspondences,
camera,
inliers,
&CheiralityOptions::default(),
)?;
Some((recovered.rotation, recovered.translation_unit))
}
pub fn recover_relative_pose_with_options(
essential: &Matrix3<f64>,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
inliers: &[usize],
options: &CheiralityOptions,
) -> Option<RelativePoseRecovery> {
let svd = essential.try_svd(true, true, f64::EPSILON * 5.0, 10_000)?;
let u = svd.u?;
let v_t = svd.v_t?;
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_unit = u.column(2).into_owned();
let candidates = [(r1, t_unit), (r1, -t_unit), (r2, t_unit), (r2, -t_unit)];
let mut ranked: Vec<(i64, Matrix3<f64>, Vector3<f64>)> = Vec::with_capacity(4);
let min_angle_rad = options.min_tri_angle_deg.to_radians();
for (rotation, translation) in candidates {
let score = cheirality_score(
&rotation,
&translation,
correspondences,
camera,
inliers,
min_angle_rad,
);
if score > 0 {
ranked.push((score, rotation, translation));
}
}
ranked.sort_by(|a, b| b.0.cmp(&a.0));
let (best_score, rotation, translation) = ranked.first().copied()?;
let second_score = ranked.get(1).map_or(0, |(s, _, _)| *s);
if options.min_positive_depth_fraction > 0.0 {
let required = (options.min_positive_depth_fraction * inliers.len() as f64).ceil() as i64;
if best_score < required {
return None;
}
}
if options.max_ambiguity_ratio < 1.0
&& second_score > 0
&& (second_score as f64) > (best_score as f64) * options.max_ambiguity_ratio
{
return None;
}
let rotation = UnitQuaternion::from_matrix(&rotation);
let alternate = ranked.get(1).and_then(|(score, r, t)| {
if *score <= 0 {
return None;
}
let r = UnitQuaternion::from_matrix(r);
let rot_diff = r.rotation_to(&rotation).angle();
let t_anti = t.dot(&translation) < 0.0;
if rot_diff > 1e-2 || t_anti {
Some((r, *t))
} else {
None
}
});
Some(RelativePoseRecovery {
rotation,
translation_unit: translation,
best_score,
second_score: second_score.max(0),
alternate,
})
}
#[inline]
fn triangulate_in_front(
p_prev: &Matrix3x4<f64>,
p_curr: &Matrix3x4<f64>,
rotation: &Matrix3<f64>,
translation: &Vector3<f64>,
prev: Point2<f64>,
curr: Point2<f64>,
) -> Option<Vector3<f64>> {
let mut a = nalgebra::Matrix4::<f64>::zeros();
for column in 0..4 {
a[(0, column)] = prev.x * p_prev[(2, column)] - p_prev[(0, column)];
a[(1, column)] = prev.y * p_prev[(2, column)] - p_prev[(1, column)];
a[(2, column)] = curr.x * p_curr[(2, column)] - p_curr[(0, column)];
a[(3, column)] = curr.y * p_curr[(2, column)] - p_curr[(1, column)];
}
let svd = a.try_svd(false, true, f64::EPSILON * 5.0, 10_000)?;
let v_t = svd.v_t?;
let solution = v_t.row(3);
let w = solution[3];
if w.abs() < 1e-12 {
return None;
}
let world = Vector3::new(solution[0] / w, solution[1] / w, solution[2] / w);
let camera_curr = rotation * world + translation;
if world.z <= 0.0 || camera_curr.z <= 0.0 {
return None;
}
Some(world)
}
fn all_in_front_for_some_pose(
essential: &Matrix3<f64>,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> bool {
if correspondences.is_empty() {
return false;
}
let Some(svd) = essential.try_svd(true, true, f64::EPSILON * 5.0, 10_000) else {
return false;
};
let (Some(u), Some(v_t)) = (svd.u, svd.v_t) else {
return false;
};
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_unit = u.column(2).into_owned();
let points: Option<Vec<(Point2<f64>, Point2<f64>)>> = correspondences
.iter()
.map(|c| {
Some((
camera.normalize_pixel(&c.previous_xy)?,
camera.normalize_pixel(&c.current_xy)?,
))
})
.collect();
let Some(points) = points else {
return false;
};
let rays: Vec<(Vector3<f64>, Vector3<f64>)> = points
.iter()
.map(|(p, c)| {
(
Vector3::new(p.x, p.y, 1.0).normalize(),
Vector3::new(c.x, c.y, 1.0).normalize(),
)
})
.collect();
[(r1, t_unit), (r1, -t_unit), (r2, t_unit), (r2, -t_unit)]
.iter()
.any(|(rotation, translation)| {
rays.iter()
.all(|(x1, x2)| depths_positive(rotation, translation, x1, x2))
})
}
fn depths_positive(
rotation: &Matrix3<f64>,
translation: &Vector3<f64>,
x1: &Vector3<f64>,
x2: &Vector3<f64>,
) -> bool {
let rx1 = rotation * x1;
let a = -rx1.dot(x2);
let b1 = -rx1.dot(translation);
let b2 = x2.dot(translation);
let lambda1 = b1 - a * b2;
let lambda2 = -a * b1 + b2;
lambda1 > 0.0 && lambda2 > 0.0
}
fn cheirality_score(
rotation: &Matrix3<f64>,
translation: &Vector3<f64>,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
inliers: &[usize],
min_tri_angle_rad: f64,
) -> i64 {
let p_prev = Matrix3x4::new(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
let mut p_curr = Matrix3x4::zeros();
p_curr.fixed_view_mut::<3, 3>(0, 0).copy_from(rotation);
p_curr.fixed_view_mut::<3, 1>(0, 3).copy_from(translation);
let cam2_centre = -(rotation.transpose() * translation);
let mut score: i64 = 0;
for &index in inliers {
let correspondence = &correspondences[index];
let Some(prev) = camera.normalize_pixel(&correspondence.previous_xy) else {
continue;
};
let Some(curr) = camera.normalize_pixel(&correspondence.current_xy) else {
continue;
};
let Some(world) = triangulate_in_front(&p_prev, &p_curr, rotation, translation, prev, curr)
else {
continue;
};
if min_tri_angle_rad > 0.0 {
let ray1 = world;
let ray2 = world - cam2_centre;
let n1 = ray1.norm();
let n2 = ray2.norm();
if n1 < 1e-12 || n2 < 1e-12 {
continue;
}
let cos = (ray1.dot(&ray2) / (n1 * n2)).clamp(-1.0, 1.0);
if cos.acos() < min_tri_angle_rad {
continue;
}
}
score += 1;
}
score
}
fn hartley_normalization<I>(points: I) -> Option<(Matrix3<f64>, Vec<Point2<f64>>)>
where
I: IntoIterator<Item = Point2<f64>>,
{
let collected: Vec<Point2<f64>> = points.into_iter().collect();
if collected.is_empty() {
return None;
}
let mut mean_x = 0.0;
let mut mean_y = 0.0;
for point in &collected {
mean_x += point.x;
mean_y += point.y;
}
let count = collected.len() as f64;
mean_x /= count;
mean_y /= count;
let mut mean_distance = 0.0;
for point in &collected {
let dx = point.x - mean_x;
let dy = point.y - mean_y;
mean_distance += (dx * dx + dy * dy).sqrt();
}
mean_distance /= count;
if mean_distance < 1.0e-12 {
return None;
}
let scale = std::f64::consts::SQRT_2 / mean_distance;
let transform = Matrix3::new(
scale,
0.0,
-scale * mean_x,
0.0,
scale,
-scale * mean_y,
0.0,
0.0,
1.0,
);
let normalized = collected
.into_iter()
.map(|point| Point2::new(scale * (point.x - mean_x), scale * (point.y - mean_y)))
.collect();
Some((transform, normalized))
}
fn normalize_pairs(
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
) -> Option<Vec<(Point2<f64>, Point2<f64>)>> {
correspondences
.iter()
.map(|correspondence| {
Some((
camera.normalize_pixel(&correspondence.previous_xy)?,
camera.normalize_pixel(&correspondence.current_xy)?,
))
})
.collect()
}
fn normalize_correspondences_with_cameras(
correspondences: &[TwoViewCorrespondence],
previous_camera: &Camera,
current_camera: &Camera,
) -> Option<Vec<TwoViewCorrespondence>> {
correspondences
.iter()
.map(|correspondence| {
Some(TwoViewCorrespondence::new(
previous_camera.normalize_pixel(&correspondence.previous_xy)?,
current_camera.normalize_pixel(&correspondence.current_xy)?,
))
})
.collect()
}
fn score_inliers(
essential: &Matrix3<f64>,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
threshold_sq: f64,
) -> Vec<usize> {
let mut inliers = Vec::with_capacity(correspondences.len());
for (index, correspondence) in correspondences.iter().enumerate() {
let Some(distance_sq) = sampson_distance_squared(essential, correspondence, camera) else {
continue;
};
if distance_sq <= threshold_sq {
inliers.push(index);
}
}
inliers
}
fn mean_sampson_error(
essential: &Matrix3<f64>,
correspondences: &[TwoViewCorrespondence],
camera: &Camera,
inliers: &[usize],
) -> f64 {
if inliers.is_empty() {
return f64::INFINITY;
}
let mut total = 0.0;
let mut count = 0.0;
for &index in inliers {
if let Some(distance_sq) =
sampson_distance_squared(essential, &correspondences[index], camera)
{
total += distance_sq.sqrt();
count += 1.0;
}
}
if count > 0.0 {
total / count
} else {
f64::INFINITY
}
}
fn sampson_distance_squared(
essential: &Matrix3<f64>,
correspondence: &TwoViewCorrespondence,
camera: &Camera,
) -> Option<f64> {
let prev = camera.normalize_pixel(&correspondence.previous_xy)?;
let curr = camera.normalize_pixel(&correspondence.current_xy)?;
let prev_h = Vector3::new(prev.x, prev.y, 1.0);
let curr_h = Vector3::new(curr.x, curr.y, 1.0);
let e_prev = essential * prev_h;
let et_curr = essential.transpose() * curr_h;
let numerator = curr_h.dot(&e_prev).powi(2);
let denominator = e_prev.x.powi(2) + e_prev.y.powi(2) + et_curr.x.powi(2) + et_curr.y.powi(2);
if denominator < 1e-18 {
return None;
}
Some(numerator / denominator)
}
#[cfg(test)]
mod tests {
use super::*;
use nalgebra::{Point3, UnitQuaternion};
use visloc_core::geometry::Pose;
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>) -> Point2<f64> {
camera
.project(&pose.transform_world_point(point))
.expect("synthetic point must project in front of the camera")
}
fn synthetic_world_points() -> Vec<Point3<f64>> {
vec![
Point3::new(-1.0, -1.0, 5.0),
Point3::new(1.0, -1.0, 5.0),
Point3::new(-1.0, 1.0, 5.0),
Point3::new(1.0, 1.0, 5.0),
Point3::new(0.0, 0.0, 5.0),
Point3::new(0.5, -0.25, 6.0),
Point3::new(-0.7, 0.4, 4.5),
Point3::new(0.6, 0.8, 5.5),
Point3::new(-0.3, -0.6, 4.8),
Point3::new(0.2, 0.2, 6.5),
]
}
fn correspondences(
previous_pose: &Pose,
current_pose: &Pose,
camera: &Camera,
points: &[Point3<f64>],
) -> Vec<TwoViewCorrespondence> {
points
.iter()
.map(|point| TwoViewCorrespondence {
previous_xy: project(previous_pose, camera, point),
current_xy: project(current_pose, camera, point),
})
.collect()
}
#[test]
fn essential_ransac_recovers_pure_translation() {
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(&previous, ¤t, &camera, &synthetic_world_points());
let estimator = RelativePoseEstimator::default();
let pose = estimator
.estimate_with_scale(&correspondences, &camera, 0.3)
.expect("relative pose must be recovered");
let translation = pose.previous_to_current.translation;
assert!(
(translation - Vector3::new(-0.3, 0.0, 0.0)).norm() < 5.0e-3,
"translation drifted: {translation:?}"
);
let rotation = pose.previous_to_current.rotation.angle();
assert!(
rotation < 5.0e-3,
"rotation should be near zero: {rotation}"
);
assert!(pose.inliers.len() >= 8);
assert!(pose.mean_sampson_error < 5.0e-3);
}
#[test]
fn essential_ransac_accepts_distinct_pinhole_cameras() {
let previous_camera = synthetic_camera();
let current_camera = Camera::pinhole(2, 800, 600, 700.0, 680.0, 400.0, 300.0);
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 = synthetic_world_points()
.iter()
.map(|point| {
TwoViewCorrespondence::new(
project(&previous, &previous_camera, point),
project(¤t, ¤t_camera, point),
)
})
.collect::<Vec<_>>();
let estimated = RelativePoseEstimator::default()
.estimate_with_cameras(&correspondences, &previous_camera, ¤t_camera)
.expect("distinct pinhole calibrations must still recover a pose");
assert!(estimated.inliers.len() >= 8);
assert!(estimated.previous_to_current.rotation.angle() < 5.0e-3);
assert!(
(estimated.translation_unit - Vector3::new(-1.0, 0.0, 0.0)).norm() < 5.0e-3,
"translation direction drifted: {:?}",
estimated.translation_unit
);
}
#[test]
fn essential_ransac_recovers_translation_with_yaw() {
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.05);
let current_world_to_camera = SE3::new(yaw, Vector3::new(-0.2, 0.0, -0.05));
let current = Pose {
world_to_camera: current_world_to_camera.clone(),
};
let correspondences =
correspondences(&previous, ¤t, &camera, &synthetic_world_points());
let estimator = RelativePoseEstimator::default();
let scale = current_world_to_camera.translation.norm();
let pose = estimator
.estimate_with_scale(&correspondences, &camera, scale)
.expect("relative pose must be recovered");
assert!(pose.inliers.len() >= 8);
let translation_error =
(pose.previous_to_current.translation - current_world_to_camera.translation).norm();
assert!(
translation_error < 5.0e-3,
"translation drifted: error={translation_error}"
);
let rotation_error = pose
.previous_to_current
.rotation
.rotation_to(¤t_world_to_camera.rotation)
.angle()
.abs();
assert!(
rotation_error < 5.0e-3,
"rotation drifted: error_rad={rotation_error}"
);
}
#[test]
fn essential_ransac_recovers_pure_translation_with_eight_points() {
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.30, 0.0, 0.0));
let points = [
Point3::new(-1.0, -1.0, 4.5),
Point3::new(1.0, -1.0, 4.6),
Point3::new(-1.0, 1.0, 5.5),
Point3::new(1.0, 1.0, 5.4),
Point3::new(0.0, 0.0, 5.0),
Point3::new(0.5, -0.25, 6.0),
Point3::new(-0.6, 0.4, 4.8),
Point3::new(0.4, 0.7, 5.2),
];
let correspondences = correspondences(&previous, ¤t, &camera, &points);
let estimator = RelativePoseEstimator::default();
let pose = estimator
.estimate_with_scale(&correspondences, &camera, 0.30)
.expect("relative pose must be recovered");
let translation = pose.previous_to_current.translation;
assert!(
(translation - Vector3::new(-0.30, 0.0, 0.0)).norm() < 5.0e-3,
"translation drifted: {translation:?}"
);
assert!(pose.inliers.len() >= 8);
}
#[test]
fn essential_ransac_returns_none_for_too_few_points() {
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.2, 0.0, 0.0));
let mut points = synthetic_world_points();
points.truncate(6);
let correspondences = correspondences(&previous, ¤t, &camera, &points);
let estimator = RelativePoseEstimator::default();
assert!(estimator.estimate(&correspondences, &camera).is_none());
}
#[test]
fn weighted_ransac_recovers_pose_with_correctly_ordered_confidence_weights() {
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 correspondences =
correspondences(&previous, ¤t, &camera, &synthetic_world_points());
let n_inliers = correspondences.len();
let mut weights: Vec<f32> = vec![0.9; n_inliers];
let outlier_seeds = [
(50.0, 60.0, 200.0, 90.0),
(310.0, 100.0, 70.0, 410.0),
(250.0, 200.0, 100.0, 100.0),
(440.0, 330.0, 200.0, 60.0),
(90.0, 350.0, 320.0, 240.0),
(10.0, 410.0, 540.0, 430.0),
(280.0, 70.0, 110.0, 200.0),
(190.0, 30.0, 460.0, 160.0),
(370.0, 250.0, 50.0, 50.0),
(230.0, 410.0, 600.0, 100.0),
(100.0, 110.0, 300.0, 350.0),
(420.0, 90.0, 30.0, 220.0),
(150.0, 300.0, 480.0, 60.0),
(350.0, 150.0, 70.0, 320.0),
(60.0, 250.0, 230.0, 70.0),
(270.0, 380.0, 540.0, 290.0),
(390.0, 20.0, 90.0, 270.0),
(210.0, 170.0, 470.0, 380.0),
];
for (px, py, cx, cy) in outlier_seeds {
correspondences.push(TwoViewCorrespondence::new(
nalgebra::Point2::new(px, py),
nalgebra::Point2::new(cx, cy),
));
weights.push(0.05);
}
assert_eq!(weights.len(), correspondences.len());
let ransac = EssentialRansac {
estimator: EightPointEssentialMatrixEstimator::default(),
config: EssentialRansacConfig {
iterations: 64,
sampson_threshold: 5.0e-3,
seed: 11,
},
};
let weighted_report = ransac
.estimate_with_weights(&correspondences, &camera, &weights)
.expect("weighted RANSAC must recover a model from clean inliers");
let recovered_inliers: usize = weighted_report
.inliers
.iter()
.filter(|&&i| i < n_inliers)
.count();
assert!(
recovered_inliers >= n_inliers - 1,
"weighted RANSAC should recover the geometric inliers, got {recovered_inliers}/{n_inliers}"
);
assert!(weighted_report.mean_sampson_error < 5.0e-3);
}
#[test]
fn weighted_ransac_falls_back_to_uniform_when_weights_are_all_zero() {
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(&previous, ¤t, &camera, &synthetic_world_points());
let weights: Vec<f32> = vec![0.0; correspondences.len()];
let ransac = EssentialRansac::default();
let unweighted = ransac.estimate(&correspondences, &camera).unwrap();
let weighted = ransac
.estimate_with_weights(&correspondences, &camera, &weights)
.unwrap();
assert_eq!(unweighted.inliers, weighted.inliers);
}
#[test]
fn hardened_cheirality_recovers_clean_translation() {
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(&previous, ¤t, &camera, &synthetic_world_points());
let estimator = RelativePoseEstimator {
cheirality: CheiralityOptions::hardened(),
..RelativePoseEstimator::default()
};
let pose = estimator
.estimate_with_scale(&correspondences, &camera, 0.3)
.expect("hardened recovery must succeed on a clean baseline");
assert!(
(pose.previous_to_current.translation - Vector3::new(-0.3, 0.0, 0.0)).norm() < 5e-3
);
}
#[test]
fn hardened_cheirality_rejects_near_planar_pure_rotation() {
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.02);
let current = Pose::from_world_to_camera(yaw, Vector3::new(-1e-4, 0.0, 0.0));
let correspondences =
correspondences(&previous, ¤t, &camera, &synthetic_world_points());
let report = EssentialRansac::default()
.estimate(&correspondences, &camera)
.expect("essential still fits a near-planar pair");
let recovered = recover_relative_pose_with_options(
&report.essential,
&correspondences,
&camera,
&report.inliers,
&CheiralityOptions::hardened(),
);
assert!(
recovered.is_none(),
"pure-rotation / tiny-baseline essentials must fail hardened gates"
);
}
#[test]
fn chirality_margin_is_high_on_clean_baseline() {
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(&previous, ¤t, &camera, &synthetic_world_points());
let report = EssentialRansac::default()
.estimate(&correspondences, &camera)
.unwrap();
let recovered = recover_relative_pose_with_options(
&report.essential,
&correspondences,
&camera,
&report.inliers,
&CheiralityOptions::default(),
)
.unwrap();
assert!(
recovered.chirality_margin() > 0.5,
"clean baseline should separate winner from runner-up, margin={}",
recovered.chirality_margin()
);
}
#[test]
fn hardened_keep_ambiguous_exposes_alternate_on_clean_pair() {
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(&previous, ¤t, &camera, &synthetic_world_points());
let report = EssentialRansac::default()
.estimate(&correspondences, &camera)
.unwrap();
let recovered = recover_relative_pose_with_options(
&report.essential,
&correspondences,
&camera,
&report.inliers,
&CheiralityOptions::hardened_keep_ambiguous(),
)
.expect("clean pair must recover under keep-ambiguous");
assert!(recovered.best_score > 0);
if recovered.second_score > 0 {
assert!(
recovered.alternate.is_some() || recovered.chirality_margin() > 0.99,
"expected an alternate or a decisive margin"
);
}
}
}