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>
35 #include <Inventor/nodes/SoUnits.h>
46 ArmarXPhysicsWorldVisualization::ArmarXPhysicsWorldVisualization()
49 coinVisu =
new SoSeparator;
52 sceneViewableSep =
new SoSeparator;
53 coinVisu->addChild(sceneViewableSep);
55 dynamicsVisu =
new SoSeparator;
57 sceneViewableSep->addChild(dynamicsVisu);
58 baseCoordVisu =
new SoSeparator;
62 floorVisu =
new SoSeparator;
63 this->lastSceneData.floor =
false;
64 coinVisu->addChild(floorVisu);
66 modelType = VirtualRobot::SceneObject::Full;
69 mutex.reset(
new std::recursive_mutex());
70 mutexData.reset(
new std::recursive_mutex());
73 ArmarXPhysicsWorldVisualization::~ArmarXPhysicsWorldVisualization()
90 ArmarXPhysicsWorldVisualization::releaseResources()
93 auto l = getScopedLock();
95 addedVisualizations.clear();
96 addedRobotVisualizations.clear();
99 safeUnref(dynamicsVisu);
100 safeUnref(baseCoordVisu);
104 ArmarXPhysicsWorldVisualization::onInitComponent()
107 std::string top = getProperty<std::string>(
"ReportVisuTopicName").getValue();
113 ArmarXPhysicsWorldVisualization::onConnectComponent()
118 ArmarXPhysicsWorldVisualization::onDisconnectComponent()
123 ArmarXPhysicsWorldVisualization::onExitComponent()
128 ArmarXPhysicsWorldVisualization::reportSceneUpdated(
const SceneVisuData& actualData,
132 auto l = getScopedDataLock();
133 this->currentSceneData = actualData;
137 ArmarXPhysicsWorldVisualization::getVisualization()
143 ArmarXPhysicsWorldVisualization::getViewableScene()
145 return sceneViewableSep;
149 ArmarXPhysicsWorldVisualization::buildVisu(
151 VirtualRobot::SceneObject::VisualizationType visuType)
154 auto l = getScopedLock();
155 SoNode* n = CoinVisualizationFactory::getCoinVisualization(o, visuType);
162 ArmarXPhysicsWorldVisualization::buildVisu(
164 VirtualRobot::SceneObject::VisualizationType visuType)
167 auto l = getScopedLock();
169 std::vector<RobotNodePtr> collectedRobotNodes = ro->getRobotNodes();
170 ARMARX_DEBUG <<
"Robot visu with " << collectedRobotNodes.size() <<
" nodes";
171 SoSeparator* n =
new SoSeparator();
173 for (
const RobotNodePtr& node : collectedRobotNodes)
176 VisualizationNodePtr visu = node->getVisualization(visuType);
177 auto coinVisualizationNode = std::dynamic_pointer_cast<CoinVisualizationNode>(visu);
178 if (coinVisualizationNode)
181 if (coinVisualizationNode->getCoinVisualization())
185 SoNode* no = coinVisualizationNode->getCoinVisualization();
186 SoSeparator* sep =
dynamic_cast<SoSeparator*
>(no);
189 ARMARX_DEBUG <<
"ADDING SEP count " << sep->getNumChildren();
196 ARMARX_DEBUG <<
"Node " << node->getName() <<
" doesn't have a coin visu model";
201 ARMARX_DEBUG <<
"Node " << node->getName() <<
" doesn't have a visu model";
210 ArmarXPhysicsWorldVisualization::addVisualization(
212 VirtualRobot::SceneObject::VisualizationType visuType)
215 auto l = getScopedLock();
218 removeVisualization(ro);
220 if (SoNode* n = buildVisu(ro, visuType))
223 dynamicsVisu->addChild(n);
224 addedRobotVisualizations[ro] = n;
229 ro->setThreadsafe(
false);
233 ArmarXPhysicsWorldVisualization::addFloorVisualization(Eigen::Vector3f floorPos,
234 Eigen::Vector3f floorUp,
236 std::string floorTexture)
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);
247 floorVisu->addChild(n);
252 ArmarXPhysicsWorldVisualization::removeFloorVisualization()
255 auto l = getScopedLock();
256 floorVisu->removeAllChildren();
260 ArmarXPhysicsWorldVisualization::showBaseCoord(
bool enable,
float scale)
263 auto l = getScopedLock();
265 if (enable && sceneViewableSep->findChild(baseCoordVisu) < 0)
268 baseCoordVisu->removeAllChildren();
269 std::string
str(
"Root");
270 baseCoordVisu->addChild(
271 VirtualRobot::CoinVisualizationFactory::CreateCoordSystemVisualization(scale,
273 sceneViewableSep->addChild(baseCoordVisu);
276 if (!enable && sceneViewableSep->findChild(baseCoordVisu) >= 0)
279 sceneViewableSep->removeChild(baseCoordVisu);
284 ArmarXPhysicsWorldVisualization::addVisualization(
286 VirtualRobot::SceneObject::VisualizationType visuType)
291 auto l = getScopedLock();
292 removeVisualization(
so);
293 SoNode* n = buildVisu(
so, visuType);
294 ARMARX_DEBUG <<
"addVisualization " <<
so->getName() <<
" Ref: " << n->getRefCount();
298 dynamicsVisu->addChild(n);
299 addedVisualizations[
so] = n;
301 ARMARX_DEBUG <<
"addVisualization " <<
so->getName() <<
" Ref: " << n->getRefCount();
309 auto l = getScopedLock();
313 if (addedVisualizations.find(
so) != addedVisualizations.end())
316 SoNode* node = addedVisualizations[
so];
318 <<
", Ref: " << node->getRefCount();
319 addedVisualizations.erase(
so);
320 dynamicsVisu->removeChild(node);
325 ArmarXPhysicsWorldVisualization::removeObjectVisualization(
const std::string& name)
328 auto l = getScopedLock();
332 auto copiedVisus = addedVisualizations;
333 for (
auto& o : copiedVisus)
337 if (
so->getName() == name)
340 removeVisualization(
so);
345 ARMARX_ERROR <<
"No object with name " << name <<
" found";
349 ArmarXPhysicsWorldVisualization::removeRobotVisualization(
const std::string& name)
352 auto l = getScopedLock();
356 for (
auto& r : addedRobotVisualizations)
359 if (r.first->getName() == name)
362 removeVisualization(r.first);
367 ARMARX_ERROR <<
"No robot with name " << name <<
" found";
371 ArmarXPhysicsWorldVisualization::removeVisualization(
RobotPtr r)
374 auto l = getScopedLock();
378 if (addedRobotVisualizations.find(r) != addedRobotVisualizations.end())
381 ARMARX_DEBUG <<
"found added robot, removing " << r->getName();
382 SoNode* node = addedRobotVisualizations[r];
383 dynamicsVisu->removeChild(node);
384 addedRobotVisualizations.erase(r);
391 ArmarXPhysicsWorldVisualization::enableVisuType(
bool fullModel)
394 auto l = getScopedLock();
397 if (fullModel && modelType == VirtualRobot::SceneObject::Full)
402 if (!fullModel && modelType == VirtualRobot::SceneObject::Collision)
410 modelType = VirtualRobot::SceneObject::Full;
414 modelType = VirtualRobot::SceneObject::Collision;
426 dynamicsVisu->removeAllChildren();
429 for (
const auto& pair : addedRobotVisualizations)
433 SoNode* n = buildVisu(ro, modelType);
437 dynamicsVisu->addChild(n);
438 addedRobotVisualizations[ro] = n;
443 ARMARX_DEBUG <<
"Robot " << ro->getName() <<
" has no visualization for model type "
450 std::map<VirtualRobot::SceneObjectPtr, SoNode*>::iterator it2 = addedVisualizations.begin();
452 for (; it2 != addedVisualizations.end(); it2++)
456 SoNode* n = buildVisu(
so, modelType);
460 dynamicsVisu->addChild(n);
461 addedVisualizations[
so] = n;
469 ArmarXPhysicsWorldVisualization::setMutex(std::shared_ptr<std::recursive_mutex>
const& m)
495 ArmarXPhysicsWorldVisualization::synchronizeSceneObjectPoses(NamePoseMap& objMap,
508 if (objMap.find(currentObjVisu->getName()) == objMap.end())
510 ARMARX_ERROR <<
"No object with name " << currentObjVisu->getName() <<
"in objMap";
514 ARMARX_IMPORTANT <<
"object " << currentObjVisu->getName() <<
":\n" << objMap[currentObjVisu->getName()] ;
519 if (objMap.find(currentObjVisu->getName()) != objMap.end())
522 VirtualRobot::RobotNodePtr rnv =
523 std::dynamic_pointer_cast<VirtualRobot::RobotNode>(currentObjVisu);
524 PosePtr p = PosePtr::dynamicCast(objMap[currentObjVisu->getName()]);
531 RobotNodeActuatorPtr rna(
new RobotNodeActuator(rnv));
532 rna->updateVisualizationPose(gp, jvMap[currentObjVisu->getName()],
false);
537 currentObjVisu->setGlobalPoseNoChecks(gp);
541 std::vector<SceneObjectPtr> childrenV = currentObjVisu->getChildren();
543 for (
const auto& i : childrenV)
546 if (!synchronizeSceneObjectPoses(objMap, jvMap, i))
557 ArmarXPhysicsWorldVisualization::synchronizeFloor()
560 if (lastSceneData.floor == currentSceneData.floor &&
561 lastSceneData.floorTextureFile == currentSceneData.floorTextureFile)
566 if (currentSceneData.floor)
569 VirtualRobot::MathTools::Plane p = VirtualRobot::MathTools::getFloorPlane();
570 addFloorVisualization(p.p, p.n, 50000.0f, currentSceneData.floorTextureFile);
574 removeFloorVisualization();
581 ArmarXPhysicsWorldVisualization::synchronizeRobots()
584 for (
auto& robotDef : currentSceneData.robots)
587 if (!robotDef.updated)
593 bool robotLoaded =
false;
595 for (
auto& currentRob : lastSceneData.robots)
598 if (robotDef.name == currentRob.name)
610 if (!loadRobot(robotDef.name,
626 ARMARX_ERROR <<
"internal error, no robot with name " << robotDef.name;
628 for (
auto& r : addedRobotVisualizations)
630 ARMARX_ERROR <<
"Available Robot:" << r.first->getName();
639 if (!synchronizeSceneObjectPoses(
640 robotDef.robotNodePoses, robotDef.jointValues, currentObjVisu))
648 const bool updateChildren =
true;
649 currentObjVisu->updatePose(updateChildren);
652 robotDef.updated =
false;
657 for (
auto& currentRob : lastSceneData.robots)
660 bool robFound =
false;
662 for (
auto& robotDef : currentSceneData.robots)
664 if (robotDef.name == currentRob.name)
674 ARMARX_INFO <<
"Removing robot:" << currentRob.name;
675 removeRobotVisualization(currentRob.name);
683 ArmarXPhysicsWorldVisualization::synchronizeObjects()
686 for (
auto& objectDef : currentSceneData.objects)
689 if (!objectDef.updated)
695 bool objectLoaded =
false;
697 for (
auto& currentObj : lastSceneData.objects)
699 if (objectDef.name == currentObj.name && getObject(objectDef.name))
711 if (!loadObject(objectDef))
714 << objectDef.name <<
" failed"
715 <<
"\n " <<
VAROUT(objectDef.name) <<
"\n "
716 <<
VAROUT(objectDef.objectClassName) <<
"\n "
717 <<
VAROUT(objectDef.filename) <<
"\n "
718 <<
VAROUT(objectDef.project);
727 ARMARX_ERROR <<
"internal error, no object for " << objectDef.name;
733 if (!synchronizeSceneObjectPoses(objectDef.objectPoses, tmpMap, ob))
741 objectDef.updated =
false;
746 for (
auto& currentObj : lastSceneData.objects)
749 bool objFound =
false;
751 for (
auto& objectDef : currentSceneData.objects)
753 if (objectDef.name == currentObj.name)
763 ARMARX_INFO <<
"Removing object:" << currentObj.name;
764 removeObjectVisualization(currentObj.name);
772 ArmarXPhysicsWorldVisualization::synchronizeVisualizationData()
777 auto lockVisu = getScopedLock();
778 auto lockData = getScopedDataLock();
780 IceUtil::Time durationlock = IceUtil::Time::now() - startTime;
782 if (durationlock.toMilliSecondsDouble() > 10)
785 <<
"Copy and Lock took long: " << durationlock.toMilliSecondsDouble()
792 auto startTime1 = IceUtil::Time::now();
797 if (duration1.toMilliSecondsDouble() > 10)
800 <<
" floorSync took long: " << duration1.toMilliSecondsDouble() <<
" ms";
804 auto startTime2 = IceUtil::Time::now();
809 if (duration2.toMilliSecondsDouble() > 10)
812 <<
" RobotSync took long: " << duration2.toMilliSecondsDouble() <<
" ms";
816 auto startTime3 = IceUtil::Time::now();
818 synchronizeObjects();
821 if (duration3.toMilliSecondsDouble() > 10)
824 <<
" ObjectSync took long: " << duration3.toMilliSecondsDouble() <<
" ms";
828 this->lastSceneData = this->currentSceneData;
831 synchronize2TimeMS = (
float)duration.toMilliSecondsDouble();
838 ArmarXPhysicsWorldVisualization::getSyncVisuTime()
840 return synchronize2TimeMS;
844 ArmarXPhysicsWorldVisualization::loadObject(
const ObjectVisuData& obj)
849 if (not(obj.filename.empty() or obj.project.empty()))
852 return loadObject(obj.filename, obj.project, obj.name);
861 return createPrimitiveObject(obj.objectPrimitiveData, obj.name);
865 ArmarXPhysicsWorldVisualization::loadRobot(
const std::string& robotName,
866 const std::string& robotFile,
872 if (robotFile.empty())
897 std::string fn = robotFile;
898 ArmarXDataPath::getAbsolutePath(fn, fn);
909 loadMode = VirtualRobot::RobotIO::eCollisionModel;
911 result = RobotIO::loadRobot(fn, loadMode);
923 ARMARX_INFO <<
"Scaling robot, factor: " << scaling;
924 result = result->clone(result->getName(), result->getCollisionChecker(), scaling);
930 result->setName(robotName);
931 result->setPropagatingJointValuesEnabled(
true);
932 ARMARX_DEBUG <<
"adding newly loaded robot to visualization";
933 VirtualRobot::SceneObject::VisualizationType visuMode = modelType;
937 visuMode = VirtualRobot::SceneObject::Collision;
939 addVisualization(result, visuMode);
945 ArmarXPhysicsWorldVisualization::loadObject(
const std::string& objectFile,
947 const std::string& instanceName)
950 if (objectFile.empty())
975 std::string fn = objectFile;
976 ArmarXDataPath::getAbsolutePath(fn, fn);
985 o = ObjectIO::loadManipulationObject(fn);
987 catch (VirtualRobotException& e)
989 ARMARX_ERROR <<
" ERROR while creating manip (file:" << fn <<
")\n" << e.what();
998 o = ObjectIO::loadObstacle(fn);
1000 catch (VirtualRobotException& e)
1008 ARMARX_ERROR <<
" ERROR while creating (file:" << fn <<
")";
1012 o->setName(instanceName);
1013 addVisualization(o, modelType);
1018 ArmarXPhysicsWorldVisualization::getObject(
const std::string& name)
1021 for (
auto& [
object, visu] : addedVisualizations)
1023 if (object->getName() == name)
1031 std::vector<RobotPtr>
1032 ArmarXPhysicsWorldVisualization::getRobots()
const
1035 std::vector<RobotPtr> result;
1036 result.reserve(addedRobotVisualizations.size());
1037 for (
auto& r : addedRobotVisualizations)
1039 result.push_back(r.first);
1044 std::vector<SceneObjectPtr>
1045 ArmarXPhysicsWorldVisualization::getObjects()
const
1048 std::vector<SceneObjectPtr> result;
1049 result.reserve(addedVisualizations.size());
1050 for (
auto& r : addedVisualizations)
1052 result.push_back(r.first);
1058 ArmarXPhysicsWorldVisualization::getSyncTimestamp()
const
1061 return currentSceneData.timestamp * 1000;
1065 ArmarXPhysicsWorldVisualization::createPrimitiveObject(
1066 ObjectVisuPrimitivePtr objectPrimitiveData,
1067 const std::string& name)
1070 if (!objectPrimitiveData)
1076 VirtualRobot::ObstaclePtr o;
1079 switch (objectPrimitiveData->type)
1084 BoxVisuPrimitivePtr box = BoxVisuPrimitivePtr::dynamicCast(objectPrimitiveData);
1096 c.transparency = box->color.a;
1097 o = VirtualRobot::Obstacle::createBox(box->width, box->height, box->depth,
c);
1098 o->setMass(box->massKG);
1105 SphereVisuPrimitivePtr
s = SphereVisuPrimitivePtr::dynamicCast(objectPrimitiveData);
1117 c.transparency =
s->color.a;
1118 o = VirtualRobot::Obstacle::createSphere(
s->radius,
c);
1119 o->setMass(
s->massKG);
1126 CylinderVisuPrimitivePtr
s =
1127 CylinderVisuPrimitivePtr::dynamicCast(objectPrimitiveData);
1139 c.transparency =
s->color.a;
1140 o = VirtualRobot::Obstacle::createCylinder(
s->radius,
s->length,
c);
1141 o->setMass(
s->massKG);
1158 addVisualization(o, modelType);
1163 ArmarXPhysicsWorldVisualization::getRobot(
const std::string& name)
1166 for (
auto& r : addedRobotVisualizations)
1168 if (r.first->getName() == name)