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