ElementOps.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 
5 #include <Eigen/Core>
6 #include <Eigen/Geometry>
7 
8 #include <SimoxUtility/color/GlasbeyLUT.h>
9 #include <SimoxUtility/math/convert/rpy_to_quat.h>
10 
11 #include <RobotAPI/interface/ArViz/Elements.h>
12 
13 #include "Color.h"
14 
15 namespace armarx::viz
16 {
17  using data::ColoredPoint;
18 
19  struct AxesFlags
20  {
21  bool x = false;
22  bool y = false;
23  bool z = false;
24  bool local = false;
25  };
26 
27  static const AxesFlags AXES_X = {true, false, false, false};
28  static const AxesFlags AXES_Y = {false, true, false, false};
29  static const AxesFlags AXES_Z = {false, false, true, false};
30  static const AxesFlags AXES_XY = {true, true, false, false};
31  static const AxesFlags AXES_YZ = {false, true, true, false};
32  static const AxesFlags AXES_XZ = {true, false, true, false};
33  static const AxesFlags AXES_XYZ = {true, true, true, false};
34 
36  {
38 
39  Self&
40  none()
41  {
42  data_.enableFlags = 0;
43  return *this;
44  }
45 
46  Self&
48  {
49  data_.enableFlags |= data::InteractionEnableFlags::SELECT;
50  return *this;
51  }
52 
53  Self&
54  contextMenu(std::vector<std::string> const& options)
55  {
56  data_.enableFlags |= data::InteractionEnableFlags::CONTEXT_MENU;
57  data_.contextMenuOptions = options;
58  // Context menu (right click) implies selection
59  return selection();
60  }
61 
62  Self&
63  translation(AxesFlags const& axes = AXES_XYZ)
64  {
65  data_.enableFlags |= (axes.x ? data::InteractionEnableFlags::TRANSLATION_X : 0);
66  data_.enableFlags |= (axes.y ? data::InteractionEnableFlags::TRANSLATION_Y : 0);
67  data_.enableFlags |= (axes.z ? data::InteractionEnableFlags::TRANSLATION_Z : 0);
68  // Translation implies selection
69  return selection();
70  }
71 
72  Self&
73  rotation(AxesFlags const& axes = AXES_XYZ)
74  {
75  data_.enableFlags |= (axes.x ? data::InteractionEnableFlags::ROTATION_X : 0);
76  data_.enableFlags |= (axes.y ? data::InteractionEnableFlags::ROTATION_Y : 0);
77  data_.enableFlags |= (axes.z ? data::InteractionEnableFlags::ROTATION_Z : 0);
78  // Rotation implies selection
79  return selection();
80  }
81 
82  Self&
83  scaling(AxesFlags const& axes = AXES_XYZ)
84  {
85  data_.enableFlags |= (axes.x ? data::InteractionEnableFlags::SCALING_X : 0);
86  data_.enableFlags |= (axes.y ? data::InteractionEnableFlags::SCALING_Y : 0);
87  data_.enableFlags |= (axes.z ? data::InteractionEnableFlags::SCALING_Z : 0);
88  // Rotation implies selection
89  return selection();
90  }
91 
92  Self&
94  {
95  return translation().rotation();
96  }
97 
98  Self&
100  {
101  data_.enableFlags |= data::InteractionEnableFlags::TRANSFORM_HIDE;
102  return *this;
103  }
104 
105  data::InteractionDescription data_;
106  };
107 
110  {
111  return InteractionDescription();
112  }
113 
114  // Move the ice datatypes into their own namespace
115  template <typename DerivedT, typename ElementT>
117  {
118  public:
119  ElementOps(std::string const& id) : data_(new ElementT)
120  {
121  data_->id = id;
122  data_->scale.e0 = 1.0f;
123  data_->scale.e1 = 1.0f;
124  data_->scale.e2 = 1.0f;
125  }
126 
127  DerivedT&
128  id(const std::string& id)
129  {
130  data_->id = id;
131 
132  return *static_cast<DerivedT*>(this);
133  }
134 
135  DerivedT&
136  position(float x, float y, float z)
137  {
138  auto& pose = data_->pose;
139  pose.x = x;
140  pose.y = y;
141  pose.z = z;
142  return *static_cast<DerivedT*>(this);
143  }
144 
145  DerivedT&
146  position(Eigen::Vector3f const& pos)
147  {
148  return position(pos.x(), pos.y(), pos.z());
149  }
150 
151  DerivedT&
153  {
154  auto& pose = data_->pose;
155  pose.qw = ori.w();
156  pose.qx = ori.x();
157  pose.qy = ori.y();
158  pose.qz = ori.z();
159 
160  return *static_cast<DerivedT*>(this);
161  }
162 
163  DerivedT&
165  {
166  return orientation(Eigen::Quaternionf(ori));
167  }
168 
169  DerivedT&
170  orientation(float r, float p, float y)
171  {
172  return orientation(simox::math::rpy_to_quat(r, p, y));
173  }
174 
175  DerivedT&
177  {
178  return position(pose.block<3, 1>(0, 3)).orientation(pose.block<3, 3>(0, 0));
179  }
180 
181  DerivedT&
182  pose(Eigen::Vector3f const& position, Eigen::Quaternionf const& orientation)
183  {
184  return this->position(position).orientation(orientation);
185  }
186 
187  DerivedT&
188  pose(Eigen::Vector3f const& position, Eigen::Matrix3f const& orientation)
189  {
190  return this->position(position).orientation(orientation);
191  }
192 
193  DerivedT&
194  pose(const Eigen::Affine3f& pose)
195  {
196  return this->position(pose.translation()).orientation(pose.linear());
197  }
198 
200  pose() const
201  {
202  auto& p = data_->pose;
204  m(0, 3) = p.x;
205  m(1, 3) = p.y;
206  m(2, 3) = p.z;
207  m.topLeftCorner<3, 3>() = Eigen::Quaternionf{p.qw, p.qx, p.qy, p.qz}.toRotationMatrix();
208  return m;
209  }
210 
211  DerivedT&
213  {
214  return pose(p * pose());
215  }
216 
217  DerivedT&
219  {
220  data_->color = color;
221 
222  return *static_cast<DerivedT*>(this);
223  }
224 
225  template <class... Ts>
226  DerivedT&
227  color(Ts&&... ts)
228  {
229  return color({std::forward<Ts>(ts)...});
230  }
231 
232  DerivedT&
233  colorGlasbeyLUT(std::size_t id, int alpha = 255)
234  {
235  return color(Color::fromRGBA(simox::color::GlasbeyLUT::at(id, alpha)));
236  }
237 
238  DerivedT&
240  {
241  if (value)
242  {
243  data_->flags |= data::ElementFlags::OVERRIDE_MATERIAL;
244  }
245  else
246  {
247  data_->flags &= ~data::ElementFlags::OVERRIDE_MATERIAL;
248  }
249 
250  return *static_cast<DerivedT*>(this);
251  }
252 
253  DerivedT&
254  scale(Eigen::Vector3f scale)
255  {
256  data_->scale.e0 = scale.x();
257  data_->scale.e1 = scale.y();
258  data_->scale.e2 = scale.z();
259 
260  return *static_cast<DerivedT*>(this);
261  }
262 
263  DerivedT&
264  scale(float x, float y, float z)
265  {
266  data_->scale.e0 = x;
267  data_->scale.e1 = y;
268  data_->scale.e2 = z;
269 
270  return *static_cast<DerivedT*>(this);
271  }
272 
273  DerivedT&
274  scale(float s)
275  {
276  return scale(s, s, s);
277  }
278 
279  DerivedT&
281  {
282  data_->flags |= data::ElementFlags::HIDDEN;
283 
284  return *static_cast<DerivedT*>(this);
285  }
286 
287  DerivedT&
289  {
290  data_->flags &= ~data::ElementFlags::HIDDEN;
291 
292  return *static_cast<DerivedT*>(this);
293  }
294 
295  DerivedT&
297  {
298  if (visible)
299  {
300  return show();
301  }
302  else
303  {
304  return hide();
305  }
306  }
307 
308  DerivedT&
309  enable(InteractionDescription const& interactionDescription)
310  {
311  data_->interaction = interactionDescription.data_;
312  return *static_cast<DerivedT*>(this);
313  }
314 
316  };
317 
318 } // namespace armarx::viz
armarx::viz::InteractionDescription::data_
data::InteractionDescription data_
Definition: ElementOps.h:105
armarx::viz::ElementOps::pose
DerivedT & pose(Eigen::Vector3f const &position, Eigen::Matrix3f const &orientation)
Definition: ElementOps.h:188
armarx::viz::interaction
InteractionDescription interaction()
Definition: ElementOps.h:109
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::viz::ElementOps::color
DerivedT & color(Ts &&... ts)
Definition: ElementOps.h:227
armarx::viz::ElementOps::id
DerivedT & id(const std::string &id)
Definition: ElementOps.h:128
armarx::viz::InteractionDescription::translation
Self & translation(AxesFlags const &axes=AXES_XYZ)
Definition: ElementOps.h:63
armarx::viz::ElementOps::pose
DerivedT & pose(Eigen::Vector3f const &position, Eigen::Quaternionf const &orientation)
Definition: ElementOps.h:182
armarx::viz::Color::fromRGBA
static Color fromRGBA(int r, int g, int b, int a=255)
Construct a byte color from R, G, B and optional alpha.
Definition: Color.h:46
armarx::viz::InteractionDescription::rotation
Self & rotation(AxesFlags const &axes=AXES_XYZ)
Definition: ElementOps.h:73
armarx::viz::InteractionDescription::Self
InteractionDescription Self
Definition: ElementOps.h:37
armarx::viz::ElementOps::orientation
DerivedT & orientation(Eigen::Matrix3f const &ori)
Definition: ElementOps.h:164
armarx::viz::ElementOps::position
DerivedT & position(Eigen::Vector3f const &pos)
Definition: ElementOps.h:146
armarx::viz::InteractionDescription::scaling
Self & scaling(AxesFlags const &axes=AXES_XYZ)
Definition: ElementOps.h:83
armarx::viz::ElementOps::data_
IceInternal::Handle< ElementT > data_
Definition: ElementOps.h:315
armarx::viz::ElementOps::colorGlasbeyLUT
DerivedT & colorGlasbeyLUT(std::size_t id, int alpha=255)
Definition: ElementOps.h:233
armarx::viz::ElementOps::ElementOps
ElementOps(std::string const &id)
Definition: ElementOps.h:119
armarx::viz::ElementOps::position
DerivedT & position(float x, float y, float z)
Definition: ElementOps.h:136
IceInternal::Handle< ElementT >
armarx::viz::ElementOps::orientation
DerivedT & orientation(float r, float p, float y)
Definition: ElementOps.h:170
armarx::viz::ElementOps
Definition: ElementOps.h:116
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::viz::ElementOps::pose
Eigen::Matrix4f pose() const
Definition: ElementOps.h:200
Color.h
armarx::viz::InteractionDescription
Definition: ElementOps.h:35
armarx::viz::InteractionDescription::contextMenu
Self & contextMenu(std::vector< std::string > const &options)
Definition: ElementOps.h:54
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::viz::ElementOps::orientation
DerivedT & orientation(Eigen::Quaternionf const &ori)
Definition: ElementOps.h:152
armarx::viz::Color
Definition: Color.h:12
armarx::viz::AxesFlags::z
bool z
Definition: ElementOps.h:23
armarx::viz::InteractionDescription::hideDuringTransform
Self & hideDuringTransform()
Definition: ElementOps.h:99
armarx::viz::ElementOps::overrideMaterial
DerivedT & overrideMaterial(bool value)
Definition: ElementOps.h:239
armarx::viz::ElementOps::transformPose
DerivedT & transformPose(Eigen::Matrix4f const &p)
Definition: ElementOps.h:212
armarx::viz::ElementOps::scale
DerivedT & scale(float s)
Definition: ElementOps.h:274
armarx::viz::ElementOps::pose
DerivedT & pose(const Eigen::Affine3f &pose)
Definition: ElementOps.h:194
armarx::viz::InteractionDescription::transform
Self & transform()
Definition: ElementOps.h:93
armarx::viz::AxesFlags
Definition: ElementOps.h:19
armarx::viz::ElementOps::pose
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition: ElementOps.h:176
armarx::viz::ElementOps::scale
DerivedT & scale(Eigen::Vector3f scale)
Definition: ElementOps.h:254
armarx::Quaternion< float, 0 >
armarx::viz::ElementOps::show
DerivedT & show()
Definition: ElementOps.h:288
armarx::viz::ElementOps::color
DerivedT & color(Color color)
Definition: ElementOps.h:218
armarx::viz::ElementOps::scale
DerivedT & scale(float x, float y, float z)
Definition: ElementOps.h:264
armarx::viz::ElementOps::hide
DerivedT & hide()
Definition: ElementOps.h:280
armarx::viz::AxesFlags::y
bool y
Definition: ElementOps.h:22
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:649
armarx::viz::InteractionDescription::selection
Self & selection()
Definition: ElementOps.h:47
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::viz
This file is part of ArmarX.
Definition: ArVizStorage.cpp:418
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::viz::InteractionDescription::none
Self & none()
Definition: ElementOps.h:40
armarx::viz::ElementOps::enable
DerivedT & enable(InteractionDescription const &interactionDescription)
Definition: ElementOps.h:309
armarx::viz::ElementOps::visible
DerivedT & visible(bool visible)
Definition: ElementOps.h:296