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