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>
50 "WorkingMemoryName",
"WorkingMemory",
"Name of WorkingMemory component");
52 "PriorKnowledgeName",
"PriorKnowledge",
"Name of PriorKnowledge component");
54 "RobotStateComponent",
55 "Name of RobotStateComponent component");
57 "DebugDrawerTopicName",
"DebugDrawerUpdates",
"The name of the debug drawer topic");
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);
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(
137 [](memoryx::ObjectInstanceWrapper
const&
object)
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() <<
"'";
162 semantic::data::Graph
165 std::lock_guard<std::mutex> lock(componentMutex);
167 std::vector<memoryx::ObjectInstanceWrapper> objectInstances =
168 objectInstancesSegment_.queryObjects();
169 filterObjectsWithoutCollisionModel(objectInstances);
171 semrel::ShapeList shapes;
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));
216 memoryx::RelationList relations = semantic::toMemory(attributedGraph);
217 relationSegment->replaceRelations(relations);
222#define GET_PROP(name) prop_##name = getProperty<decltype(prop_##name)>(#name).getValue()
246 robotStateComponent =
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);
265 this, &SemanticRelationAnalyzer::update, prop_UpdatePeriodInMS);
273 updateTask =
nullptr;
288 static std::string
const LAYER_NAME =
"SemanticRelationAnalyzer";
291 SemanticRelationAnalyzer::update()
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Property< PropertyType > getProperty(const std::string &name)
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
std::string getName() const
Retrieve name of object.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
The periodic task executes one thread method repeatedly using the time period specified in the constr...
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
armarx::semantic::GraphStorageTopicPrx const & getGraphStorageTopic()
SemanticRelationAnalyzerPropertyDefinitions(std::string prefix)
void onInitComponent() override
void onDisconnectComponent() override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
semantic::data::Graph extractSupportGraphFromWorkingMemory(const Ice::Current &) override
void onConnectComponent() override
void onExitComponent() override
static void setAsImplementation(const std::string &tag)
static void setAsImplementation(const armarx::DebugDrawerInterfacePrx &debugDrawer)
VirtualRobot::ManipulationObjectPtr object
#define ARMARX_INFO
The normal logging level.
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
data::Graph toIce(const semrel::AttributedGraph &input)
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.