mc_rtc  2.14.0
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
48  {
49  static_assert(!std::is_same_v<DataT, DataT>);
50  }
51  }();
52  out.geometry.data = data;
53  setVisualColor(out, color);
54  return out;
55 }
56 
58 template<rbd::parsers::Geometry::Type type>
60 {
61  if(visual.geometry.type != type) { mc_rtc::log::error_and_throw("Visual type not matching the expected type"); }
63  auto & data = visual.geometry.data;
64  if constexpr(type == Type::BOX) { return boost::get<rbd::parsers::Geometry::Box>(data); }
65  else if constexpr(type == Type::CYLINDER) { return boost::get<rbd::parsers::Geometry::Cylinder>(data); }
66  else if constexpr(type == Type::SPHERE) { return boost::get<rbd::parsers::Geometry::Sphere>(data); }
67  else if constexpr(type == Type::MESH) { return boost::get<rbd::parsers::Geometry::Mesh>(data); }
68  else if constexpr(type == Type::SUPERELLIPSOID) { return boost::get<rbd::parsers::Geometry::Superellipsoid>(data); }
69  else
70  {
71  static_assert(static_cast<int>(type) != static_cast<int>(type));
72  }
73 }
74 
75 } // namespace details
76 
78 inline rbd::parsers::Visual makeVisualSphere(double radius, const mc_rtc::gui::Color & color)
79 {
81  s.radius = radius;
82  return details::makeVisual(s, color);
83 }
84 
86 inline constexpr auto getVisualSphere = details::getVisualGeometry<rbd::parsers::Geometry::Type::SPHERE>;
87 
89 inline rbd::parsers::Visual makeVisualCylinder(double radius, double length, const mc_rtc::gui::Color & color)
90 {
92  c.length = length;
93  c.radius = radius;
94  return details::makeVisual(c, color);
95 }
96 
98 inline constexpr auto getVisualCylinder = details::getVisualGeometry<rbd::parsers::Geometry::Type::CYLINDER>;
99 
101 inline rbd::parsers::Visual makeVisualBox(const Eigen::Vector3d & dim, const mc_rtc::gui::Color & color)
102 {
104  b.size = dim;
105  return details::makeVisual(b, color);
106 }
107 
109 inline constexpr auto getVisualBox = details::getVisualGeometry<rbd::parsers::Geometry::Type::BOX>;
110 
112 inline rbd::parsers::Visual makeVisualMesh(const std::string & path, Eigen::Vector3d scaleV)
113 {
114  rbd::parsers::Geometry::Mesh m;
115  m.filename = path;
116  m.scaleV = scaleV;
117  return details::makeVisual(m, {});
118 }
119 
121 inline rbd::parsers::Visual makeVisualMesh(const std::string & path, double scale)
122 {
123  return makeVisualMesh(path, Eigen::Vector3d(scale, scale, scale));
124 }
125 
127 inline constexpr auto getVisualMesh = details::getVisualGeometry<rbd::parsers::Geometry::Type::MESH>;
128 
130 inline rbd::parsers::Visual makeVisualSuperellispoid(const Eigen::Vector3d & size,
131  double epsilon1,
132  double epsilon2,
133  const mc_rtc::gui::Color & color)
134 {
135  rbd::parsers::Geometry::Superellipsoid se;
136  se.size = size;
137  se.epsilon1 = epsilon1;
138  se.epsilon2 = epsilon2;
139  return details::makeVisual(se, color);
140 }
141 
143 inline constexpr auto getVisualSuperellipsoid =
144  details::getVisualGeometry<rbd::parsers::Geometry::Type::SUPERELLIPSOID>;
145 
146 } // namespace mc_rtc
rbd::parsers::Visual makeVisual(const DataT &data, const mc_rtc::gui::Color &color)
Definition: visual_utils.h:35
auto & getVisualGeometry(rbd::parsers::Visual &visual)
Definition: visual_utils.h:59
void setVisualColor(rbd::parsers::Visual &visual, const mc_rtc::gui::Color &color)
Definition: visual_utils.h:20
Type
Definition: types.h:31
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
auto Visual(const std::string &name, GetVisual get_visual_fn, GetPos get_pos_fn)
Definition: Visual.h:64
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
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
void error_and_throw(Args &&... args)
Definition: logging.h:47
Definition: Contact.h:88
constexpr auto getVisualCylinder
Definition: visual_utils.h:98
rbd::parsers::Visual makeVisualBox(const Eigen::Vector3d &dim, const mc_rtc::gui::Color &color)
Definition: visual_utils.h:101
rbd::parsers::Visual makeVisualSphere(double radius, const mc_rtc::gui::Color &color)
Definition: visual_utils.h:78
constexpr auto getVisualBox
Definition: visual_utils.h:109
rbd::parsers::Visual makeVisualMesh(const std::string &path, Eigen::Vector3d scaleV)
Definition: visual_utils.h:112
constexpr auto getVisualMesh
Definition: visual_utils.h:127
constexpr auto getVisualSphere
Definition: visual_utils.h:86
rbd::parsers::Visual makeVisualSuperellispoid(const Eigen::Vector3d &size, double epsilon1, double epsilon2, const mc_rtc::gui::Color &color)
Definition: visual_utils.h:130
rbd::parsers::Visual makeVisualCylinder(double radius, double length, const mc_rtc::gui::Color &color)
Definition: visual_utils.h:89
constexpr auto getVisualSuperellipsoid
Definition: visual_utils.h:143
Definition: types.h:20
double b
Definition: types.h:28
double a
Definition: types.h:29
double g
Definition: types.h:27
double r
Definition: types.h:26