Easy3D 3.0.0
Loading...
Searching...
No Matches
keyframe_interpolator.h
1
28#ifndef EASY3D_RENDERER_KEYFRAME_INTERPOLATOR_H
29#define EASY3D_RENDERER_KEYFRAME_INTERPOLATOR_H
30
31#include <easy3d/core/types.h>
32#include "easy3d/util/signal.h"
33#include <easy3d/util/timer.h>
34#include <easy3d/renderer/frame.h>
35
36#include <iosfwd>
37#include <vector>
38
39
40namespace easy3d {
41
42 class Frame;
43 class Camera;
44 class LinesDrawable;
45 class TrianglesDrawable;
46
94 {
95 public:
102 explicit KeyframeInterpolator(Frame *frame = nullptr);
103
105 virtual ~KeyframeInterpolator();
106
109 public:
117 bool add_keyframe(const Frame& frame);
118
127 bool add_keyframe(const Frame& frame, float time);
128
134
145 bool replace_keyframes(const std::vector<Frame>& frames,
146 const std::vector<float>& times,
147 const std::vector<float>& dwells);
148
157 void set_keyframe_time(std::size_t index, float t);
158
166 void set_keyframe_position(std::size_t index, const vec3& pos);
167
175 void set_keyframe_orientation(std::size_t index, const quat& q);
176
181 void delete_path();
183
186 public:
195 Frame* frame() const { return frame_; }
196
197 public:
202 void set_frame(Frame* const frame) { frame_ = frame; }
204
207 public:
213 std::size_t number_of_keyframes() const { return keyframes_.size(); }
214
222 Frame keyframe(std::size_t index) const;
223
231 float keyframe_time(std::size_t index) const;
232
240 const vec3& keyframe_position(std::size_t index) const;
241
249 const quat& keyframe_orientation(std::size_t index) const;
250
258 float duration() const;
259
266 float first_time() const;
267
274 float last_time() const;
275
284 float playback_elapsed() const;
286
289 public:
290
301 Method interpolation_method() const { return interpolation_method_; }
302
309 float interpolation_speed() const { return interpolation_speed_; }
310
319 int interpolation_period() const { return static_cast<int>(1000.0 / fps_); }
320
326 int frame_rate() const { return fps_; }
327
328 public:
334
339 void set_interpolation_speed(float speed);
340
345 void set_frame_rate(int fps);
346
356 void set_path_periodic(bool periodic);
358 bool is_path_periodic() const { return path_is_periodic_; }
360
363 public:
369 bool is_interpolation_started() const { return interpolation_started_; }
370
371 public:
380 void start_interpolation();
381
395
403 void seek_to_keyframe(std::size_t index);
404
409 void stop_interpolation();
410
420
421 public:
426 const std::vector<Frame>& interpolate();
427
437 float total_duration() const;
438
439 private:
440 // Where each keyframe sits along the path, as a fraction of the path's total: 0 at the
441 // first keyframe, 1 at the last.
442 //
443 // length_fraction() is the fraction of the path's LENGTH at which a keyframe sits: it
444 // is the position spline's own parameter, and the pose must be read with it.
445 std::vector<float> length_fraction() const;
446
447 // Cinematic speed profile within one travel segment: fraction of the segment's length
448 // covered at time fraction w (in [0,1]). stop_start/stop_end say whether the camera is
449 // at rest at the segment's ends.
450 static float segment_fraction(float w, bool stop_start, bool stop_end);
451
452 public:
454
457 public:
458
473 bool update_path_drawable(LinesDrawable& drawable, float thickness = 2.0f,
474 const vec4& color = vec4(0.15f, 0.45f, 1.0f, 1.0f));
475
498 bool update_cameras_drawable(TrianglesDrawable& drawable, const Camera* camera, float camera_width,
499 const vec4& color = vec4(0.2f, 0.7f, 0.3f, 1.0f)) const;
500
517 static std::size_t camera_glyph_triangles();
518
520
523 public:
533 void set_keyframe_dwell(std::size_t index, float seconds);
534
541 float keyframe_dwell(std::size_t index) const;
543
546 public:
552 bool save_keyframes(std::ostream& output) const;
558 bool load_keyframes(std::istream& input);
560
561 private:
562 // Copy constructor and operator= are declared private and undefined
563 // Prevents everyone from trying to use them
565 KeyframeInterpolator& operator=(const KeyframeInterpolator& kfi);
566
567 // Internal private Keyframe representation
568 class Keyframe
569 {
570 public:
571 Keyframe(const Frame& fr, float t);
572 const vec3& position() const { return p_; }
573 const quat& orientation() const { return q_; }
574 float time() const { return time_; }
575 void set_time(float t) { time_ = t; }
576 void set_position(const vec3& p) { p_ = p; }
577 void set_orientation(const quat& q) { q_ = q; }
585 float dwell_time() const { return dwell_time_; }
591 void set_dwell_time(float seconds) { dwell_time_ = seconds; }
592 void flip_if_needed(const quat& prev); // flip its orientation if needed
593 private:
594 vec3 p_;
595 quat q_;
596 float time_;
597 float dwell_time_ = 0.0f;
598 };
599
600 // Appends the keyframe cameras as glyph triangles to the given buffers: the glyph
601 // mesh is created once (shape::create_camera) and instanced per keyframe with that
602 // keyframe's Frame matrix. Every glyph contributes the same triangle range, and
603 // glyph i occupies the contiguous indices [i * camera_glyph_triangles(), ...), which
604 // is what maps a picked triangle back to its keyframe.
605 void append_camera_glyphs(float camera_width, float fov, float aspect,
606 std::vector<vec3>& points, std::vector<unsigned int>& indices) const;
607
608 private:
609 // Associated frame
610 Frame* frame_;
611
612 // Keyframes
613 std::vector<Keyframe> keyframes_;
614 std::vector<Frame> interpolated_path_;
615
616 // Rhythm
617 Timer<> timer_;
618 int fps_;
619 Method interpolation_method_;
620 float interpolation_speed_;
621 bool interpolation_started_;
622 bool manual_playback_ = false;
623 bool timer_mode_used_ = false;
624 int last_stopped_index_;
625 float playback_elapsed_ = 0.0f;
626
627 // is path valid? Adding new keyframes or editing a keyframe invalidates the path
628 bool pathIsValid_;
629 bool path_is_periodic_ = false;
630
631 public:
634 };
635
636}
637
638
639#endif // EASY3D_RENDERER_KEYFRAME_INTERPOLATOR_H
A perspective or orthographic camera.
Definition camera.h:142
The Frame class represents a coordinate system, defined by a position and an orientation.
Definition frame.h:79
A keyframe interpolator for animation generation.
Definition keyframe_interpolator.h:94
float interpolation_speed() const
Returns the current interpolation speed.
Definition keyframe_interpolator.h:309
const quat & keyframe_orientation(std::size_t index) const
Returns the orientation of the index-th keyframe.
Definition keyframe_interpolator.cpp:285
void delete_last_keyframe()
Removes the lastly added keyframe from the path.
Definition keyframe_interpolator.cpp:195
void set_path_periodic(bool periodic)
Sets whether the path is interpolated as a closed periodic loop.
Definition keyframe_interpolator.cpp:338
static std::size_t camera_glyph_triangles()
The number of triangles one camera glyph occupies in update_cameras_drawable().
Definition keyframe_interpolator.cpp:463
Method
Definition keyframe_interpolator.h:292
@ INTERPOLATION
Interpolate between keyframes.
Definition keyframe_interpolator.h:294
@ FITTING
Fit a curve to the keyframes.
Definition keyframe_interpolator.h:293
float duration() const
Returns the duration of the KeyframeInterpolator path, expressed in seconds.
Definition keyframe_interpolator.cpp:290
Signal interpolation_stopped
Emitted when the interpolation is stopped.
Definition keyframe_interpolator.h:633
std::size_t number_of_keyframes() const
Returns the number of keyframes used by the interpolation.
Definition keyframe_interpolator.h:213
bool advance_interpolation()
Advance manual playback by one interpolation sample.
Definition keyframe_interpolator.cpp:399
float playback_elapsed() const
Returns the playback position in seconds, along the path's timeline (travel plus dwell).
Definition keyframe_interpolator.cpp:309
void set_keyframe_orientation(std::size_t index, const quat &q)
Sets the orientation of the index-th keyframe.
Definition keyframe_interpolator.cpp:240
const std::vector< Frame > & interpolate()
Computes and returns all the interpolated frames.
Definition keyframe_interpolator.cpp:676
bool replace_keyframes(const std::vector< Frame > &frames, const std::vector< float > &times, const std::vector< float > &dwells)
Atomically replaces the path with frames, times and optional dwells.
Definition keyframe_interpolator.cpp:204
void set_keyframe_time(std::size_t index, float t)
Sets the time corresponding to the index-th keyframe.
Definition keyframe_interpolator.cpp:226
virtual ~KeyframeInterpolator()
Definition keyframe_interpolator.cpp:152
void set_interpolation_method(Method m)
Sets the interpolation_method().
Definition keyframe_interpolator.cpp:316
int interpolation_period() const
Returns the current interpolation period, expressed in milliseconds.
Definition keyframe_interpolator.h:319
void start_interpolation()
Starts the interpolation process.
Definition keyframe_interpolator.cpp:347
void set_keyframe_position(std::size_t index, const vec3 &pos)
Sets the position of the index-th keyframe.
Definition keyframe_interpolator.cpp:233
void set_frame(Frame *const frame)
Sets the frame() associated with the KeyframeInterpolator.
Definition keyframe_interpolator.h:202
bool load_keyframes(std::istream &input)
Reads the camera path's keyframes from a stream.
Definition keyframe_interpolator.cpp:544
void set_interpolation_speed(float speed)
Sets the interpolation_speed().
Definition keyframe_interpolator.cpp:322
int frame_rate() const
Returns the desired frame rate. Default value is 30.
Definition keyframe_interpolator.h:326
Frame keyframe(std::size_t index) const
Returns the Frame associated with the keyframe at index.
Definition keyframe_interpolator.cpp:269
void start_manual_interpolation()
Start interpolation without creating a timer or worker thread.
Definition keyframe_interpolator.cpp:388
float total_duration() const
Returns the duration of the path, in seconds.
Definition keyframe_interpolator.cpp:924
const vec3 & keyframe_position(std::size_t index) const
Returns the position of the index-th keyframe.
Definition keyframe_interpolator.cpp:280
void seek_to_keyframe(std::size_t index)
Stops playback and positions the frame and playhead at the given keyframe.
Definition keyframe_interpolator.cpp:421
bool save_keyframes(std::ostream &output) const
Writes the camera path's keyframes to a stream.
Definition keyframe_interpolator.cpp:520
float last_time() const
Returns the time corresponding to the last keyframe, expressed in seconds.
Definition keyframe_interpolator.cpp:302
void toggle_interpolation()
Calls start_interpolation() or stop_interpolation(), depending on is_interpolation_started().
Definition keyframe_interpolator.h:414
bool is_path_periodic() const
Definition keyframe_interpolator.h:358
void set_frame_rate(int fps)
Sets the desired frame rate.
Definition keyframe_interpolator.cpp:330
void stop_interpolation()
Stops an interpolation started with start_interpolation().
Definition keyframe_interpolator.cpp:440
Signal frame_interpolated
Emitted when a frame is interpolated.
Definition keyframe_interpolator.h:632
bool update_cameras_drawable(TrianglesDrawable &drawable, const Camera *camera, float camera_width, const vec4 &color=vec4(0.2f, 0.7f, 0.3f, 1.0f)) const
Fills drawable with the keyframe cameras as solid glyphs, for drawing them as scene geometry.
Definition keyframe_interpolator.cpp:447
bool is_interpolation_started() const
Returns whether the interpolation is being performed.
Definition keyframe_interpolator.h:369
float keyframe_dwell(std::size_t index) const
Returns the dwell time of the index-th keyframe, in seconds.
Definition keyframe_interpolator.cpp:254
Frame * frame() const
Returns the associated Frame, which is interpolated by the KeyframeInterpolator.
Definition keyframe_interpolator.h:195
void delete_path()
Removes all keyframes from the path.
Definition keyframe_interpolator.cpp:259
void set_keyframe_dwell(std::size_t index, float seconds)
Sets the dwell time of the index-th keyframe.
Definition keyframe_interpolator.cpp:247
float first_time() const
Returns the time corresponding to the first keyframe, expressed in seconds.
Definition keyframe_interpolator.cpp:295
Method interpolation_method() const
Returns the interpolation method.
Definition keyframe_interpolator.h:301
bool update_path_drawable(LinesDrawable &drawable, float thickness=2.0f, const vec4 &color=vec4(0.15f, 0.45f, 1.0f, 1.0f))
Fills drawable with the interpolated camera path, for drawing it as scene geometry.
Definition keyframe_interpolator.cpp:509
float keyframe_time(std::size_t index) const
Returns the time corresponding to the index-th keyframe.
Definition keyframe_interpolator.cpp:275
bool add_keyframe(const Frame &frame)
Appends a new keyframe to the path.
Definition keyframe_interpolator.cpp:157
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:56
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition drawable_triangles.h:46
std::string time(double time, int num_digits)
Format a duration in milliseconds using a readable unit.
Definition string.cpp:227
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