void cloudCallback(const sensor_msgs::PointCloud2ConstPtr& cloud_msg) {
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
pcl::fromROSMsg(*cloud_msg, *cloud);
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud(cloud);
pass.setFilterFieldName("z");
pass.setFilterLimits(0.5, 1.5);
pcl::PointCloud<pcl::PointXYZ> cloud_filtered;
pass.filter(cloud_filtered);
// Publish cloud_filtered
}The passthrough filter keeps points within the specified limits on the chosen field. Here, it filters points on the 'z' axis between 0.5 and 1.5 meters.
The function pcl::fromROSMsg expects a dereferenced PointCloud2 message and a reference to a PCL point cloud. Option A correctly uses pointers and dereferences.
void cloudCallback(const sensor_msgs::PointCloud2ConstPtr& cloud_msg) {
pcl::PointCloud<pcl::PointXYZ> cloud;
pcl::fromROSMsg(*cloud_msg, cloud);
pcl::VoxelGrid<pcl::PointXYZ> sor;
sor.setInputCloud(cloud.makeShared());
sor.setLeafSize(0.1f, 0.1f, 0.1f);
pcl::PointCloud<pcl::PointXYZ> cloud_filtered;
sor.filter(cloud_filtered);
// Publish cloud_filtered
}makeShared() is a method of pcl::PointCloud pointers, not objects. Calling it on an object causes a crash.
The cube volume is 5x5x5 = 125 m³. With 0.5m leaf size, the number of voxels is (5/0.5)^3 = 10^3 = 1000. The filter reduces points to roughly one per voxel.
The tf library manages coordinate transforms, allowing point clouds from different frames to be aligned correctly.
