libanim

annotate src/track.c @ 43:a720e8d7023d

fixed a bug in the manual slerping in anm_get_node_rotation
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 01 Mar 2013 08:21:19 +0200
parents 9cd8afd6fa6d
children 9691b3521fa3
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@2 70 void anm_copy_track(struct anm_track *dest, struct anm_track *src)
nuclear@2 71 {
nuclear@2 72 free(dest->name);
nuclear@2 73 if(dest->keys) {
nuclear@2 74 dynarr_free(dest->keys);
nuclear@2 75 }
nuclear@2 76
nuclear@2 77 if(src->name) {
nuclear@2 78 dest->name = malloc(strlen(src->name) + 1);
nuclear@2 79 strcpy(dest->name, src->name);
nuclear@2 80 }
nuclear@2 81
nuclear@2 82 dest->count = src->count;
nuclear@2 83 dest->keys = dynarr_alloc(src->count, sizeof *dest->keys);
nuclear@2 84 memcpy(dest->keys, src->keys, src->count * sizeof *dest->keys);
nuclear@2 85
nuclear@2 86 dest->def_val = src->def_val;
nuclear@2 87 dest->interp = src->interp;
nuclear@2 88 dest->extrap = src->extrap;
nuclear@2 89 }
nuclear@2 90
nuclear@0 91 int anm_set_track_name(struct anm_track *track, const char *name)
nuclear@0 92 {
nuclear@0 93 char *tmp;
nuclear@0 94
nuclear@0 95 if(!(tmp = malloc(strlen(name) + 1))) {
nuclear@0 96 return -1;
nuclear@0 97 }
nuclear@0 98 free(track->name);
nuclear@0 99 track->name = tmp;
nuclear@0 100 return 0;
nuclear@0 101 }
nuclear@0 102
nuclear@0 103 const char *anm_get_track_name(struct anm_track *track)
nuclear@0 104 {
nuclear@0 105 return track->name;
nuclear@0 106 }
nuclear@0 107
nuclear@0 108 void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in)
nuclear@0 109 {
nuclear@0 110 track->interp = in;
nuclear@0 111 }
nuclear@0 112
nuclear@0 113 void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex)
nuclear@0 114 {
nuclear@0 115 track->extrap = ex;
nuclear@0 116 }
nuclear@0 117
nuclear@0 118 anm_time_t anm_remap_time(struct anm_track *track, anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@0 119 {
nuclear@0 120 return remap_time[track->extrap](tm, start, end);
nuclear@0 121 }
nuclear@0 122
nuclear@0 123 void anm_set_track_default(struct anm_track *track, float def)
nuclear@0 124 {
nuclear@0 125 track->def_val = def;
nuclear@0 126 }
nuclear@0 127
nuclear@0 128 int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key)
nuclear@0 129 {
nuclear@0 130 int idx = anm_get_key_interval(track, key->time);
nuclear@0 131
nuclear@0 132 /* if we got a valid keyframe index, compare them... */
nuclear@0 133 if(idx >= 0 && idx < track->count && keycmp(key, track->keys + idx) == 0) {
nuclear@0 134 /* ... it's the same key, just update the value */
nuclear@0 135 track->keys[idx].val = key->val;
nuclear@0 136 } else {
nuclear@0 137 /* ... it's a new key, add it and re-sort them */
nuclear@0 138 void *tmp;
nuclear@0 139 if(!(tmp = dynarr_push(track->keys, key))) {
nuclear@0 140 return -1;
nuclear@0 141 }
nuclear@0 142 track->keys = tmp;
nuclear@0 143 /* TODO lazy qsort */
nuclear@0 144 qsort(track->keys, ++track->count, sizeof *track->keys, keycmp);
nuclear@0 145 }
nuclear@0 146 return 0;
nuclear@0 147 }
nuclear@0 148
nuclear@0 149 static int keycmp(const void *a, const void *b)
nuclear@0 150 {
nuclear@0 151 return ((struct anm_keyframe*)a)->time - ((struct anm_keyframe*)b)->time;
nuclear@0 152 }
nuclear@0 153
nuclear@0 154 struct anm_keyframe *anm_get_keyframe(struct anm_track *track, int idx)
nuclear@0 155 {
nuclear@0 156 if(idx < 0 || idx >= track->count) {
nuclear@0 157 return 0;
nuclear@0 158 }
nuclear@0 159 return track->keys + idx;
nuclear@0 160 }
nuclear@0 161
nuclear@0 162 int anm_get_key_interval(struct anm_track *track, anm_time_t tm)
nuclear@0 163 {
nuclear@0 164 int last;
nuclear@0 165
nuclear@0 166 if(!track->count || tm < track->keys[0].time) {
nuclear@0 167 return -1;
nuclear@0 168 }
nuclear@0 169
nuclear@0 170 last = track->count - 1;
nuclear@0 171 if(tm > track->keys[last].time) {
nuclear@0 172 return last;
nuclear@0 173 }
nuclear@0 174
nuclear@0 175 return find_prev_key(track->keys, 0, last, tm);
nuclear@0 176 }
nuclear@0 177
nuclear@0 178 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm)
nuclear@0 179 {
nuclear@0 180 int mid;
nuclear@0 181
nuclear@0 182 if(end - start <= 1) {
nuclear@0 183 return start;
nuclear@0 184 }
nuclear@0 185
nuclear@0 186 mid = (start + end) / 2;
nuclear@0 187 if(tm < arr[mid].time) {
nuclear@0 188 return find_prev_key(arr, start, mid, tm);
nuclear@0 189 }
nuclear@0 190 if(tm > arr[mid].time) {
nuclear@0 191 return find_prev_key(arr, mid, end, tm);
nuclear@0 192 }
nuclear@0 193 return mid;
nuclear@0 194 }
nuclear@0 195
nuclear@0 196 int anm_set_value(struct anm_track *track, anm_time_t tm, float val)
nuclear@0 197 {
nuclear@0 198 struct anm_keyframe key;
nuclear@0 199 key.time = tm;
nuclear@0 200 key.val = val;
nuclear@0 201
nuclear@0 202 return anm_set_keyframe(track, &key);
nuclear@0 203 }
nuclear@0 204
nuclear@0 205 float anm_get_value(struct anm_track *track, anm_time_t tm)
nuclear@0 206 {
nuclear@0 207 int idx0, idx1, last_idx;
nuclear@0 208 anm_time_t tstart, tend;
nuclear@0 209 float t, dt;
nuclear@0 210 float v0, v1, v2, v3;
nuclear@0 211
nuclear@0 212 if(!track->count) {
nuclear@0 213 return track->def_val;
nuclear@0 214 }
nuclear@0 215
nuclear@0 216 last_idx = track->count - 1;
nuclear@0 217
nuclear@0 218 tstart = track->keys[0].time;
nuclear@0 219 tend = track->keys[last_idx].time;
nuclear@0 220
nuclear@0 221 if(tstart == tend) {
nuclear@0 222 return track->keys[0].val;
nuclear@0 223 }
nuclear@0 224
nuclear@0 225 tm = remap_time[track->extrap](tm, tstart, tend);
nuclear@0 226
nuclear@0 227 idx0 = anm_get_key_interval(track, tm);
nuclear@0 228 assert(idx0 >= 0 && idx0 < track->count);
nuclear@0 229 idx1 = idx0 + 1;
nuclear@0 230
nuclear@0 231 if(idx0 == last_idx) {
nuclear@0 232 return track->keys[idx0].val;
nuclear@0 233 }
nuclear@0 234
nuclear@0 235 dt = (float)(track->keys[idx1].time - track->keys[idx0].time);
nuclear@0 236 t = (float)(tm - track->keys[idx0].time) / dt;
nuclear@0 237
nuclear@0 238 v1 = track->keys[idx0].val;
nuclear@0 239 v2 = track->keys[idx1].val;
nuclear@0 240
nuclear@0 241 /* get the neigboring values to allow for cubic interpolation */
nuclear@0 242 v0 = idx0 > 0 ? track->keys[idx0 - 1].val : v1;
nuclear@0 243 v3 = idx1 < last_idx ? track->keys[idx1 + 1].val : v2;
nuclear@0 244
nuclear@0 245 return interp[track->interp](v0, v1, v2, v3, t);
nuclear@0 246 }
nuclear@0 247
nuclear@0 248
nuclear@0 249 static float interp_step(float v0, float v1, float v2, float v3, float t)
nuclear@0 250 {
nuclear@0 251 return v1;
nuclear@0 252 }
nuclear@0 253
nuclear@0 254 static float interp_linear(float v0, float v1, float v2, float v3, float t)
nuclear@0 255 {
nuclear@0 256 return v1 + (v2 - v1) * t;
nuclear@0 257 }
nuclear@0 258
nuclear@0 259 static float interp_cubic(float a, float b, float c, float d, float t)
nuclear@0 260 {
nuclear@0 261 float x, y, z, w;
nuclear@0 262 float tsq = t * t;
nuclear@0 263
nuclear@0 264 x = -a + 3.0 * b - 3.0 * c + d;
nuclear@0 265 y = 2.0 * a - 5.0 * b + 4.0 * c - d;
nuclear@0 266 z = c - a;
nuclear@0 267 w = 2.0 * b;
nuclear@0 268
nuclear@0 269 return 0.5 * (x * tsq * t + y * tsq + z * t + w);
nuclear@0 270 }
nuclear@0 271
nuclear@0 272 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@0 273 {
nuclear@0 274 return remap_repeat(tm, start, end);
nuclear@0 275 }
nuclear@0 276
nuclear@0 277 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@0 278 {
nuclear@3 279 if(start == end) {
nuclear@3 280 return start;
nuclear@3 281 }
nuclear@0 282 return tm < start ? start : (tm >= end ? end - 1 : tm);
nuclear@0 283 }
nuclear@0 284
nuclear@0 285 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@0 286 {
nuclear@6 287 anm_time_t x, interv = end - start;
nuclear@0 288
nuclear@6 289 if(interv == 0) {
nuclear@6 290 return start;
nuclear@6 291 }
nuclear@6 292
nuclear@6 293 x = (tm - start) % interv;
nuclear@3 294 if(x < 0) {
nuclear@3 295 x += interv;
nuclear@3 296 }
nuclear@3 297 return x + start;
nuclear@3 298
nuclear@3 299 /*if(tm < start) {
nuclear@0 300 while(tm < start) {
nuclear@0 301 tm += interv;
nuclear@0 302 }
nuclear@0 303 return tm;
nuclear@0 304 }
nuclear@3 305 return (tm - start) % interv + start;*/
nuclear@0 306 }