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