27#include <VirtualRobot/CollisionDetection/CollisionChecker.h>
28#include <VirtualRobot/CollisionDetection/CollisionCheckerImplementation.h>
29#include <VirtualRobot/CollisionDetection/CollisionModel.h>
30#include <VirtualRobot/ManipulationObject.h>
31#include <VirtualRobot/Visualization/TriMeshModel.h>
40#include <SemanticObjectRelations/Shapes/json/ShapeSerializers.h>
41#include <SemanticObjectRelations/SupportAnalysis/json.h>
42#include <SemanticObjectRelations/Visualization/ContactDetectionVisualizer.h>
43#include <SemanticObjectRelations/Visualization/SupportAnalysisVisualizer.h>
52 "WorkingMemoryName",
"WorkingMemory",
"Name of WorkingMemory component");
54 "PriorKnowledgeName",
"PriorKnowledge",
"Name of PriorKnowledge component");
56 "RobotStateComponent",
57 "Name of RobotStateComponent component");
59 "DebugDrawerTopicName",
"DebugDrawerUpdates",
"The name of the debug drawer topic");
64 semrel::VisuLevel::RESULT,
65 "Visualization level: Determines what is shown in the debug drawer\n"
66 "Possible values: live, verbose, result, user, disabled")
67 .map(
"live", semrel::VisuLevel::LIVE_VISU)
68 .map(
"verbose", semrel::VisuLevel::VERBOSE)
69 .map(
"result", semrel::VisuLevel::RESULT)
70 .map(
"user", semrel::VisuLevel::USER)
71 .map(
"disabled", semrel::VisuLevel::DISABLED);
74 "ContactMarginInMM", 5.0f,
"Margin (mm) used during contact detection between objects");
77 static semrel::ContactPointList
78 calculateContactPoints(semrel::ShapeList
const&
objects)
80 semrel::ContactPointList result;
82 VirtualRobot::CollisionCheckerPtr collisionChecker =
83 VirtualRobot::CollisionChecker::getGlobalCollisionChecker();
84 for (std::size_t i = 0; i <
objects.size(); ++i)
88 for (std::size_t j = i + 1; j <
objects.size(); ++j)
92 Eigen::Affine3f poseB(shapeB.
object->getGlobalPose());
94 VirtualRobot::CollisionModelPtr collModelA = shapeA.
object->getCollisionModel();
95 VirtualRobot::CollisionModelPtr collModelB = shapeB.
object->getCollisionModel();
96 VirtualRobot::MultiCollisionResult collisions =
97 collisionChecker->checkMultipleCollisions(collModelA, collModelB);
100 auto& facesB = collModelB->getTriMeshModel()->faces;
101 auto& normalsB = collModelB->getTriMeshModel()->normals;
102 for (VirtualRobot::SingleCollisionPair
const& collision : collisions.pairs)
105 VirtualRobot::MathTools::TriangleFace
const& faceOnB = facesB.at(collision.id2);
107 Eigen::Vector3f globalPointOnB = collision.contact2;
109 Eigen::Vector3f localNormalOnB;
110 if (faceOnB.idNormal1 == UINT_MAX)
112 localNormalOnB = poseB.linear() * faceOnB.normal;
116 Eigen::Vector3f n1B = normalsB[faceOnB.idNormal1];
117 Eigen::Vector3f n2B = normalsB[faceOnB.idNormal2];
118 Eigen::Vector3f n3B = normalsB[faceOnB.idNormal3];
120 localNormalOnB = (n1B + n2B + n3B).normalized();
122 Eigen::Vector3f globalNormalOnB = poseB.linear() * localNormalOnB;
124 result.emplace_back(&shapeA, &shapeB, globalPointOnB, globalNormalOnB);
133 filterObjectsWithoutCollisionModel(std::vector<memoryx::ObjectInstanceWrapper>&
objects)
136 auto removeIter = std::remove_if(
139 [](memoryx::ObjectInstanceWrapper
const&
object)
142 if (object.manipulationObject->getCollisionModel())
144 if (object.manipulationObject->getCollisionModel()->getTriMeshModel())
150 ARMARX_WARNING_S <<
"No tri-mesh model in '"
151 << object.instanceInMemory->getMostProbableClass() <<
"'";
156 ARMARX_WARNING_S <<
"No collision model in '"
157 << object.instanceInMemory->getMostProbableClass() <<
"'";
164 semantic::data::Graph
167 std::lock_guard<std::mutex> lock(componentMutex);
169 std::vector<memoryx::ObjectInstanceWrapper> objectInstances =
170 objectInstancesSegment_.queryObjects();
171 filterObjectsWithoutCollisionModel(objectInstances);
173 semrel::ShapeList shapes;
174 std::set<semrel::ShapeID> safeShapes;
175 std::set<std::string> safeObjectClassNames = {
"workbench",
"booth"};
177 std::vector<std::string> names;
179 for (
auto&
object : objectInstances)
181 std::string objectClass =
object.instanceInMemory->getMostProbableClass();
183 if (safeObjectClassNames.count(objectClass) > 0)
185 ARMARX_IMPORTANT <<
"Adding safe object: " <<
object.instanceInMemory->getName();
186 safeShapes.insert(semrel::ShapeID{uniqueID});
188 object.manipulationObject->getCollisionModel()->inflateModel(prop_ContactMarginInMM);
189 std::unique_ptr<semantic::SimoxObjectShape> shape =
190 std::make_unique<semantic::SimoxObjectShape>(
object.manipulationObject,
192 shape->entityId =
object.instanceInMemory->getId();
194 shape->setID(semrel::ShapeID{uniqueID});
196 shapes.push_back(std::move(shape));
202 semrel::ContactPointList contactPoints = calculateContactPoints(shapes);
203 semrel::ContactGraph contactGraph = semrel::buildContactGraph(contactPoints, shapes);
205 semrel::ContactDetectionVisualizer& visu = semrel::ContactDetectionVisualizer::get();
206 visu.drawContactPoints(contactPoints);
208 semrel::SupportGraph supportGraph =
209 supportAnalysis.performSupportAnalysis(shapes, safeShapes, &contactGraph);
213 semrel::AttributedGraph attributedGraph = semrel::toAttributedGraph(supportGraph);
214 attributedGraph.serializeObjects(semrel::toShapeMap(shapes));
218 memoryx::RelationList relations = semantic::toMemory(attributedGraph);
219 relationSegment->replaceRelations(relations);
224#define GET_PROP(name) prop_##name = getProperty<decltype(prop_##name)>(#name).getValue()
248 robotStateComponent =
254 semrel::VisualizerInterface::get().setMinimumVisuLevel(visuLevel);
256 relationSegment = workingMemory->getRelationsSegment();
257 objectInstancesSegment_.initFromProxies(priorKnowledge, workingMemory, robotStateComponent);
259 jsonSerializer.reset(
260 new semantic::JsonSimoxShapeSerializer(&objectInstancesSegment_.objectClassSegment));
261 semantic::JsonSimoxShapeSerializer::registerSerializer(jsonSerializer,
true);
267 this, &SemanticRelationAnalyzer::update, prop_UpdatePeriodInMS);
275 updateTask =
nullptr;
290 static std::string
const LAYER_NAME =
"SemanticRelationAnalyzer";
293 SemanticRelationAnalyzer::update()
303 return "SemanticRelationAnalyzer";
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
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)
Brief description of class SemanticRelationAnalyzer.
void onInitComponent() override
void onDisconnectComponent() override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
semantic::data::Graph extractSupportGraphFromWorkingMemory(const Ice::Current &) override
void onConnectComponent() override
static std::string GetDefaultName()
void onExitComponent() override
std::string getDefaultName() const override
Retrieve default name of component.
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.