SimoxCSpaceVisualizerWidgetController.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package RobotComponents
19  * @author Raphael Grimm (raphael dot grimm at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 #include <vector>
28 #include <deque>
29 #include <unordered_map>
30 #include <string>
31 #include <memory>
32 #include <tuple>
33 
34 #include <QPointer>
35 #include <QCheckBox>
36 #include <QTimer>
37 
38 #include <Inventor/nodes/SoSeparator.h>
39 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualization.h>
40 
45 
48 #include "../QtWidgets/IndexedQCheckBox.h"
49 #include "../QtWidgets/MotionPlanningServerTaskList.h"
52 
53 #pragma GCC diagnostic push
54 #pragma GCC diagnostic ignored "-Wpedantic"
55 #include <RobotComponents/gui-plugins/MotionPlanning/ui_SimoxCSpaceVisualizerWidget.h>
56 #pragma GCC diagnostic pop
57 
59 
60 namespace armarx
61 {
62 
63  /**
64  * @class SimoxCSpaceVisualizerWidgetController
65  * @page RobotComponents-GuiPlugins-SimoxCSpaceVisualizer Collision Space Visualizer
66  *
67  * @brief The SimoxCSpaceVisualizer allows to visualize any \ref armarx::SimoxCSpace
68  * used by a planning task on the configurated \ref armarx::MotionPlanningServer
69  *
70  * @see \ref RobotComponents-Tutorials-MotionPlanning
71  * @see \ref RobotComponents-Tutorials-SimoxCSpace
72  * @see \ref RobotComponents-GuiPlugins-SimoxCSpaceVisualizer
73  *
74  * @image html mplan_simox_cspace_gui-empty.png
75  * Tasks to load are displayed in the top left corner. You can select one and load it.
76  *
77  * @image html mplan_simox_cspace_gui-loaded.png
78  * Once loaded the bottom left shows all dimensions used for planning, their bounds and current value displayed in the 3D Viewer.
79  * The 3D Viewer shows all objects, the agent and draws found paths.
80  * The paths are visualized for the tcp of the kinematic chain used for planning.
81  * The path consits of the tcp's poses for each node in the path and lines connecting them.
82  *
83  * @image html mplan_simox_cspace_gui-hide_obj.png
84  * To hide an object or the agent, uncheck its box in the bottom mid list.
85  *
86  * @image html mplan_simox_cspace_gui-trace.png
87  * To display the trace of the movement along the path (instead of lines connecting the nodes), check the
88  * box "show joint traces instead of edges" in the top mid.
89  *
90  * @image html mplan_simox_cspace_gui-multi_traces.png
91  * To display the path for other joints, simply select them from the list int the top mid.
92  *
93  * @image html mplan_simox_cspace_gui-move_along_path.png
94  * To play back the motion, select the path in the bottom right and move the slider.
95  * You can play it back by setting some value in the spin box "Animation speed".
96  *
97  * @image html mplan_simox_cspace_gui-set_pose.png
98  * To set a pose, edit the joint values in the bottom left.
99  */
103  {
104  Q_OBJECT
105  protected:
106  public:
107  struct PathData
108  {
109  VectorXfSeq path;
110 
111  float dcdStep = std::numeric_limits<float>::infinity();
112 
113  bool visible = true;
114 
115  /**
116  * @return The path's length.
117  */
118  float getLength() const
119  {
120  return accPathLength.back();
121  }
122  /**
123  * @brief The path's length up to the i-th node.
124  */
125  std::vector<float> accPathLength;
126 
127  static std::string getEdgeLayerName(std::size_t i)
128  {
129  return "PathEdges::" + to_string(i);
130  }
131  };
132 
133  /**
134  * Controller Constructor
135  */
137 
138  /**
139  * Controller destructor
140  */
141  ~SimoxCSpaceVisualizerWidgetController() override = default;
142 
143  /**
144  * @see ArmarXWidgetController::loadSettings()
145  */
146  void loadSettings(QSettings* settings) override;
147 
148  /**
149  * @see ArmarXWidgetController::saveSettings()
150  */
151  void saveSettings(QSettings* settings) override;
152 
153  /**
154  * @return The Widget name displayed in the ArmarXGui to create an
155  * instance of this class.
156  */
157  QString getWidgetName() const override;
158 
159  /**
160  * @see armarx::Component::onInitComponent()
161  */
162  void onInitComponent() override;
163 
164  /**
165  * @param parent The dialog's parent.
166  * @return The plugin's config dialog.
167  */
168  QPointer<QDialog> getConfigDialog(QWidget* parent) override;
169 
170  /**
171  * @brief Callback called when the config dialog is closed.
172  */
173  void configured() override
174  {
175  motionPlanningServerProxyName = dialog->getProxyName("MotionPlanningServer");
176  }
177 
178  /**
179  * @see armarx::Component::onConnectComponent()
180  */
181  void onConnectComponent() override;
182 
183  void onDisconnectComponent() override
184  {
185  motionPlanningServerTaskList->enableAutoUpdate(false);
186  resetSimoxCSpace();
187  }
188 
189  /**
190  * @brief Performs cleanup on exit.
191  */
192  void onExitComponent() override
193  {
194  resetSimoxCSpace();
195  soSeparatorCleanUpAndRemoveFromRoot(visuAgent);
196  soSeparatorCleanUp(visuRoot);
197 
198  }
199 
200  /**
201  * @return The bullet scene used to display the CSpace.
202  */
203  SoNode* getScene() override;
204 
205  /**
206  * @brief Sets the mutex used to protect the bullet scene.
207  * @param mutex3D The mutex to set.
208  */
209  void setMutex3D(RecursiveMutexPtr const& mutex3D) override;
210 
211  /**
212  * @brief Connects all slots.
213  */
214  void connectSlots();
215 
216  /**
217  * @return The current user selected solution path. (exception if there is none)
218  */
220  {
221  return paths.at(widget.spinBoxPathNumber->value() - 1);
222  }
223 
224  public slots:
225  void loadTask();
226 
227  //set state
228  /**
229  * @brief Updates the current path according to the user selection.
230  */
231  void setCurrentPath();
232  /**
233  * @brief Updates the current position on the current path according to the user selection.
234  */
235  void setCurrentPathPosition();
236  /**
237  * @brief Updates the current displayed configuration .
238  */
239  void setCurrentConfig();
240  void updateCollisionState();
241 
242  //draw
243  void drawEdges(bool drawTrace);
244  void drawEdges()
245  {
246  drawEdges(widget.checkBoxDrawTrace->isChecked());
247  }
248 
249  //set visibility
250  /**
251  * @brief Sets the visibility of the given stationary object.
252  * @param index The object's index.
253  */
254  void setVisibilityObjectStationary(int index, Qt::CheckState state)
255  {
256  soSeparatorRootChildVis(visuObjStat.at(index), state == Qt::Checked);
257  }
258 
259  /**
260  * @brief Sets the visibility of the given moveable object.
261  * @param index The object's index.
262  */
263  void setVisibilityAgent(int state)
264  {
265  soSeparatorRootChildVis(visuAgent, state == Qt::Checked);
266  }
267 
268  /**
269  * @brief Sets the visibility of the given solution path.
270  * @param index The path's index.
271  * @param state The checkboxes state.
272  */
273  void setVisibilityPath(int index, Qt::CheckState state);
274 
275  /**
276  * @brief Hides all solution paths.
277  */
278  void hideAllPaths();
279 
280  //do work
281  void traceStepChanged();
282  void toggleRobotNodePath(int index, Qt::CheckState);
283  void updateEdgeVisibility();
284  void highlightCollisionNodes(bool checked);
285 
286  protected:
287  //helper
288  void loadTaskFromServer();
289 
290  void loadTaskFromSend();
291 
292  /**
293  * @brief Cleans up a soseparator and removes it from the root separator.
294  * @param toRemove The separator to remove.
295  */
296  void soSeparatorCleanUpAndRemoveFromRoot(SoSeparator*& toRemove);
297  /**
298  * @brief Cleans up a soseparator. (removes children and the separator.
299  * @param toClear The separator to clean up.
300  */
301  void soSeparatorCleanUp(SoSeparator*& toClear);
302  /**
303  * @brief Removes a soseparator from the root separator.
304  * @param toRemove The separator to remove.
305  */
306  void soSeparatorRemoveFromRoot(SoSeparator* toRemove);
307  /**
308  * @brief Adds a soseparator to the root separator.
309  * @param toAdd The separator to add.
310  */
311  void soSeparatorAddToRoot(SoSeparator* toAdd);
312  /**
313  * @brief Toggles a soseparator's visibility.
314  * @param toToggle The separator to toggle.
315  */
316  void soSeparatorToggleRootChild(SoSeparator* toToggle);
317 
318  void soSeparatorRootChildVis(SoSeparator* child, bool visible = true);
319 
320  //set and init
321  /**
322  * @brief Sets a new simox CSpace (and cleans up the old one)
323  * @param newCSpace The new CSpace.
324  */
325  void setSimoxCSpace(SimoxCSpacePtr newCSpace);
326 
327  void setPaths(std::vector<Path> newPaths);
328 
329  /**
330  * @brief Sets a configuration and visualizes it.
331  * @param cfg The configuration.
332  */
333  void setAndApplyConfig(const VectorXf& cfg);
334 
335  //reset
336  /**
337  * @brief Cleans up the current CSpace.(all visualization will be disabled)
338  */
339  void resetSimoxCSpace();
340 
341  void resetPaths();
342 
344  {
345  widget.sliderPathPosition->setScale(0, 0);
346  }
347 
348  /**
349  * @brief Enables or disables the configuration selection and controles for object visibility
350  * according to the parameter. (called when an CSpace is added)
351  * @param active True = active, false = inactive.
352  */
353  void setEnabledSimoxCSpace(bool active = true);
354 
355  void setEnabledPaths(bool active = true);
356 
357  void timerEvent(QTimerEvent*) override;
358  void setCurrentPathPosition(double p);
359 
360  private:
361  //ui
362  /**
363  * @brief The plugin's ui.
364  */
365  Ui::SimoxCSpaceVisualizerWidget widget;
366  /**
367  * @brief The plugin's config dialog.
368  */
369  QPointer<SimpleConfigDialog> dialog;
370 
371  QPointer<MotionPlanningServerTaskList> motionPlanningServerTaskList;
372 
373  //components
374  std::string motionPlanningServerProxyName;
375  MotionPlanningServerInterfacePrx motionPlanningServerProxy;
376 
377  //basic visu
378  /**
379  * @brief The debug drawer used to draw edges and nodes.
380  */
382  /**
383  * @brief The bullet animation's root separator.
384  */
385  SoSeparator* visuRoot;
386 
387  /**
388  * @brief CSpace used to do collision checks and calculations. (using the visualization one would move the visualized agents)
389  */
390  SimoxCSpacePtr cspaceUtil;
391  /**
392  * @brief CSpace used for visualization. (to visualize configurations)
393  */
394  SimoxCSpacePtr cspaceVisu;
395  /**
396  * @brief Separators holding stationary object's models.
397  */
398  std::vector<SoSeparator*> visuObjStat;
399  SoSeparator* visuAgent = nullptr;
401  //data
402  std::vector<PathData> paths;
403  /**
404  * @brief The robot's nodes and whether they are visible (poses + traces are drawn for them)
405  */
406  std::vector<std::pair<VirtualRobot::RobotNodePtr, bool>> robotNodes;
407  QTimer updateCollisionStateTimer;
408  const long timerPeriod
409  {
410  30
411  };
412  };
413 }
DebugDrawerComponent.h
armarx::SimoxCSpaceVisualizerWidgetController::setVisibilityObjectStationary
void setVisibilityObjectStationary(int index, Qt::CheckState state)
Sets the visibility of the given stationary object.
Definition: SimoxCSpaceVisualizerWidgetController.h:254
armarx::SimoxCSpaceVisualizerWidgetController::PathData::accPathLength
std::vector< float > accPathLength
The path's length up to the i-th node.
Definition: SimoxCSpaceVisualizerWidgetController.h:125
armarx::SimoxCSpaceVisualizerWidgetController::PathData::path
VectorXfSeq path
Definition: SimoxCSpaceVisualizerWidgetController.h:109
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::SimoxCSpaceVisualizerWidgetController::resetCurrentPath
void resetCurrentPath()
Definition: SimoxCSpaceVisualizerWidgetController.h:343
ScaledCSpace.h
armarx::SimoxCSpaceVisualizerWidgetController::PathData
Definition: SimoxCSpaceVisualizerWidgetController.h:107
armarx::SimoxCSpaceVisualizerWidgetController::configured
void configured() override
Callback called when the config dialog is closed.
Definition: SimoxCSpaceVisualizerWidgetController.h:173
armarx::SimoxCSpaceVisualizerWidgetController::onExitComponent
void onExitComponent() override
Performs cleanup on exit.
Definition: SimoxCSpaceVisualizerWidgetController.h:192
armarx::SimoxCSpaceVisualizerWidgetController::getCurrentPath
PathData & getCurrentPath()
Definition: SimoxCSpaceVisualizerWidgetController.h:219
SimpleConfigDialog.h
armarx::SimoxCSpaceVisualizerWidgetController::drawEdges
void drawEdges()
Definition: SimoxCSpaceVisualizerWidgetController.h:244
IceInternal::Handle< SimoxCSpace >
ArmarXGuiPlugin.h
ARMARXCOMPONENT_IMPORT_EXPORT
#define ARMARXCOMPONENT_IMPORT_EXPORT
Definition: ImportExportComponent.h:38
Metrics.h
CoinVisualizationPtr
boost::shared_ptr< VirtualRobot::CoinVisualization > CoinVisualizationPtr
Definition: ManipulatorVisualization.h:52
armarx::SimoxCSpaceVisualizerWidgetController::PathData::getEdgeLayerName
static std::string getEdgeLayerName(std::size_t i)
Definition: SimoxCSpaceVisualizerWidgetController.h:127
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
SimoxCSpace.h
ArmarXWidgetController.h
armarx::SimoxCSpaceVisualizerWidgetController::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: SimoxCSpaceVisualizerWidgetController.h:183
armarx::SimoxCSpaceVisualizerWidgetController::PathData::getLength
float getLength() const
Definition: SimoxCSpaceVisualizerWidgetController.h:118
armarx::SimoxCSpaceVisualizerWidgetController::setVisibilityAgent
void setVisibilityAgent(int state)
Sets the visibility of the given moveable object.
Definition: SimoxCSpaceVisualizerWidgetController.h:263
armarx::SimoxCSpaceVisualizerWidgetController
Definition: SimoxCSpaceVisualizerWidgetController.h:100
armarx::ArmarXComponentWidgetController
Definition: ArmarXComponentWidgetController.h:39
CollisionCheckUtil.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
ImportExportComponent.h