ArmarXPhysicsWorldVisualization.cpp
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
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
26
27#include <VirtualRobot/ManipulationObject.h> // IWYU prgma: keep
28#include <VirtualRobot/MathTools.h>
29#include <VirtualRobot/Nodes/RobotNodeActuator.h>
30#include <VirtualRobot/Obstacle.h>
31#include <VirtualRobot/VirtualRobotException.h>
32#include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
33#include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationNode.h>
34#include <VirtualRobot/XML/ObjectIO.h>
35
39
40#include <Inventor/nodes/SoUnits.h>
44
45using namespace VirtualRobot;
46using namespace memoryx;
47
48namespace armarx
49{
50
52 {
53 //enableContactVisu = false;
54
55 // ArmarXPhysicsWorldVisualization_run__register_component_executable__test currently broken here: likely internal coin issue
56 coinVisu = new SoSeparator;
57 coinVisu->ref();
58
59 sceneViewableSep = new SoSeparator;
60 coinVisu->addChild(sceneViewableSep);
61
62 dynamicsVisu = new SoSeparator;
63 dynamicsVisu->ref();
65 baseCoordVisu = new SoSeparator;
66 baseCoordVisu->ref();
67
68 // floor is not part of the viewable visu (allows better viewAll handling)
69 floorVisu = new SoSeparator;
70 this->lastSceneData.floor = false;
71 coinVisu->addChild(floorVisu);
72
73 modelType = VirtualRobot::SceneObject::Full;
74
75 // initially set mutex
76 mutex.reset(new std::recursive_mutex());
77 mutexData.reset(new std::recursive_mutex());
78 }
79
84
85 std::string
87 {
88 return "ArmarXPhysicsWorldVisualization";
89 }
90
91 template <typename T>
92 static void
93 safeUnref(T*& p)
94 {
95 if (p)
96 {
97 p->unref();
98 p = nullptr;
99 }
100 }
101
102 void
104 {
106 auto l = getScopedLock();
107
108 addedVisualizations.clear();
110
111 safeUnref(coinVisu);
112 safeUnref(dynamicsVisu);
113 safeUnref(baseCoordVisu);
114 }
115
116 void
118 {
120 std::string top = getProperty<std::string>("ReportVisuTopicName").getValue();
121 ARMARX_VERBOSE << "Using topic " << top;
122 usingTopic(top);
123 }
124
125 void
129
130 void
134
135 void
139
140 void
142 const Ice::Current&)
143 {
145 auto l = getScopedDataLock();
146 this->currentSceneData = actualData;
147 }
148
149 SoSeparator*
154
155 SoSeparator*
160
161 SoNode*
163 SceneObjectPtr o,
164 VirtualRobot::SceneObject::VisualizationType visuType)
165 {
167 auto l = getScopedLock();
168 SoNode* n = CoinVisualizationFactory::getCoinVisualization(o, visuType);
169 n->ref();
170 return n;
171 }
172
173 SoNode*
175 RobotPtr ro,
176 VirtualRobot::SceneObject::VisualizationType visuType)
177 {
179 auto l = getScopedLock();
180 ARMARX_DEBUG << "buildVisu type: " << visuType;
181 std::vector<RobotNodePtr> collectedRobotNodes = ro->getRobotNodes();
182 ARMARX_DEBUG << "Robot visu with " << collectedRobotNodes.size() << " nodes";
183 SoSeparator* n = new SoSeparator();
184 n->ref();
185 for (const RobotNodePtr& node : collectedRobotNodes)
186 {
188 VisualizationNodePtr visu = node->getVisualization(visuType);
189 auto coinVisualizationNode = std::dynamic_pointer_cast<CoinVisualizationNode>(visu);
190 if (coinVisualizationNode)
191 {
193 if (coinVisualizationNode->getCoinVisualization())
194 {
196 ARMARX_DEBUG << "ADDING VISU " << node->getName();
197 SoNode* no = coinVisualizationNode->getCoinVisualization();
198 SoSeparator* sep = dynamic_cast<SoSeparator*>(no);
199 if (sep)
200 {
201 ARMARX_DEBUG << "ADDING SEP count " << sep->getNumChildren();
202 }
203
204 n->addChild(no);
205 }
206 else
207 {
208 ARMARX_DEBUG << "Node " << node->getName() << " doesn't have a coin visu model";
209 }
210 }
211 else
212 {
213 ARMARX_DEBUG << "Node " << node->getName() << " doesn't have a visu model";
214 }
215 }
216
217 ARMARX_DEBUG << "buildVisu end";
218 return n;
219 }
220
221 void
223 RobotPtr ro,
224 VirtualRobot::SceneObject::VisualizationType visuType)
225 {
227 auto l = getScopedLock();
228
229 VR_ASSERT(ro);
231
232 if (SoNode* n = buildVisu(ro, visuType))
233 {
235 dynamicsVisu->addChild(n);
237 n->unref();
238 }
239
240 // we have our own mutex protection
241 ro->setThreadsafe(false);
242 }
243
244 void
246 Eigen::Vector3f floorUp,
247 float floorExtendMM,
248 std::string floorTexture)
249 {
251 ARMARX_INFO << "Adding floor visu, texture file:" << floorTexture;
252 auto l = getScopedLock();
253 floorVisu->removeAllChildren();
254 SoNode* n = (SoNode*)CoinVisualizationFactory::CreatePlaneVisualization(
255 floorPos,
256 floorUp,
257 floorExtendMM,
258 true,
259 CoinVisualizationFactory::Color::Gray(),
260 floorTexture);
261 if (n)
262 {
264 floorVisu->addChild(n);
265 }
266 }
267
268 void
270 {
272 auto l = getScopedLock();
273 floorVisu->removeAllChildren();
274 }
275
276 void
278 {
280 auto l = getScopedLock();
281
282 if (enable && sceneViewableSep->findChild(baseCoordVisu) < 0)
283 {
285 baseCoordVisu->removeAllChildren();
286 std::string str("Root");
287 baseCoordVisu->addChild(
288 VirtualRobot::CoinVisualizationFactory::CreateCoordSystemVisualization(scale,
289 &str));
291 }
292
293 if (!enable && sceneViewableSep->findChild(baseCoordVisu) >= 0)
294 {
296 sceneViewableSep->removeChild(baseCoordVisu);
297 }
298 }
299
300 void
302 SceneObjectPtr so,
303 VirtualRobot::SceneObject::VisualizationType visuType)
304 {
307
308 auto l = getScopedLock();
310 SoNode* n = buildVisu(so, visuType);
311 ARMARX_DEBUG << "addVisualization " << so->getName() << " Ref: " << n->getRefCount();
312 if (n)
313 {
315 dynamicsVisu->addChild(n);
316 addedVisualizations[so] = n;
317 n->unref();
318 ARMARX_DEBUG << "addVisualization " << so->getName() << " Ref: " << n->getRefCount();
319 }
320 }
321
322 void
324 {
326 auto l = getScopedLock();
327 ARMARX_DEBUG << "removeVisualization" << flush;
328 VR_ASSERT(so);
329
330 if (addedVisualizations.find(so) != addedVisualizations.end())
331 {
333 SoNode* node = addedVisualizations[so];
334 ARMARX_DEBUG << "RemoveVisualization: " << so->getName()
335 << ", Ref: " << node->getRefCount();
336 addedVisualizations.erase(so);
337 dynamicsVisu->removeChild(node);
338 }
339 }
340
341 void
343 {
345 auto l = getScopedLock();
346 ARMARX_DEBUG << "removeVisualization";
347
348 // removeVisualizations erases elements from addedVisualizations so we have to make a copy
349 auto copiedVisus = addedVisualizations;
350 for (auto& o : copiedVisus)
351 {
353 VirtualRobot::SceneObjectPtr const& so = o.first;
354 if (so->getName() == name)
355 {
358 return;
359 }
360 }
361
362 ARMARX_ERROR << "No object with name " << name << " found";
363 }
364
365 void
367 {
369 auto l = getScopedLock();
370 ARMARX_DEBUG << "remove robot visualization:" << name;
372
373 for (auto& r : addedRobotVisualizations)
374 {
376 if (r.first->getName() == name)
377 {
379 removeVisualization(r.first);
380 return;
381 }
382 }
383
384 ARMARX_ERROR << "No robot with name " << name << " found";
385 }
386
387 void
389 {
391 auto l = getScopedLock();
392 ARMARX_DEBUG << "removeVisualization robot";
393 VR_ASSERT(r);
394
396 {
398 ARMARX_DEBUG << "found added robot, removing " << r->getName();
399 SoNode* node = addedRobotVisualizations[r];
400 dynamicsVisu->removeChild(node);
402 }
403 ARMARX_DEBUG << "removeVisualization robot end";
404 }
405
406 void
408 {
410 auto l = getScopedLock();
411 ARMARX_DEBUG << "enableVisuType" << flush;
412
413 if (fullModel && modelType == VirtualRobot::SceneObject::Full)
414 {
415 return;
416 }
417
418 if (!fullModel && modelType == VirtualRobot::SceneObject::Collision)
419 {
420 return;
421 }
423
424 if (fullModel)
425 {
426 modelType = VirtualRobot::SceneObject::Full;
427 }
428 else
429 {
430 modelType = VirtualRobot::SceneObject::Collision;
431 }
432
433 if (fullModel)
434 {
435 ARMARX_INFO << "Switching to visuType full:";
436 }
437 else
438 {
439 ARMARX_INFO << "Switching to visuType colModel:";
440 }
441
442 dynamicsVisu->removeAllChildren();
443
444 // rebuild robots
445 for (const auto& pair : addedRobotVisualizations)
446 {
448 const RobotPtr& ro = pair.first;
449 SoNode* n = buildVisu(ro, modelType);
450
451 if (n)
452 {
453 dynamicsVisu->addChild(n);
455 n->unref();
456 }
457 else
458 {
459 ARMARX_DEBUG << "Robot " << ro->getName() << " has no visualization for model type "
460 << modelType;
461 }
462 }
464
465 // rebuild objects
466 std::map<VirtualRobot::SceneObjectPtr, SoNode*>::iterator it2 = addedVisualizations.begin();
467
468 for (; it2 != addedVisualizations.end(); it2++)
469 {
471 SceneObjectPtr so = it2->first;
472 SoNode* n = buildVisu(so, modelType);
473
474 if (n)
475 {
476 dynamicsVisu->addChild(n);
477 addedVisualizations[so] = n;
478 n->unref();
479 }
480 }
481 }
482
483 void
484 ArmarXPhysicsWorldVisualization::setMutex(std::shared_ptr<std::recursive_mutex> const& m)
485 {
486 mutex = m;
487 }
488
489 auto
491 {
492 RecursiveLockPtr l(new std::unique_lock(*mutex));
493 return l;
494 }
495
496 auto
498 {
499 RecursiveLockPtr l(new std::unique_lock(*mutexData));
500 return l;
501 }
502
503 /*
504 void ArmarXPhysicsWorldVisualization::enableContactVisu(bool enable)
505 {
506 contactVisuEnabled = enable;
507 }*/
508
509
510 bool
512 NameValueMap& jvMap,
513 SceneObjectPtr currentObjVisu)
514 {
516 if (!currentObjVisu)
517 {
518 return false;
519 }
520
521#if 0
522
523 // some sanity checks:
524 if (objMap.find(currentObjVisu->getName()) == objMap.end())
525 {
526 ARMARX_ERROR << "No object with name " << currentObjVisu->getName() << "in objMap";
527 }
528 else
529 {
530 ARMARX_IMPORTANT << "object " << currentObjVisu->getName() << ":\n" << objMap[currentObjVisu->getName()] ;
531 }
532
533#endif
534
535 if (objMap.find(currentObjVisu->getName()) != objMap.end())
536 {
538 VirtualRobot::RobotNodePtr rnv =
539 std::dynamic_pointer_cast<VirtualRobot::RobotNode>(currentObjVisu);
540 PosePtr p = PosePtr::dynamicCast(objMap[currentObjVisu->getName()]);
541 Eigen::Matrix4f gp = p->toEigen();
542
543 if (rnv)
544 {
546 // we can update the joint value via an RobotNodeActuator
547 RobotNodeActuatorPtr rna(new RobotNodeActuator(rnv));
548 rna->updateVisualizationPose(gp, jvMap[currentObjVisu->getName()], false);
549 }
550 else
551 {
553 currentObjVisu->setGlobalPoseNoChecks(gp);
554 }
555 }
557 std::vector<SceneObjectPtr> childrenV = currentObjVisu->getChildren();
558
559 for (const auto& i : childrenV)
560 {
562 if (!synchronizeSceneObjectPoses(objMap, jvMap, i))
563 {
564 return false;
565 }
566 }
567
568 return true;
569 }
570
571 bool
573 {
575 if (lastSceneData.floor == currentSceneData.floor &&
576 lastSceneData.floorTextureFile == currentSceneData.floorTextureFile)
577 {
578 return true;
579 }
580
581 if (currentSceneData.floor)
582 {
584 VirtualRobot::MathTools::Plane p = VirtualRobot::MathTools::getFloorPlane();
585 addFloorVisualization(p.p, p.n, 50000.0f, currentSceneData.floorTextureFile);
586 }
587 else
588 {
590 }
591
592 return true;
593 }
594
595 bool
597 {
599 for (auto& robotDef : currentSceneData.robots)
600 {
602 if (!robotDef.updated)
603 {
604 continue;
605 }
606
607 // check if we need to load the robot
608 bool robotLoaded = false;
609
610 for (auto& currentRob : lastSceneData.robots)
611 {
613 if (robotDef.name == currentRob.name)
614 {
615 robotLoaded = true;
616 break;
617 }
618 }
619
620 if (!robotLoaded)
621 {
623 ARMARX_INFO << deactivateSpam() << "New robot:" << robotDef.name;
624
625 if (!loadRobot(robotDef.name,
626 robotDef.robotFile,
627 robotDef.project,
628 robotDef.scaling,
629 robotDef.colModel))
630 {
631 ARMARX_ERROR << deactivateSpam() << "Loading robot failed";
632 continue;
633 }
634 }
635
637 VirtualRobot::RobotPtr rob = getRobot(robotDef.name);
638
639 if (!rob)
640 {
641 ARMARX_ERROR << "internal error, no robot with name " << robotDef.name;
642
643 for (auto& r : addedRobotVisualizations)
644 {
645 ARMARX_ERROR << "Available Robot:" << r.first->getName();
646 }
647
648 continue;
649 }
651
652 VirtualRobot::SceneObjectPtr currentObjVisu = rob->getRootNode();
653
655 robotDef.robotNodePoses, robotDef.jointValues, currentObjVisu))
656 {
657 ARMARX_ERROR << deactivateSpam() << "Failed to synchronize objects...";
658 continue;
659 }
661
662 // Run forward kinematics, including propagating joint values.
663 const bool updateChildren = true;
664 currentObjVisu->updatePose(updateChildren);
665
666 // never use this data again
667 robotDef.updated = false;
668 }
670
671 // check if a robot is removed
672 for (auto& currentRob : lastSceneData.robots)
673 {
675 bool robFound = false;
676
677 for (auto& robotDef : currentSceneData.robots)
678 {
679 if (robotDef.name == currentRob.name)
680 {
681 robFound = true;
682 break;
683 }
684 }
685
686 if (!robFound)
687 {
689 ARMARX_INFO << "Removing robot:" << currentRob.name;
690 removeRobotVisualization(currentRob.name);
691 }
692 }
693
694 return true;
695 }
696
697 bool
699 {
701 for (auto& objectDef : currentSceneData.objects)
702 {
704 if (!objectDef.updated)
705 {
706 continue;
707 }
708
709 // check if we need to load the robot
710 bool objectLoaded = false;
711
712 for (auto& currentObj : lastSceneData.objects)
713 {
714 if (objectDef.name == currentObj.name && getObject(objectDef.name))
715 {
716 objectLoaded = true;
717 break;
718 }
719 }
721
722 if (!objectLoaded)
723 {
724 ARMARX_INFO << deactivateSpam() << "New Object:" << objectDef.name;
725
726 if (!loadObject(objectDef))
727 {
728 ARMARX_ERROR << deactivateSpam(5, objectDef.name) << "Loading object "
729 << objectDef.name << " failed"
730 << "\n " << VAROUT(objectDef.name) << "\n "
731 << VAROUT(objectDef.objectClassName) << "\n "
732 << VAROUT(objectDef.filename) << "\n "
733 << VAROUT(objectDef.project);
734 continue;
735 }
736 }
738
739 VirtualRobot::SceneObjectPtr ob = getObject(objectDef.name);
740 if (!ob)
741 {
742 ARMARX_ERROR << "internal error, no object for " << objectDef.name;
743 continue;
744 }
745
746 NameValueMap tmpMap;
747
748 if (!synchronizeSceneObjectPoses(objectDef.objectPoses, tmpMap, ob))
749 {
750 ARMARX_ERROR << deactivateSpam() << "Failed to synchronize objects...";
751 continue;
752 }
754
755 // never use this data again
756 objectDef.updated = false;
757 }
759
760 // check if a object is removed
761 for (auto& currentObj : lastSceneData.objects)
762 {
764 bool objFound = false;
765
766 for (auto& objectDef : currentSceneData.objects)
767 {
768 if (objectDef.name == currentObj.name)
769 {
770 objFound = true;
771 break;
772 }
773 }
775
776 if (!objFound)
777 {
778 ARMARX_INFO << "Removing object:" << currentObj.name;
779 removeObjectVisualization(currentObj.name);
780 }
781 }
782
783 return true;
784 }
785
786 bool
788 {
790 IceUtil::Time startTime = IceUtil::Time::now();
791
792 auto lockVisu = getScopedLock();
793 auto lockData = getScopedDataLock();
794
795 IceUtil::Time durationlock = IceUtil::Time::now() - startTime;
796
797 if (durationlock.toMilliSecondsDouble() > 10)
798 {
800 << "Copy and Lock took long: " << durationlock.toMilliSecondsDouble()
801 << " ms";
802 }
804
805
806 // check for updates
807 auto startTime1 = IceUtil::Time::now();
808
810
811 IceUtil::Time duration1 = IceUtil::Time::now() - startTime1;
812 if (duration1.toMilliSecondsDouble() > 10)
813 {
815 << " floorSync took long: " << duration1.toMilliSecondsDouble() << " ms";
816 }
818
819 auto startTime2 = IceUtil::Time::now();
820
822
823 IceUtil::Time duration2 = IceUtil::Time::now() - startTime2;
824 if (duration2.toMilliSecondsDouble() > 10)
825 {
827 << " RobotSync took long: " << duration2.toMilliSecondsDouble() << " ms";
828 }
829
830
831 auto startTime3 = IceUtil::Time::now();
832
834
835 IceUtil::Time duration3 = IceUtil::Time::now() - startTime3;
836 if (duration3.toMilliSecondsDouble() > 10)
837 {
839 << " ObjectSync took long: " << duration3.toMilliSecondsDouble() << " ms";
840 }
842
843 this->lastSceneData = this->currentSceneData;
844
845 IceUtil::Time duration = IceUtil::Time::now() - startTime;
846 synchronize2TimeMS = (float)duration.toMilliSecondsDouble();
847
848 return true;
849 }
850
851 float
856
857 bool
859 {
861
862 // try loading from file
863 if (not(obj.filename.empty() or obj.project.empty()))
864 {
866 return loadObject(obj.filename, obj.project, obj.name);
867 }
869
870 // try loading from memory => nope. outdated
871
873
874 // try to ceate primitive
875 return createPrimitiveObject(obj.objectPrimitiveData, obj.name);
876 }
877
878 bool
879 ArmarXPhysicsWorldVisualization::loadRobot(const std::string& robotName,
880 const std::string& robotFile,
881 const std::string& project,
882 float scaling,
883 bool colModel)
884 {
886 if (robotFile.empty())
887 {
888 ARMARX_INFO << deactivateSpam() << "Empty file";
889 return false;
890 }
891
892 if (!project.empty())
893 {
895 ARMARX_INFO << "Adding to datapaths of " << project;
896 armarx::CMakePackageFinder finder(project);
897
898 if (!finder.packageFound())
899 {
900 ARMARX_ERROR << "ArmarX Package " << project << " has not been found!";
901 }
902 else
903 {
904 ARMARX_INFO << "Adding to datapaths: " << finder.getDataDir();
906 }
907 }
909
910 // load robot
911 std::string fn = robotFile;
913 ARMARX_INFO << "Loading robot from " << fn;
915
916 try
917 {
919 VirtualRobot::RobotIO::RobotDescription loadMode = VirtualRobot::RobotIO::eFull;
920
921 if (colModel)
922 {
923 loadMode = VirtualRobot::RobotIO::eCollisionModel;
924 }
925 result = RobotIO::loadRobot(fn, loadMode);
926 ARMARX_DEBUG << "Loading robot done ";
927 }
928 catch (...)
929 {
930 ARMARX_IMPORTANT << "Robot loading failed, file:" << fn;
931 return false;
932 }
934
935 if (scaling != 1.0f)
936 {
937 ARMARX_INFO << "Scaling robot, factor: " << scaling;
938 result = result->clone(result->getName(), result->getCollisionChecker(), scaling);
939 }
940
941
942 // ensure consistent names
943 ARMARX_DEBUG << "Setting up newly loaded robot";
944 result->setName(robotName);
945 result->setPropagatingJointValuesEnabled(true);
946 ARMARX_DEBUG << "adding newly loaded robot to visualization";
947 VirtualRobot::SceneObject::VisualizationType visuMode = modelType;
948 if (colModel)
949 {
951 visuMode = VirtualRobot::SceneObject::Collision;
952 }
953 addVisualization(result, visuMode);
954 return true;
955 }
956
957 bool
958 ArmarXPhysicsWorldVisualization::loadObject(const std::string& objectFile,
959 const std::string& project,
960 const std::string& instanceName)
961 {
963 if (objectFile.empty())
964 {
965 ARMARX_INFO << deactivateSpam() << "Empty file";
966 return false;
967 }
968
969 if (!project.empty())
970 {
972 ARMARX_INFO << "Adding to datapaths of " << project;
973 armarx::CMakePackageFinder finder(project);
974
975 if (!finder.packageFound())
976 {
977 ARMARX_ERROR << "ArmarX Package " << project << " has not been found!";
978 }
979 else
980 {
981 ARMARX_INFO << "Adding to datapaths: " << finder.getDataDir();
983 }
984 }
986
987 // load object
988 std::string fn = objectFile;
990 ARMARX_INFO << "Loading object from " << fn;
991
992 ObstaclePtr o;
993 if (!o)
994 {
995 try
996 {
998 o = ObjectIO::loadManipulationObject(fn);
999 }
1000 catch (VirtualRobotException& e)
1001 {
1002 ARMARX_ERROR << " ERROR while creating manip (file:" << fn << ")\n" << e.what();
1003 return false;
1004 }
1005 }
1006 if (!o)
1007 {
1008 try
1009 {
1011 o = ObjectIO::loadObstacle(fn);
1012 }
1013 catch (VirtualRobotException& e)
1014 {
1015 //ARMARX_ERROR_S << " ERROR while creating obstacle (file:" << fn << ")" << endl << e.what() ;
1016 //return false;
1017 }
1018 }
1019 if (!o)
1020 {
1021 ARMARX_ERROR << " ERROR while creating (file:" << fn << ")";
1022 return false;
1023 }
1024
1025 o->setName(instanceName);
1027 return true;
1028 }
1029
1030 SceneObjectPtr
1032 {
1034 for (auto& [object, visu] : addedVisualizations)
1035 {
1036 if (object->getName() == name)
1037 {
1038 return object;
1039 }
1040 }
1041 return nullptr;
1042 }
1043
1044 std::vector<RobotPtr>
1046 {
1048 std::vector<RobotPtr> result;
1049 result.reserve(addedRobotVisualizations.size());
1050 for (auto& r : addedRobotVisualizations)
1051 {
1052 result.push_back(r.first);
1053 }
1054 return result;
1055 }
1056
1057 std::vector<SceneObjectPtr>
1059 {
1061 std::vector<SceneObjectPtr> result;
1062 result.reserve(addedVisualizations.size());
1063 for (auto& r : addedVisualizations)
1064 {
1065 result.push_back(r.first);
1066 }
1067 return result;
1068 }
1069
1070 Ice::Long
1072 {
1073 // currentSceneData.timestamp seems to use milliseconds
1074 return currentSceneData.timestamp * 1000;
1075 }
1076
1077 bool
1079 ObjectVisuPrimitivePtr objectPrimitiveData,
1080 const std::string& name)
1081 {
1083 if (!objectPrimitiveData)
1084 {
1085 ARMARX_INFO << deactivateSpam() << "Empty primitive data";
1086 return false;
1087 }
1088
1089 VirtualRobot::ObstaclePtr o;
1090
1091 // create object
1092 switch (objectPrimitiveData->type)
1093 {
1094 case Box:
1095 {
1097 BoxVisuPrimitivePtr box = BoxVisuPrimitivePtr::dynamicCast(objectPrimitiveData);
1098
1099 if (!box)
1100 {
1101 ARMARX_ERROR << "Could not cast object...";
1102 return false;
1103 }
1104
1105 VirtualRobot::VisualizationFactory::Color c;
1106 c.r = box->color.r;
1107 c.g = box->color.g;
1108 c.b = box->color.b;
1109 c.transparency = box->color.a;
1110 o = VirtualRobot::Obstacle::createBox(box->width, box->height, box->depth, c);
1111 o->setMass(box->massKG);
1112 }
1113 break;
1114
1115 case Sphere:
1116 {
1118 SphereVisuPrimitivePtr s = SphereVisuPrimitivePtr::dynamicCast(objectPrimitiveData);
1119
1120 if (!s)
1121 {
1122 ARMARX_ERROR << "Could not cast object...";
1123 return false;
1124 }
1125
1126 VirtualRobot::VisualizationFactory::Color c;
1127 c.r = s->color.r;
1128 c.g = s->color.g;
1129 c.b = s->color.b;
1130 c.transparency = s->color.a;
1131 o = VirtualRobot::Obstacle::createSphere(s->radius, c);
1132 o->setMass(s->massKG);
1133 }
1134 break;
1135
1136 case Cylinder:
1137 {
1139 CylinderVisuPrimitivePtr s =
1140 CylinderVisuPrimitivePtr::dynamicCast(objectPrimitiveData);
1141
1142 if (!s)
1143 {
1144 ARMARX_ERROR << "Could not cast object...";
1145 return false;
1146 }
1147
1148 VirtualRobot::VisualizationFactory::Color c;
1149 c.r = s->color.r;
1150 c.g = s->color.g;
1151 c.b = s->color.b;
1152 c.transparency = s->color.a;
1153 o = VirtualRobot::Obstacle::createCylinder(s->radius, s->length, c);
1154 o->setMass(s->massKG);
1155 }
1156 break;
1157
1158 default:
1159 ARMARX_ERROR << "Object type nyi...";
1160 return false;
1161 }
1163
1164 if (!o)
1165 {
1166 ARMARX_ERROR << " ERROR while creating object";
1167 return false;
1168 }
1169
1170 o->setName(name);
1172 return true;
1173 }
1174
1175 RobotPtr
1177 {
1179 for (auto& r : addedRobotVisualizations)
1180 {
1181 if (r.first->getName() == name)
1182 {
1183 return r.first;
1184 }
1185 }
1186
1187 return RobotPtr();
1188 }
1189
1190
1191} // namespace armarx
1192
#define float
Definition 16_Level.h:22
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
#define VAROUT(x)
constexpr T c
std::string str(const T &t)
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
static void addDataPaths(const std::string &dataPathList)
The ArmarXPhysicsWorldVisualization class organizes the Coin3D visualization of the physics scene....
void onInitComponent() override
Pure virtual hook for the subclass.
bool synchronizeSceneObjectPoses(NamePoseMap &objMap, NameValueMap &jvMap, VirtualRobot::SceneObjectPtr currentObjVisu)
std::map< VirtualRobot::SceneObjectPtr, SoNode * > addedVisualizations
VirtualRobot::RobotPtr getRobot(const std::string &name)
SoSeparator * getVisualization()
getVisu returns the visualization that is synchronized with ArmarXPhysicsWorld
VirtualRobot::SceneObjectPtr getObject(const std::string &name)
std::vector< VirtualRobot::RobotPtr > getRobots() const
std::vector< VirtualRobot::SceneObjectPtr > getObjects() const
void addFloorVisualization(Eigen::Vector3f floorPos, Eigen::Vector3f floorUp, float floorExtendMM, std::string floorTexture)
void onConnectComponent() override
Pure virtual hook for the subclass.
std::shared_ptr< std::recursive_mutex > mutex
mutex to protect access to visualization models
void addVisualization(VirtualRobot::SceneObjectPtr so, VirtualRobot::SceneObject::VisualizationType visuType=VirtualRobot::SceneObject::Full)
std::shared_ptr< std::unique_lock< std::recursive_mutex > > RecursiveLockPtr
std::shared_ptr< std::recursive_mutex > mutexData
mutex to process SceneData
SoSeparator * getViewableScene()
Returns the scene without floor (allows for better viewAll handling)
void setMutex(std::shared_ptr< std::recursive_mutex > const &m)
void reportSceneUpdated(const ::armarx::SceneVisuData &actualData, const ::Ice::Current &=Ice::emptyCurrent) override
bool loadRobot(const std::string &robotName, const std::string &robotFile, const std::string &project, float scaling, bool colModel)
bool createPrimitiveObject(ObjectVisuPrimitivePtr objectPrimitiveData, const std::string &name)
std::map< VirtualRobot::RobotPtr, SoNode * > addedRobotVisualizations
SoNode * buildVisu(VirtualRobot::SceneObjectPtr o, VirtualRobot::SceneObject::VisualizationType visuType=VirtualRobot::SceneObject::Full)
buildVisu Creates visualization with increased ref counter.
VirtualRobot::SceneObject::VisualizationType modelType
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
bool packageFound() const
Returns whether or not this package was found with cmake.
Property< PropertyType > getProperty(const std::string &name)
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition Logging.cpp:99
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< Pose > PosePtr
Definition Pose.h:306
const LogSender::manipulator flush
Definition LogSender.h:251
VirtualRobot headers.
#define ARMARX_TRACE
Definition trace.h:77