CGAL 使用Voronoi协方差估计点云的法线

一、算法原理

1、主要函数

头文件

#include <CGAL/vcm_estimate_normals.h>    // Voronoi协方差计算法向量

函数一

void CGAL::vcm_estimate_normals  ( PointRange &  points,  
  double  offset_radius,  
  double  convolution_radius,  
  const NamedParameters &  np = parameters::default_values()  
 )  

  使用Voronoi协方差测量和卷积半径估计点云的法线。输出法线的方向是随机的。

  • points:点云
  • offset_radius:offset_radius
  • convolution_radius:convolution_radius
  • k: 邻域点数。
  • np:下面列出的命名参数中的一个可选序列。
    在这里插入图片描述
    函数二
void CGAL::vcm_estimate_normals  ( PointRange &  points,  
  double  offset_radius,  
  unsigned int  k,  
  const NamedParameters &  np = parameters::default_values()  
 )  

  使用Voronoi协方差测量和卷积半径估计点云的法线。输出法线的方向是随机的。

  • points:点云
  • offset_radius:offset_radius
  • k: 进行convolution计算的邻域点数。
  • 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/vcm_estimate_normals.h>    // Voronoi协方差计算法向量
#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//kitten.xyz");
	const char* output_filename = "cgal//jet_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;
	}

	const int nb_neighbors = 18; // K近邻搜索的邻域点数
	// -------------使用Voronoi协方差测量和卷积半径估计点云的法线--------------------
	double  offset_radius = 0.01;
	double  convolution_radius = 0.02;
	CGAL::vcm_estimate_normals(points, offset_radius, convolution_radius,
		CGAL::parameters::point_map(CGAL::First_of_pair_property_map<PointVectorPair>())
		.normal_map(CGAL::Second_of_pair_property_map<PointVectorPair>()));
	// ---------------------------------结果保存--------------------------------------
	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(6)))
		return -1;

	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用CGAL和PCL库来进行带有法线点云数据的转换。下面是一个简单的示例代码,展示了如何将CGAL中的点云数据转换为PCL库中的PointNormal类型的点云数据: ```cpp #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> #include <CGAL/Point_with_normal_3.h> #include <CGAL/IO/read_xyz_points.h> #include <CGAL/property_map.h> #include <CGAL/IO/write_ply_points.h> #include <pcl/point_types.h> #include <pcl/io/pcd_io.h> #include <pcl/io/ply_io.h> #include <pcl/point_cloud.h> #include <pcl/features/normal_3d.h> typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; typedef Kernel::Point_3 Point_3; typedef CGAL::Point_with_normal_3<Kernel> Point_with_normal_3; typedef std::vector< Point_with_normal_3 > PointList; int main() { // 读取带有法线点云数据 PointList points; std::ifstream stream("input.xyz"); if (!stream || !CGAL::read_xyz_points(stream, std::back_inserter(points), CGAL::parameters::point_map(CGAL::First_of_pair_property_map<Point_with_normal_3>()). normal_map(CGAL::Second_of_pair_property_map<Point_with_normal_3>()))) { std::cerr << "Error: cannot read file!" << std::endl; return 1; } // 将点云数据转换为PCL库中的PointNormal类型的点云数据 pcl::PointCloud<pcl::PointNormal>::Ptr pcl_cloud(new pcl::PointCloud<pcl::PointNormal>); for (const auto& point : points) { pcl::PointNormal pcl_point; pcl_point.x = point.x(); pcl_point.y = point.y(); pcl_point.z = point.z(); pcl_point.normal_x = point.normal().x(); pcl_point.normal_y = point.normal().y(); pcl_point.normal_z = point.normal().z(); pcl_cloud->push_back(pcl_point); } // 保存PCL点云数据 pcl::io::savePLYFile("output.ply", *pcl_cloud); return 0; } ``` 上述代码将从"input.xyz"文件中读取带有法线点云数据,并将其转换为PCL库中的PointNormal类型的点云数据。最后,将转换后的点云数据保存为"output.ply"文件。你可以根据自己的需求修改文件路径和名称。 请确保已正确安装和配置CGAL和PCL库,并将相关头文件和库文件包含到你的项目中。这样,你就可以使用上述示例代码将CGAL中的点云数据转换为PCL库中的PointNormal类型的点云数据了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

点云侠

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

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

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

打赏作者

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

抵扣说明:

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

余额充值