rev |
line source |
nuclear@27
|
1 #include <stdlib.h>
|
nuclear@27
|
2 #include <string.h>
|
nuclear@27
|
3 #include <assert.h>
|
nuclear@27
|
4 #include "track.h"
|
nuclear@27
|
5 #include "dynarr.h"
|
nuclear@27
|
6
|
nuclear@27
|
7 static int keycmp(const void *a, const void *b);
|
nuclear@27
|
8 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm);
|
nuclear@27
|
9
|
nuclear@27
|
10 static float interp_step(float v0, float v1, float v2, float v3, float t);
|
nuclear@27
|
11 static float interp_linear(float v0, float v1, float v2, float v3, float t);
|
nuclear@27
|
12 static float interp_cubic(float v0, float v1, float v2, float v3, float t);
|
nuclear@27
|
13
|
nuclear@27
|
14 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end);
|
nuclear@27
|
15 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end);
|
nuclear@27
|
16 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end);
|
nuclear@27
|
17 static anm_time_t remap_pingpong(anm_time_t tm, anm_time_t start, anm_time_t end);
|
nuclear@27
|
18
|
nuclear@27
|
19 /* XXX keep this in sync with enum anm_interpolator at track.h */
|
nuclear@27
|
20 static float (*interp[])(float, float, float, float, float) = {
|
nuclear@27
|
21 interp_step,
|
nuclear@27
|
22 interp_linear,
|
nuclear@27
|
23 interp_cubic,
|
nuclear@27
|
24 0
|
nuclear@27
|
25 };
|
nuclear@27
|
26
|
nuclear@27
|
27 /* XXX keep this in sync with enum anm_extrapolator at track.h */
|
nuclear@27
|
28 static anm_time_t (*remap_time[])(anm_time_t, anm_time_t, anm_time_t) = {
|
nuclear@27
|
29 remap_extend,
|
nuclear@27
|
30 remap_clamp,
|
nuclear@27
|
31 remap_repeat,
|
nuclear@27
|
32 remap_pingpong,
|
nuclear@27
|
33 0
|
nuclear@27
|
34 };
|
nuclear@27
|
35
|
nuclear@27
|
36 int anm_init_track(struct anm_track *track)
|
nuclear@27
|
37 {
|
nuclear@27
|
38 memset(track, 0, sizeof *track);
|
nuclear@27
|
39
|
nuclear@27
|
40 if(!(track->keys = dynarr_alloc(0, sizeof *track->keys))) {
|
nuclear@27
|
41 return -1;
|
nuclear@27
|
42 }
|
nuclear@27
|
43 track->interp = ANM_INTERP_LINEAR;
|
nuclear@27
|
44 track->extrap = ANM_EXTRAP_CLAMP;
|
nuclear@27
|
45 return 0;
|
nuclear@27
|
46 }
|
nuclear@27
|
47
|
nuclear@27
|
48 void anm_destroy_track(struct anm_track *track)
|
nuclear@27
|
49 {
|
nuclear@27
|
50 dynarr_free(track->keys);
|
nuclear@27
|
51 }
|
nuclear@27
|
52
|
nuclear@27
|
53 struct anm_track *anm_create_track(void)
|
nuclear@27
|
54 {
|
nuclear@27
|
55 struct anm_track *track;
|
nuclear@27
|
56
|
nuclear@27
|
57 if((track = malloc(sizeof *track))) {
|
nuclear@27
|
58 if(anm_init_track(track) == -1) {
|
nuclear@27
|
59 free(track);
|
nuclear@27
|
60 return 0;
|
nuclear@27
|
61 }
|
nuclear@27
|
62 }
|
nuclear@27
|
63 return track;
|
nuclear@27
|
64 }
|
nuclear@27
|
65
|
nuclear@27
|
66 void anm_free_track(struct anm_track *track)
|
nuclear@27
|
67 {
|
nuclear@27
|
68 anm_destroy_track(track);
|
nuclear@27
|
69 free(track);
|
nuclear@27
|
70 }
|
nuclear@27
|
71
|
nuclear@49
|
72 void anm_copy_track(struct anm_track *dest, const struct anm_track *src)
|
nuclear@27
|
73 {
|
nuclear@27
|
74 free(dest->name);
|
nuclear@27
|
75 if(dest->keys) {
|
nuclear@27
|
76 dynarr_free(dest->keys);
|
nuclear@27
|
77 }
|
nuclear@27
|
78
|
nuclear@27
|
79 if(src->name) {
|
nuclear@27
|
80 dest->name = malloc(strlen(src->name) + 1);
|
nuclear@27
|
81 strcpy(dest->name, src->name);
|
nuclear@27
|
82 }
|
nuclear@27
|
83
|
nuclear@27
|
84 dest->count = src->count;
|
nuclear@27
|
85 dest->keys = dynarr_alloc(src->count, sizeof *dest->keys);
|
nuclear@27
|
86 memcpy(dest->keys, src->keys, src->count * sizeof *dest->keys);
|
nuclear@27
|
87
|
nuclear@27
|
88 dest->def_val = src->def_val;
|
nuclear@27
|
89 dest->interp = src->interp;
|
nuclear@27
|
90 dest->extrap = src->extrap;
|
nuclear@27
|
91 }
|
nuclear@27
|
92
|
nuclear@27
|
93 int anm_set_track_name(struct anm_track *track, const char *name)
|
nuclear@27
|
94 {
|
nuclear@27
|
95 char *tmp;
|
nuclear@27
|
96
|
nuclear@27
|
97 if(!(tmp = malloc(strlen(name) + 1))) {
|
nuclear@27
|
98 return -1;
|
nuclear@27
|
99 }
|
nuclear@27
|
100 free(track->name);
|
nuclear@27
|
101 track->name = tmp;
|
nuclear@27
|
102 return 0;
|
nuclear@27
|
103 }
|
nuclear@27
|
104
|
nuclear@27
|
105 const char *anm_get_track_name(struct anm_track *track)
|
nuclear@27
|
106 {
|
nuclear@27
|
107 return track->name;
|
nuclear@27
|
108 }
|
nuclear@27
|
109
|
nuclear@27
|
110 void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in)
|
nuclear@27
|
111 {
|
nuclear@27
|
112 track->interp = in;
|
nuclear@27
|
113 }
|
nuclear@27
|
114
|
nuclear@27
|
115 void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex)
|
nuclear@27
|
116 {
|
nuclear@27
|
117 track->extrap = ex;
|
nuclear@27
|
118 }
|
nuclear@27
|
119
|
nuclear@27
|
120 anm_time_t anm_remap_time(struct anm_track *track, anm_time_t tm, anm_time_t start, anm_time_t end)
|
nuclear@27
|
121 {
|
nuclear@27
|
122 return remap_time[track->extrap](tm, start, end);
|
nuclear@27
|
123 }
|
nuclear@27
|
124
|
nuclear@27
|
125 void anm_set_track_default(struct anm_track *track, float def)
|
nuclear@27
|
126 {
|
nuclear@27
|
127 track->def_val = def;
|
nuclear@27
|
128 }
|
nuclear@27
|
129
|
nuclear@27
|
130 int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key)
|
nuclear@27
|
131 {
|
nuclear@27
|
132 int idx = anm_get_key_interval(track, key->time);
|
nuclear@27
|
133
|
nuclear@27
|
134 /* if we got a valid keyframe index, compare them... */
|
nuclear@27
|
135 if(idx >= 0 && idx < track->count && keycmp(key, track->keys + idx) == 0) {
|
nuclear@27
|
136 /* ... it's the same key, just update the value */
|
nuclear@27
|
137 track->keys[idx].val = key->val;
|
nuclear@27
|
138 } else {
|
nuclear@27
|
139 /* ... it's a new key, add it and re-sort them */
|
nuclear@27
|
140 void *tmp;
|
nuclear@27
|
141 if(!(tmp = dynarr_push(track->keys, key))) {
|
nuclear@27
|
142 return -1;
|
nuclear@27
|
143 }
|
nuclear@27
|
144 track->keys = tmp;
|
nuclear@27
|
145 /* TODO lazy qsort */
|
nuclear@27
|
146 qsort(track->keys, ++track->count, sizeof *track->keys, keycmp);
|
nuclear@27
|
147 }
|
nuclear@27
|
148 return 0;
|
nuclear@27
|
149 }
|
nuclear@27
|
150
|
nuclear@27
|
151 static int keycmp(const void *a, const void *b)
|
nuclear@27
|
152 {
|
nuclear@27
|
153 return ((struct anm_keyframe*)a)->time - ((struct anm_keyframe*)b)->time;
|
nuclear@27
|
154 }
|
nuclear@27
|
155
|
nuclear@27
|
156 struct anm_keyframe *anm_get_keyframe(struct anm_track *track, int idx)
|
nuclear@27
|
157 {
|
nuclear@27
|
158 if(idx < 0 || idx >= track->count) {
|
nuclear@27
|
159 return 0;
|
nuclear@27
|
160 }
|
nuclear@27
|
161 return track->keys + idx;
|
nuclear@27
|
162 }
|
nuclear@27
|
163
|
nuclear@27
|
164 int anm_get_key_interval(struct anm_track *track, anm_time_t tm)
|
nuclear@27
|
165 {
|
nuclear@27
|
166 int last;
|
nuclear@27
|
167
|
nuclear@27
|
168 if(!track->count || tm < track->keys[0].time) {
|
nuclear@27
|
169 return -1;
|
nuclear@27
|
170 }
|
nuclear@27
|
171
|
nuclear@27
|
172 last = track->count - 1;
|
nuclear@27
|
173 if(tm > track->keys[last].time) {
|
nuclear@27
|
174 return last;
|
nuclear@27
|
175 }
|
nuclear@27
|
176
|
nuclear@27
|
177 return find_prev_key(track->keys, 0, last, tm);
|
nuclear@27
|
178 }
|
nuclear@27
|
179
|
nuclear@27
|
180 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm)
|
nuclear@27
|
181 {
|
nuclear@27
|
182 int mid;
|
nuclear@27
|
183
|
nuclear@27
|
184 if(end - start <= 1) {
|
nuclear@27
|
185 return start;
|
nuclear@27
|
186 }
|
nuclear@27
|
187
|
nuclear@27
|
188 mid = (start + end) / 2;
|
nuclear@27
|
189 if(tm < arr[mid].time) {
|
nuclear@27
|
190 return find_prev_key(arr, start, mid, tm);
|
nuclear@27
|
191 }
|
nuclear@27
|
192 if(tm > arr[mid].time) {
|
nuclear@27
|
193 return find_prev_key(arr, mid, end, tm);
|
nuclear@27
|
194 }
|
nuclear@27
|
195 return mid;
|
nuclear@27
|
196 }
|
nuclear@27
|
197
|
nuclear@27
|
198 int anm_set_value(struct anm_track *track, anm_time_t tm, float val)
|
nuclear@27
|
199 {
|
nuclear@27
|
200 struct anm_keyframe key;
|
nuclear@27
|
201 key.time = tm;
|
nuclear@27
|
202 key.val = val;
|
nuclear@27
|
203
|
nuclear@27
|
204 return anm_set_keyframe(track, &key);
|
nuclear@27
|
205 }
|
nuclear@27
|
206
|
nuclear@27
|
207 float anm_get_value(struct anm_track *track, anm_time_t tm)
|
nuclear@27
|
208 {
|
nuclear@27
|
209 int idx0, idx1, last_idx;
|
nuclear@27
|
210 anm_time_t tstart, tend;
|
nuclear@27
|
211 float t, dt;
|
nuclear@27
|
212 float v0, v1, v2, v3;
|
nuclear@27
|
213
|
nuclear@27
|
214 if(!track->count) {
|
nuclear@27
|
215 return track->def_val;
|
nuclear@27
|
216 }
|
nuclear@27
|
217
|
nuclear@27
|
218 last_idx = track->count - 1;
|
nuclear@27
|
219
|
nuclear@27
|
220 tstart = track->keys[0].time;
|
nuclear@27
|
221 tend = track->keys[last_idx].time;
|
nuclear@27
|
222
|
nuclear@27
|
223 if(tstart == tend) {
|
nuclear@27
|
224 return track->keys[0].val;
|
nuclear@27
|
225 }
|
nuclear@27
|
226
|
nuclear@27
|
227 tm = remap_time[track->extrap](tm, tstart, tend);
|
nuclear@27
|
228
|
nuclear@27
|
229 idx0 = anm_get_key_interval(track, tm);
|
nuclear@27
|
230 assert(idx0 >= 0 && idx0 < track->count);
|
nuclear@27
|
231 idx1 = idx0 + 1;
|
nuclear@27
|
232
|
nuclear@27
|
233 if(idx0 == last_idx) {
|
nuclear@27
|
234 return track->keys[idx0].val;
|
nuclear@27
|
235 }
|
nuclear@27
|
236
|
nuclear@27
|
237 dt = (float)(track->keys[idx1].time - track->keys[idx0].time);
|
nuclear@27
|
238 t = (float)(tm - track->keys[idx0].time) / dt;
|
nuclear@27
|
239
|
nuclear@27
|
240 v1 = track->keys[idx0].val;
|
nuclear@27
|
241 v2 = track->keys[idx1].val;
|
nuclear@27
|
242
|
nuclear@27
|
243 /* get the neigboring values to allow for cubic interpolation */
|
nuclear@27
|
244 v0 = idx0 > 0 ? track->keys[idx0 - 1].val : v1;
|
nuclear@27
|
245 v3 = idx1 < last_idx ? track->keys[idx1 + 1].val : v2;
|
nuclear@27
|
246
|
nuclear@27
|
247 return interp[track->interp](v0, v1, v2, v3, t);
|
nuclear@27
|
248 }
|
nuclear@27
|
249
|
nuclear@27
|
250
|
nuclear@27
|
251 static float interp_step(float v0, float v1, float v2, float v3, float t)
|
nuclear@27
|
252 {
|
nuclear@27
|
253 return v1;
|
nuclear@27
|
254 }
|
nuclear@27
|
255
|
nuclear@27
|
256 static float interp_linear(float v0, float v1, float v2, float v3, float t)
|
nuclear@27
|
257 {
|
nuclear@27
|
258 return v1 + (v2 - v1) * t;
|
nuclear@27
|
259 }
|
nuclear@27
|
260
|
nuclear@27
|
261 static float interp_cubic(float a, float b, float c, float d, float t)
|
nuclear@27
|
262 {
|
nuclear@27
|
263 float x, y, z, w;
|
nuclear@27
|
264 float tsq = t * t;
|
nuclear@27
|
265
|
nuclear@27
|
266 x = -a + 3.0 * b - 3.0 * c + d;
|
nuclear@27
|
267 y = 2.0 * a - 5.0 * b + 4.0 * c - d;
|
nuclear@27
|
268 z = c - a;
|
nuclear@27
|
269 w = 2.0 * b;
|
nuclear@27
|
270
|
nuclear@27
|
271 return 0.5 * (x * tsq * t + y * tsq + z * t + w);
|
nuclear@27
|
272 }
|
nuclear@27
|
273
|
nuclear@27
|
274 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end)
|
nuclear@27
|
275 {
|
nuclear@27
|
276 return remap_repeat(tm, start, end);
|
nuclear@27
|
277 }
|
nuclear@27
|
278
|
nuclear@27
|
279 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end)
|
nuclear@27
|
280 {
|
nuclear@27
|
281 if(start == end) {
|
nuclear@27
|
282 return start;
|
nuclear@27
|
283 }
|
nuclear@27
|
284 return tm < start ? start : (tm >= end ? end - 1 : tm);
|
nuclear@27
|
285 }
|
nuclear@27
|
286
|
nuclear@27
|
287 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end)
|
nuclear@27
|
288 {
|
nuclear@27
|
289 anm_time_t x, interv = end - start;
|
nuclear@27
|
290
|
nuclear@27
|
291 if(interv == 0) {
|
nuclear@27
|
292 return start;
|
nuclear@27
|
293 }
|
nuclear@27
|
294
|
nuclear@27
|
295 x = (tm - start) % interv;
|
nuclear@27
|
296 if(x < 0) {
|
nuclear@27
|
297 x += interv;
|
nuclear@27
|
298 }
|
nuclear@27
|
299 return x + start;
|
nuclear@27
|
300
|
nuclear@27
|
301 /*if(tm < start) {
|
nuclear@27
|
302 while(tm < start) {
|
nuclear@27
|
303 tm += interv;
|
nuclear@27
|
304 }
|
nuclear@27
|
305 return tm;
|
nuclear@27
|
306 }
|
nuclear@27
|
307 return (tm - start) % interv + start;*/
|
nuclear@27
|
308 }
|
nuclear@27
|
309
|
nuclear@27
|
310 static anm_time_t remap_pingpong(anm_time_t tm, anm_time_t start, anm_time_t end)
|
nuclear@27
|
311 {
|
nuclear@27
|
312 anm_time_t interv = end - start;
|
nuclear@27
|
313 anm_time_t x = remap_repeat(tm, start, end + interv);
|
nuclear@27
|
314
|
nuclear@27
|
315 return x > end ? end + interv - x : x;
|
nuclear@27
|
316 }
|