http://doc.cgal.org/latest/Surface_reconstruction_points_3/
The following example reads a point set, creates a Poisson implicit function and reconstructs a surface.
File Surface_reconstruction_points_3/poisson_reconstruction_example.cpp
1 #include <CGAL/trace.h> 2 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h> 3 #include <CGAL/Polyhedron_3.h> 4 #include <CGAL/IO/Polyhedron_iostream.h> 5 #include <CGAL/Surface_mesh_default_triangulation_3.h> 6 #include <CGAL/make_surface_mesh.h> 7 #include <CGAL/Implicit_surface_3.h> 8 #include <CGAL/IO/output_surface_facets_to_polyhedron.h> 9 #include <CGAL/Poisson_reconstruction_function.h> 10 #include <CGAL/Point_with_normal_3.h> 11 #include <CGAL/property_map.h> 12 #include <CGAL/IO/read_xyz_points.h> 13 #include <CGAL/compute_average_spacing.h> 14 #include <vector> 15 #include <fstream> 16 // Types 17 typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel; 18 typedef Kernel::FT FT; 19 typedef Kernel::Point_3 Point; 20 typedef CGAL::Point_with_normal_3<Kernel> Point_with_normal; 21 typedef Kernel::Sphere_3 Sphere; 22 typedef std::vector<Point_with_normal> PointList; 23 typedef CGAL::Polyhedron_3<Kernel> Polyhedron; 24 typedef CGAL::Poisson_reconstruction_function<Kernel> Poisson_reconstruction_function; 25 typedef CGAL::Surface_mesh_default_triangulation_3 STr; 26 typedef CGAL::Surface_mesh_complex_2_in_triangulation_3<STr> C2t3; 27 typedef CGAL::Implicit_surface_3<Kernel, Poisson_reconstruction_function> Surface_3; 28 int main(void) 29 { 30 // Poisson options 31 FT sm_angle = 20.0; // Min triangle angle in degrees. 32 FT sm_radius = 30; // Max triangle size w.r.t. point set average spacing. 33 FT sm_distance = 0.375; // Surface Approximation error w.r.t. point set average spacing. 34 // Reads the point set file in points[]. 35 // Note: read_xyz_points_and_normals() requires an iterator over points 36 // + property maps to access each point's position and normal. 37 // The position property map can be omitted here as we use iterators over Point_3 elements. 38 PointList points; 39 std::ifstream stream("data/kitten.xyz"); 40 if (!stream || 41 !CGAL::read_xyz_points_and_normals( 42 stream, 43 std::back_inserter(points), 44 CGAL::make_normal_of_point_with_normal_pmap(PointList::value_type()))) 45 { 46 std::cerr << "Error: cannot read file data/kitten.xyz" << std::endl; 47 return EXIT_FAILURE; 48 } 49 // Creates implicit function from the read points using the default solver. 50 // Note: this method requires an iterator over points 51 // + property maps to access each point's position and normal. 52 // The position property map can be omitted here as we use iterators over Point_3 elements. 53 Poisson_reconstruction_function function(points.begin(), points.end(), 54 CGAL::make_normal_of_point_with_normal_pmap(PointList::value_type()) ); 55 // Computes the Poisson indicator function f() 56 // at each vertex of the triangulation. 57 if ( ! function.compute_implicit_function() ) 58 return EXIT_FAILURE; 59 // Computes average spacing 60 FT average_spacing = CGAL::compute_average_spacing(points.begin(), points.end(), 61 6 /* knn = 1 ring */); 62 // Gets one point inside the implicit surface 63 // and computes implicit function bounding sphere radius. 64 Point inner_point = function.get_inner_point(); 65 Sphere bsphere = function.bounding_sphere(); 66 FT radius = std::sqrt(bsphere.squared_radius()); 67 // Defines the implicit surface: requires defining a 68 // conservative bounding sphere centered at inner point. 69 FT sm_sphere_radius = 5.0 * radius; 70 FT sm_dichotomy_error = sm_distance*average_spacing/1000.0; // Dichotomy error must be << sm_distance 71 Surface_3 surface(function, 72 Sphere(inner_point,sm_sphere_radius*sm_sphere_radius), 73 sm_dichotomy_error/sm_sphere_radius); 74 // Defines surface mesh generation criteria 75 CGAL::Surface_mesh_default_criteria_3<STr> criteria(sm_angle, // Min triangle angle (degrees) 76 sm_radius*average_spacing, // Max triangle size 77 sm_distance*average_spacing); // Approximation error 78 // Generates surface mesh with manifold option 79 STr tr; // 3D Delaunay triangulation for surface mesh generation 80 C2t3 c2t3(tr); // 2D complex in 3D Delaunay triangulation 81 CGAL::make_surface_mesh(c2t3, // reconstructed mesh 82 surface, // implicit surface 83 criteria, // meshing criteria 84 CGAL::Manifold_with_boundary_tag()); // require manifold mesh 85 if(tr.number_of_vertices() == 0) 86 return EXIT_FAILURE; 87 // saves reconstructed surface mesh 88 std::ofstream out("kitten_poisson-20-30-0.375.off"); 89 Polyhedron output_mesh; 90 CGAL::output_surface_facets_to_polyhedron(c2t3, output_mesh); 91 out << output_mesh; 92 return EXIT_SUCCESS; 93 }