Easy3D 2.6.1
|
A keyframe interpolator for animation generation. More...
#include <easy3d/renderer/key_frame_interpolator.h>
Public Member Functions | |
KeyFrameInterpolator (Frame *frame=nullptr) | |
Creates a KeyFrameInterpolator, with frame as associated frame(). | |
virtual | ~KeyFrameInterpolator () |
Path creation and modification | |
bool | add_keyframe (const Frame &frame) |
Appends a new keyframe to the path. | |
bool | add_keyframe (const Frame &frame, float time) |
Appends a new keyframe to the path, with its associated time (in seconds). | |
void | delete_last_keyframe () |
Removes the lastly added keyframe from the path. | |
void | set_keyframe_time (std::size_t index, float t) |
Sets the time corresponding to the index-th keyframe. | |
void | set_keyframe_position (std::size_t index, const vec3 &pos) |
Sets the position of the index-th keyframe. | |
void | set_keyframe_orientation (std::size_t index, const quat &q) |
Sets the orientation of the index-th keyframe. | |
void | delete_path () |
Removes all keyframes from the path. | |
Associated Frame | |
Frame * | frame () const |
Returns the associated Frame and that is interpolated by the KeyFrameInterpolator. | |
void | set_frame (Frame *const frame) |
Sets the frame() associated to the KeyFrameInterpolator. | |
Access keyframe and path parameters | |
std::size_t | number_of_keyframes () const |
Returns the number of keyframes used by the interpolation. | |
Frame | keyframe (std::size_t index) const |
Returns the Frame associated with the keyframe at index . | |
float | keyframe_time (std::size_t index) const |
Returns the time corresponding to the index-th keyframe. | |
const vec3 & | keyframe_position (std::size_t index) const |
Returns the position of the index-th keyframe. | |
const quat & | keyframe_orientation (std::size_t index) const |
Returns the orientation of the index-th keyframe. | |
float | duration () const |
Returns the duration of the KeyFrameInterpolator path, expressed in seconds. | |
float | first_time () const |
Returns the time corresponding to the first keyframe, expressed in seconds. | |
float | last_time () const |
Returns the time corresponding to the last keyframe, expressed in seconds. | |
Interpolation | |
bool | is_interpolation_started () const |
Returns whether the interpolation is being performed. | |
void | start_interpolation () |
Starts the interpolation process. | |
void | stop_interpolation () |
Stops an interpolation started with start_interpolation(). | |
void | toggle_interpolation () |
Calls start_interpolation() or stop_interpolation(), depending on is_interpolation_started(). | |
const std::vector< Frame > & | interpolate () |
Computes and returns all the interpolated frames. | |
Path drawing | |
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. | |
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. | |
Interpolation parameters | |
enum | Method { FITTING , INTERPOLATION } |
Method | interpolation_method () const |
Returns the interpolation method. | |
float | interpolation_speed () const |
Returns the current interpolation speed. | |
int | interpolation_period () const |
Returns the current interpolation period, expressed in milliseconds. | |
int | frame_rate () const |
Returns the desired frame rate. Default value is 30. | |
void | set_interpolation_method (Method m) |
Sets the interpolation_method(). | |
void | set_interpolation_speed (float speed) |
Sets the interpolation_speed(). | |
void | set_frame_rate (int fps) |
Sets the desired frame rate. | |
File io | |
Signal | frame_interpolated |
Emitted when a frame is interpolated. | |
Signal | interpolation_stopped |
Emitted when the interpolation is stopped. | |
bool | save_keyframes (const std::string &file_name) const |
Saves the camera path to a file. | |
bool | read_keyframes (const std::string &file_name) |
Reads the camera path from a file. | |
A keyframe interpolator for animation generation.
A KeyFrameInterpolator holds keyframes (that define a path) and a pointer to a frame (which will be interpolated) of your application. When the user start_interpolation(), the KeyFrameInterpolator regularly updates the frame() position and orientation along the path.
A keyframe is defined by a Frame and a time, expressed in seconds. The time has to be monotonously increasing over keyframes. When interpolation_speed() equals 1.0 (default value), these times correspond to actual user's seconds during interpolation (provided that your main loop is fast enough). The interpolation is then real-time: the keyframes will be reached at their keyframe_time().
When the user start_interpolation(), a timer is started which will update the frame()'s position and orientation every interpolation_period() milliseconds. This update increases the time by interpolation_period() * interpolation_speed() milliseconds. This mechanism ensures that the number of interpolation steps is constant and equal to the total path duration() divided by the interpolation_period() * interpolation_speed(). This is especially useful for benchmarking or movie creation (constant number of snapshots).
During the interpolation, the KeyFrameInterpolator emits a frame_interpolated
Signal which will usually be connected to the viewer's update() method. The interpolation is stopped when duration has reached. Another Signal interpolation_stopped will be emitted when the interpolation reaches its end or when the stop_interpolation() method was triggered.
Note that a Camera has a keyframe_interpolator() method, that can be used to drive the Camera along a path, or to restore a saved position (a path made of a single keyframe).
Animation example:
enum Method |
|
explicit |
Creates a KeyFrameInterpolator, with frame
as associated frame().
The frame() can be set or changed using set_frame(). The interpolation_speed() and interpolation_period() are set to their default values.
frame | The frame to be associated with the KeyFrameInterpolator. |
|
virtual |
Virtual destructor. Clears the keyframe path.
bool add_keyframe | ( | const Frame & | frame | ) |
Appends a new keyframe to the path.
Same as add_keyframe(const Frame&, float), except that the keyframe_time() is automatically set to previous keyframe_time() plus one second (or 0.0 if there is no previous keyframe).
frame | The frame to be added as a keyframe. |
true
if the keyframe has been successfully added. bool add_keyframe | ( | const Frame & | frame, |
float | time ) |
Appends a new keyframe to the path, with its associated time
(in seconds).
The path will use the current frame
state.
frame | The frame to be added as a keyframe. |
time | The time associated with the keyframe. |
true
if the keyframe has been successfully added. void delete_last_keyframe | ( | ) |
Removes the lastly added keyframe from the path.
This is the reverse operation of add_keyframe(const Frame&) and add_keyframe(const Frame&, float).
void delete_path | ( | ) |
Removes all keyframes from the path.
Upon return, the number_of_keyframes() will return 0.
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.
camera | The current camera used by the viewer. |
camera_width | Controls the size of the cameras. A good value can be 5% of the scene radius, or 10% of the character height (in walking mode), for instance. |
color | The color of the cameras. |
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.
camera | The current camera used by the viewer. |
thickness | The thickness of the path. |
color | The color of the path. |
float duration | ( | ) | const |
Returns the duration of the KeyFrameInterpolator path, expressed in seconds.
The duration simply corresponds to last_time() - first_time(). It returns 0.0 if the path has less than 2 keyframes.
float first_time | ( | ) | const |
Returns the time corresponding to the first keyframe, expressed in seconds.
Returns 0.0 if the path is empty.
|
inline |
Returns the associated Frame and that is interpolated by the KeyFrameInterpolator.
The returned Frame was set using set_frame() or with the KeyFrameInterpolator constructor. When is_interpolation_started(), this Frame's position and orientation will regularly be updated by a timer, so that they follow the KeyFrameInterpolator path.
|
inline |
Returns the desired frame rate. Default value is 30.
The frame_rate() multiplied by interpolation_period() is always equal to 1.0.
const std::vector< Frame > & interpolate | ( | ) |
Computes and returns all the interpolated frames.
|
inline |
Returns the interpolation method.
Default value is Interpolation
(cubic spline interpolation).
|
inline |
Returns the current interpolation period, expressed in milliseconds.
The update of the frame() state will be done by a timer at this period when is_interpolation_started(). This period (multiplied by interpolation_speed()) will be added to be the next frame's time. The interpolation_period() multiplied by frame_rate() is always equal to 1.0.
|
inline |
Returns the current interpolation speed.
Default value is 1.0, which means keyframe_time() will be matched during the interpolation (provided that your main loop is fast enough).
|
inline |
Returns whether the interpolation is being performed.
Use start_interpolation(), stop_interpolation() or toggleInterpolation() to modify this state.
true
if the interpolation is started, false
otherwise. Frame keyframe | ( | std::size_t | index | ) | const |
Returns the Frame associated with the keyframe at index
.
The index
has to be in the range [0, number_of_keyframes()-1].
index | The index of the keyframe. |
const quat & keyframe_orientation | ( | std::size_t | index | ) | const |
Returns the orientation of the index-th
keyframe.
The index
has to be in the range [0, number_of_keyframes()-1].
index | The index of the keyframe. |
const vec3 & keyframe_position | ( | std::size_t | index | ) | const |
Returns the position of the index-th
keyframe.
The index
has to be in the range [0, number_of_keyframes()-1].
index | The index of the keyframe. |
float keyframe_time | ( | std::size_t | index | ) | const |
Returns the time corresponding to the index-th
keyframe.
The index
has to be in the range [0, number_of_keyframes()-1].
index | The index of the keyframe. |
float last_time | ( | ) | const |
Returns the time corresponding to the last keyframe, expressed in seconds.
Returns 0.0 if the path is empty.
|
inline |
Returns the number of keyframes used by the interpolation.
Use add_keyframe() to add new keyframes.
bool read_keyframes | ( | const std::string & | file_name | ) |
Reads the camera path from a file.
file_name | The name of the file. |
true
if the keyframes were successfully read. bool save_keyframes | ( | const std::string & | file_name | ) | const |
Saves the camera path to a file.
file_name | The name of the file. |
true
if the keyframes were successfully saved.
|
inline |
Sets the frame() associated to the KeyFrameInterpolator.
frame | The frame to be associated with the KeyFrameInterpolator. |
void set_frame_rate | ( | int | fps | ) |
Sets the desired frame rate.
fps | The frame rate. |
void set_interpolation_method | ( | Method | m | ) |
Sets the interpolation_method().
m | The interpolation method. |
void set_interpolation_speed | ( | float | speed | ) |
Sets the interpolation_speed().
speed | The interpolation speed. |
void set_keyframe_orientation | ( | std::size_t | index, |
const quat & | q ) |
Sets the orientation of the index-th
keyframe.
The index
has to be in the range [0, number_of_keyframes()-1].
index | The index of the keyframe. |
q | The orientation to be set for the keyframe. |
void set_keyframe_position | ( | std::size_t | index, |
const vec3 & | pos ) |
Sets the position of the index-th
keyframe.
The index
has to be in the range [0, number_of_keyframes()-1].
index | The index of the keyframe. |
pos | The position to be set for the keyframe. |
void set_keyframe_time | ( | std::size_t | index, |
float | t ) |
Sets the time corresponding to the index-th
keyframe.
The index
has to be in the range [0, number_of_keyframes()-1].
index | The index of the keyframe. |
t | The time to be set for the keyframe. |
t
have to be monotonously increasing over keyframes. void start_interpolation | ( | ) |
Starts the interpolation process.
A timer is started with an interpolation_period() period that updates the frame()'s position and orientation. is_interpolation_started() will return true
until stop_interpolation() or toggleInterpolation() is called.
void stop_interpolation | ( | ) |
Stops an interpolation started with start_interpolation().