본 글은 제가 경험해본 코드 중 좋은 구조를 가졌다고 생각되는 코드에 대해 개인적인 스타일 및 주관으로 수정하여 정의한 것입니다. 구현부는 없습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
typedef unsigned short  uint16;
typedef unsigned char   uint8;
template <class StorageType> class Interpolator;
 
template<typename Storage>
struct KeyNode
{
    Storage vlaue;
    float   time;
};
 
template<class Storage>
class KeyTrack
{
    typedef KeyNode<Storage> TimeValue;
public:
    void    Init();
 
    inline void AddKey(const float time, const Storage& value);
    inline void SetKey(const uint16 keyNr, const float time, const Storage& value);
    inline void RemoveKey(const uint16 keyNr);
    void    ClearKeys();
 
    Storage GetValueAtTime(const float currentTime, uint16* cachedKey=NULL);
 
    inline TimeValue* GetKey(const uint16 nr);
    inline TimeValue* GetFirstKey();
    inline TimeValue* GetLastKey();
 
    inline float GetFirstTime();
    inline float GetLastTime();
    inline uint16 GetNumKeys() const;
 
    inline TimeValue* FindKey(const float curTime);
 
    void SetInterpolator(Interpolator<Storage>* newInterpolator, bool needDestroy = true);
    inline Interpolator<Storage>* GetInterpolator();
 
protected:
    std::vector<TimeValue>          mKeys;
    Interpolator<Storage>           mInterpolator;
    bool                            mDestroyInterpolator;   //< flag to auto delete
};
 
 
// 시간 없어서 정리 못함
 
 
template <class DataType>
class Interpolators
{
public:
    // support type
    enum {
        TYPE_LINEAR = 0,
        TYPE_HERMITE,
        TYPE_BEZIER,
    };
    Interpolators(void) {};
    virtual ~Interpolators(void) {};
 
    virtual DataType Interpolate(const uint32 startKey, const float timeValue, KeyTrack<DataType>* interpolationSource) const = 0;
    virtual uint32 GetType() const = 0;
};
 
 
template <class ValueT>
class LinearInterpolator : public Interpolator<ValueT>
{
public:
    LinearInterpolator(void);
    ~LinearInterpolator(void);
 
    virtual ValueT Interpolate(const uint32 startKey, const float timeValue, KeyTrack<ValueT>* interpolationSource) const
    {
        KeyNode<ValueT>* firstKey = interpolationSource->GetKey( startKey );
        KeyNode<ValueT>* nextKey  = interpolationSource->GetKey( startKey + 1 );
 
        return (1.0 - timeValue) * firstKey->vlaue + timeValue * nextKey->vlaue;
    }
 
    virtual uint32 GetType() const                  { return TYPE_LINEAR; }
};
 
//< need template specialization
class LinearQuaternionInterpolator : public LinearInterpolator<Quaternion>
{
    Quaternion Interpolate(const uint32 startKey, const float timeValue, KeyTrack<Quaternion>* interpolationSource) const
    {
        KeyNode<Quaternion>* firstKey = interpolationSource->GetKey( startKey );
        KeyNode<Quaternion>* nextKey = interpolationSource->GetKey( startKey + 1 );
 
        return slerp(firstKey->value, nextKey->value, timeValue);
    }
};
 
 
template <class ValueT>
class HermiteInterpolator : public Interpolator<ValueT>
{
    virtual ValueT Interpolate(const uint32 startKey, const float timeValue, ) const {
 
        //              [ 2 -2  1  1]   [a]
        //[u3 u2 u 1]   [-3  3 -2 -1]   [b]
        //              [ 0  0  1  0]   [ta]
        //              [ 1  0  0  0]   [tb]
 
        // precalc u, u2 and u3
        const float t  = timeValue;
        const float t2 = t * t;
        const float t3 = t2  * t;
 
        return  ( 2*t3 + -3*t2 + 1) * this->mInterpolationSource->GetKey( startKey )->GetValue() +
                (-2*t3 +  3*t2)     * this->mInterpolationSource->GetKey( startKey + 1 )->GetValue() +
                (   t3 + -2*t2 + t) * mTangents[ startKey ] +
                (   t3 + -t2)       * mTangents[ startKey + 1 ];
    }
 
    virtual void Init(Interpolator<ValueT>* interpolateSource);     // tangent initialize.
    {
        assert(this->mInterpolationSource); // make sure we have a keytrack assigned
 
        // allocate the new tangents
        const uint32 numKeys = this->mInterpolationSource->GetNumKeys();
        mTangents = (ReturnType*)MCore::Realloc(mTangents, sizeof(ReturnType) * numKeys, MEMCATEGORY_MOTIONS_INTERPOLATORS);
 
        // calculate all tangents
        for (uint32 i=0; i<numKeys; ++i)
        {
            // if there is no previous or next key
            if (i==0 || i==numKeys-1)
            {
                ReturnType tangent;
                MCore::MemSet(&tangent, 0, sizeof(ReturnType));
                mTangents[i] = tangent;        
            }
            else    // if there is a previous or next key
            {
                const ReturnType& prevValue = this->mInterpolationSource->GetKey(i-1)->GetValue();
                const ReturnType& value     = this->mInterpolationSource->GetKey(i  )->GetValue();
                const ReturnType& nextValue = this->mInterpolationSource->GetKey(i+1)->GetValue();
                mTangents[i] = (0.5 * (value - prevValue)) + (0.5 * (nextValue - value));
            }
        }
    }
    ValueT& GetTangent(const uint32 keyNr);
protected:
    ValueT*     mTangents;  /**< The tangent vectors, one for each key. */
    KeyTrack<ValueT>    mInterpolationSource;
};
// the interpolation method
template <class StorageType>
MCore::Quaternion HermiteQuaternionInterpolator<StorageType>::Interpolate(const uint32 startKey, const float timeValue) const
{
    // get the two quaternions
    MCore::Quaternion a = this->mInterpolationSource->GetKey( startKey )->GetValue();
    MCore::Quaternion b = this->mInterpolationSource->GetKey( startKey + 1 )->GetValue();
 
    // check if both quaternions are on the same hypersphere or not, if not, invert one
    if (a.Dot(b) < 0.0)
        a = -a;
 
    //              [ 2 -2  1  1]   [a]
    //[u3 u2 u 1]   [-3  3 -2 -1]   [b]
    //              [ 0  0  1  0]   [ta]
    //              [ 1  0  0  0]   [tb]
 
    // interpolate, and take the exponent of that, which is the interpolated quaternion
    const float t  = timeValue;
    const float t2 = t * t;
    const float t3 = t2  * t;
 
    return (( 2*t3 + -3*t2 + 1) * a.LogN()  +
        (-2*t3 +  3*t2)     * b.LogN()  +
        (   t3 + -2*t2 + t) * this->mTangents[startKey] +
        (   t3 + -t2)       * this->mTangents[startKey+1] ).Exp().Normalize();
}