29 #include <pcl/common/common.h>
30 #include <pcl/common/transforms.h>
31 #include <pcl/features/normal_3d.h>
32 #include <pcl/filters/extract_indices.h>
33 #include <pcl/filters/filter.h>
34 #include <pcl/filters/voxel_grid.h>
35 #include <pcl/kdtree/kdtree.h>
36 #include <pcl/sample_consensus/method_types.h>
37 #include <pcl/sample_consensus/model_types.h>
38 #include <pcl/segmentation/region_growing.h>
40 #include <VirtualRobot/math/FitPlane.h>
60 offeringTopic(getProperty<std::string>(
"DebugObserverName").getValue());
61 offeringTopic(getProperty<std::string>(
"DebugDrawerTopicName").getValue());
69 ARMARX_INFO <<
"Next timeout at " + nextTimeoutAbs.toDateTime();
71 auto prx = getIceManager()->registerObject(serviceProvider, getName() +
"ServiceProvider");
72 getIceManager()->subscribeTopic(prx.first,
"ServiceRequests");
78 debugObserver = getTopic<DebugObserverInterfacePrx>(
79 getProperty<std::string>(
"DebugObserverName").getValue());
80 debugDrawer = getTopic<DebugDrawerInterfacePrx>(
81 getProperty<std::string>(
"DebugDrawerTopicName").getValue());
83 prop_DistanceThreshold = getProperty<double>(
"DistanceThreshold");
84 prop_EpsAngle = getProperty<double>(
"EpsAngle");
86 prop_IncludeTabletopPlane = getProperty<bool>(
"IncludeTabletopPlane");
87 prop_CalculatePlane = getProperty<bool>(
"CalculatePlane");
89 prop_ClusterTolerance = getProperty<double>(
"ClusterTolerance");
90 prop_MinClusterSize = getProperty<int>(
"MinClusterSize");
91 prop_MaxClusterSize = getProperty<int>(
"MaxClusterSize");
93 prop_HeightMin = getProperty<double>(
"HeightMin");
94 prop_HeightMax = getProperty<double>(
"HeightMax");
96 enableResultPointClouds<PointL>(getName() +
"Result");
97 enableResultPointClouds<PointT>(getName() +
"Plane");
99 getProxyFromProperty(remoteGui,
"RemoteGuiName",
false,
"",
false);
103 remoteGuiTask->start();
112 remoteGuiTask->stop();
113 remoteGuiTask =
nullptr;
125 pcl::PointCloud<PointT>::Ptr inputCloud(
new pcl::PointCloud<PointT>());
126 pcl::PointCloud<PointT>::Ptr plane(
new pcl::PointCloud<PointT>());
128 if (!getProperty<bool>(
"AlwaysActive").getValue() &&
TimeUtil::GetTime() > nextTimeoutAbs)
133 if (!waitForPointClouds())
135 ARMARX_INFO <<
"Timeout or error while waiting for point cloud data";
140 getPointClouds(inputCloud);
145 if (inputCloud->points.size() == 0)
151 bool calculatePlane =
false;
152 bool includeTabletopPlane =
false;
155 std::unique_lock<std::mutex> lock(prop_mutex);
157 seg.setOptimizeCoefficients(
true);
158 seg.setModelType(pcl::SACMODEL_PERPENDICULAR_PLANE);
159 seg.setMethodType(pcl::SAC_RANSAC);
160 seg.setMaxIterations(1000);
161 seg.setDistanceThreshold(prop_DistanceThreshold);
162 seg.setAxis(Eigen::Vector3f::UnitZ());
163 seg.setEpsAngle(prop_EpsAngle);
164 seg.setProbability(0.99);
166 hull.setDimension(2);
167 prism.setHeightLimits(prop_HeightMin, prop_HeightMax);
172 ec.setClusterTolerance(prop_ClusterTolerance);
173 ec.setMinClusterSize(prop_MinClusterSize);
174 ec.setMaxClusterSize(prop_MaxClusterSize);
176 includeTabletopPlane = prop_IncludeTabletopPlane;
177 calculatePlane = prop_CalculatePlane;
180 pcl::PointIndices::Ptr inliers(
new pcl::PointIndices);
181 pcl::ModelCoefficients::Ptr coefficients(
new pcl::ModelCoefficients);
182 pcl::PointCloud<PointT>::Ptr tempCloud(
new pcl::PointCloud<PointT>());
186 seg.setInputCloud(inputCloud);
187 seg.segment(*inliers, *coefficients);
189 if (!inliers->indices.size())
196 extract.setInputCloud(inputCloud);
197 extract.setIndices(inliers);
198 extract.setNegative(
false);
199 extract.filter(*plane);
201 extract.setNegative(
true);
202 extract.filter(*tempCloud);
204 pcl::PointCloud<PointT>::Ptr hullPoints(
new pcl::PointCloud<PointT>());
205 hull.setInputCloud(inputCloud);
206 hull.setIndices(inliers);
207 hull.reconstruct(*hullPoints);
209 prism.setInputCloud(tempCloud);
210 prism.setInputPlanarHull(hullPoints);
211 prism.segment(*inliers);
213 extract.setInputCloud(tempCloud);
214 extract.setIndices(inliers);
215 extract.setNegative(
false);
216 extract.filter(*tempCloud);
220 provideResultPointClouds(plane, getName() +
"Plane");
224 std::vector<Eigen::Vector3f> planePoints;
225 for (
const PointT& p : plane->points)
227 planePoints.emplace_back(Eigen::Vector3f(p.x, p.y, p.z));
229 math::Plane mplane = math::FitPlane::Fit(planePoints);
230 std::unique_lock guard(tablePlaneMutex);
231 Eigen::Vector3f planeNormal = mplane.GetNormal(Eigen::Vector3f::UnitZ());
233 tablePlane =
new visionx::TabletopSegmentationPlane(
new Vector3(mplane.Pos()),
237 pcl::PointCloud<PointL>::Ptr resultCloud(
new pcl::PointCloud<PointL>());
238 extractEuclideanClusters(tempCloud, resultCloud);
240 if (includeTabletopPlane)
242 pcl::PointCloud<PointL>::Ptr planeLabel(
new pcl::PointCloud<PointL>());
243 pcl::copyPointCloud(*plane, *planeLabel);
244 for (
PointL& p : planeLabel->points)
248 pcl::PointCloud<PointL>::Ptr combinedCloud(
new pcl::PointCloud<PointL>());
249 *combinedCloud = *resultCloud + *planeLabel;
250 resultCloud->swap(*combinedCloud);
252 resultCloud->header.stamp = inputCloud->header.stamp;
255 provideResultPointClouds(resultCloud, getName() +
"Result");
259 armarx::TabletopSegmentation::extractEuclideanClusters(
260 const pcl::PointCloud<PointT>::ConstPtr& inputCloud,
261 const pcl::PointCloud<PointL>::Ptr& resultCloud)
263 pcl::search::KdTree<PointT>::Ptr tree(
new pcl::search::KdTree<PointT>);
264 tree->setInputCloud(inputCloud);
266 std::vector<pcl::PointIndices> clusterIndices;
267 ec.setSearchMethod(tree);
268 ec.setInputCloud(inputCloud);
269 ec.extract(clusterIndices);
272 for (std::vector<pcl::PointIndices>::const_iterator it = clusterIndices.begin();
273 it != clusterIndices.end();
276 for (std::vector<int>::const_iterator pit = it->indices.begin(); pit != it->indices.end();
279 const unsigned int i =
static_cast<unsigned int>(*pit);
280 const PointT& src = inputCloud->points[i];
283 p.getVector3fMap() = src.getVector3fMap();
287 resultCloud->points.push_back(p);
292 resultCloud->width =
static_cast<unsigned int>(resultCloud->points.size());
293 resultCloud->height = 1;
294 resultCloud->is_dense =
false;
298 TabletopSegmentation::guiTask()
304 while (!remoteGuiTask->isStopped())
306 tab.receiveUpdates();
309 c.waitForCycleDuration();
314 TabletopSegmentation::guiCreate()
320 grid.addTextLabel(
"Distance Threshold", row, 0);
322 .
value(prop_DistanceThreshold)
326 grid.addChild(slider, row, 1);
330 grid.addTextLabel(
"Eps Angle", row, 0);
332 .
value(prop_EpsAngle)
333 .
min(pcl::deg2rad(1.0))
334 .
max(pcl::deg2rad(30.0))
336 grid.addChild(slider, row, 1);
340 grid.addTextLabel(
"Cluster Tolerance", row, 0);
342 .
value(prop_ClusterTolerance)
346 grid.addChild(slider, row, 1);
350 grid.addTextLabel(
"Min Cluster Size ", row, 0);
352 .
value(prop_MinClusterSize)
355 grid.addChild(slider, row, 1);
359 grid.addTextLabel(
"Max Cluster Size ", row, 0);
361 .
value(prop_MaxClusterSize)
364 grid.addChild(slider, row, 1);
368 grid.addTextLabel(
"Height Min", row, 0);
370 .
value(prop_HeightMin)
374 grid.addChild(slider, row, 1);
378 grid.addTextLabel(
"Height Max", row, 0);
380 .
value(prop_HeightMax)
384 grid.addChild(slider, row, 1);
388 grid.addTextLabel(
"Include Tabletop Plane", row, 0);
391 grid.addChild(checkbox, row, 1);
395 grid.addTextLabel(
"Calculate Plane", row, 0);
397 grid.addChild(checkbox, row, 1);
401 remoteGui->createTab(
getName(), grid);
407 std::unique_lock<std::mutex> lock(prop_mutex);
408 #define ARMARX_REMOTE_GUI_GET_PROP(name) \
409 prop_##name = tab.getValue<decltype(prop_##name)>(#name).get()
421 #undef ARMARX_REMOTE_GUI_GET_PROP
424 visionx::TabletopSegmentationPlanePtr
427 std::unique_lock guard(tablePlaneMutex);
432 const std::function<
void(
int)>& callback) :
433 serviceName(serviceName), callback(callback)
442 if (serviceName == this->serviceName)
444 callback(relativeTimeoutMs);