三维图形知识分享-泊松(Poisson)重建详细源码

本文介绍了如何利用CGAL库中的Poisson重构算法从点云数据生成表面模型,包括代码示例和关键参数设置,以及计算重建误差的过程。
摘要由CSDN通过智能技术生成

泊松重建 (Poisson reconstruction) 是一种流行的点云表面重建算法,它通过求解一个泊松问题来构建点云数据的表面。

很多开源库都实现了经典的泊松重建算法,PCL点云处理库、CGAL等等有名的三维几何算法库,在实际使用中,CGAL库的实现泊松重建比较易懂,下面是一个完整的例子。 

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyhedron_3.h>
#include <CGAL/Surface_mesh_default_triangulation_3.h>
#include <CGAL/make_surface_mesh.h>
#include <CGAL/Implicit_surface_3.h>
#include <CGAL/IO/facets_in_complex_2_to_triangle_mesh.h>
#include <CGAL/Poisson_reconstruction_function.h>
#include <CGAL/property_map.h>
#include <CGAL/IO/read_points.h>
#include <CGAL/compute_average_spacing.h>
#include <CGAL/Polygon_mesh_processing/distance.h>
#include <boost/iterator/transform_iterator.hpp>
#include <vector>
#include <fstream>
// Types
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::FT FT;
typedef Kernel::Point_3 Point;
typedef Kernel::Vector_3 Vector;
typedef std::pair<Point, Vector> Point_with_normal;
typedef CGAL::First_of_pair_property_map<Point_with_normal> Point_map;
typedef CGAL::Second_of_pair_property_map<Point_with_normal> Normal_map;
typedef Kernel::Sphere_3 Sphere;
typedef std::vector<Point_with_normal> PointList;
typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
typedef CGAL::Poisson_reconstruction_function<Kernel> Poisson_reconstruction_function;
typedef CGAL::Surface_mesh_default_triangulation_3 STr;
typedef CGAL::Surface_mesh_complex_2_in_triangulation_3<STr> C2t3;
typedef CGAL::Implicit_surface_3<Kernel, Poisson_reconstruction_function> Surface_3;
int main(void)
{
    // Poisson options
    FT sm_angle = 20.0; // Min triangle angle in degrees.
    FT sm_radius = 30; // Max triangle size w.r.t. point set average spacing.
    FT sm_distance = 0.375; // Surface Approximation error w.r.t. point set average spacing.
    // Reads the point set file in points[].
    // Note: read_points() requires an iterator over points
    // + property maps to access each point's position and normal.
    PointList points;
    if(!CGAL::IO::read_points(CGAL::data_file_path("points_3/kitten.xyz"), std::back_inserter(points),
                          CGAL::parameters::point_map(Point_map())
                                           .normal_map (Normal_map())))
    {
      std::cerr << "Error: cannot read file input file!" << std::endl;
      return EXIT_FAILURE;
    }
    // Creates implicit function from the read points using the default solver.
    // Note: this method requires an iterator over points
    // + property maps to access each point's position and normal.
    Poisson_reconstruction_function function(points.begin(), points.end(), Point_map(), Normal_map());
    // Computes the Poisson indicator function f()
    // at each vertex of the triangulation.
    if ( ! function.compute_implicit_function() )
      return EXIT_FAILURE;
    // Computes average spacing
    FT average_spacing = CGAL::compute_average_spacing<CGAL::Sequential_tag>
      (points, 6 /* knn = 1 ring */,
       CGAL::parameters::point_map (Point_map()));
    // Gets one point inside the implicit surface
    // and computes implicit function bounding sphere radius.
    Point inner_point = function.get_inner_point();
    Sphere bsphere = function.bounding_sphere();
    FT radius = std::sqrt(bsphere.squared_radius());
    // Defines the implicit surface: requires defining a
    // conservative bounding sphere centered at inner point.
    FT sm_sphere_radius = 5.0 * radius;
    FT sm_dichotomy_error = sm_distance*average_spacing/1000.0; // Dichotomy error must be << sm_distance
    Surface_3 surface(function,
                      Sphere(inner_point,sm_sphere_radius*sm_sphere_radius),
                      sm_dichotomy_error/sm_sphere_radius);
    // Defines surface mesh generation criteria
    CGAL::Surface_mesh_default_criteria_3<STr> criteria(sm_angle,  // Min triangle angle (degrees)
                                                        sm_radius*average_spacing,  // Max triangle size
                                                        sm_distance*average_spacing); // Approximation error
    // Generates surface mesh with manifold option
    STr tr; // 3D Delaunay triangulation for surface mesh generation
    C2t3 c2t3(tr); // 2D complex in 3D Delaunay triangulation
    CGAL::make_surface_mesh(c2t3,                                 // reconstructed mesh
                            surface,                              // implicit surface
                            criteria,                             // meshing criteria
                            CGAL::Manifold_with_boundary_tag());  // require manifold mesh
    if(tr.number_of_vertices() == 0)
      return EXIT_FAILURE;
    // saves reconstructed surface mesh
    std::ofstream out("kitten_poisson-20-30-0.375.off");
    Polyhedron output_mesh;
    CGAL::facets_in_complex_2_to_triangle_mesh(c2t3, output_mesh);
    out << output_mesh;
    // computes the approximation error of the reconstruction
    double max_dist =
      CGAL::Polygon_mesh_processing::approximate_max_distance_to_point_set
      (output_mesh,
       CGAL::make_range (boost::make_transform_iterator
                         (points.begin(), CGAL::Property_map_to_unary_function<Point_map>()),
                         boost::make_transform_iterator
                         (points.end(), CGAL::Property_map_to_unary_function<Point_map>())),
       4000);
    std::cout << "Max distance to point_set: " << max_dist << std::endl;
    return EXIT_SUCCESS;
}

 下面是另一个简化版实现

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
三维泊松表面重建是一种常用的点云数据处理方法,可以将离散的点云数据重建成连续的三维表面模型。PCL(Point Cloud Library)是一个开源的点云处理库,提供了许多实用的算法和工具函数,包括三维泊松表面重建算法。 在PCL中使用三维泊松表面重建算法,你可以按照以下步骤进行操作: 1. 使用PCL加载点云数据:首先,你需要使用PCL库中的函数将点云数据加载到程序中。可以使用`pcl::io::loadPCDFile()`函数加载`.pcd`格式的点云文件,或者使用其他合适的函数加载其他格式的点云数据。 2. 对点云进行预处理:在进行泊松表面重建之前,有时需要对点云进行预处理,例如去除离群点、滤波等。PCL提供了许多预处理的方法,可以根据具体情况选择合适的方法进行处理。 3. 执行三维泊松表面重建:使用`pcl::Poisson`类可以进行三维泊松表面重建。你需要创建一个`pcl::Poisson`对象,并将预处理后的点云数据传递给它。然后,调用`performReconstruction()`函数执行重建过程。 4. 获取重建的三维模型:重建完成后,你可以使用`pcl::PolygonMesh`对象来获取重建的三维模型。可以使用`pcl::toPCLPointCloud2()`函数将重建的模型转换为点云格式,或者直接保存为`.ply`等格式的文件。 需要注意的是,三维泊松表面重建是一个计算密集型的算法,对于大规模的点云数据可能需要较长的运行时间。此外,泊松表面重建的结果可能受到输入点云的质量和密度等因素的影响,需要根据具体情况进行调整和优化。 希望以上信息对你有帮助!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

老猿的春天

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

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

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

打赏作者

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

抵扣说明:

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

余额充值