CGAL 使用PCA计算点云法向量

一、算法原理

见:PCL 计算点云法向量并显示

1、主要函数

头文件

include <CGAL/pca_estimate_normals.h>

函数

void CGAL::pca_estimate_normals  ( PointRange &  points,  
  unsigned int  k,  
  const NamedParameters &  np = parameters::default_values()  
 ) 

  通过最近邻平面上的线性最小二乘拟合估计点范围的法方向。输出法线的方向是随机的。

  • points:点云
  • k: 邻域点数。
  • np:下面列出的命名参数中的一个可选序列。
    在这里插入图片描述

二、代码实现

#include <utility> // std::pair
#include <list>
#include <fstream>
#include <CGAL/property_map.h>      
#include <CGAL/IO/read_points.h>           // 读取点云
#include <CGAL/IO/write_points.h>          // 保存点云
#include <CGAL/pca_estimate_normals.h>    // pca计算法向量
#include <CGAL/compute_average_spacing.h> // 计算点云平均密度
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
// Types
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;

// 定义存储点和法线的容器
typedef std::pair<Kernel::Point_3, Kernel::Vector_3> PointVectorPair;

int main(int argc, char* argv[])
{
	const std::string fname = CGAL::data_file_path("cgal//sphere_1k.xyz");
	const char* output_filename = "cgal//pca_normal.xyz";
	// -----------------------------------读取点云------------------------------------
	std::list<PointVectorPair> points;
	if (!CGAL::IO::read_points(fname, std::back_inserter(points),
		CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>())))
	{
		std::cerr << "点云读取失败!!! " << fname << std::endl;
		return -1;
	}
	bool knn_search = true;
	const int nb_neighbors = 18; // K近邻搜索的邻域点数
	if (knn_search == true)
	{
		// ---------------------使用固定近邻点来计算法线-----------------------------
		///注意:pca_estimate_normals()需要一个范围的点以及属性映射来访问每个点的位置和法线。

		CGAL::pca_estimate_normals<CGAL::Parallel_if_available_tag>(points, nb_neighbors,
			CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>())
			.normal_map(CGAL::Second_of_pair_property_map<PointVectorPair>()));
	}
	else
	{
		double spacing = CGAL::compute_average_spacing<CGAL::Parallel_if_available_tag>(points, nb_neighbors,
			CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>()));
		// ---------------------使用固定半径进行法线计算-----------------------------
		CGAL::pca_estimate_normals<CGAL::Parallel_if_available_tag>
			(points,
				0, // 当使用邻域半径时,K=0表示对返回的邻域数量没有限制
				CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>())
				.normal_map(CGAL::Second_of_pair_property_map<PointVectorPair>())
				.neighbor_radius(2. * spacing)); // 使用平均间距的2倍作为搜索半径

	}
	// ---------------------------------结果保存--------------------------------------
	if (!CGAL::IO::write_XYZ(output_filename, points,
		CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>())
		.normal_map(CGAL::Second_of_pair_property_map<PointVectorPair>())
		.stream_precision(17)))
		return -1;

	return 0;
}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

点云侠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值