diff options
author | Joshua Leung <aligorith@gmail.com> | 2008-12-29 04:19:25 +0300 |
---|---|---|
committer | Joshua Leung <aligorith@gmail.com> | 2008-12-29 04:19:25 +0300 |
commit | 97a82102d4d24a94d8360a6f5f054e44f6fb1993 (patch) | |
tree | f204580537752d9aa602aaaae66246affe042f66 /source/blender/editors/animation/keyframes_general.c | |
parent | 87cae4caed35c50b2ef70fb104d6f057670eb94e (diff) |
2.5 - Action Editor / Animation Stuff:
* Brought back clean (OKEY), sample (Shift-OKEY), and delete (XKEY/DELKEY) tools for the Action Editor.
Currently clean uses a predefined threshold (as a rna-prop, though it's still lacking the popup to set this when it is called)
* Added new file for 'destructive' keyframe operations/tools.
* Got keyframing.c compiling. Now, some of these tools need to be operatorised. Also, the API there might change when enough of the system is stable for RNA-IPO work to take place (so that it can be tested).
Diffstat (limited to 'source/blender/editors/animation/keyframes_general.c')
-rw-r--r-- | source/blender/editors/animation/keyframes_general.c | 364 |
1 files changed, 364 insertions, 0 deletions
diff --git a/source/blender/editors/animation/keyframes_general.c b/source/blender/editors/animation/keyframes_general.c new file mode 100644 index 00000000000..57a4d6952c7 --- /dev/null +++ b/source/blender/editors/animation/keyframes_general.c @@ -0,0 +1,364 @@ +/** + * $Id: + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2008 Blender Foundation + * All rights reserved. + * + * Contributor(s): Joshua Leung + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include <stdlib.h> +#include <string.h> +#include <math.h> +#include <float.h> + +#include "MEM_guardedalloc.h" + +#include "BLI_blenlib.h" +#include "BLI_arithb.h" + +#include "DNA_action_types.h" +#include "DNA_curve_types.h" +#include "DNA_ipo_types.h" +#include "DNA_key_types.h" +#include "DNA_object_types.h" +#include "DNA_space_types.h" +#include "DNA_scene_types.h" + +#include "BKE_action.h" +#include "BKE_ipo.h" +#include "BKE_key.h" +#include "BKE_utildefines.h" + +#include "ED_anim_api.h" +#include "ED_keyframing.h" +#include "ED_keyframes_edit.h" + +/* This file contains code for various keyframe-editing tools which are 'destructive' + * (i.e. they will modify the order of the keyframes, and change the size of the array). + * While some of these tools may eventually be moved out into blenkernel, for now, it is + * fine to have these calls here. + * + * There are also a few tools here which cannot be easily coded for in the other system (yet). + * These may also be moved around at some point, but for now, they + * + * - Joshua Leung, Dec 2008 + */ + +/* **************************************************** */ + +/* Only delete the nominated keyframe from provided ipo-curve. + * Not recommended to be used many times successively. For that + * there is delete_ipo_keys(). + */ +void delete_icu_key(IpoCurve *icu, int index, short do_recalc) +{ + /* firstly check that index is valid */ + if (index < 0) + index *= -1; + if (icu == NULL) + return; + if (index >= icu->totvert) + return; + + /* Delete this key */ + memmove(&icu->bezt[index], &icu->bezt[index+1], sizeof(BezTriple)*(icu->totvert-index-1)); + icu->totvert--; + + /* recalc handles - only if it won't cause problems */ + if (do_recalc) + calchandles_ipocurve(icu); +} + +/* Delete selected keyframes in given IPO block */ +void delete_ipo_keys(Ipo *ipo) +{ + IpoCurve *icu, *next; + int i; + + if (ipo == NULL) + return; + + for (icu= ipo->curve.first; icu; icu= next) { + /* store pointer to next ipo-curve, as we may delete the current one */ + next = icu->next; + + /* Delete selected BezTriples */ + for (i=0; i<icu->totvert; i++) { + if (icu->bezt[i].f2 & SELECT) { + memmove(&icu->bezt[i], &icu->bezt[i+1], sizeof(BezTriple)*(icu->totvert-i-1)); + icu->totvert--; + i--; + } + } + + /* Only delete if there isn't an ipo-driver still hanging around on an empty curve */ + if ((icu->totvert==0) && (icu->driver==NULL)) { + BLI_remlink(&ipo->curve, icu); + free_ipo_curve(icu); + } + } +} + +/* ---------------- */ + +/* duplicate selected keyframes for the given IPO block */ +void duplicate_ipo_keys(Ipo *ipo) +{ + IpoCurve *icu; + BezTriple *newbezt; + int i; + + if (ipo == NULL) + return; + + for (icu= ipo->curve.first; icu; icu= icu->next) { + for (i=0; i<icu->totvert; i++) { + /* If a key is selected */ + if (icu->bezt[i].f2 & SELECT) { + /* Expand the list */ + newbezt = MEM_callocN(sizeof(BezTriple) * (icu->totvert+1), "beztriple"); + + memcpy(newbezt, icu->bezt, sizeof(BezTriple) * (i+1)); + memcpy(newbezt+i+1, icu->bezt+i, sizeof(BezTriple)); + memcpy(newbezt+i+2, icu->bezt+i+1, sizeof (BezTriple) *(icu->totvert-(i+1))); + icu->totvert++; + + /* reassign pointers... (free old, and add new) */ + MEM_freeN(icu->bezt); + icu->bezt=newbezt; + + /* Unselect the current key*/ + BEZ_DESEL(&icu->bezt[i]); + i++; + + /* Select the copied key */ + BEZ_SEL(&icu->bezt[i]); + } + } + } +} + +/* **************************************************** */ +/* Various Tools */ + +/* Basic IPO-Curve 'cleanup' function that removes 'double points' and unnecessary keyframes on linear-segments only */ +void clean_ipo_curve(IpoCurve *icu, float thresh) +{ + BezTriple *old_bezts, *bezt, *beztn; + BezTriple *lastb; + int totCount, i; + + /* check if any points */ + if ((icu == NULL) || (icu->totvert <= 1)) + return; + + /* make a copy of the old BezTriples, and clear IPO curve */ + old_bezts = icu->bezt; + totCount = icu->totvert; + icu->bezt = NULL; + icu->totvert = 0; + + /* now insert first keyframe, as it should be ok */ + bezt = old_bezts; + insert_vert_icu(icu, bezt->vec[1][0], bezt->vec[1][1], 0); + + /* Loop through BezTriples, comparing them. Skip any that do + * not fit the criteria for "ok" points. + */ + for (i=1; i<totCount; i++) { + float prev[2], cur[2], next[2]; + + /* get BezTriples and their values */ + if (i < (totCount - 1)) { + beztn = (old_bezts + (i+1)); + next[0]= beztn->vec[1][0]; next[1]= beztn->vec[1][1]; + } + else { + beztn = NULL; + next[0] = next[1] = 0.0f; + } + lastb= (icu->bezt + (icu->totvert - 1)); + bezt= (old_bezts + i); + + /* get references for quicker access */ + prev[0] = lastb->vec[1][0]; prev[1] = lastb->vec[1][1]; + cur[0] = bezt->vec[1][0]; cur[1] = bezt->vec[1][1]; + + /* check if current bezt occurs at same time as last ok */ + if (IS_EQT(cur[0], prev[0], thresh)) { + /* If there is a next beztriple, and if occurs at the same time, only insert + * if there is a considerable distance between the points, and also if the + * current is further away than the next one is to the previous. + */ + if (beztn && (IS_EQT(cur[0], next[0], thresh)) && + (IS_EQT(next[1], prev[1], thresh)==0)) + { + /* only add if current is further away from previous */ + if (cur[1] > next[1]) { + if (IS_EQT(cur[1], prev[1], thresh) == 0) { + /* add new keyframe */ + insert_vert_icu(icu, cur[0], cur[1], 0); + } + } + } + else { + /* only add if values are a considerable distance apart */ + if (IS_EQT(cur[1], prev[1], thresh) == 0) { + /* add new keyframe */ + insert_vert_icu(icu, cur[0], cur[1], 0); + } + } + } + else { + /* checks required are dependent on whether this is last keyframe or not */ + if (beztn) { + /* does current have same value as previous and next? */ + if (IS_EQT(cur[1], prev[1], thresh) == 0) { + /* add new keyframe*/ + insert_vert_icu(icu, cur[0], cur[1], 0); + } + else if (IS_EQT(cur[1], next[1], thresh) == 0) { + /* add new keyframe */ + insert_vert_icu(icu, cur[0], cur[1], 0); + } + } + else { + /* add if value doesn't equal that of previous */ + if (IS_EQT(cur[1], prev[1], thresh) == 0) { + /* add new keyframe */ + insert_vert_icu(icu, cur[0], cur[1], 0); + } + } + } + } + + /* now free the memory used by the old BezTriples */ + if (old_bezts) + MEM_freeN(old_bezts); +} + +/* ---------------- */ + +/* temp struct used for smooth_ipo */ +typedef struct tSmooth_Bezt { + float *h1, *h2, *h3; /* bezt->vec[0,1,2][1] */ +} tSmooth_Bezt; + +/* Use a weighted moving-means method to reduce intensity of fluctuations */ +//mode= pupmenu("Smooth IPO%t|Tweak Points%x1|Flatten Handles%x2"); +void smooth_ipo_curve(IpoCurve *icu, short mode) +{ + BezTriple *bezt; + int i, x, totSel = 0; + + /* first loop through - count how many verts are selected, and fix up handles + * this is done for both modes + */ + bezt= icu->bezt; + for (i=0; i < icu->totvert; i++, bezt++) { + if (BEZSELECTED(bezt)) { + /* line point's handles up with point's vertical position */ + bezt->vec[0][1]= bezt->vec[2][1]= bezt->vec[1][1]; + if ((bezt->h1==HD_AUTO) || (bezt->h1==HD_VECT)) bezt->h1= HD_ALIGN; + if ((bezt->h2==HD_AUTO) || (bezt->h2==HD_VECT)) bezt->h2= HD_ALIGN; + + /* add value to total */ + totSel++; + } + } + + /* check if adjust values too... */ + if (mode == 2) { + /* if any points were selected, allocate tSmooth_Bezt points to work on */ + if (totSel >= 3) { + tSmooth_Bezt *tarray, *tsb; + + /* allocate memory in one go */ + tsb= tarray= MEM_callocN(totSel*sizeof(tSmooth_Bezt), "tSmooth_Bezt Array"); + + /* populate tarray with data of selected points */ + bezt= icu->bezt; + for (i=0, x=0; (i < icu->totvert) && (x < totSel); i++, bezt++) { + if (BEZSELECTED(bezt)) { + /* tsb simply needs pointer to vec, and index */ + tsb->h1 = &bezt->vec[0][1]; + tsb->h2 = &bezt->vec[1][1]; + tsb->h3 = &bezt->vec[2][1]; + + /* advance to the next tsb to populate */ + if (x < totSel- 1) + tsb++; + else + break; + } + } + + /* calculate the new smoothed ipo's with weighted averages: + * - this is done with two passes + * - uses 5 points for each operation (which stores in the relevant handles) + * - previous: w/a ratio = 3:5:2:1:1 + * - next: w/a ratio = 1:1:2:5:3 + */ + + /* round 1: calculate previous and next */ + tsb= tarray; + for (i=0; i < totSel; i++, tsb++) { + /* don't touch end points (otherwise, curves slowly explode) */ + if (ELEM(i, 0, (totSel-1)) == 0) { + const tSmooth_Bezt *tP1 = tsb - 1; + const tSmooth_Bezt *tP2 = (i-2 > 0) ? (tsb - 2) : (NULL); + const tSmooth_Bezt *tN1 = tsb + 1; + const tSmooth_Bezt *tN2 = (i+2 < totSel) ? (tsb + 2) : (NULL); + + const float p1 = *tP1->h2; + const float p2 = (tP2) ? (*tP2->h2) : (*tP1->h2); + const float c1 = *tsb->h2; + const float n1 = *tN1->h2; + const float n2 = (tN2) ? (*tN2->h2) : (*tN1->h2); + + /* calculate previous and next */ + *tsb->h1= (3*p2 + 5*p1 + 2*c1 + n1 + n2) / 12; + *tsb->h3= (p2 + p1 + 2*c1 + 5*n1 + 3*n2) / 12; + } + } + + /* round 2: calculate new values and reset handles */ + tsb= tarray; + for (i=0; i < totSel; i++, tsb++) { + /* calculate new position by averaging handles */ + *tsb->h2 = (*tsb->h1 + *tsb->h3) / 2; + + /* reset handles now */ + *tsb->h1 = *tsb->h2; + *tsb->h3 = *tsb->h2; + } + + /* free memory required for tarray */ + MEM_freeN(tarray); + } + } + + /* recalculate handles */ + calchandles_ipocurve(icu); +} + +/* **************************************************** */ |