libanim

annotate src/track.c @ 78:769ae86eee31

makefile modified to build on mingw
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 16 Sep 2015 04:24:12 +0300
parents 821411647b40
children
rev   line source
nuclear@28 1 /*
nuclear@28 2 libanim - hierarchical keyframe animation library
nuclear@28 3 Copyright (C) 2012-2014 John Tsiombikas <nuclear@member.fsf.org>
nuclear@28 4
nuclear@28 5 This program is free software: you can redistribute it and/or modify
nuclear@28 6 it under the terms of the GNU Lesser General Public License as published
nuclear@28 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@28 8 (at your option) any later version.
nuclear@28 9
nuclear@28 10 This program is distributed in the hope that it will be useful,
nuclear@28 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@28 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@28 13 GNU Lesser General Public License for more details.
nuclear@28 14
nuclear@28 15 You should have received a copy of the GNU Lesser General Public License
nuclear@28 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@28 17 */
nuclear@28 18
nuclear@0 19 #include <stdlib.h>
nuclear@0 20 #include <string.h>
nuclear@0 21 #include <assert.h>
nuclear@0 22 #include "track.h"
nuclear@0 23 #include "dynarr.h"
nuclear@0 24
nuclear@0 25 static int keycmp(const void *a, const void *b);
nuclear@0 26 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm);
nuclear@0 27
nuclear@0 28 static float interp_step(float v0, float v1, float v2, float v3, float t);
nuclear@0 29 static float interp_linear(float v0, float v1, float v2, float v3, float t);
nuclear@0 30 static float interp_cubic(float v0, float v1, float v2, float v3, float t);
nuclear@0 31
nuclear@0 32 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end);
nuclear@0 33 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end);
nuclear@0 34 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end);
nuclear@12 35 static anm_time_t remap_pingpong(anm_time_t tm, anm_time_t start, anm_time_t end);
nuclear@0 36
nuclear@0 37 /* XXX keep this in sync with enum anm_interpolator at track.h */
nuclear@0 38 static float (*interp[])(float, float, float, float, float) = {
nuclear@0 39 interp_step,
nuclear@0 40 interp_linear,
nuclear@0 41 interp_cubic,
nuclear@0 42 0
nuclear@0 43 };
nuclear@0 44
nuclear@0 45 /* XXX keep this in sync with enum anm_extrapolator at track.h */
nuclear@0 46 static anm_time_t (*remap_time[])(anm_time_t, anm_time_t, anm_time_t) = {
nuclear@0 47 remap_extend,
nuclear@0 48 remap_clamp,
nuclear@0 49 remap_repeat,
nuclear@12 50 remap_pingpong,
nuclear@0 51 0
nuclear@0 52 };
nuclear@0 53
nuclear@0 54 int anm_init_track(struct anm_track *track)
nuclear@0 55 {
nuclear@0 56 memset(track, 0, sizeof *track);
nuclear@0 57
nuclear@0 58 if(!(track->keys = dynarr_alloc(0, sizeof *track->keys))) {
nuclear@0 59 return -1;
nuclear@0 60 }
nuclear@0 61 track->interp = ANM_INTERP_LINEAR;
nuclear@0 62 track->extrap = ANM_EXTRAP_CLAMP;
nuclear@0 63 return 0;
nuclear@0 64 }
nuclear@0 65
nuclear@0 66 void anm_destroy_track(struct anm_track *track)
nuclear@0 67 {
nuclear@0 68 dynarr_free(track->keys);
nuclear@0 69 }
nuclear@0 70
nuclear@0 71 struct anm_track *anm_create_track(void)
nuclear@0 72 {
nuclear@0 73 struct anm_track *track;
nuclear@0 74
nuclear@0 75 if((track = malloc(sizeof *track))) {
nuclear@0 76 if(anm_init_track(track) == -1) {
nuclear@0 77 free(track);
nuclear@0 78 return 0;
nuclear@0 79 }
nuclear@0 80 }
nuclear@0 81 return track;
nuclear@0 82 }
nuclear@0 83
nuclear@0 84 void anm_free_track(struct anm_track *track)
nuclear@0 85 {
nuclear@0 86 anm_destroy_track(track);
nuclear@0 87 free(track);
nuclear@0 88 }
nuclear@0 89
nuclear@17 90 void anm_copy_track(struct anm_track *dest, const struct anm_track *src)
nuclear@2 91 {
nuclear@2 92 free(dest->name);
nuclear@2 93 if(dest->keys) {
nuclear@2 94 dynarr_free(dest->keys);
nuclear@2 95 }
nuclear@2 96
nuclear@2 97 if(src->name) {
nuclear@2 98 dest->name = malloc(strlen(src->name) + 1);
nuclear@2 99 strcpy(dest->name, src->name);
nuclear@2 100 }
nuclear@2 101
nuclear@2 102 dest->count = src->count;
nuclear@2 103 dest->keys = dynarr_alloc(src->count, sizeof *dest->keys);
nuclear@2 104 memcpy(dest->keys, src->keys, src->count * sizeof *dest->keys);
nuclear@2 105
nuclear@2 106 dest->def_val = src->def_val;
nuclear@2 107 dest->interp = src->interp;
nuclear@2 108 dest->extrap = src->extrap;
nuclear@2 109 }
nuclear@2 110
nuclear@0 111 int anm_set_track_name(struct anm_track *track, const char *name)
nuclear@0 112 {
nuclear@0 113 char *tmp;
nuclear@0 114
nuclear@0 115 if(!(tmp = malloc(strlen(name) + 1))) {
nuclear@0 116 return -1;
nuclear@0 117 }
nuclear@0 118 free(track->name);
nuclear@0 119 track->name = tmp;
nuclear@0 120 return 0;
nuclear@0 121 }
nuclear@0 122
nuclear@0 123 const char *anm_get_track_name(struct anm_track *track)
nuclear@0 124 {
nuclear@0 125 return track->name;
nuclear@0 126 }
nuclear@0 127
nuclear@0 128 void anm_set_track_interpolator(struct anm_track *track, enum anm_interpolator in)
nuclear@0 129 {
nuclear@0 130 track->interp = in;
nuclear@0 131 }
nuclear@0 132
nuclear@0 133 void anm_set_track_extrapolator(struct anm_track *track, enum anm_extrapolator ex)
nuclear@0 134 {
nuclear@0 135 track->extrap = ex;
nuclear@0 136 }
nuclear@0 137
nuclear@0 138 anm_time_t anm_remap_time(struct anm_track *track, anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@0 139 {
nuclear@0 140 return remap_time[track->extrap](tm, start, end);
nuclear@0 141 }
nuclear@0 142
nuclear@0 143 void anm_set_track_default(struct anm_track *track, float def)
nuclear@0 144 {
nuclear@0 145 track->def_val = def;
nuclear@0 146 }
nuclear@0 147
nuclear@0 148 int anm_set_keyframe(struct anm_track *track, struct anm_keyframe *key)
nuclear@0 149 {
nuclear@0 150 int idx = anm_get_key_interval(track, key->time);
nuclear@0 151
nuclear@0 152 /* if we got a valid keyframe index, compare them... */
nuclear@0 153 if(idx >= 0 && idx < track->count && keycmp(key, track->keys + idx) == 0) {
nuclear@0 154 /* ... it's the same key, just update the value */
nuclear@0 155 track->keys[idx].val = key->val;
nuclear@0 156 } else {
nuclear@0 157 /* ... it's a new key, add it and re-sort them */
nuclear@0 158 void *tmp;
nuclear@0 159 if(!(tmp = dynarr_push(track->keys, key))) {
nuclear@0 160 return -1;
nuclear@0 161 }
nuclear@0 162 track->keys = tmp;
nuclear@0 163 /* TODO lazy qsort */
nuclear@0 164 qsort(track->keys, ++track->count, sizeof *track->keys, keycmp);
nuclear@0 165 }
nuclear@0 166 return 0;
nuclear@0 167 }
nuclear@0 168
nuclear@0 169 static int keycmp(const void *a, const void *b)
nuclear@0 170 {
nuclear@0 171 return ((struct anm_keyframe*)a)->time - ((struct anm_keyframe*)b)->time;
nuclear@0 172 }
nuclear@0 173
nuclear@0 174 struct anm_keyframe *anm_get_keyframe(struct anm_track *track, int idx)
nuclear@0 175 {
nuclear@0 176 if(idx < 0 || idx >= track->count) {
nuclear@0 177 return 0;
nuclear@0 178 }
nuclear@0 179 return track->keys + idx;
nuclear@0 180 }
nuclear@0 181
nuclear@0 182 int anm_get_key_interval(struct anm_track *track, anm_time_t tm)
nuclear@0 183 {
nuclear@0 184 int last;
nuclear@0 185
nuclear@0 186 if(!track->count || tm < track->keys[0].time) {
nuclear@0 187 return -1;
nuclear@0 188 }
nuclear@0 189
nuclear@0 190 last = track->count - 1;
nuclear@0 191 if(tm > track->keys[last].time) {
nuclear@0 192 return last;
nuclear@0 193 }
nuclear@0 194
nuclear@0 195 return find_prev_key(track->keys, 0, last, tm);
nuclear@0 196 }
nuclear@0 197
nuclear@0 198 static int find_prev_key(struct anm_keyframe *arr, int start, int end, anm_time_t tm)
nuclear@0 199 {
nuclear@0 200 int mid;
nuclear@0 201
nuclear@0 202 if(end - start <= 1) {
nuclear@0 203 return start;
nuclear@0 204 }
nuclear@0 205
nuclear@0 206 mid = (start + end) / 2;
nuclear@0 207 if(tm < arr[mid].time) {
nuclear@0 208 return find_prev_key(arr, start, mid, tm);
nuclear@0 209 }
nuclear@0 210 if(tm > arr[mid].time) {
nuclear@0 211 return find_prev_key(arr, mid, end, tm);
nuclear@0 212 }
nuclear@0 213 return mid;
nuclear@0 214 }
nuclear@0 215
nuclear@0 216 int anm_set_value(struct anm_track *track, anm_time_t tm, float val)
nuclear@0 217 {
nuclear@0 218 struct anm_keyframe key;
nuclear@0 219 key.time = tm;
nuclear@0 220 key.val = val;
nuclear@0 221
nuclear@0 222 return anm_set_keyframe(track, &key);
nuclear@0 223 }
nuclear@0 224
nuclear@0 225 float anm_get_value(struct anm_track *track, anm_time_t tm)
nuclear@0 226 {
nuclear@0 227 int idx0, idx1, last_idx;
nuclear@0 228 anm_time_t tstart, tend;
nuclear@0 229 float t, dt;
nuclear@0 230 float v0, v1, v2, v3;
nuclear@0 231
nuclear@0 232 if(!track->count) {
nuclear@0 233 return track->def_val;
nuclear@0 234 }
nuclear@0 235
nuclear@0 236 last_idx = track->count - 1;
nuclear@0 237
nuclear@0 238 tstart = track->keys[0].time;
nuclear@0 239 tend = track->keys[last_idx].time;
nuclear@0 240
nuclear@0 241 if(tstart == tend) {
nuclear@0 242 return track->keys[0].val;
nuclear@0 243 }
nuclear@0 244
nuclear@0 245 tm = remap_time[track->extrap](tm, tstart, tend);
nuclear@0 246
nuclear@0 247 idx0 = anm_get_key_interval(track, tm);
nuclear@0 248 assert(idx0 >= 0 && idx0 < track->count);
nuclear@0 249 idx1 = idx0 + 1;
nuclear@0 250
nuclear@0 251 if(idx0 == last_idx) {
nuclear@0 252 return track->keys[idx0].val;
nuclear@0 253 }
nuclear@0 254
nuclear@0 255 dt = (float)(track->keys[idx1].time - track->keys[idx0].time);
nuclear@0 256 t = (float)(tm - track->keys[idx0].time) / dt;
nuclear@0 257
nuclear@0 258 v1 = track->keys[idx0].val;
nuclear@0 259 v2 = track->keys[idx1].val;
nuclear@0 260
nuclear@0 261 /* get the neigboring values to allow for cubic interpolation */
nuclear@0 262 v0 = idx0 > 0 ? track->keys[idx0 - 1].val : v1;
nuclear@0 263 v3 = idx1 < last_idx ? track->keys[idx1 + 1].val : v2;
nuclear@0 264
nuclear@0 265 return interp[track->interp](v0, v1, v2, v3, t);
nuclear@0 266 }
nuclear@0 267
nuclear@0 268
nuclear@0 269 static float interp_step(float v0, float v1, float v2, float v3, float t)
nuclear@0 270 {
nuclear@0 271 return v1;
nuclear@0 272 }
nuclear@0 273
nuclear@0 274 static float interp_linear(float v0, float v1, float v2, float v3, float t)
nuclear@0 275 {
nuclear@0 276 return v1 + (v2 - v1) * t;
nuclear@0 277 }
nuclear@0 278
nuclear@0 279 static float interp_cubic(float a, float b, float c, float d, float t)
nuclear@0 280 {
nuclear@0 281 float x, y, z, w;
nuclear@0 282 float tsq = t * t;
nuclear@0 283
nuclear@0 284 x = -a + 3.0 * b - 3.0 * c + d;
nuclear@0 285 y = 2.0 * a - 5.0 * b + 4.0 * c - d;
nuclear@0 286 z = c - a;
nuclear@0 287 w = 2.0 * b;
nuclear@0 288
nuclear@0 289 return 0.5 * (x * tsq * t + y * tsq + z * t + w);
nuclear@0 290 }
nuclear@0 291
nuclear@0 292 static anm_time_t remap_extend(anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@0 293 {
nuclear@0 294 return remap_repeat(tm, start, end);
nuclear@0 295 }
nuclear@0 296
nuclear@0 297 static anm_time_t remap_clamp(anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@0 298 {
nuclear@3 299 if(start == end) {
nuclear@3 300 return start;
nuclear@3 301 }
nuclear@0 302 return tm < start ? start : (tm >= end ? end - 1 : tm);
nuclear@0 303 }
nuclear@0 304
nuclear@0 305 static anm_time_t remap_repeat(anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@0 306 {
nuclear@6 307 anm_time_t x, interv = end - start;
nuclear@0 308
nuclear@6 309 if(interv == 0) {
nuclear@6 310 return start;
nuclear@6 311 }
nuclear@6 312
nuclear@6 313 x = (tm - start) % interv;
nuclear@3 314 if(x < 0) {
nuclear@3 315 x += interv;
nuclear@3 316 }
nuclear@3 317 return x + start;
nuclear@3 318
nuclear@3 319 /*if(tm < start) {
nuclear@0 320 while(tm < start) {
nuclear@0 321 tm += interv;
nuclear@0 322 }
nuclear@0 323 return tm;
nuclear@0 324 }
nuclear@3 325 return (tm - start) % interv + start;*/
nuclear@0 326 }
nuclear@12 327
nuclear@12 328 static anm_time_t remap_pingpong(anm_time_t tm, anm_time_t start, anm_time_t end)
nuclear@12 329 {
nuclear@12 330 anm_time_t interv = end - start;
nuclear@12 331 anm_time_t x = remap_repeat(tm, start, end + interv);
nuclear@12 332
nuclear@12 333 return x > end ? end + interv - x : x;
nuclear@12 334 }