Easy3D 2.5.3
Tutorial_206_CameraInterpolation

The source file containing the main() function:

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/util/resource.h>
29#include <easy3d/util/initializer.h>
30
31
32// This example shows how to
33// - creat an exploration path using the key frame interpolator,
34// - play the path as an animation.
35
36using namespace easy3d;
37
38int main(int argc, char** argv) {
39 // initialize Easy3D.
40 initialize();
41
42 const std::string& file_name = resource::directory() + "/data/sphere.obj";
43
44 CameraInterpolation viewer(EXAMPLE_TITLE);
45 if (!viewer.add_model(file_name, true)) {
46 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
47 return EXIT_FAILURE;
48 }
49
50 return viewer.run();
51}
Definition: collider.cpp:182
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35

The header file of the 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_CAMERA_INTERPOLATION_H
28#define EASY3D_TUTORIAL_CAMERA_INTERPOLATION_H
29
30#include <easy3d/viewer/viewer.h>
31
32
33namespace easy3d {
34 class KeyFrameInterpolator;
35}
36
37// This tutorial shows how to interpolate camera frames to
38// animate model exploration.
39
40class CameraInterpolation : public easy3d::Viewer
41{
42public:
43 explicit CameraInterpolation(const std::string& title);
44 ~CameraInterpolation() override;
45
46protected:
47 bool key_press_event(int key, int modifiers) override;
48 void draw() const override;
49
50private:
51 easy3d::KeyFrameInterpolator* interpolator_;
52};
53
54
55#endif // EASY3D_TUTORIAL_CAMERA_INTERPOLATION_H
A keyframe interpolator for animation generation.
Definition: key_frame_interpolator.h:104
The built-in Easy3D viewer.
Definition: viewer.h:61

The source file of the 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/camera.h>
29#include <easy3d/renderer/manipulated_camera_frame.h>
30#include <easy3d/renderer/key_frame_interpolator.h>
31#include <easy3d/core/model.h>
32
33
34using namespace easy3d;
35
36
37CameraInterpolation::CameraInterpolation(const std::string& title)
38 : Viewer(title)
39{
40 interpolator_ = new KeyFrameInterpolator(camera_->frame());
41 // update the viewer when the interpolation finishes
42 easy3d::connect(&interpolator_->interpolation_stopped, (Viewer*)this, &Viewer::update);
43
44 usage_string_ =
45 "------------ Camera Interpolation usage ---------- \n"
46 "Press 'K' to add key frames\n"
47 "Press 'Space' to start/stop the animation\n"
48 "Press 'D' to delete the camera path\n"
49 "-------------------------------------------------- \n";
50}
51
52
53CameraInterpolation::~CameraInterpolation()
54{
55 delete interpolator_;
56}
57
58
59bool CameraInterpolation::key_press_event(int key, int modifiers)
60{
61 if (key == KEY_K && modifiers == 0) {
62 easy3d::Frame *frame = camera()->frame();
63 if (interpolator_->add_keyframe(*frame)) {
64 float dist = distance(camera_->sceneCenter(), frame->position());
65 if (dist > camera_->sceneRadius())
66 camera_->setSceneRadius(dist);
67 std::cout << "Key frame added" << std::endl;
68 return true;
69 }
70 else
71 return false;
72 }
73 else if (key == KEY_SPACE && modifiers == 0) {
74 if (interpolator_->is_interpolation_started()) {
75 interpolator_->stop_interpolation();
76 std::cout << "Animation stopped" << std::endl;
77 }
78 else {
79 interpolator_->start_interpolation();
80 if (interpolator_->is_interpolation_started())
81 std::cout << "Animation started" << std::endl;
82 }
83 return true;
84 }
85 else if (key == KEY_D && modifiers == 0) {
86 interpolator_->delete_path();
87 // update scene bounding box
88 Box3 box;
89 for (auto m : models_)
90 box.grow(m->bounding_box());
91 camera_->setSceneBoundingBox(box.min_point(), box.max_point());
92 std::cout << "path deleted"<< std::endl;
93 return true;
94 }
95 else
96 return Viewer::key_press_event(key, modifiers);
97}
98
99
100void CameraInterpolation::draw() const {
101 Viewer::draw();
102
103 // shown only when it is not animating
104 if (!interpolator_->is_interpolation_started()) {
105 interpolator_->draw_cameras(camera(), camera()->sceneRadius() * 0.05f);
106 interpolator_->draw_path(camera());
107 }
108}
The Frame class represents a coordinate system, defined by a position and an orientation.
Definition: frame.h:152
vec3 position() const
Definition: frame.cpp:410
const Point & min_point() const
Definition: box.h:93
const Point & max_point() const
Definition: box.h:98
void grow(const Point &p)
Definition: box.h:216
T distance(const Vec< N, T > &v1, const Vec< N, T > &v2)
Computes the distance between two vectors/points.
Definition: vec.h:295
int connect(SIGNAL *signal, FUNCTION const &slot)
Connects a function to the signal.
Definition: signal.h:202