Easy3D 2.5.3
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
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/core/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;
53 class TrianglesDrawable;
54
104 {
105 public:
111 explicit KeyFrameInterpolator(Frame *frame = nullptr);
112
114 virtual ~KeyFrameInterpolator();
115
118 public:
125 bool add_keyframe(const Frame& frame);
126
133 bool add_keyframe(const Frame& frame, float time);
134
140
147 void set_keyframe_time(std::size_t index, float t);
148
154 void set_keyframe_position(std::size_t index, const vec3& pos);
155
161 void set_keyframe_orientation(std::size_t index, const quat& q);
162
167 void delete_path();
169
172 public:
180 Frame* frame() const { return frame_; }
181
182 public:
184 void set_frame(Frame* const frame) { frame_ = frame; }
186
189 public:
194 std::size_t number_of_keyframes() const { return keyframes_.size(); }
195
201 Frame keyframe(std::size_t index) const;
202
208 float keyframe_time(std::size_t index) const;
209
215 const vec3& keyframe_position(std::size_t index) const;
216
222 const quat& keyframe_orientation(std::size_t index) const;
223
230 float duration() const;
231
237 float firstTime() const;
238
244 float lastTime() const;
246
249 public:
250
251 enum Method { FITTING, INTERPOLATION };
256 Method interpolation_method() const { return interpolation_method_; }
257
263 float interpolation_speed() const { return interpolation_speed_; }
264
272 int interpolation_period() const { return static_cast<int>(1000.0 / fps_); }
273
278 int frame_rate() const { return fps_; }
279
280 public:
281
285 void set_interpolation_method(Method m);
286
290 void set_interpolation_speed(float speed);
291
293 void set_frame_rate(int fps);
295
298 public:
303 bool is_interpolation_started() const { return interpolation_started_; }
304
305 public:
306
315 void start_interpolation();
316
321 void stop_interpolation();
322
329 else
331 }
332
333 public:
335 const std::vector<Frame>& interpolate();
337
340 public:
341
348 void draw_cameras(const Camera* camera, float camera_width, const vec4& color = vec4(0.5f, 0.8f, 0.5f, 1.0f));
349
354 void draw_path(const Camera* camera, float thickness = 2.0f, const vec4& color = vec4(1.0f, 1.0f, 0.5f, 1.0f));
355
357
360 public:
362 bool save_keyframes(const std::string& file_name) const;
364 bool read_keyframes(const std::string& file_name);
366
367 private:
368 // Copy constructor and operator= are declared private and undefined
369 // Prevents everyone from trying to use them
371 KeyFrameInterpolator& operator=(const KeyFrameInterpolator& kfi);
372
373 // Internal private Keyframe representation
374 class Keyframe
375 {
376 public:
377 Keyframe(const Frame& fr, float t);
378 const vec3& position() const { return p_; }
379 const quat& orientation() const { return q_; }
380 float time() const { return time_; }
381 void set_time(float t) { time_ = t; }
382 void set_position(const vec3& p) { p_ = p; }
383 void set_orientation(const quat& q) { q_ = q; }
384 void flip_if_needed(const quat& prev); // flip its orientation if needed
385 private:
386 vec3 p_;
387 quat q_;
388 float time_;
389 };
390
391 private:
392 // Associated frame
393 Frame* frame_;
394
395 // Key Frames
396 std::vector<Keyframe> keyframes_;
397 std::vector<Frame> interpolated_path_;
398
399 // Rhythm
400 Timer<> timer_;
401 int fps_;
402 Method interpolation_method_;
403 float interpolation_speed_;
404 bool interpolation_started_;
405 int last_stopped_index_;
406
407 // is path valid? Adding new keyframes or editing a keyframe invalidates the path
408 bool pathIsValid_;
409
410 LinesDrawable* path_drawable_;
411 TrianglesDrawable* cameras_drawable_;
412
413 public:
414 Signal<> frame_interpolated;
415 Signal<> interpolation_stopped;
416 };
417
418}
419
420
421#endif // EASY3D_RENDERER_KEY_FRAME_INTERPOLATOR_H
A perspective or orthographic camera.
Definition: camera.h:116
The Frame class represents a coordinate system, defined by a position and an orientation.
Definition: frame.h:152
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:263
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
float duration() const
Returns the duration of the KeyFrameInterpolator path, expressed in seconds.
Definition: key_frame_interpolator.cpp:176
bool read_keyframes(const std::string &file_name)
reads camera path from a file
Definition: key_frame_interpolator.cpp:360
std::size_t number_of_keyframes() const
Returns the number of keyframes used by the interpolation.
Definition: key_frame_interpolator.h:194
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:387
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:302
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:197
int interpolation_period() const
Returns the current interpolation period, expressed in milliseconds.
Definition: key_frame_interpolator.h:272
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:215
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)
Definition: key_frame_interpolator.h:184
float lastTime() const
Returns the time corresponding to the last keyframe, expressed in seconds.
Definition: key_frame_interpolator.cpp:189
void set_interpolation_speed(float speed)
Sets the interpolation_speed().
Definition: key_frame_interpolator.cpp:203
int frame_rate() const
Returns the desired frame rate. Default value is 30.
Definition: key_frame_interpolator.h:278
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 firstTime() const
Returns the time corresponding to the first keyframe, expressed in seconds.
Definition: key_frame_interpolator.cpp:181
void toggle_interpolation()
Calls start_interpolation() or stop_interpolation(), depending on is_interpolation_started().
Definition: key_frame_interpolator.h:326
void set_frame_rate(int fps)
Sets the desired frame rate.
Definition: key_frame_interpolator.cpp:209
void stop_interpolation()
Stops an interpolation started with start_interpolation().
Definition: key_frame_interpolator.cpp:247
bool save_keyframes(const std::string &file_name) const
saves the camera path to a file
Definition: key_frame_interpolator.cpp:340
bool is_interpolation_started() const
Returns whether the interpolation is being performed.
Definition: key_frame_interpolator.h:303
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:253
Frame * frame() const
Returns the associated Frame and that is interpolated by the KeyFrameInterpolator.
Definition: key_frame_interpolator.h:180
void delete_path()
Removes all keyframes from the path.
Definition: key_frame_interpolator.cpp:140
Method interpolation_method() const
Returns the interpolation method.
Definition: key_frame_interpolator.h:256
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
std::string time(double time, int num_digits)
Converts time (in millisecond) into a string with the most suitable/readable unit....
Definition: string.cpp:223
Definition: collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition: types.h:45
Quat< float > quat
A quaternion of float type.
Definition: types.h:86
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition: types.h:47