• CGAL 计算点云平均密度


    一、主要函数

    头文件

    #include 
    
    • 1

    函数

    FT CGAL::compute_average_spacing  ( const PointRange &  points,  
      unsigned int  k,  
      const NamedParameters &  np = parameters::default_values()  
     ) 
    
    • 1
    • 2
    • 3
    • 4
    • points:输入点云
    • k:邻域点数,k需要大于2。
        函数compute_average_spacing()计算所有输入点到它们的k个最近邻点的平均间距,k由用户指定。该函数提供了一个点集密度的阶数,用于曲面重构管道的下游,自动确定曲面重构的输出网格尺寸等参数。

    二、代码实现

    1、版本一

    #include 
    #include 
    #include 
    #include  // 计算平均密度
    
    #include 
    typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
    
    
    int main()
    {
    	CGAL::Point_set_3<Kernel::Point_3> point_set;		//创建一个三维点云容器
    	if (!CGAL::IO::read_XYZ("cgal//kitten.xyz", point_set))
    	{
    		std::cerr << "点云读取失败\n" << std::endl;
    		return -1;
    	}
    	std::cout << "加载点云的点数:" << point_set.size() << std::endl;
    
    	int k = 6; // 进行密度计算的邻域点数
    	double spacing = CGAL::compute_average_spacing<CGAL::Sequential_tag>(point_set, k);
    
    	std::cout << "点云的平均密度为:" << spacing << std::endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    2、版本二

    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    
    // Types
    typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
    typedef Kernel::FT FT;
    typedef Kernel::Point_3 Point;
    
    // Data type := index, followed by the point, followed by three integers that
    // define the Red Green Blue color of the point.
    typedef boost::tuple<int, Point, int, int, int> IndexedPointWithColorTuple;
    
    // Concurrency
    typedef CGAL::Parallel_if_available_tag Concurrency_tag;
    
    int main(int argc, char* argv[])
    {
    	const std::string fname = (argc > 1) ? argv[1] : CGAL::data_file_path("points_3/sphere_20k.xyz");
    
    	// Reads a file in points.
    	// As the point is the second element of the tuple (that is with index 1)
    	// we use a property map that accesses the 1st element of the tuple.
    
    	std::vector<IndexedPointWithColorTuple> points;
    	if (!CGAL::IO::read_points(fname, std::back_inserter(points),
    		CGAL::parameters::point_map(CGAL::Nth_of_tuple_property_map<1, IndexedPointWithColorTuple>())))
    	{
    		std::cerr << "Error: cannot read file " << fname << std::endl;
    		return EXIT_FAILURE;
    	}
    
    	// Initialize index and RGB color fields in tuple.
    	// As the index and RGB color are respectively the first and third-fifth elements
    	// of the tuple we use a get function from the property map that accesses the 0
    	// and 2-4th elements of the tuple.
    	for (unsigned int i = 0; i < points.size(); i++)
    	{
    		points[i].get<0>() = i; // set index value of tuple to i
    
    		points[i].get<2>() = 0; // set RGB color to black
    		points[i].get<3>() = 0;
    		points[i].get<4>() = 0;
    	}
    
    	// Computes average spacing.
    	const unsigned int nb_neighbors = 6; // 1 ring
    	FT average_spacing = CGAL::compute_average_spacing<Concurrency_tag>(
    		points, nb_neighbors,
    		CGAL::parameters::point_map(CGAL::Nth_of_tuple_property_map<1, IndexedPointWithColorTuple>()));
    
    	std::cout << "Average spacing: " << average_spacing << std::endl;
    
    	return EXIT_SUCCESS;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    三、结果展示

    加载点云的点数:5210
    点云的平均密度为:0.0168572
    
    • 1
    • 2
  • 相关阅读:
    京东怎么上架产品,定时上下架教程
    架构师之路5. 浪潮LG - 离职
    【JavaWeb从入门到实战】MySQL进阶上篇之约束&数据库设计
    Basemap的投影
    反射Reflect
    transition和animation的区别?
    YZ系列工具之YZ13:VBA_过滤数据并行删除
    Double4 VR智能互动教学应用系统演示
    什么?你还不知道ERD Online要干什么
    【无标题】
  • 原文地址:https://blog.csdn.net/qq_36686437/article/details/126793215