# template class cogs::Interpolator

## Overview

Wrapper class for key frame animation offering effective interpolation between values in time. More…

    #include <Interpolator.h>

    template <typename Type>
    class Interpolator {
    public:
        // structs

        struct Keyframe;

        // construction

        Interpolator();
        Interpolator(const std::initializer_list<std::pair<float, Type>>& keys);

        // methods

        void Clear();
        void ClearAndSet(const std::initializer_list<std::pair<float, Type>>& keys);
        bool IsEmpty() const;
        size_t GetKeyCount() const;
        void InsertKey(const float time, Type value);
        Type GetValueAtTime(const float time) const;
        Type GetValueAtTimeCubic(const float time) const;
        Type GetKeyValue(const size_t key_index) const;
        float GetTimeValue(const size_t key_index) const;
        void SetKeyValue(const size_t key_index, const Type value);
        float GetLastKeyTime() const;
        int32_t GetKeyBefore(const float time) const;
        void Reserve(size_t keyframe_count);
        void ShrinkToFit();
        void RemoveLastKey();
    };

## Detailed Documentation

Wrapper class for key frame animation offering effective interpolation between values in time.

### Construction

    Interpolator()

Creates an Action with no keys.

    Interpolator(const std::initializer_list<std::pair<float, Type>>& keys)

Creates an Action from list of <time,value> pairs.

### Methods

    void Clear()

Deletes current all current key frames.

    void ClearAndSet(const std::initializer_list<std::pair<float, Type>>& keys)

Deletes current key array and sets up new array from list of <time,value> pairs.

    bool IsEmpty() const

Returns true when no key exist.

    size_t GetKeyCount() const

Returns number of key frames.

    void InsertKey(const float time, Type value)

Creates new key frame with specified value and inserts it into array.

    Type GetValueAtTime(const float time) const

Binary searches two keys nearest to specified time and interpolates between them.

Returns:

Zero when keys are empty. Value of first key if time is lower or equal to the time of first key. Value of last key if time is higher or equal to the time of first key. Otherwise, an interpolated value between two closest keys.

    Type GetKeyValue(const size_t key_index) const

Returns value of key on specified index (position in array ordered by time).

    float GetTimeValue(const size_t key_index) const

Returns time of key on specified index.

    void SetKeyValue(const size_t key_index, const Type value)

Returns value of key on specified index (position in array ordered by time).

    float GetLastKeyTime() const

Returns time of last existing key frame.

    int32_t GetKeyBefore(const float time) const

Returns index of key at, or immediately before specified time.

Returns:

A highest key index for which key.time <= time. Returns -1 if keys are empty or specified time is lower then time of any key.

    void Reserve(size_t keyframe_count)

Reserves required memory for specified amount of key frames. Improves speed of adding new key frames.

    void ShrinkToFit()

Releases allocated and not used memory for new frames.