for (size_t i = 0; i < cloud->points.size(); ++i) { cloud->points[i].x=1024*rand()/(RAND_MAX+1.0f); cloud->points[i].y=1024*rand()/(RAND_MAX+1.0f); cloud->points[i].z=1024*rand()/(RAND_MAX+1.0f); }
pcl::ConditionAnd<pcl::PointXYZ>::Ptr range_cond(new pcl::ConditionAnd<pcl::PointXYZ>);//创建条件定义对象 //为此条件对象添加比较算子 range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZ>::ConstPtr (new pcl::FieldComparison<pcl::PointXYZ>("z",pcl::ComparisonOps::GT,0.0)));//添加在z字段上大于0的比较算子 range_cond->addComparison(pcl::FieldComparison<pcl::PointXYZ>::ConstPtr (new pcl::FieldComparison<pcl::PointXYZ>("z",pcl::ComparisonOps::LT,0.8)));//添加在z字段上小于0.8的比较算子
pcl::ConditionalRemoval<pcl::PointXYZ> condrem;//创建条件滤波器 condrem.setInputCloud(cloud);//设定输入点云 condrem.setCondition(range_cond);//设定条件 condrem.setKeepOrganized(false);//设定为true则保留点云的结构,否则不保留,默认为false condrem.filter(*cloud_filtered);//输出结果 } else{ std::cerr<<"Please specify common line arg '-r' or '-c'"<<std::endl; return 0; } std::cerr<<"Cloud before filtering: "<<std::endl; for (size_t i = 0; i < cloud->points.size(); ++i) { std::cerr<<" "<<cloud->points[i].x<<" " <<cloud->points[i].y<<" " <<cloud->points[i].z<<" " <<std::endl; } std::cerr<<"Cloud after filtering: "<<std::endl; for (size_t i = 0; i < cloud_filtered->points.size(); ++i) { std::cerr<<" "<<cloud_filtered->points[i].x<<" " <<cloud_filtered->points[i].y<<" " <<cloud_filtered->points[i].z<<" " <<std::endl; } return 0; }