Easy3D 2.5.3
Tutorial_404_VirtualScanner

The source file containing the main() function:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#include "viewer.h"
28#include <easy3d/core/model.h>
29#include <easy3d/renderer/renderer.h>
30#include <easy3d/renderer/drawable_points.h>
31#include <easy3d/util/resource.h>
32#include <easy3d/util/initializer.h>
33
34
35using namespace easy3d;
36
37// This example shows how to perform virtual scanning of a given model.
38
39
40int main(int argc, char **argv) {
41 // initialize Easy3D.
42 initialize();
43
44 // create the viewer.
45 VirtualScanner viewer(EXAMPLE_TITLE);
46
47 const std::string file_name = resource::directory() + "/data/house/house.obj";
48 Model *model = viewer.add_model(file_name, true);
49 if (!model) {
50 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
51 return EXIT_FAILURE;
52 }
53
54 auto d = model->renderer()->get_points_drawable("locks");
55 if (d)
56 d->set_visible(false);
57
58 // run the viewer
59 return viewer.run();
60}
61
The base class of renderable 3D models.
Definition: model.h:49
Renderer * renderer()
Gets the renderer of this model.
Definition: model.h:94
PointsDrawable * get_points_drawable(const std::string &name) const
Definition: renderer.cpp:286
Definition: collider.cpp:182
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35

The header file of the viewer class:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_TUTORIAL_VIRTUAL_SCANNER_H
28#define EASY3D_TUTORIAL_VIRTUAL_SCANNER_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33class VirtualScanner : public easy3d::Viewer
34{
35public:
36 explicit VirtualScanner(const std::string& title = "VirtualScanner");
37
38protected:
39 bool key_press_event(int key, int modifiers) override;
40
41private:
42 bool add_noise_;
43};
44
45
46#endif // EASY3D_TUTORIAL_VIRTUAL_SCANNER_H
The built-in Easy3D viewer.
Definition: viewer.h:61

The source file of the viewer class:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#include "viewer.h"
28#include <easy3d/core/point_cloud.h>
29#include <easy3d/renderer/camera.h>
30#include <easy3d/renderer/read_pixel.h>
31#include <easy3d/renderer/framebuffer_object.h>
32#include <easy3d/algo/gaussian_noise.h>
33
34
35using namespace easy3d;
36
37VirtualScanner::VirtualScanner(const std::string &title)
38 : Viewer(title)
39 , add_noise_(false)
40{
41 camera()->setUpVector(vec3(0, 1, 0));
42
43 usage_string_ =
44 "-------------- Virtual Scanner usage -------------- \n"
45 "- change the view using the mouse.\n"
46 "- press the 'Space' key to perform scanning. Everything (and only those) visible\n"
47 " will be captured in a point cloud.\n"
48 "- press 'n' to toggle Gaussian noise.\n"
49 "---------------------------------------------------------- \n";
50}
51
52
53bool VirtualScanner::key_press_event(int key, int modifiers) {
54 if (key == KEY_SPACE && modifiers == 0) {
55 int fw, fh;
56 framebuffer_size(fw, fh);
57
58 FramebufferObject fbo(fw, fh, 0);
59 fbo.add_depth_buffer(GL_DEPTH_COMPONENT32F);
60 fbo.bind();
61 glClearDepth(1.0f);
62 glClear(GL_DEPTH_BUFFER_BIT);
63 draw();
64 fbo.release();
65
66 std::vector<float> depths;
67 fbo.read_depth(depths, false);
68
69 const mat4& MVP = camera()->modelViewProjectionMatrix();
70 const mat4& invMVP = inverse(MVP);
71 const int viewport[] = { 0, 0, width_, height_};
72
73 std::vector<vec3> points;
74 for (int x=0; x<width_; ++x) {
75 for (int y=0; y<height_; ++y) {
76 // NOTE: when dealing with OpenGL, we always work in the highdpi screen space
77#if defined(__APPLE__)
78 const int idx = static_cast<int>(static_cast<float>(y) * dpi_scaling() * static_cast<float>(fw) + static_cast<float>(x) * dpi_scaling());
79#else
80 const int idx = static_cast<int>(y * fw + x);
81#endif
82 const float d = depths[idx];
83 if (d < 1.0f) {
84 vec3 vs(static_cast<float>(x), static_cast<float>(y), d);
85 vs.x = static_cast<float>(vs.x - static_cast<float>(viewport[0])) / static_cast<float>(viewport[2]) * 2.0f - 1.0f;
86 vs.y = static_cast<float>(vs.y - static_cast<float>(viewport[1])) / static_cast<float>(viewport[3]) * 2.0f - 1.0f;
87 vs.z = vs.z * 2.0f - 1.0f;
88 points.push_back(invMVP * vs);
89 }
90 }
91 }
92
93 if (!points.empty()) {
94 auto cloud = new PointCloud;
95 for (const auto& p : points)
96 cloud->add_vertex(p);
97
98 if (add_noise_) {
99 const float ratio = 0.0001f;
100 const float sigma = current_model()->bounding_box().radius() * ratio;
101 GaussianNoise::apply(cloud, sigma);
102 std::cout << "Gaussian noise added (sigma = " << ratio << " * model radius)" << std::endl;
103 }
104 add_model(cloud);
105 update();
106 }
107
108 return false;
109 }
110 else if (key == KEY_N && modifiers == 0) {
111 add_noise_ = !add_noise_;
112 if (add_noise_)
113 std::cout << "add_noise = ON" << std::endl;
114 else
115 std::cout << "add_noise = OFF" << std::endl;
116 return false;
117 }
118 else
119 return Viewer::key_press_event(key, modifiers);
120}
121
An implementation of framebuffer object (FBO).
Definition: framebuffer_object.h:122
A data structure for point clouds.
Definition: point_cloud.h:45
Vertex add_vertex(const vec3 &p)
add a new vertex with position p
Definition: point_cloud.cpp:177
virtual Model * add_model(const std::string &file_name, bool create_default_drawables=true)
Add a model from a file to the viewer to be visualized. On success, the viewer will be in charge of t...
Definition: viewer.cpp:1204
mat4 viewport(float w, float h)
Creates a viewport matrix. Simulating glViewport().
Definition: transform.cpp:128
Mat< N, N, T > inverse(const Mat< N, N, T > &m)
Return the inverse of N x N (square) matrix m.
Definition: mat.h:977