plane_fitting_ransac.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <pcl/ModelCoefficients.h>
4 #include <pcl/sample_consensus/method_types.h>
5 #include <pcl/sample_consensus/model_types.h>
6 #include <pcl/segmentation/sac_segmentation.h>
7 
8 #include "plane_fitting_ransac.h"
9 
10 
11 // This header exposes more includes, but also the function template.
12 
13 namespace visionx::tools
14 {
15 
16  template <typename PointT, typename IndicesPtrT = pcl::IndicesPtr>
17  std::optional<PlaneFittingResult> fitPlaneRansac(
18  typename pcl::PointCloud<PointT>::Ptr cloud,
19  double distanceThreshold = 1.0,
20  const IndicesPtrT& indices = nullptr)
21  {
22  // Reference: http://pointclouds.org/documentation/tutorials/planar_segmentation.php
23 
24  pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients);
25  pcl::PointIndices::Ptr inliers(new pcl::PointIndices);
26 
27  // Create the segmentation object
28  pcl::SACSegmentation<PointT> seg;
29 
30  // Optional
31  seg.setOptimizeCoefficients(true);
32 
33  // Mandatory
34  seg.setModelType(pcl::SACMODEL_PLANE);
35  seg.setMethodType(pcl::SAC_RANSAC);
36  seg.setDistanceThreshold(distanceThreshold);
37 
38  seg.setInputCloud(cloud);
39  if (indices)
40  {
41  seg.setIndices(indices);
42  }
43  seg.segment(*inliers, *coefficients);
44 
45  if (inliers->indices.empty())
46  {
47  return std::nullopt;
48  }
49 
50  float a = coefficients->values.at(0);
51  float b = coefficients->values.at(1);
52  float c = coefficients->values.at(2);
53  float d = coefficients->values.at(3);
54  Eigen::Hyperplane3f plane(Eigen::Vector3f(a, b, c), d);
55 
56  // Assure plane normal points updwards.
57  if (plane.normal().dot(Eigen::Vector3f::UnitZ()) < 0)
58  {
59  plane = Eigen::Hyperplane3f(- plane.normal(), - plane.offset());
60  }
61 
62  return PlaneFittingResult { inliers, plane };
63  }
64 
65 }
visionx::tools
Definition: PCLUtilities.cpp:4
visionx::tools::fitPlaneRansac
std::optional< PlaneFittingResult > fitPlaneRansac(typename pcl::PointCloud< PointT >::Ptr cloud, double distanceThreshold=1.0, const IndicesPtrT &indices=nullptr)
Definition: plane_fitting_ransac.hpp:17
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::ctrlutil::a
double a(double t, double a0, double j)
Definition: CtrlUtil.h:45
pcl::graph::indices
pcl::PointIndices::Ptr indices(const PCG &g)
Retrieve the indices of the points of the point cloud stored in a point cloud graph that actually bel...
Definition: point_cloud_graph.h:737
plane_fitting_ransac.h
Eigen::Hyperplane3f
Hyperplane< float, 3 > Hyperplane3f
Definition: Elements.h:38
visionx::tools::PlaneFittingResult
Definition: plane_fitting_ransac.h:24