一、概述
在CGAL 5.5版本中,grid_simplify_point_set()
中添加了一个新的可选命名参数min_points_per_cell
。通过在单元格中添加最小数量的点以便保留一个点,还可以过滤掉低密度区域和异常值:在密集采样点云的情况下,这比使用网格简化然后异常值去除产生更好的结果。
二、代码实现
#include <vector>
#include <fstream>
#include <CGAL/grid_simplify_point_set.h> // 体素滤波
#include <CGAL/IO/read_points.h>
#include <CGAL/IO/write_points.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
// types
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
int main(int argc, char* argv[])
{
// ------------------------------加载点云---------------------------------
std::vector<Kernel::Point_3> points;
if (CGAL::IO::read_points("cgal//CSite.xyz", std::back_inserter(points)) == -1)
{
std::cerr << "Error: cannot read file" << std::endl;
return -1;
}
std::cout << "从文件中读取" << points.size() << "个点。" << std::endl;
// ------------------------最小点约束的体素滤波--------------------------
double gridSize = 0.5; // 体素大小
unsigned int minPoints = 2;// 每个体素内要求的最小点数
auto iterator_to_first_to_remove
= CGAL::grid_simplify_point_set
(points, gridSize,
CGAL::parameters::min_points_per_cell(minPoints)); // optional
points.erase(iterator_to_first_to_remove, points.end());
CGAL::IO::write_points("cgal//CSite1_min_voxel.xyz", points, CGAL::parameters::stream_precision(14));
std::cout << "采样后的点:" << points.size() << std::endl;
// shrink_to_fit() 是 C++ STL 中的内置函数,它减少容器的容量以适应其大小并销毁超出容量的所有元素
points.shrink_to_fit();
return 0;
}
三、结果展示
1、原始点云
2、滤波后