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 { c.write(builder); }
139  }
140  builder.finish_array();
141  }
142 
143 protected:
144  GetColorT get_color_fn_;
145 };
146 
147 } // namespace details
148 
150 template<typename GetTrianglesT>
151 auto Polyhedron(const std::string & name, GetTrianglesT get_triangles_fn)
152 {
153  return details::PolyhedronTrianglesListImpl<GetTrianglesT>(name, {}, get_triangles_fn);
154 }
155 
157 template<typename GetTrianglesT>
158 auto Polyhedron(const std::string & name, const PolyhedronConfig & config, GetTrianglesT get_triangles_fn)
159 {
160  return details::PolyhedronTrianglesListImpl<GetTrianglesT>(name, config, get_triangles_fn);
161 }
162 
164 template<typename GetVerticesOrTrianglesT, typename GetTrianglesOrColorsT>
165 auto Polyhedron(const std::string & name,
166  GetVerticesOrTrianglesT get_vertices_or_triangles_fn,
167  GetTrianglesOrColorsT get_triangles_or_colors_fn)
168 {
169  if constexpr(details::CheckReturnType<GetVerticesOrTrianglesT, std::vector<std::array<Eigen::Vector3d, 3>>>::value)
170  {
171  auto poly = Polyhedron(name, PolyhedronConfig{}, get_vertices_or_triangles_fn);
173  get_triangles_or_colors_fn);
174  }
175  else
176  {
178  name, {}, get_vertices_or_triangles_fn, get_triangles_or_colors_fn);
179  }
180 }
181 
183 template<typename GetVerticesOrTrianglesT, typename GetTrianglesOrColorsT>
184 auto Polyhedron(const std::string & name,
185  const PolyhedronConfig & config,
186  GetVerticesOrTrianglesT get_vertices_or_triangles_fn,
187  GetTrianglesOrColorsT get_triangles_or_colors_fn)
188 {
189  if constexpr(details::CheckReturnType<GetVerticesOrTrianglesT, std::vector<std::array<Eigen::Vector3d, 3>>>::value)
190  {
191  auto poly = Polyhedron(name, config, get_vertices_or_triangles_fn);
193  get_triangles_or_colors_fn);
194  }
195  else
196  {
198  name, config, get_vertices_or_triangles_fn, get_triangles_or_colors_fn);
199  }
200 }
201 
203 template<typename GetTrianglesT, typename GetColorT>
204 auto ColoredPolyhedron(const std::string & name,
205  const PolyhedronConfig & config,
206  GetTrianglesT get_triangles_fn,
207  GetColorT get_color_fn)
208 {
209  auto poly = Polyhedron(name, config, get_triangles_fn);
210  return details::ColoredPolyhedronImpl<decltype(poly), GetColorT>(std::move(poly), get_color_fn);
211 }
212 
214 template<typename GetVerticesT, typename GetTrianglesT, typename GetColorT>
215 auto Polyhedron(const std::string & name,
216  GetVerticesT get_vertices_fn,
217  GetTrianglesT get_triangles_fn,
218  GetColorT get_color_fn)
219 {
220  auto poly = Polyhedron(name, get_vertices_fn, get_triangles_fn);
221  return details::ColoredPolyhedronImpl<decltype(poly), GetColorT>(std::move(poly), get_color_fn);
222 }
223 
225 template<typename GetVerticesT, typename GetTrianglesT, typename GetColorT>
226 auto Polyhedron(const std::string & name,
227  const PolyhedronConfig & config,
228  GetVerticesT get_vertices_fn,
229  GetTrianglesT get_triangles_fn,
230  GetColorT get_color_fn)
231 {
232  auto poly = Polyhedron(name, config, get_vertices_fn, get_triangles_fn);
233  return details::ColoredPolyhedronImpl<decltype(poly), GetColorT>(std::move(poly), get_color_fn);
234 }
235 
236 } // namespace mc_rtc::gui
mc_rtc::gui::Element
Definition: elements.h:58
mc_rtc::gui::details::PolyhedronTrianglesListImpl::get_triangles_fn_
GetTrianglesT get_triangles_fn_
Definition: Polyhedron.h:48
mc_rtc::MessagePackBuilder
Definition: MessagePackBuilder.h:86
mc_rtc::gui::details::PolyhedronVerticesTrianglesImpl::get_vertices_fn_
GetVerticesT get_vertices_fn_
Definition: Polyhedron.h:88
mc_rtc::gui::details::PolyhedronVerticesTrianglesImpl::write_size
static constexpr size_t write_size()
Definition: Polyhedron.h:77
mc_rtc::gui::PolyhedronConfig
Definition: types.h:398
types.h
mc_rtc::gui::PolyhedronConfig::write_size
static constexpr size_t write_size()
Definition: types.h:445
mc_rtc::gui::details::PolyhedronTrianglesListImpl::type
static constexpr auto type
Definition: Polyhedron.h:25
mc_rtc::gui::details::PolyhedronTrianglesListImpl::write_size
static constexpr size_t write_size()
Definition: Polyhedron.h:38
mc_rtc::MessagePackBuilder::finish_array
void finish_array()
mc_rtc::gui::details::PolyhedronTrianglesListImpl::config_
PolyhedronConfig config_
Definition: Polyhedron.h:47
mc_rtc::gui::details::PolyhedronVerticesTrianglesImpl
Definition: Polyhedron.h:58
mc_rtc::gui::details::PolyhedronVerticesTrianglesImpl::PolyhedronVerticesTrianglesImpl
PolyhedronVerticesTrianglesImpl()
Definition: Polyhedron.h:75
mc_rtc::gui::Elements::PolyhedronTrianglesList
@ PolyhedronTrianglesList
mc_rtc::gui::details::PolyhedronTrianglesListImpl::PolyhedronTrianglesListImpl
PolyhedronTrianglesListImpl()
Definition: Polyhedron.h:36
mc_rtc::gui::details::PolyhedronTrianglesListImpl::write
void write(mc_rtc::MessagePackBuilder &builder)
Definition: Polyhedron.h:40
mc_rtc::gui::details::PolyhedronVerticesTrianglesImpl::PolyhedronVerticesTrianglesImpl
PolyhedronVerticesTrianglesImpl(const std::string &name, const PolyhedronConfig &config, GetVerticesT get_vertices_fn, GetTrianglesT get_triangles_fn)
Definition: Polyhedron.h:62
mc_rtc::gui::Polyhedron
auto Polyhedron(const std::string &name, GetTrianglesT get_triangles_fn)
Definition: Polyhedron.h:151
elements.h
mc_rtc::gui::Elements::PolyhedronVerticesTriangles
@ PolyhedronVerticesTriangles
mc_rtc::gui::details::write
auto write(T &value)
Definition: traits.h:224
mc_rtc::MessagePackBuilder::start_array
void start_array(size_t size)
mc_rtc::gui::details::PolyhedronTrianglesListImpl
Definition: Polyhedron.h:23
mc_rtc::gui::details::PolyhedronVerticesTrianglesImpl::config_
PolyhedronConfig config_
Definition: Polyhedron.h:87
mc_rtc::gui::details::PolyhedronVerticesTrianglesImpl::write
void write(mc_rtc::MessagePackBuilder &builder)
Definition: Polyhedron.h:79
mc_rtc::gui::details::PolyhedronVerticesTrianglesImpl::type
static constexpr auto type
Definition: Polyhedron.h:60
mc_rtc::gui::details::ColoredPolyhedronImpl::ColoredPolyhedronImpl
ColoredPolyhedronImpl(PolyhedronT &&poly, GetColorT get_color_fn)
Definition: Polyhedron.h:101
mc_rtc::MessagePackBuilder::write
void write()
mc_rtc::gui::details::ColoredPolyhedronImpl::get_color_fn_
GetColorT get_color_fn_
Definition: Polyhedron.h:144
mc_rtc::gui::ColoredPolyhedron
auto ColoredPolyhedron(const std::string &name, const PolyhedronConfig &config, GetTrianglesT get_triangles_fn, GetColorT get_color_fn)
Definition: Polyhedron.h:204
mc_rtc::gui::details::ColoredPolyhedronImpl
Definition: Polyhedron.h:99
mc_rtc::gui::Element::write_size
static constexpr size_t write_size()
Definition: elements.h:76
mc_rtc::gui::details::ColoredPolyhedronImpl::write_size
static constexpr size_t write_size()
Definition: Polyhedron.h:121
mc_rtc::gui::details::ColoredPolyhedronImpl::ColoredPolyhedronImpl
ColoredPolyhedronImpl()
Definition: Polyhedron.h:119
mc_rtc::gui::details::PolyhedronVerticesTrianglesImpl::get_triangles_fn_
GetTrianglesT get_triangles_fn_
Definition: Polyhedron.h:89
mc_rtc::gui::details::PolyhedronTrianglesListImpl::PolyhedronTrianglesListImpl
PolyhedronTrianglesListImpl(const std::string &name, const PolyhedronConfig &config, GetTrianglesT get_triangles_fn)
Definition: Polyhedron.h:27
traits.h
mc_rtc::gui::details::ColoredPolyhedronImpl::write
void write(mc_rtc::MessagePackBuilder &builder)
Definition: Polyhedron.h:123
mc_rtc::gui::details::CheckReturnType
Definition: traits.h:86
mc_rtc::gui
Definition: Observer.h:15
mc_rtc::gui::Element::name
const std::string & name() const
Definition: elements.h:61