use derive_more::{AsMut, AsRef, Constructor, Deref, DerefMut, From, Into};
use nalgebra::{Matrix3, Point2, Point3, Vector2};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
PartialOrd,
AsMut,
AsRef,
Constructor,
Deref,
DerefMut,
From,
Into,
)]
pub struct ImageKeyPoint(pub Point2<f32>);
#[derive(
Debug,
Clone,
Copy,
PartialEq,
PartialOrd,
AsMut,
AsRef,
Constructor,
Deref,
DerefMut,
From,
Into,
)]
pub struct NormalizedKeyPoint(pub Point2<f32>);
impl NormalizedKeyPoint {
pub fn with_depth(self, depth: f32) -> CameraPoint {
CameraPoint((depth * self.coords).push(depth).into())
}
}
impl From<CameraPoint> for NormalizedKeyPoint {
fn from(camera: CameraPoint) -> Self {
NormalizedKeyPoint(camera.xy() / camera.z)
}
}
#[derive(
Debug,
Clone,
Copy,
PartialEq,
PartialOrd,
AsMut,
AsRef,
Constructor,
Deref,
DerefMut,
From,
Into,
)]
pub struct CameraPoint(pub Point3<f32>);
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Constructor)]
pub struct CameraIntrinsics {
pub focals: Vector2<f32>,
pub principal_point: Point2<f32>,
pub skew: f32,
}
impl CameraIntrinsics {
pub fn identity() -> Self {
Self {
focals: Vector2::new(1.0, 1.0),
skew: 0.0,
principal_point: Point2::new(0.0, 0.0),
}
}
pub fn focals(self, focals: Vector2<f32>) -> Self {
Self { focals, ..self }
}
pub fn focal(self, focal: f32) -> Self {
Self {
focals: Vector2::new(focal, focal),
..self
}
}
pub fn principal_point(self, principal_point: Point2<f32>) -> Self {
Self {
principal_point,
..self
}
}
pub fn skew(self, skew: f32) -> Self {
Self { skew, ..self }
}
#[rustfmt::skip]
pub fn matrix(&self) -> Matrix3<f32> {
Matrix3::new(
self.focals.x, self.skew, self.principal_point.x,
0.0, self.focals.y, self.principal_point.y,
0.0, 0.0, 1.0,
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Constructor)]
pub struct CameraSpecification {
pub pixels: Vector2<usize>,
pub pixel_dimensions: Vector2<f32>,
}
impl CameraSpecification {
pub fn from_sensor(pixels: Vector2<usize>, sensor_dimensions: Vector2<f32>) -> Self {
Self {
pixels,
pixel_dimensions: Vector2::new(
sensor_dimensions.x / pixels.x as f32,
sensor_dimensions.y / pixels.y as f32,
),
}
}
pub fn from_sensor_square(pixels: Vector2<usize>, sensor_width: f32) -> Self {
let pixel_width = sensor_width / pixels.x as f32;
Self {
pixels,
pixel_dimensions: Vector2::new(pixel_width, pixel_width),
}
}
pub fn intrinsics_centered(&self, focal: f32) -> CameraIntrinsics {
CameraIntrinsics::identity()
.focal(focal)
.principal_point(self.pixel_dimensions.map(|p| p as f32 / 2.0 - 0.5).into())
}
}