libanim

view src/track.c @ 33:f12663c5c907

changed macosx dylib name to libanim.dylib
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 17 May 2014 22:51:25 +0300
parents 821411647b40
children
line source
1 /*
2 libanim - hierarchical keyframe animation library
3 Copyright (C) 2012-2014 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/>.
17 */
19 #include <stdlib.h>
20 #include <string.h>
21 #include <assert.h>
22 #include "track.h"
23 #include "dynarr.h"
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) = {
39 interp_step,
40 interp_linear,
41 interp_cubic,
42 0
43 };
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) = {
47 remap_extend,
48 remap_clamp,
49 remap_repeat,
50 remap_pingpong,
51 0
52 };
54 int anm_init_track(struct anm_track *track)
55 {
56 memset(track, 0, sizeof *track);
58 if(!(track->keys = dynarr_alloc(0, sizeof *track->keys))) {
59 return -1;
60 }
61 track->interp = ANM_INTERP_LINEAR;
62 track->extrap = ANM_EXTRAP_CLAMP;
63 return 0;
64 }
66 void anm_destroy_track(struct anm_track *track)
67 {
68 dynarr_free(track->keys);
69 }
71 struct anm_track *anm_create_track(void)
72 {
73 struct anm_track *track;
75 if((track = malloc(sizeof *track))) {
76 if(anm_init_track(track) == -1) {
77 free(track);
78 return 0;
79 }
80 }
81 return track;
82 }
84 void anm_free_track(struct anm_track *track)
85 {
86 anm_destroy_track(track);
87 free(track);
88 }
90 void anm_copy_track(struct anm_track *dest, const struct anm_track *src)
91 {
92 free(dest->name);
93 if(dest->keys) {
94 dynarr_free(dest->keys);
95 }
97 if(src->name) {
98 dest->name = malloc(strlen(src->name) + 1);
99 strcpy(dest->name, src->name);
100 }
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;
109 }
111 int anm_set_track_name(struct anm_track *track, const char *name)
112 {
113 char *tmp;
115 if(!(tmp = malloc(strlen(name) + 1))) {
116 return -1;
117 }
118 free(track->name);
119 track->name = tmp;
120 return 0;
121 }
123 const char *anm_get_track_name(struct anm_track *track)
124 {
125 return track->name;
126 }
128 void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in)
129 {
130 track->interp = in;
131 }
133 void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex)
134 {
135 track->extrap = ex;
136 }
138 anm_time_t anm_remap_time(struct anm_track *track, anm_time_t tm, anm_time_t start, anm_time_t end)
139 {
140 return remap_time[track->extrap](tm, start, end);
141 }
143 void anm_set_track_default(struct anm_track *track, float def)
144 {
145 track->def_val = def;
146 }
148 int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key)
149 {
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;
156 } else {
157 /* ... it's a new key, add it and re-sort them */
158 void *tmp;
159 if(!(tmp = dynarr_push(track->keys, key))) {
160 return -1;
161 }
162 track->keys = tmp;
163 /* TODO lazy qsort */
164 qsort(track->keys, ++track->count, sizeof *track->keys, keycmp);
165 }
166 return 0;
167 }
169 static int keycmp(const void *a, const void *b)
170 {
171 return ((struct anm_keyframe*)a)->time - ((struct anm_keyframe*)b)->time;
172 }
174 struct anm_keyframe *anm_get_keyframe(struct anm_track *track, int idx)
175 {
176 if(idx < 0 || idx >= track->count) {
177 return 0;
178 }
179 return track->keys + idx;
180 }
182 int anm_get_key_interval(struct anm_track *track, anm_time_t tm)
183 {
184 int last;
186 if(!track->count || tm < track->keys[0].time) {
187 return -1;
188 }
190 last = track->count - 1;
191 if(tm > track->keys[last].time) {
192 return last;
193 }
195 return find_prev_key(track->keys, 0, last, tm);
196 }
198 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm)
199 {
200 int mid;
202 if(end - start <= 1) {
203 return start;
204 }
206 mid = (start + end) / 2;
207 if(tm < arr[mid].time) {
208 return find_prev_key(arr, start, mid, tm);
209 }
210 if(tm > arr[mid].time) {
211 return find_prev_key(arr, mid, end, tm);
212 }
213 return mid;
214 }
216 int anm_set_value(struct anm_track *track, anm_time_t tm, float val)
217 {
218 struct anm_keyframe key;
219 key.time = tm;
220 key.val = val;
222 return anm_set_keyframe(track, &key);
223 }
225 float anm_get_value(struct anm_track *track, anm_time_t tm)
226 {
227 int idx0, idx1, last_idx;
228 anm_time_t tstart, tend;
229 float t, dt;
230 float v0, v1, v2, v3;
232 if(!track->count) {
233 return track->def_val;
234 }
236 last_idx = track->count - 1;
238 tstart = track->keys[0].time;
239 tend = track->keys[last_idx].time;
241 if(tstart == tend) {
242 return track->keys[0].val;
243 }
245 tm = remap_time[track->extrap](tm, tstart, tend);
247 idx0 = anm_get_key_interval(track, tm);
248 assert(idx0 >= 0 && idx0 < track->count);
249 idx1 = idx0 + 1;
251 if(idx0 == last_idx) {
252 return track->keys[idx0].val;
253 }
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);
266 }
269 static float interp_step(float v0, float v1, float v2, float v3, float t)
270 {
271 return v1;
272 }
274 static float interp_linear(float v0, float v1, float v2, float v3, float t)
275 {
276 return v1 + (v2 - v1) * t;
277 }
279 static float interp_cubic(float a, float b, float c, float d, float t)
280 {
281 float x, y, z, w;
282 float tsq = t * t;
284 x = -a + 3.0 * b - 3.0 * c + d;
285 y = 2.0 * a - 5.0 * b + 4.0 * c - d;
286 z = c - a;
287 w = 2.0 * b;
289 return 0.5 * (x * tsq * t + y * tsq + z * t + w);
290 }
292 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end)
293 {
294 return remap_repeat(tm, start, end);
295 }
297 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end)
298 {
299 if(start == end) {
300 return start;
301 }
302 return tm < start ? start : (tm >= end ? end - 1 : tm);
303 }
305 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end)
306 {
307 anm_time_t x, interv = end - start;
309 if(interv == 0) {
310 return start;
311 }
313 x = (tm - start) % interv;
314 if(x < 0) {
315 x += interv;
316 }
317 return x + start;
319 /*if(tm < start) {
320 while(tm < start) {
321 tm += interv;
322 }
323 return tm;
324 }
325 return (tm - start) % interv + start;*/
326 }
328 static anm_time_t remap_pingpong(anm_time_t tm, anm_time_t start, anm_time_t end)
329 {
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;
334 }