Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_402_FacePicker

Selecting an individual face of a surface mesh by clicking on it with the mouse.

Overview

This tutorial refines the model-level picking of Tutorial_401_ModelPicker down to a single face: clicking a face highlights it and prints its index. The loaded model (P.off) is a quad mesh, which makes the tutorial more interesting — the GPU can only rasterize triangles, so picking must map the hit triangle back to the original polygonal face.

The technique

Easy3D picks with a GPU primitive-ID pass (see Tutorial_401_ModelPicker): the scene is re-rendered off screen with each triangle writing an encoded index instead of a color, and the pixel under the cursor is read back. Viewer::pick() returns that triangle index and, when provenance is available, the original face index. The subtlety here is that a SurfaceMesh may contain non-triangular faces: for unified rendering, every polygon is internally triangulated, and the mesh stores a "f:triangle_range" face property mapping each original face to the [first, last] range of triangles it produced. Highlighting must therefore be expressed in triangle indices: we look up the picked face's triangle range and pass it to buffer::update_highlight_mask(), so the whole polygon lights up, not just the one triangle that happened to be under the cursor.

Easy3D API

Running it

Left-click a face to highlight it (the console prints "picked face f<index>"). Clicking another face moves the highlight; clicking the background clears it. Dragging still rotates the camera because unhandled events are forwarded to the base Viewer.

1
27#include "viewer.h"
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
30
83using namespace easy3d;
84
85int main(int argc, char **argv) {
86 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
87 initialize();
88
89 // A quad mesh: its faces are polygons, not triangles, so picking must map the hit
90 // triangle back to the original face (see the "f:triangle_range" discussion above).
91 const std::string file_name = resource::directory() + "/data/quad_mesh/P.off";
92
93 // Our custom viewer: a Viewer subclass that highlights the face under a left click.
94 TutorialFacePicker viewer(EXAMPLE_TITLE);
95
96 // This calls the overridden add_model(), which loads the mesh and makes its edges
97 // visible so the individual faces are easy to see and aim at.
98 if (!viewer.add_model(file_name)) {
99 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
100 return EXIT_FAILURE;
101 }
102
103 // Enter the interactive loop (returns when the window is closed).
104 return viewer.run();
105}
Definition collider.cpp:182

The header file of the viewer class:

1
27#ifndef EASY3D_TUTORIAL_FACE_PICKER_VIEWER_H
28#define EASY3D_TUTORIAL_FACE_PICKER_VIEWER_H
29
30#include <easy3d/viewer/viewer.h>
31
32// This class demonstrates how to pick faces in a surface mesh using the mouse.
33
34// Forward declaration only: the header just needs the Model name for the add_model()
35// signature, so we avoid pulling the full model header into every includer.
36namespace easy3d {
37 class Model;
38}
39
47class TutorialFacePicker : public easy3d::Viewer {
48public:
49 // Sets up a front view and installs the on-screen usage hint.
50 explicit TutorialFacePicker(const std::string &title);
51
52 // Loads a model via the base class, then makes its "edges" drawable visible.
53 easy3d::Model *add_model(const std::string &file_name, bool create_drawables = true,
54 bool fit_scene = true) override;
55
56protected:
57 // Intercepts plain left clicks to pick and highlight a face; forwards the rest to Viewer.
58 bool mouse_press_event(int x, int y, int button, int modifiers) override;
59};
60
61#endif // EASY3D_TUTORIAL_FACE_PICKER_VIEWER_H
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Model * add_model(const std::string &file_name, bool create_drawables=true, bool fit_scene=true) override
Add a model loaded from a file.
Definition viewer.cpp:1306

The source file of the viewer class:

1
27#include "viewer.h"
28#include <easy3d/renderer/camera.h>
29#include <easy3d/core/surface_mesh.h>
30#include <easy3d/renderer/drawable_lines.h>
31#include <easy3d/renderer/drawable_triangles.h>
32#include <easy3d/renderer/buffer.h>
33#include <easy3d/renderer/renderer.h>
34#include <easy3d/util/logging.h>
35
36using namespace easy3d;
37
38// The implementation is reproduced on the example page via \include in main.cpp, so we hide
39// it from Doxygen's separate symbol documentation to avoid documenting it twice.
40// \cond
41
42TutorialFacePicker::TutorialFacePicker(const std::string &title) : Viewer(title) {
43 // Look straight at the front of the mesh so faces are easy to aim at.
44 camera()->set_up_vector(vec3(0, 1, 0));
45 camera()->set_view_direction(vec3(0, 0, -1));
46
47 // hint_ is shown in the viewer HUD; it tells the user how to interact.
48 hint_ = "Click the left button on a face to pick it";
49}
50
51// A left click without modifiers picks a face; every other event is forwarded to the base
52// Viewer so camera navigation still works.
53bool TutorialFacePicker::mouse_press_event(int x, int y, int button, int modifiers) {
54 if (button == BUTTON_LEFT && modifiers == 0) {
55 // Mouse hooks and pick() both use logical window coordinates on both backends
56 // (DPI-independent), so the cursor can be passed straight through.
57 // GPU primitive-ID pick: returns the nearest model/drawable and the hit face index.
58 const auto hit = pick(x, y);
59 // Clear any previous highlight so only the newly picked face stays lit.
60 for (const auto &m : models())
61 for (const auto &d : m->renderer()->triangles_drawables())
62 d->update_highlight_mask({});
63 // We can only highlight a face if the hit model is a surface mesh rendered as triangles.
64 auto *model = dynamic_cast<SurfaceMesh *>(hit.model.get());
65 auto *drawable = dynamic_cast<TrianglesDrawable *>(hit.drawable.get());
66 if (model && drawable && hit.face_index >= 0) {
67 // Non-triangular faces are internally triangulated for rendering. This property
68 // maps each original face to the [first, last] range of triangles it produced.
69 const auto ranges = model->get_face_property<std::pair<int, int>>("f:triangle_range");
70 if (ranges) {
71 // Tint the whole triangle range so the entire polygon lights up, not
72 // just the single triangle that happened to be under the cursor.
73 const auto &range = ranges[SurfaceMesh::Face(hit.face_index)];
74 buffer::update_highlight_mask(drawable, range.first, range.second);
75 std::cout << "picked face f" << hit.face_index << std::endl;
76 }
77 }
78 // Request a redraw so the highlight change appears immediately.
79 update();
80 }
81 return Viewer::mouse_press_event(x, y, button, modifiers);
82}
83
84// Load the model through the base class, then tweak its appearance for face picking.
85Model *TutorialFacePicker::add_model(const std::string &file_name, bool create, bool fit_scene) {
86 auto model = Viewer::add_model(file_name, create, fit_scene);
87 if (!model)
88 return nullptr;
89
90 // make sure the edges are visible
91 // The second argument (false) suppresses a warning if the drawable is absent. Showing
92 // the edges draws the face boundaries, which makes individual faces easy to see.
93 if (auto edges = model->renderer()->get_lines_drawable("edges", false))
94 edges->set_visible(true);
95
96 return model;
97}
98
99// \endcond
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition drawable_triangles.h:46
Definition surface_mesh.h:191
See also
Viewer, SurfaceMesh, TrianglesDrawable, Tutorial_401_ModelPicker