Orbbec SDK K4A Wrapper
Loading...
Searching...
No Matches
k4a.hpp
Go to the documentation of this file.
1
7#ifndef K4A_HPP
8#define K4A_HPP
9
10#include "k4a.h"
11
12#include <algorithm>
13#include <chrono>
14#include <cstdint>
15#include <limits>
16#include <stdexcept>
17#include <string>
18#include <vector>
19
20namespace k4a
21{
22
33class error : public std::runtime_error
34{
35public:
36 using runtime_error::runtime_error;
37};
38
39// Helper functions not intended for use by client code
40//
41namespace internal
42{
44
51template<typename output_type, typename input_type> output_type clamp_cast(input_type input)
52{
53 static_assert(std::is_arithmetic<input_type>::value, "clamp_cast only supports arithmetic types");
54 static_assert(std::is_arithmetic<output_type>::value, "clamp_cast only supports arithmetic types");
55 const input_type min_value = std::is_signed<input_type>() ?
56 static_cast<input_type>(std::numeric_limits<output_type>::min()) :
57 0;
58
59 input_type max_value = static_cast<input_type>(std::numeric_limits<output_type>::max());
60 if (max_value < 0)
61 {
62 // Output type is of greater or equal size to input type and we've overflowed.
63 //
64 max_value = std::numeric_limits<input_type>::max();
65 }
66 input = std::min(input, max_value);
67 input = std::max(input, min_value);
68 return static_cast<output_type>(input);
69}
71} // namespace internal
72
80class image
81{
82public:
88 image(k4a_image_t handle = nullptr) noexcept : m_handle(handle) {}
89
92 image(const image &other) noexcept : m_handle(other.m_handle)
93 {
94 if (m_handle != nullptr)
95 {
96 k4a_image_reference(m_handle);
97 }
98 }
99
102 image(image &&other) noexcept : m_handle(other.m_handle)
103 {
104 other.m_handle = nullptr;
105 }
106
107 ~image()
108 {
109 reset();
110 }
111
114 image &operator=(const image &other) noexcept
115 {
116 if (this != &other)
117 {
118 reset();
119 m_handle = other.m_handle;
120 if (m_handle != nullptr)
121 {
122 k4a_image_reference(m_handle);
123 }
124 }
125 return *this;
126 }
127
130 image &operator=(image &&other) noexcept
131 {
132 if (this != &other)
133 {
134 reset();
135 m_handle = other.m_handle;
136 other.m_handle = nullptr;
137 }
138 return *this;
139 }
140
143 image &operator=(std::nullptr_t) noexcept
144 {
145 reset();
146 return *this;
147 }
148
151 bool operator==(const image &other) const noexcept
152 {
153 return m_handle == other.m_handle;
154 }
155
158 bool operator==(std::nullptr_t) const noexcept
159 {
160 return m_handle == nullptr;
161 }
162
165 bool operator!=(const image &other) const noexcept
166 {
167 return m_handle != other.m_handle;
168 }
169
172 bool operator!=(std::nullptr_t) const noexcept
173 {
174 return m_handle != nullptr;
175 }
176
179 explicit operator bool() const noexcept
180 {
181 return is_valid();
182 }
183
186 bool is_valid() const noexcept
187 {
188 return m_handle != nullptr;
189 }
190
198 k4a_image_t handle() const noexcept
199 {
200 return m_handle;
201 }
202
205 void reset() noexcept
206 {
207 if (m_handle != nullptr)
208 {
209 k4a_image_release(m_handle);
210 m_handle = nullptr;
211 }
212 }
213
219 static image create(k4a_image_format_t format, int width_pixels, int height_pixels, int stride_bytes)
220 {
221 k4a_image_t handle = nullptr;
222 k4a_result_t result = k4a_image_create(format, width_pixels, height_pixels, stride_bytes, &handle);
223 if (K4A_RESULT_SUCCEEDED != result)
224 {
225 throw error("Failed to create image!");
226 }
227 return image(handle);
228 }
229
236 int width_pixels,
237 int height_pixels,
238 int stride_bytes,
239 uint8_t *buffer,
240 size_t buffer_size,
241 k4a_memory_destroy_cb_t *buffer_release_cb,
242 void *buffer_release_cb_context)
243 {
244 k4a_image_t handle = nullptr;
245 k4a_result_t result = k4a_image_create_from_buffer(format,
246 width_pixels,
247 height_pixels,
248 stride_bytes,
249 buffer,
250 buffer_size,
251 buffer_release_cb,
252 buffer_release_cb_context,
253 &handle);
254 if (K4A_RESULT_SUCCEEDED != result)
255 {
256 throw error("Failed to create image from buffer");
257 }
258 return image(handle);
259 }
260
265 uint8_t *get_buffer() noexcept
266 {
267 return k4a_image_get_buffer(m_handle);
268 }
269
274 const uint8_t *get_buffer() const noexcept
275 {
276 return k4a_image_get_buffer(m_handle);
277 }
278
283 size_t get_size() const noexcept
284 {
285 return k4a_image_get_size(m_handle);
286 }
287
293 {
294 return k4a_image_get_format(m_handle);
295 }
296
301 int get_width_pixels() const noexcept
302 {
303 return k4a_image_get_width_pixels(m_handle);
304 }
305
310 int get_height_pixels() const noexcept
311 {
312 return k4a_image_get_height_pixels(m_handle);
313 }
314
319 int get_stride_bytes() const noexcept
320 {
321 return k4a_image_get_stride_bytes(m_handle);
322 }
323
328 std::chrono::microseconds get_device_timestamp() const noexcept
329 {
330 return std::chrono::microseconds(k4a_image_get_device_timestamp_usec(m_handle));
331 }
332
337 std::chrono::nanoseconds get_system_timestamp() const noexcept
338 {
339 return std::chrono::nanoseconds(k4a_image_get_system_timestamp_nsec(m_handle));
340 }
341
348 std::chrono::microseconds get_exposure() const noexcept
349 {
350 return std::chrono::microseconds(k4a_image_get_exposure_usec(m_handle));
351 }
352
359 uint32_t get_white_balance() const noexcept
360 {
361 return k4a_image_get_white_balance(m_handle);
362 }
363
370 uint32_t get_iso_speed() const noexcept
371 {
372 return k4a_image_get_iso_speed(m_handle);
373 }
374
379 void set_timestamp(std::chrono::microseconds timestamp) noexcept
380 {
381 k4a_image_set_device_timestamp_usec(m_handle, internal::clamp_cast<uint64_t>(timestamp.count()));
382 }
383
390 void set_exposure_time(std::chrono::microseconds exposure) noexcept
391 {
392 k4a_image_set_exposure_usec(m_handle, internal::clamp_cast<uint64_t>(exposure.count()));
393 }
394
401 void set_white_balance(uint32_t white_balance) noexcept
402 {
403 k4a_image_set_white_balance(m_handle, white_balance);
404 }
405
412 void set_iso_speed(uint32_t iso_speed) noexcept
413 {
414 k4a_image_set_iso_speed(m_handle, iso_speed);
415 }
416
417private:
418 k4a_image_t m_handle;
419};
420
427{
428public:
434 capture(k4a_capture_t handle = nullptr) noexcept : m_handle(handle) {}
435
438 capture(const capture &other) noexcept : m_handle(other.m_handle)
439 {
440 if (m_handle != nullptr)
441 {
442 k4a_capture_reference(m_handle);
443 }
444 }
445
448 capture(capture &&other) noexcept : m_handle(other.m_handle)
449 {
450 other.m_handle = nullptr;
451 }
452
453 ~capture()
454 {
455 reset();
456 }
457
460 // TODO:
461 capture &operator=(const capture &other) noexcept
462 {
463 if (this != &other)
464 {
465 reset();
466 m_handle = other.m_handle;
467 if (m_handle != nullptr)
468 {
469 k4a_capture_reference(m_handle);
470 }
471 }
472 return *this;
473 }
474
477 capture &operator=(capture &&other) noexcept
478 {
479 if (this != &other)
480 {
481 reset();
482 m_handle = other.m_handle;
483 other.m_handle = nullptr;
484 }
485 return *this;
486 }
487
490 capture &operator=(std::nullptr_t) noexcept
491 {
492 reset();
493 return *this;
494 }
495
498 bool operator==(const capture &other) const noexcept
499 {
500 return m_handle == other.m_handle;
501 }
502
505 bool operator==(std::nullptr_t) const noexcept
506 {
507 return m_handle == nullptr;
508 }
509
512 bool operator!=(const capture &other) const noexcept
513 {
514 return m_handle != other.m_handle;
515 }
516
519 bool operator!=(std::nullptr_t) const noexcept
520 {
521 return m_handle != nullptr;
522 }
523
526 explicit operator bool() const noexcept
527 {
528 return is_valid();
529 }
530
533 bool is_valid() const noexcept
534 {
535 return m_handle != nullptr;
536 }
537
545 k4a_capture_t handle() const noexcept
546 {
547 return m_handle;
548 }
549
552 void reset() noexcept
553 {
554 if (m_handle != nullptr)
555 {
556 k4a_capture_release(m_handle);
557 m_handle = nullptr;
558 }
559 }
560
565 image get_color_image() const noexcept
566 {
567 return image(k4a_capture_get_color_image(m_handle));
568 }
569
574 image get_depth_image() const noexcept
575 {
576 return image(k4a_capture_get_depth_image(m_handle));
577 }
578
583 image get_ir_image() const noexcept
584 {
585 return image(k4a_capture_get_ir_image(m_handle));
586 }
587
592 void set_color_image(const image &color_image) noexcept
593 {
594 k4a_capture_set_color_image(m_handle, color_image.handle());
595 }
596
601 void set_depth_image(const image &depth_image) noexcept
602 {
603 k4a_capture_set_depth_image(m_handle, depth_image.handle());
604 }
605
610 void set_ir_image(const image &ir_image) noexcept
611 {
612 k4a_capture_set_ir_image(m_handle, ir_image.handle());
613 }
614
621 void set_temperature_c(float temperature_c) noexcept
622 {
623 k4a_capture_set_temperature_c(m_handle, temperature_c);
624 }
625
632 float get_temperature_c() const noexcept
633 {
634 return k4a_capture_get_temperature_c(m_handle);
635 }
636
643 {
644 k4a_capture_t handle = nullptr;
645 k4a_result_t result = k4a_capture_create(&handle);
646 if (K4A_RESULT_SUCCEEDED != result)
647 {
648 throw error("Failed to create capture!");
649 }
650 return capture(handle);
651 }
652
653private:
654 k4a_capture_t m_handle;
655};
656
663{
670 k4a_calibration_type_t source_camera,
671 k4a_calibration_type_t target_camera) const
672 {
673 k4a_float3_t target_point3d;
674 k4a_result_t result =
675 k4a_calibration_3d_to_3d(this, &source_point3d, source_camera, target_camera, &target_point3d);
676
677 if (K4A_RESULT_SUCCEEDED != result)
678 {
679 throw error("Calibration contained invalid transformation parameters!");
680 }
681 return target_point3d;
682 }
683
692 bool convert_2d_to_3d(const k4a_float2_t &source_point2d,
693 float source_depth,
694 k4a_calibration_type_t source_camera,
695 k4a_calibration_type_t target_camera,
696 k4a_float3_t *target_point3d) const
697 {
698 int valid = 0;
700 this, &source_point2d, source_depth, source_camera, target_camera, target_point3d, &valid);
701
702 if (K4A_RESULT_SUCCEEDED != result)
703 {
704 throw error("Calibration contained invalid transformation parameters!");
705 }
706 return static_cast<bool>(valid);
707 }
708
716 bool convert_3d_to_2d(const k4a_float3_t &source_point3d,
717 k4a_calibration_type_t source_camera,
718 k4a_calibration_type_t target_camera,
719 k4a_float2_t *target_point2d) const
720 {
721 int valid = 0;
722 k4a_result_t result =
723 k4a_calibration_3d_to_2d(this, &source_point3d, source_camera, target_camera, target_point2d, &valid);
724
725 if (K4A_RESULT_SUCCEEDED != result)
726 {
727 throw error("Calibration contained invalid transformation parameters!");
728 }
729 return static_cast<bool>(valid);
730 }
731
740 bool convert_2d_to_2d(const k4a_float2_t &source_point2d,
741 float source_depth,
742 k4a_calibration_type_t source_camera,
743 k4a_calibration_type_t target_camera,
744 k4a_float2_t *target_point2d) const
745 {
746 int valid = 0;
748 this, &source_point2d, source_depth, source_camera, target_camera, target_point2d, &valid);
749
750 if (K4A_RESULT_SUCCEEDED != result)
751 {
752 throw error("Calibration contained invalid transformation parameters!");
753 }
754 return static_cast<bool>(valid);
755 }
756
764 bool convert_color_2d_to_depth_2d(const k4a_float2_t &source_point2d,
765 const image &depth_image,
766 k4a_float2_t *target_point2d) const
767 {
768 int valid = 0;
769 k4a_result_t result =
770 k4a_calibration_color_2d_to_depth_2d(this, &source_point2d, depth_image.handle(), target_point2d, &valid);
771
772 if (K4A_RESULT_SUCCEEDED != result)
773 {
774 throw error("Calibration contained invalid transformation parameters!");
775 }
776 return static_cast<bool>(valid);
777 }
778
784 static calibration get_from_raw(char *raw_calibration,
785 size_t raw_calibration_size,
786 k4a_depth_mode_t target_depth_mode,
787 k4a_color_resolution_t target_color_resolution)
788 {
789 calibration calib;
790 k4a_result_t result = k4a_calibration_get_from_raw(raw_calibration,
791 raw_calibration_size,
792 target_depth_mode,
793 target_color_resolution,
794 &calib);
795
796 if (K4A_RESULT_SUCCEEDED != result)
797 {
798 throw error("Failed to load calibration from raw calibration blob!");
799 }
800 return calib;
801 }
802
808 static calibration get_from_raw(uint8_t *raw_calibration,
809 size_t raw_calibration_size,
810 k4a_depth_mode_t target_depth_mode,
811 k4a_color_resolution_t target_color_resolution)
812 {
813 return get_from_raw(reinterpret_cast<char *>(raw_calibration),
814 raw_calibration_size,
815 target_depth_mode,
816 target_color_resolution);
817 }
818
824 static calibration get_from_raw(std::vector<uint8_t> &raw_calibration,
825 k4a_depth_mode_t target_depth_mode,
826 k4a_color_resolution_t target_color_resolution)
827 {
828 return get_from_raw(reinterpret_cast<char *>(raw_calibration.data()),
829 raw_calibration.size(),
830 target_depth_mode,
831 target_color_resolution);
832 }
833};
834
841{
842public:
848 m_handle(k4a_transformation_create(&calibration)),
853 {
854 }
855
861 transformation(k4a_transformation_t handle = nullptr) noexcept : m_handle(handle) {}
862
865 transformation(transformation &&other) noexcept :
866 m_handle(other.m_handle),
867 m_color_resolution(other.m_color_resolution),
868 m_depth_resolution(other.m_depth_resolution)
869 {
870 other.m_handle = nullptr;
871 }
872
873 transformation(const transformation &) = delete;
874
876 {
877 destroy();
878 }
879
883 {
884 if (this != &other)
885 {
886 destroy();
887 m_handle = other.m_handle;
888 m_color_resolution = other.m_color_resolution;
889 m_depth_resolution = other.m_depth_resolution;
890 other.m_handle = nullptr;
891 }
892
893 return *this;
894 }
895
898 transformation &operator=(std::nullptr_t) noexcept
899 {
900 destroy();
901 return *this;
902 }
903
904 transformation &operator=(const transformation &) = delete;
905
908 void destroy() noexcept
909 {
910 if (m_handle != nullptr)
911 {
912 k4a_transformation_destroy(m_handle);
913 m_handle = nullptr;
914 }
915 }
916
923 void depth_image_to_color_camera(const image &depth_image, image *transformed_depth_image) const
924 {
925 k4a_result_t result = k4a_transformation_depth_image_to_color_camera(m_handle,
926 depth_image.handle(),
927 transformed_depth_image->handle());
928 if (K4A_RESULT_SUCCEEDED != result)
929 {
930 throw error("Failed to convert depth map to color camera geometry!");
931 }
932 }
933
940 image depth_image_to_color_camera(const image &depth_image) const
941 {
942 image transformed_depth_image = image::create(K4A_IMAGE_FORMAT_DEPTH16,
943 m_color_resolution.width,
944 m_color_resolution.height,
945 m_color_resolution.width *
946 static_cast<int32_t>(sizeof(uint16_t)));
947 depth_image_to_color_camera(depth_image, &transformed_depth_image);
948 return transformed_depth_image;
949 }
950
958 const image &custom_image,
959 image *transformed_depth_image,
960 image *transformed_custom_image,
962 uint32_t invalid_custom_value) const
963 {
964 k4a_result_t result = k4a_transformation_depth_image_to_color_camera_custom(m_handle,
965 depth_image.handle(),
966 custom_image.handle(),
967 transformed_depth_image->handle(),
968 transformed_custom_image->handle(),
969 interpolation_type,
970 invalid_custom_value);
971 if (K4A_RESULT_SUCCEEDED != result)
972 {
973 throw error("Failed to convert depth map and custom image to color camera geometry!");
974 }
975 }
976
983 std::pair<image, image>
985 const image &custom_image,
987 uint32_t invalid_custom_value) const
988 {
989 image transformed_depth_image = image::create(K4A_IMAGE_FORMAT_DEPTH16,
990 m_color_resolution.width,
991 m_color_resolution.height,
992 m_color_resolution.width *
993 static_cast<int32_t>(sizeof(uint16_t)));
994 int32_t bytes_per_pixel;
995 switch (custom_image.get_format())
996 {
998 bytes_per_pixel = static_cast<int32_t>(sizeof(int8_t));
999 break;
1001 bytes_per_pixel = static_cast<int32_t>(sizeof(int16_t));
1002 break;
1003 default:
1004 throw error("Failed to support this format of custom image!");
1005 }
1006 image transformed_custom_image = image::create(custom_image.get_format(),
1007 m_color_resolution.width,
1008 m_color_resolution.height,
1009 m_color_resolution.width * bytes_per_pixel);
1011 custom_image,
1012 &transformed_depth_image,
1013 &transformed_custom_image,
1014 interpolation_type,
1015 invalid_custom_value);
1016 return { std::move(transformed_depth_image), std::move(transformed_custom_image) };
1017 }
1018
1025 void color_image_to_depth_camera(const image &depth_image,
1026 const image &color_image,
1027 image *transformed_color_image) const
1028 {
1029 k4a_result_t result = k4a_transformation_color_image_to_depth_camera(m_handle,
1030 depth_image.handle(),
1031 color_image.handle(),
1032 transformed_color_image->handle());
1033 if (K4A_RESULT_SUCCEEDED != result)
1034 {
1035 throw error("Failed to convert color image to depth camera geometry!");
1036 }
1037 }
1038
1045 image color_image_to_depth_camera(const image &depth_image, const image &color_image) const
1046 {
1047 image transformed_color_image = image::create(K4A_IMAGE_FORMAT_COLOR_BGRA32,
1048 m_depth_resolution.width,
1049 m_depth_resolution.height,
1050 m_depth_resolution.width * 4 *
1051 static_cast<int32_t>(sizeof(uint8_t)));
1052 color_image_to_depth_camera(depth_image, color_image, &transformed_color_image);
1053 return transformed_color_image;
1054 }
1055
1062 void depth_image_to_point_cloud(const image &depth_image, k4a_calibration_type_t camera, image *xyz_image) const
1063 {
1064 k4a_result_t result =
1065 k4a_transformation_depth_image_to_point_cloud(m_handle, depth_image.handle(), camera, xyz_image->handle());
1066 if (K4A_RESULT_SUCCEEDED != result)
1067 {
1068 throw error("Failed to transform depth image to point cloud!");
1069 }
1070 }
1071
1079 {
1081 depth_image.get_width_pixels(),
1082 depth_image.get_height_pixels(),
1083 depth_image.get_width_pixels() * 3 * static_cast<int32_t>(sizeof(int16_t)));
1084 depth_image_to_point_cloud(depth_image, camera, &xyz_image);
1085 return xyz_image;
1086 }
1087
1088private:
1089 k4a_transformation_t m_handle;
1090 struct resolution
1091 {
1092 int32_t width;
1093 int32_t height;
1094 };
1095 resolution m_color_resolution;
1096 resolution m_depth_resolution;
1097};
1098
1105{
1106public:
1112 device(k4a_device_t handle = nullptr) noexcept : m_handle(handle) {}
1113
1116 device(device &&dev) noexcept : m_handle(dev.m_handle)
1117 {
1118 dev.m_handle = nullptr;
1119 }
1120
1121 device(const device &) = delete;
1122
1123 ~device()
1124 {
1125 close();
1126 }
1127
1128 device &operator=(const device &) = delete;
1129
1132 device &operator=(device &&dev) noexcept
1133 {
1134 if (this != &dev)
1135 {
1136 close();
1137 m_handle = dev.m_handle;
1138 dev.m_handle = nullptr;
1139 }
1140 return *this;
1141 }
1142
1145 explicit operator bool() const noexcept
1146 {
1147 return is_valid();
1148 }
1149
1152 bool is_valid() const noexcept
1153 {
1154 return m_handle != nullptr;
1155 }
1156
1162 k4a_device_t handle() const noexcept
1163 {
1164 return m_handle;
1165 }
1166
1171 void close() noexcept
1172 {
1173 if (m_handle != nullptr)
1174 {
1175 k4a_device_close(m_handle);
1176 m_handle = nullptr;
1177 }
1178 }
1179
1185 bool get_capture(capture *cap, std::chrono::milliseconds timeout)
1186 {
1187 k4a_capture_t capture_handle = nullptr;
1188 int32_t timeout_ms = internal::clamp_cast<int32_t>(timeout.count());
1189 k4a_wait_result_t result = k4a_device_get_capture(m_handle, &capture_handle, timeout_ms);
1190 if (result == K4A_WAIT_RESULT_FAILED)
1191 {
1192 throw error("Failed to get capture from device!");
1193 }
1194 else if (result == K4A_WAIT_RESULT_TIMEOUT)
1195 {
1196 return false;
1197 }
1198
1199 *cap = capture(capture_handle);
1200 return true;
1201 }
1202
1209 {
1210 return get_capture(cap, std::chrono::milliseconds(K4A_WAIT_INFINITE));
1211 }
1212
1218 bool get_imu_sample(k4a_imu_sample_t *imu_sample, std::chrono::milliseconds timeout)
1219 {
1220 int32_t timeout_ms = internal::clamp_cast<int32_t>(timeout.count());
1221 k4a_wait_result_t result = k4a_device_get_imu_sample(m_handle, imu_sample, timeout_ms);
1222 if (result == K4A_WAIT_RESULT_FAILED)
1223 {
1224 throw error("Failed to get IMU sample from device!");
1225 }
1226 else if (result == K4A_WAIT_RESULT_TIMEOUT)
1227 {
1228 return false;
1229 }
1230
1231 return true;
1232 }
1233
1240 {
1241 return get_imu_sample(imu_sample, std::chrono::milliseconds(K4A_WAIT_INFINITE));
1242 }
1243
1250 {
1251 k4a_result_t result = k4a_device_start_cameras(m_handle, configuration);
1252 if (K4A_RESULT_SUCCEEDED != result)
1253 {
1254 throw error("Failed to start cameras!");
1255 }
1256 }
1257
1262 void stop_cameras() noexcept
1263 {
1264 k4a_device_stop_cameras(m_handle);
1265 }
1266
1273 {
1274 k4a_result_t result = k4a_device_start_imu(m_handle);
1275 if (K4A_RESULT_SUCCEEDED != result)
1276 {
1277 throw error("Failed to start IMU!");
1278 }
1279 }
1280
1285 void stop_imu() noexcept
1286 {
1287 k4a_device_stop_imu(m_handle);
1288 }
1289
1295 std::string get_serialnum() const
1296 {
1297 std::string serialnum;
1298 size_t buffer = 0;
1299 k4a_buffer_result_t result = k4a_device_get_serialnum(m_handle, &serialnum[0], &buffer);
1300
1301 if (result == K4A_BUFFER_RESULT_TOO_SMALL && buffer > 1)
1302 {
1303 serialnum.resize(buffer);
1304 result = k4a_device_get_serialnum(m_handle, &serialnum[0], &buffer);
1305 if (result == K4A_BUFFER_RESULT_SUCCEEDED && serialnum[buffer - 1] == 0)
1306 {
1307 // std::string expects there to not be as null terminator at the end of its data but
1308 // k4a_device_get_serialnum adds a null terminator, so we drop the last character of the string after we
1309 // get the result back.
1310 serialnum.resize(buffer - 1);
1311 }
1312 }
1313
1314 if (result != K4A_BUFFER_RESULT_SUCCEEDED)
1315 {
1316 throw error("Failed to read device serial number!");
1317 }
1318
1319 return serialnum;
1320 }
1321
1328 {
1329 k4a_result_t result = k4a_device_get_color_control(m_handle, command, mode, value);
1330 if (K4A_RESULT_SUCCEEDED != result)
1331 {
1332 throw error("Failed to read color control!");
1333 }
1334 }
1335
1342 {
1343 k4a_result_t result = k4a_device_set_color_control(m_handle, command, mode, value);
1344 if (K4A_RESULT_SUCCEEDED != result)
1345 {
1346 throw error("Failed to set color control!");
1347 }
1348 }
1349
1355 std::vector<uint8_t> get_raw_calibration() const
1356 {
1357 std::vector<uint8_t> calibration;
1358 size_t buffer = 0;
1359 k4a_buffer_result_t result = k4a_device_get_raw_calibration(m_handle, nullptr, &buffer);
1360
1361 if (result == K4A_BUFFER_RESULT_TOO_SMALL && buffer > 1)
1362 {
1363 calibration.resize(buffer);
1364 result = k4a_device_get_raw_calibration(m_handle, &calibration[0], &buffer);
1365 }
1366
1367 if (result != K4A_BUFFER_RESULT_SUCCEEDED)
1368 {
1369 throw error("Failed to read raw device calibration!");
1370 }
1371
1372 return calibration;
1373 }
1374
1381 {
1382 calibration calib;
1383 k4a_result_t result = k4a_device_get_calibration(m_handle, depth_mode, color_resolution, &calib);
1384
1385 if (K4A_RESULT_SUCCEEDED != result)
1386 {
1387 throw error("Failed to read device calibration!");
1388 }
1389 return calib;
1390 }
1391
1401 {
1402 bool sync_in_jack_connected, sync_out_jack_connected;
1403 k4a_result_t result = k4a_device_get_sync_jack(m_handle, &sync_in_jack_connected, &sync_out_jack_connected);
1404
1405 if (K4A_RESULT_SUCCEEDED != result)
1406 {
1407 throw error("Failed to read sync jack status!");
1408 }
1409 return sync_in_jack_connected;
1410 }
1411
1421 {
1422 bool sync_in_jack_connected, sync_out_jack_connected;
1423 k4a_result_t result = k4a_device_get_sync_jack(m_handle, &sync_in_jack_connected, &sync_out_jack_connected);
1424
1425 if (K4A_RESULT_SUCCEEDED != result)
1426 {
1427 throw error("Failed to read sync jack status!");
1428 }
1429 return sync_out_jack_connected;
1430 }
1431
1438 {
1439 k4a_hardware_version_t version;
1440 k4a_result_t result = k4a_device_get_version(m_handle, &version);
1441
1442 if (K4A_RESULT_SUCCEEDED != result)
1443 {
1444 throw error("Failed to read device firmware information!");
1445 }
1446 return version;
1447 }
1448
1454 static device open(uint32_t index)
1455 {
1456 k4a_device_t handle = nullptr;
1457 k4a_result_t result = k4a_device_open(index, &handle);
1458
1459 if (K4A_RESULT_SUCCEEDED != result)
1460 {
1461 throw error("Failed to open device!");
1462 }
1463 return device(handle);
1464 }
1465
1470 static uint32_t get_installed_count() noexcept
1471 {
1472 return k4a_device_get_installed_count();
1473 }
1474
1475private:
1476 k4a_device_t m_handle;
1477};
1478
1483} // namespace k4a
1484
1485#endif
bool operator!=(std::nullptr_t) const noexcept
Definition: k4a.hpp:519
bool operator!=(const capture &other) const noexcept
Definition: k4a.hpp:512
image get_depth_image() const noexcept
Definition: k4a.hpp:574
void set_temperature_c(float temperature_c) noexcept
Definition: k4a.hpp:621
float get_temperature_c() const noexcept
Definition: k4a.hpp:632
capture & operator=(const capture &other) noexcept
Definition: k4a.hpp:461
k4a_capture_t handle() const noexcept
Definition: k4a.hpp:545
void set_depth_image(const image &depth_image) noexcept
Definition: k4a.hpp:601
bool operator==(std::nullptr_t) const noexcept
Definition: k4a.hpp:505
capture(const capture &other) noexcept
Definition: k4a.hpp:438
capture(k4a_capture_t handle=nullptr) noexcept
Definition: k4a.hpp:434
capture & operator=(capture &&other) noexcept
Definition: k4a.hpp:477
image get_color_image() const noexcept
Definition: k4a.hpp:565
static capture create()
Definition: k4a.hpp:642
capture(capture &&other) noexcept
Definition: k4a.hpp:448
image get_ir_image() const noexcept
Definition: k4a.hpp:583
bool operator==(const capture &other) const noexcept
Definition: k4a.hpp:498
void reset() noexcept
Definition: k4a.hpp:552
bool is_valid() const noexcept
Definition: k4a.hpp:533
void set_ir_image(const image &ir_image) noexcept
Definition: k4a.hpp:610
void set_color_image(const image &color_image) noexcept
Definition: k4a.hpp:592
capture & operator=(std::nullptr_t) noexcept
Definition: k4a.hpp:490
static device open(uint32_t index)
Definition: k4a.hpp:1454
device(k4a_device_t handle=nullptr) noexcept
Definition: k4a.hpp:1112
void close() noexcept
Definition: k4a.hpp:1171
bool get_capture(capture *cap)
Definition: k4a.hpp:1208
void stop_imu() noexcept
Definition: k4a.hpp:1285
device & operator=(device &&dev) noexcept
Definition: k4a.hpp:1132
std::string get_serialnum() const
Definition: k4a.hpp:1295
void get_color_control(k4a_color_control_command_t command, k4a_color_control_mode_t *mode, int32_t *value) const
Definition: k4a.hpp:1327
void start_cameras(const k4a_device_configuration_t *configuration)
Definition: k4a.hpp:1249
void stop_cameras() noexcept
Definition: k4a.hpp:1262
k4a_hardware_version_t get_version() const
Definition: k4a.hpp:1437
bool get_capture(capture *cap, std::chrono::milliseconds timeout)
Definition: k4a.hpp:1185
bool get_imu_sample(k4a_imu_sample_t *imu_sample)
Definition: k4a.hpp:1239
bool get_imu_sample(k4a_imu_sample_t *imu_sample, std::chrono::milliseconds timeout)
Definition: k4a.hpp:1218
k4a_device_t handle() const noexcept
Definition: k4a.hpp:1162
std::vector< uint8_t > get_raw_calibration() const
Definition: k4a.hpp:1355
device(device &&dev) noexcept
Definition: k4a.hpp:1116
static uint32_t get_installed_count() noexcept
Definition: k4a.hpp:1470
void start_imu()
Definition: k4a.hpp:1272
calibration get_calibration(k4a_depth_mode_t depth_mode, k4a_color_resolution_t color_resolution) const
Definition: k4a.hpp:1380
bool is_sync_in_connected() const
Definition: k4a.hpp:1400
bool is_sync_out_connected() const
Definition: k4a.hpp:1420
bool is_valid() const noexcept
Definition: k4a.hpp:1152
void set_color_control(k4a_color_control_command_t command, k4a_color_control_mode_t mode, int32_t value)
Definition: k4a.hpp:1341
std::chrono::microseconds get_exposure() const noexcept
Definition: k4a.hpp:348
uint8_t * get_buffer() noexcept
Definition: k4a.hpp:265
static image create_from_buffer(k4a_image_format_t format, int width_pixels, int height_pixels, int stride_bytes, uint8_t *buffer, size_t buffer_size, k4a_memory_destroy_cb_t *buffer_release_cb, void *buffer_release_cb_context)
Definition: k4a.hpp:235
std::chrono::microseconds get_device_timestamp() const noexcept
Definition: k4a.hpp:328
int get_width_pixels() const noexcept
Definition: k4a.hpp:301
const uint8_t * get_buffer() const noexcept
Definition: k4a.hpp:274
void set_white_balance(uint32_t white_balance) noexcept
Definition: k4a.hpp:401
void reset() noexcept
Definition: k4a.hpp:205
int get_stride_bytes() const noexcept
Definition: k4a.hpp:319
uint32_t get_iso_speed() const noexcept
Definition: k4a.hpp:370
size_t get_size() const noexcept
Definition: k4a.hpp:283
image & operator=(std::nullptr_t) noexcept
Definition: k4a.hpp:143
bool operator!=(std::nullptr_t) const noexcept
Definition: k4a.hpp:172
static image create(k4a_image_format_t format, int width_pixels, int height_pixels, int stride_bytes)
Definition: k4a.hpp:219
int get_height_pixels() const noexcept
Definition: k4a.hpp:310
void set_timestamp(std::chrono::microseconds timestamp) noexcept
Definition: k4a.hpp:379
k4a_image_t handle() const noexcept
Definition: k4a.hpp:198
image & operator=(image &&other) noexcept
Definition: k4a.hpp:130
bool is_valid() const noexcept
Definition: k4a.hpp:186
uint32_t get_white_balance() const noexcept
Definition: k4a.hpp:359
bool operator!=(const image &other) const noexcept
Definition: k4a.hpp:165
std::chrono::nanoseconds get_system_timestamp() const noexcept
Definition: k4a.hpp:337
bool operator==(std::nullptr_t) const noexcept
Definition: k4a.hpp:158
k4a_image_format_t get_format() const noexcept
Definition: k4a.hpp:292
void set_iso_speed(uint32_t iso_speed) noexcept
Definition: k4a.hpp:412
image(k4a_image_t handle=nullptr) noexcept
Definition: k4a.hpp:88
image(image &&other) noexcept
Definition: k4a.hpp:102
image & operator=(const image &other) noexcept
Definition: k4a.hpp:114
void set_exposure_time(std::chrono::microseconds exposure) noexcept
Definition: k4a.hpp:390
bool operator==(const image &other) const noexcept
Definition: k4a.hpp:151
image(const image &other) noexcept
Definition: k4a.hpp:92
void destroy() noexcept
Definition: k4a.hpp:908
transformation & operator=(transformation &&other) noexcept
Definition: k4a.hpp:882
image color_image_to_depth_camera(const image &depth_image, const image &color_image) const
Definition: k4a.hpp:1045
std::pair< image, image > depth_image_to_color_camera_custom(const image &depth_image, const image &custom_image, k4a_transformation_interpolation_type_t interpolation_type, uint32_t invalid_custom_value) const
Definition: k4a.hpp:984
transformation(k4a_transformation_t handle=nullptr) noexcept
Definition: k4a.hpp:861
void depth_image_to_color_camera_custom(const image &depth_image, const image &custom_image, image *transformed_depth_image, image *transformed_custom_image, k4a_transformation_interpolation_type_t interpolation_type, uint32_t invalid_custom_value) const
Definition: k4a.hpp:957
void depth_image_to_color_camera(const image &depth_image, image *transformed_depth_image) const
Definition: k4a.hpp:923
transformation(transformation &&other) noexcept
Definition: k4a.hpp:865
transformation & operator=(std::nullptr_t) noexcept
Definition: k4a.hpp:898
transformation(const k4a_calibration_t &calibration) noexcept
Definition: k4a.hpp:847
image depth_image_to_color_camera(const image &depth_image) const
Definition: k4a.hpp:940
void depth_image_to_point_cloud(const image &depth_image, k4a_calibration_type_t camera, image *xyz_image) const
Definition: k4a.hpp:1062
void color_image_to_depth_camera(const image &depth_image, const image &color_image, image *transformed_color_image) const
Definition: k4a.hpp:1025
image depth_image_to_point_cloud(const image &depth_image, k4a_calibration_type_t camera) const
Definition: k4a.hpp:1078
#define K4A_WAIT_INFINITE
Definition: k4atypes.h:1244
k4a_transformation_interpolation_type_t
Definition: k4atypes.h:460
k4a_color_control_mode_t
Definition: k4atypes.h:626
k4a_depth_mode_t
Definition: k4atypes.h:291
k4a_wait_result_t
Definition: k4atypes.h:247
k4a_result_t
Definition: k4atypes.h:218
k4a_buffer_result_t
Definition: k4atypes.h:232
k4a_calibration_type_t
Definition: k4atypes.h:662
k4a_color_resolution_t
Definition: k4atypes.h:310
k4a_image_format_t
Definition: k4atypes.h:333
k4a_color_control_command_t
Definition: k4atypes.h:507
@ K4A_WAIT_RESULT_TIMEOUT
Definition: k4atypes.h:250
@ K4A_WAIT_RESULT_FAILED
Definition: k4atypes.h:249
@ K4A_RESULT_SUCCEEDED
Definition: k4atypes.h:219
@ K4A_BUFFER_RESULT_TOO_SMALL
Definition: k4atypes.h:235
@ K4A_BUFFER_RESULT_SUCCEEDED
Definition: k4atypes.h:233
@ K4A_IMAGE_FORMAT_CUSTOM
Definition: k4atypes.h:445
@ K4A_IMAGE_FORMAT_CUSTOM8
Definition: k4atypes.h:424
@ K4A_IMAGE_FORMAT_DEPTH16
Definition: k4atypes.h:398
@ K4A_IMAGE_FORMAT_CUSTOM16
Definition: k4atypes.h:435
@ K4A_IMAGE_FORMAT_COLOR_BGRA32
Definition: k4atypes.h:386
K4A_EXPORT k4a_result_t k4a_calibration_2d_to_2d(const k4a_calibration_t *calibration, const k4a_float2_t *source_point2d, const float source_depth_mm, const k4a_calibration_type_t source_camera, const k4a_calibration_type_t target_camera, k4a_float2_t *target_point2d, int *valid)
K4A_EXPORT k4a_result_t k4a_calibration_3d_to_2d(const k4a_calibration_t *calibration, const k4a_float3_t *source_point3d_mm, const k4a_calibration_type_t source_camera, const k4a_calibration_type_t target_camera, k4a_float2_t *target_point2d, int *valid)
K4A_EXPORT k4a_result_t k4a_calibration_3d_to_3d(const k4a_calibration_t *calibration, const k4a_float3_t *source_point3d_mm, const k4a_calibration_type_t source_camera, const k4a_calibration_type_t target_camera, k4a_float3_t *target_point3d_mm)
K4A_EXPORT k4a_result_t k4a_calibration_color_2d_to_depth_2d(const k4a_calibration_t *calibration, const k4a_float2_t *source_point2d, const k4a_image_t depth_image, k4a_float2_t *target_point2d, int *valid)
K4A_EXPORT k4a_result_t k4a_calibration_2d_to_3d(const k4a_calibration_t *calibration, const k4a_float2_t *source_point2d, const float source_depth_mm, const k4a_calibration_type_t source_camera, const k4a_calibration_type_t target_camera, k4a_float3_t *target_point3d_mm, int *valid)
void() k4a_memory_destroy_cb_t(void *buffer, void *context)
Definition: k4atypes.h:871
k4a_calibration_camera_t color_camera_calibration
Definition: k4atypes.h:1107
k4a_calibration_camera_t depth_camera_calibration
Definition: k4atypes.h:1105
static calibration get_from_raw(std::vector< uint8_t > &raw_calibration, k4a_depth_mode_t target_depth_mode, k4a_color_resolution_t target_color_resolution)
Definition: k4a.hpp:824
static calibration get_from_raw(uint8_t *raw_calibration, size_t raw_calibration_size, k4a_depth_mode_t target_depth_mode, k4a_color_resolution_t target_color_resolution)
Definition: k4a.hpp:808
k4a_float3_t convert_3d_to_3d(const k4a_float3_t &source_point3d, k4a_calibration_type_t source_camera, k4a_calibration_type_t target_camera) const
Definition: k4a.hpp:669
bool convert_3d_to_2d(const k4a_float3_t &source_point3d, k4a_calibration_type_t source_camera, k4a_calibration_type_t target_camera, k4a_float2_t *target_point2d) const
Definition: k4a.hpp:716
bool convert_2d_to_3d(const k4a_float2_t &source_point2d, float source_depth, k4a_calibration_type_t source_camera, k4a_calibration_type_t target_camera, k4a_float3_t *target_point3d) const
Definition: k4a.hpp:692
static calibration get_from_raw(char *raw_calibration, size_t raw_calibration_size, k4a_depth_mode_t target_depth_mode, k4a_color_resolution_t target_color_resolution)
Definition: k4a.hpp:784
bool convert_color_2d_to_depth_2d(const k4a_float2_t &source_point2d, const image &depth_image, k4a_float2_t *target_point2d) const
Definition: k4a.hpp:764
bool convert_2d_to_2d(const k4a_float2_t &source_point2d, float source_depth, k4a_calibration_type_t source_camera, k4a_calibration_type_t target_camera, k4a_float2_t *target_point2d) const
Definition: k4a.hpp:740