This example shows how to select a subset of a point cloud by sketching a rectangle or a lasso using the mouse.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
34 class PointCloud;
35}
36
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
51
52 void post_draw() override;
53
54
56
57private:
59};
60
61
62#endif
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
40
41
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) {
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
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
107
108
109 glEnable(GL_BLEND);
110 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
112 glDisable(GL_BLEND);
113 }
114}
115
116
117void TutorialPointSelection::mark_selection(
PointCloud *cloud) {
122 colors[v] = select[v] ?
vec3(1,0,0) : drawable->color().xyz();
124 drawable->update();
125}
126
127
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
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
46
47
48int main(int argc, char **argv) {
49
51
53
54
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");
65
66
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