Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_704_Cloud_DownSampling

Reducing a point cloud's size (downsampling) with two complementary strategies.

Overview

This interactive tutorial loads the fountain point cloud and offers two keyboard-triggered simplification steps: uniform downsampling to a target point count (Ctrl+U) and grid-based downsampling (Ctrl+G). Both shrink the cloud in place and refresh the rendering so the effect is immediately visible. Downsampling is a common preprocessing step to tame the massive clouds produced by LiDAR and photogrammetry.

The technique

The goal is to keep far fewer points while preserving the overall shape. Uniform simplification targets an explicit point count and removes points so the survivors stay roughly evenly spaced (it can also serve to drop near-duplicate points). Grid simplification instead overlays a regular 3D grid on the cloud's bounding box and keeps a single representative point per occupied cell — very fast, but the representative is chosen arbitrarily, so the result is non-uniform. A good cell size is tied to the data: here it is twice the average nearest-neighbor spacing, reported by average_space(). Both algorithms return the set of vertices to remove; the caller then deletes them, calls collect_garbage() to compact the storage, and updates the renderer so the portable geometry snapshots are re-extracted from the reduced cloud.

Easy3D API

Running it

Press Ctrl+U to uniformly downsample to half the current points (never fewer than 100), or Ctrl+G for grid downsampling with cell size = 2 x average spacing. The console reports how many points were removed and how many remain after each step.

1
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/point_cloud.h>
29#include <easy3d/renderer/camera.h>
30#include <easy3d/renderer/drawable_points.h>
31#include <easy3d/renderer/renderer.h>
32#include <easy3d/algo/point_cloud_simplification.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
82using namespace easy3d;
83
84namespace {
85
86 // Shared helper: delete the vertices the algorithm flagged, compact the cloud, and refresh
87 // the rendering buffers so the reduced cloud is what gets drawn.
88 void remove_points(PointCloud *cloud, const std::vector<PointCloud::Vertex> &to_remove) {
89 const auto old_num = cloud->n_vertices();
90 for (auto v : to_remove)
91 cloud->delete_vertex(v); // marks a vertex deleted (does not compact yet)
92 cloud->collect_garbage(); // actually removes deleted vertices and renumbers handles
93 std::cout << old_num - cloud->n_vertices() << " points removed, " << cloud->n_vertices() << " points remain"
94 << std::endl;
95 cloud->renderer()->update(); // refresh portable geometry snapshots from the smaller cloud
96 }
97
98 // Callback for Ctrl+U: uniformly reduce the cloud to half its current size (min 100 points).
99 bool uniform_downsample(Viewer *viewer, Model *model) {
100 auto cloud = dynamic_cast<PointCloud *>(model);
101 if (!cloud)
102 return false;
103 // Target count: half the points, but never below 100 so the cloud stays visible.
104 const auto target = std::max(100u, cloud->n_vertices() / 2);
105 remove_points(cloud, PointCloudSimplification::uniform_simplification(cloud, target));
106 viewer->update();
107 return true;
108 }
109
110 // Callback for Ctrl+G: keep one representative point per cell of a regular grid.
111 bool grid_downsample(Viewer *viewer, Model *model) {
112 auto cloud = dynamic_cast<PointCloud *>(model);
113 if (!cloud)
114 return false;
115 // Size the grid from the data: cell = 2 x the average nearest-neighbor spacing.
116 const float spacing = PointCloudSimplification::average_space(cloud);
117 remove_points(cloud, PointCloudSimplification::grid_simplification(cloud, spacing * 2.0f));
118 viewer->update();
119 return true;
120 }
121
122} // namespace
123
124int main(int argc, char **argv) {
125 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
126 initialize();
127
128 const std::string file = resource::directory() + "/data/fountain/pointcloud.ply";
129
130 // Create the viewer and load the fountain cloud; downcast the generic Model to PointCloud.
131 Viewer viewer(EXAMPLE_TITLE);
132 auto cloud = dynamic_cast<PointCloud *>(viewer.add_model(file, true));
133 if (!cloud) {
134 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
135 return EXIT_FAILURE;
136 }
137
138 // Orient the camera to view the scene from its front.
139 viewer.camera()->set_up_vector(vec3(0, 1, 0));
140 viewer.camera()->set_view_direction(vec3(0, 0, -1));
141
142 auto drawable = cloud->renderer()->get_points_drawable("vertices");
143 drawable->set_point_size(3.0f);
144
145 // Register the two strategies on Ctrl+U and Ctrl+G. The lambdas adapt the (Viewer*, Model*)
146 // callback signature expected by bind_shortcut() to our helper functions.
147 viewer.bind_shortcut([](Viewer *v, Model *m) { return uniform_downsample(v, m); }, cloud, Viewer::KEY_U, Viewer::MODIF_CTRL);
148 viewer.bind_shortcut([](Viewer *v, Model *m) { return grid_downsample(v, m); }, cloud, Viewer::KEY_G, Viewer::MODIF_CTRL);
149 // Show the shortcuts in the on-screen help overlay.
150 viewer.set_usage(
151 "default",
152 "Ctrl + u: uniform downsampling to half of the current points\n" "Ctrl + g: grid downsampling (cell size = 2 x average spacing)");
153
154 // Enter the event loop; returns when the window is closed.
155 return viewer.run();
156}
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
Renderer * renderer()
Definition model.cpp:66
A data structure for point clouds.
Definition point_cloud.h:46
void collect_garbage()
Remove deleted vertices.
Definition point_cloud.cpp:194
void delete_vertex(Vertex v)
Deletes the vertex v from the cloud.
Definition point_cloud.cpp:180
unsigned int n_vertices() const
Returns number of vertices in the cloud.
Definition point_cloud.h:423
void update()
Invalidates the rendering buffers of the model and thus updates the rendering (delayed in rendering).
Definition renderer.cpp:322
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
See also
PointCloudSimplification, PointCloud, Renderer