CGAL 使用曲面拟合方法计算点云法向量

一、算法原理

1、主要函数

头文件

#include <CGAL/jet_estimate_normals.h>

函数

void CGAL::jet_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/jet_estimate_normals.h>    // jet计算法向量
#include <CGAL/mst_orient_normals.h>      // 最小生成树法线定向
#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)
	{
		// ---------------------使用固定近邻点来计算法线-----------------------------
		// 注意:jet_estimate_normals()需要一个范围的点以及属性映射来访问每个点的位置和法线。

		CGAL::jet_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::jet_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倍作为搜索半径

	}
	// ---------------------------------法线定向--------------------------------------
	//注意:mst_orient_normals()需要一个范围的点以及属性映射来访问每个点的位置和法线。
	auto unoriented_points_begin = CGAL::mst_orient_normals(points, nb_neighbors,
		CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>())
		.normal_map(CGAL::Second_of_pair_property_map<PointVectorPair>()));

	// 可选操作: 删除未进行法线定向的点
	//points.erase(unoriented_points_begin, points.end());
	// ---------------------------------结果保存--------------------------------------
	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;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
点云向量重定向是指通过CGAL(Computational Geometry Algorithms Library)库中的算法点云数据中的向量进行重新定向的过程。 在计算机图形学和计算几何学中,向量是表征一个平面、曲面或物体表面在某一点上垂直于该表面的向量。在点云数据中,每个点都有一个向量。但有时候,在某些应用中需要对向量进行重定向,以使它们在整个点云中更加一致和连续。 CGAL库为点云向量重定向提供了一些有用的算法。其中一个重要的算法线初始估计算,它可以估计点云中每个点的初始向量。该算法使用了一些几何特征的计算方法,例如曲率估计和线方向的一致性。 另一个重要的算法线重定向算法。该算法的目标是通过考虑点云的拓扑关系,使得点云中的向量更加一致和连续。线重定向算法使用了一种基于曲面平滑的优化方法,通过最小化向量之间的角度差异来调整每个点的向量使用CGAL库进行点云向量重定向需要先将点云数据加载到CGAL的数据结构中,例如点云网格或点云三角化。然后,可以调用库中的函数来执行线初始估计和线重定向算法。 通过点云向量重定向,可以使得点云中的向量更加一致,从而提高了许多点云处理任务的效果,例如表面重建、模型拟合和物体识别等。 总而言之,CGAL库提供了一些有效的算法来对点云数据中的向量进行重定向。通过这些算法,可以使得点云中的向量更加一致和连续,提高了点云处理任务的效果和精度。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

点云侠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值