libanim

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