Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_702_Cloud_SurfaceReconstruction

Reconstructing a smooth surface mesh from an oriented point cloud (Poisson reconstruction).

Overview

This interactive tutorial loads the polyhedron point cloud (which already carries normals) and, on Ctrl+R, reconstructs a continuous surface from it. The point cloud is then replaced by the resulting triangle mesh in the viewer. It builds directly on Tutorial_701_Cloud_NormalEstimation: Poisson reconstruction needs consistently oriented normals as input.

The technique

Poisson surface reconstruction (Kazhdan, Bolitho, and Hoppe, "Poisson Surface Reconstruction", Symposium on Geometry Processing 2006) casts reconstruction as a spatial Poisson problem. The oriented normals define a vector field; the method finds an indicator function (1 inside the object, 0 outside) whose gradient best matches that field, then extracts an isosurface as a triangle mesh. It solves on an adaptive octree, so the "depth" parameter caps the resolution: depth d corresponds to a voxel grid no finer than 2^d x 2^d x 2^d. Higher depth captures more detail at greater memory/time cost. The result is a smooth, watertight SurfaceMesh.

Easy3D API

Running it

Press Ctrl+R to run the reconstruction (depth 6, printed to the console). The point cloud is removed and the reconstructed surface is shown in its place. If the cloud has no normals, an error message points you to Tutorial_701 for estimating them first.

1
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/point_cloud.h>
29#include <easy3d/core/model.h>
30#include <easy3d/core/surface_mesh.h>
31#include <easy3d/renderer/drawable_triangles.h>
32#include <easy3d/renderer/renderer.h>
33#include <easy3d/algo/point_cloud_poisson_reconstruction.h>
34#include <easy3d/util/resource.h>
35#include <easy3d/util/initializer.h>
36
78using namespace easy3d;
79
80// Callback for Ctrl+R: reconstruct a surface from the oriented point cloud.
81bool reconstruction(Viewer *viewer, Model *model) {
82 if (!viewer || !model)
83 return false;
84
85 auto cloud = dynamic_cast<PointCloud *>(model);
86 // Poisson reconstruction requires oriented normals; bail out with guidance if they are absent.
87 auto normals = cloud->get_vertex_property<vec3>("v:normal");
88 if (!normals) {
89 std::cerr << "Poisson surface reconstruction method requires normal information."
90 << " Please provide normal information. Alternatively, you can use the "
91 << " Tutorial_701_Cloud_NormalEstimation for normal estimation" << std::endl;
92 return false;
93 }
94
95 // Depth caps the octree resolution (a 2^6 = 64-wide voxel grid here). Higher = more detail.
96 const int depth = 6;
98 algo.set_depth(depth);
99 std::cout << "reconstruction depth: " << depth << std::endl;
100 // apply() solves the Poisson equation and extracts an isosurface as a new SurfaceMesh.
101 auto surface = algo.apply(cloud);
102 if (surface != nullptr) {
103 // Show the reconstructed mesh, then remove the original cloud.
104 viewer->add_model(surface, true);
105 // Draw the surface in a single uniform color.
106 auto faces = surface->renderer()->get_triangles_drawable("faces");
107 faces->set_coloring_method(State::UNIFORM_COLOR);
108 viewer->delete_model(cloud);
109 viewer->update(); // request a redraw of the new scene
110 }
111
112 return true;
113}
114
115int main(int argc, char **argv) {
116 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
117 initialize();
118
119 // This sample cloud already includes normals, so it is ready for reconstruction.
120 const std::string file = resource::directory() + "/data/polyhedron.bin";
121
122 // Create the viewer that will display the cloud and host the keyboard callback.
123 Viewer viewer(EXAMPLE_TITLE);
124
125 Model *model = viewer.add_model(file, true);
126 if (!model) {
127 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
128 return EXIT_FAILURE;
129 }
130
131 // Register the reconstruction step on Ctrl+R, bound to this model.
132 viewer.bind_shortcut(reconstruction, model, Viewer::KEY_R, Viewer::MODIF_CTRL);
133 // Show the shortcut in the on-screen help overlay.
134 viewer.set_usage("default", "Ctrl + r: run reconstruction");
135
136 // Enter the event loop; returns when the window is closed.
137 return viewer.run();
138}
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
A data structure for point clouds.
Definition point_cloud.h:46
VertexProperty< T > get_vertex_property(const std::string &name) const
Get the vertex property named name of type T.
Definition point_cloud.h:514
Poisson surface reconstruction.
Definition point_cloud_poisson_reconstruction.h:44
std::shared_ptr< SurfaceMesh > apply(const PointCloud *cloud, const std::string &density_attr_name="v:density") const
Perform Poisson surface reconstruction.
Definition point_cloud_poisson_reconstruction.cpp:193
void set_depth(int d)
Set reconstruction depth.
Definition point_cloud_poisson_reconstruction.h:63
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
See also
PoissonReconstruction, SurfaceMesh, Tutorial_701_Cloud_NormalEstimation