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