CGAL 读写.xyz格式的点云

一、概述

  下面的示例从输入文件读取点集并将其写入文件,两者都采用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.2287881个点的坐标为:-0.145242 -0.260515 -0.0066762个点的坐标为:-0.105662 -0.247177 0.1930063个点的坐标为:-0.146439 -0.10852 0.2001914个点的坐标为:-0.080971 -0.201227 0.2358875个点的坐标为:-0.046093 0.056768 0.3269146个点的坐标为:-0.060315 0.012698 0.3153177个点的坐标为:-0.123808 0.124865 0.2385658个点的坐标为:-0.193908 0.359075 0.0807259个点的坐标为:-0.286 0.252357 -0.12353810个点的坐标为:-0.246638 0.010038 -0.1591511个点的坐标为:0.105626 0.415783 0.06123812个点的坐标为:0.101478 0.139104 0.21169313个点的坐标为:0.072605 0.141517 0.21421214个点的坐标为:0.072546 0.317745 0.2315215个点的坐标为:0.112578 0.420875 0.25968916个点的坐标为:-0.031954 0.031205 0.2981917个点的坐标为:0.059409 0.033591 0.28508118个点的坐标为:0.180547 -0.024784 0.22085919个点的坐标为:0.092677 -0.141563 0.152956
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

点云侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值