Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_701_Cloud_NormalEstimation

Estimating per-point normals for a point cloud, then making their orientations consistent.

Overview

This interactive tutorial loads the bunny point cloud and adds two keyboard-triggered steps: estimate a normal at every point (Ctrl+E), then reorient the normals so they all point the same way (Ctrl+R). The resulting normals are copied into the drawable's portable snapshot so the points can be shaded. Normal estimation is a prerequisite for many downstream tasks, notably Poisson surface reconstruction (Tutorial_702_Cloud_SurfaceReconstruction) and RANSAC plane extraction (Tutorial_703).

The technique

A raw point cloud has no normals — only positions. To estimate one, we look at a point's k nearest neighbors and fit a local plane: the covariance matrix of the neighbor positions has three eigenvectors, and the one with the smallest eigenvalue (least variance) is perpendicular to the best-fit plane, i.e. the normal. This is Principal Component Analysis (PCA). PCA fixes the normal's axis but not its sign: each normal may point "in" or "out" arbitrarily. Reorientation resolves this by building a neighborhood graph and propagating a consistent flip assignment over a minimum spanning tree, so neighboring normals agree (Hoppe et al., "Surface reconstruction from unorganized points", SIGGRAPH 1992).

Easy3D API

Running it

Press Ctrl+E to estimate normals, then Ctrl+R to reorient them. Points use a distinct back color, so inconsistently oriented normals are visible as differently colored patches before reorientation.

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/renderer/camera.h>
33#include <easy3d/algo/point_cloud_normals.h>
34#include <easy3d/util/resource.h>
35#include <easy3d/util/initializer.h>
36
80using namespace easy3d;
81
82// Callback for Ctrl+E: estimate a normal at every point via PCA on its k nearest neighbors.
83bool estimate(Viewer *viewer, Model *model) {
84 if (!viewer || !model)
85 return false;
86
87 // The bound model is a PointCloud; downcast from the generic Model handle.
88 auto cloud = dynamic_cast<PointCloud *>(model);
89 // estimate() writes the per-point normals into the "v:normal" vertex property.
90 if (PointCloudNormals::estimate(cloud)) {
91 auto normals = cloud->get_vertex_property<vec3>("v:normal");
92 auto drawable = cloud->renderer()->get_points_drawable("vertices");
93 // Copy the vertex normals into the drawable's portable snapshot so the points can be shaded.
94 drawable->update_normal_buffer(normals.vector());
95 viewer->update(); // request a redraw to show the new shading
96 return true;
97 } else
98 return false;
99}
100
101// Callback for Ctrl+R: flip normals so their orientations are globally consistent.
102bool reorient(Viewer *viewer, Model *model) {
103 if (!viewer || !model)
104 return false;
105
106 auto cloud = dynamic_cast<PointCloud *>(model);
107 auto normals = cloud->get_vertex_property<vec3>("v:normal");
108 // Reorientation needs existing normals; tell the user to estimate them first.
109 if (!normals) {
110 std::cerr << "normal information does not exist" << std::endl;
111 return false;
112 }
113
114 // reorient() propagates a consistent sign over a minimum spanning tree of the neighborhood graph.
115 if (PointCloudNormals::reorient(cloud)) {
116 auto drawable = cloud->renderer()->get_points_drawable("vertices");
117 // Update the portable snapshot with the now consistently oriented normals.
118 drawable->update_normal_buffer(normals.vector());
119 viewer->update();
120 return true;
121 } else
122 return false;
123}
124
125int main(int argc, char **argv) {
126 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
127 initialize();
128
129 const std::string file = resource::directory() + "/data/bunny.bin";
130
131 // Create the viewer that will display the cloud and host the keyboard callbacks.
132 Viewer viewer(EXAMPLE_TITLE);
133
134 // Load the bunny point cloud; the viewer takes ownership (second argument = true).
135 auto model = viewer.add_model(file, true);
136 if (!model) {
137 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
138 return EXIT_FAILURE;
139 }
140 // Orient the camera to view the bunny from its front.
141 viewer.camera()->set_up_vector(vec3(0, 1, 0));
142 viewer.camera()->set_view_direction(vec3(1, 0, 0));
143
144 // Configure how the points are drawn.
145 auto drawable = model->renderer()->get_points_drawable("vertices");
146 drawable->set_uniform_coloring(vec4(0.6f, 0.6f, 1.0f, 1.0f)); // a single light-blue color
147 drawable->set_point_size(3.0f);
148 // Shade back-facing points differently so flipped normals are easy to spot.
149 drawable->set_distinct_back_color(true);
150
151 // Register the two processing steps on Ctrl+E and Ctrl+R, each bound to this model.
152 viewer.bind_shortcut(estimate, model, Viewer::KEY_E, Viewer::MODIF_CTRL);
153 viewer.bind_shortcut(reorient, model, Viewer::KEY_R, Viewer::MODIF_CTRL);
154 // Show the shortcuts in the on-screen help overlay.
155 viewer.set_usage("default", "Ctrl + e: estimate normals\n" "Ctrl + r: reorient normals");
156
157 // Enter the event loop; returns when the window is closed.
158 return viewer.run();
159}
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
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
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
See also
PointCloudNormals, PointCloud, PointsDrawable, Tutorial_702_Cloud_SurfaceReconstruction, Tutorial_703_Cloud_PlaneExtraction