nuclear@0: #include nuclear@0: #include nuclear@0: #include nuclear@0: #include "track.h" nuclear@0: #include "dynarr.h" nuclear@0: nuclear@0: static int keycmp(const void *a, const void *b); nuclear@0: static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm); nuclear@0: nuclear@0: static float interp_step(float v0, float v1, float v2, float v3, float t); nuclear@0: static float interp_linear(float v0, float v1, float v2, float v3, float t); nuclear@0: static float interp_cubic(float v0, float v1, float v2, float v3, float t); nuclear@0: nuclear@0: static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end); nuclear@0: static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end); nuclear@0: static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end); nuclear@0: static anm_time_t remap_pingpong(anm_time_t tm, anm_time_t start, anm_time_t end); nuclear@0: nuclear@0: /* XXX keep this in sync with enum anm_interpolator at track.h */ nuclear@0: static float (*interp[])(float, float, float, float, float) = { nuclear@0: interp_step, nuclear@0: interp_linear, nuclear@0: interp_cubic, nuclear@0: 0 nuclear@0: }; nuclear@0: nuclear@0: /* XXX keep this in sync with enum anm_extrapolator at track.h */ nuclear@0: static anm_time_t (*remap_time[])(anm_time_t, anm_time_t, anm_time_t) = { nuclear@0: remap_extend, nuclear@0: remap_clamp, nuclear@0: remap_repeat, nuclear@0: remap_pingpong, nuclear@0: 0 nuclear@0: }; nuclear@0: nuclear@0: int anm_init_track(struct anm_track *track) nuclear@0: { nuclear@0: memset(track, 0, sizeof *track); nuclear@0: nuclear@0: if(!(track->keys = dynarr_alloc(0, sizeof *track->keys))) { nuclear@0: return -1; nuclear@0: } nuclear@0: track->interp = ANM_INTERP_LINEAR; nuclear@0: track->extrap = ANM_EXTRAP_CLAMP; nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: void anm_destroy_track(struct anm_track *track) nuclear@0: { nuclear@0: dynarr_free(track->keys); nuclear@0: } nuclear@0: nuclear@0: struct anm_track *anm_create_track(void) nuclear@0: { nuclear@0: struct anm_track *track; nuclear@0: nuclear@0: if((track = malloc(sizeof *track))) { nuclear@0: if(anm_init_track(track) == -1) { nuclear@0: free(track); nuclear@0: return 0; nuclear@0: } nuclear@0: } nuclear@0: return track; nuclear@0: } nuclear@0: nuclear@0: void anm_free_track(struct anm_track *track) nuclear@0: { nuclear@0: anm_destroy_track(track); nuclear@0: free(track); nuclear@0: } nuclear@0: nuclear@0: void anm_copy_track(struct anm_track *dest, struct anm_track *src) nuclear@0: { nuclear@0: free(dest->name); nuclear@0: if(dest->keys) { nuclear@0: dynarr_free(dest->keys); nuclear@0: } nuclear@0: nuclear@0: if(src->name) { nuclear@0: dest->name = malloc(strlen(src->name) + 1); nuclear@0: strcpy(dest->name, src->name); nuclear@0: } nuclear@0: nuclear@0: dest->count = src->count; nuclear@0: dest->keys = dynarr_alloc(src->count, sizeof *dest->keys); nuclear@0: memcpy(dest->keys, src->keys, src->count * sizeof *dest->keys); nuclear@0: nuclear@0: dest->def_val = src->def_val; nuclear@0: dest->interp = src->interp; nuclear@0: dest->extrap = src->extrap; nuclear@0: } nuclear@0: nuclear@0: int anm_set_track_name(struct anm_track *track, const char *name) nuclear@0: { nuclear@0: char *tmp; nuclear@0: nuclear@0: if(!(tmp = malloc(strlen(name) + 1))) { nuclear@0: return -1; nuclear@0: } nuclear@0: free(track->name); nuclear@0: track->name = tmp; nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: const char *anm_get_track_name(struct anm_track *track) nuclear@0: { nuclear@0: return track->name; nuclear@0: } nuclear@0: nuclear@0: void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in) nuclear@0: { nuclear@0: track->interp = in; nuclear@0: } nuclear@0: nuclear@0: void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex) nuclear@0: { nuclear@0: track->extrap = ex; nuclear@0: } nuclear@0: nuclear@0: anm_time_t anm_remap_time(struct anm_track *track, anm_time_t tm, anm_time_t start, anm_time_t end) nuclear@0: { nuclear@0: return remap_time[track->extrap](tm, start, end); nuclear@0: } nuclear@0: nuclear@0: void anm_set_track_default(struct anm_track *track, float def) nuclear@0: { nuclear@0: track->def_val = def; nuclear@0: } nuclear@0: nuclear@0: int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key) nuclear@0: { nuclear@0: int idx = anm_get_key_interval(track, key->time); nuclear@0: nuclear@0: /* if we got a valid keyframe index, compare them... */ nuclear@0: if(idx >= 0 && idx < track->count && keycmp(key, track->keys + idx) == 0) { nuclear@0: /* ... it's the same key, just update the value */ nuclear@0: track->keys[idx].val = key->val; nuclear@0: } else { nuclear@0: /* ... it's a new key, add it and re-sort them */ nuclear@0: void *tmp; nuclear@0: if(!(tmp = dynarr_push(track->keys, key))) { nuclear@0: return -1; nuclear@0: } nuclear@0: track->keys = tmp; nuclear@0: /* TODO lazy qsort */ nuclear@0: qsort(track->keys, ++track->count, sizeof *track->keys, keycmp); nuclear@0: } nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: static int keycmp(const void *a, const void *b) nuclear@0: { nuclear@0: return ((struct anm_keyframe*)a)->time - ((struct anm_keyframe*)b)->time; nuclear@0: } nuclear@0: nuclear@0: struct anm_keyframe *anm_get_keyframe(struct anm_track *track, int idx) nuclear@0: { nuclear@0: if(idx < 0 || idx >= track->count) { nuclear@0: return 0; nuclear@0: } nuclear@0: return track->keys + idx; nuclear@0: } nuclear@0: nuclear@0: int anm_get_key_interval(struct anm_track *track, anm_time_t tm) nuclear@0: { nuclear@0: int last; nuclear@0: nuclear@0: if(!track->count || tm < track->keys[0].time) { nuclear@0: return -1; nuclear@0: } nuclear@0: nuclear@0: last = track->count - 1; nuclear@0: if(tm > track->keys[last].time) { nuclear@0: return last; nuclear@0: } nuclear@0: nuclear@0: return find_prev_key(track->keys, 0, last, tm); nuclear@0: } nuclear@0: nuclear@0: static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm) nuclear@0: { nuclear@0: int mid; nuclear@0: nuclear@0: if(end - start <= 1) { nuclear@0: return start; nuclear@0: } nuclear@0: nuclear@0: mid = (start + end) / 2; nuclear@0: if(tm < arr[mid].time) { nuclear@0: return find_prev_key(arr, start, mid, tm); nuclear@0: } nuclear@0: if(tm > arr[mid].time) { nuclear@0: return find_prev_key(arr, mid, end, tm); nuclear@0: } nuclear@0: return mid; nuclear@0: } nuclear@0: nuclear@0: int anm_set_value(struct anm_track *track, anm_time_t tm, float val) nuclear@0: { nuclear@0: struct anm_keyframe key; nuclear@0: key.time = tm; nuclear@0: key.val = val; nuclear@0: nuclear@0: return anm_set_keyframe(track, &key); nuclear@0: } nuclear@0: nuclear@0: float anm_get_value(struct anm_track *track, anm_time_t tm) nuclear@0: { nuclear@0: int idx0, idx1, last_idx; nuclear@0: anm_time_t tstart, tend; nuclear@0: float t, dt; nuclear@0: float v0, v1, v2, v3; nuclear@0: nuclear@0: if(!track->count) { nuclear@0: return track->def_val; nuclear@0: } nuclear@0: nuclear@0: last_idx = track->count - 1; nuclear@0: nuclear@0: tstart = track->keys[0].time; nuclear@0: tend = track->keys[last_idx].time; nuclear@0: nuclear@0: if(tstart == tend) { nuclear@0: return track->keys[0].val; nuclear@0: } nuclear@0: nuclear@0: tm = remap_time[track->extrap](tm, tstart, tend); nuclear@0: nuclear@0: idx0 = anm_get_key_interval(track, tm); nuclear@0: assert(idx0 >= 0 && idx0 < track->count); nuclear@0: idx1 = idx0 + 1; nuclear@0: nuclear@0: if(idx0 == last_idx) { nuclear@0: return track->keys[idx0].val; nuclear@0: } nuclear@0: nuclear@0: dt = (float)(track->keys[idx1].time - track->keys[idx0].time); nuclear@0: t = (float)(tm - track->keys[idx0].time) / dt; nuclear@0: nuclear@0: v1 = track->keys[idx0].val; nuclear@0: v2 = track->keys[idx1].val; nuclear@0: nuclear@0: /* get the neigboring values to allow for cubic interpolation */ nuclear@0: v0 = idx0 > 0 ? track->keys[idx0 - 1].val : v1; nuclear@0: v3 = idx1 < last_idx ? track->keys[idx1 + 1].val : v2; nuclear@0: nuclear@0: return interp[track->interp](v0, v1, v2, v3, t); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: static float interp_step(float v0, float v1, float v2, float v3, float t) nuclear@0: { nuclear@0: return v1; nuclear@0: } nuclear@0: nuclear@0: static float interp_linear(float v0, float v1, float v2, float v3, float t) nuclear@0: { nuclear@0: return v1 + (v2 - v1) * t; nuclear@0: } nuclear@0: nuclear@0: static float interp_cubic(float a, float b, float c, float d, float t) nuclear@0: { nuclear@0: float x, y, z, w; nuclear@0: float tsq = t * t; nuclear@0: nuclear@0: x = -a + 3.0 * b - 3.0 * c + d; nuclear@0: y = 2.0 * a - 5.0 * b + 4.0 * c - d; nuclear@0: z = c - a; nuclear@0: w = 2.0 * b; nuclear@0: nuclear@0: return 0.5 * (x * tsq * t + y * tsq + z * t + w); nuclear@0: } nuclear@0: nuclear@0: static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end) nuclear@0: { nuclear@0: return remap_repeat(tm, start, end); nuclear@0: } nuclear@0: nuclear@0: static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end) nuclear@0: { nuclear@0: if(start == end) { nuclear@0: return start; nuclear@0: } nuclear@0: return tm < start ? start : (tm >= end ? end - 1 : tm); nuclear@0: } nuclear@0: nuclear@0: static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end) nuclear@0: { nuclear@0: anm_time_t x, interv = end - start; nuclear@0: nuclear@0: if(interv == 0) { nuclear@0: return start; nuclear@0: } nuclear@0: nuclear@0: x = (tm - start) % interv; nuclear@0: if(x < 0) { nuclear@0: x += interv; nuclear@0: } nuclear@0: return x + start; nuclear@0: nuclear@0: /*if(tm < start) { nuclear@0: while(tm < start) { nuclear@0: tm += interv; nuclear@0: } nuclear@0: return tm; nuclear@0: } nuclear@0: return (tm - start) % interv + start;*/ nuclear@0: } nuclear@0: nuclear@0: static anm_time_t remap_pingpong(anm_time_t tm, anm_time_t start, anm_time_t end) nuclear@0: { nuclear@0: anm_time_t interv = end - start; nuclear@0: anm_time_t x = remap_repeat(tm, start, end + interv); nuclear@0: nuclear@0: return x > end ? end + interv - x : x; nuclear@0: }