forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.h
1683 lines (1410 loc) · 52.8 KB
/
types.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2015 Intel Corporation. All Rights Reserved.
// This header defines vocabulary types and utility mechanisms used ubiquitously by the
// rest of the library. As clearer module boundaries form, declarations might be moved
// out of this file and into more appropriate locations.
#pragma once
#ifndef LIBREALSENSE_TYPES_H
#define LIBREALSENSE_TYPES_H
#include "../include/librealsense2/hpp/rs_types.hpp"
#include <stdint.h>
#include <cassert> // For assert
#include <cstring> // For memcmp
#include <vector> // For vector
#include <sstream> // For ostringstream
#include <mutex> // For mutex, unique_lock
#include <memory> // For unique_ptr
#include <map>
#include <limits>
#include <algorithm>
#include <condition_variable>
#include <functional>
#include <utility> // For std::forward
#include "backend.h"
#include "concurrency.h"
#if BUILD_EASYLOGGINGPP
#include "../third-party/easyloggingpp/src/easylogging++.h"
#endif // BUILD_EASYLOGGINGPP
typedef unsigned char byte;
const int RS2_USER_QUEUE_SIZE = 128;
#ifndef DBL_EPSILON
const double DBL_EPSILON = 2.2204460492503131e-016; // smallest such that 1.0+DBL_EPSILON != 1.0
#endif
#pragma warning(disable: 4250)
#ifdef ANDROID
#include "../common/android_helpers.h"
#endif
namespace librealsense
{
#define UNKNOWN_VALUE "UNKNOWN"
const double TIMESTAMP_USEC_TO_MSEC = 0.001;
///////////////////////////////////
// Utility types for general use //
///////////////////////////////////
struct to_string
{
std::ostringstream ss;
template<class T> to_string & operator << (const T & val) { ss << val; return *this; }
operator std::string() const { return ss.str(); }
};
template<typename T, size_t size>
inline size_t copy_array(T(&dst)[size], const T(&src)[size])
{
assert(dst != nullptr && src != nullptr);
for (size_t i = 0; i < size; i++)
{
dst[i] = src[i];
}
return size;
}
template<typename T, size_t sizem, size_t sizen>
inline size_t copy_2darray(T(&dst)[sizem][sizen], const T(&src)[sizem][sizen])
{
assert(dst != nullptr && src != nullptr);
for (size_t i = 0; i < sizem; i++)
{
for (size_t j = 0; j < sizen; j++)
{
dst[i][j] = src[i][j];
}
}
return sizem * sizen;
}
void copy(void* dst, void const* src, size_t size);
std::string make_less_screamy(const char* str);
///////////////////////
// Logging mechanism //
///////////////////////
void log_to_console(rs2_log_severity min_severity);
void log_to_file(rs2_log_severity min_severity, const char * file_path);
#if BUILD_EASYLOGGINGPP
#define LOG_DEBUG(...) do { CLOG(DEBUG ,"librealsense") << __VA_ARGS__; } while(false)
#define LOG_INFO(...) do { CLOG(INFO ,"librealsense") << __VA_ARGS__; } while(false)
#define LOG_WARNING(...) do { CLOG(WARNING ,"librealsense") << __VA_ARGS__; } while(false)
#define LOG_ERROR(...) do { CLOG(ERROR ,"librealsense") << __VA_ARGS__; } while(false)
#define LOG_FATAL(...) do { CLOG(FATAL ,"librealsense") << __VA_ARGS__; } while(false)
#else // BUILD_EASYLOGGINGPP
#define LOG_DEBUG(...) do { ; } while(false)
#define LOG_INFO(...) do { ; } while(false)
#define LOG_WARNING(...) do { ; } while(false)
#define LOG_ERROR(...) do { ; } while(false)
#define LOG_FATAL(...) do { ; } while(false)
#endif // BUILD_EASYLOGGINGPP
// Enhancement for debug mode that incurs performance penalty with STL
// std::clamp to be introduced with c++17
template< typename T>
inline T clamp_val(T val, const T& min, const T& max)
{
static_assert((std::is_arithmetic<T>::value), "clamping supports arithmetic built-in types only");
#ifdef _DEBUG
const T t = val < min ? min : val;
return t > max ? max : t;
#else
return std::min(std::max(val, min), max);
#endif
}
//////////////////////////
// Exceptions mechanism //
//////////////////////////
class librealsense_exception : public std::exception
{
public:
const char* get_message() const noexcept
{
return _msg.c_str();
}
rs2_exception_type get_exception_type() const noexcept
{
return _exception_type;
}
const char* what() const noexcept override
{
return _msg.c_str();
}
protected:
librealsense_exception(const std::string& msg,
rs2_exception_type exception_type) noexcept
: _msg(msg),
_exception_type(exception_type)
{}
private:
std::string _msg;
rs2_exception_type _exception_type;
};
class recoverable_exception : public librealsense_exception
{
public:
recoverable_exception(const std::string& msg,
rs2_exception_type exception_type) noexcept;
};
class unrecoverable_exception : public librealsense_exception
{
public:
unrecoverable_exception(const std::string& msg,
rs2_exception_type exception_type) noexcept
: librealsense_exception(msg, exception_type)
{
LOG_ERROR(msg);
}
};
class io_exception : public unrecoverable_exception
{
public:
io_exception(const std::string& msg) noexcept
: unrecoverable_exception(msg, RS2_EXCEPTION_TYPE_IO)
{}
};
class camera_disconnected_exception : public unrecoverable_exception
{
public:
camera_disconnected_exception(const std::string& msg) noexcept
: unrecoverable_exception(msg, RS2_EXCEPTION_TYPE_CAMERA_DISCONNECTED)
{}
};
class backend_exception : public unrecoverable_exception
{
public:
backend_exception(const std::string& msg,
rs2_exception_type exception_type) noexcept
: unrecoverable_exception(msg, exception_type)
{}
};
class linux_backend_exception : public backend_exception
{
public:
linux_backend_exception(const std::string& msg) noexcept
: backend_exception(generate_last_error_message(msg), RS2_EXCEPTION_TYPE_BACKEND)
{}
private:
std::string generate_last_error_message(const std::string& msg) const
{
return msg + " Last Error: " + strerror(errno);
}
};
class windows_backend_exception : public backend_exception
{
public:
// TODO: get last error
windows_backend_exception(const std::string& msg) noexcept
: backend_exception(msg, RS2_EXCEPTION_TYPE_BACKEND)
{}
};
class invalid_value_exception : public recoverable_exception
{
public:
invalid_value_exception(const std::string& msg) noexcept
: recoverable_exception(msg, RS2_EXCEPTION_TYPE_INVALID_VALUE)
{}
};
class wrong_api_call_sequence_exception : public recoverable_exception
{
public:
wrong_api_call_sequence_exception(const std::string& msg) noexcept
: recoverable_exception(msg, RS2_EXCEPTION_TYPE_WRONG_API_CALL_SEQUENCE)
{}
};
class not_implemented_exception : public recoverable_exception
{
public:
not_implemented_exception(const std::string& msg) noexcept
: recoverable_exception(msg, RS2_EXCEPTION_TYPE_NOT_IMPLEMENTED)
{}
};
#pragma pack(push, 1)
template<class T> class big_endian
{
T be_value;
public:
operator T () const
{
T le_value = 0;
for (unsigned int i = 0; i < sizeof(T); ++i) reinterpret_cast<char *>(&le_value)[i] = reinterpret_cast<const char *>(&be_value)[sizeof(T) - i - 1];
return le_value;
}
};
#pragma pack(pop)
template <class T>
class lazy
{
public:
lazy() : _init([]() { T t{}; return t; }) {}
lazy(std::function<T()> initializer) : _init(std::move(initializer)) {}
T* operator->() const
{
return operate();
}
T& operator*()
{
return *operate();
}
const T& operator*() const
{
return *operate();
}
lazy(lazy&& other) noexcept
{
std::lock_guard<std::mutex> lock(other._mtx);
if (!other._was_init)
{
_init = move(other._init);
_was_init = false;
}
else
{
_init = move(other._init);
_was_init = true;
_ptr = move(other._ptr);
}
}
lazy& operator=(std::function<T()> func) noexcept
{
return *this = lazy<T>(std::move(func));
}
lazy& operator=(lazy&& other) noexcept
{
std::lock_guard<std::mutex> lock1(_mtx);
std::lock_guard<std::mutex> lock2(other._mtx);
if (!other._was_init)
{
_init = move(other._init);
_was_init = false;
}
else
{
_init = move(other._init);
_was_init = true;
_ptr = move(other._ptr);
}
return *this;
}
private:
T* operate() const
{
std::lock_guard<std::mutex> lock(_mtx);
if (!_was_init)
{
_ptr = std::unique_ptr<T>(new T(_init()));
_was_init = true;
}
return _ptr.get();
}
mutable std::mutex _mtx;
mutable bool _was_init = false;
std::function<T()> _init;
mutable std::unique_ptr<T> _ptr;
};
class unique_id
{
public:
static uint64_t generate_id()
{
static std::atomic<uint64_t> id(0);
return ++id;
}
unique_id(const unique_id&) = delete;
unique_id& operator=(const unique_id&) = delete;
};
template<typename T, int sz>
int arr_size(T(&)[sz])
{
return sz;
}
template<typename T>
std::string array2str(T& data)
{
std::stringstream ss;
for (auto i = 0; i < arr_size(data); i++)
ss << " [" << i << "] = " << data[i] << "\t";
return ss.str();
}
typedef float float_4[4];
/////////////////////////////
// Enumerated type support //
/////////////////////////////
#define RS2_ENUM_HELPERS(TYPE, PREFIX) const char* get_string(TYPE value); \
inline bool is_valid(TYPE value) { return value >= 0 && value < RS2_##PREFIX##_COUNT; } \
inline std::ostream & operator << (std::ostream & out, TYPE value) { if(is_valid(value)) return out << get_string(value); else return out << (int)value; } \
inline bool try_parse(const std::string& str, TYPE& res) \
{ \
for (int i = 0; i < static_cast<int>(RS2_ ## PREFIX ## _COUNT); i++) { \
auto v = static_cast<TYPE>(i); \
if(str == get_string(v)) { res = v; return true; } \
} \
return false; \
}
RS2_ENUM_HELPERS(rs2_stream, STREAM)
RS2_ENUM_HELPERS(rs2_format, FORMAT)
RS2_ENUM_HELPERS(rs2_distortion, DISTORTION)
RS2_ENUM_HELPERS(rs2_option, OPTION)
RS2_ENUM_HELPERS(rs2_camera_info, CAMERA_INFO)
RS2_ENUM_HELPERS(rs2_frame_metadata_value, FRAME_METADATA)
RS2_ENUM_HELPERS(rs2_timestamp_domain, TIMESTAMP_DOMAIN)
RS2_ENUM_HELPERS(rs2_sr300_visual_preset, SR300_VISUAL_PRESET)
RS2_ENUM_HELPERS(rs2_extension, EXTENSION)
RS2_ENUM_HELPERS(rs2_exception_type, EXCEPTION_TYPE)
RS2_ENUM_HELPERS(rs2_log_severity, LOG_SEVERITY)
RS2_ENUM_HELPERS(rs2_notification_category, NOTIFICATION_CATEGORY)
RS2_ENUM_HELPERS(rs2_playback_status, PLAYBACK_STATUS)
RS2_ENUM_HELPERS(rs2_matchers, MATCHER)
////////////////////////////////////////////
// World's tiniest linear algebra library //
////////////////////////////////////////////
#pragma pack(push, 1)
struct int2 { int x, y; };
struct float2 { float x, y; float & operator [] (int i) { return (&x)[i]; } };
struct float3 { float x, y, z; float & operator [] (int i) { return (&x)[i]; } };
struct float4 { float x, y, z, w; float & operator [] (int i) { return (&x)[i]; } };
struct float3x3 { float3 x, y, z; float & operator () (int i, int j) { return (&x)[j][i]; } }; // column-major
struct pose { float3x3 orientation; float3 position; };
#pragma pack(pop)
inline bool operator == (const float3 & a, const float3 & b) { return a.x == b.x && a.y == b.y && a.z == b.z; }
inline float3 operator + (const float3 & a, const float3 & b) { return{ a.x + b.x, a.y + b.y, a.z + b.z }; }
inline float3 operator * (const float3 & a, float b) { return{ a.x*b, a.y*b, a.z*b }; }
inline bool operator == (const float4 & a, const float4 & b) { return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w; }
inline float4 operator + (const float4 & a, const float4 & b) { return{ a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w }; }
inline bool operator == (const float3x3 & a, const float3x3 & b) { return a.x == b.x && a.y == b.y && a.z == b.z; }
inline float3 operator * (const float3x3 & a, const float3 & b) { return a.x*b.x + a.y*b.y + a.z*b.z; }
inline float3x3 operator * (const float3x3 & a, const float3x3 & b) { return{ a*b.x, a*b.y, a*b.z }; }
inline float3x3 transpose(const float3x3 & a) { return{ {a.x.x,a.y.x,a.z.x}, {a.x.y,a.y.y,a.z.y}, {a.x.z,a.y.z,a.z.z} }; }
inline bool operator == (const pose & a, const pose & b) { return a.orientation == b.orientation && a.position == b.position; }
inline float3 operator * (const pose & a, const float3 & b) { return a.orientation * b + a.position; }
inline pose operator * (const pose & a, const pose & b) { return{ a.orientation * b.orientation, a * b.position }; }
inline pose inverse(const pose & a) { auto inv = transpose(a.orientation); return{ inv, inv * a.position * -1 }; }
inline pose to_pose(const rs2_extrinsics& a)
{
pose r{};
for (int i = 0; i < 3; i++) r.position[i] = a.translation[i];
for (int j = 0; j < 3; j++)
for (int i = 0; i < 3; i++)
r.orientation(i, j) = a.rotation[j * 3 + i];
return r;
}
inline rs2_extrinsics from_pose(pose a)
{
rs2_extrinsics r;
for (int i = 0; i < 3; i++) r.translation[i] = a.position[i];
for (int j = 0; j < 3; j++)
for (int i = 0; i < 3; i++)
r.rotation[j * 3 + i] = a.orientation(i, j);
return r;
}
inline rs2_extrinsics identity_matrix() {
rs2_extrinsics r;
// Do it the silly way to avoid infite warnings about the dangers of memset
for (int i = 0; i < 3; i++) r.translation[i] = 0.f;
for (int j = 0; j < 3; j++)
for (int i = 0; i < 3; i++)
r.rotation[j * 3 + i] = (i == j) ? 1.f : 0.f;
return r;
}
inline rs2_extrinsics inverse(const rs2_extrinsics& a) { auto p = to_pose(a); return from_pose(inverse(p)); }
///////////////////
// Pixel formats //
///////////////////
typedef std::tuple<uint32_t, int, size_t> native_pixel_format_tuple;
typedef std::tuple<rs2_stream, int, rs2_format> output_tuple;
typedef std::tuple<platform::stream_profile_tuple, native_pixel_format_tuple, std::vector<output_tuple>> request_mapping_tuple;
struct stream_profile
{
rs2_stream stream;
int index;
uint32_t width, height, fps;
rs2_format format;
};
inline bool operator==(const stream_profile& a,
const stream_profile& b)
{
return (a.width == b.width) &&
(a.height == b.height) &&
(a.fps == b.fps) &&
(a.format == b.format) &&
(a.index == b.index);
}
struct stream_descriptor
{
stream_descriptor() : type(RS2_STREAM_ANY), index(0) {}
stream_descriptor(rs2_stream type, int index = 0) : type(type), index(index) {}
rs2_stream type;
int index;
};
struct resolution
{
uint32_t width, height;
};
using resolution_func = std::function<resolution(resolution res)>;
struct stream_output {
stream_output(stream_descriptor stream_desc_in,
rs2_format format_in,
resolution_func res_func = [](resolution res) {return res; })
: stream_desc(stream_desc_in),
format(format_in),
stream_resolution(res_func)
{}
stream_descriptor stream_desc;
rs2_format format;
resolution_func stream_resolution;
};
struct pixel_format_unpacker
{
bool requires_processing;
void(*unpack)(byte * const dest[], const byte * source, int width, int height);
std::vector<stream_output> outputs;
platform::stream_profile get_uvc_profile(const stream_profile& request, uint32_t fourcc, const std::vector<platform::stream_profile>& uvc_profiles) const
{
platform::stream_profile uvc_profile{};
auto it = std::find_if(begin(uvc_profiles), end(uvc_profiles),
[&fourcc, &request, this](const platform::stream_profile& uvc_p)
{
for (auto & o : outputs)
{
auto res = o.stream_resolution(resolution{ uvc_p.width, uvc_p.height });
if (o.stream_desc.type == request.stream && o.stream_desc.index == request.index &&
res.width == request.width && res.height == request.height &&
uvc_p.format == fourcc && request.fps == uvc_p.fps)
return true;
}
return false;
});
if (it != end(uvc_profiles))
{
uvc_profile = *it;
}
return uvc_profile;
}
bool satisfies(const stream_profile& request, uint32_t fourcc, const std::vector<platform::stream_profile>& uvc_profiles) const
{
auto uvc_profile = get_uvc_profile(request, fourcc, uvc_profiles);
return provides_stream(request, fourcc, uvc_profile) &&
get_format(request.stream, request.index) == request.format;
}
bool provides_stream(const stream_profile& request, uint32_t fourcc, const platform::stream_profile& uvc_profile) const
{
for (auto& o : outputs)
{
auto res = o.stream_resolution(resolution{ uvc_profile.width, uvc_profile.height });
if (o.stream_desc.type == request.stream && o.stream_desc.index == request.index &&
res.width == request.width && res.height == request.height)
return true;
}
return false;
}
rs2_format get_format(rs2_stream stream, int index) const
{
for (auto& o : outputs)
if (o.stream_desc.type == stream && o.stream_desc.index == index)
return o.format;
throw invalid_value_exception("missing output");
}
operator std::vector<output_tuple>()
{
std::vector<output_tuple> tuple_outputs;
for (auto output : outputs)
{
tuple_outputs.push_back(std::make_tuple(output.stream_desc.type, output.stream_desc.index, output.format));
}
return tuple_outputs;
}
};
struct native_pixel_format
{
uint32_t fourcc;
int plane_count;
size_t bytes_per_pixel;
std::vector<pixel_format_unpacker> unpackers;
size_t get_image_size(int width, int height) const { return width * height * plane_count * bytes_per_pixel; }
operator native_pixel_format_tuple() const
{
return std::make_tuple(fourcc, plane_count, bytes_per_pixel);
}
};
class stream_profile_interface;
struct request_mapping
{
platform::stream_profile profile;
native_pixel_format* pf;
pixel_format_unpacker* unpacker;
// The request lists is there just for lookup and is not involved in object comparison
mutable std::vector<std::shared_ptr<stream_profile_interface>> original_requests;
operator request_mapping_tuple() const
{
return std::make_tuple(profile, *pf, *unpacker);
}
bool requires_processing() const { return unpacker->requires_processing; }
};
inline bool operator< (const request_mapping& first, const request_mapping& second)
{
return request_mapping_tuple(first) < request_mapping_tuple(second);
}
inline bool operator==(const request_mapping& a,
const request_mapping& b)
{
return (a.profile == b.profile) && (a.pf == b.pf) && (a.unpacker == b.unpacker);
}
class frame_interface;
struct frame_holder
{
frame_interface* frame;
frame_interface* operator->()
{
return frame;
}
operator bool() const { return frame != nullptr; }
operator frame_interface*() const { return frame; }
frame_holder(frame_interface* f)
{
frame = f;
}
~frame_holder();
frame_holder(frame_holder&& other)
: frame(other.frame)
{
other.frame = nullptr;
}
frame_holder() : frame(nullptr) {}
frame_holder& operator=(frame_holder&& other);
frame_holder clone() const;
private:
frame_holder& operator=(const frame_holder& other) = delete;
frame_holder(const frame_holder& other);
};
class firmware_version
{
int m_major, m_minor, m_patch, m_build;
bool is_any;
std::string string_representation;
std::string to_string() const;
static std::vector<std::string> split(const std::string& str);
static int parse_part(const std::string& name, int part);
public:
firmware_version() : m_major(0), m_minor(0), m_patch(0), m_build(0), is_any(true), string_representation(to_string()) {}
firmware_version(int major, int minor, int patch, int build, bool is_any = false)
: m_major(major), m_minor(minor), m_patch(patch), m_build(build), is_any(is_any), string_representation(to_string()) {}
static firmware_version any()
{
return{};
}
explicit firmware_version(const std::string& name)
: m_major(parse_part(name, 0)), m_minor(parse_part(name, 1)), m_patch(parse_part(name, 2)), m_build(parse_part(name, 3)), is_any(false), string_representation(to_string()) {}
bool operator<=(const firmware_version& other) const
{
if (is_any || other.is_any) return true;
if (m_major > other.m_major) return false;
if ((m_major == other.m_major) && (m_minor > other.m_minor)) return false;
if ((m_major == other.m_major) && (m_minor == other.m_minor) && (m_patch > other.m_patch)) return false;
if ((m_major == other.m_major) && (m_minor == other.m_minor) && (m_patch == other.m_patch) && (m_build > other.m_build)) return false;
return true;
}
bool operator==(const firmware_version& other) const
{
return is_any || (other.m_major == m_major && other.m_minor == m_minor && other.m_patch == m_patch && other.m_build == m_build);
}
bool operator> (const firmware_version& other) const { return !(*this < other) || is_any; }
bool operator!=(const firmware_version& other) const { return !(*this == other); }
bool operator<(const firmware_version& other) const { return !(*this == other) && (*this <= other); }
bool operator>=(const firmware_version& other) const { return (*this == other) || (*this > other); }
bool is_between(const firmware_version& from, const firmware_version& until) const
{
return (from <= *this) && (*this <= until);
}
operator const char*() const
{
return string_representation.c_str();
}
operator std::string() const
{
return string_representation.c_str();
}
};
// This class is used to buffer up several writes to a structure-valued XU control, and send the entire structure all at once
// Additionally, it will ensure that any fields not set in a given struct will retain their original values
template<class T, class R, class W> struct struct_interface
{
T struct_;
R reader;
W writer;
bool active;
struct_interface(R r, W w) : reader(r), writer(w), active(false) {}
void activate() { if (!active) { struct_ = reader(); active = true; } }
template<class U> double get(U T::* field) { activate(); return static_cast<double>(struct_.*field); }
template<class U, class V> void set(U T::* field, V value) { activate(); struct_.*field = static_cast<U>(value); }
void commit() { if (active) writer(struct_); }
};
template<class T, class R, class W>
std::shared_ptr<struct_interface<T, R, W>> make_struct_interface(R r, W w)
{
return std::make_shared<struct_interface<T, R, W>>(r, w);
}
// Provides an efficient wraparound for built-in arithmetic times, for use-cases such as a rolling timestamp
template <typename T, typename S>
class arithmetic_wraparound
{
public:
arithmetic_wraparound() :
last_input(std::numeric_limits<T>::lowest()), accumulated(0) {
static_assert(
(std::is_arithmetic<T>::value) &&
(std::is_arithmetic<S>::value) &&
(std::numeric_limits<T>::max() < std::numeric_limits<S>::max()) &&
(std::numeric_limits<T>::lowest() >= std::numeric_limits<S>::lowest())
, "Wraparound class requirements are not met");
}
S calc(const T input)
{
accumulated += static_cast<T>(input - last_input); // Automatically resolves wraparounds
last_input = input;
return (accumulated);
}
void reset() { last_input = std::numeric_limits<T>::lowest(); accumulated = 0; }
private:
T last_input;
S accumulated;
};
typedef void(*frame_callback_function_ptr)(rs2_frame * frame, void * user);
class frame_callback : public rs2_frame_callback
{
frame_callback_function_ptr fptr;
void * user;
public:
frame_callback() : frame_callback(nullptr, nullptr) {}
frame_callback(frame_callback_function_ptr on_frame, void * user) : fptr(on_frame), user(user) {}
operator bool() const { return fptr != nullptr; }
void on_frame (rs2_frame * frame) override {
if (fptr)
{
try { fptr(frame, user); } catch (...)
{
LOG_ERROR("Received an execption from frame callback!");
}
}
}
void release() override { delete this; }
};
class internal_frame_processor_fptr_callback : public rs2_frame_processor_callback
{
rs2_frame_processor_callback_ptr fptr;
void * user;
public:
internal_frame_processor_fptr_callback() : internal_frame_processor_fptr_callback(nullptr, nullptr) {}
internal_frame_processor_fptr_callback(rs2_frame_processor_callback_ptr on_frame, void * user)
: fptr(on_frame), user(user) {}
operator bool() const { return fptr != nullptr; }
void on_frame(rs2_frame * frame, rs2_source * source) override {
if (fptr)
{
try { fptr(frame, source, user); }
catch (...)
{
LOG_ERROR("Received an execption from frame callback!");
}
}
}
void release() override { delete this; }
};
template<class T>
class internal_frame_callback : public rs2_frame_callback
{
T on_frame_function; //Callable of type: void(frame_interface* frame)
public:
explicit internal_frame_callback(T on_frame) : on_frame_function(on_frame) {}
void on_frame(rs2_frame* fref) override
{
on_frame_function((frame_interface*)(fref));
}
void release() override { delete this; }
};
typedef void(*notifications_callback_function_ptr)(rs2_notification * notification, void * user);
class notifications_callback : public rs2_notifications_callback
{
notifications_callback_function_ptr nptr;
void * user;
public:
notifications_callback() : notifications_callback(nullptr, nullptr) {}
notifications_callback(notifications_callback_function_ptr on_notification, void * user) : nptr(on_notification), user(user) {}
operator bool() const { return nptr != nullptr; }
void on_notification(rs2_notification * notification) override {
if (nptr)
{
try { nptr(notification, user); }
catch (...)
{
LOG_ERROR("Received an execption from frame callback!");
}
}
}
void release() override { delete this; }
};
typedef void(*devices_changed_function_ptr)(rs2_device_list* removed, rs2_device_list* added, void * user);
class devices_changed_callback: public rs2_devices_changed_callback
{
devices_changed_function_ptr nptr;
void * user;
public:
devices_changed_callback() : devices_changed_callback(nullptr, nullptr) {}
devices_changed_callback(devices_changed_function_ptr on_devices_changed, void * user) : nptr(on_devices_changed), user(user) {}
operator bool() const { return nptr != nullptr; }
void on_devices_changed(rs2_device_list* removed, rs2_device_list* added) override {
if (nptr)
{
try { nptr(removed, added, user); }
catch (...)
{
LOG_ERROR("Received an execption from frame callback!");
}
}
}
void release() override { delete this; }
};
typedef std::unique_ptr<rs2_log_callback, void(*)(rs2_log_callback*)> log_callback_ptr;
typedef std::shared_ptr<rs2_frame_callback> frame_callback_ptr;
typedef std::shared_ptr<rs2_frame_processor_callback> frame_processor_callback_ptr;
typedef std::shared_ptr<rs2_notifications_callback> notifications_callback_ptr;
typedef std::shared_ptr<rs2_devices_changed_callback> devices_changed_callback_ptr;
using internal_callback = std::function<void(rs2_device_list* removed, rs2_device_list* added)>;
class devices_changed_callback_internal : public rs2_devices_changed_callback
{
internal_callback _callback;
public:
explicit devices_changed_callback_internal(internal_callback callback)
: _callback(callback)
{}
void on_devices_changed(rs2_device_list* removed, rs2_device_list* added) override
{
_callback(removed , added);
}
void release() override { delete this; }
};
struct notification
{
notification(rs2_notification_category category, int type, rs2_log_severity severity, std::string description)
:category(category), type(type), severity(severity), description(description)
{
timestamp = std::chrono::duration<double, std::milli>(std::chrono::system_clock::now().time_since_epoch()).count();
LOG_INFO(description);
}
rs2_notification_category category;
int type;
rs2_log_severity severity;
std::string description;
double timestamp;
std::string serialized_data;
};
class notification_decoder
{
public:
virtual ~notification_decoder() = default;
virtual notification decode(int value) = 0;
};
class notifications_processor
{
public:
notifications_processor();
~notifications_processor();
void set_callback(notifications_callback_ptr callback);
notifications_callback_ptr get_callback() const;
void raise_notification(const notification);
private:
notifications_callback_ptr _callback;
std::mutex _callback_mutex;
dispatcher _dispatcher;
};
////////////////////////////////////////
// Helper functions for library types //
////////////////////////////////////////
inline rs2_intrinsics pad_crop_intrinsics(const rs2_intrinsics & i, int pad_crop)
{
return{ i.width + pad_crop * 2, i.height + pad_crop * 2, i.ppx + pad_crop, i.ppy + pad_crop,
i.fx, i.fy, i.model, {i.coeffs[0], i.coeffs[1], i.coeffs[2], i.coeffs[3], i.coeffs[4]} };
}
inline rs2_intrinsics scale_intrinsics(const rs2_intrinsics & i, int width, int height)
{
const float sx = static_cast<float>(width) / i.width, sy = static_cast<float>(height) / i.height;
return{ width, height, i.ppx*sx, i.ppy*sy, i.fx*sx, i.fy*sy, i.model,
{i.coeffs[0], i.coeffs[1], i.coeffs[2], i.coeffs[3], i.coeffs[4]} };
}
inline bool operator == (const rs2_intrinsics & a, const rs2_intrinsics & b) { return std::memcmp(&a, &b, sizeof(a)) == 0; }
inline uint32_t pack(uint8_t c0, uint8_t c1, uint8_t c2, uint8_t c3)
{
return (c0 << 24) | (c1 << 16) | (c2 << 8) | c3;
}
template<class T, int C>
class small_heap
{
T buffer[C];
bool is_free[C];
std::mutex mutex;
bool keep_allocating = true;
std::condition_variable cv;
int size = 0;
public:
small_heap()
{
for (auto i = 0; i < C; i++)
{
is_free[i] = true;
buffer[i] = std::move(T());