Simulating a 3D scanner: capturing the currently visible surface of a model as a point cloud.
This tutorial turns the viewer into a virtual range scanner. The user navigates to a viewpoint and presses 'space'; everything visible from that viewpoint (and only that) is captured as a new PointCloud and added to the scene. Repeating this from different viewpoints mimics acquiring multiple scans of a real object, the raw input of a typical 3D reconstruction pipeline. An optional Gaussian noise mode imitates sensor noise.
A range scanner (LiDAR, structured light, depth camera) measures the distance to the nearest surface along many viewing directions. Rendering already computes exactly that: the depth buffer stores, for every pixel, the nearest visible surface point — hidden surfaces are removed by the depth test. So a virtual scan is simply: render the scene, read back the depth buffer, and back-project each valid pixel into world space. The math is the inverse of the usual rendering pipeline: a pixel (x, y) with depth d is mapped to normalized device coordinates (2(x+0.5)/w - 1, 1 - 2(y+0.5)/h, 2d - 1), multiplied by the inverse model-view-projection matrix, and divided by the resulting homogeneous w. The half-pixel offsets sample pixel centers. Pixels with no geometry (background) are skipped, so the scan contains only visible surface samples — one point per pixel, like a depth image. Adding zero-mean Gaussian noise with a small standard deviation simulates the measurement error of a real scanner.
Navigate with the mouse as usual. Press 'space' to capture a scan from the current view (a new model named "Virtual scan" appears); press 'n' to toggle Gaussian noise on the captured points. Scan from several viewpoints to build up multi-view coverage.
28#include <easy3d/core/point_cloud.h>
29#include <easy3d/renderer/camera.h>
30#include <easy3d/renderer/manipulated_camera_frame.h>
31#include <easy3d/renderer/clipping_plane.h>
33#include <easy3d/algo/gaussian_noise.h>
38TutorialVirtualScanner::TutorialVirtualScanner(
const std::string &title) :
Viewer(title) {
41 "-------------- Virtual Scanner usage -------------- \n"
42 "- change the view using the mouse.\n"
43 "- press the 'Space' key to perform scanning. Everything (and only those) visible\n"
44 " will be captured in a point cloud.\n"
45 "- press 'n' to toggle Gaussian noise.\n"
46 "--------------------------------------------------- \n";
48 "press 'Space' to perform scanning\n"
49 "press 'n' to toggle Gaussian noise";
55bool TutorialVirtualScanner::key_press_event(
int key,
int modifiers) {
56 if (modifiers == 0 && key == KEY_N) {
57 add_noise_ = !add_noise_;
60 if (modifiers == 0 && key == KEY_SPACE) {
63 framebuffer_size(w, h);
67 const auto inv =
inverse(camera()->model_view_projection_matrix());
71 const auto frame = capture(samples());
72 std::vector<vec3> points;
73 for (
unsigned y = 0; y < frame.height; ++y)
74 for (
unsigned x = 0; x < frame.width; ++x) {
75 const size_t pixel = size_t(y) * frame.width + x;
76 const float d = frame.depth[pixel];
78 if (frame.ids[pixel].object == UINT32_MAX || !std::isfinite(d) || d < 0 || d >= 1)
83 const vec4 p = inv *
vec4(2 * (
float(x) + .5f) / frame.width - 1,
84 1 - 2 * (
float(y) + .5f) / frame.height, 2 * d - 1, 1);
86 if (std::isfinite(p.w) && p.w != 0)
87 points.emplace_back(p.x / p.w, p.y / p.w, p.z / p.w);
92 auto cloud = std::make_shared<easy3d::PointCloud>();
93 cloud->set_name(
"Virtual scan");
94 for (
const auto &p : points)
101 add_model(cloud,
true,
false);
104 return Viewer::key_press_event(key, modifiers);
109std::vector<vec3> Viewer::scan_visible_points(
unsigned samples)
const {
110 if (!samples || samples > 64 || (samples & (samples - 1)))
111 throw std::invalid_argument(
"invalid capture sample count");
112 Camera snapshot(*camera());
115 snapshot.frame()->set_pivot_point(camera()->pivot_point());
116 snapshot.set_projection_matrix(camera()->projection_matrix());
117 snapshot.set_model_view_matrix(camera()->model_view_matrix());
118 const auto inv = transform::checked_inverse(snapshot.model_view_projection_matrix());
119 const auto frame = impl_->capture(snapshot,
background_color(),
unsigned(supported_sample_count(
int(samples))),
120 framebuffer_width(), framebuffer_height());
121 std::vector<vec3> points;
122 points.reserve(std::count_if(frame.depth.begin(), frame.depth.end(), [](
float d) { return d >= 0 && d < 1; }));
123 for (
unsigned y = 0; y < frame.height; ++y)
124 for (
unsigned x = 0; x < frame.width; ++x) {
125 const size_t pixel = size_t(y) * frame.width + x;
126 const float d = frame.depth[pixel];
127 if (frame.ids[pixel].object == UINT32_MAX || !std::isfinite(d) || d < 0 || d >= 1)
129 const int viewport[] = {0, 0, int(frame.width), int(frame.height)};
130 points.push_back(transform::unproject_inverse(
vec3(x + .5f, y + .5f, d), inv, viewport,
false));
A perspective or orthographic camera.
Definition camera.h:142
static void apply(SurfaceMesh *mesh, float sigma, bool preserve_boundary=false)
Add Gaussian noise (that has a normal distribution) to the surface mesh.
Definition gaussian_noise.cpp:41
EASY3D_UTIL_EXPORT vec4 background_color
Background color of the viewer.
Mat< N, N, T > inverse(const Mat< N, N, T > &m)
Returns the inverse of an N x N (square) matrix.
Definition mat.h:1210