Merge of itasc branch. Project files, scons and cmake should be working. Makefile...
[blender.git] / source / blender / blenkernel / intern / action.c
index bb458cc..4cfd35a 100644 (file)
@@ -21,6 +21,7 @@
  * All rights reserved.
  *
  * Contributor(s): Full recode, Ton Roosendaal, Crete 2005
+ *                              Full recode, Joshua Leung, 2009
  *
  * ***** END GPL LICENSE BLOCK *****
  */
@@ -31,7 +32,8 @@
 
 #include <string.h>
 #include <math.h>
-#include <stdlib.h>    /* for NULL */
+#include <stdlib.h>
+#include <stddef.h>    
 
 #include "MEM_guardedalloc.h"
 
@@ -60,6 +62,7 @@
 #include "BKE_main.h"
 #include "BKE_object.h"
 #include "BKE_utildefines.h"
+#include "BIK_api.h"
 
 #include "BLI_arithb.h"
 #include "BLI_blenlib.h"
@@ -68,8 +71,6 @@
 #include "RNA_access.h"
 #include "RNA_types.h"
 
-//XXX #include "nla.h"
-
 /* *********************** NOTE ON POSE AND ACTION **********************
 
   - Pose is the local (object level) component of armature. The current
@@ -212,6 +213,7 @@ bAction *copy_action (bAction *src)
        return dst;
 }
 
+/* *************** Action Groups *************** */
 
 /* Get the active action-group for an Action */
 bActionGroup *get_active_actiongroup (bAction *act)
@@ -404,14 +406,15 @@ bActionGroup *action_groups_find_named (bAction *act, const char name[])
        return NULL;
 }
 
-/* ************************ Pose channels *************** */
+/* *************** Pose channels *************** */
 
 /* usually used within a loop, so we got a N^2 slowdown */
 bPoseChannel *get_pose_channel(const bPose *pose, const char *name)
 {
        bPoseChannel *chan;
-
-       if (pose==NULL) return NULL;
+       
+       if (ELEM(NULL, pose, name) || (name[0] == 0))
+               return NULL;
        
        for (chan=pose->chanbase.first; chan; chan=chan->next) {
                if (chan->name[0] == name[0]) {
@@ -449,7 +452,7 @@ bPoseChannel *verify_pose_channel(bPose* pose, const char* name)
        chan->limitmin[0]= chan->limitmin[1]= chan->limitmin[2]= -180.0f;
        chan->limitmax[0]= chan->limitmax[1]= chan->limitmax[2]= 180.0f;
        chan->stiffness[0]= chan->stiffness[1]= chan->stiffness[2]= 0.0f;
-       
+       chan->ikrotweight = chan->iklinweight = 0.0f;
        Mat4One(chan->constinv);
        
        BLI_addtail(&pose->chanbase, chan);
@@ -475,7 +478,18 @@ bPoseChannel *get_active_posechannel (Object *ob)
        return NULL;
 }
 
-
+const char *get_ikparam_name(bPose *pose)
+{
+       if (pose) {
+               switch (pose->iksolver) {
+               case IKSOLVER_LEGACY:
+                       return NULL;
+               case IKSOLVER_ITASC:
+                       return "bItasc";
+               }
+       }
+       return NULL;
+}
 /* dst should be freed already, makes entire duplicate */
 void copy_pose (bPose **dst, bPose *src, int copycon)
 {
@@ -497,7 +511,10 @@ void copy_pose (bPose **dst, bPose *src, int copycon)
        outPose= MEM_callocN(sizeof(bPose), "pose");
        
        BLI_duplicatelist(&outPose->chanbase, &src->chanbase);
-       
+       outPose->iksolver = src->iksolver;
+       outPose->ikdata = NULL;
+       outPose->ikparam = MEM_dupallocN(src->ikparam);
+
        if (copycon) {
                for (pchan=outPose->chanbase.first; pchan; pchan=pchan->next) {
                        copy_constraints(&listb, &pchan->constraints);  // copy_constraints NULLs listb
@@ -509,6 +526,39 @@ void copy_pose (bPose **dst, bPose *src, int copycon)
        *dst=outPose;
 }
 
+void init_pose_itasc(bItasc *itasc)
+{
+       if (itasc) {
+               itasc->iksolver = IKSOLVER_ITASC;
+               itasc->minstep = 0.01f;
+               itasc->maxstep = 0.06f;
+               itasc->numiter = 100;
+               itasc->numstep = 4;
+               itasc->precision = 0.005f;
+               itasc->flag = ITASC_AUTO_STEP|ITASC_INITIAL_REITERATION|ITASC_SIMULATION;
+               itasc->feedback = 20.f;
+               itasc->maxvel = 50.f;
+               itasc->solver = ITASC_SOLVER_SDLS;
+               itasc->dampmax = 0.5;
+               itasc->dampeps = 0.15;
+       }
+}
+void init_pose_ikparam(bPose *pose)
+{
+       bItasc *itasc;
+       switch (pose->iksolver) {
+       case IKSOLVER_ITASC:
+               itasc = MEM_callocN(sizeof(bItasc), "itasc");
+               init_pose_itasc(itasc);
+               pose->ikparam = itasc;
+               break;
+       case IKSOLVER_LEGACY:
+       default:
+               pose->ikparam = NULL;
+               break;
+       }
+}
+
 void free_pose_channels(bPose *pose) 
 {
        bPoseChannel *pchan;
@@ -532,62 +582,15 @@ void free_pose(bPose *pose)
                /* free pose-groups */
                if (pose->agroups.first)
                        BLI_freelistN(&pose->agroups);
-               
-               /* free pose */
-               MEM_freeN(pose);
-       }
-}
-
-void game_copy_pose(bPose **dst, bPose *src)
-{
-       bPose *out;
-       bPoseChannel *pchan, *outpchan;
-       GHash *ghash;
-       
-       /* the game engine copies the current armature pose and then swaps
-        * the object pose pointer. this makes it possible to change poses
-        * without affecting the original blender data. */
-
-       if (!src) {
-               *dst=NULL;
-               return;
-       }
-       else if (*dst==src) {
-               printf("copy_pose source and target are the same\n");
-               *dst=NULL;
-               return;
-       }
-       
-       out= MEM_dupallocN(src);
-       out->agroups.first= out->agroups.last= NULL;
-       BLI_duplicatelist(&out->chanbase, &src->chanbase);
 
-       /* remap pointers */
-       ghash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
+               /* free IK solver state */
+               BIK_clear_data(pose);
 
-       pchan= src->chanbase.first;
-       outpchan= out->chanbase.first;
-       for (; pchan; pchan=pchan->next, outpchan=outpchan->next)
-               BLI_ghash_insert(ghash, pchan, outpchan);
-
-       for (pchan=out->chanbase.first; pchan; pchan=pchan->next) {
-               pchan->parent= BLI_ghash_lookup(ghash, pchan->parent);
-               pchan->child= BLI_ghash_lookup(ghash, pchan->child);
-               pchan->path= NULL;
-       }
-
-       BLI_ghash_free(ghash, NULL, NULL);
-       
-       *dst=out;
-}
+               /* free IK solver param */
+               if (pose->ikparam)
+                       MEM_freeN(pose->ikparam);
 
-void game_free_pose(bPose *pose)
-{
-       if (pose) {
-               /* we don't free constraints, those are owned by the original pose */
-               if(pose->chanbase.first)
-                       BLI_freelistN(&pose->chanbase);
-               
+               /* free pose */
                MEM_freeN(pose);
        }
 }
@@ -694,51 +697,59 @@ void framechange_poses_clear_unkeyed(void)
        }
 }
 
-/* ************************ END Pose channels *************** */
+/* ************************** Bone Groups ************************** */
 
-/* ************** time ****************** */
-
-static bActionStrip *get_active_strip(Object *ob)
+/* Adds a new bone-group */
+void pose_add_group (Object *ob)
 {
-#if 0  // XXX old animation system
-       bActionStrip *strip;
+       bPose *pose= (ob) ? ob->pose : NULL;
+       bActionGroup *grp;
        
-       if(ob->action==NULL)
-               return NULL;
-               
-       for (strip=ob->nlastrips.first; strip; strip=strip->next)
-               if(strip->flag & ACTSTRIP_ACTIVE)
-                       break;
+       if (ELEM(NULL, ob, ob->pose))
+               return;
        
-       if(strip && strip->act==ob->action)
-               return strip;
-#endif // XXX old animation system
-               
-       return NULL;
-}
-
-/* if the conditions match, it converts current time to strip time */
-// TODO: change this adt
-float get_action_frame(Object *ob, float cframe)
-{
-       bActionStrip *strip= get_active_strip(ob);
+       grp= MEM_callocN(sizeof(bActionGroup), "PoseGroup");
+       strcpy(grp->name, "Group");
+       BLI_addtail(&pose->agroups, grp);
+       BLI_uniquename(&pose->agroups, grp, "Group", '.', offsetof(bActionGroup, name), 32);
        
-       //if(strip)
-       //      return get_actionstrip_frame(strip, cframe, 0);
-       return cframe;
+       pose->active_group= BLI_countlist(&pose->agroups);
 }
 
-/* inverted, strip time to current time */
-// TODO: change this to adt
-float get_action_frame_inv(Object *ob, float cframe)
+/* Remove the active bone-group */
+void pose_remove_group (Object *ob)
 {
-       bActionStrip *strip= get_active_strip(ob);
+       bPose *pose= (ob) ? ob->pose : NULL;
+       bActionGroup *grp = NULL;
+       bPoseChannel *pchan;
        
-       //if(strip)
-       //      return get_actionstrip_frame(strip, cframe, 1);
-       return cframe;
+       /* sanity checks */
+       if (ELEM(NULL, ob, pose))
+               return;
+       if (pose->active_group <= 0)
+               return;
+       
+       /* get group to remove */
+       grp= BLI_findlink(&pose->agroups, pose->active_group-1);
+       if (grp) {
+               /* adjust group references (the trouble of using indices!):
+                *      - firstly, make sure nothing references it 
+                *      - also, make sure that those after this item get corrected
+                */
+               for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
+                       if (pchan->agrp_index == pose->active_group)
+                               pchan->agrp_index= 0;
+                       else if (pchan->agrp_index > pose->active_group)
+                               pchan->agrp_index--;
+               }
+               
+               /* now, remove it from the pose */
+               BLI_freelinkN(&pose->agroups, grp);
+               pose->active_group= 0;
+       }
 }
 
+/* ************** F-Curve Utilities for Actions ****************** */
 
 /* Check if the given action has any keyframes */
 short action_has_motion(const bAction *act)
@@ -758,14 +769,15 @@ short action_has_motion(const bAction *act)
 }
 
 /* Calculate the extents of given action */
-void calc_action_range(const bAction *act, float *start, float *end, int incl_hidden)
+void calc_action_range(const bAction *act, float *start, float *end, short incl_modifiers)
 {
        FCurve *fcu;
        float min=999999999.0f, max=-999999999.0f;
-       short foundvert=0;
+       short foundvert=0, foundmod=0;
 
        if (act) {
                for (fcu= act->curves.first; fcu; fcu= fcu->next) {
+                       /* if curve has keyframes, consider them first */
                        if (fcu->totvert) {
                                float nmin, nmax;
                                
@@ -778,10 +790,53 @@ void calc_action_range(const bAction *act, float *start, float *end, int incl_hi
                                
                                foundvert= 1;
                        }
+                       
+                       /* if incl_modifiers is enabled, need to consider modifiers too
+                        *      - only really care about the last modifier
+                        */
+                       if ((incl_modifiers) && (fcu->modifiers.last)) {
+                               FModifier *fcm= fcu->modifiers.last;
+                               
+                               /* only use the maximum sensible limits of the modifiers if they are more extreme */
+                               switch (fcm->type) {
+                                       case FMODIFIER_TYPE_LIMITS: /* Limits F-Modifier */
+                                       {
+                                               FMod_Limits *fmd= (FMod_Limits *)fcm->data;
+                                               
+                                               if (fmd->flag & FCM_LIMIT_XMIN) {
+                                                       min= MIN2(min, fmd->rect.xmin);
+                                               }
+                                               if (fmd->flag & FCM_LIMIT_XMAX) {
+                                                       max= MAX2(max, fmd->rect.xmax);
+                                               }
+                                       }
+                                               break;
+                                               
+                                       case FMODIFIER_TYPE_CYCLES: /* Cycles F-Modifier */
+                                       {
+                                               FMod_Cycles *fmd= (FMod_Cycles *)fcm->data;
+                                               
+                                               if (fmd->before_mode != FCM_EXTRAPOLATE_NONE)
+                                                       min= MINAFRAMEF;
+                                               if (fmd->after_mode != FCM_EXTRAPOLATE_NONE)
+                                                       max= MAXFRAMEF;
+                                       }
+                                               break;
+                                               
+                                       // TODO: function modifier may need some special limits
+                                               
+                                       default: /* all other standard modifiers are on the infinite range... */
+                                               min= MINAFRAMEF;
+                                               max= MAXFRAMEF;
+                                               break;
+                               }
+                               
+                               foundmod= 1;
+                       }
                }
        }       
        
-       if (foundvert) {
+       if (foundvert || foundmod) {
                if(min==max) max+= 1.0f;
                *start= min;
                *end= max;
@@ -792,9 +847,102 @@ void calc_action_range(const bAction *act, float *start, float *end, int incl_hi
        }
 }
 
+/* Return flags indicating which transforms the given object/posechannel has 
+ *     - if 'curves' is provided, a list of links to these curves are also returned
+ */
+short action_get_item_transforms (bAction *act, Object *ob, bPoseChannel *pchan, ListBase *curves)
+{
+       PointerRNA ptr;
+       FCurve *fcu;
+       char *basePath=NULL;
+       short flags=0;
+       
+       /* build PointerRNA from provided data to obtain the paths to use */
+       if (pchan)
+               RNA_pointer_create((ID *)ob, &RNA_PoseChannel, pchan, &ptr);
+       else if (ob)
+               RNA_id_pointer_create((ID *)ob, &ptr);
+       else    
+               return 0;
+               
+       /* get the basic path to the properties of interest */
+       basePath= RNA_path_from_ID_to_struct(&ptr);
+       if (basePath == NULL)
+               return 0;
+               
+       /* search F-Curves for the given properties 
+        *      - we cannot use the groups, since they may not be grouped in that way...
+        */
+       for (fcu= act->curves.first; fcu; fcu= fcu->next) {
+               char *bPtr=NULL, *pPtr=NULL;
+               
+               /* if enough flags have been found, we can stop checking unless we're also getting the curves */
+               if ((flags == ACT_TRANS_ALL) && (curves == NULL))
+                       break;
+                       
+               /* just in case... */
+               if (fcu->rna_path == NULL)
+                       continue;
+               
+               /* step 1: check for matching base path */
+               bPtr= strstr(fcu->rna_path, basePath);
+               
+               if (bPtr) {
+                       /* step 2: check for some property with transforms 
+                        *      - to speed things up, only check for the ones not yet found 
+                        *        unless we're getting the curves too
+                        *      - if we're getting the curves, the BLI_genericNodeN() creates a LinkData
+                        *        node wrapping the F-Curve, which then gets added to the list
+                        *      - once a match has been found, the curve cannot possibly be any other one
+                        */
+                       if ((curves) || (flags & ACT_TRANS_LOC) == 0) {
+                               pPtr= strstr(fcu->rna_path, "location");
+                               if ((pPtr) && (pPtr >= bPtr)) {
+                                       flags |= ACT_TRANS_LOC;
+                                       
+                                       if (curves) 
+                                               BLI_addtail(curves, BLI_genericNodeN(fcu));
+                                       continue;
+                               }
+                       }
+                       
+                       if ((curves) || (flags & ACT_TRANS_SCALE) == 0) {
+                               pPtr= strstr(fcu->rna_path, "scale");
+                               if ((pPtr) && (pPtr >= bPtr)) {
+                                       flags |= ACT_TRANS_SCALE;
+                                       
+                                       if (curves) 
+                                               BLI_addtail(curves, BLI_genericNodeN(fcu));
+                                       continue;
+                               }
+                       }
+                       
+                       if ((curves) || (flags & ACT_TRANS_ROT) == 0) {
+                               pPtr= strstr(fcu->rna_path, "rotation");
+                               if ((pPtr) && (pPtr >= bPtr)) {
+                                       flags |= ACT_TRANS_ROT;
+                                       
+                                       if (curves) 
+                                               BLI_addtail(curves, BLI_genericNodeN(fcu));
+                                       continue;
+                               }
+                       }
+               }
+       }
+       
+       /* free basePath */
+       MEM_freeN(basePath);
+       
+       /* return flags found */
+       return flags;
+}
+
+/* ************** Pose Management Tools ****************** */
+
 /* Copy the data from the action-pose (src) into the pose */
 /* both args are assumed to be valid */
 /* exported to game engine */
+/* Note! this assumes both poses are aligned, this isnt always true when dealing with user poses */
 void extract_pose_from_pose(bPose *pose, const bPose *src)
 {
        const bPoseChannel *schan;
@@ -805,7 +953,7 @@ void extract_pose_from_pose(bPose *pose, const bPose *src)
                return;
        }
 
-       for (schan=src->chanbase.first; schan; schan=schan->next, pchan= pchan->next) {
+       for (schan=src->chanbase.first; (schan && pchan); schan=schan->next, pchan= pchan->next) {
                copy_pose_channel_data(pchan, schan);
        }
 }
@@ -1033,75 +1181,6 @@ static void blend_pose_offset_bone(bActionStrip *strip, bPose *dst, bPose *src,
        VecAddf(dst->cyclic_offset, dst->cyclic_offset, src->cyclic_offset);
 }
 
-
-/* Only allowed for Poses with identical channels */
-void blend_poses(bPose *dst, bPose *src, float srcweight, short mode)
-{
-       bPoseChannel *dchan;
-       const bPoseChannel *schan;
-       bConstraint *dcon, *scon;
-       float dstweight;
-       int i;
-       
-       switch (mode){
-       case ACTSTRIPMODE_BLEND:
-               dstweight = 1.0F - srcweight;
-               break;
-       case ACTSTRIPMODE_ADD:
-               dstweight = 1.0F;
-               break;
-       default :
-               dstweight = 1.0F;
-       }
-       
-       schan= src->chanbase.first;
-       for (dchan = dst->chanbase.first; dchan; dchan=dchan->next, schan= schan->next){
-               if (schan->flag & (POSE_ROT|POSE_LOC|POSE_SIZE)) {
-                       /* replaced quat->matrix->quat conversion with decent quaternion interpol (ton) */
-                       
-                       /* Do the transformation blend */
-                       if (schan->flag & POSE_ROT) {
-                               /* quat interpolation done separate */
-                               if (schan->rotmode == PCHAN_ROT_QUAT) {
-                                       float dquat[4], squat[4];
-                                       
-                                       QUATCOPY(dquat, dchan->quat);
-                                       QUATCOPY(squat, schan->quat);
-                                       if (mode==ACTSTRIPMODE_BLEND)
-                                               QuatInterpol(dchan->quat, dquat, squat, srcweight);
-                                       else {
-                                               QuatMulFac(squat, srcweight);
-                                               QuatMul(dchan->quat, dquat, squat);
-                                       }
-                                       
-                                       NormalQuat(dchan->quat);
-                               }
-                       }
-
-                       for (i=0; i<3; i++) {
-                               /* blending for loc and scale are pretty self-explanatory... */
-                               if (schan->flag & POSE_LOC)
-                                       dchan->loc[i] = (dchan->loc[i]*dstweight) + (schan->loc[i]*srcweight);
-                               if (schan->flag & POSE_SIZE)
-                                       dchan->size[i] = 1.0f + ((dchan->size[i]-1.0f)*dstweight) + ((schan->size[i]-1.0f)*srcweight);
-                               
-                               /* euler-rotation interpolation done here instead... */
-                               // FIXME: are these results decent?
-                               if ((schan->flag & POSE_ROT) && (schan->rotmode))
-                                       dchan->eul[i] = (dchan->eul[i]*dstweight) + (schan->eul[i]*srcweight);
-                       }
-                       dchan->flag |= schan->flag;
-               }
-               for(dcon= dchan->constraints.first, scon= schan->constraints.first; dcon && scon; dcon= dcon->next, scon= scon->next) {
-                       /* no 'add' option for constraint blending */
-                       dcon->enforce= dcon->enforce*(1.0f-srcweight) + scon->enforce*srcweight;
-               }
-       }
-       
-       /* this pose is now in src time */
-       dst->ctime= src->ctime;
-}
-
 typedef struct NlaIpoChannel {
        struct NlaIpoChannel *next, *prev;
        float val;
@@ -1109,7 +1188,7 @@ typedef struct NlaIpoChannel {
        int type;
 } NlaIpoChannel;
 
-void extract_ipochannels_from_action(ListBase *lb, ID *id, bAction *act, const char *name, float ctime)
+static void extract_ipochannels_from_action(ListBase *lb, ID *id, bAction *act, const char *name, float ctime)
 {
        bActionChannel *achan= get_action_channel(act, name);
        IpoCurve *icu;
@@ -1202,7 +1281,7 @@ static void blend_ipochannels(ListBase *dst, ListBase *src, float srcweight, int
        }
 }
 
-int execute_ipochannels(ListBase *lb)
+static int execute_ipochannels(ListBase *lb)
 {
        NlaIpoChannel *nic;
        int count = 0;