azure_kinect/
calibration.rs

1use crate::enums::CalibrationType;
2use crate::*;
3use azure_kinect_sys::k4a::*;
4
5pub struct Calibration<'a> {
6    api: &'a azure_kinect_sys::api::Api,
7    pub(crate) calibration: k4a_calibration_t,
8}
9
10impl<'a> Calibration<'a> {
11    pub(crate) fn from_handle(
12        api: &azure_kinect_sys::api::Api,
13        calibration: k4a_calibration_t,
14    ) -> Calibration {
15        Calibration { api, calibration }
16    }
17
18    #[deprecated(since = "0.2", note = "Factory::calibration_get_from_raw")]
19    pub fn from_raw(
20        factory: &'a Factory,
21        raw_calibration: &Vec<u8>,
22        target_depth_mode: DepthMode,
23        target_color_resolution: ColorResolution,
24    ) -> Result<Calibration<'a>, Error> {
25        factory.calibration_get_from_raw(
26            raw_calibration,
27            target_depth_mode,
28            target_color_resolution,
29        )
30    }
31
32    /// Transform a 3d point of a source coordinate system into a 3d point of the target coordinate system.
33    pub fn convert_3d_to_3d(
34        &self,
35        source_point3d: &Float3,
36        source_camera: CalibrationType,
37        target_camera: CalibrationType,
38    ) -> Result<Float3, Error> {
39        let mut target_point3d = Float3::default();
40        Error::from_k4a_result_t(unsafe {
41            (self.api.funcs.k4a_calibration_3d_to_3d)(
42                &self.calibration,
43                &source_point3d.value,
44                source_camera.into(),
45                target_camera.into(),
46                &mut target_point3d.value,
47            )
48        })
49        .to_result(target_point3d)
50    }
51
52    /// Transform a 2d pixel coordinate with an associated depth value of the source camera into a 3d point of the target coordinate system.
53    /// Returns false if the point is invalid in the target coordinate system (and therefore target_point3d should not be used)
54    pub fn convert_2d_to_3d(
55        &self,
56        source_point2d: &Float2,
57        source_depth: f32,
58        source_camera: CalibrationType,
59        target_camera: CalibrationType,
60    ) -> Result<(Float3, bool), Error> {
61        let mut target_point3d = Float3::default();
62        let mut valid: i32 = 0;
63        Error::from_k4a_result_t(unsafe {
64            (self.api.funcs.k4a_calibration_2d_to_3d)(
65                &self.calibration,
66                &source_point2d.value,
67                source_depth,
68                source_camera.into(),
69                target_camera.into(),
70                &mut target_point3d.value,
71                &mut valid,
72            )
73        })
74        .to_result((target_point3d, valid != 0))
75    }
76
77    /// Transform a 3d point of a source coordinate system into a 2d pixel coordinate of the target camera.
78    /// Returns false if the point is invalid in the target coordinate system (and therefore target_point2d should not be used)
79    pub fn convert_3d_to_2d(
80        &self,
81        source_point3d: &Float3,
82        source_camera: CalibrationType,
83        target_camera: CalibrationType,
84    ) -> Result<(Float2, bool), Error> {
85        let mut target_point2d = Float2::default();
86        let mut valid: i32 = 0;
87        Error::from_k4a_result_t(unsafe {
88            (self.api.funcs.k4a_calibration_3d_to_2d)(
89                &self.calibration,
90                &source_point3d.value,
91                source_camera.into(),
92                target_camera.into(),
93                &mut target_point2d.value,
94                &mut valid,
95            )
96        })
97        .to_result((target_point2d, valid != 0))
98    }
99
100    /// Transform a 2d pixel coordinate with an associated depth value of the source camera into a 2d pixel coordinate of the target camera
101    /// Returns false if the point is invalid in the target coordinate system (and therefore target_point2d should not be used)
102    pub fn convert_2d_to_2d(
103        &self,
104        source_point2d: &Float2,
105        source_depth: f32,
106        source_camera: CalibrationType,
107        target_camera: CalibrationType,
108    ) -> Result<(Float2, bool), Error> {
109        let mut target_point2d = Float2::default();
110        let mut valid: i32 = 0;
111        Error::from_k4a_result_t(unsafe {
112            (self.api.funcs.k4a_calibration_2d_to_2d)(
113                &self.calibration,
114                &source_point2d.value,
115                source_depth,
116                source_camera.into(),
117                target_camera.into(),
118                &mut target_point2d.value,
119                &mut valid,
120            )
121        })
122        .to_result((target_point2d, valid != 0))
123    }
124
125    /// Transform a 2D pixel coordinate from color camera into a 2D pixel coordinate of the depth camera. This function
126    /// searches along an epipolar line in the depth image to find the corresponding depth pixel.
127    /// Returns false if the point is invalid in the target coordinate system (and therefore target_point2d should not be used)
128    pub fn convert_color_2d_to_depth_2d(
129        &self,
130        source_point2d: &Float2,
131        depth_image: &Image,
132    ) -> Result<(Float2, bool), Error> {
133        let mut target_point2d = Float2::default();
134        let mut valid: i32 = 0;
135        Error::from_k4a_result_t(unsafe {
136            (self.api.funcs.k4a_calibration_color_2d_to_depth_2d)(
137                &self.calibration,
138                &source_point2d.value,
139                depth_image.handle,
140                &mut target_point2d.value,
141                &mut valid,
142            )
143        })
144        .to_result((target_point2d, valid != 0))
145    }
146}