2 libanim - hierarchical keyframe animation library
3 Copyright (C) 2012-2023 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 #include "cgmath/cgmath.h"
27 static int find_prev_key(const struct anm_keyframe *arr, int start, int end, anm_time_t tm);
29 static float interp_step(float v0, float v1, float v2, float v3, float t);
30 static float interp_linear(float v0, float v1, float v2, float v3, float t);
31 static float interp_cubic(float v0, float v1, float v2, float v3, float t);
33 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end);
34 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end);
35 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end);
36 static anm_time_t remap_pingpong(anm_time_t tm, anm_time_t start, anm_time_t end);
38 /* XXX keep this in sync with enum anm_interpolator at track.h */
39 static float (*interp[])(float, float, float, float, float) = {
46 /* XXX keep this in sync with enum anm_extrapolator at track.h */
47 static anm_time_t (*remap_time[])(anm_time_t, anm_time_t, anm_time_t) = {
55 int anm_init_track(struct anm_track *track)
57 memset(track, 0, sizeof *track);
59 if(!(track->keys = dynarr_alloc(0, sizeof *track->keys))) {
62 track->keys_sorted = 1;
63 track->interp = ANM_INTERP_LINEAR;
64 track->extrap = ANM_EXTRAP_CLAMP;
68 void anm_destroy_track(struct anm_track *track)
70 dynarr_free(track->keys);
73 struct anm_track *anm_create_track(void)
75 struct anm_track *track;
77 if((track = malloc(sizeof *track))) {
78 if(anm_init_track(track) == -1) {
86 void anm_free_track(struct anm_track *track)
88 anm_destroy_track(track);
92 void anm_copy_track(struct anm_track *dest, const struct anm_track *src)
96 dynarr_free(dest->keys);
100 dest->name = malloc(strlen(src->name) + 1);
101 strcpy(dest->name, src->name);
104 dest->count = src->count;
105 dest->keys = dynarr_alloc(src->count, sizeof *dest->keys);
106 memcpy(dest->keys, src->keys, src->count * sizeof *dest->keys);
108 dest->def_val = src->def_val;
109 dest->interp = src->interp;
110 dest->extrap = src->extrap;
111 dest->keys_sorted = src->keys_sorted;
114 int anm_set_track_name(struct anm_track *track, const char *name)
118 if(!(tmp = malloc(strlen(name) + 1))) {
126 const char *anm_get_track_name(const struct anm_track *track)
131 void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in)
136 void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex)
141 anm_time_t anm_remap_time(const struct anm_track *track, anm_time_t tm, anm_time_t start, anm_time_t end)
143 return remap_time[track->extrap](tm, start, end);
146 void anm_set_track_default(struct anm_track *track, float def)
148 track->def_val = def;
151 static int keycmp(const void *a, const void *b)
153 return ((struct anm_keyframe*)a)->time - ((struct anm_keyframe*)b)->time;
156 int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key)
158 int idx = anm_get_key_interval(track, key->time);
160 /* if we got a valid keyframe index, compare them... */
161 if(idx >= 0 && idx < track->count && keycmp(key, track->keys + idx) == 0) {
162 /* ... it's the same key, just update the value */
163 track->keys[idx].val = key->val;
165 /* ... it's a new key, add it and re-sort them if necessary */
167 if(!(tmp = dynarr_push(track->keys, key))) {
171 idx = track->count++;
172 if(idx > 0 && track->keys[idx - 1].time > key->time) {
173 /* key shold not go to the end, mark for re-sorting */
174 track->keys_sorted = 0;
180 #define lazysort_keys(track) \
181 if(track->count > 1 && !track->keys_sorted) { \
182 qsort(track->keys, track->count, sizeof *track->keys, keycmp); \
183 ((struct anm_track*)track)->keys_sorted = 1; \
186 struct anm_keyframe *anm_get_keyframe(const struct anm_track *track, int idx)
188 if(idx < 0 || idx >= track->count) {
191 lazysort_keys(track);
192 return track->keys + idx;
195 int anm_get_key_interval(const struct anm_track *track, anm_time_t tm)
199 lazysort_keys(track);
201 if(!track->count || tm < track->keys[0].time) {
205 last = track->count - 1;
206 if(tm > track->keys[last].time) {
210 return find_prev_key(track->keys, 0, last, tm);
213 static int find_prev_key(const struct anm_keyframe *arr, int start, int end, anm_time_t tm)
217 if(end - start <= 1) {
221 mid = (start + end) / 2;
222 if(tm < arr[mid].time) {
223 return find_prev_key(arr, start, mid, tm);
225 if(tm > arr[mid].time) {
226 return find_prev_key(arr, mid, end, tm);
231 int anm_set_value(struct anm_track *track, anm_time_t tm, float val)
233 struct anm_keyframe key;
237 return anm_set_keyframe(track, &key);
240 float anm_get_value(const struct anm_track *track, anm_time_t tm)
242 int idx0, idx1, last_idx;
243 anm_time_t tstart, tend;
245 float v0, v1, v2, v3;
248 return track->def_val;
250 lazysort_keys(track);
252 last_idx = track->count - 1;
254 tstart = track->keys[0].time;
255 tend = track->keys[last_idx].time;
258 return track->keys[0].val;
261 tm = remap_time[track->extrap](tm, tstart, tend);
263 idx0 = anm_get_key_interval(track, tm);
264 assert(idx0 >= 0 && idx0 < track->count);
267 if(idx0 == last_idx) {
268 return track->keys[idx0].val;
271 dt = (float)(track->keys[idx1].time - track->keys[idx0].time);
272 t = (float)(tm - track->keys[idx0].time) / dt;
274 v1 = track->keys[idx0].val;
275 v2 = track->keys[idx1].val;
277 /* get the neigboring values to allow for cubic interpolation */
278 v0 = idx0 > 0 ? track->keys[idx0 - 1].val : v1;
279 v3 = idx1 < last_idx ? track->keys[idx1 + 1].val : v2;
281 return interp[track->interp](v0, v1, v2, v3, t);
285 void anm_get_quat(const struct anm_track *xtrk, const struct anm_track *ytrk,
286 const struct anm_track *ztrk, const struct anm_track *wtrk, anm_time_t tm, float *qres)
288 int idx0, idx1, last_idx;
289 anm_time_t tstart, tend;
294 qres[0] = xtrk->def_val;
295 qres[1] = ytrk->def_val;
296 qres[2] = ztrk->def_val;
297 qres[3] = wtrk->def_val;
306 last_idx = xtrk->count - 1;
308 tstart = xtrk->keys[0].time;
309 tend = xtrk->keys[last_idx].time;
312 qres[0] = xtrk->keys[0].val;
313 qres[1] = ytrk->keys[0].val;
314 qres[2] = ztrk->keys[0].val;
315 qres[3] = wtrk->keys[0].val;
319 tm = anm_remap_time(xtrk, tm, tstart, tend);
321 idx0 = anm_get_key_interval(xtrk, tm);
322 assert(idx0 >= 0 && idx0 < xtrk->count);
325 if(idx0 == last_idx) {
326 qres[0] = xtrk->keys[idx0].val;
327 qres[1] = ytrk->keys[idx0].val;
328 qres[2] = ztrk->keys[idx0].val;
329 qres[3] = wtrk->keys[idx0].val;
333 dt = (float)(xtrk->keys[idx1].time - xtrk->keys[idx0].time);
334 t = (float)(tm - xtrk->keys[idx0].time) / dt;
336 q1.x = xtrk->keys[idx0].val;
337 q1.y = ytrk->keys[idx0].val;
338 q1.z = ztrk->keys[idx0].val;
339 q1.w = wtrk->keys[idx0].val;
341 q2.x = xtrk->keys[idx1].val;
342 q2.y = ytrk->keys[idx1].val;
343 q2.z = ztrk->keys[idx1].val;
344 q2.w = wtrk->keys[idx1].val;
346 cgm_qslerp((cgm_quat*)qres, &q1, &q2, t);
350 static float interp_step(float v0, float v1, float v2, float v3, float t)
355 static float interp_linear(float v0, float v1, float v2, float v3, float t)
357 return v1 + (v2 - v1) * t;
360 static float interp_cubic(float a, float b, float c, float d, float t)
365 x = -a + 3.0 * b - 3.0 * c + d;
366 y = 2.0 * a - 5.0 * b + 4.0 * c - d;
370 return 0.5 * (x * tsq * t + y * tsq + z * t + w);
373 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end)
375 return remap_repeat(tm, start, end);
378 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end)
383 return tm < start ? start : (tm >= end ? end : tm);
386 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end)
388 anm_time_t x, interv = end - start;
394 x = (tm - start) % interv;
406 return (tm - start) % interv + start;*/
409 static anm_time_t remap_pingpong(anm_time_t tm, anm_time_t start, anm_time_t end)
411 anm_time_t interv = end - start;
412 anm_time_t x = remap_repeat(tm, start, end + interv);
414 return x > end ? end + interv - x : x;