cv_core/
camera.rs

1use crate::{ImagePoint, KeyPoint};
2use nalgebra::{Unit, Vector3};
3
4/// Describes the direction that the projection onto the camera's optical center
5/// came from. It is implemented on projection items from different camera models.
6/// It is also implemented for `Unit<Vector3<f64>>` if you want to pre-compute the
7/// normalized bearings for efficiency or to turn all camera models into a unified type.
8pub trait Bearing {
9    /// Returns a unit vector of the direction that the projection
10    /// created by the feature projects out of the
11    /// optical center of the camera. This is defined as the the
12    /// position delta of the feature from the optical center of the camera.
13    fn bearing(&self) -> Unit<Vector3<f64>> {
14        Unit::new_normalize(self.bearing_unnormalized())
15    }
16
17    /// Returns the unnormalized bearing which has direction point towards the direction
18    /// that the signal entered the camera's center. The magnitude of this vector
19    /// is unknown. Use this if you are sure that you do not need a normalized
20    /// bearing. This may be faster.
21    fn bearing_unnormalized(&self) -> Vector3<f64>;
22
23    /// Converts a bearing vector back into this bearing type.
24    ///
25    /// This is useful if you would like to go backwards from reconstruction space to image space.
26    /// See [`CameraModel::uncalibrate`] for how to then convert the camera bearing into image coordinates.
27    fn from_bearing_vector(bearing: Vector3<f64>) -> Self;
28
29    /// Converts a bearing unit vector back into this bearing type.
30    ///
31    /// This is useful if you would like to go backwards from reconstruction space to image space.
32    /// See [`CameraModel::uncalibrate`] for how to then convert the camera bearing into image coordinates.
33    fn from_bearing_unit_vector(bearing: Unit<Vector3<f64>>) -> Self
34    where
35        Self: Sized,
36    {
37        Self::from_bearing_vector(bearing.into_inner())
38    }
39}
40
41impl Bearing for Unit<Vector3<f64>> {
42    fn bearing(&self) -> Unit<Vector3<f64>> {
43        *self
44    }
45
46    fn bearing_unnormalized(&self) -> Vector3<f64> {
47        self.into_inner()
48    }
49
50    fn from_bearing_vector(bearing: Vector3<f64>) -> Self {
51        Unit::new_normalize(bearing)
52    }
53
54    fn from_bearing_unit_vector(bearing: Unit<Vector3<f64>>) -> Self {
55        bearing
56    }
57}
58
59/// Allows conversion between the point on an image and the internal projection
60/// which can describe the bearing of the projection out of the camera.
61pub trait CameraModel {
62    type Projection: Bearing;
63
64    /// Extracts a projection from a pixel location in an image.
65    fn calibrate<P>(&self, point: P) -> Self::Projection
66    where
67        P: ImagePoint;
68
69    /// Extracts the pixel location in the image from the projection.
70    fn uncalibrate(&self, projection: Self::Projection) -> KeyPoint;
71}