Search code examples
c++point-cloud-libraryransac

PCL: Accessing fields of pcl::ModelCoefficients::Ptr for line model approximation using RANSAC


I am trying to estimate a line through the points of a point cloud using a RANSAC method provided by the Point Cloud Library. I can create the object, and estimate the line model without a problem, as so:

pcl::PointCloud<pcl::PointXYZ>::ConstPtr source_cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::ModelCoefficients::Ptr line_coefficients(new pcl::ModelCoefficients);
pcl::PointIndices::Ptr inliers (new pcl::PointIndices);

// Populate point cloud...

// Create the segmentation object 
pcl::SACSegmentation<pcl::PointXYZ> seg;
seg.setModelType (pcl::SACMODEL_LINE);
seg.setMethodType (pcl::SAC_RANSAC);
seg.setDistanceThreshold (distance_thresh);
seg.setInputCloud (source_cloud); 
seg.segment (*inliers, *line_coefficients);

I now try to access the model parameters and I am simply too dumb to do it... according to the API there should be six accessible parameters:

The six coefficients of the line are given by a point on the line and the direction of the line as: [point_on_line.x point_on_line.y point_on_line.z line_direction.x line_direction.y line_direction.z]

I am therefore trying to access them as so:

line_coefficients->line_direction->x

However, this does not work. I keep getting the error:

No member named 'line_direction' in in 'pcl::ModelCoefficients'.

I don't really know what I'm doing wrong... anybody got any ideas? Thanks in advance!


Solution

  • The documentation is just telling you how the values are to be interpreted. pcl::ModelCoefficients is a struct which has a member values of type std::vector<float>.

    So to get the line_direction and point_on_line do:

    const auto pt_line_x = line_coefficients->values[0];
    const auto pt_line_y = line_coefficients->values[1];
    const auto pt_line_z = line_coefficients->values[2];
    const auto pt_direction_x = line_coefficients->values[3];
    const auto pt_direction_y = line_coefficients->values[4];
    const auto pt_direction_z = line_coefficients->values[5];