adding goat3d to the project
[deeprace] / libs / goat3d / src / track.h
1 /*
2 libanim - hierarchical keyframe animation library
3 Copyright (C) 2012-2023 John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* An animation track defines the values of a single scalar over time
20  * and supports various interpolation and extrapolation modes.
21  */
22 #ifndef LIBANIM_TRACK_H_
23 #define LIBANIM_TRACK_H_
24
25 #include <limits.h>
26 /*#include "config.h"*/
27
28 enum anm_interpolator {
29         ANM_INTERP_STEP,
30         ANM_INTERP_LINEAR,
31         ANM_INTERP_CUBIC
32 };
33
34 enum anm_extrapolator {
35         ANM_EXTRAP_EXTEND,      /* extend to infinity */
36         ANM_EXTRAP_CLAMP,       /* clamp to last value */
37         ANM_EXTRAP_REPEAT,      /* repeat motion */
38         ANM_EXTRAP_PINGPONG     /* repeat with mirroring */
39 };
40
41 typedef long anm_time_t;
42 #define ANM_TIME_MIN    LONG_MIN
43 #define ANM_TIME_MAX    LONG_MAX
44 #define ANM_TIME_INVAL  LONG_MIN
45
46 #define ANM_SEC2TM(x)   ((anm_time_t)((x) * 1000))
47 #define ANM_MSEC2TM(x)  ((anm_time_t)(x))
48 #define ANM_TM2SEC(x)   ((x) / 1000.0)
49 #define ANM_TM2MSEC(x)  (x)
50
51 struct anm_keyframe {
52         anm_time_t time;
53         float val;
54 };
55
56 struct anm_track {
57         char *name;
58         int count;
59         struct anm_keyframe *keys;
60
61         float def_val;
62
63         enum anm_interpolator interp;
64         enum anm_extrapolator extrap;
65
66         int keys_sorted;
67 };
68
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72
73 /* track constructor and destructor */
74 int anm_init_track(struct anm_track *track);
75 void anm_destroy_track(struct anm_track *track);
76
77 /* helper functions that use anm_init_track and anm_destroy_track internally */
78 struct anm_track *anm_create_track(void);
79 void anm_free_track(struct anm_track *track);
80
81 /* copies track src to dest
82  * XXX: dest must have been initialized first
83  */
84 void anm_copy_track(struct anm_track *dest, const struct anm_track *src);
85
86 int anm_set_track_name(struct anm_track *track, const char *name);
87 const char *anm_get_track_name(const struct anm_track *track);
88
89 void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in);
90 void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex);
91
92 anm_time_t anm_remap_time(const struct anm_track *track, anm_time_t tm,
93                 anm_time_t start, anm_time_t end);
94
95 void anm_set_track_default(struct anm_track *track, float def);
96
97 /* set or update a keyframe */
98 int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key);
99
100 /* get the idx-th keyframe, returns null if it doesn't exist */
101 struct anm_keyframe *anm_get_keyframe(const struct anm_track *track, int idx);
102
103 /* Finds the 0-based index of the intra-keyframe interval which corresponds
104  * to the specified time. If the time falls exactly onto the N-th keyframe
105  * the function returns N.
106  *
107  * Special cases:
108  * - if the time is before the first keyframe -1 is returned.
109  * - if the time is after the last keyframe, the index of the last keyframe
110  *   is returned.
111  */
112 int anm_get_key_interval(const struct anm_track *track, anm_time_t tm);
113
114 int anm_set_value(struct anm_track *track, anm_time_t tm, float val);
115
116 /* evaluates and returns the value of the track for a particular time */
117 float anm_get_value(const struct anm_track *track, anm_time_t tm);
118
119 /* evaluates a set of 4 tracks treated as a quaternion, to perform slerp instead
120  * of linear interpolation. Result is returned through the last argument, which
121  * is expected to point to an array of 4 floats (x,y,z,w)
122  */
123 void anm_get_quat(const struct anm_track *xtrk, const struct anm_track *ytrk,
124                 const struct anm_track *ztrk, const struct anm_track *wtrk, anm_time_t tm, float *qres);
125
126 #ifdef __cplusplus
127 }
128 #endif
129
130
131 #endif  /* LIBANIM_TRACK_H_ */