ShapesSupportRelations.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package VisionX::ArmarXObjects::ShapesSupportRelations
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2019
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "ShapesSupportRelations.h"
24 
26 
29 
30 #include <SemanticObjectRelations/SupportAnalysis/json.h>
31 
33 {
35  std::string prefix) :
37  {
38  // ICE
39 
40  defineOptionalProperty<std::string>("ice.ShapesTopicName",
41  "ShapesTopic",
42  "Name of the topic on which shapes are reported.");
43 
44  defineOptionalProperty<std::string>(
45  "ice.SemanticGraphName",
46  "Support",
47  "Name of the graph when reported to the SemanticGraphStorage topic.");
48  defineOptionalProperty<std::string>("ice.SemanticGraphTopicName",
49  "SemanticGraphTopic",
50  "Name of SemanticGraphStorage topic.");
51 
52  defineOptionalProperty<std::string>("ice.RobotStateComponentName",
53  "RobotStateComponent",
54  "Name of the robot state component.");
55 
56  defineOptionalProperty<std::string>("pc.PointCloudFrameName",
58  "Name of the frame of the point cloud coordinates. \n"
59  "Used to determine the gravity vector.");
60 
61  defineOptionalProperty<std::string>("ice.DebugObserverName",
62  "DebugObserver",
63  "Name of the topic the DebugObserver listens to.");
64 
65 
66  // PARAMETERS
67 
68  defineOptionalProperty<float>(
69  "GR.ContactMargin",
70  10.,
71  "Distance by which objects are increased for contact computation [mm].")
72  .setMin(0.);
73 
74  defineOptionalProperty<float>(
75  "GR.VerticalSepPlaneAngleMax",
76  10.,
77  "Maximal angle [degree] between gravity and separating plane for \n"
78  "separating plane to be considered vertical.")
79  .setMin(0.);
80 
81  defineOptionalProperty<bool>(
82  "GR.VerticalSepPlaneAssumeSupport",
83  false,
84  "If true, edges are added if the separating plane is vertical.");
85 
86 
87  defineOptionalProperty<bool>(
88  "UD.enabled", true, "Enable or disble uncertainty detection (UD).");
89 
90  defineOptionalProperty<float>(
91  "UD.SupportAreaRatioMin",
92  0.7f,
93  "Minimal support area ratio of an object to consider it safe.")
94  .setMin(0.)
95  .setMax(1.);
96 
97 
98  // VISUALIZATION
99 
101  *this, properties::defaults::visualizationLevelName, semrel::VisuLevel::DISABLED);
102  }
103 
104  std::string
106  {
107  return "ShapesSupportRelations";
108  }
109 
110  void
112  {
113  // Register offered topices and used proxies here.
114 
115  usingTopicFromProperty("ice.ShapesTopicName");
116 
117  offeringTopicFromProperty("ice.SemanticGraphTopicName");
118  getProperty(graphName, "ice.SemanticGraphName");
119 
120  offeringTopicFromProperty("ice.DebugObserverName");
121  debugDrawer.offeringTopic(*this); // Calls this->offeringTopic().
122 
123  // usingProxyFromProperty("ice.RobotStateComponentName"); // RobotStateComponent is not required.
124  getProperty(pointCloudFrameName, "pc.PointCloudFrameName");
125 
126  supportAnalysis.setContactMargin(getProperty<float>("GR.ContactMargin"));
127  supportAnalysis.setVertSepPlaneAngleMax(getProperty<float>("GR.VerticalSepPlaneAngleMax"));
128  supportAnalysis.setVertSepPlaneAssumeSupport(
129  getProperty<bool>("GR.VerticalSepPlaneAssumeSupport"));
130 
131  supportAnalysis.setUncertaintyDetectionEnabled(getProperty<bool>("UD.enabled"));
132  supportAnalysis.setSupportAreaRatioMin(getProperty<float>("UD.SupportAreaRatioMin"));
133  }
134 
135  void
137  {
138  // Get topics and proxies here. Pass the *InterfacePrx type as template argument.
139  // Robot state component may not be available.
140  getProxyFromProperty(robotStateComponent, "ice.RobotStateComponentName", false, "", false);
141  getTopicFromProperty(graphTopic, "ice.SemanticGraphTopicName");
142 
143  if (!robotStateComponent)
144  {
145  ARMARX_INFO << "RobotStateComponent is not available. Global gravity vector "
146  << gravityInGlobal.transpose() << " will be used.";
147  }
148 
149  getTopicFromProperty(debugObserver, "ice.DebugObserverName");
150  debugDrawer.getTopic(*this); // Calls this->getTopic().
151 
152  setArmarXHooksAsImplementation(debugDrawer, getName());
154  }
155 
156  void
158  {
159  robotStateComponent = nullptr;
160  }
161 
162  void
164  {
165  }
166 
167  void
168  ShapesSupportRelations::reportShapes(const std::string& name,
169  const data::ShapeList& objectsIce,
170  const Ice::Current&)
171  {
172  const semrel::ShapeList objects = semantic::fromIce(objectsIce);
173  const semrel::ShapeMap objectsMap = semrel::toShapeMap(objects);
174 
175  std::set<semrel::ShapeID> safeObjectsIDs;
176  // Use the lowest object as safe object?
177 
178  const data::Graph graph = extractSupportGraph(objectsMap, safeObjectsIDs);
179 
180  std::stringstream graphName;
181  graphName << this->graphName << " (" << name << ")";
182  graphTopic->reportGraph(graphName.str(), graph);
183  }
184 
186  ShapesSupportRelations::extractSupportGraph(const data::ShapeList& objectsIce,
187  const Ice::LongSeq& safeObjectIDsIce,
188  const Ice::Current&)
189  {
190  const semrel::ShapeList objects = semantic::fromIce(objectsIce);
191  const semrel::ShapeMap objectsMap = semrel::toShapeMap(objects);
192 
193  std::set<semrel::ShapeID> safeObjectIDs;
194  for (long id : safeObjectIDsIce)
195  {
196  safeObjectIDs.insert(semrel::ShapeID{id});
197  }
198 
199  const data::Graph ice = extractSupportGraph(objectsMap, safeObjectIDs);
200  return ice;
201  }
202 
204  ShapesSupportRelations::extractSupportGraph(const semrel::ShapeMap& objects,
205  const std::set<semrel::ShapeID>& safeObjectIDs)
206  {
207  // Update gravity vector.
208  supportAnalysis.setGravityVector(getGravityFromRobotStateComponent());
209 
210  // Perform support analysis.
211  const semrel::SupportGraph supportGraph =
212  supportAnalysis.performSupportAnalysis(objects, safeObjectIDs);
213  ARMARX_INFO << "Support Graph: " << supportGraph.str();
214 
215  const data::Graph graphIce = semantic::toIce(supportGraph, objects);
216  return graphIce;
217  }
218 
219  Eigen::Vector3f
220  ShapesSupportRelations::getGravityFromRobotStateComponent()
221  {
222  if (!robotStateComponent || pointCloudFrameName == armarx::GlobalFrame)
223  {
224  return gravityInGlobal;
225  }
226  else
227  {
229  robotStateComponent, "", {}, VirtualRobot::RobotIO::eStructure);
230 
231  FramedDirection dir(gravityInGlobal, armarx::GlobalFrame, "");
232  dir.changeFrame(robot, pointCloudFrameName);
233  return dir.toEigen();
234  }
235  }
236 
239  {
242  }
243 
244 
245 } // namespace armarx::semantic
armarx::DebugDrawerTopic::offeringTopic
void offeringTopic(ManagedIceObject &component, const std::string &topicNameOverride="") const
Call offeringTopic([topicName]) on the given component.
Definition: DebugDrawerTopic.cpp:80
hooks.h
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:65
armarx::Component::offeringTopicFromProperty
void offeringTopicFromProperty(const std::string &propertyName)
Offer a topic whose name is specified by the given property.
Definition: Component.cpp:159
armarx::semantic::ShapesSupportRelations::extractSupportGraph
data::Graph extractSupportGraph(const data::ShapeList &objects, const Ice::LongSeq &safeObjectIDs, const Ice::Current &=Ice::emptyCurrent) override
Service: Extract the support graph for objects and return it.
Definition: ShapesSupportRelations.cpp:186
armarx::semantic::setArmarXHooksAsImplementation
void setArmarXHooksAsImplementation(const DebugDrawerInterfacePrx &debugDrawer, const std::string &logTag="SemanticObjectRelations")
Definition: hooks.cpp:7
ShapesSupportRelations.h
armarx::semantic::toIce
data::Graph toIce(const semrel::AttributedGraph &input)
Definition: graph.cpp:15
armarx::Component::getTopicFromProperty
TopicProxyType getTopicFromProperty(const std::string &propertyName)
Get a topic proxy whose name is specified by the given property.
Definition: Component.h:221
armarx::Component::getProxyFromProperty
ProxyType getProxyFromProperty(const std::string &propertyName, bool addToDependencies=false, const std::string &endpoints="", bool throwOnProxyError=true)
Get a proxy whose name is specified by the given property.
Definition: Component.h:242
armarx::semantic::ShapesSupportRelationsPropertyDefinitions
Property definitions of ShapesSupportRelations.
Definition: ShapesSupportRelations.h:47
armarx::semantic::ShapesSupportRelations::onInitComponent
void onInitComponent() override
Definition: ShapesSupportRelations.cpp:111
FramedPose.h
armarx::semantic::ShapesSupportRelations::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ShapesSupportRelations.cpp:238
armarx::semantic::properties::defineVisualizationLevel
void defineVisualizationLevel(armarx::ComponentPropertyDefinitions &defs, const std::string &propertyName=defaults::visualizationLevelName, semrel::VisuLevel defaultLevel=semrel::VisuLevel::RESULT, const std::string &description=defaults::visualizationLevelDescription)
Definition: ArmarXVisualizer.cpp:217
armarx::Component::usingTopicFromProperty
void usingTopicFromProperty(const std::string &propertyName, bool orderedPublishing=false)
Use a topic whose name is specified by the given property.
Definition: Component.cpp:165
armarx::Graph
boost::subgraph< CloudGraph > Graph
Definition: Common.h:58
armarx::semantic::ShapesSupportRelationsPropertyDefinitions::ShapesSupportRelationsPropertyDefinitions
ShapesSupportRelationsPropertyDefinitions(std::string prefix)
Definition: ShapesSupportRelations.cpp:34
armarx::semantic::ShapesSupportRelations::onExitComponent
void onExitComponent() override
Definition: ShapesSupportRelations.cpp:163
armarx::semantic::ShapesSupportRelations::onDisconnectComponent
void onDisconnectComponent() override
Definition: ShapesSupportRelations.cpp:157
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::semantic
Definition: ShapesSupportRelations.cpp:32
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::VariantType::FramedDirection
const VariantTypeId FramedDirection
Definition: FramedPose.h:37
armarx::semantic::properties::defaults::visualizationLevelName
const std::string visualizationLevelName
Definition: ArmarXVisualizer.cpp:211
armarx::PropertyUser::getProperty
Property< PropertyType > getProperty(const std::string &name)
Property creation and retrieval.
Definition: PropertyUser.h:180
armarx::RemoteRobot::createLocalClone
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
Definition: RemoteRobot.cpp:381
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:108
armarx::semantic::ShapesSupportRelations::reportShapes
void reportShapes(const std::string &name, const data::ShapeList &objects, const Ice::Current &) override
Topic/pipeline: Extract the support graph for objects and publish it to the graph topic.
Definition: ShapesSupportRelations.cpp:168
ice_serialization.h
armarx::DebugDrawerTopic::getTopic
DebugDrawerInterfacePrx getTopic() const
Get the topic.
Definition: DebugDrawerTopic.cpp:62
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
armarx::semantic::fromIce
semrel::AttributedGraph fromIce(const semantic::data::Graph &graph)
Definition: graph.h:31
armarx::semantic::ShapesSupportRelations::getDefaultName
std::string getDefaultName() const override
Definition: ShapesSupportRelations.cpp:105
armarx::semantic::ShapesSupportRelations::onConnectComponent
void onConnectComponent() override
Definition: ShapesSupportRelations.cpp:136
armarx::semantic::properties::setMinimumVisuLevel
void setMinimumVisuLevel(armarx::PropertyUser &defs, const std::string &propertyName=defaults::visualizationLevelName)
Definition: ArmarXVisualizer.cpp:237
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19