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>
69 ARMARX_INFO <<
"Next timeout at " + nextTimeoutAbs.toDateTime();
72 getIceManager()->subscribeTopic(prx.first,
"ServiceRequests");
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>());
135 ARMARX_INFO <<
"Timeout or error while waiting for point cloud data";
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);
168 prism.setViewPoint(std::numeric_limits<float>::max(),
169 std::numeric_limits<float>::max(),
170 std::numeric_limits<float>::max());
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);
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;
259armarx::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;
298TabletopSegmentation::guiTask()
302 RemoteGui::TabProxy tab(remoteGui,
getName());
304 while (!remoteGuiTask->isStopped())
306 tab.receiveUpdates();
309 c.waitForCycleDuration();
314TabletopSegmentation::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
424visionx::TabletopSegmentationPlanePtr
427 std::unique_lock guard(tablePlaneMutex);
432 const std::function<
void(
int)>& callback) :
433 serviceName(serviceName), callback(callback)
439 Ice::Int relativeTimeoutMs,
442 if (serviceName == this->serviceName)
444 callback(relativeTimeoutMs);
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
#define ARMARX_REMOTE_GUI_GET_PROP(name)
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.
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.
IceManagerPtr getIceManager() const
Returns the IceManager.
ServiceProvider(const std::string &serviceName, const std::function< void(int)> &callback)
void requestService(const std::string &, Ice::Int relativeTimeoutMs, const Ice::Current &)
visionx::TabletopSegmentationPlanePtr getTablePlane(const Ice::Current &) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onExitPointCloudProcessor() override
void onConnectPointCloudProcessor() override
void onDisconnectPointCloudProcessor() override
void onInitPointCloudProcessor() override
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
void enableResultPointClouds(std::string resultProviderName="")
Enables visualization.
int getPointClouds(const PointCloudPtrT &pointCloudPtr)
Poll PointClouds from provider.
bool waitForPointClouds(int milliseconds=1000)
Wait for new PointClouds.
void provideResultPointClouds(const PointCloudPtrT &pointClouds, std::string providerName="")
sends result PointClouds for visualization
#define ARMARX_INFO
The normal logging level.
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
#define ARMARX_VERBOSE
The logging level for verbose information.
detail::CheckBoxBuilder makeCheckBox(std::string const &name)
detail::FloatSliderBuilder makeFloatSlider(std::string const &name)
detail::GridLayoutBuilder makeGridLayout(std::string const &name="")
detail::IntSliderBuilder makeIntSlider(std::string const &name)
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Derived & steps(int steps)
Derived & value(ValueT const &value)