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 
8 namespace armarx::viz
9 {
11  {
12  /** Nothing happened. */
13  None,
14 
15  /** An element was selected. */
16  Select,
17  /** An element was deselected. */
18  Deselect,
19 
20  /** A context menu entry was chosen. */
22 
23  /** The element was transformed (translated or rotated). */
24  Transform,
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  {
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 
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  {
149  InteractionFeedback const*
150  begin() const
151  {
152  return begin_;
153  }
154 
155  InteractionFeedback const*
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 
196  {
197  element.pose(pose);
198  initialPose = 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 
219  orientation(Eigen::Quaternionf const& quaternion)
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
239  {
240  return transformation * initialPose;
241  }
242 
243  // Returns true, if the element has been changed and the layer needs to be comitted again
246  {
247  TransformationResult result;
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
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 
281 
282  ElementT element;
283  };
284 
285 } // namespace armarx::viz
armarx::viz::InteractionFeedbackType::ContextMenuChosen
@ ContextMenuChosen
A context menu entry was chosen.
armarx::viz::Transformable::position
Transformable & position(Eigen::Vector3f const &position)
Definition: Interaction.h:203
armarx::viz::TransformationResult::wasTransformed
bool wasTransformed
Definition: Interaction.h:180
armarx::viz::TransformationResult::needsLayerUpdate
bool needsLayerUpdate
Definition: Interaction.h:184
armarx::viz::InteractionFeedbackRange::end_
InteractionFeedback const * end_
Definition: Interaction.h:174
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:48
armarx::viz::interaction
InteractionDescription interaction()
Definition: ElementOps.h:109
ElementOps.h
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::viz::InteractionFeedbackType
InteractionFeedbackType
Definition: Interaction.h:10
armarx::viz::InteractionFeedback::element
std::string const & element() const
Definition: Interaction.h:114
armarx::viz::Transformable::transformation
Eigen::Matrix4f transformation
Definition: Interaction.h:280
armarx::viz::InteractionFeedback::transformation
Eigen::Matrix4f transformation() const
Definition: Interaction.h:132
armarx::viz::InteractionFeedbackRange::begin_
InteractionFeedback const * begin_
Definition: Interaction.h:173
armarx::viz::InteractionFeedbackRange::empty
bool empty() const
Definition: Interaction.h:168
armarx::viz::InteractionFeedbackType::None
@ None
Nothing happened.
armarx::viz::Transformable::enable
Transformable & enable(viz::InteractionDescription const &interaction)
Definition: Interaction.h:227
armarx::viz::Transformable
Definition: Interaction.h:188
armarx::viz::InteractionFeedback::layer
std::string const & layer() const
Definition: Interaction.h:108
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
armarx::viz::InteractionFeedbackType::Select
@ Select
An element was selected.
armarx::viz::InteractionFeedback::isTransformBegin
bool isTransformBegin() const
Definition: Interaction.h:90
armarx::viz::Transformable::handle
TransformationResult handle(viz::InteractionFeedback const &interaction)
Definition: Interaction.h:245
armarx::viz::Transformable::pose
Transformable & pose(Eigen::Matrix4f const &pose)
Definition: Interaction.h:195
armarx::viz::InteractionDescription
Definition: ElementOps.h:35
armarx::viz::InteractionFeedbackType::Transform
@ Transform
The element was transformed (translated or rotated).
armarx::viz::Transformable::initialPose
Eigen::Matrix4f initialPose
Definition: Interaction.h:279
armarx::viz::InteractionFeedback::revision
long revision() const
Definition: Interaction.h:120
armarx::viz::InteractionFeedback::scale
Eigen::Vector3f scale() const
Definition: Interaction.h:138
armarx::viz::Transformable::element
ElementT element
Definition: Interaction.h:282
armarx::viz::InteractionFeedback::type
InteractionFeedbackType type() const
Definition: Interaction.h:62
armarx::viz::InteractionFeedbackRange
Definition: Interaction.h:147
armarx::viz::InteractionFeedback
Definition: Interaction.h:59
armarx::viz::InteractionFeedback::isTransformEnd
bool isTransformEnd() const
Definition: Interaction.h:102
armarx::viz::Transformable::Transformable
Transformable(std::string const &name)
Definition: Interaction.h:190
armarx::viz::Transformable::orientation
Transformable & orientation(Eigen::Quaternionf const &quaternion)
Definition: Interaction.h:219
armarx::viz::InteractionFeedbackType::Deselect
@ Deselect
An element was deselected.
armarx::aron::similarity::FloatSimilarity::NONE
@ NONE
Definition: FloatSimilarity.h:14
armarx::Quaternion< float, 0 >
armarx::viz::Transformable::orientation
Transformable & orientation(Eigen::Matrix3f const &rotationMatrix)
Definition: Interaction.h:211
armarx::viz::toString
const char * toString(InteractionFeedbackType type)
Definition: Interaction.h:28
armarx::viz::Transformable::getCurrentPose
Eigen::Matrix4f getCurrentPose() const
Definition: Interaction.h:238
armarx::viz::InteractionFeedbackRange::size
std::size_t size() const
Definition: Interaction.h:162
armarx::viz::InteractionFeedback::data_
data::InteractionFeedback data_
Definition: Interaction.h:144
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:649
armarx::viz::InteractionFeedback::isTransformDuring
bool isTransformDuring() const
Definition: Interaction.h:96
armarx::viz::InteractionFeedbackRange::begin
InteractionFeedback const * begin() const
Definition: Interaction.h:150
armarx::viz::InteractionFeedbackRange::end
InteractionFeedback const * end() const
Definition: Interaction.h:156
armarx::viz
This file is part of ArmarX.
Definition: ArVizStorage.cpp:418
armarx::viz::InteractionFeedback::chosenContextMenuEntry
int chosenContextMenuEntry() const
Definition: Interaction.h:126
armarx::viz::TransformationResult
Definition: Interaction.h:177