25 #include <VirtualRobot/CollisionDetection/CollisionChecker.h>
26 #include <VirtualRobot/CollisionDetection/CollisionCheckerImplementation.h>
27 #include <VirtualRobot/CollisionDetection/CollisionModel.h>
28 #include <VirtualRobot/ManipulationObject.h>
29 #include <VirtualRobot/Visualization/TriMeshModel.h>
38 #include <SemanticObjectRelations/Shapes/json/ShapeSerializers.h>
39 #include <SemanticObjectRelations/SupportAnalysis/json.h>
40 #include <SemanticObjectRelations/Visualization/ContactDetectionVisualizer.h>
41 #include <SemanticObjectRelations/Visualization/SupportAnalysisVisualizer.h>
49 defineOptionalProperty<std::string>(
50 "WorkingMemoryName",
"WorkingMemory",
"Name of WorkingMemory component");
51 defineOptionalProperty<std::string>(
52 "PriorKnowledgeName",
"PriorKnowledge",
"Name of PriorKnowledge component");
53 defineOptionalProperty<std::string>(
"RobotStateComponentName",
54 "RobotStateComponent",
55 "Name of RobotStateComponent component");
56 defineOptionalProperty<std::string>(
57 "DebugDrawerTopicName",
"DebugDrawerUpdates",
"The name of the debug drawer topic");
58 defineOptionalProperty<int>(
"UpdatePeriodInMS", 10,
"Update period in ms");
60 defineOptionalProperty<semrel::VisuLevel>(
62 semrel::VisuLevel::RESULT,
63 "Visualization level: Determines what is shown in the debug drawer\n"
64 "Possible values: live, verbose, result, user, disabled")
65 .map(
"live", semrel::VisuLevel::LIVE_VISU)
66 .map(
"verbose", semrel::VisuLevel::VERBOSE)
67 .map(
"result", semrel::VisuLevel::RESULT)
68 .map(
"user", semrel::VisuLevel::USER)
69 .map(
"disabled", semrel::VisuLevel::DISABLED);
71 defineOptionalProperty<float>(
72 "ContactMarginInMM", 5.0f,
"Margin (mm) used during contact detection between objects");
75 static semrel::ContactPointList
76 calculateContactPoints(semrel::ShapeList
const& objects)
78 semrel::ContactPointList result;
80 VirtualRobot::CollisionCheckerPtr collisionChecker =
81 VirtualRobot::CollisionChecker::getGlobalCollisionChecker();
82 for (std::size_t i = 0; i < objects.size(); ++i)
86 for (std::size_t j = i + 1; j < objects.size(); ++j)
90 Eigen::Affine3f poseB(shapeB.
object->getGlobalPose());
92 VirtualRobot::CollisionModelPtr collModelA = shapeA.
object->getCollisionModel();
93 VirtualRobot::CollisionModelPtr collModelB = shapeB.
object->getCollisionModel();
94 VirtualRobot::MultiCollisionResult collisions =
95 collisionChecker->checkMultipleCollisions(collModelA, collModelB);
98 auto& facesB = collModelB->getTriMeshModel()->faces;
99 auto& normalsB = collModelB->getTriMeshModel()->normals;
100 for (VirtualRobot::SingleCollisionPair
const& collision : collisions.pairs)
103 VirtualRobot::MathTools::TriangleFace
const& faceOnB = facesB.at(collision.id2);
105 Eigen::Vector3f globalPointOnB = collision.contact2;
107 Eigen::Vector3f localNormalOnB;
108 if (faceOnB.idNormal1 == UINT_MAX)
110 localNormalOnB = poseB.linear() * faceOnB.normal;
114 Eigen::Vector3f n1B = normalsB[faceOnB.idNormal1];
115 Eigen::Vector3f n2B = normalsB[faceOnB.idNormal2];
116 Eigen::Vector3f n3B = normalsB[faceOnB.idNormal3];
118 localNormalOnB = (n1B + n2B + n3B).normalized();
120 Eigen::Vector3f globalNormalOnB = poseB.linear() * localNormalOnB;
122 result.emplace_back(&shapeA, &shapeB, globalPointOnB, globalNormalOnB);
131 filterObjectsWithoutCollisionModel(std::vector<memoryx::ObjectInstanceWrapper>& objects)
134 auto removeIter = std::remove_if(
140 if (object.manipulationObject->getCollisionModel())
142 if (object.manipulationObject->getCollisionModel()->getTriMeshModel())
148 ARMARX_WARNING_S <<
"No tri-mesh model in '"
149 << object.instanceInMemory->getMostProbableClass() <<
"'";
154 ARMARX_WARNING_S <<
"No collision model in '"
155 << object.instanceInMemory->getMostProbableClass() <<
"'";
159 objects.erase(removeIter, objects.end());
163 SemanticRelationAnalyzer::extractSupportGraphFromWorkingMemory(
const Ice::Current&)
165 std::lock_guard<std::mutex> lock(componentMutex);
167 std::vector<memoryx::ObjectInstanceWrapper> objectInstances =
168 objectInstancesSegment_.queryObjects();
169 filterObjectsWithoutCollisionModel(objectInstances);
172 std::set<semrel::ShapeID> safeShapes;
173 std::set<std::string> safeObjectClassNames = {
"workbench",
"booth"};
175 std::vector<std::string>
names;
177 for (
auto&
object : objectInstances)
179 std::string objectClass =
object.instanceInMemory->getMostProbableClass();
181 if (safeObjectClassNames.count(objectClass) > 0)
183 ARMARX_IMPORTANT <<
"Adding safe object: " <<
object.instanceInMemory->getName();
184 safeShapes.insert(semrel::ShapeID{uniqueID});
186 object.manipulationObject->getCollisionModel()->inflateModel(prop_ContactMarginInMM);
187 std::unique_ptr<semantic::SimoxObjectShape> shape =
188 std::make_unique<semantic::SimoxObjectShape>(
object.manipulationObject,
190 shape->entityId =
object.instanceInMemory->getId();
192 shape->setID(semrel::ShapeID{uniqueID});
194 shapes.push_back(std::move(shape));
200 semrel::ContactPointList contactPoints = calculateContactPoints(
shapes);
201 semrel::ContactGraph contactGraph = semrel::buildContactGraph(contactPoints,
shapes);
203 semrel::ContactDetectionVisualizer& visu = semrel::ContactDetectionVisualizer::get();
204 visu.drawContactPoints(contactPoints);
206 semrel::SupportGraph supportGraph =
207 supportAnalysis.performSupportAnalysis(
shapes, safeShapes, &contactGraph);
211 semrel::AttributedGraph attributedGraph = semrel::toAttributedGraph(supportGraph);
212 attributedGraph.serializeObjects(semrel::toShapeMap(
shapes));
214 getGraphStorageTopic()->begin_reportGraph(
"Support", iceGraph);
217 relationSegment->replaceRelations(relations);
222 #define GET_PROP(name) prop_##name = getProperty<decltype(prop_##name)>(#name).getValue()
225 SemanticRelationAnalyzer::onInitComponent()
236 offeringTopic(prop_DebugDrawerTopicName);
238 semantic::ArmarXLog::setAsImplementation(getName());
242 SemanticRelationAnalyzer::onConnectComponent()
244 workingMemory = getProxy<memoryx::WorkingMemoryInterfacePrx>(prop_WorkingMemoryName);
245 priorKnowledge = getProxy<memoryx::PriorKnowledgeInterfacePrx>(prop_PriorKnowledgeName);
246 robotStateComponent =
247 getProxy<RobotStateComponentInterfacePrx>(prop_RobotStateComponentName);
248 debugDrawer = getTopic<DebugDrawerInterfacePrx>(prop_DebugDrawerTopicName);
250 semantic::ArmarXVisualizer::setAsImplementation(debugDrawer);
251 semrel::VisuLevel visuLevel = getProperty<semrel::VisuLevel>(
"VisuLevel").getValue();
252 semrel::VisualizerInterface::get().setMinimumVisuLevel(visuLevel);
254 relationSegment = workingMemory->getRelationsSegment();
255 objectInstancesSegment_.initFromProxies(priorKnowledge, workingMemory, robotStateComponent);
257 jsonSerializer.reset(
258 new semantic::JsonSimoxShapeSerializer(&objectInstancesSegment_.objectClassSegment));
259 semantic::JsonSimoxShapeSerializer::registerSerializer(jsonSerializer,
true);
262 extractSupportGraphFromWorkingMemory(Ice::Current());
270 SemanticRelationAnalyzer::onDisconnectComponent()
273 updateTask =
nullptr;
277 SemanticRelationAnalyzer::onExitComponent()
282 SemanticRelationAnalyzer::createPropertyDefinitions()
288 static std::string
const LAYER_NAME =
"SemanticRelationAnalyzer";