Easy3D 2.5.3
Tutorial_703_Cloud_PlaneExtraction
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_ransac.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// - extract planes from a point cloud using RANSAC.
40
41
42bool extract_plane(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 << "Plane extraction using RANSAC requires normal information but it is not available" << std::endl;
50 return false;
51 }
52
54 algo.add_primitive_type(PrimitivesRansac::PLANE);
55
56 // you can try different parameters of RANSAC (usually you don't need to tune them)
57 const int num = algo.detect(cloud, 200, 0.005f, 0.02f, 0.8f, 0.001f);
58 if (num > 0) {
59 std::cout << num << " primitives extracted" << std::endl;
60
61 // assign each plane a unique color
62 auto segments = cloud->vertex_property<int>("v:primitive_index");
63 const std::string color_name = "v:color-segments";
64 auto coloring = cloud->vertex_property<vec3>(color_name, vec3(0, 0, 0));
65 Renderer::color_from_segmentation(cloud, segments, coloring);
66
67 auto drawable = cloud->renderer()->get_points_drawable("vertices");
68 drawable->set_property_coloring(State::VERTEX, color_name);
69
70 drawable->update();
71 viewer->update();
72 }
73 return true;
74}
75
76
77int main(int argc, char **argv) {
78 // initialize Easy3D.
79 initialize();
80
81 const std::string file = resource::directory() + "/data/polyhedron.bin";
82
83 // create the viewer.
84 Viewer viewer(EXAMPLE_TITLE);
85
86 Model *model = viewer.add_model(file, true);
87 if (!model) {
88 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
89 return EXIT_FAILURE;
90 }
91
92 // setup rendering parameters
93 auto drawable = model->renderer()->get_points_drawable("vertices");
94 drawable->set_uniform_coloring(vec4(0.6f, 0.6f, 1.0f, 1.0f));
95 drawable->set_point_size(3.0f);
96
97 // usage
98 viewer.set_usage("'Ctrl + e': extract planes");
99 // set up the function to be executed and its corresponding shortcut
100 viewer.bind(extract_plane, model, Viewer::KEY_E, Viewer::MODIF_CTRL);
101
102 // run the viewer
103 return viewer.run();
104}
105
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
Extract primitives from point clouds using RANSAC.Usage example:
Definition: point_cloud_ransac.h:48
int detect(PointCloud *cloud, unsigned int min_support=1000, float dist_threshold=0.005f, float bitmap_resolution=0.02f, float normal_threshold=0.8f, float overlook_probability=0.001f)
Extract primitives from a point cloud.
Definition: point_cloud_ransac.cpp:242
void add_primitive_type(PrimType t)
Setup the primitive types to be extracted.
Definition: point_cloud_ransac.cpp:232
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
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