Easy3D 2.5.3
Tutorial_403_PointSelection

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/drawable_points.h>
30#include <easy3d/renderer/renderer.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 select a subset of a point cloud by sketching a rectangle or a lasso
38// using the mouse.
39
40int main(int argc, char **argv) {
41 // initialize Easy3D.
42 initialize();
43
44 const std::string file = resource::directory() + "/data/polyhedron.bin";
45
46 // create the viewer.
47 PointSelection viewer(EXAMPLE_TITLE);
48
49 Model *model = viewer.add_model(file, true);
50 if (!model) {
51 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
52 return EXIT_FAILURE;
53 }
54
55 auto drawable = model->renderer()->get_points_drawable("vertices");
56 dynamic_cast<PointsDrawable*>(drawable)->set_point_size(5.0f);
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
The drawable for rendering a set of points, e.g., point clouds, vertices of a mesh.
Definition: drawable_points.h:42
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_POINT_SELECTION_H
28#define EASY3D_TUTORIAL_POINT_SELECTION_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33namespace easy3d {
34 class PointCloud;
35}
36
37class PointSelection : public easy3d::Viewer
38{
39public:
40 explicit PointSelection(const std::string& title = "PointSelection");
41
42private:
44 bool mouse_press_event(int x, int y, int button, int modifiers) override;
46 bool mouse_release_event(int x, int y, int button, int modifiers) override;
48 bool mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers) override;
49
50 // This function will be called after the main draw procedure.
51 // It draws the axes of the coordinate system, the sketching rectangle/lasso, etc. overlaid on the scene.
52 void post_draw() override;
53
54 // highlight the selection
55 void mark_selection(easy3d::PointCloud* cloud);
56
57private:
58 easy3d::Polygon2 polygon_;
59};
60
61
62#endif // EASY3D_TUTORIAL_POINT_SELECTION_H
A 2D polygon representation.
Definition: polygon.h:41
A data structure for point clouds.
Definition: point_cloud.h:45
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/renderer/opengl.h>
29#include <easy3d/core/point_cloud.h>
30#include <easy3d/gui/picker_point_cloud.h>
31#include <easy3d/renderer/shape.h>
32#include <easy3d/renderer/drawable_points.h>
33#include <easy3d/renderer/renderer.h>
34#include <easy3d/util/logging.h>
35
36
37#define USE_LASSO 1
38
39using namespace easy3d;
40
41PointSelection::PointSelection(const std::string &title) : Viewer(title) {
42 usage_string_ =
43 "-------------- Point Selection usage -------------- \n"
44 "Press Ctrl key, then drag the mouse to select (left button) or deselect (right button) points\n"
45 "--------------------------------------------------- \n";
46}
47
48
50bool PointSelection::mouse_press_event(int x, int y, int button, int modifiers) {
51 if (modifiers == MODIF_CTRL) {
52 polygon_.clear();
53 polygon_.push_back(vec2(static_cast<float>(x), static_cast<float>(y)));
54 return false;
55 } else
56 return Viewer::mouse_press_event(x, y, button, modifiers);
57}
58
59
61bool PointSelection::mouse_release_event(int x, int y, int button, int modifiers) {
62 if (modifiers == MODIF_CTRL) {
63 if (polygon_.size() >= 3) {
64 auto cloud = dynamic_cast<PointCloud *>(current_model());
65 if (cloud) {
66 PointCloudPicker picker(camera());
67#if USE_LASSO
68 picker.pick_vertices(cloud, polygon_, button == BUTTON_RIGHT);
69#else
70 picker.pick_vertices(cloud, Rect(polygon_[0], polygon_[2]), button == BUTTON_RIGHT);
71#endif
72 mark_selection(cloud);
73
74 polygon_.clear();
75 }
76 }
77 return false;
78 } else
79 return Viewer::mouse_release_event(x, y, button, modifiers);
80}
81
82
84bool PointSelection::mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers) {
85 if (modifiers == MODIF_CTRL) {
86#if USE_LASSO
87 polygon_.push_back(vec2(static_cast<float>(x), static_cast<float>(y)));
88#else // rectangle
89 const vec2 first_point = polygon_[0];
90 polygon_.clear();
91 polygon_.push_back(first_point);
92 polygon_.push_back(vec2(first_point.x, static_cast<float>(y)));
93 polygon_.push_back(vec2(static_cast<float>(x), static_cast<float>(y)));
94 polygon_.push_back(vec2(static_cast<float>(x), first_point.y));
95#endif
96 return false;
97 } else
98 return Viewer::mouse_drag_event(x, y, dx, dy, button, modifiers);
99}
100
101
102void PointSelection::post_draw() {
103 if (polygon_.size() >= 3) {
104 // draw the boundary of the rect/lasso
105 shape::draw_polygon_wire(polygon_, vec4(1.0f, 0.0f, 0.0f, 1.0f), width(), height(), -1.0f);
106
107 // draw its transparent face
108 glEnable(GL_BLEND);
109 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
110 shape::draw_polygon_filled(polygon_, vec4(1.0f, 0.0f, 0.0f, 0.2f), width(), height(), -0.9f);
111 glDisable(GL_BLEND);
112 }
113}
114
115
116void PointSelection::mark_selection(PointCloud *cloud) {
117 auto drawable = cloud->renderer()->get_points_drawable("vertices");
118 auto select = cloud->vertex_property<bool>("v:select");
119 auto colors = cloud->vertex_property<vec3>("v:color");
120 for(auto v : cloud->vertices())
121 colors[v] = select[v] ? vec3(1,0,0) : drawable->color().xyz(); // mark selected points red
122 drawable->set_coloring(easy3d::State::COLOR_PROPERTY, easy3d::State::VERTEX, "v:color");
123 drawable->update();
124}
The GenericRect class defines a rectangle in the 2D space.
Definition: rect.h:42
VertexContainer vertices() const
returns vertex container for C++11 range-based for-loops
Definition: point_cloud.h:444
VertexProperty< T > vertex_property(const std::string &name, const T t=T())
if a vertex property of type T with name name exists, it is returned. otherwise this property is adde...
Definition: point_cloud.h:361
Implementation of picking points from a point cloud.
Definition: picker_point_cloud.h:44
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition: vec.h:34