libanim

view src/track.c @ 57:2da758956e50

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