Easy3D 2.6.1
Loading...
Searching...
No Matches
Tutorial_307_CrossSection/main.cpp

This example shows how to render a model with a clipping plane or cross-sections.

The header file of the viewer class:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_TUTORIAL_CROSS_SECTION_H
28#define EASY3D_TUTORIAL_CROSS_SECTION_H
29
30#include <easy3d/viewer/viewer.h>
31
32namespace easy3d {
33
34 class CrossSection : public Viewer
35 {
36 public:
37 explicit CrossSection(const std::string& title);
38
39 protected:
40 // manipulate the clipping plane
41 bool mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers) override;
42 };
43
44}
45
46#endif // EASY3D_TUTORIAL_CROSS_SECTION_H
Definition collider.cpp:182

The source file of the viewer class:

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
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
32
33namespace easy3d {
34
35 // \cond
36
37 CrossSection::CrossSection(const std::string &title) : Viewer(title) {
38 // Code of interest:
40
41 hint_ =
42 "Left button: rotate the clipping plane \n"
43 "Right button: translate the clipping plane \n"
44 "Alt + 'c': enable/disable clipping plane\n"
45 "Alt + 'v': show/hide clipping plane \n"
46 "Alt + 'x': toggle cross-section";
47 }
48
49
50 bool CrossSection::mouse_drag_event(int x, int y, int dx, int dy, int button, int modifiers) {
51 // Code of interest:
52 ManipulatedFrame* frame = ClippingPlane::instance()->manipulator()->frame();
53 if (pressed_button_ == BUTTON_LEFT)
54 frame->action_rotate(x, y, dx, dy, camera(), ManipulatedFrame::NONE);
55 else if (pressed_button_ == BUTTON_RIGHT)
56 frame->action_translate(x, y, dx, dy, camera(), ManipulatedFrame::NONE);
57 else
58 return Viewer::mouse_drag_event(x, y, dx, dy, button, modifiers);
59
60 return false;
61 }
62
63 // \endcond
64
65}
static ClippingPlane * instance()
Definition clipping_plane.cpp:61
virtual Manipulator * manipulator()
returns the manipulator attached to the clipping plane.
Definition clipping_plane.h:106
void set_enabled(bool b)
enables/disables the clipping plane.
Definition clipping_plane.h:56
@ NONE
No constraint.
Definition manipulated_frame.h:165
virtual void action_rotate(int mouse_x, int mouse_y, int mouse_dx, int mouse_dy, Camera *camera, ScreenAxis axis)
Rotates the frame based on mouse movement.
ManipulatedFrame * frame()
Returns the manipulated frame.
Definition manipulator.h:69
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#include <easy3d/renderer/camera.h>
28#include <easy3d/util/resource.h>
29#include <easy3d/util/initializer.h>
30
31#include "viewer.h"
32
43
44using namespace easy3d;
45
46int main(int argc, char** argv) {
47 // initialize Easy3D.
48 initialize();
49
50 const std::string file_name = resource::directory() + "/data/bunny.ply";
51 CrossSection viewer(EXAMPLE_TITLE);
52 viewer.camera()->setViewDirection(vec3(-1, 1, -1));
53
54 if (!viewer.add_model(file_name)) {
55 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
56 return EXIT_FAILURE;
57 }
58
59 return viewer.run();
60}
std::string directory()
Returns the resource directory (containing color maps, shaders, textures, fonts, etc....
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
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