This example shows how to select a subset of a point cloud by sketching a rectangle or a lasso using the mouse.
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>
43TutorialPointSelection::TutorialPointSelection(
const std::string &title) :
Viewer(title) {
44 hint_ =
"Pressing Shift, drag the mouse to select (left) or deselect (right) points";
49bool TutorialPointSelection::mouse_press_event(
int x,
int y,
int button,
int modifiers) {
50 if (modifiers == MODIF_SHIFT) {
52 polygon_.push_back(
vec2(
static_cast<float>(x),
static_cast<float>(y)));
55 return Viewer::mouse_press_event(x, y, button, modifiers);
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());
67 picker.pick_vertices(cloud, polygon_, button == BUTTON_RIGHT);
69 picker.pick_vertices(cloud,
Rect(polygon_[0], polygon_[2]), button == BUTTON_RIGHT);
71 mark_selection(cloud);
78 return Viewer::mouse_release_event(x, y, button, modifiers);
83bool TutorialPointSelection::mouse_drag_event(
int x,
int y,
int dx,
int dy,
int button,
int modifiers) {
84 if (modifiers == MODIF_SHIFT) {
86 polygon_.push_back(
vec2(
static_cast<float>(x),
static_cast<float>(y)));
88 const vec2 first_point = polygon_[0];
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));
97 return Viewer::mouse_drag_event(x, y, dx, dy, button, modifiers);
101void TutorialPointSelection::post_draw() {
104 if (polygon_.size() >= 3) {
106 shape::draw_polygon_wire(polygon_,
vec4(1.0f, 0.0f, 0.0f, 1.0f), width(), height(), -1.0f);
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);
117void TutorialPointSelection::mark_selection(
PointCloud *cloud) {
121 for(
auto v : cloud->vertices())
122 colors[v] = select[v] ?
vec3(1,0,0) : drawable->color().xyz();
The GenericRect class defines a rectangle in the 2D space, where the origin of the coordinate system ...
Definition rect.h:45
Renderer * renderer()
Gets the renderer of this model.
Definition model.cpp:66
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
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition vec.h:30
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>
48int main(
int argc,
char **argv) {
52 const std::string file = resource::directory() +
"/data/polyhedron.bin";
55 TutorialPointSelection viewer(EXAMPLE_TITLE);
57 auto model = viewer.add_model(file,
true);
59 LOG(ERROR) <<
"failed to load model. Please make sure the file exists and format is correct.";
63 auto drawable = model->renderer()->get_points_drawable(
"vertices");
The drawable for rendering a set of points, e.g., point clouds, vertices of a mesh.
Definition drawable_points.h:42