一、概述
下面的示例从输入文件读取点集并将其写入文件,两者都采用XYZ格式。位置和法线成对存储,并通过属性映射访问。
二、代码实现
#include <iostream>
#include <utility> // std::pair
#include <vector>
#include <CGAL/property_map.h>
#include <CGAL/IO/read_xyz_points.h>
#include <CGAL/IO/write_xyz_points.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
// types
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
// 存储点坐标和法向量的pair容器
typedef std::pair<Kernel::Point_3, Kernel::Vector_3> Pwn;
int main(int argc, char* argv[])
{
const std::string fname = CGAL::data_file_path("cgal//oni.pwn");
// ------------------------从.xyz文件中读取点云------------------------------
std::vector<Pwn> points;
if (!CGAL::IO::read_XYZ(fname, std::back_inserter(points),
CGAL::parameters::point_map(CGAL::First_of_pair_property_map<Pwn>())
.normal_map(CGAL::Second_of_pair_property_map<Pwn>())))
{
std::cerr << "点云读取失败!!!" << fname << std::endl;
return -1;
}
int i = 0;
for (auto& p : points)
{
std::cout << "第" << i << "个点的坐标为:" << p.first << std::endl;
++i;
if (i == 20)
{
break;
}
}
// ------------------------保存点云到.xyz文件---------------------------------
if (!CGAL::IO::write_XYZ("cgal//oni_copy.xyz", points,
CGAL::parameters::point_map(CGAL::First_of_pair_property_map<Pwn>())
.normal_map(CGAL::Second_of_pair_property_map<Pwn>())
.stream_precision(8)))// 最后一个参数表示数据精度
return -1;
return 0;
}
三、结果展示
第0个点的坐标为:-0.120621 -0.048544 -0.228788
第1个点的坐标为:-0.145242 -0.260515 -0.006676
第2个点的坐标为:-0.105662 -0.247177 0.193006
第3个点的坐标为:-0.146439 -0.10852 0.200191
第4个点的坐标为:-0.080971 -0.201227 0.235887
第5个点的坐标为:-0.046093 0.056768 0.326914
第6个点的坐标为:-0.060315 0.012698 0.315317
第7个点的坐标为:-0.123808 0.124865 0.238565
第8个点的坐标为:-0.193908 0.359075 0.080725
第9个点的坐标为:-0.286 0.252357 -0.123538
第10个点的坐标为:-0.246638 0.010038 -0.15915
第11个点的坐标为:0.105626 0.415783 0.061238
第12个点的坐标为:0.101478 0.139104 0.211693
第13个点的坐标为:0.072605 0.141517 0.214212
第14个点的坐标为:0.072546 0.317745 0.23152
第15个点的坐标为:0.112578 0.420875 0.259689
第16个点的坐标为:-0.031954 0.031205 0.29819
第17个点的坐标为:0.059409 0.033591 0.285081
第18个点的坐标为:0.180547 -0.024784 0.220859
第19个点的坐标为:0.092677 -0.141563 0.152956