Easy3D 2.6.1
Loading...
Searching...
No Matches
Tutorial_403_PointSelection/main.cpp

This example shows how to select a subset of a point cloud by sketching a rectangle or a lasso using the mouse.

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 TutorialPointSelection : public easy3d::Viewer
38{
39public:
40 explicit TutorialPointSelection(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 data structure for point clouds.
Definition point_cloud.h:45
The built-in Easy3D viewer.
Definition viewer.h:63
Definition collider.cpp:182
GenericPolygon< float > Polygon2
A 2D polygon of float type.
Definition types.h:116

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
41// \cond
42
43TutorialPointSelection::TutorialPointSelection(const std::string &title) : Viewer(title) {
44 hint_ = "Pressing Shift, drag the mouse to select (left) or deselect (right) points";
45}
46
47
49bool TutorialPointSelection::mouse_press_event(int x, int y, int button, int modifiers) {
50 if (modifiers == MODIF_SHIFT) {
51 polygon_.clear();
52 polygon_.push_back(vec2(static_cast<float>(x), static_cast<float>(y)));
53 return false;
54 } else
55 return Viewer::mouse_press_event(x, y, button, modifiers);
56}
57
58
60bool TutorialPointSelection::mouse_release_event(int x, int y, int button, int modifiers) {
61 if (modifiers == MODIF_SHIFT) {
62 if (polygon_.size() >= 3) {
63 auto cloud = dynamic_cast<PointCloud *>(current_model());
64 if (cloud) {
65 PointCloudPicker picker(camera());
66#if USE_LASSO
67 picker.pick_vertices(cloud, polygon_, button == BUTTON_RIGHT);
68#else
69 picker.pick_vertices(cloud, Rect(polygon_[0], polygon_[2]), button == BUTTON_RIGHT);
70#endif
71 mark_selection(cloud);
72
73 polygon_.clear();
74 }
75 }
76 return false;
77 } else
78 return Viewer::mouse_release_event(x, y, button, modifiers);
79}
80
81
83bool TutorialPointSelection::mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers) {
84 if (modifiers == MODIF_SHIFT) {
85#if USE_LASSO
86 polygon_.push_back(vec2(static_cast<float>(x), static_cast<float>(y)));
87#else // rectangle
88 const vec2 first_point = polygon_[0];
89 polygon_.clear();
90 polygon_.push_back(first_point);
91 polygon_.push_back(vec2(first_point.x, static_cast<float>(y)));
92 polygon_.push_back(vec2(static_cast<float>(x), static_cast<float>(y)));
93 polygon_.push_back(vec2(static_cast<float>(x), first_point.y));
94#endif
95 return false;
96 } else
97 return Viewer::mouse_drag_event(x, y, dx, dy, button, modifiers);
98}
99
100
101void TutorialPointSelection::post_draw() {
102 Viewer::post_draw();
103
104 if (polygon_.size() >= 3) {
105 // draw the boundary of the rect/lasso
106 shape::draw_polygon_wire(polygon_, vec4(1.0f, 0.0f, 0.0f, 1.0f), width(), height(), -1.0f);
107
108 // draw its transparent face
109 glEnable(GL_BLEND);
110 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
111 shape::draw_polygon_filled(polygon_, vec4(1.0f, 0.0f, 0.0f, 0.2f), width(), height(), -0.9f);
112 glDisable(GL_BLEND);
113 }
114}
115
116
117void TutorialPointSelection::mark_selection(PointCloud *cloud) {
118 auto drawable = cloud->renderer()->get_points_drawable("vertices");
119 auto select = cloud->vertex_property<bool>("v:select");
120 auto colors = cloud->vertex_property<vec3>("v:color");
121 for(auto v : cloud->vertices())
122 colors[v] = select[v] ? vec3(1,0,0) : drawable->color().xyz(); // mark selected points red
123 drawable->set_coloring(easy3d::State::COLOR_PROPERTY, easy3d::State::VERTEX, "v:color");
124 drawable->update();
125}
126
127// \endcond
Renderer * renderer()
Gets the renderer of this model.
Definition model.cpp:66
VertexContainer vertices() const
Returns vertex container for C++11 range-based for-loops.
Definition point_cloud.h:671
VertexProperty< T > vertex_property(const std::string &name, const T t=T())
Gets or adds a vertex property of type T with name name.
Definition point_cloud.h:539
Implementation of picking points from a point cloud.
Definition picker_point_cloud.h:44
PointsDrawable * get_points_drawable(const std::string &name, bool warning_not_found=true) const
Definition renderer.cpp:332
@ COLOR_PROPERTY
Using a color property.
Definition state.h:59
@ VERTEX
Property defined on vertices.
Definition state.h:69
void draw_polygon_wire(const Polygon2 &polygon, const vec4 &color, int width, int height, float depth)
Draws a polygon (line loop) in the screen space.
Definition shape.cpp:451
void draw_polygon_filled(const Polygon2 &polygon, const vec4 &color, int width, int height, float depth)
Draws a filled polygon in the screen space.
Definition shape.cpp:492
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
GenericRect< float > Rect
A 2D axis-aligned rectangle of float type.
Definition types.h:111
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46
Vec< 2, float > vec2
A 2D point/vector of float type.
Definition types.h:42
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
44
45using namespace easy3d;
46
47
48int main(int argc, char **argv) {
49 // initialize Easy3D.
50 initialize();
51
52 const std::string file = resource::directory() + "/data/polyhedron.bin";
53
54 // create the viewer.
55 TutorialPointSelection viewer(EXAMPLE_TITLE);
56
57 auto model = viewer.add_model(file, true);
58 if (!model) {
59 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
60 return EXIT_FAILURE;
61 }
62
63 auto drawable = model->renderer()->get_points_drawable("vertices");
64 dynamic_cast<PointsDrawable*>(drawable)->set_point_size(3.0f);
65
66 // run the viewer
67 return viewer.run();
68}
69
The drawable for rendering a set of points, e.g., point clouds, vertices of a mesh.
Definition drawable_points.h:42
std::string directory()
Returns the resource directory (containing color maps, shaders, textures, fonts, etc....
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39