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