|
Easy3D 3.0.0
|
A keyframe interpolator for animation generation. More...
#include <easy3d/renderer/keyframe_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. | |
| bool | replace_keyframes (const std::vector< Frame > &frames, const std::vector< float > ×, const std::vector< float > &dwells) |
Atomically replaces the path with frames, times and optional dwells. | |
| 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, which is interpolated by the KeyframeInterpolator. | |
| void | set_frame (Frame *const frame) |
| Sets the frame() associated with 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. | |
| float | playback_elapsed () const |
| Returns the playback position in seconds, along the path's timeline (travel plus dwell). | |
Dwell time | |
| void | set_keyframe_dwell (std::size_t index, float seconds) |
Sets the dwell time of the index-th keyframe. | |
| float | keyframe_dwell (std::size_t index) const |
Returns the dwell time of the index-th keyframe, in seconds. | |
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. | |
| void | set_path_periodic (bool periodic) |
| Sets whether the path is interpolated as a closed periodic loop. | |
| bool | is_path_periodic () const |
Stream io | |
| Signal | frame_interpolated |
| Emitted when a frame is interpolated. | |
| Signal | interpolation_stopped |
| Emitted when the interpolation is stopped. | |
| bool | save_keyframes (std::ostream &output) const |
| Writes the camera path's keyframes to a stream. | |
| bool | load_keyframes (std::istream &input) |
| Reads the camera path's keyframes from a stream. | |
Interpolation | |
| bool | is_interpolation_started () const |
| Returns whether the interpolation is being performed. | |
| void | start_interpolation () |
| Starts the interpolation process. | |
| void | start_manual_interpolation () |
| Start interpolation without creating a timer or worker thread. | |
| bool | advance_interpolation () |
| Advance manual playback by one interpolation sample. | |
| void | seek_to_keyframe (std::size_t index) |
| Stops playback and positions the frame and playhead at the given keyframe. | |
| 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. | |
| float | total_duration () const |
| Returns the duration of the path, in seconds. | |
Path drawing | |
| 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. | |
| 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. | |
| static std::size_t | camera_glyph_triangles () |
| The number of triangles one camera glyph occupies in update_cameras_drawable(). | |
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 calls 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 calls 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 its duration has been 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 the 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. | bool advance_interpolation | ( | ) |
Advance manual playback by one interpolation sample.
|
static |
The number of triangles one camera glyph occupies in update_cameras_drawable().
The glyph mesh has a fixed topology, so the count is the same for every keyframe and does not depend on camera_width, the camera's field of view, the screen aspect or the number of keyframes; those only move the glyph vertices. In a drawable filled by update_cameras_drawable(), glyph i owns the triangles [i * camera_glyph_triangles(), (i + 1) * camera_glyph_triangles()), so a picked triangle t belongs to keyframe t / camera_glyph_triangles().
| 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.
| 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, which 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 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 toggle_interpolation() to modify this state.
true if the interpolation is started, false otherwise.
|
inline |
| 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. |
| float keyframe_dwell | ( | std::size_t | index | ) | const |
Returns the dwell time of the index-th keyframe, in seconds.
| 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.
| bool load_keyframes | ( | std::istream & | input | ) |
Reads the camera path's keyframes from a stream.
| input | The stream that supplies the keyframes. |
true if the keyframes were successfully read.
|
inline |
Returns the number of keyframes used by the interpolation.
Use add_keyframe() to add new keyframes.
| float playback_elapsed | ( | ) | const |
Returns the playback position in seconds, along the path's timeline (travel plus dwell).
The position of the sample the associated frame currently holds: 0 at the first sample, growing along the timeline and reaching the path's end. It is preserved while playback is paused and is 0 for an empty or newly edited path.
| 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.
Unlike repeated add_keyframe() calls, this preserves loaded-style paths with coincident poses or nonmonotone times. A size mismatch leaves the path unchanged and returns false. Periodic endpoint policy is preserved.
| frames | The replacement poses. |
| times | One timestamp per pose, in seconds. |
| dwells | Optional dwell seconds per pose; omitted entries default to zero. |
true when the path was replaced. | bool save_keyframes | ( | std::ostream & | output | ) | const |
Writes the camera path's keyframes to a stream.
| output | The stream that receives the keyframes. |
true if the keyframes were successfully written. | void seek_to_keyframe | ( | std::size_t | index | ) |
Stops playback and positions the frame and playhead at the given keyframe.
The index must be in [0, number_of_keyframes() - 1]. The elapsed time includes all preceding travel and dwell; manual playback resumes at the first interpolation sample at or after that position.
| index | The keyframe to seek to. |
|
inline |
Sets the frame() associated with the KeyframeInterpolator.
| frame | The frame to be associated with the KeyframeInterpolator. |
| void set_frame_rate | ( | int | fps | ) |
Sets the desired frame rate.
| fps | Frame rate in 1..1000. Changing it stops playback. |
| 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 | Positive finite interpolation speed. Changing it stops playback. |
| void set_keyframe_dwell | ( | std::size_t | index, |
| float | seconds | ||
| ) |
Sets the dwell time of the index-th keyframe.
The camera pauses for seconds at this keyframe before proceeding to the next one. The index has to be in the range [0, number_of_keyframes()-1].
| index | The index of the keyframe. |
| seconds | Seconds to pause at the keyframe (0 = no pause). |
| 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 has to be monotonically increasing over keyframes. | void set_path_periodic | ( | bool | periodic | ) |
Sets whether the path is interpolated as a closed periodic loop.
Periodic interpolation has an effect only for cubic interpolation with at least three distinct keyframes whose first and last poses are identical. It wraps position spline boundary conditions and orientation tangents across the duplicated closing keyframe. Without a dwell there, playback passes the closing pose at cruise speed instead of stopping at both copies. Otherwise interpolation remains open.
| periodic | Whether to use periodic endpoint handling. Changing it stops playback. |
| 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 toggle_interpolation() is called.
| void start_manual_interpolation | ( | ) |
Start interpolation without creating a timer or worker thread.
The host must call advance_interpolation() at interpolation_period() intervals. Stop and restart preserve the next sample; completion resets to the first. Use a fresh interpolator for manual mode: switching an instance that used timer playback is rejected.
| void stop_interpolation | ( | ) |
Stops an interpolation started with start_interpolation().
| float total_duration | ( | ) | const |
Returns the duration of the path, in seconds.
This is the timeline interpolate() samples: the travel time between consecutive keyframes plus the dwell time of every keyframe. At an interpolation speed of 1.0 it is also the duration of the playback and of a recorded video, which is what a UI shows next to "Record Video".
| 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.
A drawable filled here takes part in the normal depth pass: the back side of each glyph is hidden behind its front side, and scene surfaces occlude the glyphs (and the glyphs occlude them) on both backends. Add the drawable to the viewer's scene (e.g. with Viewer::add_drawable()) and refresh it when the keyframes, the camera's projection, or camera_width changes. Each glyph is a solid view frustum with its apex at the keyframe position, opening along that keyframe's view direction, with an up arrow on its front face; camera_width sets the front face's width, the screen aspect its height and the field of view its depth. Because the glyphs are ordinary triangles, they can also be picked: glyph i owns the triangles [i * camera_glyph_triangles(), (i + 1) * camera_glyph_triangles()).
| drawable | The drawable to fill; its buffers and color are replaced. |
| camera | The current camera; its field of view and screen aspect size the glyphs. |
| camera_width | Controls the size of the glyphs, in world units. A good value can be 5% of the scene radius, or 10% of the character height (in walking mode). |
| color | The color of the glyphs. |
false, leaving drawable untouched, if the path has no keyframe, if camera is null, or if camera_width is not a positive finite number. | 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.
The polyline takes part in the normal scene pass, so scene surfaces occlude it and it is drawn like any other drawable on both backends. Add the drawable to the viewer's scene (e.g. with Viewer::add_drawable()) and refresh it when the keyframes change. The line is a plain wide line (no cylinder impostor), so thickness is its on-screen width in pixels.
| drawable | The drawable to fill; its buffers, color and line style are replaced. |
| thickness | The thickness of the path, in pixels. |
| color | The color of the path. |
false, leaving drawable untouched, if the path has fewer than two interpolated samples (fewer than two keyframes, or interpolation failed).