Selecting a subset of a point cloud by sketching a rectangle or a lasso with the mouse.
This tutorial implements region selection on a point cloud: while holding Shift, the user drags out a rectangle (key 'R') or a free-form lasso (key 'L'), and every point projecting inside the region becomes selected (colored red). Dragging with the right button instead deselects. Where Tutorial_401/402 pick a single element under the cursor, this tutorial selects many elements at once — the standard "marquee/lasso" interaction of 3D editors.
Region selection asks a different question than picking: not "what is the nearest element
at this pixel?" but "which elements project inside this 2D region?". Note that this is a through-selection: points hidden behind other points are still selected, because selection is defined by projection, not by visibility. Testing every point against the polygon on the CPU would be O(n) per gesture with a serial point-in-polygon test; instead Easy3D evaluates all points on the GPU in parallel (a vertex-shader/compute pass): each point is transformed by the model-view-projection matrix, tested against the region and the clipping planes, and the answer is packed into a bitmask — one bit per point, 32 points per 32-bit word — so only a compact result is read back. The request carries the current selection as an input bitmask so a deselect gesture can clear bits, and the returned words become the new selection. While the gesture is in progress, the sketched region is drawn as a screen-space overlay: the lasso interior is triangulated with the Tessellator (a polygon may be concave or self-overlapping, so a simple fan would not do) and the outline is built from thin quads.
Hold Shift and drag with the left button to select points, or with the right button to deselect. Press 'L' for lasso mode (default), 'R' for rectangle mode, and Escape to cancel an ongoing gesture. Selected points turn red.
29#include <easy3d/core/point_cloud.h>
30#include <easy3d/renderer/camera.h>
31#include <easy3d/renderer/renderer.h>
32#include <easy3d/renderer/drawable_points.h>
33#include <easy3d/algo/tessellator.h>
40TutorialPointSelection::TutorialPointSelection(
const std::string &title) :
Viewer(title) {
43 "-------------- Point selection usage --------------\n"
44 "Shift + left/right drag: select/deselect points\n"
45 "L: lasso mode; R: rectangle mode; Escape: cancel\n"
46 "---------------------------------------------------\n";
48 "Pressing Shift, drag the mouse to select (left) or deselect (right) points\n"
49 "\tL: lasso mode (default)\n"
50 "\tR: rectangle mode";
56bool TutorialPointSelection::mouse_press_event(
int x,
int y,
int button,
int modifiers) {
57 if ((modifiers & MODIF_SHIFT) && (button == BUTTON_LEFT || button == BUTTON_RIGHT)) {
59 for (
const auto &model : models())
60 if (model.get() == current_model() && dynamic_cast<
easy3d::
PointCloud *>(model.get())) {
68 framebuffer_size(fw, fh);
69 polygon_.push_back(
easy3d::vec2(
float(x) * fw / camera()->screen_width(),
70 float(y) * fh / camera()->screen_height()));
75 return Viewer::mouse_press_event(x, y, button, modifiers);
79bool TutorialPointSelection::mouse_drag_event(
int x,
int y,
int dx,
int dy,
int button,
int modifiers) {
84 return Viewer::mouse_drag_event(x, y, dx, dy, button, modifiers);
89bool TutorialPointSelection::mouse_release_event(
int x,
int y,
int button,
int modifiers) {
91 return Viewer::mouse_release_event(x, y, button, modifiers);
93 if (button != button_)
97 const auto model = target_.lock();
98 if (model && std::find(models().begin(), models().end(), model) != models().end()) {
100 auto drawable = cloud->renderer()->get_points_drawable(
"vertices");
102 if (drawable && cloud->renderer()->is_visible() && drawable->is_visible()) {
106 framebuffer_size(fw, fh);
112 request.
points = point_resource(drawable);
114 request.
words.resize((
size_t(request.
points->source_count()) + 31) / 32, 0);
117 auto selected = cloud->vertex_property<
bool>(
"v:select");
118 for (
auto v : cloud->vertices())
120 request.words[v.idx() >> 5] |= 1u << (v.idx() & 31);
123 const auto transform = drawable->manipulated_matrix();
127 for (
const auto &plane : clipping_planes())
128 request.local_clipping_planes.push_back(
transpose(transform) * plane);
129 request.
deselect = button_ == BUTTON_RIGHT;
133 const auto words = select_points(request);
135 auto colors = cloud->vertex_property<
easy3d::vec3>(
"v:color");
136 for (
auto v : cloud->vertices()) {
137 selected[v] = (words[v.idx() >> 5] & (1u << (v.idx() & 31))) != 0;
138 colors[v] = selected[v] ?
easy3d::vec3(1, 0, 0) : drawable->color().xyz();
142 drawable->update_color_buffer(colors.vector());
150bool TutorialPointSelection::key_press_event(
int key,
int modifiers) {
151 if (key == KEY_ESCAPE && active_) {
155 if (modifiers == 0 && (key == KEY_R || key == KEY_L)) {
157 rectangle_ = key == KEY_R;
160 return Viewer::key_press_event(key, modifiers);
165bool TutorialPointSelection::focus_event(
bool focused) {
172void TutorialPointSelection::post_resize(
int,
int) {
178void TutorialPointSelection::pre_draw() {
181 if (!active_ || polygon_.size() < 3) {
192 for (
const auto &p : polygon_)
197 std::vector<easy3d::graphics::Vertex> fill, edge;
198 for (
const auto &triangle : tess.elements())
199 for (auto index : triangle) {
200 const auto *p = tess.
vertices()[index]->data();
206 for (
size_t i = 0, j = polygon_.size() - 1; i < polygon_.size(); j = i++) {
207 const auto a = polygon_[j], b = polygon_[i], d = b - a;
208 if (d.length2() < 1e-6f)
211 const auto n =
easy3d::vec2(-d.y, d.x) * (.75f / d.length());
212 const easy3d::vec2 corners[] = {a - n, b - n, b + n, a + n};
213 for (
unsigned k : {0u, 1u, 2u, 0u, 2u, 3u}) {
219 fill_ = fill.empty() ? nullptr : std::make_shared<easy3d::graphics::Geometry>(std::move(fill));
220 edge_ = edge.empty() ? nullptr : std::make_shared<easy3d::graphics::Geometry>(std::move(edge));
226 framebuffer_size(fw, fh);
227 std::vector<easy3d::graphics::DrawCommand> overlays;
228 for (
unsigned i = 0; i < 2; ++i) {
236 overlays.push_back(draw);
238 set_overlays(std::move(overlays));
244void TutorialPointSelection::extend(
int x,
int y) {
247 framebuffer_size(fw, fh);
248 const easy3d::vec2 p{float(x) * fw / camera()->screen_width(),
249 float(y) * fh / camera()->screen_height()};
252 const auto first = polygon_.front();
253 polygon_ = {first, {first.x, p.y}, p, {p.x, first.y}};
254 }
else if ((p - polygon_.back()).length2() >= 1)
256 polygon_.push_back(p);
262void TutorialPointSelection::cancel() {
A data structure for point clouds.
Definition point_cloud.h:46
@ COLOR_PROPERTY
Using a color property.
Definition state.h:59
@ VERTEX
Property defined on vertices.
Definition state.h:69
Tessellator subdivides concave planar polygons, polygons with holes, or polygons with intersecting ed...
Definition tessellator.h:55
void begin_polygon(const vec3 &normal)
Begin the tessellation of a complex polygon.
Definition tessellator.cpp:145
void end_polygon()
Finish tessellating the current polygon and append its elements to the result.
Definition tessellator.cpp:233
void begin_contour()
Begin a contour of the current polygon.
Definition tessellator.cpp:158
const std::vector< Vertex * > & vertices() const
Get the unique vertices accumulated since the last reset().
Definition tessellator.cpp:238
void end_contour()
Finish the current contour.
Definition tessellator.cpp:228
Mat< M, N, T > transpose(const Mat< N, M, T > &m)
Transposes a matrix.
Definition mat.h:1126
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
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
Complete backend-independent description of a draw operation.
Definition render_device.h:219
std::shared_ptr< const Geometry > geometry
Geometry snapshot to draw.
Definition render_device.h:220
mat4 model_view_projection
Model-to-clip transform in Easy3D/OpenGL convention.
Definition render_device.h:224
vec4 color
Uniform RGBA multiplier.
Definition render_device.h:226
Inputs for selection of point centers inside a screen polygon.
Definition render_device.h:500
mat4 model_view_projection
Transform from point-local coordinates to OpenGL clip coordinates.
Definition render_device.h:508
std::vector< std::uint32_t > words
Selection bitset, one bit per source slot.
Definition render_device.h:506
bool deselect
Clear bits inside the polygon instead of setting them.
Definition render_device.h:514
std::shared_ptr< const PointData > points
Immutable point positions and source-slot mapping.
Definition render_device.h:502
std::uint32_t height
Framebuffer height in pixels.
Definition render_device.h:512
std::vector< vec2 > polygon
Polygon in top-left framebuffer pixel coordinates.
Definition render_device.h:504
std::uint32_t width
Framebuffer width in pixels.
Definition render_device.h:510
Per-vertex data consumed by a draw command.
Definition render_device.h:91
vec3 position
Position in geometry-local coordinates.
Definition render_device.h:93