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
69namespace 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:
96 ARMARX_INFO_S << message;
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
109{
110 class PrimitiveExtractor;
111 using PrimitiveExtractorPtr = std::shared_ptr<PrimitiveExtractor>;
112} // namespace AffordanceKit
113
114namespace armarx
115{
116 /**
117 * @class PrimitiveExtractorPropertyDefinitions
118 * @brief
119 */
121 {
122 public:
125 {
127 "PointCloudSegmenterResult",
128 "name of the segmented point cloud data processor");
129
130 // required primitive parameters
132 "minimumSegmentSize",
133 100,
134 "Minimum segment size to be considered in the primitive extraction");
136 "maximumSegmentSize",
137 75000,
138 "Maximum segment size to be considered in the primitive extraction");
140 "planeMaximumIteration", 100, "Maximum ransac iteration for plane estimation");
142 "planeDistanceThreshold", 10.0, "Distance threshold for plane estimation");
144 "planeNormalDistance", 0.10, "Normal distance for plane estimation");
145 defineOptionalProperty<double>("cylinderMaximumIteration",
146 100,
147 "Maximum ransac iteration for cylinder estimation");
149 "cylinderDistanceThreshold", 0.1, "Distance threshold for cylinder estimation");
151 "cylinderRadiousLimit", 0.1, "Radious limit for cylinder estimation");
153 "sphereMaximumIteration", 100, "Maximum ransac iteration for sphere estimation");
155 "sphereDistanceThreshold", 0.9, "Distance threshold for sphere estimation");
157 "sphereNormalDistance", 0.1, "Normal distance for sphere estimation");
159 "vebose", false, "Required for debugging the primitive engine");
160 defineOptionalProperty<float>("euclideanClusteringTolerance",
161 50.0,
162 "Euclidean distance threshold for fine clustering");
164 "outlierThreshold", 50.0, "Outlier threshold for computing model outliers");
166 "circularDistanceThreshold", 0.01, "Distance threshold for circle estimation");
168 "enablePrimitiveFusion", false, "Fuse new primitives with existing ones");
169 defineOptionalProperty<bool>("UseAffordanceExtractionLibExport",
170 false,
171 "Use export functionality of affordance extraction lib");
173 "SpatialSamplingDistance", 50, "Distance between two spatial samplings in mm");
175 "NumOrientationalSamplings", 4, "Number of orientational samplings");
177 "EnableBoxPrimitives", false, "Toggle detection of box primitives");
178
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");
187 "RobotStateComponentName",
188 "RobotStateComponent",
189 "Name of the robot state component that should be used");
190
192 "PriorKnowledgeProxyName", "PriorKnowledge", "name of prior memory proxy");
194 "WorkingMemoryName", "WorkingMemory", "name of WorkingMemory component");
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
std::string timestamp()
constexpr T c
Default component property definition container.
Definition Component.h:70
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:46
visionx::PrimitiveExtractorParameters getParameters(const Ice::Current &c=Ice::emptyCurrent) override
void setParameters(const visionx::PrimitiveExtractorParameters &parameters, const Ice::Current &c=Ice::emptyCurrent) override
void enablePipelineStep(const Ice::Current &c=Ice::emptyCurrent) override
void onConnectPointCloudProcessor() override
void disablePipelineStep(const Ice::Current &c=Ice::emptyCurrent) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onDisconnectPointCloudProcessor() override
void loadParametersFromFile(const std::string &filename, const Ice::Current &c=Ice::emptyCurrent) override
void onInitPointCloudProcessor() override
armarx::TimestampBasePtr getLastProcessedTimestamp(const Ice::Current &c=Ice::emptyCurrent) override
std::string getDefaultName() const override
bool isPipelineStepEnabled(const Ice::Current &c=Ice::emptyCurrent) override
A brief description.
A brief description.
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)
The PointCloudProcessor class provides an interface for access to PointCloudProviders via Ice and sha...
#define ARMARX_INFO_S
Definition Logging.h:202
std::shared_ptr< PrimitiveExtractor > PrimitiveExtractorPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface > RobotStateComponentInterfacePrx
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface > DebugDrawerInterfacePrx
void print(pcl::console::VERBOSITY_LEVEL level, const char *format,...)