Easy3D 2.6.1
Loading...
Searching...
No Matches
Tutorial_702_Cloud_SurfaceReconstruction/main.cpp

This example shows how to reconstruct a smooth surface from a point cloud using the Poisson surface reconstruction method.

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/model.h>
29#include <easy3d/renderer/drawable_triangles.h>
30#include <easy3d/renderer/renderer.h>
31#include <easy3d/algo/point_cloud_poisson_reconstruction.h>
32#include <easy3d/util/resource.h>
33#include <easy3d/util/initializer.h>
34
41
42using namespace easy3d;
43
44
45bool reconstruction(Viewer* viewer, Model* model) {
46 if (!viewer || !model)
47 return false;
48
49 auto cloud = dynamic_cast<PointCloud *>(model);
50 auto normals = cloud->get_vertex_property<vec3>("v:normal");
51 if (!normals) {
52 std::cerr << "Poisson surface reconstruction method requires normal information."
53 << " Please provide normal information. Alternatively, you can use the "
54 << " Tutorial_701_Cloud_NormalEstimation for normal estimation" << std::endl;
55 return false;
56 }
57
58 const int depth = 6;
60 algo.set_depth(depth);
61 std::cout << "reconstruction depth: " << depth << std::endl;
62 Model* surface = algo.apply(cloud);
63 if (surface != nullptr) {
64 viewer->add_model(std::shared_ptr<Model>(surface), true);
65 // setup rendering parameters
66 auto faces = surface->renderer()->get_triangles_drawable("faces");
67 faces->set_coloring_method(State::UNIFORM_COLOR);
68 viewer->delete_model(cloud);
69 viewer->update();
70 }
71
72 return true;
73}
74
75
76int main(int argc, char **argv) {
77 // initialize Easy3D.
78 initialize();
79
80 const std::string file = resource::directory() + "/data/polyhedron.bin";
81
82 // create the viewer.
83 Viewer viewer(EXAMPLE_TITLE);
84
85 Model *model = viewer.add_model(file, true);
86 if (!model) {
87 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
88 return EXIT_FAILURE;
89 }
90
91 // set up the function to be executed and its corresponding shortcut
92 viewer.bind(reconstruction, model, Viewer::KEY_R, Viewer::MODIF_CTRL);
93 // usage
94 viewer.set_usage("", "Ctrl + r: run reconstruction");
95
96 // run the viewer
97 return viewer.run();
98}
99
The base class of renderable 3D models.
Definition model.h:50
A data structure for point clouds.
Definition point_cloud.h:45
VertexProperty< T > get_vertex_property(const std::string &name) const
Get the vertex property named name of type T.
Definition point_cloud.h:513
Poisson surface reconstruction.
Definition point_cloud_poisson_reconstruction.h:43
void set_depth(int d)
Set reconstruction depth.
Definition point_cloud_poisson_reconstruction.h:62
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
@ UNIFORM_COLOR
Uniformly colored.
Definition state.h:58
The built-in Easy3D viewer.
Definition viewer.h:63
std::string directory()
Returns the resource directory (containing color maps, shaders, textures, fonts, etc....
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39