use nalgebra::{Matrix3, Matrix4, Rotation3, Translation3, UnitQuaternion, Vector3};
#[derive(Clone, Debug)]
pub struct Homography {
pub h: Matrix3<f64>,
}
impl Homography {
pub fn new(h: Matrix3<f64>) -> Self {
Self { h }
}
}
#[derive(Clone, Debug)]
pub struct FundamentalMatrix {
pub f: Matrix3<f64>,
}
impl FundamentalMatrix {
pub fn new(f: Matrix3<f64>) -> Self {
Self { f }
}
}
#[derive(Clone, Debug)]
pub struct EssentialMatrix {
pub e: Matrix3<f64>,
}
impl EssentialMatrix {
pub fn new(e: Matrix3<f64>) -> Self {
Self { e }
}
}
#[derive(Clone, Debug)]
pub struct AbsolutePose {
pub rotation: UnitQuaternion<f64>,
pub translation: Translation3<f64>,
}
impl AbsolutePose {
pub fn new(rotation: UnitQuaternion<f64>, translation: Translation3<f64>) -> Self {
Self {
rotation,
translation,
}
}
pub fn from_rt(r: Matrix3<f64>, t: Vector3<f64>) -> Self {
let rot = Rotation3::from_matrix_unchecked(r);
let quat = UnitQuaternion::from_rotation_matrix(&rot);
let tr = Translation3::from(t);
Self::new(quat, tr)
}
}
#[derive(Clone, Debug)]
pub struct RigidTransform {
pub rotation: UnitQuaternion<f64>,
pub translation: Translation3<f64>,
}
impl RigidTransform {
pub fn new(rotation: UnitQuaternion<f64>, translation: Translation3<f64>) -> Self {
Self {
rotation,
translation,
}
}
pub fn to_matrix4(&self) -> Matrix4<f64> {
let r = self.rotation.to_homogeneous();
let t = self.translation.to_homogeneous();
r * t
}
}
#[derive(Clone, Debug)]
pub struct Line {
pub params: Vector3<f64>,
}
impl Line {
pub fn new(a: f64, b: f64, c: f64) -> Self {
let norm = (a * a + b * b).sqrt();
if norm < 1e-10 {
Self {
params: Vector3::new(0.0, 1.0, c),
}
} else {
Self {
params: Vector3::new(a / norm, b / norm, c / norm),
}
}
}
pub fn from_normalized(params: Vector3<f64>) -> Self {
Self { params }
}
pub fn params(&self) -> &Vector3<f64> {
&self.params
}
pub fn distance_to_point(&self, x: f64, y: f64) -> f64 {
(self.params[0] * x + self.params[1] * y + self.params[2]).abs()
}
pub fn to_slope_intercept(&self) -> Option<(f64, f64)> {
if self.params[1].abs() < 1e-10 {
None } else {
let slope = -self.params[0] / self.params[1];
let intercept = -self.params[2] / self.params[1];
Some((slope, intercept))
}
}
}