libanim

annotate src/track.c @ 0:fad4701f484e

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