Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_307_CrossSection

Inspecting a model's interior with an interactively movable clipping plane and cross-sections.

Overview

This tutorial shows how to cut a model open while rendering it: everything on one side of a plane is hidden, so you can look inside the geometry. Easy3D ships a ready-made, manipulable ClippingPlane; the custom CrossSection viewer below simply enables it and re-routes mouse drags so the plane can be moved directly (without holding a modifier key). The entry point loads the Stanford bunny and picks a viewing direction that makes the cut easy to see.

The technique

A plane splits space into two half-spaces: for a plane with normal n and offset d, the sign of dot(n, p) + d tells on which side a point p lies. Clipping evaluates this test per fragment in the shader and discards fragments on the forbidden side — the mesh itself is never modified, so the cut can be moved interactively at no geometry-processing cost. A cross-section generalizes this to a thin slab between two parallel planes (plane0() and plane1() in ClippingPlane): only fragments inside the slab survive, revealing a slice of the model, like a CT scan slice. The slab thickness is defined relative to the scene size. A translucent guide quad is drawn to show where the plane is, and a ManipulatedFrame (a local coordinate frame with rotate/translate actions driven by mouse deltas) lets the user reorient and slide the plane through the model.

Easy3D API

Running it

Drag with the left button to rotate the clipping plane, drag with the right button to translate it. Alt+'c' enables/disables clipping, Alt+'v' shows/hides the plane guide, and Alt+'x' toggles cross-section (slab) mode.

1
27#include <easy3d/renderer/camera.h>
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
30
31#include "viewer.h"
32
83using namespace easy3d;
84
85int main(int argc, char **argv) {
86 // initialize Easy3D.
87 initialize();
88
89 // The bunny is a closed surface, so clipping it reveals a hollow interior — a good
90 // demonstration of what the plane cuts away.
91 const std::string file_name = resource::directory() + "/data/bunny.ply";
92 // Our viewer subclass enables the clipping plane in its constructor.
93 CrossSection viewer(EXAMPLE_TITLE);
94
95 // Look at the bunny from an oblique direction so the cut surface is visible.
96 viewer.camera()->set_view_direction(vec3(-1, 1, -1));
97
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_CROSS_SECTION_H
28#define EASY3D_TUTORIAL_CROSS_SECTION_H
29
30#include "easy3d/viewer/viewer.h"
31
32
41class CrossSection : public easy3d::Viewer {
42public:
43 // Enables the clipping plane and installs the on-screen key hints.
44 CrossSection(const std::string &title);
45
46protected:
47 // Left drag rotates the plane, right drag translates it; other buttons keep the
48 // default viewer behavior (see viewer.cpp).
49 bool mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers) override;
50};
51
52
53#endif // EASY3D_TUTORIAL_CROSS_SECTION_H
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143

The source file of the viewer class:

1
27#include "viewer.h"
28#include <easy3d/renderer/clipping_plane.h>
29#include <easy3d/renderer/manipulated_frame.h>
30#include <easy3d/renderer/manipulator.h>
31
32using namespace easy3d;
33
34// \cond
35CrossSection::CrossSection(const std::string &title) : Viewer(title) {
36 // Code of interest: both backends use the shared clipping-plane controller.
37 // ClippingPlane is a singleton: there is exactly one clipping plane per program,
38 // and every drawable's shader consults it, so enabling it here clips all models.
39 ClippingPlane::instance()->set_enabled(true);
40 // hint_ is displayed in the viewer HUD; it documents the built-in Alt shortcuts
41 // (toggle clipping, show/hide the plane guide, toggle cross-section mode).
42 hint_ =
43 "Left button: rotate the clipping plane \n" "Right button: translate the clipping plane \n"
44 "Alt + 'c': enable/disable clipping plane\n" "Alt + 'v': show/hide clipping plane \n"
45 "Alt + 'x': toggle cross-section";
46}
47
48// Route mouse drags to the plane's manipulator instead of the camera.
49bool CrossSection::mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers) {
50 // This tutorial manipulates the plane directly, without the Alt modifier
51 // required by the default viewer controls.
52 // The manipulator owns a ManipulatedFrame: a local 3D frame attached to the plane.
53 // Its action_* helpers convert 2D mouse deltas (dx, dy) into 3D rotations/translations
54 // expressed in camera space, so the plane moves intuitively from the current viewpoint.
55 ManipulatedFrame *frame = ClippingPlane::instance()->manipulator()->frame();
56 if (button == BUTTON_LEFT)
57 frame->action_rotate(x, y, dx, dy, camera(), ManipulatedFrame::NONE);
58 else if (button == BUTTON_RIGHT)
59 frame->action_translate(x, y, dx, dy, camera(), ManipulatedFrame::NONE);
60 else
61 // Any other button (e.g. middle) keeps the default navigation behavior.
62 return Viewer::mouse_drag_event(x, y, dx, dy, button, modifiers);
63 return false;
64}
65// \endcond
A Frame that can be rotated and translated using the mouse.
Definition manipulated_frame.h:58
See also
ClippingPlane, ManipulatedFrame, Viewer, Camera