Easy3D 2.6.1
Loading...
Searching...
No Matches
key_frame_interpolator.h
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
36
37#ifndef EASY3D_RENDERER_KEY_FRAME_INTERPOLATOR_H
38#define EASY3D_RENDERER_KEY_FRAME_INTERPOLATOR_H
39
40#include <easy3d/core/types.h>
41#include "easy3d/util/signal.h"
42#include <easy3d/util/timer.h>
43#include <easy3d/renderer/frame.h>
44
45#include <vector>
46
47
48namespace easy3d {
49
50 class Frame;
51 class Camera;
52 class LinesDrawable;
54
104 {
105 public:
112 explicit KeyFrameInterpolator(Frame *frame = nullptr);
113
115 virtual ~KeyFrameInterpolator();
116
119 public:
127 bool add_keyframe(const Frame& frame);
128
137 bool add_keyframe(const Frame& frame, float time);
138
144
153 void set_keyframe_time(std::size_t index, float t);
154
162 void set_keyframe_position(std::size_t index, const vec3& pos);
163
171 void set_keyframe_orientation(std::size_t index, const quat& q);
172
177 void delete_path();
179
182 public:
191 Frame* frame() const { return frame_; }
192
193 public:
198 void set_frame(Frame* const frame) { frame_ = frame; }
200
203 public:
209 std::size_t number_of_keyframes() const { return keyframes_.size(); }
210
218 Frame keyframe(std::size_t index) const;
219
227 float keyframe_time(std::size_t index) const;
228
236 const vec3& keyframe_position(std::size_t index) const;
237
245 const quat& keyframe_orientation(std::size_t index) const;
246
254 float duration() const;
255
262 float first_time() const;
263
270 float last_time() const;
272
275 public:
276
282
287 Method interpolation_method() const { return interpolation_method_; }
288
295 float interpolation_speed() const { return interpolation_speed_; }
296
305 int interpolation_period() const { return static_cast<int>(1000.0 / fps_); }
306
312 int frame_rate() const { return fps_; }
313
314 public:
320
325 void set_interpolation_speed(float speed);
326
331 void set_frame_rate(int fps);
333
336 public:
342 bool is_interpolation_started() const { return interpolation_started_; }
343
344 public:
353 void start_interpolation();
354
359 void stop_interpolation();
360
370
371 public:
376 const std::vector<Frame>& interpolate();
378
381 public:
382
390 void draw_cameras(const Camera* camera, float camera_width, const vec4& color = vec4(0.5f, 0.8f, 0.5f, 1.0f));
391
398 void draw_path(const Camera* camera, float thickness = 2.0f, const vec4& color = vec4(1.0f, 1.0f, 0.5f, 1.0f));
399
401
404 public:
410 bool save_keyframes(const std::string& file_name) const;
416 bool read_keyframes(const std::string& file_name);
418
419 private:
420 // Copy constructor and operator= are declared private and undefined
421 // Prevents everyone from trying to use them
423 KeyFrameInterpolator& operator=(const KeyFrameInterpolator& kfi);
424
425 // Internal private Keyframe representation
426 class Keyframe
427 {
428 public:
429 Keyframe(const Frame& fr, float t);
430 const vec3& position() const { return p_; }
431 const quat& orientation() const { return q_; }
432 float time() const { return time_; }
433 void set_time(float t) { time_ = t; }
434 void set_position(const vec3& p) { p_ = p; }
435 void set_orientation(const quat& q) { q_ = q; }
436 void flip_if_needed(const quat& prev); // flip its orientation if needed
437 private:
438 vec3 p_;
439 quat q_;
440 float time_;
441 };
442
443 private:
444 // Associated frame
445 Frame* frame_;
446
447 // Key Frames
448 std::vector<Keyframe> keyframes_;
449 std::vector<Frame> interpolated_path_;
450
451 // Rhythm
452 Timer<> timer_;
453 int fps_;
454 Method interpolation_method_;
455 float interpolation_speed_;
456 bool interpolation_started_;
457 int last_stopped_index_;
458
459 // is path valid? Adding new keyframes or editing a keyframe invalidates the path
460 bool pathIsValid_;
461
462 LinesDrawable* path_drawable_;
463 TrianglesDrawable* cameras_drawable_;
464
465 public:
468 };
469
470}
471
472
473#endif // EASY3D_RENDERER_KEY_FRAME_INTERPOLATOR_H
A perspective or orthographic camera.
Definition camera.h:113
The Frame class represents a coordinate system, defined by a position and an orientation.
Definition frame.h:78
A keyframe interpolator for animation generation.
Definition key_frame_interpolator.h:104
float interpolation_speed() const
Returns the current interpolation speed.
Definition key_frame_interpolator.h:295
const quat & keyframe_orientation(std::size_t index) const
Returns the orientation of the index-th keyframe.
Definition key_frame_interpolator.cpp:171
virtual ~KeyFrameInterpolator()
Definition key_frame_interpolator.cpp:69
void delete_last_keyframe()
Removes the lastly added keyframe from the path.
Definition key_frame_interpolator.cpp:111
Method
Definition key_frame_interpolator.h:278
@ INTERPOLATION
Interpolate between keyframes.
Definition key_frame_interpolator.h:280
@ FITTING
Fit a curve to the keyframes.
Definition key_frame_interpolator.h:279
float duration() const
Returns the duration of the KeyFrameInterpolator path, expressed in seconds.
Definition key_frame_interpolator.cpp:176
Signal interpolation_stopped
Emitted when the interpolation is stopped.
Definition key_frame_interpolator.h:467
bool read_keyframes(const std::string &file_name)
Reads the camera path from a file.
Definition key_frame_interpolator.cpp:358
std::size_t number_of_keyframes() const
Returns the number of keyframes used by the interpolation.
Definition key_frame_interpolator.h:209
void set_keyframe_orientation(std::size_t index, const quat &q)
Sets the orientation of the index-th keyframe.
Definition key_frame_interpolator.cpp:133
const std::vector< Frame > & interpolate()
Computes and returns all the interpolated frames.
Definition key_frame_interpolator.cpp:385
void draw_path(const Camera *camera, float thickness=2.0f, const vec4 &color=vec4(1.0f, 1.0f, 0.5f, 1.0f))
Draws the interpolated camera path.
Definition key_frame_interpolator.cpp:300
void set_keyframe_time(std::size_t index, float t)
Sets the time corresponding to the index-th keyframe.
Definition key_frame_interpolator.cpp:119
void set_interpolation_method(Method m)
Sets the interpolation_method().
Definition key_frame_interpolator.cpp:195
int interpolation_period() const
Returns the current interpolation period, expressed in milliseconds.
Definition key_frame_interpolator.h:305
KeyFrameInterpolator(Frame *frame=nullptr)
Creates a KeyFrameInterpolator, with frame as associated frame().
Definition key_frame_interpolator.cpp:55
void start_interpolation()
Starts the interpolation process.
Definition key_frame_interpolator.cpp:213
void set_keyframe_position(std::size_t index, const vec3 &pos)
Sets the position of the index-th keyframe.
Definition key_frame_interpolator.cpp:126
void set_frame(Frame *const frame)
Sets the frame() associated to the KeyFrameInterpolator.
Definition key_frame_interpolator.h:198
void set_interpolation_speed(float speed)
Sets the interpolation_speed().
Definition key_frame_interpolator.cpp:201
int frame_rate() const
Returns the desired frame rate. Default value is 30.
Definition key_frame_interpolator.h:312
Frame keyframe(std::size_t index) const
Returns the Frame associated with the keyframe at index.
Definition key_frame_interpolator.cpp:155
const vec3 & keyframe_position(std::size_t index) const
Returns the position of the index-th keyframe.
Definition key_frame_interpolator.cpp:166
float last_time() const
Returns the time corresponding to the last keyframe, expressed in seconds.
Definition key_frame_interpolator.cpp:188
void toggle_interpolation()
Calls start_interpolation() or stop_interpolation(), depending on is_interpolation_started().
Definition key_frame_interpolator.h:364
void set_frame_rate(int fps)
Sets the desired frame rate.
Definition key_frame_interpolator.cpp:207
void stop_interpolation()
Stops an interpolation started with start_interpolation().
Definition key_frame_interpolator.cpp:245
bool save_keyframes(const std::string &file_name) const
Saves the camera path to a file.
Definition key_frame_interpolator.cpp:338
Signal frame_interpolated
Emitted when a frame is interpolated.
Definition key_frame_interpolator.h:466
bool is_interpolation_started() const
Returns whether the interpolation is being performed.
Definition key_frame_interpolator.h:342
void draw_cameras(const Camera *camera, float camera_width, const vec4 &color=vec4(0.5f, 0.8f, 0.5f, 1.0f))
Draws the virtual 3D cameras for the keyframes.
Definition key_frame_interpolator.cpp:251
Frame * frame() const
Returns the associated Frame and that is interpolated by the KeyFrameInterpolator.
Definition key_frame_interpolator.h:191
void delete_path()
Removes all keyframes from the path.
Definition key_frame_interpolator.cpp:140
float first_time() const
Returns the time corresponding to the first keyframe, expressed in seconds.
Definition key_frame_interpolator.cpp:181
Method interpolation_method() const
Returns the interpolation method.
Definition key_frame_interpolator.h:287
float keyframe_time(std::size_t index) const
Returns the time corresponding to the index-th keyframe.
Definition key_frame_interpolator.cpp:161
bool add_keyframe(const Frame &frame)
Appends a new keyframe to the path.
Definition key_frame_interpolator.cpp:74
The drawable for rendering a set of line segments, e.g., edges of a mesh, vector fields.
Definition drawable_lines.h:40
A light-weight implementation of the simple signal-slot mechanism.
Definition signal.h:54
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition drawable_triangles.h:46
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
Quat< float > quat
A quaternion of float type.
Definition types.h:85
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46