一、概述
随机删除用户指定的输入点的一部分。
1、主要函数
头文件
#include <CGAL/random_simplify_point_set.h> // 随机下采样
函数
PointRange::iterator CGAL::random_simplify_point_set ( PointRange & points,
double removed_percentage
)
-
removed_percentage
:删除掉的点所占比例。
二、代码实现
#include <iostream>
#include <CGAL/IO/read_points.h>
#include <CGAL/IO/write_points.h>
#include <CGAL/random_simplify_point_set.h> // 随机下采样
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
// types
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
int main(int argc, char*argv[])
{
const std::string fname = CGAL::data_file_path("cgal//points_3//CSite.xyz");
const std::string output_filename("cgal//CSite_random.xyz");
// -----------------------------加载点云-------------------------------
std::vector<Kernel::Point_3> points;
if(!CGAL::IO::read_points(fname, std::back_inserter(points)))
{
std::cerr << "Error: cannot read file " << fname << std::endl;
return -1;
}
std::cout << "从点云数据中读取:" << points.size() << "个点。" << std::endl;
// ----------------------------随机下采样------------------------------
const double r_p = 30.0; // 删除掉的点所占比例
points.erase(CGAL::random_simplify_point_set(points, r_p), points.end());
// Optional: after erase(), use Scott Meyer's "swap trick" to trim excess capacity
std::vector<Kernel::Point_3>(points).swap(points);
std::cout << "下采样之后还有:" << points.size() << "个点。" << std::endl;
// -----------------------------保存点云-------------------------------
if(!CGAL::IO::write_points(output_filename, points, CGAL::parameters::stream_precision(17)))
return -1;
return 0;
}
三、结果展示
从点云数据中读取:1366408个点。
下采样之后还有:956485个点。
1、原始点云
![在这里插入图片描述](https://img-blog.csdnimg.cn/fa99e2bd172a4e7790e1a30e609fe65e.png#pic_center)
2、下采样后的点云
![在这里插入图片描述](https://img-blog.csdnimg.cn/ae3c99c4d46d449baa9d279fe7608374.png#pic_center)