Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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) 2018-2021 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 
29 #include <array>
30 #include <cstring>
31 #include <map>
32 #include <msgpack.hpp>
33 #include <string>
34 #include <vector>
35 
36 #include "open3d/core/Tensor.h"
37 
38 namespace open3d {
39 namespace io {
40 namespace rpc {
41 namespace messages {
42 
43 inline std::string EndiannessStr() {
44  auto IsLittleEndian = []() -> bool {
45  uint32_t a = 1;
46  uint8_t b;
47  // Use memcpy as a reliable way to access a single byte.
48  // Other approaches, e.g. union, often rely on undefined behaviour.
49  std::memcpy(&b, &a, sizeof(uint8_t));
50  return b == 1;
51  };
52 
53  return IsLittleEndian() ? "<" : ">";
54 }
55 
58 template <class T>
59 inline std::string TypeStr() {
60  return "";
61 }
62 template <>
63 inline std::string TypeStr<float>() {
64  return EndiannessStr() + "f4";
65 }
66 template <>
67 inline std::string TypeStr<double>() {
68  return EndiannessStr() + "f8";
69 }
70 template <>
71 inline std::string TypeStr<int8_t>() {
72  return "|i1";
73 }
74 template <>
75 inline std::string TypeStr<int16_t>() {
76  return EndiannessStr() + "i2";
77 }
78 template <>
79 inline std::string TypeStr<int32_t>() {
80  return EndiannessStr() + "i4";
81 }
82 template <>
83 inline std::string TypeStr<int64_t>() {
84  return EndiannessStr() + "i8";
85 }
86 template <>
87 inline std::string TypeStr<uint8_t>() {
88  return "|u1";
89 }
90 template <>
91 inline std::string TypeStr<uint16_t>() {
92  return EndiannessStr() + "u2";
93 }
94 template <>
95 inline std::string TypeStr<uint32_t>() {
96  return EndiannessStr() + "u4";
97 }
98 template <>
99 inline std::string TypeStr<uint64_t>() {
100  return EndiannessStr() + "u8";
101 }
102 
123 struct Array {
124  static std::string MsgId() { return "array"; }
125 
128  template <class T>
129  static Array FromPtr(const T* const ptr,
130  const std::vector<int64_t>& shape) {
131  Array arr;
132  arr.type = TypeStr<T>();
133  arr.shape = shape;
134  arr.data.ptr = (const char*)ptr;
135  int64_t num = 1;
136  for (int64_t n : shape) num *= n;
137  arr.data.size = uint32_t(sizeof(T) * num);
138  return arr;
139  }
140 
144  static Array FromTensor(const core::Tensor& tensor) {
145  // We require the tensor to be contiguous and to use the CPU.
146  auto t = tensor.To(core::Device("CPU:0")).Contiguous();
147  auto a = DISPATCH_DTYPE_TO_TEMPLATE(t.GetDtype(), [&]() {
148  auto arr = messages::Array::FromPtr(
149  (scalar_t*)t.GetDataPtr(),
150  static_cast<std::vector<int64_t>>(t.GetShape()));
151  arr.tensor_ = t;
152  return arr;
153  });
154  return a;
155  }
156 
157  // Object for keeping a reference to the tensor. not meant to be serialized.
159 
160  std::string type;
161  std::vector<int64_t> shape;
162  msgpack::type::raw_ref data;
163 
164  template <class T>
165  const T* Ptr() const {
166  return (T*)data.ptr;
167  }
168 
171  bool CheckRank(const std::vector<int>& expected_ranks,
172  std::string& errstr) const {
173  for (auto rank : expected_ranks) {
174  if (shape.size() == size_t(rank)) return true;
175  }
176  errstr += " expected rank to be in (";
177  for (auto rank : expected_ranks) {
178  errstr += std::to_string(rank) + ", ";
179  }
180  errstr += std::string(")") + " but got shape [";
181  for (auto d : shape) {
182  errstr += std::to_string(d) + ", ";
183  }
184  errstr += "]";
185  return false;
186  }
187  bool CheckRank(const std::vector<int>& expected_ranks) const {
188  std::string _;
189  return CheckRank(expected_ranks, _);
190  }
191 
195  bool CheckShape(const std::vector<int64_t>& expected_shape,
196  std::string& errstr) const {
197  if (!CheckRank({int(expected_shape.size())}, errstr)) {
198  return false;
199  }
200 
201  for (size_t i = 0; i < expected_shape.size(); ++i) {
202  int64_t d_expected = expected_shape[i];
203  int64_t d = shape[i];
204  if ((d_expected != -1 && d_expected != d) || d < 0) {
205  errstr += " expected shape [";
206  for (auto d : expected_shape) {
207  if (d != -1) {
208  errstr += "?, ";
209  } else {
210  errstr += std::to_string(d) + ", ";
211  }
212  }
213  errstr += "] but got [";
214  for (auto d : shape) {
215  errstr += std::to_string(d) + ", ";
216  }
217  errstr += "]";
218  return false;
219  }
220  }
221  return true;
222  }
223  bool CheckShape(const std::vector<int64_t>& expected_shape) const {
224  std::string _;
225  return CheckShape(expected_shape, _);
226  }
227 
231  bool CheckNonEmpty(std::string& errstr) const {
232  int64_t n = 1;
233  for (auto d : shape) n *= d;
234  if (0 == n || shape.empty()) {
235  errstr += " expected non empty array but got array with shape [";
236  for (auto d : shape) {
237  errstr += std::to_string(d) + ", ";
238  }
239  errstr += "]";
240  return false;
241  }
242  return true;
243  }
244  bool CheckNonEmpty() const {
245  std::string _;
246  return CheckNonEmpty(_);
247  }
248 
252  bool CheckType(const std::vector<std::string>& expected_types,
253  std::string& errstr) const {
254  for (const auto& t : expected_types) {
255  if (t == type) return true;
256  }
257  errstr += " expected array type to be one of (";
258  for (const auto& t : expected_types) {
259  errstr += t + ", ";
260  }
261  errstr += ") but got " + type;
262  return false;
263  }
264  bool CheckType(const std::vector<std::string>& expected_types) const {
265  std::string _;
266  return CheckType(expected_types, _);
267  }
268 
269  // macro for creating the serialization/deserialization code
270  MSGPACK_DEFINE_MAP(type, shape, data);
271 };
272 
274 struct MeshData {
275  static std::string MsgId() { return "mesh_data"; }
276 
281  std::string o3d_type;
282 
287  std::map<std::string, Array> vertex_attributes;
288 
297  std::map<std::string, Array> face_attributes;
298 
308  std::map<std::string, Array> line_attributes;
309 
311  std::string material = "";
313  std::map<std::string, float> material_scalar_attributes;
315  std::map<std::string, std::array<float, 4>> material_vector_attributes;
317  std::map<std::string, Array> texture_maps;
318 
319  void SetO3DTypeToPointCloud() { o3d_type = "PointCloud"; }
320  void SetO3DTypeToLineSet() { o3d_type = "LineSet"; }
321  void SetO3DTypeToTriangleMesh() { o3d_type = "TriangleMesh"; }
322 
323  bool O3DTypeIsPointCloud() const { return o3d_type == "PointCloud"; }
324  bool O3DTypeIsLineSet() const { return o3d_type == "LineSet"; }
325  bool O3DTypeIsTriangleMesh() const { return o3d_type == "TriangleMesh"; }
326 
327  bool CheckVertices(std::string& errstr) const {
328  if (vertices.shape.empty()) return true;
329  std::string tmp = "invalid vertices array:";
330  bool status = vertices.CheckNonEmpty(tmp) &&
331  vertices.CheckShape({-1, 3}, tmp);
332  if (!status) errstr += tmp;
333  return status;
334  }
335 
336  bool CheckFaces(std::string& errstr) const {
337  if (faces.shape.empty()) return true;
338 
339  std::string tmp = "invalid faces array:";
340 
341  bool status = faces.CheckRank({1, 2}, tmp);
342  if (!status) {
343  errstr += tmp;
344  return false;
345  }
346 
347  status = faces.CheckType({TypeStr<int32_t>(), TypeStr<int64_t>()}, tmp);
348  if (!status) {
349  errstr += tmp;
350  return false;
351  }
352 
353  if (faces.CheckRank({1, 2})) {
354  status = faces.CheckNonEmpty(tmp);
355  if (!status) {
356  errstr += tmp;
357  return false;
358  }
359  }
360 
361  if (faces.CheckRank({2})) {
362  status = faces.shape[1] > 2;
363  tmp += " expected shape [?, >2] but got [" +
364  std::to_string(faces.shape[0]) + ", " +
365  std::to_string(faces.shape[1]) + "]";
366  if (!status) {
367  errstr += tmp;
368  return false;
369  }
370  }
371  return status;
372  }
373 
374  bool CheckO3DType(std::string& errstr) const {
375  if (o3d_type.empty() || O3DTypeIsPointCloud() || O3DTypeIsLineSet() ||
376  O3DTypeIsTriangleMesh()) {
377  return true;
378  } else {
379  errstr +=
380  " invalid o3d_type. Expected 'PointCloud', 'TriangleMesh', "
381  "or 'LineSet' but got '" +
382  o3d_type + "'.";
383  return false;
384  }
385  }
386 
387  bool CheckMessage(std::string& errstr) const {
388  std::string tmp = "invalid mesh_data message:";
389  bool status =
390  CheckO3DType(tmp) && CheckVertices(tmp) && CheckFaces(tmp);
391  if (!status) errstr += tmp;
392  return status;
393  }
394 
395  MSGPACK_DEFINE_MAP(o3d_type,
396  vertices,
397  vertex_attributes,
398  faces,
399  face_attributes,
400  lines,
401  line_attributes,
402  material,
403  material_scalar_attributes,
404  material_vector_attributes,
405  texture_maps);
406 };
407 
410 struct SetMeshData {
411  static std::string MsgId() { return "set_mesh_data"; }
412 
413  SetMeshData() : time(0) {}
414 
416  std::string path;
420  std::string layer;
421 
424 
425  MSGPACK_DEFINE_MAP(path, time, layer, data);
426 };
427 
429 struct GetMeshData {
430  static std::string MsgId() { return "get_mesh_data"; }
431 
432  GetMeshData() : time(0) {}
433 
435  std::string path;
439  std::string layer;
440 
441  MSGPACK_DEFINE_MAP(path, time, layer);
442 };
443 
445 struct CameraData {
446  static std::string MsgId() { return "camera_data"; }
447 
448  CameraData() : width(0), height(0) {}
449 
450  // extrinsic parameters defining the world to camera transform, i.e.,
451  // X_cam = X_world * R + t
452 
454  std::array<double, 4> R;
456  std::array<double, 3> t;
457 
461  std::string intrinsic_model;
462  std::vector<double> intrinsic_parameters;
463 
465  int width;
466  int height;
467 
469  std::map<std::string, Array> images;
470 
472  R, t, intrinsic_model, intrinsic_parameters, width, height, images);
473 };
474 
478  static std::string MsgId() { return "set_camera_data"; }
479 
480  SetCameraData() : time(0) {}
481 
483  std::string path;
487  std::string layer;
488 
491 
492  MSGPACK_DEFINE_MAP(path, time, layer, data);
493 };
494 
497 struct SetTime {
498  static std::string MsgId() { return "set_time"; }
499  SetTime() : time(0) {}
501 
502  MSGPACK_DEFINE_MAP(time);
503 };
504 
508  static std::string MsgId() { return "set_active_camera"; }
509  std::string path;
510 
511  MSGPACK_DEFINE_MAP(path);
512 };
513 
517  static std::string MsgId() { return "set_properties"; }
518  std::string path;
519 
520  // application specific members go here.
521 
522  MSGPACK_DEFINE_MAP(path);
523 };
524 
527 struct Request {
528  std::string msg_id;
529  MSGPACK_DEFINE_MAP(msg_id);
530 };
531 
534 struct Reply {
535  std::string msg_id;
536  MSGPACK_DEFINE_MAP(msg_id);
537 };
538 
541 struct Status {
542  static std::string MsgId() { return "status"; }
543 
544  Status() : code(0) {}
545  Status(int code, const std::string& str) : code(code), str(str) {}
546  static Status OK() { return Status(); }
548  return Status(1, "unsupported msg_id");
549  }
551  return Status(2, "error during unpacking");
552  }
554  return Status(3, "error while processing message");
555  }
556 
560  std::string str;
561 
562  MSGPACK_DEFINE_MAP(code, str);
563 };
564 
565 } // namespace messages
566 } // namespace rpc
567 } // namespace io
568 } // namespace open3d
std::string msg_id
Definition: Messages.h:528
std::string msg_id
Definition: Messages.h:535
int height
Definition: FilePCD.cpp:72
bool CheckNonEmpty() const
Definition: Messages.h:244
bool CheckType(const std::vector< std::string > &expected_types, std::string &errstr) const
Definition: Messages.h:252
std::string TypeStr< float >()
Definition: Messages.h:63
std::string TypeStr< uint8_t >()
Definition: Messages.h:87
std::string path
Definition: Messages.h:518
Definition: Messages.h:123
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:485
const T * Ptr() const
Definition: Messages.h:165
static Array FromTensor(const core::Tensor &tensor)
Definition: Messages.h:144
static std::string MsgId()
Definition: Messages.h:446
std::string path
Path defining the location in the scene tree.
Definition: Messages.h:416
bool CheckMessage(std::string &errstr) const
Definition: Messages.h:387
std::string TypeStr()
Definition: Messages.h:59
Status(int code, const std::string &str)
Definition: Messages.h:545
CameraData data
The data to be set.
Definition: Messages.h:490
std::string TypeStr< int64_t >()
Definition: Messages.h:83
int32_t time
The time associated with this data.
Definition: Messages.h:418
struct for storing MeshData, e.g., PointClouds, TriangleMesh, ..
Definition: Messages.h:274
static Status ErrorProcessingMessage()
Definition: Messages.h:553
Definition: Messages.h:541
std::string TypeStr< int8_t >()
Definition: Messages.h:71
static std::string MsgId()
Definition: Messages.h:124
std::string path
Definition: Messages.h:509
void SetO3DTypeToTriangleMesh()
Definition: Messages.h:321
static std::string MsgId()
Definition: Messages.h:275
Definition: Messages.h:410
struct for storing camera data
Definition: Messages.h:445
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
void SetO3DTypeToPointCloud()
Definition: Messages.h:319
msgpack::type::raw_ref data
Definition: Messages.h:162
bool O3DTypeIsTriangleMesh() const
Definition: Messages.h:325
std::map< std::string, Array > texture_maps
map of arrays that can be interpreted as textures
Definition: Messages.h:317
Definition: Messages.h:527
std::string path
Path defining the location in the scene tree.
Definition: Messages.h:435
bool CheckVertices(std::string &errstr) const
Definition: Messages.h:327
Definition: Messages.h:497
bool CheckShape(const std::vector< int64_t > &expected_shape, std::string &errstr) const
Definition: Messages.h:195
std::string path
Path defining the location in the scene tree.
Definition: Messages.h:483
bool CheckNonEmpty(std::string &errstr) const
Definition: Messages.h:231
GetMeshData()
Definition: Messages.h:432
static Status OK()
Definition: Messages.h:546
MSGPACK_DEFINE_MAP(type, shape, data)
std::vector< int64_t > shape
Definition: Messages.h:161
Array vertices
shape must be [num_verts,3]
Definition: Messages.h:284
Status()
Definition: Messages.h:544
static Status ErrorUnsupportedMsgId()
Definition: Messages.h:547
static std::string MsgId()
Definition: Messages.h:411
int height
Definition: Messages.h:466
bool CheckFaces(std::string &errstr) const
Definition: Messages.h:336
std::string TypeStr< uint16_t >()
Definition: Messages.h:91
std::map< std::string, Array > face_attributes
stores arbitrary attributes for each face
Definition: Messages.h:297
std::map< std::string, Array > line_attributes
stores arbitrary attributes for each line
Definition: Messages.h:308
std::string TypeStr< uint32_t >()
Definition: Messages.h:95
std::string layer
The layer for which to return the data.
Definition: Messages.h:487
std::string TypeStr< uint64_t >()
Definition: Messages.h:99
bool O3DTypeIsPointCloud() const
Definition: Messages.h:323
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:713
Tensor Contiguous() const
Definition: Tensor.cpp:746
bool CheckRank(const std::vector< int > &expected_ranks) const
Definition: Messages.h:187
static std::string MsgId()
Definition: Messages.h:430
bool CheckType(const std::vector< std::string > &expected_types) const
Definition: Messages.h:264
std::map< std::string, std::array< float, 4 > > material_vector_attributes
Material vector[4] properties.
Definition: Messages.h:315
Definition: Device.h:39
static std::string MsgId()
Definition: Messages.h:478
Array lines
Definition: Messages.h:306
std::vector< double > intrinsic_parameters
Definition: Messages.h:462
std::string TypeStr< int16_t >()
Definition: Messages.h:75
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:437
std::string intrinsic_model
Definition: Messages.h:461
static std::string MsgId()
Definition: Messages.h:517
static std::string MsgId()
Definition: Messages.h:508
std::string TypeStr< int32_t >()
Definition: Messages.h:79
static std::string MsgId()
Definition: Messages.h:498
std::string EndiannessStr()
Definition: Messages.h:43
SetTime()
Definition: Messages.h:499
bool CheckO3DType(std::string &errstr) const
Definition: Messages.h:374
std::string str
string representation of the code
Definition: Messages.h:560
CameraData()
Definition: Messages.h:448
std::map< std::string, Array > images
map of arrays that can be interpreted as camera images
Definition: Messages.h:469
SetMeshData()
Definition: Messages.h:413
#define DISPATCH_DTYPE_TO_TEMPLATE(DTYPE,...)
Definition: Dispatch.h:49
int32_t time
Definition: Messages.h:500
Definition: PinholeCameraIntrinsic.cpp:35
core::Tensor tensor_
Definition: Messages.h:158
std::string o3d_type
Definition: Messages.h:281
void SetO3DTypeToLineSet()
Definition: Messages.h:320
std::string layer
The layer for this data.
Definition: Messages.h:420
bool O3DTypeIsLineSet() const
Definition: Messages.h:324
std::array< double, 3 > t
translation
Definition: Messages.h:456
struct for defining a "get_mesh_data" message, which requests mesh data.
Definition: Messages.h:429
bool CheckShape(const std::vector< int64_t > &expected_shape) const
Definition: Messages.h:223
std::string layer
The layer for which to return the data.
Definition: Messages.h:439
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:423
bool CheckRank(const std::vector< int > &expected_ranks, std::string &errstr) const
Definition: Messages.h:171
SetCameraData()
Definition: Messages.h:480
static Array FromPtr(const T *const ptr, const std::vector< int64_t > &shape)
Definition: Messages.h:129
Definition: Messages.h:534
int width
image dimensions in pixels
Definition: Messages.h:465
std::map< std::string, float > material_scalar_attributes
Material scalar properties.
Definition: Messages.h:313
Array faces
Definition: Messages.h:295
std::array< double, 4 > R
rotation R as quaternion [x,y,z,w]
Definition: Messages.h:454
static Status ErrorUnpackingFailed()
Definition: Messages.h:550
static std::string MsgId()
Definition: Messages.h:542
std::map< std::string, Array > vertex_attributes
Definition: Messages.h:287
int width
Definition: FilePCD.cpp:71
int32_t code
return code. 0 means everything is OK.
Definition: Messages.h:558
std::string type
Definition: Messages.h:160
std::string TypeStr< double >()
Definition: Messages.h:67