visualization_meshcat_types.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef EXOTICA_CORE_VISUALIZATION_MESHCAT_TYPES_H_
31 #define EXOTICA_CORE_VISUALIZATION_MESHCAT_TYPES_H_
32 
33 #define MSGPACK_USE_DEFINE_MAP
34 
35 #include <iostream>
36 #include <msgpack.hpp>
37 
39 
40 #include <exotica_core/scene.h>
41 #include <exotica_core/tools.h>
42 
43 // Backwards compatibility switch 16.04 with meshpack version < 2.0.0
44 #if (MSGPACK_VERSION_MAJOR >= 2)
45 typedef msgpack::v1::type::raw_ref msgpack_raw_ref;
46 typedef msgpack::v1::type::ext msgpack_ext;
47 #else
48 typedef msgpack::type::raw_ref msgpack_raw_ref;
49 typedef std::vector<float> msgpack_ext;
50 #define MSGPACK_FEATURE_NOT_SUPPORTED
51 #endif
52 
53 namespace exotica
54 {
55 namespace visualization
56 {
57 inline long RGB(double R, double G, double B)
58 {
59  return static_cast<long>(std::min(std::max(R, 0.0), 1.0) * 255) * 65536L + static_cast<long>(std::min(std::max(G, 0.0), 1.0) * 255) * 256L + static_cast<long>(std::min(std::max(B, 0.0), 1.0) * 255);
60 }
61 
62 unsigned char random_char()
63 {
64  std::random_device rd;
65  std::mt19937 gen(rd());
66  std::uniform_int_distribution<> dis(0, 255);
67  return static_cast<unsigned char>(dis(gen));
68 }
69 
70 std::string generate_hex(const unsigned int len)
71 {
72  std::stringstream ss;
73  for (auto i = 0; i < len; ++i)
74  {
75  auto rc = random_char();
76  std::stringstream hexstream;
77  hexstream << std::hex << int(rc);
78  auto hex = hexstream.str();
79  ss << (hex.length() < 2 ? '0' + hex : hex);
80  }
81  return ss.str();
82 }
83 
84 std::string generate_uuid()
85 {
86  return generate_hex(4) + "-" + generate_hex(4) + "-" + generate_hex(4) + "-" + generate_hex(4);
87 }
88 
89 struct Base
90 {
91  Base() = default;
92  Base(const std::string& type_in, const std::string& path_in) : type(type_in), path(path_in){};
93  std::string type;
94  std::string path;
95 };
96 
97 template <typename T>
98 struct Property : Base
99 {
100  Property() : Base("set_property", ""){};
101  Property(const std::string& path_in, const std::string& property_in, const T& value_in) : Base("set_property", path_in), property(property_in), value(value_in){};
102  std::string property;
103  T value;
104  MSGPACK_DEFINE(type, path, property, value);
105 };
106 
107 struct MetaData
108 {
109  MetaData() = default;
110  MetaData(double version_in, const std::string& type_in) : version(version_in), type(type_in){};
111  double version;
112  std::string type;
113  MSGPACK_DEFINE(version, type);
114 };
115 
116 struct Material
117 {
118  Material(long color_in, double opacity_in = 1.0, const std::string& type_in = "MeshPhongMaterial", const std::string& uuid_in = "") : color(color_in), opacity(opacity_in), uuid(uuid_in), type(type_in)
119  {
120  if (uuid_in == "") uuid = generate_uuid();
121  };
123  {
124  uuid = "0000-0000-0000-0000"; // Default material UUID
125  };
126  std::string uuid = "";
127  std::string type = "MeshPhongMaterial";
128  long color = 16777215;
129  long ambient = 0;
130  long emissive = 0;
131  long specular = 1118481;
132  double shininess = 30.0;
133  double opacity = 1.0;
134  bool transparent = false;
135  bool wireframe = false;
136  MSGPACK_DEFINE(uuid, type, color, ambient, emissive, specular, shininess, opacity, transparent, wireframe);
137 };
138 
140 {
141  ObjectData() = default;
142  ObjectData(const std::string& type_in, const std::string& uuid_in,
143  const std::string& geometry_in, const std::string& material_in) : type(type_in), uuid(uuid_in), geometry(geometry_in), material(material_in){};
144  std::string type;
145  std::string uuid;
146  std::string geometry;
147  std::string material;
148  std::vector<double> matrix = {1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0};
149  MSGPACK_DEFINE(type, uuid, geometry, material, matrix);
150 };
151 
152 template <typename T>
153 struct Object
154 {
155  MetaData metadata = MetaData(4.5, "Object");
157  std::vector<Material> materials;
158  std::vector<T> geometries;
159  MSGPACK_DEFINE(metadata, geometries, materials, object);
160 };
161 
162 template <typename T>
164 {
165  MetaData metadata = MetaData(4.5, "Object");
167  std::vector<Material> materials;
168  std::vector<T> geometries;
169  MSGPACK_DEFINE(metadata, geometries, materials, object);
170 };
171 
172 template <typename T>
174 {
175  SetObjectType() = default;
176  SetObjectType(const std::string& path_in, const T& object_in) : type("set_object"), path(path_in), object(object_in){};
177  std::string type;
178  std::string path;
180  MSGPACK_DEFINE(type, path, object);
181 };
182 
183 template <typename T>
184 SetObjectType<T> SetObject(const std::string& path_in, const T& object_in)
185 {
186  return SetObjectType<T>(path_in, object_in);
187 };
188 
189 struct Geometry
190 {
191  Geometry() = default;
192  Geometry(const std::string& type_in, const std::string& uuid_in = "") : type(type_in), uuid(uuid_in)
193  {
194  if (uuid_in == "") uuid = generate_uuid();
195  };
196  std::string uuid;
197  std::string type;
198 };
199 
201 {
202  GeometryBox() : Geometry("BoxGeometry", ""){};
203  GeometryBox(double width_in, double height_in, double depth_in, const std::string& uuid_in = "") : Geometry("BoxGeometry", uuid_in), width(width_in), height(height_in), depth(depth_in){};
204  double width;
205  double height;
206  double depth;
207  MSGPACK_DEFINE(uuid, type, width, height, depth)
208 };
209 
211 {
212  GeometryCylinder() : Geometry("CylinderGeometry", ""){};
213  GeometryCylinder(double radius_in, double height_in, int radialSegments_in = 50, const std::string& uuid_in = "") : Geometry("CylinderGeometry", uuid_in), radiusTop(radius_in), radiusBottom(radius_in), height(height_in), radialSegments(radialSegments_in){};
214  double height;
215  double radiusTop;
216  double radiusBottom;
218  MSGPACK_DEFINE(uuid, type, height, radiusTop, radiusBottom, radialSegments)
219 };
220 
222 {
223  GeometrySphere() : Geometry("SphereGeometry", ""){};
224  GeometrySphere(double radius_in, int widthSegments_in = 50, int heightSegments_in = 50, const std::string& uuid_in = "") : Geometry("SphereGeometry", uuid_in), radius(radius_in), heightSegments(heightSegments_in), widthSegments(widthSegments_in){};
225  double radius;
228  MSGPACK_DEFINE(uuid, type, radius, widthSegments, heightSegments)
229 };
230 
232 {
233  GeometryMesh() : Geometry("_meshfile_geometry", ""){};
234 
235  GeometryMesh(const std::string& file_name_in, const std::string& url_in = "", const std::string& format_in = "", const std::string& uuid_in = "") : Geometry("_meshfile_geometry", uuid_in), format(format_in)
236  {
237  file_name = ParsePath(file_name_in);
238  if (format_in == "")
239  {
240  std::string::size_type pos = file_name.rfind(".");
241  if (pos != std::string::npos)
242  {
243  format = file_name.substr(pos + 1);
244  }
245  }
246  std::transform(format.begin(), format.end(), format.begin(), ::tolower);
247  url = ParsePath(url_in);
248  std::ifstream file(file_name, std::ios::binary | std::ios::ate);
249  std::streamsize size = file.tellg();
250  file.seekg(0, std::ios::beg);
251 
252  buffer.resize(size);
253  file.read(buffer.data(), size);
254 
255  data.ptr = buffer.data();
256  data.size = buffer.size();
257  }
258 
259  GeometryMesh(const GeometryMesh& other) : Geometry(other.type, other.uuid)
260  {
261  buffer = other.buffer;
262  url = other.url;
263  resources = other.resources;
264  matrix = other.matrix;
265  format = other.format;
266  data.ptr = buffer.data();
267  data.size = buffer.size();
268  }
269 
271  {
272  if (this != &other)
273  {
274  type = other.type;
275  uuid = other.uuid;
276  buffer = other.buffer;
277  url = other.url;
278  resources = other.resources;
279  matrix = other.matrix;
280  format = other.format;
281  data.ptr = buffer.data();
282  data.size = buffer.size();
283  }
284  return *this;
285  }
286 
287  std::vector<char> buffer;
288  std::string file_name;
289  std::string format;
291  std::string url;
292  std::map<std::string, std::string> resources;
293  std::vector<double> matrix = {1.0, 0.0, 0.0, 0.0,
294  0.0, 1.0, 0.0, 0.0,
295  0.0, 0.0, 1.0, 0.0,
296  0.0, 0.0, 0.0, 1.0};
297  MSGPACK_DEFINE(uuid, type, format, resources, url, data, matrix)
298 };
299 
301 {
302  ArrayFloat() = default;
303  ~ArrayFloat() = default;
304 
305  ArrayFloat(double* data_in, unsigned int size)
306  {
307  data.resize(size);
308  for (unsigned int i = 0; i < size; ++i)
309  data[i] = static_cast<float>(data_in[i]);
310 #ifdef MSGPACK_FEATURE_NOT_SUPPORTED
311  array = data;
312  WARNING("MSGPACK version does not support sending this type of data. Ignoring.");
313 #else
314  array = msgpack_ext(0x17, reinterpret_cast<const char*>(data.data()), sizeof(float) * data.size());
315 #endif
316  }
317 
318  ArrayFloat(const ArrayFloat& other)
319  {
320  itemSize = other.itemSize;
321  normalized = other.normalized;
322  data = other.data;
323 #ifdef MSGPACK_FEATURE_NOT_SUPPORTED
324  array = data;
325 #else
326  array = msgpack_ext(0x17, reinterpret_cast<const char*>(data.data()), sizeof(float) * data.size());
327 #endif
328  }
329 
330  int itemSize = 3;
331  std::string type = "Float32Array";
332  bool normalized = false;
333  std::vector<float> data;
335 
336  MSGPACK_DEFINE(itemSize, type, normalized, array)
337 };
338 
339 struct ArrayInt
340 {
341  ArrayInt() = default;
342 
343  ArrayInt(unsigned int* data, unsigned int size)
344  {
345  array.resize(size);
346  for (unsigned int i = 0; i < size; ++i)
347  array[i] = data[i];
348  }
349 
350  int itemSize = 3;
351  std::string type = "Uint32Array";
352  bool normalized = false;
353  std::vector<uint32_t> array;
354 
355  MSGPACK_DEFINE(itemSize, type, normalized, array)
356 };
357 
359 {
360  GeometryMeshBufferData() = default;
362  {
363  std::shared_ptr<shapes::Mesh> shape = std::static_pointer_cast<shapes::Mesh>(shape_in);
364  attributes.insert(std::make_pair<std::string, ArrayFloat>("position", ArrayFloat(shape->vertices, shape->vertex_count * 3)));
365  if (shape->vertex_normals)
366  attributes.insert(std::make_pair<std::string, ArrayFloat>("normal", ArrayFloat(shape->vertex_normals, shape->vertex_count * 3)));
367  index = ArrayInt(shape->triangles, shape->triangle_count * 3);
368  }
369 
370  std::map<std::string, ArrayFloat> attributes;
372 
373  MSGPACK_DEFINE(attributes, index)
374 };
375 
377 {
378  GeometryMeshBuffer() : Geometry("BufferGeometry", ""){};
379 
380  GeometryMeshBuffer(shapes::ShapePtr shape_in, const std::string& uuid_in = "") : Geometry("BufferGeometry", uuid_in)
381  {
382  data = GeometryMeshBufferData(shape_in);
383  };
384 
386  std::vector<double> matrix = {1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0};
387  MSGPACK_DEFINE(uuid, type, data, matrix)
388 };
389 
390 template <typename T>
391 Object<T> CreateGeometryObject(const T& geometry_in, const Material& material_in = Material(), const std::string& uuid_in = "")
392 {
393  Object<T> ret;
394  ret.geometries.push_back(geometry_in);
395  ret.materials.push_back(material_in);
396  ret.object.type = "Mesh";
397  ret.object.uuid = uuid_in == "" ? generate_uuid() : uuid_in;
398  ret.object.geometry = geometry_in.uuid;
399  ret.object.material = material_in.uuid;
400  return ret;
401 };
402 
403 template <typename T>
404 MeshObject<T> CreateMeshObject(const T& geometry_in, const Material& material_in = Material(), const std::string& uuid_in = "")
405 {
406  MeshObject<T> ret;
407  ret.object = geometry_in;
408  ret.object.type = "_meshfile_object";
409  return ret;
410 };
411 
413 {
414  SetTransform() = default;
415  SetTransform(const std::string& path_in, const std::vector<double>& matrix_in) : type("set_transform"), path(path_in), matrix(matrix_in){};
416  std::string type;
417  std::string path;
418  std::vector<double> matrix;
419  MSGPACK_DEFINE(type, path, matrix);
420 };
421 
422 struct Key
423 {
424  Key() = default;
425  Key(double time_in, const std::vector<double>& value_in) : time(time_in), value(value_in){};
426  double time;
427  std::vector<double> value;
428  MSGPACK_DEFINE(time, value);
429 };
430 
431 struct Track
432 {
433  Track() = default;
434  Track(const std::string& name_in, const std::string& type_in) : name(name_in), type(type_in){};
435  std::string name;
436  std::string type;
437  std::vector<Key> keys;
438  MSGPACK_DEFINE(name, type, keys);
439 };
440 
441 struct Clip
442 {
443  Clip() = default;
444  Clip(double fps_in, const std::string& name_in) : fps(fps_in), name(name_in){};
445  double fps;
446  std::string name;
447  std::vector<Track> tracks;
448  MSGPACK_DEFINE(fps, name, tracks);
449 };
450 
451 struct Animation
452 {
453  Animation() = default;
454  Animation(const std::string& path_in) : path(path_in){};
455  std::string path;
457  MSGPACK_DEFINE(path, clip);
458 };
459 
461 {
462  AnimationOption() = default;
463  bool play = true;
464  int repetitions = 1;
465  MSGPACK_DEFINE(play, repetitions);
466 };
467 
469 {
470  SetAnimation() = default;
471  std::string type = "set_animation";
472  std::string path = "";
473  std::vector<Animation> animations;
475  MSGPACK_DEFINE(type, path, animations, options);
476 };
477 
478 struct Delete
479 {
480  Delete() = default;
481  Delete(const std::string& path_in) : path(path_in){};
482  std::string type = "delete";
483  std::string path = "";
484  MSGPACK_DEFINE(type, path);
485 };
486 } // namespace visualization
487 } // namespace exotica
488 #endif // EXOTICA_CORE_VISUALIZATION_MESHCAT_TYPES_H_
const char version[]
std::vector< float > msgpack_ext
Track(const std::string &name_in, const std::string &type_in)
Geometry(const std::string &type_in, const std::string &uuid_in="")
ArrayFloat(double *data_in, unsigned int size)
Material(long color_in, double opacity_in=1.0, const std::string &type_in="MeshPhongMaterial", const std::string &uuid_in="")
long RGB(double R, double G, double B)
msgpack::type::raw_ref msgpack_raw_ref
std::shared_ptr< Shape > ShapePtr
std::string generate_hex(const unsigned int len)
TFSIMD_FORCE_INLINE Vector3 normalized() const
ArrayInt(unsigned int *data, unsigned int size)
unsigned int index
ObjectData(const std::string &type_in, const std::string &uuid_in, const std::string &geometry_in, const std::string &material_in)
GeometryMesh & operator=(const GeometryMesh &other)
SetObjectType< T > SetObject(const std::string &path_in, const T &object_in)
GeometryMesh(const std::string &file_name_in, const std::string &url_in="", const std::string &format_in="", const std::string &uuid_in="")
std::string ParsePath(const std::string &path)
Definition: tools.cpp:145
Property(const std::string &path_in, const std::string &property_in, const T &value_in)
Object< T > CreateGeometryObject(const T &geometry_in, const Material &material_in=Material(), const std::string &uuid_in="")
std::map< std::string, std::string > resources
MeshObject< T > CreateMeshObject(const T &geometry_in, const Material &material_in=Material(), const std::string &uuid_in="")
GeometryMeshBuffer(shapes::ShapePtr shape_in, const std::string &uuid_in="")
#define WARNING(x)
With endline.
Definition: printable.h:56


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sat Apr 10 2021 02:34:49