Easy3D 2.5.3
Tutorial_702_Cloud_SurfaceReconstruction
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_points.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
35
36using namespace easy3d;
37
38// This example shows how to
39// - reconstruct a smooth surface from a point cloud using the Poisson surface reconstruction method
40
41
42bool reconstruction(Viewer* viewer, Model* model) {
43 if (!viewer || !model)
44 return false;
45
46 auto cloud = dynamic_cast<PointCloud *>(model);
47 auto normals = cloud->get_vertex_property<vec3>("v:normal");
48 if (!normals) {
49 std::cerr << "Poisson surface reconstruction method requires normal information."
50 << " Please provide normal information. Alternatively, you can use the "
51 << " Tutorial_701_Cloud_NormalEstimation for normal estimation" << std::endl;
52 return false;
53 }
54
55 const int depth = 6;
57 algo.set_depth(depth);
58 std::cout << "reconstruction depth: " << depth << std::endl;
59 Model* surface = algo.apply(cloud);
60 if (surface != nullptr) {
61 viewer->add_model(surface, true);
62 viewer->delete_model(cloud);
63 viewer->update();
64 }
65
66 return true;
67}
68
69
70int main(int argc, char **argv) {
71 // initialize Easy3D.
72 initialize();
73
74 const std::string file = resource::directory() + "/data/polyhedron.bin";
75
76 // create the viewer.
77 Viewer viewer(EXAMPLE_TITLE);
78
79 Model *model = viewer.add_model(file, true);
80 if (!model) {
81 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
82 return EXIT_FAILURE;
83 }
84
85 // setup rendering parameters
86 auto drawable = model->renderer()->get_points_drawable("vertices");
87 drawable->set_uniform_coloring(vec4(0.6f, 0.6f, 1.0f, 1.0f));
88 drawable->set_point_size(3.0f);
89
90 // usage
91 viewer.set_usage("'Ctrl + r': run reconstruction");
92 // set up the function to be executed and its corresponding shortcut
93 viewer.bind(reconstruction, model, Viewer::KEY_R, Viewer::MODIF_CTRL);
94
95 // run the viewer
96 return viewer.run();
97}
98
The base class of renderable 3D models.
Definition: model.h:49
Renderer * renderer()
Gets the renderer of this model.
Definition: model.h:94
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. returns an invalid VertexProperty if the property does ...
Definition: point_cloud.h:340
Poisson surface reconstruction.
Definition: point_cloud_poisson_reconstruction.h:41
void set_depth(int d)
Set reconstruction depth. This integer is the maximum depth of the tree that will be used for surface...
Definition: point_cloud_poisson_reconstruction.h:53
SurfaceMesh * apply(const PointCloud *cloud, const std::string &density_attr_name="v:density") const
reconstruction
Definition: point_cloud_poisson_reconstruction.cpp:193
PointsDrawable * get_points_drawable(const std::string &name) const
Definition: renderer.cpp:286
void set_uniform_coloring(const vec4 &color)
Definition: state.cpp:89
The built-in Easy3D viewer.
Definition: viewer.h:61
bool delete_model(Model *model)
Delete a model. The memory of the model will be released and its existing drawables also be deleted.
Definition: viewer.cpp:1273
virtual Model * add_model(const std::string &file_name, bool create_default_drawables=true)
Add a model from a file to the viewer to be visualized. On success, the viewer will be in charge of t...
Definition: viewer.cpp:1204
void update() const
Update the display (i.e., repaint).
Definition: viewer.cpp:631
Definition: collider.cpp:182
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35