RealSense Cross Platform API
RealSense Cross-platform API
rs_frame.hpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 RealSense, Inc. All Rights Reserved.
3 
4 #ifndef LIBREALSENSE_RS2_FRAME_HPP
5 #define LIBREALSENSE_RS2_FRAME_HPP
6 
7 #include "rs_types.hpp"
8 
9 namespace rs2
10 {
11  class frame_source;
12  class frame_queue;
13  class syncer;
14  class processing_block;
15  class pointcloud;
16  class sensor;
17  class frame;
18  class pipeline_profile;
19  class points;
20  class video_stream_profile;
21 
23  {
24  public:
28  stream_profile() : _profile(nullptr) {}
29 
34  int stream_index() const { return _index; }
39  rs2_stream stream_type() const { return _type; }
44  rs2_format format() const { return _format; }
49  int fps() const { return _framerate; }
54  int unique_id() const { return _uid; }
55 
64  {
65  rs2_error* e = nullptr;
66  auto ref = rs2_clone_stream_profile(_profile, type, index, format, &e);
67  error::handle(e);
68  stream_profile res(ref);
69  res._clone = std::shared_ptr<rs2_stream_profile>(ref, [](rs2_stream_profile* r) { rs2_delete_stream_profile(r); });
70 
71  return res;
72  }
73 
79  bool operator==(const stream_profile& rhs)
80  {
81  return stream_index() == rhs.stream_index() &&
82  stream_type() == rhs.stream_type() &&
83  format() == rhs.format() &&
84  fps() == rhs.fps();
85  }
86 
91  template<class T>
92  bool is() const
93  {
94  T extension(*this);
95  return extension;
96  }
97 
102  template<class T>
103  T as() const
104  {
105  T extension(*this);
106  return extension;
107  }
108 
113  std::string stream_name() const
114  {
115  rs2_error * e = nullptr;
116  std::string name = rs2_get_stream_profile_name( _profile, &e );
117 
118  if( name.empty() )
119  {
120  std::stringstream ss;
122  if( stream_index() != 0 )
123  ss << " " << stream_index();
124  name = ss.str();
125  }
126 
127  return name;
128  }
129 
134  bool is_default() const { return _default; }
135 
140  operator bool() const { return _profile != nullptr; }
141 
146  const rs2_stream_profile* get() const { return _profile; }
147 
158  {
159  rs2_error* e = nullptr;
160  rs2_extrinsics res;
161  rs2_get_extrinsics(get(), to.get(), &res, &e);
162  error::handle(e);
163  return res;
164  }
172  {
173  rs2_error* e = nullptr;
174  rs2_register_extrinsics(get(), to.get(), extrinsics, &e);
175  error::handle(e);
176  }
177 
178  bool is_cloned() { return bool(_clone); }
179  explicit stream_profile(const rs2_stream_profile* profile) : _profile(profile)
180  {
181  rs2_error* e = nullptr;
183  error::handle(e);
184 
186  error::handle(e);
187 
188  }
189  operator const rs2_stream_profile*() { return _profile; }
190  explicit operator std::shared_ptr<rs2_stream_profile>() { return _clone; }
191 
192  protected:
193  friend class rs2::sensor;
194  friend class rs2::frame;
195  friend class rs2::pipeline_profile;
197 
199  std::shared_ptr<rs2_stream_profile> _clone;
200 
201  int _index = 0;
202  int _uid = 0;
203  int _framerate = 0;
206 
207  bool _default = false;
208  };
209 
211  {
212  public:
214 
220  : stream_profile(sp)
221  {
222  rs2_error* e = nullptr;
223  if (!sp || (rs2_stream_profile_is(sp.get(), RS2_EXTENSION_VIDEO_PROFILE, &e) == 0 && !e))
224  {
225  _profile = nullptr;
226  }
227  error::handle(e);
228 
229  if (_profile)
230  {
231  rs2_get_video_stream_resolution(_profile, &_width, &_height, &e);
232  error::handle(e);
233  }
234  }
235 
236  int width() const
237  {
238  return _width;
239  }
240 
241  int height() const
242  {
243  return _height;
244  }
250  {
251  rs2_error* e = nullptr;
252  rs2_intrinsics intr;
254  error::handle(e);
255  return intr;
256  }
257 
258  bool operator==(const video_stream_profile& other) const
259  {
260  return (((stream_profile&)*this)==other &&
261  width() == other.width() &&
262  height() == other.height());
263  }
264 
265  using stream_profile::clone;
266 
277  stream_profile clone(rs2_stream type, int index, rs2_format format, int width, int height, const rs2_intrinsics& intr) const
278  {
279  rs2_error* e = nullptr;
280  auto ref = rs2_clone_video_stream_profile(_profile, type, index, format, width, height, &intr, &e);
281  error::handle(e);
282  stream_profile res(ref);
283  res._clone = std::shared_ptr<rs2_stream_profile>(ref, [](rs2_stream_profile* r) { rs2_delete_stream_profile(r); });
284 
285  return res;
286  }
287  private:
288  int _width = 0;
289  int _height = 0;
290  };
291 
292 
294  {
295  public:
301  : stream_profile(sp)
302  {
303  rs2_error* e = nullptr;
304  if (!sp || (rs2_stream_profile_is(sp.get(), RS2_EXTENSION_MOTION_PROFILE, &e) == 0 && !e))
305  {
306  _profile = nullptr;
307  }
308  error::handle(e);
309  }
310 
316  {
317  rs2_error* e = nullptr;
319  rs2_get_motion_intrinsics(_profile, &intrin, &e);
320  error::handle(e);
321  return intrin;
322  }
323  };
324 
326  {
327  public:
333  : stream_profile(sp)
334  {
335  rs2_error* e = nullptr;
336  if (!sp || (rs2_stream_profile_is(sp.get(), RS2_EXTENSION_POSE_PROFILE, &e) == 0 && !e))
337  {
338  _profile = nullptr;
339  }
340  error::handle(e);
341  }
342  };
343 
345  {
346  public:
348  : stream_profile(sp)
349  {
350  rs2_error* e = nullptr;
351  if (!sp || (rs2_stream_profile_is(sp.get(), RS2_EXTENSION_PERCEPTION_PROFILE, &e) == 0 && !e))
352  {
353  _profile = nullptr;
354  }
355  error::handle(e);
356  }
357  };
358 
363  {
364  public:
365  virtual rs2::frame process(rs2::frame frame) const = 0;
366  virtual ~filter_interface() = default;
367  };
368 
369  class frame
370  {
371  public:
375  frame() : frame_ref(nullptr) {}
380  frame(rs2_frame* ref) : frame_ref(ref)
381  {
382 #ifdef _DEBUG
383  if (ref)
384  {
385  rs2_error* e = nullptr;
386  auto r = rs2_get_frame_number(ref, &e);
387  if (!e)
388  frame_number = r;
389  auto s = rs2_get_frame_stream_profile(ref, &e);
390  if (!e)
391  profile = stream_profile(s);
392  }
393  else
394  {
395  frame_number = 0;
396  profile = stream_profile();
397  }
398 #endif
399  }
404  frame(frame&& other) noexcept : frame_ref(other.frame_ref)
405  {
406  other.frame_ref = nullptr;
407 #ifdef _DEBUG
408  frame_number = other.frame_number;
409  profile = other.profile;
410 #endif
411  }
417  {
418  swap(other);
419  return *this;
420  }
421 
426  frame(const frame& other)
427  : frame_ref(other.frame_ref)
428  {
429  if (frame_ref) add_ref();
430 #ifdef _DEBUG
431  frame_number = other.frame_number;
432  profile = other.profile;
433 #endif
434  }
439  void swap(frame& other)
440  {
441  std::swap(frame_ref, other.frame_ref);
442 
443 #ifdef _DEBUG
444  std::swap(frame_number, other.frame_number);
445  std::swap(profile, other.profile);
446 #endif
447  }
448 
453  {
454  if (frame_ref)
455  {
456  rs2_release_frame(frame_ref);
457  }
458  }
459 
463  void keep() { rs2_keep_frame(frame_ref); }
464 
469  operator bool() const { return frame_ref != nullptr; }
470 
472  {
473  rs2_error* e = nullptr;
474  auto r = rs2_get_frame_sensor(frame_ref, &e);
475  error::handle(e);
476  return r;
477  }
478 
500  double get_timestamp() const
501  {
502  rs2_error* e = nullptr;
503  auto r = rs2_get_frame_timestamp(frame_ref, &e);
504  error::handle(e);
505  return r;
506  }
507 
512  {
513  rs2_error* e = nullptr;
514  auto r = rs2_get_frame_timestamp_domain(frame_ref, &e);
515  error::handle(e);
516  return r;
517  }
518 
524  {
525  rs2_error* e = nullptr;
526  auto r = rs2_get_frame_metadata(frame_ref, frame_metadata, &e);
527  error::handle(e);
528  return r;
529  }
530 
536  {
537  rs2_error* e = nullptr;
538  auto r = rs2_supports_frame_metadata(frame_ref, frame_metadata, &e);
539  error::handle(e);
540  return r != 0;
541  }
542 
547  unsigned long long get_frame_number() const
548  {
549  rs2_error* e = nullptr;
550  auto r = rs2_get_frame_number(frame_ref, &e);
551  error::handle(e);
552  return r;
553  }
554 
559  const int get_data_size() const
560  {
561  rs2_error* e = nullptr;
562  auto r = rs2_get_frame_data_size(frame_ref, &e);
563  error::handle(e);
564  return r;
565  }
566 
571  const void* get_data() const
572  {
573  rs2_error* e = nullptr;
574  auto r = rs2_get_frame_data(frame_ref, &e);
575  error::handle(e);
576  return r;
577  }
578 
588  const void* get_gpu_data_or_upload( bool* copied = nullptr ) const
589  {
590  rs2_error* e = nullptr;
591  int c = 0;
592  auto r = rs2_get_frame_gpu_data_or_upload(frame_ref, &c, &e);
593  error::handle(e);
594  if( copied ) *copied = ( c != 0 );
595  return r;
596  }
597 
603  {
604  rs2_error* e = nullptr;
605  auto s = rs2_get_frame_stream_profile(frame_ref, &e);
606  error::handle(e);
607  return stream_profile(s);
608  }
609 
614  template<class T>
615  bool is() const
616  {
617  T extension(*this);
618  return extension;
619  }
624  template<class T>
625  T as() const
626  {
627  T extension(*this);
628  return extension;
629  }
630 
635  rs2_frame* get() const { return frame_ref; }
636  explicit operator rs2_frame*() { return frame_ref; }
637 
639  {
640  return filter.process(*this);
641  }
642 
643  protected:
649  void add_ref() const
650  {
651  rs2_error* e = nullptr;
652  rs2_frame_add_ref(frame_ref, &e);
653  error::handle(e);
654  }
655 
656  void reset()
657  {
658  if (frame_ref)
659  {
660  rs2_release_frame(frame_ref);
661  }
662  frame_ref = nullptr;
663  }
664 
665  private:
666  friend class rs2::frame_source;
667  friend class rs2::frame_queue;
668  friend class rs2::syncer;
669  friend class rs2::processing_block;
670  friend class rs2::pointcloud;
671  friend class rs2::points;
672 
673  rs2_frame* frame_ref;
674 
675 #ifdef _DEBUG
676  stream_profile profile;
677  unsigned long long frame_number = 0;
678 #endif
679  };
680 
681  class video_frame : public frame
682  {
683  public:
688  video_frame(const frame& f)
689  : frame(f)
690  {
691  rs2_error* e = nullptr;
692  if (!f || (rs2_is_frame_extendable_to(f.get(), RS2_EXTENSION_VIDEO_FRAME, &e) == 0 && !e))
693  {
694  reset();
695  }
696  error::handle(e);
697  }
698 
699 
704  int get_width() const
705  {
706  rs2_error* e = nullptr;
707  auto r = rs2_get_frame_width(get(), &e);
708  error::handle(e);
709  return r;
710  }
711 
716  int get_height() const
717  {
718  rs2_error* e = nullptr;
719  auto r = rs2_get_frame_height(get(), &e);
720  error::handle(e);
721  return r;
722  }
723 
729  {
730  rs2_error* e = nullptr;
731  auto r = rs2_get_frame_stride_in_bytes(get(), &e);
732  error::handle(e);
733  return r;
734  }
735 
740  int get_bits_per_pixel() const
741  {
742  rs2_error* e = nullptr;
743  auto r = rs2_get_frame_bits_per_pixel(get(), &e);
744  error::handle(e);
745  return r;
746  }
747 
752  int get_bytes_per_pixel() const { return get_bits_per_pixel() / 8; }
753 
764  bool extract_target_dimensions(rs2_calib_target_type calib_type, float* target_dims, unsigned int target_dims_size) const
765  {
766  rs2_error* e = nullptr;
767  rs2_extract_target_dimensions(get(), calib_type, target_dims, target_dims_size, &e);
768  error::handle(e);
769  return (e == nullptr);
770  }
771  };
772 
773  struct vertex {
774  float x, y, z;
775  operator const float*() const { return &x; }
776  };
778  float u, v;
779  operator const float*() const { return &u; }
780  };
781 
782  class points : public frame
783  {
784  public:
788  points() : frame(), _size(0) {}
789 
794  points(const frame& f)
795  : frame(f), _size(0)
796  {
797  rs2_error* e = nullptr;
798  if (!f || (rs2_is_frame_extendable_to(f.get(), RS2_EXTENSION_POINTS, &e) == 0 && !e))
799  {
800  reset();
801  }
802  error::handle(e);
803 
804  if (get())
805  {
806  _size = rs2_get_frame_points_count(get(), &e);
807  error::handle(e);
808  }
809  }
814  const vertex* get_vertices() const
815  {
816  rs2_error* e = nullptr;
817  auto res = rs2_get_frame_vertices(get(), &e);
818  error::handle(e);
819  return (const vertex*)res;
820  }
821 
827  void export_to_ply(const std::string& fname, video_frame texture)
828  {
829  rs2_frame* ptr = nullptr;
830  std::swap(texture.frame_ref, ptr);
831  rs2_error* e = nullptr;
832  rs2_export_to_ply(get(), fname.c_str(), ptr, &e);
833  error::handle(e);
834  }
840  {
841  rs2_error* e = nullptr;
842  auto res = rs2_get_frame_texture_coordinates(get(), &e);
843  error::handle(e);
844  return (const texture_coordinate*)res;
845  }
846 
847  size_t size() const
848  {
849  return _size;
850  }
851 
852  private:
853  size_t _size;
854  };
855 
856 
857  class labeled_points : public frame
858  {
859  public:
863  labeled_points() : frame(), _size(0) {}
864 
870  : frame(f), _size(0)
871  {
872  rs2_error* e = nullptr;
873  if (!f || (rs2_is_frame_extendable_to(f.get(), RS2_EXTENSION_LABELED_POINTS, &e) == 0 && !e))
874  {
875  reset();
876  }
877  error::handle(e);
878 
879  if (get())
880  {
881  _size = rs2_get_frame_labeled_points_count(get(), &e);
882  error::handle(e);
883  }
884  }
885 
890  const vertex* get_vertices() const
891  {
892  rs2_error* e = nullptr;
893  auto res = rs2_get_frame_labeled_vertices(get(), &e);
894  error::handle(e);
895  return (const vertex*)res;
896  }
897 
902  const uint8_t* get_labels() const
903  {
904  rs2_error* e = nullptr;
905  auto res = rs2_get_frame_labels(get(), &e);
906  error::handle(e);
907  return (uint8_t * )res;
908  }
909 
910  // Returns the vertices counts
911  size_t size() const
912  {
913  return _size;
914  }
915 
920  unsigned int get_width() const
921  {
922  rs2_error* e = nullptr;
923  auto r = rs2_get_frame_labeled_points_width(get(), &e);
924  error::handle(e);
925  return r;
926  }
927 
932  unsigned int get_height() const
933  {
934  rs2_error* e = nullptr;
935  auto r = rs2_get_frame_labeled_points_height(get(), &e);
936  error::handle(e);
937  return r;
938  }
939 
944  unsigned int get_bits_per_pixel() const
945  {
946  rs2_error* e = nullptr;
948  error::handle(e);
949  return r;
950  }
951 
952  private:
953  size_t _size;
954  };
955 
956  class depth_frame : public video_frame
957  {
958  public:
963  depth_frame(const frame& f)
964  : video_frame(f)
965  {
966  rs2_error* e = nullptr;
967  if (!f || (rs2_is_frame_extendable_to(f.get(), RS2_EXTENSION_DEPTH_FRAME, &e) == 0 && !e))
968  {
969  reset();
970  }
971  error::handle(e);
972  }
973 
980  float get_distance(int x, int y) const
981  {
982  rs2_error * e = nullptr;
983  auto r = rs2_depth_frame_get_distance(get(), x, y, &e);
984  error::handle(e);
985  return r;
986  }
987 
992  float get_units() const
993  {
994  rs2_error * e = nullptr;
995  auto r = rs2_depth_frame_get_units( get(), &e );
996  error::handle( e );
997  return r;
998  }
999  };
1000 
1002  {
1003  public:
1009  : depth_frame(f)
1010  {
1011  rs2_error* e = nullptr;
1012  if (!f || (rs2_is_frame_extendable_to(f.get(), RS2_EXTENSION_DISPARITY_FRAME, &e) == 0 && !e))
1013  {
1014  reset();
1015  }
1016  error::handle(e);
1017  }
1022  float get_baseline(void) const
1023  {
1024  rs2_error * e = nullptr;
1025  auto r = rs2_depth_stereo_frame_get_baseline(get(), &e);
1026  error::handle(e);
1027  return r;
1028  }
1029  };
1030 
1046  class gpu_frame : public frame
1047  {
1048  public:
1049  gpu_frame(const frame& f)
1050  : frame(f)
1051  {
1052  rs2_error* e = nullptr;
1053  if (!f || (rs2_is_frame_extendable_to(f.get(), RS2_EXTENSION_GPU_FRAME, &e) == 0 && !e))
1054  {
1055  reset();
1056  }
1057  error::handle(e);
1058  }
1059 
1066  const void* get_gpu_data() const
1067  {
1068  rs2_error* e = nullptr;
1069  auto r = rs2_get_frame_gpu_data(get(), &e);
1070  error::handle(e);
1071  return r;
1072  }
1073  };
1074 
1075  class motion_frame : public frame
1076  {
1077  public:
1083  : frame(f)
1084  {
1085  rs2_error* e = nullptr;
1086  if (!f || (rs2_is_frame_extendable_to(f.get(), RS2_EXTENSION_MOTION_FRAME, &e) == 0 && !e))
1087  {
1088  reset();
1089  }
1090  error::handle(e);
1091  }
1098  {
1099  auto data = reinterpret_cast<const float*>(get_data());
1100  return rs2_vector{ data[0], data[1], data[2] };
1101  }
1108  {
1109  return *reinterpret_cast< rs2_combined_motion const * >( get_data() );
1110  }
1111  };
1112 
1113  class pose_frame : public frame
1114  {
1115  public:
1120  pose_frame(const frame& f)
1121  : frame(f)
1122  {
1123  rs2_error* e = nullptr;
1124  if (!f || (rs2_is_frame_extendable_to(f.get(), RS2_EXTENSION_POSE_FRAME, &e) == 0 && !e))
1125  {
1126  reset();
1127  }
1128  error::handle(e);
1129  }
1135  {
1136  rs2_pose pose_data;
1137  rs2_error* e = nullptr;
1138  rs2_pose_frame_get_pose_data(get(), &pose_data, &e);
1139  error::handle(e);
1140  return pose_data;
1141  }
1142  };
1143 
1144  class perception_frame : public frame
1145  {
1146  public:
1151  {
1152  }
1153 
1158  perception_frame( const frame & f ) : frame( f )
1159  {
1160  rs2_error * e = nullptr;
1161  if( ! f || ( rs2_is_frame_extendable_to( f.get(), RS2_EXTENSION_PERCEPTION_FRAME, &e ) == 0 && ! e ) )
1162  {
1163  reset();
1164  }
1165  error::handle( e );
1166  }
1167  };
1168 
1170  {
1171  public:
1176 
1182  : perception_frame(f)
1183  {
1184  rs2_error* e = nullptr;
1185  if (!f || (rs2_is_frame_extendable_to(f.get(), RS2_EXTENSION_OBJECT_DETECTION_FRAME, &e) == 0 && !e))
1186  {
1187  reset();
1188  }
1189  error::handle(e);
1190  }
1191 
1196  unsigned int get_detection_count() const
1197  {
1198  rs2_error* e = nullptr;
1199  auto r = rs2_get_frame_object_detection_count(get(), &e);
1200  error::handle(e);
1201  return r;
1202  }
1203 
1209  rs2_object_detection get_detection(unsigned int index) const
1210  {
1211  rs2_object_detection detection;
1212  rs2_error* e = nullptr;
1213  rs2_get_frame_object_detection(get(), index, &detection, &e);
1214  error::handle(e);
1215  return detection;
1216  }
1217  };
1218 
1219  class frameset : public frame
1220  {
1221  public:
1225  frameset() :_size(0) {};
1230  frameset(const frame& f)
1231  : frame(f), _size(0)
1232  {
1233  rs2_error* e = nullptr;
1234  if (!f || (rs2_is_frame_extendable_to(f.get(), RS2_EXTENSION_COMPOSITE_FRAME, &e) == 0 && !e))
1235  {
1236  reset();
1237  // TODO - consider explicit constructor to move resultion to compile time
1238  }
1239  error::handle(e);
1240 
1241  if (get())
1242  {
1243  _size = rs2_embedded_frames_count(get(), &e);
1244  error::handle(e);
1245  }
1246  }
1247 
1255  {
1256  frame result;
1257  foreach_rs([&result, s, f](frame frm) {
1258  if (!result && frm.get_profile().stream_type() == s && (f == RS2_FORMAT_ANY || f == frm.get_profile().format()))
1259  {
1260  result = std::move(frm);
1261  }
1262  });
1263  return result;
1264  }
1272  {
1273  auto frm = first_or_default(s, f);
1274  if (!frm) throw error("Frame of requested stream type was not found!");
1275  return frm;
1276  }
1277 
1283  {
1285  return f.as<depth_frame>();
1286  }
1291  video_frame get_color_frame( const size_t index = 0 ) const
1292  {
1293  frame f;
1294 
1295  foreach_rs( [&f, index]( const frame & frm ) {
1296  if( !f && frm.get_profile().stream_type() == RS2_STREAM_COLOR &&
1297  frm.get_profile().stream_index() == index )
1298  f = frm;
1299  } );
1300 
1301  if( ! f )
1302  {
1303  // Color frame can also come from infrared sensor
1304  foreach_rs( [&f, index]( const frame & frm ) {
1305  if( !f && frm.get_profile().stream_type() == RS2_STREAM_INFRARED &&
1306  frm.get_profile().stream_index() == index &&
1307  frm.get_profile().format() == RS2_FORMAT_RGB8 )
1308  f = frm;
1309  } );
1310  }
1311 
1312  return f;
1313  }
1314 
1316  {
1318 
1319  return f.as<labeled_points>();
1320  }
1326  video_frame get_infrared_frame(const size_t index = 0) const
1327  {
1328  frame f;
1329  if (!index)
1330  {
1332  }
1333  else
1334  {
1335  foreach_rs([&f, index](const frame& frm) {
1336  if( !f && frm.get_profile().stream_type() == RS2_STREAM_INFRARED &&
1337  frm.get_profile().stream_index() == index )
1338  f = frm;
1339  });
1340  }
1341  return f;
1342  }
1343 
1349  video_frame get_fisheye_frame(const size_t index = 0) const
1350  {
1351  frame f;
1352  if (!index)
1353  {
1355  }
1356  else
1357  {
1358  foreach_rs([&f, index](const frame& frm) {
1359  if (frm.get_profile().stream_type() == RS2_STREAM_FISHEYE &&
1360  frm.get_profile().stream_index() == index) f = frm;
1361  });
1362  }
1363  return f;
1364  }
1365 
1371  pose_frame get_pose_frame(const size_t index = 0) const
1372  {
1373  frame f;
1374  if (!index)
1375  {
1377  }
1378  else
1379  {
1380  foreach_rs([&f, index](const frame& frm) {
1381  if (frm.get_profile().stream_type() == RS2_STREAM_POSE &&
1382  frm.get_profile().stream_index() == index) f = frm;
1383  });
1384  }
1385  return f.as<pose_frame>();
1386  }
1387 
1394  {
1395  frame f;
1396  if (!index)
1397  {
1399  }
1400  else
1401  {
1402  foreach_rs([&f, index](const frame& frm) {
1404  frm.get_profile().stream_index() == index) f = frm;
1405  });
1406  }
1407  return f.as<object_detection_frame>();
1408  }
1409 
1414  size_t size() const
1415  {
1416  return _size;
1417  }
1418 
1423  template<class T>
1424  void foreach_rs(T action) const
1425  {
1426  rs2_error* e = nullptr;
1427  auto count = size();
1428  for (size_t i = 0; i < count; i++)
1429  {
1430  auto fref = rs2_extract_frame(get(), (int)i, &e);
1431  error::handle(e);
1432 
1433  action(frame(fref));
1434  }
1435  }
1441  frame operator[](size_t index) const
1442  {
1443  rs2_error* e = nullptr;
1444  if (index < size())
1445  {
1446  auto fref = rs2_extract_frame(get(), (int)index, &e);
1447  error::handle(e);
1448  return frame(fref);
1449  }
1450 
1451  throw error("Requested index is out of range!");
1452  }
1453 
1454  class iterator
1455  {
1456  public:
1457  // inheriting from std::iterator template is deprecated in C++17, this is the new way to define an iterator
1458  // go to https://www.fluentcpp.com/2018/05/08/std-iterator-deprecated/ for more info
1459  using iterator_category = std::forward_iterator_tag;
1461  using difference_type = std::ptrdiff_t;
1462  using pointer = frame*;
1463  using reference = frame&;
1464 
1465  iterator(const frameset* owner, size_t index = 0) : _index(index), _owner(owner) {}
1466  iterator& operator++() { ++_index; return *this; }
1467  bool operator==(const iterator& other) const { return _index == other._index; }
1468  bool operator!=(const iterator& other) const { return !(*this == other); }
1469 
1470  frame operator*() { return (*_owner)[_index]; }
1471  private:
1472  size_t _index = 0;
1473  const frameset* _owner;
1474  };
1475 
1476  iterator begin() const { return iterator(this); }
1477  iterator end() const { return iterator(this, size()); }
1478  private:
1479  size_t _size;
1480  };
1481 
1482  template<class T>
1484  {
1485  T on_frame_function;
1486  public:
1487  explicit frame_callback(T on_frame) : on_frame_function(on_frame) {}
1488 
1489  void on_frame(rs2_frame* fref) override
1490  {
1491  on_frame_function(frame{ fref });
1492  }
1493 
1494  void release() override { delete this; }
1495  };
1496 }
1497 #endif // LIBREALSENSE_RS2_FRAME_HPP
const void * rs2_get_frame_gpu_data_or_upload(const rs2_frame *frame, int *copied, rs2_error **error)
Definition: rs_types.hpp:115
void rs2_register_extrinsics(const rs2_stream_profile *from, const rs2_stream_profile *to, rs2_extrinsics extrin, rs2_error **error)
stream_profile clone(rs2_stream type, int index, rs2_format format, int width, int height, const rs2_intrinsics &intr) const
Definition: rs_frame.hpp:277
Definition: rs_frame.hpp:293
iterator begin() const
Definition: rs_frame.hpp:1476
frame apply_filter(filter_interface &filter)
Definition: rs_frame.hpp:638
Definition: rs_frame.hpp:22
int get_bytes_per_pixel() const
Definition: rs_frame.hpp:752
depth_frame(const frame &f)
Definition: rs_frame.hpp:963
Definition: rs_frame.hpp:681
Definition: rs_types.hpp:26
void rs2_export_to_ply(const rs2_frame *frame, const char *fname, rs2_frame *texture, rs2_error **error)
Definition: rs_sensor.hpp:103
float rs2_depth_frame_get_units(const rs2_frame *frame, rs2_error **error)
Definition: rs_frame.hpp:369
video_stream_profile()
Definition: rs_frame.hpp:213
void release() override
Definition: rs_frame.hpp:1494
void add_ref() const
Definition: rs_frame.hpp:649
int rs2_get_frame_points_count(const rs2_frame *frame, rs2_error **error)
frame first(rs2_stream s, rs2_format f=RS2_FORMAT_ANY) const
Definition: rs_frame.hpp:1271
const rs2_stream_profile * rs2_get_frame_stream_profile(const rs2_frame *frame, rs2_error **error)
rs2_sensor * rs2_get_frame_sensor(const rs2_frame *frame, rs2_error **error)
rs2_motion_device_intrinsic get_motion_intrinsics() const
Definition: rs_frame.hpp:315
int _uid
Definition: rs_frame.hpp:202
stream_profile()
Definition: rs_frame.hpp:28
Definition: rs_pipeline.hpp:18
int rs2_is_frame_extendable_to(const rs2_frame *frame, rs2_extension extension_type, rs2_error **error)
rs2_format format() const
Definition: rs_frame.hpp:44
Definition: rs_types.h:160
void rs2_get_video_stream_resolution(const rs2_stream_profile *mode, int *width, int *height, rs2_error **error)
Definition: rs_frame.hpp:782
void register_extrinsics_to(const stream_profile &to, rs2_extrinsics extrinsics)
Definition: rs_frame.hpp:171
const void * get_gpu_data() const
Definition: rs_frame.hpp:1066
frame operator*()
Definition: rs_frame.hpp:1470
Definition: rs_frame.hpp:325
void rs2_keep_frame(rs2_frame *frame)
void rs2_get_extrinsics(const rs2_stream_profile *from, const rs2_stream_profile *to, rs2_extrinsics *extrin, rs2_error **error)
const void * rs2_get_frame_gpu_data(const rs2_frame *frame, rs2_error **error)
std::string stream_name() const
Definition: rs_frame.hpp:113
frameset()
Definition: rs_frame.hpp:1225
float y
Definition: rs_frame.hpp:774
int _index
Definition: rs_frame.hpp:201
Definition: rs_sensor.h:53
Definition: rs_sensor.h:72
const vertex * get_vertices() const
Definition: rs_frame.hpp:890
rs2_calib_target_type
Calibration target type.
Definition: rs_frame.h:169
pose_frame get_pose_frame(const size_t index=0) const
Definition: rs_frame.hpp:1371
unsigned int rs2_get_frame_object_detection_count(const rs2_frame *frame, rs2_error **error)
Definition: rs_sensor.h:57
gpu_frame(const frame &f)
Definition: rs_frame.hpp:1049
Definition: rs_types.h:162
frame(frame &&other) noexcept
Definition: rs_frame.hpp:404
Definition: rs_frame.hpp:1219
object_detection_frame()
Definition: rs_frame.hpp:1175
Definition: rs_types.h:210
bool is() const
Definition: rs_frame.hpp:92
void export_to_ply(const std::string &fname, video_frame texture)
Definition: rs_frame.hpp:827
rs2_time_t rs2_get_frame_timestamp(const rs2_frame *frame, rs2_error **error)
int rs2_stream_profile_is(const rs2_stream_profile *mode, rs2_extension type, rs2_error **error)
Definition: rs_context.hpp:11
rs2_pixel * rs2_get_frame_texture_coordinates(const rs2_frame *frame, rs2_error **error)
rs2_vertex * rs2_get_frame_labeled_vertices(const rs2_frame *frame, rs2_error **error)
double get_timestamp() const
Definition: rs_frame.hpp:500
labeled_points get_labeled_point_cloud_frame() const
Definition: rs_frame.hpp:1315
bool is() const
Definition: rs_frame.hpp:615
size_t size() const
Definition: rs_frame.hpp:847
Definition: rs_frame.hpp:344
float u
Definition: rs_frame.hpp:778
int rs2_supports_frame_metadata(const rs2_frame *frame, rs2_frame_metadata_value frame_metadata, rs2_error **error)
Definition: rs_types.h:217
Definition: rs_processing.hpp:250
size_t size() const
Definition: rs_frame.hpp:911
object_detection_frame(const frame &f)
Definition: rs_frame.hpp:1181
rs2_stream_profile * rs2_clone_stream_profile(const rs2_stream_profile *mode, rs2_stream stream, int index, rs2_format format, rs2_error **error)
float z
Definition: rs_frame.hpp:774
int fps() const
Definition: rs_frame.hpp:49
Definition: rs_frame.hpp:857
void reset()
Definition: rs_frame.hpp:656
iterator & operator++()
Definition: rs_frame.hpp:1466
Definition: rs_types.h:216
struct rs2_sensor rs2_sensor
Definition: rs_types.h:319
labeled_points(const frame &f)
Definition: rs_frame.hpp:869
Definition: rs_sensor.h:51
rs2_extrinsics get_extrinsics_to(const stream_profile &to) const
Definition: rs_frame.hpp:157
int rs2_get_frame_height(const rs2_frame *frame, rs2_error **error)
std::shared_ptr< rs2_stream_profile > _clone
Definition: rs_frame.hpp:199
pose_frame(const frame &f)
Definition: rs_frame.hpp:1120
object_detection_frame get_object_detection_frame(const size_t index=0) const
Definition: rs_frame.hpp:1393
void swap(frame &other)
Definition: rs_frame.hpp:439
perception_stream_profile(const stream_profile &sp)
Definition: rs_frame.hpp:347
Definition: rs_types.h:170
Definition: rs_frame.hpp:1454
frame & operator=(frame other)
Definition: rs_frame.hpp:416
Definition: rs_sensor.h:76
Definition: rs_frame.hpp:1075
frame_callback(T on_frame)
Definition: rs_frame.hpp:1487
int rs2_get_frame_stride_in_bytes(const rs2_frame *frame, rs2_error **error)
Definition: rs_frame.hpp:1483
rs2_vector get_motion_data() const
Definition: rs_frame.hpp:1097
frame(rs2_frame *ref)
Definition: rs_frame.hpp:380
const rs2_stream_profile * _profile
Definition: rs_frame.hpp:198
frame first_or_default(rs2_stream s, rs2_format f=RS2_FORMAT_ANY) const
Definition: rs_frame.hpp:1254
rs2::frame process(rs2::frame frame) const override
Definition: rs_processing.hpp:369
video_frame(const frame &f)
Definition: rs_frame.hpp:688
Definition: rs_frame.hpp:1046
void rs2_get_stream_profile_data(const rs2_stream_profile *mode, rs2_stream *stream, rs2_format *format, int *index, int *unique_id, int *framerate, rs2_error **error)
void * rs2_get_frame_labels(const rs2_frame *frame, rs2_error **error)
points()
Definition: rs_frame.hpp:788
std::forward_iterator_tag iterator_category
Definition: rs_frame.hpp:1459
float get_distance(int x, int y) const
Definition: rs_frame.hpp:980
video_frame get_color_frame(const size_t index=0) const
Definition: rs_frame.hpp:1291
Definition: rs_frame.hpp:1144
Definition: rs_processing.hpp:17
int rs2_embedded_frames_count(rs2_frame *composite, rs2_error **error)
int rs2_is_stream_profile_default(const rs2_stream_profile *mode, rs2_error **error)
int _framerate
Definition: rs_frame.hpp:203
Definition: rs_types.h:163
motion_frame(const frame &f)
Definition: rs_frame.hpp:1082
Definition: rs_types.h:220
~frame()
Definition: rs_frame.hpp:452
int rs2_get_frame_width(const rs2_frame *frame, rs2_error **error)
Definition: rs_frame.hpp:1169
float rs2_depth_frame_get_distance(const rs2_frame *frame_ref, int x, int y, rs2_error **error)
frame operator[](size_t index) const
Definition: rs_frame.hpp:1441
RS2_STREAM_MOTION / RS2_FORMAT_COMBINED_MOTION content is similar to ROS2&#39;s Imu message.
Definition: rs_sensor.h:118
Definition: rs_types.h:161
rs2_timestamp_domain rs2_get_frame_timestamp_domain(const rs2_frame *frameset, rs2_error **error)
Definition: rs_types.h:110
rs2_format _format
Definition: rs_frame.hpp:204
void rs2_delete_stream_profile(rs2_stream_profile *mode)
motion_stream_profile(const stream_profile &sp)
Definition: rs_frame.hpp:300
Definition: rs_types.h:169
void foreach_rs(T action) const
Definition: rs_frame.hpp:1424
rs2_timestamp_domain get_frame_timestamp_domain() const
Definition: rs_frame.hpp:511
bool extract_target_dimensions(rs2_calib_target_type calib_type, float *target_dims, unsigned int target_dims_size) const
Definition: rs_frame.hpp:764
unsigned int get_detection_count() const
Definition: rs_frame.hpp:1196
T as() const
Definition: rs_frame.hpp:103
T as() const
Definition: rs_frame.hpp:625
video_stream_profile(const stream_profile &sp)
Definition: rs_frame.hpp:219
struct rs2_stream_profile rs2_stream_profile
Definition: rs_types.h:306
Definition: rs_sensor.h:71
unsigned int get_width() const
Definition: rs_frame.hpp:920
virtual ~filter_interface()=default
rs2_format
A stream&#39;s format identifies how binary data is encoded within a frame.
Definition: rs_sensor.h:69
unsigned int rs2_get_frame_labeled_points_height(const rs2_frame *frame, rs2_error **error)
Definition: rs_processing.hpp:429
bool is_default() const
Definition: rs_frame.hpp:134
void keep()
Definition: rs_frame.hpp:463
unsigned long long rs2_get_frame_number(const rs2_frame *frame, rs2_error **error)
int stream_index() const
Definition: rs_frame.hpp:34
video_frame get_infrared_frame(const size_t index=0) const
Definition: rs_frame.hpp:1326
const texture_coordinate * get_texture_coordinates() const
Definition: rs_frame.hpp:839
int get_bits_per_pixel() const
Definition: rs_frame.hpp:740
void rs2_get_frame_object_detection(const rs2_frame *frame, unsigned int index, rs2_object_detection *detection, rs2_error **error)
unsigned int get_height() const
Definition: rs_frame.hpp:932
bool operator==(const video_stream_profile &other) const
Definition: rs_frame.hpp:258
bool operator==(const iterator &other) const
Definition: rs_frame.hpp:1467
static void handle(rs2_error *e)
Definition: rs_types.hpp:167
Definition: rs_sensor.h:50
rs2_sensor * get_sensor()
Definition: rs_frame.hpp:471
int rs2_get_frame_data_size(const rs2_frame *frame, rs2_error **error)
rs2_combined_motion get_combined_motion_data() const
Definition: rs_frame.hpp:1107
rs2_stream
Streams are different types of data provided by RealSense devices.
Definition: rs_sensor.h:47
void rs2_extract_target_dimensions(const rs2_frame *frame, rs2_calib_target_type calib_type, float *target_dims, unsigned int target_dims_size, rs2_error **error)
const uint8_t * get_labels() const
Definition: rs_frame.hpp:902
Definition: rs_sensor.h:63
Definition: rs_processing.hpp:360
int get_height() const
Definition: rs_frame.hpp:716
unsigned int rs2_get_frame_labeled_points_bits_per_pixel(const rs2_frame *frame, rs2_error **error)
const rs2_stream_profile * get() const
Definition: rs_frame.hpp:146
bool _default
Definition: rs_frame.hpp:207
Definition: rs_frame.hpp:1001
void rs2_get_video_stream_intrinsics(const rs2_stream_profile *mode, rs2_intrinsics *intrinsics, rs2_error **error)
Definition: rs_sensor.h:52
unsigned long long get_frame_number() const
Definition: rs_frame.hpp:547
Cross-stream extrinsics: encodes the topology describing how the different devices are oriented...
Definition: rs_sensor.h:111
Definition: rs_types.h:159
iterator end() const
Definition: rs_frame.hpp:1477
iterator(const frameset *owner, size_t index=0)
Definition: rs_frame.hpp:1465
bool is_cloned()
Definition: rs_frame.hpp:178
Definition: rs_types.h:219
3D vector in Euclidean coordinate space
Definition: rs_types.h:99
virtual rs2::frame process(rs2::frame frame) const =0
int get_width() const
Definition: rs_frame.hpp:704
const void * get_data() const
Definition: rs_frame.hpp:571
const char * rs2_stream_to_string(rs2_stream stream)
void on_frame(rs2_frame *fref) override
Definition: rs_frame.hpp:1489
Definition: rs_sensor.h:49
long long rs2_metadata_type
Definition: rs_types.h:343
float get_units() const
Definition: rs_frame.hpp:992
points(const frame &f)
Definition: rs_frame.hpp:794
Definition: rs_frame.hpp:362
Definition: rs_types.h:166
int get_stride_in_bytes() const
Definition: rs_frame.hpp:728
unsigned int rs2_get_frame_labeled_points_width(const rs2_frame *frame, rs2_error **error)
rs2_vertex * rs2_get_frame_vertices(const rs2_frame *frame, rs2_error **error)
disparity_frame(const frame &f)
Definition: rs_frame.hpp:1008
int width() const
Definition: rs_frame.hpp:236
void rs2_frame_add_ref(rs2_frame *frame, rs2_error **error)
Video stream intrinsics.
Definition: rs_types.h:60
rs2_stream_profile * rs2_clone_video_stream_profile(const rs2_stream_profile *mode, rs2_stream stream, int index, rs2_format format, int width, int height, const rs2_intrinsics *intr, rs2_error **error)
Definition: rs_processing.hpp:699
int height() const
Definition: rs_frame.hpp:241
const char * rs2_get_stream_profile_name(const rs2_stream_profile *profile, rs2_error **error)
rs2_pose get_pose_data() const
Definition: rs_frame.hpp:1134
bool operator!=(const iterator &other) const
Definition: rs_frame.hpp:1468
unsigned int get_bits_per_pixel() const
Definition: rs_frame.hpp:944
bool operator==(const stream_profile &rhs)
Definition: rs_frame.hpp:79
Motion device intrinsics: scale, bias, and variances.
Definition: rs_types.h:73
depth_frame get_depth_frame() const
Definition: rs_frame.hpp:1282
Definition: rs_processing.hpp:133
float rs2_depth_stereo_frame_get_baseline(const rs2_frame *frame_ref, rs2_error **error)
stream_profile(const rs2_stream_profile *profile)
Definition: rs_frame.hpp:179
void rs2_release_frame(rs2_frame *frame)
rs2_metadata_type get_frame_metadata(rs2_frame_metadata_value frame_metadata) const
Definition: rs_frame.hpp:523
frameset(const frame &f)
Definition: rs_frame.hpp:1230
Definition: rs_frame.hpp:777
int rs2_get_frame_labeled_points_count(const rs2_frame *frame, rs2_error **error)
rs2_frame * rs2_extract_frame(rs2_frame *composite, int index, rs2_error **error)
size_t size() const
Definition: rs_frame.hpp:1414
Definition: rs_types.h:171
rs2_metadata_type rs2_get_frame_metadata(const rs2_frame *frame, rs2_frame_metadata_value frame_metadata, rs2_error **error)
Definition: rs_types.h:172
labeled_points()
Definition: rs_frame.hpp:863
struct rs2_error rs2_error
Definition: rs_types.h:295
bool supports_frame_metadata(rs2_frame_metadata_value frame_metadata) const
Definition: rs_frame.hpp:535
rs2_stream stream_type() const
Definition: rs_frame.hpp:39
float x
Definition: rs_frame.hpp:774
Definition: rs_frame.hpp:210
Definition: rs_frame.hpp:1113
frame(const frame &other)
Definition: rs_frame.hpp:426
rs2_intrinsics get_intrinsics() const
Definition: rs_frame.hpp:249
const int get_data_size() const
Definition: rs_frame.hpp:559
void rs2_get_motion_intrinsics(const rs2_stream_profile *mode, rs2_motion_device_intrinsic *intrinsics, rs2_error **error)
video_frame get_fisheye_frame(const size_t index=0) const
Definition: rs_frame.hpp:1349
rs2_frame * get() const
Definition: rs_frame.hpp:635
void rs2_pose_frame_get_pose_data(const rs2_frame *frame, rs2_pose *pose, rs2_error **error)
frame()
Definition: rs_frame.hpp:375
const void * get_gpu_data_or_upload(bool *copied=nullptr) const
Definition: rs_frame.hpp:588
Definition: rs_sensor.h:62
float v
Definition: rs_frame.hpp:778
stream_profile clone(rs2_stream type, int index, rs2_format format) const
Definition: rs_frame.hpp:63
perception_frame(const frame &f)
Definition: rs_frame.hpp:1158
int rs2_get_frame_bits_per_pixel(const rs2_frame *frame, rs2_error **error)
rs2_frame_metadata_value
Per-Frame-Metadata is the set of read-only properties that might be exposed for each individual frame...
Definition: rs_frame.h:29
rs2_object_detection get_detection(unsigned int index) const
Definition: rs_frame.hpp:1209
struct rs2_frame rs2_frame
Definition: rs_types.h:298
const void * rs2_get_frame_data(const rs2_frame *frame, rs2_error **error)
perception_frame()
Definition: rs_frame.hpp:1150
std::ptrdiff_t difference_type
Definition: rs_frame.hpp:1461
float get_baseline(void) const
Definition: rs_frame.hpp:1022
Object detection result from algorithm.
Definition: rs_types.h:123
pose_stream_profile(const stream_profile &sp)
Definition: rs_frame.hpp:332
rs2_stream _type
Definition: rs_frame.hpp:205
Definition: rs_frame.hpp:773
stream_profile get_profile() const
Definition: rs_frame.hpp:602
const vertex * get_vertices() const
Definition: rs_frame.hpp:814
Definition: rs_frame.hpp:956
rs2_timestamp_domain
Specifies the clock in relation to which the frame timestamp was measured.
Definition: rs_frame.h:19
int unique_id() const
Definition: rs_frame.hpp:54