Skip to main content

vision_calibration_core/models/
projection.rs

1use nalgebra::{Point2, RealField, Vector3};
2use serde::{Deserialize, Serialize};
3
4/// Projection model from a camera direction to normalized coordinates.
5pub trait ProjectionModel<S: RealField + Copy> {
6    /// Project a direction in camera coordinates to normalized coordinates.
7    ///
8    /// Returns `None` when the direction is not projectable (e.g. behind camera).
9    fn project_dir(&self, dir_c: &Vector3<S>) -> Option<Point2<S>>;
10    /// Unproject normalized coordinates to a direction in camera coordinates.
11    fn unproject_dir(&self, n: &Point2<S>) -> Vector3<S>;
12}
13
14/// Classic pinhole projection model.
15#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
16pub struct Pinhole;
17
18impl<S: RealField + Copy> ProjectionModel<S> for Pinhole {
19    fn project_dir(&self, dir_c: &Vector3<S>) -> Option<Point2<S>> {
20        if dir_c.z <= S::zero() {
21            return None;
22        }
23        Some(Point2::new(dir_c.x / dir_c.z, dir_c.y / dir_c.z))
24    }
25
26    fn unproject_dir(&self, n: &Point2<S>) -> Vector3<S> {
27        Vector3::new(n.x, n.y, S::one())
28    }
29}