Open3D (C++ API)  0.16.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) 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
38namespace open3d {
39namespace io {
40namespace rpc {
41namespace messages {
42
43inline 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
58template <class T>
59inline std::string TypeStr() {
60 return "";
61}
62template <>
63inline std::string TypeStr<float>() {
64 return EndiannessStr() + "f4";
65}
66template <>
67inline std::string TypeStr<double>() {
68 return EndiannessStr() + "f8";
69}
70template <>
71inline std::string TypeStr<int8_t>() {
72 return "|i1";
73}
74template <>
75inline std::string TypeStr<int16_t>() {
76 return EndiannessStr() + "i2";
77}
78template <>
79inline std::string TypeStr<int32_t>() {
80 return EndiannessStr() + "i4";
81}
82template <>
83inline std::string TypeStr<int64_t>() {
84 return EndiannessStr() + "i8";
85}
86template <>
87inline std::string TypeStr<uint8_t>() {
88 return "|u1";
89}
90template <>
91inline std::string TypeStr<uint16_t>() {
92 return EndiannessStr() + "u2";
93}
94template <>
95inline std::string TypeStr<uint32_t>() {
96 return EndiannessStr() + "u4";
97}
98template <>
99inline std::string TypeStr<uint64_t>() {
100 return EndiannessStr() + "u8";
101}
102
123struct 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
271};
272
274struct 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() ||
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
396 vertices,
398 faces,
400 lines,
402 material,
406};
407
411 static std::string MsgId() { return "set_mesh_data"; }
412
414
416 std::string path;
420 std::string layer;
421
424
426};
427
430 static std::string MsgId() { return "get_mesh_data"; }
431
433
435 std::string path;
439 std::string layer;
440
442};
443
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;
467
469 std::map<std::string, Array> images;
470
473};
474
478 static std::string MsgId() { return "set_camera_data"; }
479
481
483 std::string path;
487 std::string layer;
488
491
493};
494
497struct SetTime {
498 static std::string MsgId() { return "set_time"; }
499 SetTime() : time(0) {}
501
503};
504
508 static std::string MsgId() { return "set_active_camera"; }
509 std::string path;
510
512};
513
517 static std::string MsgId() { return "set_properties"; }
518 std::string path;
519
520 // application specific members go here.
521
523};
524
527struct Request {
528 std::string msg_id;
530};
531
534struct Reply {
535 std::string msg_id;
537};
538
541struct 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
563};
564
565} // namespace messages
566} // namespace rpc
567} // namespace io
568} // namespace open3d
#define DISPATCH_DTYPE_TO_TEMPLATE(DTYPE,...)
Definition: Dispatch.h:49
Definition: Device.h:37
Definition: Tensor.h:51
Tensor Contiguous() const
Definition: Tensor.cpp:758
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:725
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:567
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:493
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:414
std::string TypeStr()
Definition: Messages.h:59
std::string TypeStr< int8_t >()
Definition: Messages.h:71
std::string TypeStr< uint64_t >()
Definition: Messages.h:99
std::string TypeStr< int16_t >()
Definition: Messages.h:75
std::string EndiannessStr()
Definition: Messages.h:43
std::string TypeStr< float >()
Definition: Messages.h:63
std::string TypeStr< double >()
Definition: Messages.h:67
std::string TypeStr< uint8_t >()
Definition: Messages.h:87
std::string TypeStr< int32_t >()
Definition: Messages.h:79
std::string TypeStr< int64_t >()
Definition: Messages.h:83
std::string TypeStr< uint32_t >()
Definition: Messages.h:95
std::string TypeStr< uint16_t >()
Definition: Messages.h:91
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Messages.h:123
bool CheckNonEmpty() const
Definition: Messages.h:244
bool CheckRank(const std::vector< int > &expected_ranks, std::string &errstr) const
Definition: Messages.h:171
MSGPACK_DEFINE_MAP(type, shape, data)
static Array FromTensor(const core::Tensor &tensor)
Definition: Messages.h:144
bool CheckNonEmpty(std::string &errstr) const
Definition: Messages.h:231
bool CheckType(const std::vector< std::string > &expected_types) const
Definition: Messages.h:264
std::string type
Definition: Messages.h:160
static Array FromPtr(const T *const ptr, const std::vector< int64_t > &shape)
Definition: Messages.h:129
const T * Ptr() const
Definition: Messages.h:165
bool CheckShape(const std::vector< int64_t > &expected_shape, std::string &errstr) const
Definition: Messages.h:195
static std::string MsgId()
Definition: Messages.h:124
bool CheckRank(const std::vector< int > &expected_ranks) const
Definition: Messages.h:187
msgpack::type::raw_ref data
Definition: Messages.h:162
bool CheckType(const std::vector< std::string > &expected_types, std::string &errstr) const
Definition: Messages.h:252
bool CheckShape(const std::vector< int64_t > &expected_shape) const
Definition: Messages.h:223
core::Tensor tensor_
Definition: Messages.h:158
std::vector< int64_t > shape
Definition: Messages.h:161
struct for storing camera data
Definition: Messages.h:445
std::array< double, 4 > R
rotation R as quaternion [x,y,z,w]
Definition: Messages.h:454
std::vector< double > intrinsic_parameters
Definition: Messages.h:462
int height
Definition: Messages.h:466
MSGPACK_DEFINE_MAP(R, t, intrinsic_model, intrinsic_parameters, width, height, images)
static std::string MsgId()
Definition: Messages.h:446
std::array< double, 3 > t
translation
Definition: Messages.h:456
int width
image dimensions in pixels
Definition: Messages.h:465
std::map< std::string, Array > images
map of arrays that can be interpreted as camera images
Definition: Messages.h:469
std::string intrinsic_model
Definition: Messages.h:461
CameraData()
Definition: Messages.h:448
struct for defining a "get_mesh_data" message, which requests mesh data.
Definition: Messages.h:429
std::string path
Path defining the location in the scene tree.
Definition: Messages.h:435
static std::string MsgId()
Definition: Messages.h:430
int32_t time
The time for which to return the data.
Definition: Messages.h:437
GetMeshData()
Definition: Messages.h:432
std::string layer
The layer for which to return the data.
Definition: Messages.h:439
struct for storing MeshData, e.g., PointClouds, TriangleMesh, ..
Definition: Messages.h:274
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 material
Material for DrawableGeometry.
Definition: Messages.h:311
void SetO3DTypeToLineSet()
Definition: Messages.h:320
std::string o3d_type
Definition: Messages.h:281
Array faces
Definition: Messages.h:295
bool CheckO3DType(std::string &errstr) const
Definition: Messages.h:374
Array lines
Definition: Messages.h:306
bool CheckMessage(std::string &errstr) const
Definition: Messages.h:387
void SetO3DTypeToTriangleMesh()
Definition: Messages.h:321
std::map< std::string, Array > texture_maps
map of arrays that can be interpreted as textures
Definition: Messages.h:317
std::map< std::string, float > material_scalar_attributes
Material scalar properties.
Definition: Messages.h:313
bool CheckVertices(std::string &errstr) const
Definition: Messages.h:327
Array vertices
shape must be [num_verts,3]
Definition: Messages.h:284
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:275
std::map< std::string, Array > vertex_attributes
Definition: Messages.h:287
bool O3DTypeIsPointCloud() const
Definition: Messages.h:323
std::map< std::string, std::array< float, 4 > > material_vector_attributes
Material vector[4] properties.
Definition: Messages.h:315
void SetO3DTypeToPointCloud()
Definition: Messages.h:319
bool CheckFaces(std::string &errstr) const
Definition: Messages.h:336
bool O3DTypeIsTriangleMesh() const
Definition: Messages.h:325
bool O3DTypeIsLineSet() const
Definition: Messages.h:324
Definition: Messages.h:534
std::string msg_id
Definition: Messages.h:535
Definition: Messages.h:527
std::string msg_id
Definition: Messages.h:528
static std::string MsgId()
Definition: Messages.h:508
std::string path
Definition: Messages.h:509
int32_t time
The time for which to return the data.
Definition: Messages.h:485
static std::string MsgId()
Definition: Messages.h:478
CameraData data
The data to be set.
Definition: Messages.h:490
std::string layer
The layer for which to return the data.
Definition: Messages.h:487
MSGPACK_DEFINE_MAP(path, time, layer, data)
SetCameraData()
Definition: Messages.h:480
std::string path
Path defining the location in the scene tree.
Definition: Messages.h:483
Definition: Messages.h:410
SetMeshData()
Definition: Messages.h:413
std::string layer
The layer for this data.
Definition: Messages.h:420
MSGPACK_DEFINE_MAP(path, time, layer, data)
std::string path
Path defining the location in the scene tree.
Definition: Messages.h:416
int32_t time
The time associated with this data.
Definition: Messages.h:418
static std::string MsgId()
Definition: Messages.h:411
MeshData data
The data to be set.
Definition: Messages.h:423
static std::string MsgId()
Definition: Messages.h:517
std::string path
Definition: Messages.h:518
Definition: Messages.h:497
int32_t time
Definition: Messages.h:500
SetTime()
Definition: Messages.h:499
static std::string MsgId()
Definition: Messages.h:498
Definition: Messages.h:541
Status()
Definition: Messages.h:544
static Status ErrorUnsupportedMsgId()
Definition: Messages.h:547
static Status ErrorProcessingMessage()
Definition: Messages.h:553
std::string str
string representation of the code
Definition: Messages.h:560
static std::string MsgId()
Definition: Messages.h:542
static Status ErrorUnpackingFailed()
Definition: Messages.h:550
int32_t code
return code. 0 means everything is OK.
Definition: Messages.h:558
static Status OK()
Definition: Messages.h:546
Status(int code, const std::string &str)
Definition: Messages.h:545