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
15namespace 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&
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&
164 orientation(Eigen::Matrix3f const& ori)
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&
176 pose(Eigen::Matrix4f const& pose)
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
199 Eigen::Matrix4f
200 pose() const
201 {
202 auto& p = data_->pose;
203 Eigen::Matrix4f m = Eigen::Matrix4f::Identity();
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&
212 transformPose(Eigen::Matrix4f const& p)
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
DerivedT & pose(Eigen::Vector3f const &position, Eigen::Matrix3f const &orientation)
Definition ElementOps.h:188
DerivedT & orientation(float r, float p, float y)
Definition ElementOps.h:170
DerivedT & enable(InteractionDescription const &interactionDescription)
Definition ElementOps.h:309
DerivedT & colorGlasbeyLUT(std::size_t id, int alpha=255)
Definition ElementOps.h:233
DerivedT & visible(bool visible)
Definition ElementOps.h:296
DerivedT & overrideMaterial(bool value)
Definition ElementOps.h:239
DerivedT & scale(float s)
Definition ElementOps.h:274
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition ElementOps.h:176
DerivedT & id(const std::string &id)
Definition ElementOps.h:128
DerivedT & scale(float x, float y, float z)
Definition ElementOps.h:264
IceInternal::Handle< ElementT > data_
Definition ElementOps.h:315
DerivedT & pose(const Eigen::Affine3f &pose)
Definition ElementOps.h:194
ElementOps(std::string const &id)
Definition ElementOps.h:119
DerivedT & orientation(Eigen::Matrix3f const &ori)
Definition ElementOps.h:164
Eigen::Matrix4f pose() const
Definition ElementOps.h:200
DerivedT & color(Color color)
Definition ElementOps.h:218
DerivedT & transformPose(Eigen::Matrix4f const &p)
Definition ElementOps.h:212
DerivedT & position(float x, float y, float z)
Definition ElementOps.h:136
DerivedT & pose(Eigen::Vector3f const &position, Eigen::Quaternionf const &orientation)
Definition ElementOps.h:182
DerivedT & orientation(Eigen::Quaternionf const &ori)
Definition ElementOps.h:152
DerivedT & scale(Eigen::Vector3f scale)
Definition ElementOps.h:254
DerivedT & color(Ts &&... ts)
Definition ElementOps.h:227
DerivedT & position(Eigen::Vector3f const &pos)
Definition ElementOps.h:146
Quaternion< float, 0 > Quaternionf
This file is part of ArmarX.
InteractionDescription interaction()
Definition ElementOps.h:109
This file offers overloads of toIce() and fromIce() functions for STL container types.
Self & rotation(AxesFlags const &axes=AXES_XYZ)
Definition ElementOps.h:73
Self & contextMenu(std::vector< std::string > const &options)
Definition ElementOps.h:54
InteractionDescription Self
Definition ElementOps.h:37
data::InteractionDescription data_
Definition ElementOps.h:105
Self & scaling(AxesFlags const &axes=AXES_XYZ)
Definition ElementOps.h:83
Self & translation(AxesFlags const &axes=AXES_XYZ)
Definition ElementOps.h:63