Interaction.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include <RobotAPI/interface/ArViz/Component.h>
5 
6 #include <Eigen/Core>
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* toString(InteractionFeedbackType type)
28  {
29  switch (type)
30  {
32  return "None";
34  return "Select";
36  return "Deselect";
38  return "ContextMenuChosen";
40  return "Transform";
41  default:
42  return "<Unknown>";
43  }
44  }
45 
46  inline Eigen::Matrix4f toEigen(data::GlobalPose const& pose)
47  {
48  Eigen::Quaternionf ori(pose.qw, pose.qx, pose.qy, pose.qz);
49 
50  Eigen::Matrix4f result;
51  result.block<3, 3>(0, 0) = ori.toRotationMatrix();
52  result.block<3, 1>(0, 3) = Eigen::Vector3f(pose.x, pose.y, pose.z);
53  result.block<1, 4>(3, 0) = Eigen::Vector4f(0.0f, 0.0f, 0.0f, 1.0f);
54  return result;
55  }
56 
58  {
60  {
61  // Maks out all the flags in the higher bits
62  int type = data_.type & 0x7;
63 
64  switch (type)
65  {
68 
69  case data::InteractionFeedbackType::SELECT:
71 
72  case data::InteractionFeedbackType::DESELECT:
74 
75  case data::InteractionFeedbackType::CONTEXT_MENU_CHOSEN:
77 
78  case data::InteractionFeedbackType::TRANSFORM:
80 
81  default:
82  throw std::runtime_error("Unexpected InteractionFeedbackType");
83  }
84  }
85 
86  bool isTransformBegin() const
87  {
88  return data_.type & data::InteractionFeedbackType::TRANSFORM_BEGIN_FLAG;
89  }
90 
91  bool isTransformDuring() const
92  {
93  return data_.type & data::InteractionFeedbackType::TRANSFORM_DURING_FLAG;
94  }
95 
96  bool isTransformEnd() const
97  {
98  return data_.type & data::InteractionFeedbackType::TRANSFORM_END_FLAG;
99  }
100 
101  std::string const& layer() const
102  {
103  return data_.layer;
104  }
105 
106  std::string const& element() const
107  {
108  return data_.element;
109  }
110 
111  long revision() const
112  {
113  return data_.revision;
114  }
115 
117  {
118  return data_.chosenContextMenuEntry;
119  }
120 
122  {
123  return toEigen(data_.transformation);
124  }
125 
126  Eigen::Vector3f scale() const
127  {
128  Eigen::Vector3f result(data_.scale.e0, data_.scale.e1, data_.scale.e2);
129  return result;
130  }
131 
132  data::InteractionFeedback data_;
133  };
134 
136  {
137  InteractionFeedback const* begin() const
138  {
139  return begin_;
140  }
141 
142  InteractionFeedback const* end() const
143  {
144  return end_;
145  }
146 
147  std::size_t size() const
148  {
149  return end_ - begin_;
150  }
151 
152  bool empty() const
153  {
154  return begin_ == end_;
155  }
156 
159  };
160 
162  {
163  // Indicates that a transfomation was applied during the interaction
164  bool wasTransformed = false;
165 
166  // Indicates that a layer update is needed
167  // If this flag is set, you should commit the containing layer with the next arviz.commit()
168  bool needsLayerUpdate = false;
169  };
170 
171  template <typename ElementT>
173  {
174  Transformable(std::string const& name)
175  : element(name)
176  {
177  }
178 
180  {
181  element.pose(pose);
182  initialPose = pose;
183  return *this;
184  }
185 
186  Transformable& position(Eigen::Vector3f const& position)
187  {
188  element.position(position);
189  initialPose.block<3, 1>(0, 3) = position;
190  return *this;
191  }
192 
193  Transformable& orientation(Eigen::Matrix3f const& rotationMatrix)
194  {
195  element.orientation(rotationMatrix);
196  initialPose.block<3, 3>(0, 0) = rotationMatrix;
197  return *this;
198  }
199 
201  {
202  element.orientation(quaternion);
203  initialPose.block<3, 3>(0, 0) = quaternion.toRotationMatrix();
204  return *this;
205  }
206 
208  {
209  element.enable(interaction);
210  // A movable element is always hidden during the interaction
211  element.data_->interaction.enableFlags |= viz::data::InteractionEnableFlags::TRANSFORM_HIDE;
212  return *this;
213  }
214 
215  // The pose after the current transformation has been applied
217  {
218  return transformation * initialPose;
219  }
220 
221  // Returns true, if the element has been changed and the layer needs to be comitted again
223  {
224  TransformationResult result;
225  if (interaction.element() == element.data_->id)
226  {
227  switch (interaction.type())
228  {
230  {
231  // Keep track of the transformation
232  transformation = interaction.transformation();
233  result.wasTransformed = true;
234  } break;
236  {
237  // If an object is deselected, we apply the transformation
240  element.pose(initialPose);
241 
242  // In this case, the user needs to commit the layer
243  result.needsLayerUpdate = true;
244  } break;
245  default:
246  {
247  // Ignore the other events
248  }
249  }
250  }
251  return result;
252  }
253 
254 
257 
258  ElementT element;
259  };
260 
261 }
armarx::viz::InteractionFeedbackType::ContextMenuChosen
@ ContextMenuChosen
A context menu entry was chosen.
armarx::viz::Transformable::position
Transformable & position(Eigen::Vector3f const &position)
Definition: Interaction.h:186
armarx::viz::TransformationResult::wasTransformed
bool wasTransformed
Definition: Interaction.h:164
armarx::viz::TransformationResult::needsLayerUpdate
bool needsLayerUpdate
Definition: Interaction.h:168
armarx::viz::InteractionFeedbackRange::end_
InteractionFeedback const * end_
Definition: Interaction.h:158
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:46
armarx::viz::interaction
InteractionDescription interaction()
Definition: ElementOps.h:101
ElementOps.h
armarx::viz::InteractionFeedbackType
InteractionFeedbackType
Definition: Interaction.h:10
armarx::viz::InteractionFeedback::element
std::string const & element() const
Definition: Interaction.h:106
armarx::viz::Transformable::transformation
Eigen::Matrix4f transformation
Definition: Interaction.h:256
armarx::viz::InteractionFeedback::transformation
Eigen::Matrix4f transformation() const
Definition: Interaction.h:121
armarx::viz::InteractionFeedbackRange::begin_
InteractionFeedback const * begin_
Definition: Interaction.h:157
armarx::viz::InteractionFeedbackRange::empty
bool empty() const
Definition: Interaction.h:152
armarx::viz::InteractionFeedbackType::None
@ None
Nothing happened.
armarx::viz::Transformable::enable
Transformable & enable(viz::InteractionDescription const &interaction)
Definition: Interaction.h:207
armarx::viz::Transformable
Definition: Interaction.h:172
armarx::viz::InteractionFeedback::layer
std::string const & layer() const
Definition: Interaction.h:101
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::viz::InteractionFeedbackType::Select
@ Select
An element was selected.
armarx::viz::InteractionFeedback::isTransformBegin
bool isTransformBegin() const
Definition: Interaction.h:86
armarx::viz::Transformable::handle
TransformationResult handle(viz::InteractionFeedback const &interaction)
Definition: Interaction.h:222
armarx::viz::Transformable::pose
Transformable & pose(Eigen::Matrix4f const &pose)
Definition: Interaction.h:179
armarx::viz::InteractionDescription
Definition: ElementOps.h:36
armarx::viz::InteractionFeedbackType::Transform
@ Transform
The element was transformed (translated or rotated).
armarx::viz::Transformable::initialPose
Eigen::Matrix4f initialPose
Definition: Interaction.h:255
armarx::viz::InteractionFeedback::revision
long revision() const
Definition: Interaction.h:111
armarx::viz::InteractionFeedback::scale
Eigen::Vector3f scale() const
Definition: Interaction.h:126
armarx::viz::Transformable::element
ElementT element
Definition: Interaction.h:258
armarx::viz::InteractionFeedback::type
InteractionFeedbackType type() const
Definition: Interaction.h:59
armarx::viz::InteractionFeedbackRange
Definition: Interaction.h:135
armarx::viz::InteractionFeedback
Definition: Interaction.h:57
armarx::viz::InteractionFeedback::isTransformEnd
bool isTransformEnd() const
Definition: Interaction.h:96
armarx::viz::Transformable::Transformable
Transformable(std::string const &name)
Definition: Interaction.h:174
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::viz::Transformable::orientation
Transformable & orientation(Eigen::Quaternionf const &quaternion)
Definition: Interaction.h:200
armarx::viz::InteractionFeedbackType::Deselect
@ Deselect
An element was deselected.
armarx::aron::similarity::FloatSimilarity::NONE
@ NONE
Definition: FloatSimilarity.h:11
armarx::Quaternion< float, 0 >
armarx::viz::Transformable::orientation
Transformable & orientation(Eigen::Matrix3f const &rotationMatrix)
Definition: Interaction.h:193
armarx::viz::toString
const char * toString(InteractionFeedbackType type)
Definition: Interaction.h:27
armarx::viz::Transformable::getCurrentPose
Eigen::Matrix4f getCurrentPose() const
Definition: Interaction.h:216
armarx::viz::InteractionFeedbackRange::size
std::size_t size() const
Definition: Interaction.h:147
armarx::viz::InteractionFeedback::data_
data::InteractionFeedback data_
Definition: Interaction.h:132
armarx::viz::InteractionFeedback::isTransformDuring
bool isTransformDuring() const
Definition: Interaction.h:91
armarx::viz::InteractionFeedbackRange::begin
InteractionFeedback const * begin() const
Definition: Interaction.h:137
armarx::viz::InteractionFeedbackRange::end
InteractionFeedback const * end() const
Definition: Interaction.h:142
armarx::viz
This file is part of ArmarX.
Definition: ArVizStorage.cpp:416
armarx::viz::InteractionFeedback::chosenContextMenuEntry
int chosenContextMenuEntry() const
Definition: Interaction.h:116
armarx::viz::TransformationResult
Definition: Interaction.h:161