visual_utils.h
Go to the documentation of this file.
1 /*
2  * Copyright 2023 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
7 #include <mc_rtc/gui/types.h>
8 
9 #include <RBDyn/parsers/common.h>
10 
13 namespace mc_rtc
14 {
15 
16 namespace details
17 {
18 
20 inline void setVisualColor(rbd::parsers::Visual & visual, const mc_rtc::gui::Color & color)
21 {
22  rbd::parsers::Material mat;
23  rbd::parsers::Material::Color col;
24  col.r = color.r;
25  col.g = color.g;
26  col.b = color.b;
27  col.a = color.a;
28  mat.type = rbd::parsers::Material::Type::COLOR;
29  mat.data = col;
30  visual.material = mat;
31 }
32 
34 template<typename DataT>
35 rbd::parsers::Visual makeVisual(const DataT & data, const mc_rtc::gui::Color & color)
36 {
37  using GeometryT = rbd::parsers::Geometry;
39  out.origin = sva::PTransformd::Identity();
40  out.geometry.type = []()
41  {
42  if constexpr(std::is_same_v<DataT, GeometryT::Box>) { return GeometryT::Type::BOX; }
43  else if constexpr(std::is_same_v<DataT, GeometryT::Cylinder>) { return GeometryT::Type::CYLINDER; }
44  else if constexpr(std::is_same_v<DataT, GeometryT::Sphere>) { return GeometryT::Type::SPHERE; }
45  else if constexpr(std::is_same_v<DataT, GeometryT::Mesh>) { return GeometryT::Type::MESH; }
46  else if constexpr(std::is_same_v<DataT, GeometryT::Superellipsoid>) { return GeometryT::Type::SUPERELLIPSOID; }
47  else { static_assert(!std::is_same_v<DataT, DataT>); }
48  }();
49  out.geometry.data = data;
50  setVisualColor(out, color);
51  return out;
52 }
53 
55 template<rbd::parsers::Geometry::Type type>
57 {
58  if(visual.geometry.type != type) { mc_rtc::log::error_and_throw("Visual type not matching the expected type"); }
60  auto & data = visual.geometry.data;
61  if constexpr(type == Type::BOX) { return boost::get<rbd::parsers::Geometry::Box>(data); }
62  else if constexpr(type == Type::CYLINDER) { return boost::get<rbd::parsers::Geometry::Cylinder>(data); }
63  else if constexpr(type == Type::SPHERE) { return boost::get<rbd::parsers::Geometry::Sphere>(data); }
64  else if constexpr(type == Type::MESH) { return boost::get<rbd::parsers::Geometry::Mesh>(data); }
65  else if constexpr(type == Type::SUPERELLIPSOID) { return boost::get<rbd::parsers::Geometry::Superellipsoid>(data); }
66  else { static_assert(static_cast<int>(type) != static_cast<int>(type)); }
67 }
68 
69 } // namespace details
70 
72 inline rbd::parsers::Visual makeVisualSphere(double radius, const mc_rtc::gui::Color & color)
73 {
75  s.radius = radius;
76  return details::makeVisual(s, color);
77 }
78 
80 inline constexpr auto getVisualSphere = details::getVisualGeometry<rbd::parsers::Geometry::Type::SPHERE>;
81 
83 inline rbd::parsers::Visual makeVisualCylinder(double radius, double length, const mc_rtc::gui::Color & color)
84 {
86  c.length = length;
87  c.radius = radius;
88  return details::makeVisual(c, color);
89 }
90 
92 inline constexpr auto getVisualCylinder = details::getVisualGeometry<rbd::parsers::Geometry::Type::CYLINDER>;
93 
95 inline rbd::parsers::Visual makeVisualBox(const Eigen::Vector3d & dim, const mc_rtc::gui::Color & color)
96 {
98  b.size = dim;
99  return details::makeVisual(b, color);
100 }
101 
103 inline constexpr auto getVisualBox = details::getVisualGeometry<rbd::parsers::Geometry::Type::BOX>;
104 
106 inline rbd::parsers::Visual makeVisualMesh(const std::string & path, Eigen::Vector3d scaleV)
107 {
108  rbd::parsers::Geometry::Mesh m;
109  m.filename = path;
110  m.scaleV = scaleV;
111  return details::makeVisual(m, {});
112 }
113 
115 inline rbd::parsers::Visual makeVisualMesh(const std::string & path, double scale)
116 {
117  return makeVisualMesh(path, Eigen::Vector3d(scale, scale, scale));
118 }
119 
121 inline constexpr auto getVisualMesh = details::getVisualGeometry<rbd::parsers::Geometry::Type::MESH>;
122 
124 inline rbd::parsers::Visual makeVisualSuperellispoid(const Eigen::Vector3d & size,
125  double epsilon1,
126  double epsilon2,
127  const mc_rtc::gui::Color & color)
128 {
129  rbd::parsers::Geometry::Superellipsoid se;
130  se.size = size;
131  se.epsilon1 = epsilon1;
132  se.epsilon2 = epsilon2;
133  return details::makeVisual(se, color);
134 }
135 
137 inline constexpr auto getVisualSuperellipsoid =
138  details::getVisualGeometry<rbd::parsers::Geometry::Type::SUPERELLIPSOID>;
139 
140 } // namespace mc_rtc
types.h
mc_rtc::gui::Sphere
auto Sphere(const std::string &name, GetRadius radius_fn, GetPos get_pos_fn, GetColor color_fn=mc_rtc::gui::Color::Red)
Definition: Sphere.h:20
mc_rtc::details::getVisualGeometry
auto & getVisualGeometry(rbd::parsers::Visual &visual)
Definition: visual_utils.h:56
mc_rtc::getVisualSphere
constexpr auto getVisualSphere
Definition: visual_utils.h:80
mc_rtc::gui::Box
auto Box(const std::string &name, GetSize size_fn, GetPos get_pos_fn, GetColor color_fn=mc_rtc::gui::Color::Red)
Definition: Box.h:20
mc_rtc::makeVisualSphere
rbd::parsers::Visual makeVisualSphere(double radius, const mc_rtc::gui::Color &color)
Definition: visual_utils.h:72
mc_rtc::details::makeVisual
rbd::parsers::Visual makeVisual(const DataT &data, const mc_rtc::gui::Color &color)
Definition: visual_utils.h:35
mc_rtc::gui::Color::r
double r
Definition: types.h:26
mc_rtc::details::setVisualColor
void setVisualColor(rbd::parsers::Visual &visual, const mc_rtc::gui::Color &color)
Definition: visual_utils.h:20
mc_rtc::getVisualMesh
constexpr auto getVisualMesh
Definition: visual_utils.h:121
mc_rtc::gui::Cylinder
auto Cylinder(const std::string &name, GetParams params_fn, GetPos get_pos_fn, GetColor color_fn=mc_rtc::gui::Color::Red)
Definition: Cylinder.h:27
mc_rtc::log::error_and_throw
void error_and_throw(Args &&... args)
Definition: logging.h:47
mc_rtc::gui::Visual
auto Visual(const std::string &name, GetVisual get_visual_fn, GetPos get_pos_fn)
Definition: Visual.h:64
mc_rtc::gui::Color
Definition: types.h:19
mc_rtc::getVisualSuperellipsoid
constexpr auto getVisualSuperellipsoid
Definition: visual_utils.h:137
mc_rtc::getVisualBox
constexpr auto getVisualBox
Definition: visual_utils.h:103
mc_rtc::makeVisualBox
rbd::parsers::Visual makeVisualBox(const Eigen::Vector3d &dim, const mc_rtc::gui::Color &color)
Definition: visual_utils.h:95
mc_rtc::getVisualCylinder
constexpr auto getVisualCylinder
Definition: visual_utils.h:92
mc_rtc::makeVisualCylinder
rbd::parsers::Visual makeVisualCylinder(double radius, double length, const mc_rtc::gui::Color &color)
Definition: visual_utils.h:83
mc_rtc::gui::Color::g
double g
Definition: types.h:27
mc_rtc::gui::plot::Type
Type
Definition: types.h:30
mc_rtc::gui::Color::b
double b
Definition: types.h:28
mc_rtc::makeVisualMesh
rbd::parsers::Visual makeVisualMesh(const std::string &path, Eigen::Vector3d scaleV)
Definition: visual_utils.h:106
mc_rtc::gui::Color::a
double a
Definition: types.h:29
mc_rtc
Definition: Contact.h:87
mc_rtc::makeVisualSuperellispoid
rbd::parsers::Visual makeVisualSuperellispoid(const Eigen::Vector3d &size, double epsilon1, double epsilon2, const mc_rtc::gui::Color &color)
Definition: visual_utils.h:124