This example shows how to extract planes from a point cloud using RANSAC.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
42
43
45 if (!viewer || !model)
46 return false;
47
48 auto cloud =
dynamic_cast<PointCloud *
>(model);
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
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
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));
68
69 auto drawable = cloud->renderer()->get_points_drawable("vertices");
71
72 drawable->update();
73 viewer->update();
74 }
75 return true;
76}
77
78
79int main(int argc, char **argv) {
80
82
84
85
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
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
100 viewer.bind(extract_plane, model, Viewer::KEY_E, Viewer::MODIF_CTRL);
101
102 viewer.set_usage("","Ctrl + e: extract planes");
103
104
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