Interaction.h
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Core>
4
6#include <RobotAPI/interface/ArViz/Component.h>
7
8namespace armarx::viz
9{
11 {
12 /** Nothing happened. */
14
15 /** An element was selected. */
17 /** An element was deselected. */
19
20 /** A context menu entry was chosen. */
22
23 /** The element was transformed (translated or rotated). */
25 };
26
27 inline const char*
29 {
30 switch (type)
31 {
33 return "None";
35 return "Select";
37 return "Deselect";
39 return "ContextMenuChosen";
41 return "Transform";
42 default:
43 return "<Unknown>";
44 }
45 }
46
47 inline Eigen::Matrix4f
48 toEigen(data::GlobalPose const& pose)
49 {
50 Eigen::Quaternionf ori(pose.qw, pose.qx, pose.qy, pose.qz);
51
52 Eigen::Matrix4f result;
53 result.block<3, 3>(0, 0) = ori.toRotationMatrix();
54 result.block<3, 1>(0, 3) = Eigen::Vector3f(pose.x, pose.y, pose.z);
55 result.block<1, 4>(3, 0) = Eigen::Vector4f(0.0f, 0.0f, 0.0f, 1.0f);
56 return result;
57 }
58
60 {
62 type() const
63 {
64 // Maks out all the flags in the higher bits
65 int type = data_.type & 0x7;
66
67 switch (type)
68 {
69 case data::InteractionFeedbackType::NONE:
71
72 case data::InteractionFeedbackType::SELECT:
74
75 case data::InteractionFeedbackType::DESELECT:
77
78 case data::InteractionFeedbackType::CONTEXT_MENU_CHOSEN:
80
81 case data::InteractionFeedbackType::TRANSFORM:
83
84 default:
85 throw std::runtime_error("Unexpected InteractionFeedbackType");
86 }
87 }
88
89 bool
91 {
92 return data_.type & data::InteractionFeedbackType::TRANSFORM_BEGIN_FLAG;
93 }
94
95 bool
97 {
98 return data_.type & data::InteractionFeedbackType::TRANSFORM_DURING_FLAG;
99 }
100
101 bool
103 {
104 return data_.type & data::InteractionFeedbackType::TRANSFORM_END_FLAG;
105 }
106
107 std::string const&
108 layer() const
109 {
110 return data_.layer;
111 }
112
113 std::string const&
114 element() const
115 {
116 return data_.element;
117 }
118
119 long
120 revision() const
121 {
122 return data_.revision;
123 }
124
125 int
127 {
128 return data_.chosenContextMenuEntry;
129 }
130
131 Eigen::Matrix4f
133 {
134 return toEigen(data_.transformation);
135 }
136
137 Eigen::Vector3f
138 scale() const
139 {
140 Eigen::Vector3f result(data_.scale.e0, data_.scale.e1, data_.scale.e2);
141 return result;
142 }
143
144 data::InteractionFeedback data_;
145 };
146
148 {
150 begin() const
151 {
152 return begin_;
153 }
154
156 end() const
157 {
158 return end_;
159 }
160
161 std::size_t
162 size() const
163 {
164 return end_ - begin_;
165 }
166
167 bool
168 empty() const
169 {
170 return begin_ == end_;
171 }
172
175 };
176
178 {
179 // Indicates that a transfomation was applied during the interaction
180 bool wasTransformed = false;
181
182 // Indicates that a layer update is needed
183 // If this flag is set, you should commit the containing layer with the next arviz.commit()
184 bool needsLayerUpdate = false;
185 };
186
187 template <typename ElementT>
189 {
190 Transformable(std::string const& name) : element(name)
191 {
192 }
193
195 pose(Eigen::Matrix4f const& pose)
196 {
197 element.pose(pose);
199 return *this;
200 }
201
203 position(Eigen::Vector3f const& position)
204 {
205 element.position(position);
206 initialPose.block<3, 1>(0, 3) = position;
207 return *this;
208 }
209
211 orientation(Eigen::Matrix3f const& rotationMatrix)
212 {
213 element.orientation(rotationMatrix);
214 initialPose.block<3, 3>(0, 0) = rotationMatrix;
215 return *this;
216 }
217
220 {
221 element.orientation(quaternion);
222 initialPose.block<3, 3>(0, 0) = quaternion.toRotationMatrix();
223 return *this;
224 }
225
228 {
229 element.enable(interaction);
230 // A movable element is always hidden during the interaction
231 element.data_->interaction.enableFlags |=
232 viz::data::InteractionEnableFlags::TRANSFORM_HIDE;
233 return *this;
234 }
235
236 // The pose after the current transformation has been applied
237 Eigen::Matrix4f
239 {
241 }
242
243 // Returns true, if the element has been changed and the layer needs to be comitted again
246 {
248 if (interaction.element() == element.data_->id)
249 {
250 switch (interaction.type())
251 {
253 {
254 // Keep track of the transformation
255 transformation = interaction.transformation();
256 result.wasTransformed = true;
257 }
258 break;
260 {
261 // If an object is deselected, we apply the transformation
263 transformation = Eigen::Matrix4f::Identity();
264 element.pose(initialPose);
265
266 // In this case, the user needs to commit the layer
267 result.needsLayerUpdate = true;
268 }
269 break;
270 default:
271 {
272 // Ignore the other events
273 }
274 }
275 }
276 return result;
277 }
278
279 Eigen::Matrix4f initialPose = Eigen::Matrix4f::Identity();
280 Eigen::Matrix4f transformation = Eigen::Matrix4f::Identity();
281
282 ElementT element;
283 };
284
285} // namespace armarx::viz
Quaternion< float, 0 > Quaternionf
This file is part of ArmarX.
InteractionDescription interaction()
Definition ElementOps.h:109
@ Transform
The element was transformed (translated or rotated).
Definition Interaction.h:24
@ ContextMenuChosen
A context menu entry was chosen.
Definition Interaction.h:21
@ Deselect
An element was deselected.
Definition Interaction.h:18
@ Select
An element was selected.
Definition Interaction.h:16
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition Interaction.h:48
const char * toString(InteractionFeedbackType type)
Definition Interaction.h:28
InteractionFeedback const * begin_
InteractionFeedback const * begin() const
InteractionFeedback const * end() const
InteractionFeedback const * end_
InteractionFeedbackType type() const
Definition Interaction.h:62
Eigen::Matrix4f transformation() const
std::string const & layer() const
std::string const & element() const
Eigen::Vector3f scale() const
data::InteractionFeedback data_
Transformable & orientation(Eigen::Matrix3f const &rotationMatrix)
Transformable & pose(Eigen::Matrix4f const &pose)
Eigen::Matrix4f getCurrentPose() const
Eigen::Matrix4f transformation
Transformable & orientation(Eigen::Quaternionf const &quaternion)
Eigen::Matrix4f initialPose
TransformationResult handle(viz::InteractionFeedback const &interaction)
Transformable(std::string const &name)
Transformable & position(Eigen::Vector3f const &position)
Transformable & enable(viz::InteractionDescription const &interaction)