This example shows how to extract planes from a point cloud using RANSAC.
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>
45 if (!viewer || !model)
48 auto cloud =
dynamic_cast<PointCloud *
>(model);
51 std::cerr <<
"Plane extraction using RANSAC requires normal information but it is not available" << std::endl;
59 const int num = algo.
detect(cloud, 200, 0.005f, 0.02f, 0.8f, 0.001f);
61 std::cout << num <<
" primitives extracted" << std::endl;
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);
69 auto drawable = cloud->renderer()->get_points_drawable(
"vertices");
70 drawable->set_property_coloring(State::VERTEX, color_name);
79int main(
int argc,
char **argv) {
83 const std::string file = resource::directory() +
"/data/polyhedron.bin";
86 Viewer viewer(EXAMPLE_TITLE);
88 Model *model = viewer.add_model(file,
true);
90 LOG(ERROR) <<
"failed to load model. Please make sure the file exists and format is correct.";
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);
100 viewer.bind(extract_plane, model, Viewer::KEY_E, Viewer::MODIF_CTRL);
102 viewer.set_usage(
"",
"Ctrl + e: extract planes");
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
The built-in Easy3D viewer.
Definition viewer.h:63
Definition collider.cpp:182