Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_703_Cloud_PlaneExtraction

Detecting planar regions in a point cloud with RANSAC and coloring each detected plane.

Overview

This interactive tutorial loads the polyhedron point cloud and, on Ctrl+E, extracts its planar faces automatically. Each detected plane is assigned a distinct color so the segmentation is visible. Like Tutorial_702, it requires per-point normals (see Tutorial_701 for estimating them).

The technique

RANSAC (RANdom SAmple Consensus; Fischler and Bolles, 1981) fits a model robustly in the presence of outliers: repeatedly draw a minimal random sample (three points define a plane), fit the candidate plane, and count how many points lie within a distance threshold (the "inliers"). The candidate with the most support wins; its inliers are removed and the search repeats for the next primitive. Easy3D implements the efficient variant of Schnabel et al. ("Efficient RANSAC for Point-Cloud Shape Detection", Computer Graphics Forum 2007), which accelerates inlier testing with a spatial bitmap and rejects candidates whose normals disagree with the samples. The detect() arguments here are: min_support = 200 points, distance threshold = 0.005, bitmap resolution = 0.02, normal compatibility threshold = 0.8, and overlook probability = 0.001.

Easy3D API

Running it

Press Ctrl+E to extract planes. The console reports how many primitives were found, and each planar region is drawn in its own color (points not on any detected plane stay black).

1
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/point_cloud.h>
29#include <easy3d/core/model.h>
30#include <easy3d/renderer/drawable_points.h>
31#include <easy3d/renderer/renderer.h>
32#include <easy3d/algo/point_cloud_ransac.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
78using namespace easy3d;
79
80// Callback for Ctrl+E: detect planes and color each one distinctly.
81bool extract_plane(Viewer *viewer, Model *model) {
82 if (!viewer || !model)
83 return false;
84
85 auto cloud = dynamic_cast<PointCloud *>(model);
86 // Efficient RANSAC uses normals to reject incompatible candidates, so they are required.
87 auto normals = cloud->get_vertex_property<vec3>("v:normal");
88 if (!normals) {
89 std::cerr << "Plane extraction using RANSAC requires normal information but it is not available" << std::endl;
90 return false;
91 }
92
93 // Configure the detector to look for planes (other types: sphere, cylinder, cone, torus).
95 algo.add_primitive_type(PrimitivesRansac::PLANE);
96
97 // Run detection. Arguments: min_support, distance threshold, bitmap resolution, normal
98 // threshold, overlook probability. You can try different values (usually no tuning is needed).
99 const int num = algo.detect(cloud, 200, 0.005f, 0.02f, 0.8f, 0.001f);
100 if (num > 0) {
101 std::cout << num << " primitives extracted" << std::endl;
102
103 // detect() labeled each point with its plane id in "v:primitive_index". Turn those integer
104 // labels into distinct RGB colors stored in a new "v:color-segments" vertex property.
105 auto segments = cloud->vertex_property<int>("v:primitive_index");
106 const std::string color_name = "v:color-segments";
107 auto coloring = cloud->vertex_property<vec3>(color_name, vec3(0, 0, 0));
108 Renderer::color_from_segmentation(cloud, segments, coloring);
109
110 // Tell the points drawable to color each point from the segmentation-color property.
111 auto drawable = cloud->renderer()->get_points_drawable("vertices");
112 drawable->set_property_coloring(State::VERTEX, color_name);
113
114 drawable->update(); // re-extract the color buffer for the GPU
115 viewer->update(); // request a redraw
116 }
117 return true;
118}
119
120int main(int argc, char **argv) {
121 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
122 initialize();
123
124 // This sample cloud already includes normals, so it is ready for RANSAC.
125 const std::string file = resource::directory() + "/data/polyhedron.bin";
126
127 // Create the viewer that will display the cloud and host the keyboard callback.
128 Viewer viewer(EXAMPLE_TITLE);
129
130 Model *model = viewer.add_model(file, true);
131 if (!model) {
132 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
133 return EXIT_FAILURE;
134 }
135
136 // Initial appearance: a uniform light-blue cloud (replaced by segment colors after detection).
137 auto drawable = model->renderer()->get_points_drawable("vertices");
138 drawable->set_uniform_coloring(vec4(0.6f, 0.6f, 1.0f, 1.0f));
139 drawable->set_point_size(3.0f);
140
141 // Register the plane-extraction step on Ctrl+E, bound to this model.
142 viewer.bind_shortcut(extract_plane, model, Viewer::KEY_E, Viewer::MODIF_CTRL);
143 // Show the shortcut in the on-screen help overlay.
144 viewer.set_usage("default", "Ctrl + e: extract planes");
145
146 // Enter the event loop; returns when the window is closed.
147 return viewer.run();
148}
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
A data structure for point clouds.
Definition point_cloud.h:46
VertexProperty< T > get_vertex_property(const std::string &name) const
Get the vertex property named name of type T.
Definition point_cloud.h:514
Extract primitives from point clouds using RANSAC.
Definition point_cloud_ransac.h:51
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
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
See also
PrimitivesRansac, Renderer, Tutorial_701_Cloud_NormalEstimation