vrshoot

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