mc_rtc  2.14.0
Polyhedron.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2019 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
8 #include <mc_rtc/gui/elements.h>
9 #include <mc_rtc/gui/types.h>
10 
11 namespace mc_rtc::gui
12 {
13 
14 namespace details
15 {
16 
22 template<typename GetTrianglesT>
24 {
25  static constexpr auto type = Elements::PolyhedronTrianglesList;
26 
27  PolyhedronTrianglesListImpl(const std::string & name, const PolyhedronConfig & config, GetTrianglesT get_triangles_fn)
28  : Element(name), config_(config), get_triangles_fn_(get_triangles_fn)
29  {
30  static_assert(details::CheckReturnType<GetTrianglesT, std::vector<std::array<Eigen::Vector3d, 3>>>::value,
31  "Polyhedron callback must return a vector of triangles with points ordered clockwise, expressed as "
32  "an std::vector<std::array<double, 3>>");
33  }
34 
37 
38  static constexpr size_t write_size() { return Element::write_size() + 1 + PolyhedronConfig::write_size(); }
39 
41  {
42  builder.write(get_triangles_fn_());
43  config_.write(builder);
44  }
45 
46 protected:
48  GetTrianglesT get_triangles_fn_;
49 };
50 
57 template<typename GetVerticesT, typename GetTrianglesT>
59 {
61 
63  const PolyhedronConfig & config,
64  GetVerticesT get_vertices_fn,
65  GetTrianglesT get_triangles_fn)
66  : Element(name), config_(config), get_vertices_fn_(get_vertices_fn), get_triangles_fn_(get_triangles_fn)
67  {
68  static_assert(details::CheckReturnType<GetVerticesT, std::vector<Eigen::Vector3d>>::value,
69  "Polyhedron vertices callback must return a vector of 3D points");
70  static_assert(details::CheckReturnType<GetTrianglesT, std::vector<std::array<size_t, 3>>>::value,
71  "Polyhedron triangles callback must return a list of 3-uple index into vertices array");
72  }
73 
76 
77  static constexpr size_t write_size() { return Element::write_size() + 2 + PolyhedronConfig::write_size(); }
78 
80  {
81  builder.write(get_vertices_fn_());
82  builder.write(get_triangles_fn_());
83  config_.write(builder);
84  }
85 
86 protected:
88  GetVerticesT get_vertices_fn_;
89  GetTrianglesT get_triangles_fn_;
90 };
91 
98 template<typename PolyhedronT, typename GetColorT>
99 struct ColoredPolyhedronImpl : public PolyhedronT
100 {
101  ColoredPolyhedronImpl(PolyhedronT && poly, GetColorT get_color_fn) : PolyhedronT(poly), get_color_fn_(get_color_fn)
102  {
103  if constexpr(PolyhedronT::type == Elements::PolyhedronTrianglesList)
104  {
105  static_assert(
106  details::CheckReturnType<GetColorT, std::vector<std::array<mc_rtc::gui::Color, 3>>>::value,
107  "Polyhedron color callback must return an std::vector<std::array<mc_rtc::gui::Color, 3>> (color for "
108  "each of the triangle vertices)");
109  }
110  else
111  {
112  static_assert(details::CheckReturnType<GetColorT, std::vector<mc_rtc::gui::Color>>::value,
113  "Polyhedron color callback must return an std::vector<mc_rtc::gui::Color> (color for "
114  "each of the vertices)");
115  }
116  }
117 
120 
121  static constexpr size_t write_size() { return PolyhedronT::write_size() + 1; }
122 
124  {
125  PolyhedronT::write(builder);
126  const auto & colors = get_color_fn_();
127  builder.start_array(colors.size());
128  for(const auto & c : colors)
129  {
130  if constexpr(PolyhedronT::type == Elements::PolyhedronTrianglesList)
131  {
132  builder.start_array(3);
133  c[0].write(builder);
134  c[1].write(builder);
135  c[2].write(builder);
136  builder.finish_array();
137  }
138  else
139  {
140  c.write(builder);
141  }
142  }
143  builder.finish_array();
144  }
145 
146 protected:
147  GetColorT get_color_fn_;
148 };
149 
150 } // namespace details
151 
153 template<typename GetTrianglesT>
154 auto Polyhedron(const std::string & name, GetTrianglesT get_triangles_fn)
155 {
156  return details::PolyhedronTrianglesListImpl<GetTrianglesT>(name, {}, get_triangles_fn);
157 }
158 
160 template<typename GetTrianglesT>
161 auto Polyhedron(const std::string & name, const PolyhedronConfig & config, GetTrianglesT get_triangles_fn)
162 {
163  return details::PolyhedronTrianglesListImpl<GetTrianglesT>(name, config, get_triangles_fn);
164 }
165 
167 template<typename GetVerticesOrTrianglesT, typename GetTrianglesOrColorsT>
168 auto Polyhedron(const std::string & name,
169  GetVerticesOrTrianglesT get_vertices_or_triangles_fn,
170  GetTrianglesOrColorsT get_triangles_or_colors_fn)
171 {
172  if constexpr(details::CheckReturnType<GetVerticesOrTrianglesT, std::vector<std::array<Eigen::Vector3d, 3>>>::value)
173  {
174  auto poly = Polyhedron(name, PolyhedronConfig{}, get_vertices_or_triangles_fn);
176  get_triangles_or_colors_fn);
177  }
178  else
179  {
181  name, {}, get_vertices_or_triangles_fn, get_triangles_or_colors_fn);
182  }
183 }
184 
186 template<typename GetVerticesOrTrianglesT, typename GetTrianglesOrColorsT>
187 auto Polyhedron(const std::string & name,
188  const PolyhedronConfig & config,
189  GetVerticesOrTrianglesT get_vertices_or_triangles_fn,
190  GetTrianglesOrColorsT get_triangles_or_colors_fn)
191 {
192  if constexpr(details::CheckReturnType<GetVerticesOrTrianglesT, std::vector<std::array<Eigen::Vector3d, 3>>>::value)
193  {
194  auto poly = Polyhedron(name, config, get_vertices_or_triangles_fn);
196  get_triangles_or_colors_fn);
197  }
198  else
199  {
201  name, config, get_vertices_or_triangles_fn, get_triangles_or_colors_fn);
202  }
203 }
204 
206 template<typename GetTrianglesT, typename GetColorT>
207 auto ColoredPolyhedron(const std::string & name,
208  const PolyhedronConfig & config,
209  GetTrianglesT get_triangles_fn,
210  GetColorT get_color_fn)
211 {
212  auto poly = Polyhedron(name, config, get_triangles_fn);
213  return details::ColoredPolyhedronImpl<decltype(poly), GetColorT>(std::move(poly), get_color_fn);
214 }
215 
217 template<typename GetVerticesT, typename GetTrianglesT, typename GetColorT>
218 auto Polyhedron(const std::string & name,
219  GetVerticesT get_vertices_fn,
220  GetTrianglesT get_triangles_fn,
221  GetColorT get_color_fn)
222 {
223  auto poly = Polyhedron(name, get_vertices_fn, get_triangles_fn);
224  return details::ColoredPolyhedronImpl<decltype(poly), GetColorT>(std::move(poly), get_color_fn);
225 }
226 
228 template<typename GetVerticesT, typename GetTrianglesT, typename GetColorT>
229 auto Polyhedron(const std::string & name,
230  const PolyhedronConfig & config,
231  GetVerticesT get_vertices_fn,
232  GetTrianglesT get_triangles_fn,
233  GetColorT get_color_fn)
234 {
235  auto poly = Polyhedron(name, config, get_vertices_fn, get_triangles_fn);
236  return details::ColoredPolyhedronImpl<decltype(poly), GetColorT>(std::move(poly), get_color_fn);
237 }
238 
239 } // namespace mc_rtc::gui
auto write(T &value)
Definition: traits.h:230
Definition: Observer.h:16
auto Polyhedron(const std::string &name, GetTrianglesT get_triangles_fn)
Definition: Polyhedron.h:154
auto ColoredPolyhedron(const std::string &name, const PolyhedronConfig &config, GetTrianglesT get_triangles_fn, GetColorT get_color_fn)
Definition: Polyhedron.h:207
Definition: MessagePackBuilder.h:87
void start_array(size_t size)
Definition: elements.h:59
const std::string & name() const
Definition: elements.h:61
static constexpr size_t write_size()
Definition: elements.h:76
Definition: types.h:400
static constexpr size_t write_size()
Definition: types.h:446
void write(mc_rtc::MessagePackBuilder &out) const
Default triangle face color.
Definition: types.h:448
ColoredPolyhedronImpl()
Definition: Polyhedron.h:119
GetColorT get_color_fn_
Definition: Polyhedron.h:147
ColoredPolyhedronImpl(PolyhedronT &&poly, GetColorT get_color_fn)
Definition: Polyhedron.h:101
void write(mc_rtc::MessagePackBuilder &builder)
Definition: Polyhedron.h:123
static constexpr size_t write_size()
Definition: Polyhedron.h:121
PolyhedronTrianglesListImpl(const std::string &name, const PolyhedronConfig &config, GetTrianglesT get_triangles_fn)
Definition: Polyhedron.h:27
void write(mc_rtc::MessagePackBuilder &builder)
Definition: Polyhedron.h:40
GetTrianglesT get_triangles_fn_
Definition: Polyhedron.h:48
static constexpr size_t write_size()
Definition: Polyhedron.h:38
PolyhedronConfig config_
Definition: Polyhedron.h:47
PolyhedronTrianglesListImpl()
Definition: Polyhedron.h:36
static constexpr auto type
Definition: Polyhedron.h:25
GetTrianglesT get_triangles_fn_
Definition: Polyhedron.h:89
GetVerticesT get_vertices_fn_
Definition: Polyhedron.h:88
PolyhedronVerticesTrianglesImpl()
Definition: Polyhedron.h:75
PolyhedronVerticesTrianglesImpl(const std::string &name, const PolyhedronConfig &config, GetVerticesT get_vertices_fn, GetTrianglesT get_triangles_fn)
Definition: Polyhedron.h:62
static constexpr size_t write_size()
Definition: Polyhedron.h:77
void write(mc_rtc::MessagePackBuilder &builder)
Definition: Polyhedron.h:79
static constexpr auto type
Definition: Polyhedron.h:60
PolyhedronConfig config_
Definition: Polyhedron.h:87