36 agentAt(PredicateInfo {
"agentAt", 2})
46 distanceThreshold = getProperty<float>(
"DistanceThreshold").getValue();
47 humanDistanceThreshold = getProperty<float>(
"HumanDistanceThreshold").getValue();
48 sceneName = getProperty<std::string>(
"PlatformGraphSceneName").getValue();
53 wm = getProxy<WorkingMemoryInterfacePrx>(
"WorkingMemory");
54 prior = getProxy<PriorKnowledgeInterfacePrx>(
"PriorKnowledge");
55 psr = getProxy<GraphNodePoseResolverInterfacePrx>(
"GraphNodePoseResolver");
57 agentInstances = wm->getAgentInstancesSegment();
58 graphSegment = prior->getGraphSegment();
60 graphNodes = graphSegment->getNodesByScene(sceneName);
65 return "AgentAtPredicateProvider";
75 PredicateInstanceList result;
76 const std::string predicateName = agentAt.name;
78 for (
const auto& agentEntity : agentInstances->getAllEntities())
80 AgentInstanceBasePtr agent = AgentInstanceBasePtr::dynamicCast(agentEntity);
81 EntityRefBasePtr agentRef = wm->getAgentInstancesSegment()->getEntityRefById(agentEntity->getId());
83 GraphNodeBasePtr closestNode = psr->getNearestNodeToPose(sceneName,
"robotlocation", agent->getPoseBase());
85 const auto agentPoseEigen = armarx::FramedPosePtr::dynamicCast(agent->getPoseBase())->toEigen();
86 auto closestPoseEigen = armarx::FramedPosePtr::dynamicCast(closestNode->getPose())->toEigen();
87 auto dist = (closestPoseEigen.block<2, 1>(0, 3) - agentPoseEigen.block<2, 1>(0, 3)).
norm();
89 ARMARX_INFO <<
"for agent '" << agent->getName() <<
"', closest node: '" << closestNode->getName() <<
"', dist: " << dist;
90 float selectedDistanceThreshold =
armarx::Contains(agent->getName(),
"human",
true) ? humanDistanceThreshold : distanceThreshold;
91 if (dist <= selectedDistanceThreshold)
93 const CachedNodeInfo& cacheEntry = getCacheEntry(closestNode->getId());
95 for (
const auto& nodeRef : cacheEntry.agentAtNodeRefs)
97 ARMARX_DEBUG <<
"agentAt(" << agent->getName() <<
", " << nodeRef->entityName <<
")";
98 result.push_back(PredicateInstance {predicateName, {agentRef, nodeRef},
true});
106 AgentAtPredicateProvider::CachedNodeInfo AgentAtPredicateProvider::getCacheEntry(
const std::string& nodeId)
108 auto it = nodeInfoCache.find(nodeId);
109 if (it != nodeInfoCache.end())
114 CachedNodeInfo newEntry {nodeId, {}};
115 std::vector<std::pair<EntityRefBasePtr, bool>> agentAtCandidates;
117 bool allRobotLocations =
true;
118 for (
const GraphNodeBasePtr& nodeBase : graphNodes)
120 GraphNodePtr node = GraphNodePtr::dynamicCast(nodeBase);
121 if (node->isMetaEntity())
126 auto it = nodeParentsCache.find(node->getName());
127 if (it == nodeParentsCache.end())
129 it = nodeParentsCache.insert({node->getName(), node->getAllParentsAsStringList()}).first;
132 auto parents = it->second;
134 auto n = psr->getNearestRobotLocationNode(node);
135 ARMARX_DEBUG <<
"to '" << node->getName() <<
"', closest robot location is '" << n->getName() <<
"'";
136 if (n->getId() == nodeId)
139 bool isRobotLocation = std::find(parents.cbegin(), parents.cend(),
"robotlocation") != parents.cend();
140 allRobotLocations &= isRobotLocation;
141 ARMARX_DEBUG <<
"for query node '" << graphSegment->getEntityRefById(nodeId)->entityName <<
"', considering node '" << node->getName() <<
"'";
142 agentAtCandidates.push_back({graphSegment->getEntityRefById(node->getId()), isRobotLocation});
146 if (!agentAtCandidates.empty())
148 if (allRobotLocations)
150 ARMARX_DEBUG <<
"for query '" << nodeId <<
"', result is '" << agentAtCandidates.front().first->entityName <<
"'";
151 newEntry.agentAtNodeRefs.push_back(agentAtCandidates.front().first);
155 for (
const auto& entry : agentAtCandidates)
159 ARMARX_DEBUG <<
"for query '" << nodeId <<
"', result is '" << entry.first->entityName <<
"'";
160 newEntry.agentAtNodeRefs.push_back(entry.first);
167 nodeInfoCache.insert({nodeId, newEntry});