ravi
ravi

Reputation: 6328

How to get all the points from point cloud residing on the surface of a given sphere

I have a point cloud consisting of PointXYZRGB. I have defined a sphere s in 3D space, hence following things are known-

  1. Center o of the sphere s as (x, y, z)
  2. Radius r of the sphere s

I want to get all points of this point cloud, which resides on the surface of given sphere.

std::vector<PointXYZRGB> getAllSurfacePoints(
                         pcl::PointCloud<pcl::PointXYZRGB> cloud){
}

Upvotes: 2

Views: 1549

Answers (1)

brad
brad

Reputation: 954

Try calling this function once to remove inner points and again to remove outer points.

pcl::PointCloud<pcl::PointXYZI>::Ptr 
passThroughFilterSphere(pcl::PointCloud<pcl::PointXYZI>::Ptr cloud, 
pcl::PointXYZI sphereCenterPoint, const double radius, bool remove_outside)
    {
        pcl::PointCloud<pcl::PointXYZI>::Ptr  filteredCloud(new pcl::PointCloud<pcl::PointXYZI>);
        float distanceFromSphereCenterPoint;
        bool pointIsWithinSphere;
        bool addPointToFilteredCloud;
        for (int point_i = 0; point_i < cloud->size(); ++point_i)
        {
            distanceFromSphereCenterPoint = pcl::euclideanDistance(cloud->at(point_i), sphereCenterPoint);
            pointIsWithinSphere = distanceFromSphereCenterPoint <= radius;
            addPointToFilteredCloud = (!pointIsWithinSphere && remove_outside) || (pointIsWithinSphere && !remove_outside);
            if (addPointToFilteredCloud){
                filteredCloud->push_back(cloud->at(point_i));
            }
        }
        return filteredCloud;
    }

Upvotes: 2

Related Questions