Open3D (C++ API)  0.18.0
Messages.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <array>
11 #include <cstring>
12 #include <map>
13 #include <msgpack.hpp>
14 #include <string>
15 #include <vector>
16 
17 #include "open3d/core/Tensor.h"
18 
19 namespace open3d {
20 namespace io {
21 namespace rpc {
22 namespace messages {
23 
24 inline std::string EndiannessStr() {
25  auto IsLittleEndian = []() -> bool {
26  uint32_t a = 1;
27  uint8_t b;
28  // Use memcpy as a reliable way to access a single byte.
29  // Other approaches, e.g. union, often rely on undefined behaviour.
30  std::memcpy(&b, &a, sizeof(uint8_t));
31  return b == 1;
32  };
33 
34  return IsLittleEndian() ? "<" : ">";
35 }
36 
39 template <class T>
40 inline std::string TypeStr() {
41  return "";
42 }
43 template <>
44 inline std::string TypeStr<float>() {
45  return EndiannessStr() + "f4";
46 }
47 template <>
48 inline std::string TypeStr<double>() {
49  return EndiannessStr() + "f8";
50 }
51 template <>
52 inline std::string TypeStr<int8_t>() {
53  return "|i1";
54 }
55 template <>
56 inline std::string TypeStr<int16_t>() {
57  return EndiannessStr() + "i2";
58 }
59 template <>
60 inline std::string TypeStr<int32_t>() {
61  return EndiannessStr() + "i4";
62 }
63 template <>
64 inline std::string TypeStr<int64_t>() {
65  return EndiannessStr() + "i8";
66 }
67 template <>
68 inline std::string TypeStr<uint8_t>() {
69  return "|u1";
70 }
71 template <>
72 inline std::string TypeStr<uint16_t>() {
73  return EndiannessStr() + "u2";
74 }
75 template <>
76 inline std::string TypeStr<uint32_t>() {
77  return EndiannessStr() + "u4";
78 }
79 template <>
80 inline std::string TypeStr<uint64_t>() {
81  return EndiannessStr() + "u8";
82 }
83 
104 struct Array {
105  static std::string MsgId() { return "array"; }
106 
109  template <class T>
110  static Array FromPtr(const T* const ptr,
111  const std::vector<int64_t>& shape) {
112  Array arr;
113  arr.type = TypeStr<T>();
114  arr.shape = shape;
115  arr.data.ptr = (const char*)ptr;
116  int64_t num = 1;
117  for (int64_t n : shape) num *= n;
118  arr.data.size = uint32_t(sizeof(T) * num);
119  return arr;
120  }
121 
125  static Array FromTensor(const core::Tensor& tensor) {
126  // We require the tensor to be contiguous and to use the CPU.
127  auto t = tensor.To(core::Device("CPU:0")).Contiguous();
128  auto a = DISPATCH_DTYPE_TO_TEMPLATE(t.GetDtype(), [&]() {
129  auto arr = messages::Array::FromPtr(
130  (scalar_t*)t.GetDataPtr(),
131  static_cast<std::vector<int64_t>>(t.GetShape()));
132  arr.tensor_ = t;
133  return arr;
134  });
135  return a;
136  }
137 
138  // Object for keeping a reference to the tensor. not meant to be serialized.
140 
141  std::string type;
142  std::vector<int64_t> shape;
143  msgpack::type::raw_ref data;
144 
145  template <class T>
146  const T* Ptr() const {
147  return (T*)data.ptr;
148  }
149 
152  bool CheckRank(const std::vector<int>& expected_ranks,
153  std::string& errstr) const {
154  for (auto rank : expected_ranks) {
155  if (shape.size() == size_t(rank)) return true;
156  }
157  errstr += " expected rank to be in (";
158  for (auto rank : expected_ranks) {
159  errstr += std::to_string(rank) + ", ";
160  }
161  errstr += std::string(")") + " but got shape [";
162  for (auto d : shape) {
163  errstr += std::to_string(d) + ", ";
164  }
165  errstr += "]";
166  return false;
167  }
168  bool CheckRank(const std::vector<int>& expected_ranks) const {
169  std::string _;
170  return CheckRank(expected_ranks, _);
171  }
172 
176  bool CheckShape(const std::vector<int64_t>& expected_shape,
177  std::string& errstr) const {
178  if (!CheckRank({int(expected_shape.size())}, errstr)) {
179  return false;
180  }
181 
182  for (size_t i = 0; i < expected_shape.size(); ++i) {
183  int64_t d_expected = expected_shape[i];
184  int64_t d = shape[i];
185  if ((d_expected != -1 && d_expected != d) || d < 0) {
186  errstr += " expected shape [";
187  for (auto d : expected_shape) {
188  if (d != -1) {
189  errstr += "?, ";
190  } else {
191  errstr += std::to_string(d) + ", ";
192  }
193  }
194  errstr += "] but got [";
195  for (auto d : shape) {
196  errstr += std::to_string(d) + ", ";
197  }
198  errstr += "]";
199  return false;
200  }
201  }
202  return true;
203  }
204  bool CheckShape(const std::vector<int64_t>& expected_shape) const {
205  std::string _;
206  return CheckShape(expected_shape, _);
207  }
208 
212  bool CheckNonEmpty(std::string& errstr) const {
213  int64_t n = 1;
214  for (auto d : shape) n *= d;
215  if (0 == n || shape.empty()) {
216  errstr += " expected non empty array but got array with shape [";
217  for (auto d : shape) {
218  errstr += std::to_string(d) + ", ";
219  }
220  errstr += "]";
221  return false;
222  }
223  return true;
224  }
225  bool CheckNonEmpty() const {
226  std::string _;
227  return CheckNonEmpty(_);
228  }
229 
233  bool CheckType(const std::vector<std::string>& expected_types,
234  std::string& errstr) const {
235  for (const auto& t : expected_types) {
236  if (t == type) return true;
237  }
238  errstr += " expected array type to be one of (";
239  for (const auto& t : expected_types) {
240  errstr += t + ", ";
241  }
242  errstr += ") but got " + type;
243  return false;
244  }
245  bool CheckType(const std::vector<std::string>& expected_types) const {
246  std::string _;
247  return CheckType(expected_types, _);
248  }
249 
250  // macro for creating the serialization/deserialization code
252 };
253 
255 struct MeshData {
256  static std::string MsgId() { return "mesh_data"; }
257 
262  std::string o3d_type;
263 
268  std::map<std::string, Array> vertex_attributes;
269 
278  std::map<std::string, Array> face_attributes;
279 
289  std::map<std::string, Array> line_attributes;
290 
292  std::string material = "";
294  std::map<std::string, float> material_scalar_attributes;
296  std::map<std::string, std::array<float, 4>> material_vector_attributes;
298  std::map<std::string, Array> texture_maps;
299 
300  void SetO3DTypeToPointCloud() { o3d_type = "PointCloud"; }
301  void SetO3DTypeToLineSet() { o3d_type = "LineSet"; }
302  void SetO3DTypeToTriangleMesh() { o3d_type = "TriangleMesh"; }
303 
304  bool O3DTypeIsPointCloud() const { return o3d_type == "PointCloud"; }
305  bool O3DTypeIsLineSet() const { return o3d_type == "LineSet"; }
306  bool O3DTypeIsTriangleMesh() const { return o3d_type == "TriangleMesh"; }
307 
308  bool CheckVertices(std::string& errstr) const {
309  if (vertices.shape.empty()) return true;
310  std::string tmp = "invalid vertices array:";
311  bool status = vertices.CheckNonEmpty(tmp) &&
312  vertices.CheckShape({-1, 3}, tmp);
313  if (!status) errstr += tmp;
314  return status;
315  }
316 
317  bool CheckFaces(std::string& errstr) const {
318  if (faces.shape.empty()) return true;
319 
320  std::string tmp = "invalid faces array:";
321 
322  bool status = faces.CheckRank({1, 2}, tmp);
323  if (!status) {
324  errstr += tmp;
325  return false;
326  }
327 
328  status = faces.CheckType({TypeStr<int32_t>(), TypeStr<int64_t>()}, tmp);
329  if (!status) {
330  errstr += tmp;
331  return false;
332  }
333 
334  if (faces.CheckRank({1, 2})) {
335  status = faces.CheckNonEmpty(tmp);
336  if (!status) {
337  errstr += tmp;
338  return false;
339  }
340  }
341 
342  if (faces.CheckRank({2})) {
343  status = faces.shape[1] > 2;
344  tmp += " expected shape [?, >2] but got [" +
345  std::to_string(faces.shape[0]) + ", " +
346  std::to_string(faces.shape[1]) + "]";
347  if (!status) {
348  errstr += tmp;
349  return false;
350  }
351  }
352  return status;
353  }
354 
355  bool CheckO3DType(std::string& errstr) const {
356  if (o3d_type.empty() || O3DTypeIsPointCloud() || O3DTypeIsLineSet() ||
358  return true;
359  } else {
360  errstr +=
361  " invalid o3d_type. Expected 'PointCloud', 'TriangleMesh', "
362  "or 'LineSet' but got '" +
363  o3d_type + "'.";
364  return false;
365  }
366  }
367 
368  bool CheckMessage(std::string& errstr) const {
369  std::string tmp = "invalid mesh_data message:";
370  bool status =
371  CheckO3DType(tmp) && CheckVertices(tmp) && CheckFaces(tmp);
372  if (!status) errstr += tmp;
373  return status;
374  }
375 
377  vertices,
379  faces,
381  lines,
383  material,
386  texture_maps);
387 };
388 
391 struct SetMeshData {
392  static std::string MsgId() { return "set_mesh_data"; }
393 
394  SetMeshData() : time(0) {}
395 
397  std::string path;
401  std::string layer;
402 
405 
407 };
408 
410 struct GetMeshData {
411  static std::string MsgId() { return "get_mesh_data"; }
412 
413  GetMeshData() : time(0) {}
414 
416  std::string path;
420  std::string layer;
421 
423 };
424 
426 struct CameraData {
427  static std::string MsgId() { return "camera_data"; }
428 
429  CameraData() : width(0), height(0) {}
430 
431  // extrinsic parameters defining the world to camera transform, i.e.,
432  // X_cam = X_world * R + t
433 
435  std::array<double, 4> R;
437  std::array<double, 3> t;
438 
442  std::string intrinsic_model;
443  std::vector<double> intrinsic_parameters;
444 
446  int width;
447  int height;
448 
450  std::map<std::string, Array> images;
451 
454 };
455 
459  static std::string MsgId() { return "set_camera_data"; }
460 
461  SetCameraData() : time(0) {}
462 
464  std::string path;
468  std::string layer;
469 
472 
474 };
475 
478 struct SetTime {
479  static std::string MsgId() { return "set_time"; }
480  SetTime() : time(0) {}
482 
484 };
485 
489  static std::string MsgId() { return "set_active_camera"; }
490  std::string path;
491 
493 };
494 
498  static std::string MsgId() { return "set_properties"; }
499  std::string path;
500 
501  // application specific members go here.
502 
504 };
505 
508 struct Request {
509  std::string msg_id;
511 };
512 
515 struct Reply {
516  std::string msg_id;
518 };
519 
522 struct Status {
523  static std::string MsgId() { return "status"; }
524 
525  Status() : code(0) {}
526  Status(int code, const std::string& str) : code(code), str(str) {}
527  static Status OK() { return Status(); }
529  return Status(1, "unsupported msg_id");
530  }
532  return Status(2, "error during unpacking");
533  }
535  return Status(3, "error while processing message");
536  }
537 
541  std::string str;
542 
544 };
545 
546 } // namespace messages
547 } // namespace rpc
548 } // namespace io
549 } // namespace open3d
#define DISPATCH_DTYPE_TO_TEMPLATE(DTYPE,...)
Definition: Dispatch.h:30
Definition: Device.h:18
Definition: Tensor.h:32
Tensor Contiguous() const
Definition: Tensor.cpp:740
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:707
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:548
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:474
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:395
std::string TypeStr()
Definition: Messages.h:40
std::string TypeStr< int8_t >()
Definition: Messages.h:52
std::string TypeStr< uint64_t >()
Definition: Messages.h:80
std::string TypeStr< int16_t >()
Definition: Messages.h:56
std::string EndiannessStr()
Definition: Messages.h:24
std::string TypeStr< float >()
Definition: Messages.h:44
std::string TypeStr< double >()
Definition: Messages.h:48
std::string TypeStr< uint8_t >()
Definition: Messages.h:68
std::string TypeStr< int32_t >()
Definition: Messages.h:60
std::string TypeStr< int64_t >()
Definition: Messages.h:64
std::string TypeStr< uint32_t >()
Definition: Messages.h:76
std::string TypeStr< uint16_t >()
Definition: Messages.h:72
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Messages.h:104
bool CheckNonEmpty() const
Definition: Messages.h:225
bool CheckRank(const std::vector< int > &expected_ranks, std::string &errstr) const
Definition: Messages.h:152
const T * Ptr() const
Definition: Messages.h:146
MSGPACK_DEFINE_MAP(type, shape, data)
static Array FromTensor(const core::Tensor &tensor)
Definition: Messages.h:125
bool CheckNonEmpty(std::string &errstr) const
Definition: Messages.h:212
bool CheckType(const std::vector< std::string > &expected_types) const
Definition: Messages.h:245
std::string type
Definition: Messages.h:141
static Array FromPtr(const T *const ptr, const std::vector< int64_t > &shape)
Definition: Messages.h:110
bool CheckShape(const std::vector< int64_t > &expected_shape, std::string &errstr) const
Definition: Messages.h:176
static std::string MsgId()
Definition: Messages.h:105
bool CheckRank(const std::vector< int > &expected_ranks) const
Definition: Messages.h:168
msgpack::type::raw_ref data
Definition: Messages.h:143
bool CheckType(const std::vector< std::string > &expected_types, std::string &errstr) const
Definition: Messages.h:233
bool CheckShape(const std::vector< int64_t > &expected_shape) const
Definition: Messages.h:204
core::Tensor tensor_
Definition: Messages.h:139
std::vector< int64_t > shape
Definition: Messages.h:142
struct for storing camera data
Definition: Messages.h:426
std::array< double, 4 > R
rotation R as quaternion [x,y,z,w]
Definition: Messages.h:435
std::vector< double > intrinsic_parameters
Definition: Messages.h:443
int height
Definition: Messages.h:447
MSGPACK_DEFINE_MAP(R, t, intrinsic_model, intrinsic_parameters, width, height, images)
static std::string MsgId()
Definition: Messages.h:427
std::array< double, 3 > t
translation
Definition: Messages.h:437
int width
image dimensions in pixels
Definition: Messages.h:446
std::map< std::string, Array > images
map of arrays that can be interpreted as camera images
Definition: Messages.h:450
std::string intrinsic_model
Definition: Messages.h:442
CameraData()
Definition: Messages.h:429
struct for defining a "get_mesh_data" message, which requests mesh data.
Definition: Messages.h:410
std::string path
Path defining the location in the scene tree.
Definition: Messages.h:416
static std::string MsgId()
Definition: Messages.h:411
int32_t time
The time for which to return the data.
Definition: Messages.h:418
GetMeshData()
Definition: Messages.h:413
std::string layer
The layer for which to return the data.
Definition: Messages.h:420
struct for storing MeshData, e.g., PointClouds, TriangleMesh, ..
Definition: Messages.h:255
std::map< std::string, Array > face_attributes
stores arbitrary attributes for each face
Definition: Messages.h:278
std::map< std::string, Array > line_attributes
stores arbitrary attributes for each line
Definition: Messages.h:289
std::string material
Material for DrawableGeometry.
Definition: Messages.h:292
void SetO3DTypeToLineSet()
Definition: Messages.h:301
std::string o3d_type
Definition: Messages.h:262
Array faces
Definition: Messages.h:276
bool CheckO3DType(std::string &errstr) const
Definition: Messages.h:355
Array lines
Definition: Messages.h:287
bool CheckMessage(std::string &errstr) const
Definition: Messages.h:368
void SetO3DTypeToTriangleMesh()
Definition: Messages.h:302
std::map< std::string, Array > texture_maps
map of arrays that can be interpreted as textures
Definition: Messages.h:298
std::map< std::string, float > material_scalar_attributes
Material scalar properties.
Definition: Messages.h:294
bool CheckVertices(std::string &errstr) const
Definition: Messages.h:308
Array vertices
shape must be [num_verts,3]
Definition: Messages.h:265
MSGPACK_DEFINE_MAP(o3d_type, vertices, vertex_attributes, faces, face_attributes, lines, line_attributes, material, material_scalar_attributes, material_vector_attributes, texture_maps)
static std::string MsgId()
Definition: Messages.h:256
std::map< std::string, Array > vertex_attributes
Definition: Messages.h:268
bool O3DTypeIsPointCloud() const
Definition: Messages.h:304
std::map< std::string, std::array< float, 4 > > material_vector_attributes
Material vector[4] properties.
Definition: Messages.h:296
void SetO3DTypeToPointCloud()
Definition: Messages.h:300
bool CheckFaces(std::string &errstr) const
Definition: Messages.h:317
bool O3DTypeIsTriangleMesh() const
Definition: Messages.h:306
bool O3DTypeIsLineSet() const
Definition: Messages.h:305
Definition: Messages.h:515
std::string msg_id
Definition: Messages.h:516
Definition: Messages.h:508
std::string msg_id
Definition: Messages.h:509
static std::string MsgId()
Definition: Messages.h:489
std::string path
Definition: Messages.h:490
int32_t time
The time for which to return the data.
Definition: Messages.h:466
static std::string MsgId()
Definition: Messages.h:459
CameraData data
The data to be set.
Definition: Messages.h:471
std::string layer
The layer for which to return the data.
Definition: Messages.h:468
MSGPACK_DEFINE_MAP(path, time, layer, data)
SetCameraData()
Definition: Messages.h:461
std::string path
Path defining the location in the scene tree.
Definition: Messages.h:464
Definition: Messages.h:391
SetMeshData()
Definition: Messages.h:394
std::string layer
The layer for this data.
Definition: Messages.h:401
MSGPACK_DEFINE_MAP(path, time, layer, data)
std::string path
Path defining the location in the scene tree.
Definition: Messages.h:397
int32_t time
The time associated with this data.
Definition: Messages.h:399
static std::string MsgId()
Definition: Messages.h:392
MeshData data
The data to be set.
Definition: Messages.h:404
static std::string MsgId()
Definition: Messages.h:498
std::string path
Definition: Messages.h:499
Definition: Messages.h:478
int32_t time
Definition: Messages.h:481
SetTime()
Definition: Messages.h:480
static std::string MsgId()
Definition: Messages.h:479
Definition: Messages.h:522
Status()
Definition: Messages.h:525
static Status ErrorUnsupportedMsgId()
Definition: Messages.h:528
static Status ErrorProcessingMessage()
Definition: Messages.h:534
std::string str
string representation of the code
Definition: Messages.h:541
static std::string MsgId()
Definition: Messages.h:523
static Status ErrorUnpackingFailed()
Definition: Messages.h:531
int32_t code
return code. 0 means everything is OK.
Definition: Messages.h:539
static Status OK()
Definition: Messages.h:527
Status(int code, const std::string &str)
Definition: Messages.h:526