Time-of-Flight Library(ToF) 4.0.2
 
calibration.hpp
1#ifndef _CHRONOPTICS_TOF_CALIBRATION_HPP_
2#define _CHRONOPTICS_TOF_CALIBRATION_HPP_
3
4#include <chronoptics/tof/calibration.h>
5
6#include <chronoptics/tof/data.hpp>
7
8namespace chronoptics {
9namespace tof {
10
11/** 2D point for indexing into array
12*/
13class Point2d : public detail::Base<tof_point_2d, tof_point_2d_delete> {
14 public:
15 /** Construct from pointer */
16 Point2d(tof_point_2d_t ptr = nullptr) {
17 this->ptr_ = ptr;
18 }
19
20 /** Get the row value
21 * @return Row value
22 */
23 int32_t row() {
24 return tof_point_2d_row(this->ptr_, TOF_ERROR_HANDLER{});
25 }
26
27 /** Get the column value
28 * @return Column value
29 */
30 int32_t col() {
31 return tof_point_2d_col(this->ptr_, TOF_ERROR_HANDLER{});
32 }
33
34};
35
36/** This class contains all the calibration information
37*/
38class Calibration : public detail::Base<tof_calibration, tof_calibration_delete> {
39 public:
40 /** Construct from pointer */
41 Calibration(tof_calibration_t ptr = nullptr) {
42 this->ptr_ = ptr;
43 }
44
45 /** Read calibration from disk
46 * @param file_location Location of calibration file
47 */
48 Calibration(StringView file_location) : Calibration(tof_calibration_new_from_disk(file_location, TOF_ERROR_HANDLER{})) {}
49
50 /** Write calibration to disk
51 * @param file_location Location to save the calibration file to
52 */
53 void write(StringView file_location) const {
54 return tof_calibration_write(this->ptr_, file_location, TOF_ERROR_HANDLER{});
55 }
56
57 /** The the calibrated frequencies in the calibration file
58 * @return The calibrated modulation frequencies
59 */
60 std::vector<float> get_calibrated_frequencies() const {
61 size_t size = tof_calibration_get_calibrated_frequencies(this->ptr_, nullptr, 0, TOF_ERROR_HANDLER{});
62 std::vector<float> vec(size);
63 size = tof_calibration_get_calibrated_frequencies(this->ptr_, vec.data(), vec.size(), TOF_ERROR_HANDLER{});
64 return vec;
65 }
66
67 /** Get focal length x
68 * @return Focal length
69 */
70 double get_focal_length_x() const {
71 return tof_calibration_get_focal_length_x(this->ptr_, TOF_ERROR_HANDLER{});
72 }
73
74 /** Get focal length y
75 * @return Focal length
76 */
77 double get_focal_length_y() const {
78 return tof_calibration_get_focal_length_y(this->ptr_, TOF_ERROR_HANDLER{});
79 }
80
81 /** Get principal point x
82 * @return Principal point
83 */
84 double get_principal_point_x() const {
85 return tof_calibration_get_principal_point_x(this->ptr_, TOF_ERROR_HANDLER{});
86 }
87
88 /** Get principal point y
89 * @return Principal point
90 */
91 double get_principal_point_y() const {
92 return tof_calibration_get_principal_point_y(this->ptr_, TOF_ERROR_HANDLER{});
93 }
94
95 /** Get the camera matrix of the depth sensor and lens
96 * @return Camera matrix, 3x3 matrix
97 */
98 std::array<double, 9> get_depth_camera_matrix() const {
99 std::array<double, 9> array;
100 auto data = tof_calibration_get_depth_camera_matrix(this->ptr_, TOF_ERROR_HANDLER{});
101 std::copy(data, data + array.size(), array.data());
102 return array;
103 }
104
105 /** Get the distortion coefficients of the depth lens
106 * @return Distortion coefficients
107 */
108 std::vector<double> get_depth_distortion_coefficients() const {
109 size_t size = tof_calibration_get_depth_distortion_coefficients(this->ptr_, nullptr, 0, TOF_ERROR_HANDLER{});
110 std::vector<double> vec(size);
111 size = tof_calibration_get_depth_distortion_coefficients(this->ptr_, vec.data(), vec.size(), TOF_ERROR_HANDLER{});
112 return vec;
113 }
114
115 /** Get the camera matrix of the rgb sensor and lens
116 * @return Camera matrix, 3x3 matrix
117 */
118 std::array<double, 9> get_rgb_camera_matrix() const {
119 std::array<double, 9> array;
120 auto data = tof_calibration_get_rgb_camera_matrix(this->ptr_, TOF_ERROR_HANDLER{});
121 std::copy(data, data + array.size(), array.data());
122 return array;
123 }
124
125 /** Get the distortion coefficients of the rgb lens
126 * @return Distortion coefficients, 5x1 matrix
127 */
128 std::array<double, 5> get_rgb_distortion_coefficients() const {
129 std::array<double, 5> array;
130 auto data = tof_calibration_get_rgb_distortion_coefficients(this->ptr_, TOF_ERROR_HANDLER{});
131 std::copy(data, data + array.size(), array.data());
132 return array;
133 }
134
135 /** Get the rotation vector between the depth and rgb sensor
136 * @return Rotation vector, 3x1 matrix
137 */
138 std::array<double, 3> get_rotation_vector() const {
139 std::array<double, 3> array;
140 auto data = tof_calibration_get_rotation_vector(this->ptr_, TOF_ERROR_HANDLER{});
141 std::copy(data, data + array.size(), array.data());
142 return array;
143 }
144
145 /** Get the translation vector between the depth and rgb sensor
146 * @return Translation vector, 3x1 matrix
147 */
148 std::array<double, 3> get_translation_vector() const {
149 std::array<double, 3> array;
150 auto data = tof_calibration_get_translation_vector(this->ptr_, TOF_ERROR_HANDLER{});
151 std::copy(data, data + array.size(), array.data());
152 return array;
153 }
154
155};
156
157/** Transform a 3D point into a 2D pixel coordinate of the depth camera
158 * @param x X value
159 * @param y Y value
160 * @param z Z value
161 * @param calibration The camera calibration
162 * @return 2D pixel coordinate of the depth camera
163 */
164inline Point2d transform_3d_to_depth_2d(float x, float y, float z, Calibration &calibration) {
165 Point2d new_point_2d(static_cast<tof_point_2d_t>(nullptr));
166 auto ptr = reinterpret_cast<tof_point_2d_t*>(&new_point_2d);
167 *ptr = tof_transform_3d_to_depth_2d(x, y, z, *reinterpret_cast<tof_calibration_t*>(&calibration), TOF_ERROR_HANDLER{});
168 return new_point_2d;
169}
170
171/** Transform a 3D point into a 2D pixel coordinate of the color camera
172 * @param x X value
173 * @param y Y value
174 * @param z Z value
175 * @param calibration The camera calibration
176 * @return 2D pixel coordinate of the color camera
177 */
178inline Point2d transform_3d_to_color_2d(float x, float y, float z, Calibration &calibration) {
179 Point2d new_point_2d(static_cast<tof_point_2d_t>(nullptr));
180 auto ptr = reinterpret_cast<tof_point_2d_t*>(&new_point_2d);
181 *ptr = tof_transform_3d_to_color_2d(x, y, z, *reinterpret_cast<tof_calibration_t*>(&calibration), TOF_ERROR_HANDLER{});
182 return new_point_2d;
183}
184
185/** Transform a 2D pixel coordinate of the color camera to the 2D pixel
186 * coordinate of the depth camera.
187 * @param row Row coordinate
188 * @param col Column cooridnate
189 * @param depth_data Depth data containing xyz information, XYZ, XYZAmp, XYZBGR
190 * and XYZBGRI are all valid
191 * @param calibration The camera calibration
192 * @return 2D pixel coordinate of the depth camera
193 */
194inline Point2d transform_color_2d_to_depth_2d(int32_t row, int32_t col, Data &depth_data, Calibration &calibration) {
195 Point2d new_point_2d(static_cast<tof_point_2d_t>(nullptr));
196 auto ptr = reinterpret_cast<tof_point_2d_t*>(&new_point_2d);
197 *ptr = tof_transform_color_2d_to_depth_2d(row, col, *reinterpret_cast<tof_data_t*>(&depth_data), *reinterpret_cast<tof_calibration_t*>(&calibration), TOF_ERROR_HANDLER{});
198 return new_point_2d;
199}
200
201} // tof
202} // chronoptics
203
204#endif
This class contains all the calibration information.
Definition: calibration.hpp:38
double get_principal_point_y() const
Get principal point y.
Definition: calibration.hpp:91
std::array< double, 3 > get_rotation_vector() const
Get the rotation vector between the depth and rgb sensor.
Calibration(tof_calibration_t ptr=nullptr)
Construct from pointer.
Definition: calibration.hpp:41
double get_focal_length_y() const
Get focal length y.
Definition: calibration.hpp:77
std::array< double, 3 > get_translation_vector() const
Get the translation vector between the depth and rgb sensor.
double get_principal_point_x() const
Get principal point x.
Definition: calibration.hpp:84
double get_focal_length_x() const
Get focal length x.
Definition: calibration.hpp:70
void write(StringView file_location) const
Write calibration to disk.
Definition: calibration.hpp:53
std::array< double, 5 > get_rgb_distortion_coefficients() const
Get the distortion coefficients of the rgb lens.
std::array< double, 9 > get_depth_camera_matrix() const
Get the camera matrix of the depth sensor and lens.
Definition: calibration.hpp:98
std::vector< double > get_depth_distortion_coefficients() const
Get the distortion coefficients of the depth lens.
std::array< double, 9 > get_rgb_camera_matrix() const
Get the camera matrix of the rgb sensor and lens.
Calibration(StringView file_location)
Read calibration from disk.
Definition: calibration.hpp:48
std::vector< float > get_calibrated_frequencies() const
The the calibrated frequencies in the calibration file.
Definition: calibration.hpp:60
2D point for indexing into array
Definition: calibration.hpp:13
int32_t row()
Get the row value.
Definition: calibration.hpp:23
Point2d(tof_point_2d_t ptr=nullptr)
Construct from pointer.
Definition: calibration.hpp:16
int32_t col()
Get the column value.
Definition: calibration.hpp:30