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

This example shows how to extract planes from a point cloud using RANSAC.

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
40
41using namespace easy3d;
42
43
44bool extract_plane(Viewer* viewer, Model* model) {
45 if (!viewer || !model)
46 return false;
47
48 auto cloud = dynamic_cast<PointCloud *>(model);
49 auto normals = cloud->get_vertex_property<vec3>("v:normal");
50 if (!normals) {
51 std::cerr << "Plane extraction using RANSAC requires normal information but it is not available" << std::endl;
52 return false;
53 }
54
57
58 // you can try different parameters of RANSAC (usually you don't need to tune them)
59 const int num = algo.detect(cloud, 200, 0.005f, 0.02f, 0.8f, 0.001f);
60 if (num > 0) {
61 std::cout << num << " primitives extracted" << std::endl;
62
63 // assign each plane a unique color
64 auto segments = cloud->vertex_property<int>("v:primitive_index");
65 const std::string color_name = "v:color-segments";
66 auto coloring = cloud->vertex_property<vec3>(color_name, vec3(0, 0, 0));
67 Renderer::color_from_segmentation(cloud, segments, coloring);
68
69 auto drawable = cloud->renderer()->get_points_drawable("vertices");
70 drawable->set_property_coloring(State::VERTEX, color_name);
71
72 drawable->update();
73 viewer->update();
74 }
75 return true;
76}
77
78
79int main(int argc, char **argv) {
80 // initialize Easy3D.
81 initialize();
82
83 const std::string file = resource::directory() + "/data/polyhedron.bin";
84
85 // create the viewer.
86 Viewer viewer(EXAMPLE_TITLE);
87
88 Model *model = viewer.add_model(file, true);
89 if (!model) {
90 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
91 return EXIT_FAILURE;
92 }
93
94 // setup rendering parameters
95 auto drawable = model->renderer()->get_points_drawable("vertices");
96 drawable->set_uniform_coloring(vec4(0.6f, 0.6f, 1.0f, 1.0f));
97 drawable->set_point_size(3.0f);
98
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 // usage
102 viewer.set_usage("","Ctrl + e: extract planes");
103
104 // run the viewer
105 return viewer.run();
106}
107
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
Extract primitives from point clouds using RANSAC.Usage example:
Definition point_cloud_ransac.h:52
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)
Set up the primitive types to be extracted.
Definition point_cloud_ransac.cpp:232
@ PLANE
Plane primitive.
Definition point_cloud_ransac.h:56
static void color_from_segmentation(SurfaceMesh *mesh, SurfaceMesh::FaceProperty< FT > segments, SurfaceMesh::FaceProperty< vec3 > colors)
Generates random colors for visualizing face-based segmentation of a SurfaceMesh.
Definition renderer.h:299
@ VERTEX
Property defined on vertices.
Definition state.h:69
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
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46