PrimitiveExtractor.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX
19  * @author Eren Aksoy ( eren dot aksoy at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 #include <stdarg.h>
28 
29 #include <cmath>
30 #include <functional>
31 #include <limits>
32 #include <map>
33 #include <mutex>
34 #include <tuple>
35 
36 #include <Eigen/Core>
37 
38 #include <pcl/common/angles.h>
39 #include <pcl/common/geometry.h>
40 #include <pcl/common/intersections.h>
41 #include <pcl/features/moment_of_inertia_estimation.h>
42 #include <pcl/kdtree/kdtree.h>
43 #include <pcl/kdtree/kdtree_flann.h>
44 
45 #include <IceUtil/IceUtil.h>
46 
47 #include <SimoxUtility/algorithm/string/string_tools.h>
48 
50 
51 #include <RobotAPI/interface/core/PoseBase.h>
52 #include <RobotAPI/interface/core/RobotState.h>
53 #include <RobotAPI/interface/visualization/DebugDrawerInterface.h>
55 
58 #include <VisionX/interface/components/PointCloudSegmenter.h>
59 #include <VisionX/interface/components/PrimitiveExtractor.h>
60 #include <VisionX/interface/components/PrimitiveMapper.h>
61 
62 #include "PrimitiveFilter.h"
63 #include "PrimitiveFusion.h"
65 #include <MemoryX/interface/components/WorkingMemoryInterface.h>
68 
69 namespace pcl::console
70 {
71  // Replace PCL's print function in order to pass the messages to the ArmarX logging system.
72  // TODO: This should be done automatically if PCL is linked into a component
73  void
74  print(pcl::console::VERBOSITY_LEVEL level, const char* format, ...)
75  {
76  std::string message;
77 
78  va_list args, args_copy;
79  va_start(args, format);
80  va_copy(args_copy, args);
81 
82  int size = std::vsnprintf(nullptr, 0, format, args) + 1;
83  message = std::string(size, ' ');
84  std::vsnprintf(&message.front(), size, format, args_copy);
85 
86  va_end(args_copy);
87  va_end(args);
88 
89  simox::alg::trim(message);
90 
91  switch (level)
92  {
93  case L_ALWAYS:
94  case L_ERROR:
95  case L_WARN:
97  break;
98 
99  case L_INFO:
100  case L_DEBUG:
101  case L_VERBOSE:
102  // Ignore
103  break;
104  }
105  }
106 } // namespace pcl::console
107 
108 namespace AffordanceKit
109 {
110  class PrimitiveExtractor;
111  using PrimitiveExtractorPtr = std::shared_ptr<PrimitiveExtractor>;
112 } // namespace AffordanceKit
113 
114 namespace armarx
115 {
116  /**
117  * @class PrimitiveExtractorPropertyDefinitions
118  * @brief
119  */
121  {
122  public:
125  {
126  defineOptionalProperty<std::string>("segmenterName",
127  "PointCloudSegmenterResult",
128  "name of the segmented point cloud data processor");
129 
130  // required primitive parameters
131  defineOptionalProperty<double>(
132  "minimumSegmentSize",
133  100,
134  "Minimum segment size to be considered in the primitive extraction");
135  defineOptionalProperty<double>(
136  "maximumSegmentSize",
137  75000,
138  "Maximum segment size to be considered in the primitive extraction");
139  defineOptionalProperty<double>(
140  "planeMaximumIteration", 100, "Maximum ransac iteration for plane estimation");
141  defineOptionalProperty<float>(
142  "planeDistanceThreshold", 10.0, "Distance threshold for plane estimation");
143  defineOptionalProperty<float>(
144  "planeNormalDistance", 0.10, "Normal distance for plane estimation");
145  defineOptionalProperty<double>("cylinderMaximumIteration",
146  100,
147  "Maximum ransac iteration for cylinder estimation");
148  defineOptionalProperty<float>(
149  "cylinderDistanceThreshold", 0.1, "Distance threshold for cylinder estimation");
150  defineOptionalProperty<float>(
151  "cylinderRadiousLimit", 0.1, "Radious limit for cylinder estimation");
152  defineOptionalProperty<double>(
153  "sphereMaximumIteration", 100, "Maximum ransac iteration for sphere estimation");
154  defineOptionalProperty<float>(
155  "sphereDistanceThreshold", 0.9, "Distance threshold for sphere estimation");
156  defineOptionalProperty<float>(
157  "sphereNormalDistance", 0.1, "Normal distance for sphere estimation");
158  defineOptionalProperty<bool>(
159  "vebose", false, "Required for debugging the primitive engine");
160  defineOptionalProperty<float>("euclideanClusteringTolerance",
161  50.0,
162  "Euclidean distance threshold for fine clustering");
163  defineOptionalProperty<float>(
164  "outlierThreshold", 50.0, "Outlier threshold for computing model outliers");
165  defineOptionalProperty<float>(
166  "circularDistanceThreshold", 0.01, "Distance threshold for circle estimation");
167  defineOptionalProperty<bool>(
168  "enablePrimitiveFusion", false, "Fuse new primitives with existing ones");
169  defineOptionalProperty<bool>("UseAffordanceExtractionLibExport",
170  false,
171  "Use export functionality of affordance extraction lib");
172  defineOptionalProperty<float>(
173  "SpatialSamplingDistance", 50, "Distance between two spatial samplings in mm");
174  defineOptionalProperty<int>(
175  "NumOrientationalSamplings", 4, "Number of orientational samplings");
176  defineOptionalProperty<bool>(
177  "EnableBoxPrimitives", false, "Toggle detection of box primitives");
178 
179  defineOptionalProperty<std::string>(
180  "sourceFrameName",
181  "DepthCamera",
182  "the robot node to use as source coordinate system for the captured point clouds");
183  defineOptionalProperty<std::string>("SegmentedPointCloudTopicName",
184  "SegmentedPointCloud",
185  "name of SegmentedPointCloud topic");
186  defineOptionalProperty<std::string>(
187  "RobotStateComponentName",
188  "RobotStateComponent",
189  "Name of the robot state component that should be used");
190 
191  defineOptionalProperty<std::string>(
192  "PriorKnowledgeProxyName", "PriorKnowledge", "name of prior memory proxy");
193  defineOptionalProperty<std::string>(
194  "WorkingMemoryName", "WorkingMemory", "name of WorkingMemory component");
195  defineOptionalProperty<std::string>(
196  "DebugDrawerTopicName", "DebugDrawerUpdates", "name of DebugDrawer topic");
197  defineOptionalProperty<std::string>("DebugObserverName",
198  "DebugObserver",
199  "Name of the topic the DebugObserver listens on");
200  }
201  };
202 
203  /**
204  * @class PrimitiveExtractor
205  *
206  * @ingroup VisionX-Components
207  * @brief A brief description
208  *
209  *
210  * Detailed Description
211  */
213  virtual public visionx::PrimitiveMapperInterface,
214  virtual public visionx::PointCloudProcessor
215  {
216  public:
217  /**
218  * @see armarx::ManagedIceObject::getDefaultName()
219  */
220  std::string
221  getDefaultName() const override
222  {
223  return "PrimitiveExtractor";
224  }
225 
226  protected:
227  /**
228  * @see visionx::PointCloudProcessor::onInitPointCloudProcessor()
229  */
230  void onInitPointCloudProcessor() override;
231 
232  /**
233  * @see visionx::PointCloudProcessor::onConnectPointCloudProcessor()
234  */
235  void onConnectPointCloudProcessor() override;
236 
237  /**
238  * @see visionx::PointCloudProcessor::onDisconnectPointCloudProcessor()
239  */
240  void onDisconnectPointCloudProcessor() override;
241 
242  /**
243  * @see visionx::PointCloudProcessor::onExitPointCloudProcessor()
244  */
245  void onExitPointCloudProcessor() override;
246 
247  /**
248  * @see visionx::PointCloudProcessor::process()
249  */
250  void process() override;
251 
252  void enablePipelineStep(const Ice::Current& c = Ice::emptyCurrent) override;
253  void disablePipelineStep(const Ice::Current& c = Ice::emptyCurrent) override;
254  bool isPipelineStepEnabled(const Ice::Current& c = Ice::emptyCurrent) override;
255  armarx::TimestampBasePtr
256  getLastProcessedTimestamp(const Ice::Current& c = Ice::emptyCurrent) override;
257 
258  void setParameters(const visionx::PrimitiveExtractorParameters& parameters,
259  const Ice::Current& c = Ice::emptyCurrent) override;
260  void loadParametersFromFile(const std::string& filename,
261  const Ice::Current& c = Ice::emptyCurrent) override;
262 
263  visionx::PrimitiveExtractorParameters
264  getParameters(const Ice::Current& c = Ice::emptyCurrent) override;
265 
266  /**
267  * @see PropertyUser::createPropertyDefinitions()
268  */
270 
271  private:
272  void fusePrimitives(std::vector<memoryx::EntityBasePtr>& newPrimitives, long timestamp);
273 
274  void extractPrimitives();
275  float getGraspPointInlierRatio(memoryx::EnvironmentalPrimitiveBasePtr leftPrimitive,
276  memoryx::EnvironmentalPrimitiveBasePtr rightPrimitive);
277 
278  void pushPrimitivesToMemory(IceUtil::Int64 originalTimestamp);
279  void pushPrimitivesToMemoryAffordanceKit(IceUtil::Int64 originalTimestamp);
280 
281  private:
282  AffordanceKit::PrimitiveExtractorPtr primitiveExtractor;
283 
284  bool isConnectedtoExtractor;
285  double minSegmentSize;
286  double maxSegmentSize;
287  double pMaxIter;
288  float pDistThres;
289  float pNorDist;
290  double cMaxIter;
291  float cDistThres;
292  float cRadLim;
293  double sMaxIter;
294  float sDistThres;
295  float sNorDist;
296  bool verbose;
297  float euclideanClusteringTolerance;
298  float outlierThreshold;
299  float circleDistThres;
300 
301  // init point clouds
302  pcl::PointCloud<pcl::PointXYZL>::Ptr labeledCloudPtr;
303  pcl::PointCloud<pcl::PointXYZL>::Ptr inliersCloudPtr;
304  pcl::PointCloud<pcl::PointXYZL>::Ptr graspCloudPtr;
305 
306  std::mutex mutexPushing;
307  std::mutex pointCloudMutex;
308  std::mutex enableMutex;
309 
310  std::mutex timestampMutex;
311  long lastProcessedTimestamp;
312 
313  bool isConnectedtoSegmenter;
314 
315  std::string providerName;
316 
317  memoryx::WorkingMemoryInterfacePrx workingMemory;
318  memoryx::EnvironmentalPrimitiveSegmentBasePrx environmentalPrimitiveSegment;
319  visionx::PointCloudSegmentationListenerPrx pointCloudSegmentationPrx;
320 
321  std::string sourceFrameName;
322  std::string agentName;
323 
324  DebugDrawerInterfacePrx debugDrawerTopicPrx;
325 
326  int lastUsedLabel;
327 
328  bool mappingEnabled;
329  bool enablePrimitiveFusion;
330 
331  armarx::RobotStateComponentInterfacePrx robotStateComponent;
332 
333  PrimitiveFusion fusionStrategy;
334  PrimitiveFilter primitiveFilter;
335 
336  size_t numViews;
337 
338  float spatialSamplingDistance;
339  int numOrientationalSamplings;
340 
341  DebugObserverInterfacePrx debugObserver;
342  };
343 } // namespace armarx
armarx::PrimitiveExtractor::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: PrimitiveExtractor.cpp:985
armarx::PrimitiveFilter
A brief description.
Definition: PrimitiveFilter.h:44
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:345
armarx::PrimitiveExtractor::onConnectPointCloudProcessor
void onConnectPointCloudProcessor() override
Definition: PrimitiveExtractor.cpp:120
PointCloudProvider.h
armarx::PrimitiveExtractor::getParameters
visionx::PrimitiveExtractorParameters getParameters(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PrimitiveExtractor.cpp:1085
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
message
message(STATUS "Boost-Library-Dir: " "${Boost_LIBRARY_DIRS}") message(STATUS "Boost-LIBRARIES
Definition: CMakeLists.txt:8
EnvironmentalPrimitiveSegment.h
armarx::PrimitiveExtractor::isPipelineStepEnabled
bool isPipelineStepEnabled(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PrimitiveExtractor.cpp:991
pcl::console
Definition: PrimitiveExtractor.h:69
armarx::PrimitiveExtractorPropertyDefinitions::PrimitiveExtractorPropertyDefinitions
PrimitiveExtractorPropertyDefinitions(std::string prefix)
Definition: PrimitiveExtractor.h:123
PrimitiveFilter.h
pcl::console::print
void print(pcl::console::VERBOSITY_LEVEL level, const char *format,...)
Definition: PrimitiveExtractor.h:74
armarx::PrimitiveExtractor
A brief description.
Definition: PrimitiveExtractor.h:212
PrimitiveFusion.h
armarx::PrimitiveExtractor::enablePipelineStep
void enablePipelineStep(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PrimitiveExtractor.cpp:188
armarx::PrimitiveExtractor::process
void process() override
Definition: PrimitiveExtractor.cpp:202
visionx::PointCloudProcessor
The PointCloudProcessor class provides an interface for access to PointCloudProviders via Ice and sha...
Definition: PointCloudProcessor.h:177
armarx::PrimitiveExtractor::getDefaultName
std::string getDefaultName() const override
Definition: PrimitiveExtractor.h:221
FramedPose.h
MemoryXCoreObjectFactories.h
filename
std::string filename
Definition: VisualizationRobot.cpp:86
armarx::PrimitiveFusion
A brief description.
Definition: PrimitiveFusion.h:42
armarx::PrimitiveExtractor::disablePipelineStep
void disablePipelineStep(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PrimitiveExtractor.cpp:195
PointCloudProcessor.h
Component.h
armarx::PrimitiveExtractor::onInitPointCloudProcessor
void onInitPointCloudProcessor() override
Definition: PrimitiveExtractor.cpp:52
armarx::PrimitiveExtractor::onDisconnectPointCloudProcessor
void onDisconnectPointCloudProcessor() override
Definition: PrimitiveExtractor.cpp:178
armarx::PrimitiveExtractor::loadParametersFromFile
void loadParametersFromFile(const std::string &filename, const Ice::Current &c=Ice::emptyCurrent) override
Definition: PrimitiveExtractor.cpp:1027
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
AffordanceKit
Definition: PrimitiveExtractor.h:108
armarx::PrimitiveExtractorPropertyDefinitions
Definition: PrimitiveExtractor.h:120
IceUtil::Handle< class PropertyDefinitionContainer >
IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface >
MemoryXTypesObjectFactories.h
armarx::PrimitiveExtractor::getLastProcessedTimestamp
armarx::TimestampBasePtr getLastProcessedTimestamp(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PrimitiveExtractor.cpp:998
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:202
armarx::PrimitiveExtractor::setParameters
void setParameters(const visionx::PrimitiveExtractorParameters &parameters, const Ice::Current &c=Ice::emptyCurrent) override
Definition: PrimitiveExtractor.cpp:1005
armarx::PrimitiveExtractor::onExitPointCloudProcessor
void onExitPointCloudProcessor() override
Definition: PrimitiveExtractor.cpp:183
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
AffordanceKit::PrimitiveExtractorPtr
std::shared_ptr< PrimitiveExtractor > PrimitiveExtractorPtr
Definition: PrimitiveExtractor.h:111