libanim
view src/track.c @ 9:710658962108
simplified the matrix calculation and removed two quaternion normalizations that were probably not needed
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 08 Mar 2013 04:54:05 +0200 |
parents | 9cd8afd6fa6d |
children | 9691b3521fa3 |
line source
1 #include <stdlib.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "track.h"
5 #include "dynarr.h"
7 static int keycmp(const void *a, const void *b);
8 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm);
10 static float interp_step(float v0, float v1, float v2, float v3, float t);
11 static float interp_linear(float v0, float v1, float v2, float v3, float t);
12 static float interp_cubic(float v0, float v1, float v2, float v3, float t);
14 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end);
15 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end);
16 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end);
18 /* XXX keep this in sync with enum anm_interpolator at track.h */
19 static float (*interp[])(float, float, float, float, float) = {
20 interp_step,
21 interp_linear,
22 interp_cubic,
23 0
24 };
26 /* XXX keep this in sync with enum anm_extrapolator at track.h */
27 static anm_time_t (*remap_time[])(anm_time_t, anm_time_t, anm_time_t) = {
28 remap_extend,
29 remap_clamp,
30 remap_repeat,
31 0
32 };
34 int anm_init_track(struct anm_track *track)
35 {
36 memset(track, 0, sizeof *track);
38 if(!(track->keys = dynarr_alloc(0, sizeof *track->keys))) {
39 return -1;
40 }
41 track->interp = ANM_INTERP_LINEAR;
42 track->extrap = ANM_EXTRAP_CLAMP;
43 return 0;
44 }
46 void anm_destroy_track(struct anm_track *track)
47 {
48 dynarr_free(track->keys);
49 }
51 struct anm_track *anm_create_track(void)
52 {
53 struct anm_track *track;
55 if((track = malloc(sizeof *track))) {
56 if(anm_init_track(track) == -1) {
57 free(track);
58 return 0;
59 }
60 }
61 return track;
62 }
64 void anm_free_track(struct anm_track *track)
65 {
66 anm_destroy_track(track);
67 free(track);
68 }
70 void anm_copy_track(struct anm_track *dest, struct anm_track *src)
71 {
72 free(dest->name);
73 if(dest->keys) {
74 dynarr_free(dest->keys);
75 }
77 if(src->name) {
78 dest->name = malloc(strlen(src->name) + 1);
79 strcpy(dest->name, src->name);
80 }
82 dest->count = src->count;
83 dest->keys = dynarr_alloc(src->count, sizeof *dest->keys);
84 memcpy(dest->keys, src->keys, src->count * sizeof *dest->keys);
86 dest->def_val = src->def_val;
87 dest->interp = src->interp;
88 dest->extrap = src->extrap;
89 }
91 int anm_set_track_name(struct anm_track *track, const char *name)
92 {
93 char *tmp;
95 if(!(tmp = malloc(strlen(name) + 1))) {
96 return -1;
97 }
98 free(track->name);
99 track->name = tmp;
100 return 0;
101 }
103 const char *anm_get_track_name(struct anm_track *track)
104 {
105 return track->name;
106 }
108 void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in)
109 {
110 track->interp = in;
111 }
113 void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex)
114 {
115 track->extrap = ex;
116 }
118 anm_time_t anm_remap_time(struct anm_track *track, anm_time_t tm, anm_time_t start, anm_time_t end)
119 {
120 return remap_time[track->extrap](tm, start, end);
121 }
123 void anm_set_track_default(struct anm_track *track, float def)
124 {
125 track->def_val = def;
126 }
128 int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key)
129 {
130 int idx = anm_get_key_interval(track, key->time);
132 /* if we got a valid keyframe index, compare them... */
133 if(idx >= 0 && idx < track->count && keycmp(key, track->keys + idx) == 0) {
134 /* ... it's the same key, just update the value */
135 track->keys[idx].val = key->val;
136 } else {
137 /* ... it's a new key, add it and re-sort them */
138 void *tmp;
139 if(!(tmp = dynarr_push(track->keys, key))) {
140 return -1;
141 }
142 track->keys = tmp;
143 /* TODO lazy qsort */
144 qsort(track->keys, ++track->count, sizeof *track->keys, keycmp);
145 }
146 return 0;
147 }
149 static int keycmp(const void *a, const void *b)
150 {
151 return ((struct anm_keyframe*)a)->time - ((struct anm_keyframe*)b)->time;
152 }
154 struct anm_keyframe *anm_get_keyframe(struct anm_track *track, int idx)
155 {
156 if(idx < 0 || idx >= track->count) {
157 return 0;
158 }
159 return track->keys + idx;
160 }
162 int anm_get_key_interval(struct anm_track *track, anm_time_t tm)
163 {
164 int last;
166 if(!track->count || tm < track->keys[0].time) {
167 return -1;
168 }
170 last = track->count - 1;
171 if(tm > track->keys[last].time) {
172 return last;
173 }
175 return find_prev_key(track->keys, 0, last, tm);
176 }
178 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm)
179 {
180 int mid;
182 if(end - start <= 1) {
183 return start;
184 }
186 mid = (start + end) / 2;
187 if(tm < arr[mid].time) {
188 return find_prev_key(arr, start, mid, tm);
189 }
190 if(tm > arr[mid].time) {
191 return find_prev_key(arr, mid, end, tm);
192 }
193 return mid;
194 }
196 int anm_set_value(struct anm_track *track, anm_time_t tm, float val)
197 {
198 struct anm_keyframe key;
199 key.time = tm;
200 key.val = val;
202 return anm_set_keyframe(track, &key);
203 }
205 float anm_get_value(struct anm_track *track, anm_time_t tm)
206 {
207 int idx0, idx1, last_idx;
208 anm_time_t tstart, tend;
209 float t, dt;
210 float v0, v1, v2, v3;
212 if(!track->count) {
213 return track->def_val;
214 }
216 last_idx = track->count - 1;
218 tstart = track->keys[0].time;
219 tend = track->keys[last_idx].time;
221 if(tstart == tend) {
222 return track->keys[0].val;
223 }
225 tm = remap_time[track->extrap](tm, tstart, tend);
227 idx0 = anm_get_key_interval(track, tm);
228 assert(idx0 >= 0 && idx0 < track->count);
229 idx1 = idx0 + 1;
231 if(idx0 == last_idx) {
232 return track->keys[idx0].val;
233 }
235 dt = (float)(track->keys[idx1].time - track->keys[idx0].time);
236 t = (float)(tm - track->keys[idx0].time) / dt;
238 v1 = track->keys[idx0].val;
239 v2 = track->keys[idx1].val;
241 /* get the neigboring values to allow for cubic interpolation */
242 v0 = idx0 > 0 ? track->keys[idx0 - 1].val : v1;
243 v3 = idx1 < last_idx ? track->keys[idx1 + 1].val : v2;
245 return interp[track->interp](v0, v1, v2, v3, t);
246 }
249 static float interp_step(float v0, float v1, float v2, float v3, float t)
250 {
251 return v1;
252 }
254 static float interp_linear(float v0, float v1, float v2, float v3, float t)
255 {
256 return v1 + (v2 - v1) * t;
257 }
259 static float interp_cubic(float a, float b, float c, float d, float t)
260 {
261 float x, y, z, w;
262 float tsq = t * t;
264 x = -a + 3.0 * b - 3.0 * c + d;
265 y = 2.0 * a - 5.0 * b + 4.0 * c - d;
266 z = c - a;
267 w = 2.0 * b;
269 return 0.5 * (x * tsq * t + y * tsq + z * t + w);
270 }
272 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end)
273 {
274 return remap_repeat(tm, start, end);
275 }
277 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end)
278 {
279 if(start == end) {
280 return start;
281 }
282 return tm < start ? start : (tm >= end ? end - 1 : tm);
283 }
285 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end)
286 {
287 anm_time_t x, interv = end - start;
289 if(interv == 0) {
290 return start;
291 }
293 x = (tm - start) % interv;
294 if(x < 0) {
295 x += interv;
296 }
297 return x + start;
299 /*if(tm < start) {
300 while(tm < start) {
301 tm += interv;
302 }
303 return tm;
304 }
305 return (tm - start) % interv + start;*/
306 }