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
- PointCloudSimplification::uniform_simplification(cloud, num): returns vertices to delete to reach the target count num.
PointCloudSimplification::grid_simplification(cloud, cell_size): one survivor per grid cell. PointCloudSimplification::average_space(cloud): estimates the mean point spacing used to size the grid.
- PointCloud::delete_vertex(v) / collect_garbage(): remove points and compact the data structure.
- Renderer::update(): refresh portable geometry snapshots after the cloud changes.
- Viewer::bind_shortcut(...): registers the two callbacks on Ctrl+U and Ctrl+G.
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.
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>
88 void remove_points(
PointCloud *cloud,
const std::vector<PointCloud::Vertex> &to_remove) {
90 for (
auto v : to_remove)
93 std::cout << old_num - cloud->
n_vertices() <<
" points removed, " << cloud->
n_vertices() <<
" points remain"
99 bool uniform_downsample(
Viewer *viewer,
Model *model) {
100 auto cloud =
dynamic_cast<PointCloud *
>(model);
104 const auto target = std::max(100u, cloud->
n_vertices() / 2);
105 remove_points(cloud, PointCloudSimplification::uniform_simplification(cloud, target));
111 bool grid_downsample(
Viewer *viewer,
Model *model) {
112 auto cloud =
dynamic_cast<PointCloud *
>(model);
116 const float spacing = PointCloudSimplification::average_space(cloud);
117 remove_points(cloud, PointCloudSimplification::grid_simplification(cloud, spacing * 2.0f));
124int main(
int argc,
char **argv) {
128 const std::string file = resource::directory() +
"/data/fountain/pointcloud.ply";
131 Viewer viewer(EXAMPLE_TITLE);
132 auto cloud =
dynamic_cast<PointCloud *
>(viewer.add_model(file,
true));
134 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
139 viewer.camera()->set_up_vector(
vec3(0, 1, 0));
140 viewer.camera()->set_view_direction(
vec3(0, 0, -1));
142 auto drawable = cloud->renderer()->get_points_drawable(
"vertices");
143 drawable->set_point_size(3.0f);
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);
152 "Ctrl + u: uniform downsampling to half of the current points\n" "Ctrl + g: grid downsampling (cell size = 2 x average spacing)");
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