diff options
author | Joshua Leung <aligorith@gmail.com> | 2008-04-28 08:46:28 +0400 |
---|---|---|
committer | Joshua Leung <aligorith@gmail.com> | 2008-04-28 08:46:28 +0400 |
commit | 5b691071cf95041d869610081d32f9761bd02c94 (patch) | |
tree | 63e0436d741d7aa359645b09dc66fab8f03c28bb /source/blender/src/editipo.c | |
parent | 961d8c5cd41292b45501f583bf343308476f338c (diff) |
Assorted tidy-ups for keyframing (including auto-keying), while trying to track down a bug.
Diffstat (limited to 'source/blender/src/editipo.c')
-rw-r--r-- | source/blender/src/editipo.c | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/source/blender/src/editipo.c b/source/blender/src/editipo.c index cbde4892216..d72bdc6fc80 100644 --- a/source/blender/src/editipo.c +++ b/source/blender/src/editipo.c @@ -2032,15 +2032,17 @@ IpoCurve *verify_ipocurve(ID *from, short blocktype, char *actname, char *constn #define BEZT_INSERT_THRESH 0.00001 /* Binary search algorithm for finding where to insert BezTriple. (for use by insert_bezt_icu) - * Returns the index to insert before, OR the -(index + 1) to replace. - * Caller will need to decrement index if > 0 to add to right place (and avoid segfaults) + * Returns the index to insert at (data already at that index will be offset if replace is 0) */ -static int binarysearch_bezt_index (BezTriple array[], BezTriple *item, int arraylen) +static int binarysearch_bezt_index (BezTriple array[], BezTriple *item, int arraylen, short *replace) { int start=0, end=arraylen; int loopbreaker= 0, maxloop= arraylen * 2; const float frame= (item)? item->vec[1][0] : 0.0f; + /* initialise replace-flag first */ + *replace= 0; + /* sneaky optimisations (don't go through searching process if...): * - keyframe to be added is to be added out of current bounds * - keyframe to be added would replace one of the existing ones on bounds @@ -2055,15 +2057,19 @@ static int binarysearch_bezt_index (BezTriple array[], BezTriple *item, int arra /* 'First' Keyframe (when only one keyframe, this case is used) */ framenum= array[0].vec[1][0]; - if (IS_EQT(frame, framenum, BEZT_INSERT_THRESH)) - return -1; + if (IS_EQT(frame, framenum, BEZT_INSERT_THRESH)) { + *replace = 1; + return 0; + } else if (frame < framenum) return 0; /* 'Last' Keyframe */ framenum= array[(arraylen-1)].vec[1][0]; - if (IS_EQT(frame, framenum, BEZT_INSERT_THRESH)) - return -(arraylen); + if (IS_EQT(frame, framenum, BEZT_INSERT_THRESH)) { + *replace= 1; + return (arraylen - 1); + } else if (frame > framenum) return arraylen; } @@ -2078,8 +2084,10 @@ static int binarysearch_bezt_index (BezTriple array[], BezTriple *item, int arra float midfra= array[mid].vec[1][0]; /* check if exactly equal to midpoint */ - if (IS_EQT(frame, midfra, BEZT_INSERT_THRESH)) - return -(mid + 1); + if (IS_EQT(frame, midfra, BEZT_INSERT_THRESH)) { + *replace = 1; + return mid; + } /* repeat in upper/lower half */ if (frame > midfra) @@ -2118,19 +2126,17 @@ int insert_bezt_icu (IpoCurve *icu, BezTriple *bezt) icu->totvert= 1; } else { - i = binarysearch_bezt_index(icu->bezt, bezt, icu->totvert); + short replace = -1; + i = binarysearch_bezt_index(icu->bezt, bezt, icu->totvert, &replace); - if (i < 0) { - /* replace existing item (need to 'invert' i first and decremement by 1) */ - i = -i - 1; - + if (replace) { /* sanity check: 'i' may in rare cases exceed arraylen */ - if (i < icu->totvert) + if ((i >= 0) && (i < icu->totvert)) *(icu->bezt + i) = *bezt; } else { /* add new */ - newb= MEM_callocN( (icu->totvert+1)*sizeof(BezTriple), "beztriple"); + newb= MEM_callocN((icu->totvert+1)*sizeof(BezTriple), "beztriple"); /* add the beztriples that should occur before the beztriple to be pasted (originally in ei->icu) */ if (i > 0) |