CoinRobotViewerAdapter.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarXGuiPlugins::RobotTrajectoryDesigner::Visualization
17 * @author Timo Birr
18 * @date 2018
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
24
26
28
30#include "Inventor/draggers/SoDragger.h"
31#include "Inventor/nodes/SoSeparator.h"
32#include "VirtualRobot/Scene.h"
33#include "VirtualRobot/Visualization/CoinVisualization/CoinVisualization.h"
34#include "VirtualRobot/Visualization/CoinVisualization/CoinVisualizationNode.h"
35#include <Inventor/Qt/SoQt.h>
36#include <Inventor/SoFullPath.h>
37#include <Inventor/SoInteraction.h>
38#include <Inventor/actions/SoLineHighlightRenderAction.h>
39#include <Inventor/misc/SoContextHandler.h>
40#include <Inventor/nodes/SoCube.h>
41#include <Inventor/nodes/SoPickStyle.h>
42#include <Inventor/nodes/SoTranslation.h>
43#include <Inventor/sensors/SoTimerSensor.h>
44
45#define ROBOT_UPDATE_TIMER_MS 333
46
47#define AUTO_FOLLOW_UPDATE 50
48
49
50using namespace VirtualRobot;
51using namespace armarx;
52using namespace Qt;
53
55{
56
57
58 robotUpdateSensor = NULL;
59 this->selectedWaypoint = 0;
60 this->wayPointCounter = 0;
61 this->viewer = std::shared_ptr<RobotViewer>(new RobotViewer(widget));
62 camera = viewer->getCamera();
64 manipulator = new ManipulatorVisualization;
65 SoInteraction::init();
66 viewer->setGLRenderAction(new SoLineHighlightRenderAction);
67 viewer->getRootNode()->addChild((SoNode*)manipulator);
68 manipulatorMoved = false;
69 startUpCameraPositioningFlag = true;
70 selected = new SoSelection();
71 //selected->addSelectionCallback(made_selection, viewer.get());
72 //selected->addcctionCallback(unmade_selection, viewer.get());
73 //selected->setPickFilterCallback(pickFilterCB, viewer.get(), true);
74 //selected->policy = SoSelection::SINGLE;
75 viewer->getRootNode()->addSelectionCallback(made_selection, viewer.get());
76 viewer->getRootNode()->addDeselectionCallback(unmade_selection, viewer.get());
77 viewer->getRootNode()->setPickFilterCallback(pickFilterCB, viewer.get(), true);
78 viewer->getRootNode()->policy = SoSelection::SINGLE;
79 manipulator->addManipFinishCallback(manipFinishCallback, this);
80 manipulator->addManipMovedCallback(manipMovedCallback, this);
81 SoSensorManager* sensor_mgr = SoDB::getSensorManager();
82 autoFollowSensor = new SoTimerSensor(autoFollowSensorTimerCB, this);
83 autoFollowSensor->setInterval(SbTime(AUTO_FOLLOW_UPDATE / 1000.0f));
84 sensor_mgr->insertTimerSensor(autoFollowSensor);
85 viewer->getRootNode()->addChild((SoNode*)manipulator);
86 manipFinishCallback(this, NULL);
87
88 //selected->ref();
90}
91
93{
94
95 this->clearTrajectory();
96 while (transitions.getLength() != 0)
97 {
98 transitions.remove(0);
99 }
100 while (wayPoints.getLength() != 0)
101 {
102 wayPoints.remove(0);
103 }
104 SoSensorManager* sensor_mgr = SoDB::getSensorManager();
105 sensor_mgr->removeTimerSensor(robotUpdateSensor);
106 sensor_mgr->removeTimerSensor(autoFollowSensor);
107 this->viewer->getRootNode()->removeAllChildren();
108
109 manipulator->addManipFinishCallback(NULL, NULL);
110 manipulator->addManipMovedCallback(NULL, NULL);
111 manipulator->removeVisualization();
112 manipulator = NULL;
113 viewer = NULL;
114 robotUpdateSensor = NULL;
115 autoFollowSensor = NULL;
116 selected = NULL;
117 dragger = NULL;
118 camera = NULL;
119 factory = NULL;
120 ARMARX_INFO << "Destroyed CoinViewer";
121}
122
123/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
124/// METHODS FOR VISUALIZATION SETUP
125/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
126void
128{
129 if (robot)
130 {
131 if ((robot) && (this->robot) && this->robot->getName() != robot->getName())
132 {
133 this->viewer->getRootNode()->removeAllChildren();
134 }
135 this->robot = robot;
136 this->selectedWaypoint = 0;
137 viewer->getRootNode()->deselectAll();
138 manipulator->removeVisualization();
139 manipulatorMoved = false;
140 viewer->getRootNode()->addChild((SoNode*)manipulator);
141 //Make Robot not pickable
142 SoPickStyle* unpickable = new SoPickStyle();
143 unpickable->style = SoPickStyle::UNPICKABLE;
144 SoPickStyle* pickable = new SoPickStyle();
145 pickable->style = SoPickStyle::SHAPE;
146 viewer->getRootNode()->addChild(unpickable);
147 CoinVisualizationPtr robotViewerVisualization =
148 this->robot->getVisualization<VirtualRobot::CoinVisualization>();
149 this->viewer->getRootNode()->addChild(robotViewerVisualization->getCoinVisualization());
150 RobotNodeSetPtr nodeset = robot->getRobotNodeSet(selectedChain.toStdString());
151 //manipulator->setVisualization(robot, nodeset);
152 SoSensorManager* sensor_mgr = SoDB::getSensorManager();
153 sensor_mgr->removeTimerSensor(robotUpdateSensor);
154 robotUpdateSensor = new SoTimerSensor(robotUpdateTimerCB, this);
155 robotUpdateSensor->setInterval(SbTime(ROBOT_UPDATE_TIMER_MS / 1000.0f));
156 sensor_mgr->insertTimerSensor(robotUpdateSensor);
157
158 viewer->getRootNode()->addChild(pickable);
159 viewer->getRootNode()->addChild((SoNode*)manipulator);
160 manipulator->addManipFinishCallback(manipFinishCallback, this);
161 manipulator->addManipMovedCallback(manipMovedCallback, this);
162 viewer->viewAll();
163 }
164}
165
166void
168{
169 CoinVisualizationPtr sceneVisualization =
170 scene->getVisualization<VirtualRobot::CoinVisualization>();
171 this->viewer->getRootNode()->addChild(sceneVisualization->getCoinVisualization());
172}
173
174void
175CoinRobotViewerAdapter::setCamera(Eigen::VectorXf position,
176 Eigen::VectorXf pointAtA,
177 Eigen::VectorXf pointAtB)
178{
179 camera->position.setValue(position[0], position[1], position[2]);
180 camera->pointAt(SbVec3f(pointAtA[0], pointAtA[1], pointAtA[2]),
181 SbVec3f(pointAtB[0], pointAtB[1], pointAtB[2]));
182 camera->viewAll(viewer->getRootNode(), SbViewportRegion());
183}
184
185/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
186/// METHODS FOR TRANSITIONS
187/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
188void
189CoinRobotViewerAdapter::addTransitionVisualization(int index, std::vector<PoseBasePtr> transition)
190{
191 VisualizationNodePtr node = this->factory->createCurve(transition, false);
192 CoinVisualizationNodePtr visu = boost::dynamic_pointer_cast<CoinVisualizationNode>(node);
193 SoNode* newTransition = visu->getCoinVisualization();
194 viewer->getRootNode()->addChild(newTransition);
195 transitions.insert(newTransition, index);
196}
197
198void
200{
201 SoNode* toDelete = transitions[index];
202 viewer->getRootNode()->removeChild(toDelete);
203 transitions.remove(index);
204}
205
206void
208 std::vector<PoseBasePtr> transition)
209{
211 VisualizationNodePtr node = this->factory->createCurve(transition, true);
212 CoinVisualizationNodePtr visu = boost::dynamic_pointer_cast<CoinVisualizationNode>(node);
213 SoNode* newTransition = visu->getCoinVisualization();
214 viewer->getRootNode()->addChild(newTransition);
215 transitions.insert(newTransition, index);
216 /* std::vector<Eigen::Vector3f> curve = std::vector<Eigen::Vector3f>();
217
218 for (Vector3BasePtr position : transition)
219 {
220 curve.push_back(Eigen::Vector3f(position->x, position->y, position->z));
221 }
222 removeTransitionVisualization(index);
223 transitions.insert(dynamic_cast<CoinVisualizationNode&>(visu).getCoinVisualization(), index);*/
224}
225
226/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
227/// INHERITED BY OBSERVER
228/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
229Eigen::Matrix4f
231{
232 return manipulator->getUserDesiredPose();
233}
234
235int
237{
238 //ARMARX_INFO << std::to_string(this->selectedWaypoint);
239 return this->selectedWaypoint;
240}
241
244{
245 return std::make_shared<AdvancedCoinVisualizationFactory>(AdvancedCoinVisualizationFactory());
246}
247
248void
250 PoseBasePtr waypoint,
251 EndEffectorPtr tcp)
252{
253
254 SoNode* newWaypoint;
255 if (tcp)
256 {
257 VirtualRobot::RobotPtr endEffectorRobot = tcp->createEefRobot("", "");
258 Pose p = Pose(waypoint->position, waypoint->orientation);
259 EndEffectorPtr eef = endEffectorRobot->getEndEffectors()[0];
260 endEffectorRobot->setGlobalPoseForRobotNode(eef->getTcp(), p.toEigen());
261 eef->getTcp()->setGlobalPoseNoChecks(p.toEigen());
262
263 newWaypoint = CoinVisualizationFactory::getCoinVisualization(
264 endEffectorRobot, SceneObject::VisualizationType::Full, true);
265 }
266 else
267 {
268 SoCube* cube = new SoCube();
269 cube->width = 0.09;
270 cube->height = 0.09;
271 cube->depth = 0.09;
272 SoTranslation* cubePoint = new SoTranslation();
273 cubePoint->translation.setValue(waypoint->position->x / 1000.0,
274 waypoint->position->y / 1000.0,
275 waypoint->position->z / 1000.0);
276 SoSeparator* parent = new SoSeparator();
277 parent->addChild(cubePoint);
278 parent->addChild(cube);
279 newWaypoint = parent;
280 }
281 const std::string s = "W" + std::to_string(index);
282 const char* c = s.c_str();
283 newWaypoint->setName(SbName(c));
284 std::string st = std::string(newWaypoint->getName().getString());
285 viewer->getRootNode()->addChild(newWaypoint);
286 viewer->getRootNode()->select(newWaypoint);
287 wayPoints.insert(newWaypoint, index);
288 wayPointCounter++;
289 viewer->getRootNode()->deselectAll();
290}
291
292void
294{
295 SoNode* toDelete = wayPoints[index];
296 viewer->getRootNode()->removeChild(toDelete);
297 wayPoints.remove(index);
298 viewer->getRootNode()->deselectAll();
299}
300
301void
303{
304 while (wayPoints.getLength() != 0)
305 {
307 }
308 wayPointCounter = 0; //just to be sure
309}
310
311void
313{
314 this->selectedWaypoint = index;
315 refreshSelectedPoint();
316}
317
318/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
319/// SETTING MANIPULATOR
320/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
321void
322CoinRobotViewerAdapter::setManipulator(VirtualRobot::RobotNodeSetPtr kc,
323 std::vector<float> jointAngles)
324{
325 if (!kc)
326 {
327 manipulator->removeVisualization();
328 return;
329 }
330 robot->getRobotNodeSet(kc->getName())->setJointValues(jointAngles);
331 manipulator->setVisualization(robot, kc);
332 manipulator->addManipFinishCallback(manipFinishCallback, this);
333 manipulator->addManipMovedCallback(manipMovedCallback, this);
334}
335
336/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
337/// UPDATING OF VISUALIZATION
338/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
339void
341{
342 viewer->render();
343}
344
345void
347{
348 viewer->show();
349}
350
351void
353{
354 viewer->hide();
355}
356
357void
359{
360 while (transitions.getLength() != 0)
361 {
363 }
364 while (wayPoints.getLength() != 0)
365 {
367 }
368}
369
372{
373 std::shared_ptr<CoinRobotViewerAdapter> reproduction =
374 std::make_shared<CoinRobotViewerAdapter>(parent);
375 reproduction->viewer->getRootNode()->addChild(this->viewer->getRootNode());
376 reproduction->camera = reproduction->viewer->getCamera();
377 Eigen::Vector3f position;
378 Eigen::Vector3f pointAtA = Eigen::Vector3f(0, 0, 0);
379 Eigen::Vector3f pointAtB;
380 position = Eigen::Vector3f(0, 2, 3.5);
381 pointAtB = Eigen::Vector3f(0, 0, 5);
382 reproduction->setCamera(position, pointAtA, pointAtB);
383 reproduction->viewer->getCamera()->viewAll(this->viewer->getRootNode(), SbViewportRegion());
384 return reproduction;
385}
386
387///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
388///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
389///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
390///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
391///CALLBACK METHODS
392/// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
393void
394CoinRobotViewerAdapter::manipFinishCallback(void* data, SoDragger* dragger)
395{
397 if (controller)
398 {
399 if (controller->observer.lock())
400 {
401 controller->observer.lock()->refresh();
402 }
403 }
404}
405
406void
407CoinRobotViewerAdapter::manipMovedCallback(void* data, SoDragger* dragger)
408{
409 CoinRobotViewerAdapter* controller = static_cast<CoinRobotViewerAdapter*>(data);
410 if (controller)
411 {
412 controller->manipulatorMoved = true;
413 if (controller->observer.lock())
414 {
415 controller->observer.lock()->refresh();
416 }
417 }
418}
419
420void
421CoinRobotViewerAdapter::autoFollowSensorTimerCB(void* data, SoSensor* sensor)
422{
423 CoinRobotViewerAdapter* controller = static_cast<CoinRobotViewerAdapter*>(data);
424
425 if (controller && controller->manipulatorMoved)
426 {
427 if (controller->observer.lock())
428 {
429 controller->observer.lock()->refresh();
430 }
431 }
432}
433
434void
435CoinRobotViewerAdapter::made_selection(void* data, SoPath* path)
436{
437 CoinRobotViewerAdapter* controller = static_cast<CoinRobotViewerAdapter*>(data);
438 if (controller)
439 {
440 //ARMARX_INFO << std::to_string(controller->selectedWaypoint);
441 std::string s = path->getTail()->getName().getString();
442 std::string number = s.substr(1);
443 controller->selectedWaypoint = std::stoi(number);
444 //ARMARX_INFO << std::to_string(controller->selectedWaypoint);
445 if (controller->observer.lock())
446 {
447 controller->observer.lock()->refresh();
448 }
449 }
450}
451
452void
453CoinRobotViewerAdapter::unmade_selection(void* data, SoPath* path)
454{
455 CoinRobotViewerAdapter* controller = static_cast<CoinRobotViewerAdapter*>(data);
456 if (!controller || !controller->robot)
457 {
458 return;
459 }
460}
461
462SoPath*
463CoinRobotViewerAdapter::pickFilterCB(void* data, const SoPickedPoint* pick)
464{
465 CoinRobotViewerAdapter* controller = static_cast<CoinRobotViewerAdapter*>(data);
466 if (!controller || !controller->robot)
467 {
468 return new SoPath();
469 }
470
471 SoFullPath* p = (SoFullPath*)pick->getPath();
472 //Make sure we didn't select a manipulator
473 for (int i = 0; i < p->getLength();)
474 {
475 SoNode* n = p->getNode(p->getLength() - 1);
476 //ARMARX_INFO << std::to_string(controller->wayPointCounter);
477 //TODO find out how to get counter
478 for (int j = 0; j < 50; j++)
479 {
480 std::string currentName = std::string(n->getName().getString());
481 //ARMARX_INFO << currentName << "XXXXXXXXXXXX";
482 //ARMARX_INFO << "W" + std::to_string(j) << "XLOKEDFOR";
483 if (currentName == "W" + std::to_string(j))
484 {
485 return p;
486 }
487 }
488 p->truncate(p->getLength() - 1);
489 }
490 return new SoPath();
491}
492
493void
494CoinRobotViewerAdapter::refreshSelectedPoint()
495{
496 SoNode* selectedPoint = static_cast<SoNode*>(wayPoints.get(this->selectedWaypoint));
497 this->viewer->getRootNode()->deselectAll();
498 this->viewer->getRootNode()->select(selectedPoint);
499}
500
501void
502CoinRobotViewerAdapter::robotUpdateTimerCB(void* data, SoSensor* sensor)
503{
504 //static_cast<SoPerspectiveCamera*>(viewer->getCamera()->);
505 CoinRobotViewerAdapter* controller = static_cast<CoinRobotViewerAdapter*>(data);
506 if (!controller || !controller->robot)
507 {
508 return;
509 }
510 //UPDATE SELCETION
511 if (controller->viewer->getRootNode()->getList()->getLength() != 0)
512 {
513 SoPath* p = (SoPath*)controller->viewer->getRootNode()->getList()->get(0);
514 if (p)
515 {
516 std::string s = p->getTail()->getName().getString();
517 if (s.length() != 0)
518 {
519 std::string number = s.substr(1);
520 controller->selectedWaypoint = std::stoi(number);
521 controller->refreshSelectedPoint();
522 if (controller->observer.lock())
523 {
524 controller->observer.lock()->refresh();
525 }
526 }
527 }
528 }
529 controller->viewer->render();
530
531
532 if (controller->startUpCameraPositioningFlag)
533 {
534 controller->viewer->cameraViewAll();
535 controller->startUpCameraPositioningFlag = false;
536 }
537}
uint8_t data[1]
uint8_t index
#define AUTO_FOLLOW_UPDATE
#define ROBOT_UPDATE_TIMER_MS
constexpr T c
The CoinRobotViewerAdapter class.
AdvancedVisualizationFactoryPtr createAdvancedVisualizationFactory() override
CoinRobotViewerAdapter(QWidget *widget)
CoinRobotViewerAdapter.
void removeWaypointVisualization(int index) override
removeWaypointVisualization removes the Waypoint with index so that it is no longer visualized
void addRobotVisualization(VirtualRobot::RobotPtr robot, QString selectedChain) override
METHODS FOR VISUALIZATION SETUP.
void clearTrajectory() override
clearTrajectory removes all visualization of waypoints and transitions but keeps the robot and the ma...
void setSelectedWaypoint(int index) override
removeWaypointVisualization removes the Waypoint with index so that it is no longer visualized
void updateRobotVisualization() override
UPDATING OF VISUALIZATION.
void addSceneVisualization(VirtualRobot::ScenePtr scene) override
addSceneVisualization visualizes the whole scene where the robot is placed in
void addWaypointVisualization(int index, PoseBasePtr waypoint, VirtualRobot::EndEffectorPtr tcp) override
METHODS FOR WAYPOINTS.
void addTransitionVisualization(int index, std::vector< PoseBasePtr > transition) override
METHODS FOR TRANSITIONS.
void enableVisualization() override
enableVisualization shows the viewer
Eigen::Matrix4f getManipulatorPose() override
INHERITED BY OBSERVER.
RobotVisualizationPtr reproduce(QWidget *parent) override
UPDATING OF VISUALIZATION.
void removeTransitionVisualization(int index) override
removeTransitionVisualization removes visualization of a certain transition
int getSelectedWaypoint() override
getSelectedWaypoint
void highlightTransitionVisualization(int index, std::vector< PoseBasePtr > transition) override
highlightTransitionVisualization highlights the transition with index i by changing the color of all ...
void setManipulator(VirtualRobot::RobotNodeSetPtr kc, std::vector< float > jointAngles) override
SETTING MANIPULATOR.
void removeAllWaypointVisualizations() override
removeAllWaypointVisualizations removes all waypoints that are currently visualized
void disableVisualization() override
disableVisualization hides the viewer
void setCamera(const Eigen::VectorXf position, const Eigen::VectorXf pointAtA, const Eigen::VectorXf pointAtB) override
setCamera sets the camera at a certain position
The Pose class.
Definition Pose.h:243
virtual Eigen::Matrix4f toEigen() const
Definition Pose.cpp:334
VirtualRobot::RobotPtr robot
VirtualRobot::ScenePtr scene
std::weak_ptr< VisualizationObserver > observer
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
double s(double t, double s0, double v0, double a0, double j)
Definition CtrlUtil.h:33
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< AdvancedCoinVisualizationFactory > AdvancedCoinVisualizationFactoryPtr
std::shared_ptr< AdvancedVisualizationFactory > AdvancedVisualizationFactoryPtr
boost::shared_ptr< VirtualRobot::CoinVisualization > CoinVisualizationPtr
std::shared_ptr< RobotVisualization > RobotVisualizationPtr
constexpr auto n() noexcept