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
- PointCloudNormals::estimate(cloud, k): PCA normal estimation; stores the result in the "v:normal" vertex property. PointCloudNormals::reorient(cloud, k): makes the orientations consistent.
- PointCloud::get_vertex_property<vec3>("v:normal"): retrieves the computed normals.
- PointsDrawable::update_normal_buffer(...): copies the normals into the portable snapshot for shading.
- Viewer::bind_shortcut(func, model, key, modifier): registers a callback on a keyboard shortcut.
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.
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>
84 if (!viewer || !model)
88 auto cloud =
dynamic_cast<PointCloud *
>(model);
90 if (PointCloudNormals::estimate(cloud)) {
92 auto drawable = cloud->renderer()->get_points_drawable(
"vertices");
94 drawable->update_normal_buffer(normals.vector());
103 if (!viewer || !model)
106 auto cloud =
dynamic_cast<PointCloud *
>(model);
110 std::cerr <<
"normal information does not exist" << std::endl;
115 if (PointCloudNormals::reorient(cloud)) {
116 auto drawable = cloud->renderer()->get_points_drawable(
"vertices");
118 drawable->update_normal_buffer(normals.vector());
125int main(
int argc,
char **argv) {
129 const std::string file = resource::directory() +
"/data/bunny.bin";
132 Viewer viewer(EXAMPLE_TITLE);
135 auto model = viewer.add_model(file,
true);
137 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
141 viewer.camera()->set_up_vector(
vec3(0, 1, 0));
142 viewer.camera()->set_view_direction(
vec3(1, 0, 0));
145 auto drawable = model->renderer()->get_points_drawable(
"vertices");
146 drawable->set_uniform_coloring(
vec4(0.6f, 0.6f, 1.0f, 1.0f));
147 drawable->set_point_size(3.0f);
149 drawable->set_distinct_back_color(
true);
152 viewer.bind_shortcut(estimate, model, Viewer::KEY_E, Viewer::MODIF_CTRL);
153 viewer.bind_shortcut(reorient, model, Viewer::KEY_R, Viewer::MODIF_CTRL);
155 viewer.set_usage(
"default",
"Ctrl + e: estimate normals\n" "Ctrl + r: reorient normals");
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