# Construct a concave or convex hull polygon for a plane model

In this tutorial we will learn how to calculate a simple 2D hull polygon (concave or convex) for a set of points supported by a plane.

# The code

Then, create a file, letâ€™s say, `concave_hull_2d.cpp` or `convex_hull_2d.cpp` in your favorite editor and place the following inside:

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69``` ```#include #include #include #include #include #include #include #include #include int main (int argc, char** argv) { pcl::PointCloud::Ptr cloud (new pcl::PointCloud), cloud_filtered (new pcl::PointCloud), cloud_projected (new pcl::PointCloud); pcl::PCDReader reader; reader.read ("table_scene_mug_stereo_textured.pcd", *cloud); // Build a filter to remove spurious NaNs pcl::PassThrough pass; pass.setInputCloud (cloud); pass.setFilterFieldName ("z"); pass.setFilterLimits (0, 1.1); pass.filter (*cloud_filtered); std::cerr << "PointCloud after filtering has: " << cloud_filtered->points.size () << " data points." << std::endl; pcl::ModelCoefficients::Ptr coefficients (new pcl::ModelCoefficients); pcl::PointIndices::Ptr inliers (new pcl::PointIndices); // Create the segmentation object pcl::SACSegmentation seg; // Optional seg.setOptimizeCoefficients (true); // Mandatory seg.setModelType (pcl::SACMODEL_PLANE); seg.setMethodType (pcl::SAC_RANSAC); seg.setDistanceThreshold (0.01); seg.setInputCloud (cloud_filtered); seg.segment (*inliers, *coefficients); std::cerr << "PointCloud after segmentation has: " << inliers->indices.size () << " inliers." << std::endl; // Project the model inliers pcl::ProjectInliers proj; proj.setModelType (pcl::SACMODEL_PLANE); // proj.setIndices (inliers); proj.setInputCloud (cloud_filtered); proj.setModelCoefficients (coefficients); proj.filter (*cloud_projected); std::cerr << "PointCloud after projection has: " << cloud_projected->points.size () << " data points." << std::endl; // Create a Concave Hull representation of the projected inliers pcl::PointCloud::Ptr cloud_hull (new pcl::PointCloud); pcl::ConcaveHull chull; chull.setInputCloud (cloud_projected); chull.setAlpha (0.1); chull.reconstruct (*cloud_hull); std::cerr << "Concave hull has: " << cloud_hull->points.size () << " data points." << std::endl; pcl::PCDWriter writer; writer.write ("table_scene_mug_stereo_textured_hull.pcd", *cloud_hull, false); return (0); } ```

Note

This tutorial is written for assuming you are looking for the CONCAVE hull. If you would like the CONVEX hull for a plane model, just replace concave with convex at EVERY point in this tutorial, including the source file, file names and the CMakeLists.txt file. You will also need to comment out setAlpha(), as this is not applicable to convex hulls.

# The explanation

In the following lines of code, a segmentation object is created and some parameters are set. We use the SACMODEL_PLANE to segment this PointCloud, and the method used to find this model is SAC_RANSAC. The actual segmentation takes place when seg.segment (*inliers, *coefficients); is called. This function stores all of the inlying points (on the plane) to inliers, and it stores the coefficients to the plane (a * x + b * y + c * z = d) in coefficients.

```  pcl::PointIndices::Ptr inliers (new pcl::PointIndices);
// Create the segmentation object
pcl::SACSegmentation<pcl::PointXYZ> seg;
// Optional
seg.setOptimizeCoefficients (true);
// Mandatory
seg.setModelType (pcl::SACMODEL_PLANE);
seg.setMethodType (pcl::SAC_RANSAC);
seg.setDistanceThreshold (0.01);

seg.setInputCloud (cloud_filtered);
seg.segment (*inliers, *coefficients);
```

The next bit of code projects the inliers onto the plane model and creates another cloud. One way that we could do this is by just extracting the inliers that we found before, but in this case we are going to use the coefficients we found before. We set the model type we are looking for and then set the coefficients, and from that the object knows which points to project from cloud_filtered to cloud_projected.

```  pcl::ProjectInliers<pcl::PointXYZ> proj;
proj.setModelType (pcl::SACMODEL_PLANE);
// proj.setIndices (inliers);
proj.setInputCloud (cloud_filtered);
proj.setModelCoefficients (coefficients);
proj.filter (*cloud_projected);
```

The real interesting part is in the lines below, where the ConcaveHull object gets created and the reconstruction is performed:

```  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_hull (new pcl::PointCloud<pcl::PointXYZ>);
pcl::ConcaveHull<pcl::PointXYZ> chull;
chull.setInputCloud (cloud_projected);
chull.setAlpha (0.1);
chull.reconstruct (*cloud_hull);
```

# Compiling and running the program

 ``` 1 2 3 4 5 6 7 8 9 10 11 12``` ```cmake_minimum_required(VERSION 2.8 FATAL_ERROR) project(concave_hull_2d) find_package(PCL 1.2 REQUIRED) include_directories(\${PCL_INCLUDE_DIRS}) link_directories(\${PCL_LIBRARY_DIRS}) add_definitions(\${PCL_DEFINITIONS}) add_executable (concave_hull_2d concave_hull_2d.cpp) target_link_libraries (concave_hull_2d \${PCL_LIBRARIES}) ```

After you have made the executable, you can run it. Simply do:

```\$ ./concave_hull_2d
```

You will see something similar to:

```PointCloud after filtering has: 139656 data points.
PointCloud after segmentation has: 123727 inliers.
PointCloud after projection has: 139656 data points.
Concave hull has: 457 data points.
```