title: pcl::RANSAC segmentation, get all planes in cloud?
body: <p>I have a Point Cloud Library function that detects the largest plane in a point cloud. This works great. Now, I would like to extend this functionality to segment out every planar surface in the cloud and copy those points to a new cloud (for example, a scene with a sphere on the floor of a room would give me back the floor and walls, but not the sphere, as it is not planar). How can I extend the below code to get all the planes, not just the largest one?
  (runtime is a factor here, so I would prefer not to just run this same code in a loop, stripping out the new largest plane each time)</p>
  <pre><code>int
  main(int argc, char** argv)
  {
      pcl::visualization::CloudViewer viewer(&quot;viewer1&quot;);
  
      pcl::PCLPointCloud2::Ptr cloud_blob(new pcl::PCLPointCloud2), cloud_filtered_blob(new pcl::PCLPointCloud2);
      pcl::PointCloud&lt;pcl::PointXYZ&gt;::Ptr cloud_filtered(new pcl::PointCloud&lt;pcl::PointXYZ&gt;), cloud_p(new pcl::PointCloud&lt;pcl::PointXYZ&gt;), cloud_f(new pcl::PointCloud&lt;pcl::PointXYZ&gt;);
  
      // Fill in the cloud data
      pcl::PCDReader reader;
      reader.read(&quot;clouds/table.pcd&quot;, *cloud_blob);
  
      // Create the filtering object: downsample the dataset using a leaf size of 1cm
      pcl::VoxelGrid&lt;pcl::PCLPointCloud2&gt; sor;
      sor.setInputCloud(cloud_blob);
      sor.setLeafSize(0.01f, 0.01f, 0.01f);
      sor.filter(*cloud_filtered_blob);
  
      // Convert to the templated PointCloud
      pcl::fromPCLPointCloud2(*cloud_filtered_blob, *cloud_filtered);
  
      std::cerr &lt;&lt; &quot;PointCloud after filtering: &quot; &lt;&lt; cloud_filtered-&gt;width * cloud_filtered-&gt;height &lt;&lt; &quot; data points.&quot; &lt;&lt; std::endl;
  
      pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients());
      pcl::PointIndices::Ptr inliers(new pcl::PointIndices());
      // Create the segmentation object
      pcl::SACSegmentation&lt;pcl::PointXYZ&gt; seg;
      // Optional
      seg.setOptimizeCoefficients(true);
      seg.setModelType(pcl::SACMODEL_PLANE);
      seg.setMethodType(pcl::SAC_RANSAC);
      seg.setMaxIterations(1000);
      seg.setDistanceThreshold(0.01);
  
      // Create the filtering object
      pcl::ExtractIndices&lt;pcl::PointXYZ&gt; extract;
  
      int i = 0, nr_points = (int)cloud_filtered-&gt;points.size();
      // While 30% of the original cloud is still there
      while (cloud_filtered-&gt;points.size() &gt; 0.3 * nr_points)
      {
          // Segment the largest planar component from the remaining cloud
          seg.setInputCloud(cloud_filtered);
          pcl::ScopeTime scopeTime(&quot;Test loop&quot;);
          {
              seg.segment(*inliers, *coefficients);
          }
          if (inliers-&gt;indices.size() == 0)
          {
              std::cerr &lt;&lt; &quot;Could not estimate a planar model for the given dataset.&quot; &lt;&lt; std::endl;
              break;
          }
  
          // Extract the inliers
          extract.setInputCloud(cloud_filtered);
          extract.setIndices(inliers);
          extract.setNegative(false);
          extract.filter(*cloud_p);
          std::cerr &lt;&lt; &quot;PointCloud representing the planar component: &quot; &lt;&lt; cloud_p-&gt;width * cloud_p-&gt;height &lt;&lt; &quot; data points.&quot; &lt;&lt; std::endl;
  
          }
  
      viewer.showCloud(cloud_p, &quot;viewer1&quot;);
      while (!viewer.wasStopped()) {}
  
      return (0);
  }
  </code></pre>