summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/assimp/code/LWOAnimation.h
blob: 257abecddd4a8735c36c5fada8e99dbd8edbf39f (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------

Copyright (c) 2006-2016, assimp team
All rights reserved.

Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:

* Redistributions of source code must retain the above
  copyright notice, this list of conditions and the
  following disclaimer.

* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the
  following disclaimer in the documentation and/or other
  materials provided with the distribution.

* Neither the name of the assimp team, nor the names of its
  contributors may be used to endorse or promote products
  derived from this software without specific prior
  written permission of the assimp team.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------
*/

/** @file  LWOAnimation.h
 *  @brief LWOAnimationResolver utility class
 *
 *  This is for all lightwave-related file format, not only LWO.
 *  LWS isthe main purpose.
*/
#ifndef AI_LWO_ANIMATION_INCLUDED
#define AI_LWO_ANIMATION_INCLUDED

#include <assimp/anim.h>
#include <vector>
#include <list>

struct aiNodeAnim;

namespace Assimp {
namespace LWO {

// ---------------------------------------------------------------------------
/** \brief List of recognized LWO envelopes
 */
enum EnvelopeType
{
    EnvelopeType_Position_X = 0x1,
    EnvelopeType_Position_Y = 0x2,
    EnvelopeType_Position_Z = 0x3,

    EnvelopeType_Rotation_Heading = 0x4,
    EnvelopeType_Rotation_Pitch = 0x5,
    EnvelopeType_Rotation_Bank = 0x6,

    EnvelopeType_Scaling_X = 0x7,
    EnvelopeType_Scaling_Y = 0x8,
    EnvelopeType_Scaling_Z = 0x9,

    // -- currently not yet handled
    EnvelopeType_Color_R = 0xa,
    EnvelopeType_Color_G = 0xb,
    EnvelopeType_Color_B = 0xc,

    EnvelopeType_Falloff_X = 0xd,
    EnvelopeType_Falloff_Y = 0xe,
    EnvelopeType_Falloff_Z = 0xf,

    EnvelopeType_Unknown
};

// ---------------------------------------------------------------------------
/** \brief List of recognized LWO interpolation modes
 */
enum InterpolationType
{
    IT_STEP, IT_LINE, IT_TCB, IT_HERM, IT_BEZI, IT_BEZ2
};


// ---------------------------------------------------------------------------
/** \brief List of recognized LWO pre or post range behaviours
 */
enum PrePostBehaviour
{
    PrePostBehaviour_Reset        = 0x0,
    PrePostBehaviour_Constant     = 0x1,
    PrePostBehaviour_Repeat       = 0x2,
    PrePostBehaviour_Oscillate    = 0x3,
    PrePostBehaviour_OffsetRepeat = 0x4,
    PrePostBehaviour_Linear       = 0x5
};

// ---------------------------------------------------------------------------
/** \brief Data structure for a LWO animation keyframe
 */
struct Key
{
    Key()
        : time(),
        value(),
        inter   (IT_LINE),
        params()
    {}

    //! Current time
    double time;

    //! Current value
    float value;

    //! How to interpolate this key with previous key?
    InterpolationType inter;

    //! Interpolation parameters
    float params[5];


    // for std::find()
    operator double () {
        return time;
    }
};

// ---------------------------------------------------------------------------
/** \brief Data structure for a LWO animation envelope
 */
struct Envelope
{
    Envelope()
        :   index()
        ,   type    (EnvelopeType_Unknown)
        ,   pre     (PrePostBehaviour_Constant)
        ,   post    (PrePostBehaviour_Constant)

        ,   old_first (0)
        ,   old_last  (0)
    {}

    //! Index of this envelope
    unsigned int index;

    //! Type of envelope
    EnvelopeType type;

    //! Pre and post-behaviour
    PrePostBehaviour pre,post;

    //! Keyframes for this envelope
    std::vector<Key> keys;


    // temporary data for AnimResolver
    size_t old_first,old_last;
};

// ---------------------------------------------------------------------------
//! @def AI_LWO_ANIM_FLAG_SAMPLE_ANIMS
//! Flag for AnimResolver, subsamples the input data with the rate specified
//! by AnimResolver::SetSampleRate().
#define AI_LWO_ANIM_FLAG_SAMPLE_ANIMS 0x1


// ---------------------------------------------------------------------------
//! @def AI_LWO_ANIM_FLAG_START_AT_ZERO
//! Flag for AnimResolver, ensures that the animations starts at zero.
#define AI_LWO_ANIM_FLAG_START_AT_ZERO 0x2

// ---------------------------------------------------------------------------
/** @brief Utility class to build Assimp animations from LWO envelopes.
 *
 *  Used for both LWO and LWS (MOT also).
 */
class AnimResolver
{
public:

    // ------------------------------------------------------------------
    /** @brief Construct an AnimResolver from a given list of envelopes
     *  @param envelopes Input envelopes. May be empty.
     *  @param Output tick rate, per second
     *  @note The input envelopes are possibly modified.
     */
    AnimResolver(std::list<Envelope>& envelopes,
        double tick);

public:

    // ------------------------------------------------------------------
    /** @brief Extract the bind-pose transformation matrix.
     *  @param out Receives bind-pose transformation matrix
     */
    void ExtractBindPose(aiMatrix4x4& out);

    // ------------------------------------------------------------------
    /** @brief Extract a node animation channel
     *  @param out Receives a pointer to a newly allocated node anim.
     *    If there's just one keyframe defined, *out is set to NULL and
     *    no animation channel is computed.
     *  @param flags Any combination of the AI_LWO_ANIM_FLAG_XXX flags.
     */
    void ExtractAnimChannel(aiNodeAnim** out, unsigned int flags = 0);


    // ------------------------------------------------------------------
    /** @brief Set the sampling rate for ExtractAnimChannel().
     *
     *  Non-linear interpolations are subsampled with this rate (keys
     *  per second). Closer sampling positions, if existent, are kept.
     *  The sampling rate defaults to 0, if this value is not changed and
     *  AI_LWO_ANIM_FLAG_SAMPLE_ANIMS is specified for ExtractAnimChannel(),
     *  the class finds a suitable sample rate by itself.
     */
    void SetSampleRate(double sr) {
        sample_rate = sr;
    }

    // ------------------------------------------------------------------
    /** @brief Getter for SetSampleRate()
     */
    double GetSampleRate() const {
        return sample_rate;
    }

    // ------------------------------------------------------------------
    /** @brief Set the animation time range
     *
     *  @param first Time where the animation starts, in ticks
     *  @param last  Time where the animation ends, in ticks
     */
    void SetAnimationRange(double _first, double _last) {
        first = _first;
        last  = _last;

        ClearAnimRangeSetup();
        UpdateAnimRangeSetup();
    }

protected:

    // ------------------------------------------------------------------
    /** @brief Build linearly subsampled keys from 3 single envelopes
     *  @param out Receives output keys
     *  @param envl_x X-component envelope
     *  @param envl_y Y-component envelope
     *  @param envl_z Z-component envelope
     *  @param flags Any combination of the AI_LWO_ANIM_FLAG_XXX flags.
     *  @note Up to two input envelopes may be NULL
     */
    void GetKeys(std::vector<aiVectorKey>& out,
        LWO::Envelope* envl_x,
        LWO::Envelope* envl_y,
        LWO::Envelope* envl_z,
        unsigned int flags);

    // ------------------------------------------------------------------
    /** @brief Resolve a single animation key by applying the right
     *  interpolation to it.
     *  @param cur Current key
     *  @param envl Envelope working on
     *  @param time time to be interpolated
     *  @param fill Receives the interpolated output value.
     */
    void DoInterpolation(std::vector<LWO::Key>::const_iterator cur,
        LWO::Envelope* envl,double time, float& fill);

    // ------------------------------------------------------------------
    /** @brief Almost the same, except we won't handle pre/post
     *  conditions here.
     *  @see DoInterpolation
     */
    void DoInterpolation2(std::vector<LWO::Key>::const_iterator beg,
        std::vector<LWO::Key>::const_iterator end,double time, float& fill);

    // ------------------------------------------------------------------
    /** @brief Interpolate 2 tracks if one is given
     *
     *  @param out Receives extra output keys
     *  @param key_out Primary output key
     *  @param time Time to interpolate for
     */
    void InterpolateTrack(std::vector<aiVectorKey>& out,
        aiVectorKey& key_out,double time);

    // ------------------------------------------------------------------
    /** @brief Subsample an animation track by a given sampling rate
     *
     *  @param out Receives output keys. Last key at input defines the
     *    time where subsampling starts.
     *  @param time Time to end subsampling at
     *  @param sample_delta Time delta between two samples
     */
    void SubsampleAnimTrack(std::vector<aiVectorKey>& out,
        double time,double sample_delta);

    // ------------------------------------------------------------------
    /** @brief Delete all keys which we inserted to match anim setup
     */
    void ClearAnimRangeSetup();

    // ------------------------------------------------------------------
    /** @brief Insert extra keys to match LWO's pre and post behaviours
     *  in a given time range [first...last]
     */
    void UpdateAnimRangeSetup();

private:
    std::list<Envelope>& envelopes;
    double sample_rate;

    LWO::Envelope* trans_x, *trans_y, *trans_z;
    LWO::Envelope* rotat_x, *rotat_y, *rotat_z;
    LWO::Envelope* scale_x, *scale_y, *scale_z;

    double first, last;
    bool need_to_setup;

    // temporary storage
    LWO::Envelope* envl_x, * envl_y, * envl_z;
    std::vector<LWO::Key>::const_iterator cur_x,cur_y,cur_z;
    bool end_x, end_y, end_z;

    unsigned int flags;
    double sample_delta;
};

} // end namespace LWO
} // end namespace Assimp

#endif // !! AI_LWO_ANIMATION_INCLUDED