31 #include <VirtualRobot/Grasping/GraspSet.h>
33 #include <SimoxUtility/algorithm/string/string_tools.h>
38 leftColor(
armarx::DrawColor {0.f, 0.f, 1.f, 0.25f}),
39 rightColor(armarx::DrawColor {0.f, 1.f, 0.f, 0.25f}),
40 leftGraspable(PredicateInfo {
"leftgraspable", 1}),
41 rightGraspable(PredicateInfo {
"rightgraspable", 1})
45 std::vector<Box> GraspablePredicateProvider::stringToBoxes(
const std::string& boxesString)
47 std::vector<Box> result;
50 for (
const auto& boxTupleString : boxTuples)
57 std::stof(boxTuple[0]),
58 std::stof(boxTuple[1]),
59 std::stof(boxTuple[2]),
60 std::stof(boxTuple[3]),
61 std::stof(boxTuple[4]),
62 std::stof(boxTuple[5])
71 boxesL = stringToBoxes(getProperty<std::string>(
"BoundingBoxesL").getValue());
72 boxesR = stringToBoxes(getProperty<std::string>(
"BoundingBoxesR").getValue());
77 debugDrawerTopicName = getProperty<std::string>(
"DebugDrawerTopicName").getValue();
79 if (!debugDrawerTopicName.empty())
87 wm = getProxy<WorkingMemoryInterfacePrx>(
"WorkingMemory");
88 objectInstances = wm->getObjectInstancesSegment();
89 prior = getProxy<PriorKnowledgeInterfacePrx>(
"PriorKnowledge");
90 objectClasses = prior->getObjectClassesSegment();
93 if (!debugDrawerTopicName.empty())
97 for (
size_t i = 0; i < boxesL.size(); ++i)
102 for (
size_t i = 0; i < boxesR.size(); ++i)
111 return "GraspablePredicateProvider";
116 return {leftGraspable, rightGraspable};
119 void GraspablePredicateProvider::addPredicateIfInside(PredicateInstanceList& result,
const std::string& predicateName,
const ObjectInstancePtr& obj,
const std::vector<Box>& boxes)
122 auto pos = obj->getPosition();
126 pos = armarx::FramedPosePtr::dynamicCast(wm->getAgentInstancesSegment()->convertToWorldPose(pos->agent, obj->getPose()))->getPosition();
129 ARMARX_DEBUG <<
"Obj: " << obj->getName() <<
" pos: " << *pos;
131 for (
const auto& box : boxes)
133 inside |= box.inside(pos);
139 result.push_back(PredicateInstance {predicateName, {objectInstances->getEntityRefById(obj->getId())},
true});
145 PredicateInstanceList result;
147 for (
const auto& entity : objectInstances->getAllEntities())
150 const std::string className =
object->getMostProbableClass();
152 auto graspabilityDescriptorIt = std::find_if(graspabilityDescriptors.cbegin(), graspabilityDescriptors.cend(), [&](
const GraspabilityDescriptor & gd)
154 return gd.className == className;
157 if (graspabilityDescriptorIt == graspabilityDescriptors.cend())
159 auto parents = objectClasses->getObjectClassByNameWithAllParents(className)->getParentClasses();
161 bool hasGraspableSuperclass = std::find(parents.cbegin(), parents.cend(),
"graspable") != parents.cend();
162 hasGraspableSuperclass |= std::find(parents.cbegin(), parents.cend(),
"bothhandsgraspable") != parents.cend();
163 GraspabilityDescriptor gd {className, hasGraspableSuperclass,
false,
false};
165 ObjectClassPtr objectClass = ObjectClassPtr::dynamicCast(objectClasses->getEntityByName(className));
167 VirtualRobot::ManipulationObjectPtr mo = simoxWrapper->getManipulationObject();
169 gd.hasLeftGrasp =
false;
170 gd.hasRightGrasp =
false;
173 auto hasGrasp = [](VirtualRobot::GraspSetPtr
set)
180 for (
size_t i = 0; i <
set->getSize(); ++i)
189 gd.hasLeftGrasp = hasGrasp(mo->getGraspSet(
"TCP L"));
190 gd.hasRightGrasp = hasGrasp(mo->getGraspSet(
"TCP R"));
193 graspabilityDescriptors.push_back(gd);
194 graspabilityDescriptorIt = std::prev(graspabilityDescriptors.cend());
196 ARMARX_INFO <<
"new graspability descriptor: [className: '" << className
197 <<
"'; child of graspable/bothhandsgraspable: " << gd.hasGraspableSuperclass
198 <<
"; has left grasp: " << gd.hasLeftGrasp
199 <<
"; has right grasp: " << gd.hasRightGrasp <<
"]";
202 const auto& gd = *graspabilityDescriptorIt;
203 ARMARX_DEBUG <<
"checking " << className <<
"/" << gd.className <<
" " << gd.hasGraspableSuperclass <<
" " << gd.hasLeftGrasp <<
" " << gd.hasRightGrasp;
205 if (!gd.hasGraspableSuperclass)
212 addPredicateIfInside(result, leftGraspable.name,
object, boxesL);
215 if (gd.hasRightGrasp)
217 addPredicateIfInside(result, rightGraspable.name,
object, boxesR);