vrshoot

view libs/anim/track.c @ 3:c179c72369be

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