2 libanim - hierarchical keyframe animation library
3 Copyright (C) 2012-2015 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 static int keycmp(const void *a, const void *b);
26 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm);
28 static float interp_step(float v0, float v1, float v2, float v3, float t);
29 static float interp_linear(float v0, float v1, float v2, float v3, float t);
30 static float interp_cubic(float v0, float v1, float v2, float v3, float t);
32 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end);
33 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end);
34 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end);
35 static anm_time_t remap_pingpong(anm_time_t tm, anm_time_t start, anm_time_t end);
37 /* XXX keep this in sync with enum anm_interpolator at track.h */
38 static float (*interp[])(float, float, float, float, float) = {
45 /* XXX keep this in sync with enum anm_extrapolator at track.h */
46 static anm_time_t (*remap_time[])(anm_time_t, anm_time_t, anm_time_t) = {
54 int anm_init_track(struct anm_track *track)
56 memset(track, 0, sizeof *track);
58 if(!(track->keys = dynarr_alloc(0, sizeof *track->keys))) {
61 track->interp = ANM_INTERP_LINEAR;
62 track->extrap = ANM_EXTRAP_CLAMP;
66 void anm_destroy_track(struct anm_track *track)
68 dynarr_free(track->keys);
71 struct anm_track *anm_create_track(void)
73 struct anm_track *track;
75 if((track = malloc(sizeof *track))) {
76 if(anm_init_track(track) == -1) {
84 void anm_free_track(struct anm_track *track)
86 anm_destroy_track(track);
90 void anm_copy_track(struct anm_track *dest, const struct anm_track *src)
94 dynarr_free(dest->keys);
98 dest->name = malloc(strlen(src->name) + 1);
99 strcpy(dest->name, src->name);
102 dest->count = src->count;
103 dest->keys = dynarr_alloc(src->count, sizeof *dest->keys);
104 memcpy(dest->keys, src->keys, src->count * sizeof *dest->keys);
106 dest->def_val = src->def_val;
107 dest->interp = src->interp;
108 dest->extrap = src->extrap;
111 int anm_set_track_name(struct anm_track *track, const char *name)
115 if(!(tmp = malloc(strlen(name) + 1))) {
123 const char *anm_get_track_name(struct anm_track *track)
128 void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in)
133 void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex)
138 anm_time_t anm_remap_time(struct anm_track *track, anm_time_t tm, anm_time_t start, anm_time_t end)
140 return remap_time[track->extrap](tm, start, end);
143 void anm_set_track_default(struct anm_track *track, float def)
145 track->def_val = def;
148 int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key)
150 int idx = anm_get_key_interval(track, key->time);
152 /* if we got a valid keyframe index, compare them... */
153 if(idx >= 0 && idx < track->count && keycmp(key, track->keys + idx) == 0) {
154 /* ... it's the same key, just update the value */
155 track->keys[idx].val = key->val;
157 /* ... it's a new key, add it and re-sort them */
159 if(!(tmp = dynarr_push(track->keys, key))) {
163 /* TODO lazy qsort */
164 qsort(track->keys, ++track->count, sizeof *track->keys, keycmp);
169 static int keycmp(const void *a, const void *b)
171 return ((struct anm_keyframe*)a)->time - ((struct anm_keyframe*)b)->time;
174 struct anm_keyframe *anm_get_keyframe(struct anm_track *track, int idx)
176 if(idx < 0 || idx >= track->count) {
179 return track->keys + idx;
182 int anm_get_key_interval(struct anm_track *track, anm_time_t tm)
186 if(!track->count || tm < track->keys[0].time) {
190 last = track->count - 1;
191 if(tm > track->keys[last].time) {
195 return find_prev_key(track->keys, 0, last, tm);
198 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm)
202 if(end - start <= 1) {
206 mid = (start + end) / 2;
207 if(tm < arr[mid].time) {
208 return find_prev_key(arr, start, mid, tm);
210 if(tm > arr[mid].time) {
211 return find_prev_key(arr, mid, end, tm);
216 int anm_set_value(struct anm_track *track, anm_time_t tm, float val)
218 struct anm_keyframe key;
222 return anm_set_keyframe(track, &key);
225 float anm_get_value(struct anm_track *track, anm_time_t tm)
227 int idx0, idx1, last_idx;
228 anm_time_t tstart, tend;
230 float v0, v1, v2, v3;
233 return track->def_val;
236 last_idx = track->count - 1;
238 tstart = track->keys[0].time;
239 tend = track->keys[last_idx].time;
242 return track->keys[0].val;
245 tm = remap_time[track->extrap](tm, tstart, tend);
247 idx0 = anm_get_key_interval(track, tm);
248 assert(idx0 >= 0 && idx0 < track->count);
251 if(idx0 == last_idx) {
252 return track->keys[idx0].val;
255 dt = (float)(track->keys[idx1].time - track->keys[idx0].time);
256 t = (float)(tm - track->keys[idx0].time) / dt;
258 v1 = track->keys[idx0].val;
259 v2 = track->keys[idx1].val;
261 /* get the neigboring values to allow for cubic interpolation */
262 v0 = idx0 > 0 ? track->keys[idx0 - 1].val : v1;
263 v3 = idx1 < last_idx ? track->keys[idx1 + 1].val : v2;
265 return interp[track->interp](v0, v1, v2, v3, t);
269 static float interp_step(float v0, float v1, float v2, float v3, float t)
274 static float interp_linear(float v0, float v1, float v2, float v3, float t)
276 return v1 + (v2 - v1) * t;
279 static float interp_cubic(float a, float b, float c, float d, float t)
284 x = -a + 3.0 * b - 3.0 * c + d;
285 y = 2.0 * a - 5.0 * b + 4.0 * c - d;
289 return 0.5 * (x * tsq * t + y * tsq + z * t + w);
292 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end)
294 return remap_repeat(tm, start, end);
297 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end)
302 return tm < start ? start : (tm >= end ? end : tm);
305 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end)
307 anm_time_t x, interv = end - start;
313 x = (tm - start) % interv;
325 return (tm - start) % interv + start;*/
328 static anm_time_t remap_pingpong(anm_time_t tm, anm_time_t start, anm_time_t end)
330 anm_time_t interv = end - start;
331 anm_time_t x = remap_repeat(tm, start, end + interv);
333 return x > end ? end + interv - x : x;