Easy3D 3.0.0
Loading...
Searching...
No Matches
KeyframeInterpolator Class Reference

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 > &times, 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().
 

Detailed Description

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).

Note
The default duration of any two consecutive keyframes is the same. So for a smoother animation, it is suggested to regularly sample the viewpoints (as equally as possible).
Attention
If a Constraint is attached to the frame() (see Frame::constraint()), it should be deactivated before calling start_interpolation(), otherwise the interpolated motion (computed as if there was no constraint) will probably be erroneous.

Animation example:

// the KeyframeInterpolator kfi is given the camera's frame that it will be driven over time.
kfi = new KeyframeInterpolator( camera()->frame() );
kfi->add_keyframe(...);
kfi->add_keyframe(...);
// ...and so on for all the keyframes.
// starts animation
kfi->start_interpolation();
A keyframe interpolator for animation generation.
Definition keyframe_interpolator.h:94
Frame * frame() const
Returns the associated Frame, which is interpolated by the KeyframeInterpolator.
Definition keyframe_interpolator.h:195
Examples
Tutorial_206_CameraInterpolation.

Member Enumeration Documentation

◆ Method

enum Method

Interpolation method

Enumerator
FITTING 

Fit a curve to the keyframes.

INTERPOLATION 

Interpolate between keyframes.

Constructor & Destructor Documentation

◆ KeyframeInterpolator()

KeyframeInterpolator ( Frame *  frame = nullptr)
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.

Parameters
frameThe frame to be associated with the KeyframeInterpolator.

◆ ~KeyframeInterpolator()

~KeyframeInterpolator ( )
virtual

Virtual destructor. Clears the keyframe path.

Member Function Documentation

◆ add_keyframe() [1/2]

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).

Parameters
frameThe frame to be added as a keyframe.
Returns
true if the keyframe has been successfully added.

◆ add_keyframe() [2/2]

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.

Parameters
frameThe frame to be added as a keyframe.
timeThe time associated with the keyframe.
Returns
true if the keyframe has been successfully added.
Attention
The keyframe_time() has to be monotonically increasing over keyframes.

◆ advance_interpolation()

bool advance_interpolation ( )

Advance manual playback by one interpolation sample.

Returns
True when a sample was applied; false when manual playback is inactive.

◆ camera_glyph_triangles()

std::size_t camera_glyph_triangles ( )
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().

Returns
The glyph's triangle count (7 with the current mesh). Call this instead of hard-coding the number.
Note
The mapping assumes the drawable's element buffer holds nothing but these glyphs, i.e. it was filled by update_cameras_drawable() alone. Refill the drawable whenever the keyframes, the camera's projection or the glyph width change; a stale drawable maps picked triangles to the old keyframes.
See also
update_cameras_drawable()

◆ delete_last_keyframe()

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).

◆ delete_path()

void delete_path ( )

Removes all keyframes from the path.

Upon return, the number_of_keyframes() will return 0.

◆ duration()

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.

Returns
The duration of the path.
See also
keyframe_time().

◆ first_time()

float first_time ( ) const

Returns the time corresponding to the first keyframe, expressed in seconds.

Returns 0.0 if the path is empty.

Returns
The time of the first keyframe.
See also
last_time(), duration(), and keyframe_time().

◆ frame()

Frame * frame ( ) const
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.

Returns
The associated Frame.
See also
set_frame()

◆ frame_rate()

int frame_rate ( ) const
inline

Returns the desired frame rate. Default value is 30.

The frame_rate() multiplied by interpolation_period() is always equal to 1.0.

Returns
The frame rate.

◆ interpolate()

const std::vector< Frame > & interpolate ( )

Computes and returns all the interpolated frames.

Returns
A vector of interpolated frames.

◆ interpolation_method()

Method interpolation_method ( ) const
inline

Returns the interpolation method.

Default value is INTERPOLATION (cubic spline interpolation).

Returns
The interpolation method.

◆ interpolation_period()

int interpolation_period ( ) const
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.

Returns
The interpolation period in milliseconds.

◆ interpolation_speed()

float interpolation_speed ( ) const
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).

Returns
The interpolation speed.

◆ is_interpolation_started()

bool is_interpolation_started ( ) const
inline

Returns whether the interpolation is being performed.

Use start_interpolation(), stop_interpolation() or toggle_interpolation() to modify this state.

Returns
true if the interpolation is started, false otherwise.

◆ is_path_periodic()

bool is_path_periodic ( ) const
inline
Returns
Whether periodic endpoint handling was requested.

◆ keyframe()

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].

Parameters
indexThe index of the keyframe.
Returns
The Frame associated with the keyframe.
See also
keyframe_time()

◆ keyframe_dwell()

float keyframe_dwell ( std::size_t  index) const

Returns the dwell time of the index-th keyframe, in seconds.

Parameters
indexThe index of the keyframe.
Returns
The dwell time in seconds.
See also
set_keyframe_dwell()

◆ keyframe_orientation()

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].

Parameters
indexThe index of the keyframe.
Returns
The orientation of the keyframe.
See also
set_keyframe_orientation()

◆ keyframe_position()

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].

Parameters
indexThe index of the keyframe.
Returns
The position of the keyframe.
See also
set_keyframe_position()

◆ keyframe_time()

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].

Parameters
indexThe index of the keyframe.
Returns
The time corresponding to the keyframe.
See also
set_keyframe_time().

◆ last_time()

float last_time ( ) const

Returns the time corresponding to the last keyframe, expressed in seconds.

Returns 0.0 if the path is empty.

Returns
The time of the last keyframe.
See also
first_time(), duration(), and keyframe_time().

◆ load_keyframes()

bool load_keyframes ( std::istream &  input)

Reads the camera path's keyframes from a stream.

Parameters
inputThe stream that supplies the keyframes.
Returns
true if the keyframes were successfully read.

◆ number_of_keyframes()

std::size_t number_of_keyframes ( ) const
inline

Returns the number of keyframes used by the interpolation.

Use add_keyframe() to add new keyframes.

Returns
The number of keyframes.

◆ playback_elapsed()

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.

Returns
The playback position in seconds (0 for an empty path or no sample yet).
See also
total_duration(), keyframe_time().

◆ replace_keyframes()

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.

Parameters
framesThe replacement poses.
timesOne timestamp per pose, in seconds.
dwellsOptional dwell seconds per pose; omitted entries default to zero.
Returns
true when the path was replaced.

◆ save_keyframes()

bool save_keyframes ( std::ostream &  output) const

Writes the camera path's keyframes to a stream.

Parameters
outputThe stream that receives the keyframes.
Returns
true if the keyframes were successfully written.

◆ seek_to_keyframe()

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.

Parameters
indexThe keyframe to seek to.

◆ set_frame()

void set_frame ( Frame *const  frame)
inline

Sets the frame() associated with the KeyframeInterpolator.

Parameters
frameThe frame to be associated with the KeyframeInterpolator.

◆ set_frame_rate()

void set_frame_rate ( int  fps)

Sets the desired frame rate.

Parameters
fpsFrame rate in 1..1000. Changing it stops playback.

◆ set_interpolation_method()

void set_interpolation_method ( Method  m)

Sets the interpolation_method().

Parameters
mThe interpolation method.

◆ set_interpolation_speed()

void set_interpolation_speed ( float  speed)

Sets the interpolation_speed().

Parameters
speedPositive finite interpolation speed. Changing it stops playback.

◆ set_keyframe_dwell()

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].

Parameters
indexThe index of the keyframe.
secondsSeconds to pause at the keyframe (0 = no pause).
See also
keyframe_dwell()

◆ set_keyframe_orientation()

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].

Parameters
indexThe index of the keyframe.
qThe orientation to be set for the keyframe.
See also
keyframe_time()

◆ set_keyframe_position()

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].

Parameters
indexThe index of the keyframe.
posThe position to be set for the keyframe.
See also
keyframe_time()

◆ set_keyframe_time()

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].

Parameters
indexThe index of the keyframe.
tThe time to be set for the keyframe.
Note
The time t has to be monotonically increasing over keyframes.
See also
keyframe_time()

◆ set_path_periodic()

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.

Parameters
periodicWhether to use periodic endpoint handling. Changing it stops playback.

◆ start_interpolation()

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.

Note
The keyframes must be defined (see add_keyframe()) before you start_interpolation(), or else the interpolation will naturally immediately stop.

◆ start_manual_interpolation()

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.

◆ stop_interpolation()

void stop_interpolation ( )

◆ total_duration()

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".

Returns
The duration in seconds (0 for an empty path).
See also
keyframe_dwell(), duration()

◆ update_cameras_drawable()

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()).

Parameters
drawableThe drawable to fill; its buffers and color are replaced.
cameraThe current camera; its field of view and screen aspect size the glyphs.
camera_widthControls 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).
colorThe color of the glyphs.
Returns
false, leaving drawable untouched, if the path has no keyframe, if camera is null, or if camera_width is not a positive finite number.

◆ update_path_drawable()

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.

Parameters
drawableThe drawable to fill; its buffers, color and line style are replaced.
thicknessThe thickness of the path, in pixels.
colorThe color of the path.
Returns
false, leaving drawable untouched, if the path has fewer than two interpolated samples (fewer than two keyframes, or interpolation failed).

The documentation for this class was generated from the following files: