Open3D (C++ API)  0.12.0
Messages.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2020 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 #include <boost/predef/other/endian.h>
29 
30 #include <map>
31 #include <msgpack.hpp>
32 #include <string>
33 #include <vector>
34 
35 #if BOOST_ENDIAN_LITTLE_BYTE
36 #define ENDIANNESS_STR "<"
37 #elif BOOST_ENDIAN_BIG_BYTE
38 #define ENDIANNESS_STR ">"
39 #else
40 #error Cannot determine endianness
41 #endif
42 
43 namespace open3d {
44 namespace io {
45 namespace rpc {
46 namespace messages {
47 
50 template <class T>
51 inline std::string TypeStr() {
52  return "";
53 }
54 template <>
55 inline std::string TypeStr<float>() {
56  return ENDIANNESS_STR "f4";
57 }
58 template <>
59 inline std::string TypeStr<double>() {
60  return ENDIANNESS_STR "f8";
61 }
62 template <>
63 inline std::string TypeStr<int8_t>() {
64  return "|i1";
65 }
66 template <>
67 inline std::string TypeStr<int16_t>() {
68  return ENDIANNESS_STR "i2";
69 }
70 template <>
71 inline std::string TypeStr<int32_t>() {
72  return ENDIANNESS_STR "i4";
73 }
74 template <>
75 inline std::string TypeStr<int64_t>() {
76  return ENDIANNESS_STR "i8";
77 }
78 template <>
79 inline std::string TypeStr<uint8_t>() {
80  return "|u1";
81 }
82 template <>
83 inline std::string TypeStr<uint16_t>() {
84  return ENDIANNESS_STR "u2";
85 }
86 template <>
87 inline std::string TypeStr<uint32_t>() {
88  return ENDIANNESS_STR "u4";
89 }
90 template <>
91 inline std::string TypeStr<uint64_t>() {
92  return ENDIANNESS_STR "u8";
93 }
94 
95 #undef ENDIANNESS_STR
96 
117 struct Array {
118  static std::string MsgId() { return "array"; }
119 
120  template <class T>
121  static Array FromPtr(const T* const ptr,
122  const std::vector<int64_t>& shape) {
123  Array arr;
124  arr.type = TypeStr<T>();
125  arr.shape = shape;
126  arr.data.ptr = (const char*)ptr;
127  int64_t num = 1;
128  for (int64_t n : shape) num *= n;
129  arr.data.size = uint32_t(sizeof(T) * num);
130  return arr;
131  }
132  std::string type;
133  std::vector<int64_t> shape;
134  msgpack::type::raw_ref data;
135 
136  template <class T>
137  const T* Ptr() const {
138  return (T*)data.ptr;
139  }
140 
143  bool CheckRank(const std::vector<int>& expected_ranks,
144  std::string& errstr) const {
145  for (auto rank : expected_ranks) {
146  if (shape.size() == size_t(rank)) return true;
147  }
148  errstr += " expected rank to be in (";
149  for (auto rank : expected_ranks) {
150  errstr += std::to_string(rank) + ", ";
151  }
152  errstr += std::string(")") + " but got shape [";
153  for (auto d : shape) {
154  errstr += std::to_string(d) + ", ";
155  }
156  errstr += "]";
157  return false;
158  }
159  bool CheckRank(const std::vector<int>& expected_ranks) const {
160  std::string _;
161  return CheckRank(expected_ranks, _);
162  }
163 
167  bool CheckShape(const std::vector<int64_t>& expected_shape,
168  std::string& errstr) const {
169  if (!CheckRank({int(expected_shape.size())}, errstr)) {
170  return false;
171  }
172 
173  for (size_t i = 0; i < expected_shape.size(); ++i) {
174  int64_t d_expected = expected_shape[i];
175  int64_t d = shape[i];
176  if ((d_expected != -1 && d_expected != d) || d < 0) {
177  errstr += " expected shape [";
178  for (auto d : expected_shape) {
179  if (d != -1) {
180  errstr += "?, ";
181  } else {
182  errstr += std::to_string(d) + ", ";
183  }
184  }
185  errstr += "] but got [";
186  for (auto d : shape) {
187  errstr += std::to_string(d) + ", ";
188  }
189  errstr += "]";
190  return false;
191  }
192  }
193  return true;
194  }
195  bool CheckShape(const std::vector<int64_t>& expected_shape) const {
196  std::string _;
197  return CheckShape(expected_shape, _);
198  }
199 
203  bool CheckNonEmpty(std::string& errstr) const {
204  int64_t n = 1;
205  for (auto d : shape) n *= d;
206  if (0 == n || shape.empty()) {
207  errstr += " expected non empty array but got array with shape [";
208  for (auto d : shape) {
209  errstr += std::to_string(d) + ", ";
210  }
211  errstr += "]";
212  return false;
213  }
214  return true;
215  }
216  bool CheckNonEmpty() const {
217  std::string _;
218  return CheckNonEmpty(_);
219  }
220 
224  bool CheckType(const std::vector<std::string>& expected_types,
225  std::string& errstr) const {
226  for (const auto& t : expected_types) {
227  if (t == type) return true;
228  }
229  errstr += " expected array type to be one of (";
230  for (const auto& t : expected_types) {
231  errstr += t + ", ";
232  }
233  errstr += ") but got " + type;
234  return false;
235  }
236  bool CheckType(const std::vector<std::string>& expected_types) const {
237  std::string _;
238  return CheckType(expected_types, _);
239  }
240 
241  // macro for creating the serialization/deserialization code
242  MSGPACK_DEFINE_MAP(type, shape, data);
243 };
244 
246 struct MeshData {
247  static std::string MsgId() { return "mesh_data"; }
248 
253  std::map<std::string, Array> vertex_attributes;
254 
263  std::map<std::string, Array> face_attributes;
264 
274  std::map<std::string, Array> line_attributes;
275 
277  std::map<std::string, Array> textures;
278 
279  bool CheckVertices(std::string& errstr) const {
280  std::string tmp = "invalid vertices array:";
281  bool status = vertices.CheckNonEmpty(tmp) &&
282  vertices.CheckShape({-1, 3}, tmp);
283  if (!status) errstr += tmp;
284  return status;
285  }
286 
287  bool CheckFaces(std::string& errstr) const {
288  if (faces.shape.empty()) return true;
289 
290  std::string tmp = "invalid faces array:";
291 
292  bool status = faces.CheckRank({1, 2}, tmp);
293  if (!status) {
294  errstr += tmp;
295  return false;
296  }
297 
298  status = faces.CheckType({TypeStr<int32_t>(), TypeStr<int64_t>()}, tmp);
299  if (!status) {
300  errstr += tmp;
301  return false;
302  }
303 
304  if (faces.CheckRank({1, 2})) {
305  status = faces.CheckNonEmpty(tmp);
306  if (!status) {
307  errstr += tmp;
308  return false;
309  }
310  }
311 
312  if (faces.CheckRank({2})) {
313  status = faces.shape[1] > 2;
314  tmp += " expected shape [?, >2] but got [" +
315  std::to_string(faces.shape[0]) + ", " +
316  std::to_string(faces.shape[1]) + "]";
317  if (!status) {
318  errstr += tmp;
319  return false;
320  }
321  }
322  return status;
323  }
324 
325  bool CheckMessage(std::string& errstr) const {
326  std::string tmp = "invalid mesh_data message:";
327  bool status = CheckVertices(errstr) && CheckFaces(errstr);
328  if (!status) errstr += tmp;
329  return status;
330  }
331 
332  MSGPACK_DEFINE_MAP(vertices,
333  vertex_attributes,
334  faces,
335  face_attributes,
336  lines,
337  line_attributes,
338  textures);
339 };
340 
343 struct SetMeshData {
344  static std::string MsgId() { return "set_mesh_data"; }
345 
346  SetMeshData() : time(0) {}
347 
349  std::string path;
353  std::string layer;
354 
357 
358  MSGPACK_DEFINE_MAP(path, time, layer, data);
359 };
360 
362 struct GetMeshData {
363  static std::string MsgId() { return "get_mesh_data"; }
364 
365  GetMeshData() : time(0) {}
366 
368  std::string path;
372  std::string layer;
373 
374  MSGPACK_DEFINE_MAP(path, time, layer);
375 };
376 
378 struct CameraData {
379  static std::string MsgId() { return "camera_data"; }
380 
381  CameraData() : width(0), height(0) {}
382 
383  // extrinsic parameters defining the world to camera transform, i.e.,
384  // X_cam = X_world * R + t
385 
387  std::array<double, 4> R;
389  std::array<double, 3> t;
390 
394  std::string intrinsic_model;
395  std::vector<double> intrinsic_parameters;
396 
398  int width;
399  int height;
400 
402  std::map<std::string, Array> images;
403 
405  R, t, intrinsic_model, intrinsic_parameters, width, height, images);
406 };
407 
411  static std::string MsgId() { return "set_camera_data"; }
412 
413  SetCameraData() : time(0) {}
414 
416  std::string path;
420  std::string layer;
421 
424 
425  MSGPACK_DEFINE_MAP(path, time, layer, data);
426 };
427 
430 struct SetTime {
431  static std::string MsgId() { return "set_time"; }
432  SetTime() : time(0) {}
434 
435  MSGPACK_DEFINE_MAP(time);
436 };
437 
441  static std::string MsgId() { return "set_active_camera"; }
442  std::string path;
443 
444  MSGPACK_DEFINE_MAP(path);
445 };
446 
450  static std::string MsgId() { return "set_properties"; }
451  std::string path;
452 
453  // application specific members go here.
454 
455  MSGPACK_DEFINE_MAP(path);
456 };
457 
460 struct Request {
461  std::string msg_id;
462  MSGPACK_DEFINE_MAP(msg_id);
463 };
464 
467 struct Reply {
468  std::string msg_id;
469  MSGPACK_DEFINE_MAP(msg_id);
470 };
471 
474 struct Status {
475  static std::string MsgId() { return "status"; }
476 
477  Status() : code(0) {}
478  Status(int code, const std::string& str) : code(code), str(str) {}
479  static Status OK() { return Status(); }
481  return Status(1, "unsupported msg_id");
482  }
484  return Status(2, "error during unpacking");
485  }
487  return Status(3, "error while processing message");
488  }
489 
493  std::string str;
494 
495  MSGPACK_DEFINE_MAP(code, str);
496 };
497 
498 } // namespace messages
499 } // namespace rpc
500 } // namespace io
501 } // namespace open3d
std::string msg_id
Definition: Messages.h:461
std::string msg_id
Definition: Messages.h:468
bool CheckNonEmpty() const
Definition: Messages.h:216
bool CheckType(const std::vector< std::string > &expected_types, std::string &errstr) const
Definition: Messages.h:224
std::string TypeStr< float >()
Definition: Messages.h:55
std::string TypeStr< uint8_t >()
Definition: Messages.h:79
std::string path
Definition: Messages.h:451
Definition: Messages.h:117
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle uint32_t
Definition: K4aPlugin.cpp:557
int32_t time
The time for which to return the data.
Definition: Messages.h:418
const T * Ptr() const
Definition: Messages.h:137
static std::string MsgId()
Definition: Messages.h:379
std::string path
Path defining the location in the scene tree.
Definition: Messages.h:349
bool CheckMessage(std::string &errstr) const
Definition: Messages.h:325
std::string TypeStr()
Definition: Messages.h:51
Status(int code, const std::string &str)
Definition: Messages.h:478
CameraData data
The data to be set.
Definition: Messages.h:423
std::string TypeStr< int64_t >()
Definition: Messages.h:75
int32_t time
The time associated with this data.
Definition: Messages.h:351
struct for storing MeshData, e.g., PointClouds, TriangleMesh, ..
Definition: Messages.h:246
static Status ErrorProcessingMessage()
Definition: Messages.h:486
Definition: Messages.h:474
std::string TypeStr< int8_t >()
Definition: Messages.h:63
static std::string MsgId()
Definition: Messages.h:118
std::string path
Definition: Messages.h:442
static std::string MsgId()
Definition: Messages.h:247
Definition: Messages.h:343
struct for storing camera data
Definition: Messages.h:378
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t int32_t
Definition: K4aPlugin.cpp:398
msgpack::type::raw_ref data
Definition: Messages.h:134
Definition: Messages.h:460
std::string path
Path defining the location in the scene tree.
Definition: Messages.h:368
bool CheckVertices(std::string &errstr) const
Definition: Messages.h:279
Definition: Messages.h:430
bool CheckShape(const std::vector< int64_t > &expected_shape, std::string &errstr) const
Definition: Messages.h:167
std::string path
Path defining the location in the scene tree.
Definition: Messages.h:416
std::map< std::string, Array > textures
map of arrays that can be interpreted as textures
Definition: Messages.h:277
bool CheckNonEmpty(std::string &errstr) const
Definition: Messages.h:203
GetMeshData()
Definition: Messages.h:365
static Status OK()
Definition: Messages.h:479
MSGPACK_DEFINE_MAP(type, shape, data)
std::vector< int64_t > shape
Definition: Messages.h:133
Array vertices
shape must be [num_verts,3]
Definition: Messages.h:250
Status()
Definition: Messages.h:477
static Status ErrorUnsupportedMsgId()
Definition: Messages.h:480
static std::string MsgId()
Definition: Messages.h:344
int height
Definition: Messages.h:399
bool CheckFaces(std::string &errstr) const
Definition: Messages.h:287
std::string TypeStr< uint16_t >()
Definition: Messages.h:83
std::map< std::string, Array > face_attributes
stores arbitrary attributes for each face
Definition: Messages.h:263
std::map< std::string, Array > line_attributes
stores arbitrary attributes for each line
Definition: Messages.h:274
std::string TypeStr< uint32_t >()
Definition: Messages.h:87
std::string layer
The layer for which to return the data.
Definition: Messages.h:420
std::string TypeStr< uint64_t >()
Definition: Messages.h:91
bool CheckRank(const std::vector< int > &expected_ranks) const
Definition: Messages.h:159
static std::string MsgId()
Definition: Messages.h:363
bool CheckType(const std::vector< std::string > &expected_types) const
Definition: Messages.h:236
static std::string MsgId()
Definition: Messages.h:411
Array lines
Definition: Messages.h:272
std::vector< double > intrinsic_parameters
Definition: Messages.h:395
std::string TypeStr< int16_t >()
Definition: Messages.h:67
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c int
Definition: K4aPlugin.cpp:479
int32_t time
The time for which to return the data.
Definition: Messages.h:370
std::string intrinsic_model
Definition: Messages.h:394
static std::string MsgId()
Definition: Messages.h:450
static std::string MsgId()
Definition: Messages.h:441
std::string TypeStr< int32_t >()
Definition: Messages.h:71
static std::string MsgId()
Definition: Messages.h:431
SetTime()
Definition: Messages.h:432
std::string str
string representation of the code
Definition: Messages.h:493
CameraData()
Definition: Messages.h:381
std::map< std::string, Array > images
map of arrays that can be interpreted as camera images
Definition: Messages.h:402
SetMeshData()
Definition: Messages.h:346
int32_t time
Definition: Messages.h:433
Definition: PinholeCameraIntrinsic.cpp:35
std::string layer
The layer for this data.
Definition: Messages.h:353
int height
Definition: FilePCD.cpp:72
std::array< double, 3 > t
translation
Definition: Messages.h:389
struct for defining a "get_mesh_data" message, which requests mesh data.
Definition: Messages.h:362
bool CheckShape(const std::vector< int64_t > &expected_shape) const
Definition: Messages.h:195
std::string layer
The layer for which to return the data.
Definition: Messages.h:372
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle image_handle timestamp_usec white_balance image_handle k4a_device_configuration_t config device_handle char size_t serial_number_size bool int32_t int32_t int32_t int32_t k4a_color_control_mode_t default_mode value const const k4a_calibration_t calibration char size_t
Definition: K4aPlugin.cpp:724
MeshData data
The data to be set.
Definition: Messages.h:356
bool CheckRank(const std::vector< int > &expected_ranks, std::string &errstr) const
Definition: Messages.h:143
SetCameraData()
Definition: Messages.h:413
static Array FromPtr(const T *const ptr, const std::vector< int64_t > &shape)
Definition: Messages.h:121
Definition: Messages.h:467
int width
image dimensions in pixels
Definition: Messages.h:398
Array faces
Definition: Messages.h:261
std::array< double, 4 > R
rotation R as quaternion [x,y,z,w]
Definition: Messages.h:387
static Status ErrorUnpackingFailed()
Definition: Messages.h:483
static std::string MsgId()
Definition: Messages.h:475
std::map< std::string, Array > vertex_attributes
Definition: Messages.h:253
int32_t code
return code. 0 means everything is OK.
Definition: Messages.h:491
int width
Definition: FilePCD.cpp:71
std::string type
Definition: Messages.h:132
std::string TypeStr< double >()
Definition: Messages.h:59