IMoFlow.h

Go to the documentation of this file.
00001 /*********************************************************************
00002  *<
00003     FILE: bipedapi.h
00004 
00005     DESCRIPTION: Biped motion flow interface classes
00006 
00007     CREATED BY: Ravi Karra, Michael Zyracki
00008 
00009     HISTORY: Created 18 October 1999
00010             
00011  *> Copyright (c) 1999-2003 All Rights Reserved.
00012  **********************************************************************/
00013 
00014 #pragma once
00015 
00016 #include "..\maxheap.h"
00017 #include "BipExp.h"
00018 #include "keytrack.h"
00019 #include "CSConstants.h"
00020 #include "..\assetmanagement\AssetUser.h"
00021 #include "..\point2.h"
00022 #include "..\ipoint2.h"
00023 
00024 /*
00025     Using the Motion Flow classes.
00026     1. You need to make sure you are linking with the biped.lib import library. This is only currently necessary
00027     for linking with these motion flow clases. If you are just using the interface in BipedApi.h,there is no
00028     need to link with this import library.
00029     2. Get the IMoFlow Interface from a corresponding BipMaster.
00030     3. The general MotionFlow class hierarcy.
00031         A.  Each Motion Flow consists of a collection of Snippets(stored animation clips) and Scripts(instances
00032         of the Snippets strung together.).
00033         B. Each Snippet Contains an animation (a .bip file), and a collection of Transistions.
00034         C. Each Transistion specifies an one way motion blend from one snippet to another.
00035         D. Each Script in the IMoFlow is a set of animations(snippets), which are  contains a list of ScriptItem's.
00036         E. Each ScriptItem contains a pointer back to the Snippet it instances.
00037 
00038     NOTE that there is a close correspondence between these SDK classes and functions and the CS motion
00039     flow maxscript implementation, so that may be used as reference for this SDK also.
00040 
00041     NOTE That these classes contain internal biped classes.  These classes are the actually ones used
00042     internally, they aren't interfaces.
00043     
00044     Example of creating a motion flow.
00045         IBipMaster *bipMaster = GetCurrentBipMaster(); //see
00046         POINT graphLocation;
00047         char str[32];
00048         //begin motion flow mode.
00049         bipMaster->BeginModes(BMODE_MOTIONFLOW);
00050         //get the motion flow
00051         IMoFlow *moFlow = bipMaster->GetMoFlow();
00052         //create a walk snippet
00053         sprintf(str,"c:\\Sumo\\walk.bip"); //the file for the snippert
00054         graphLocation.x = 20;
00055         graphLocation.y = 10;
00056         Snippet *walk =  moFlow->NewSnippet(str, graphLocation,true,true);
00057         //create a run snippet
00058         sprintf(str,"c:\\Sumo\\run.bip"); //the file for the snippert
00059         graphLocation.x = 50;
00060         graphLocation.y = 10;
00061         Snippet *run =  moFlow->NewSnippet(str, graphLocation,true,true);
00062         //create an optimized transistion between the 2 snippets.
00063         walk->AddTransition(run,TRUE);
00064         run->AddTransition(walk,TRUE);
00065         //call this global function to make sure all graphs and moflows are updated.
00066         UpdateMotionFlowGraph();
00067         //create a script and set it to be active
00068         sprintf(str,"SDK Script");
00069         moFlow->AddScript(str,true);
00070         //get the script
00071         Script *script  = moFlow->GetActiveScript();
00072         //set the start pos and direction
00073         Point3 startPos(0,0,0);
00074         script->SetStartPos(startPos);
00075         script->SetStartRot(0); //set a 0 heading degree
00076         //create the snipped
00077         script->AddSnippet(run);
00078         moFlow->CurscriptToActiveLinks(); //set the transistions in the script 
00079         script->AddSnippet(walk);
00080         moFlow->CurscriptToActiveLinks(); //set the transistions in the script 
00081         script->AddSnippet(run);
00082         moFlow->CurscriptToActiveLinks(); //set the transistions in the script 
00083         //now compute the animation.
00084         moFlow->ComputeAnimation(TRUE);
00085         //end the motion flow mode
00086         //unify the motion(convert the motion to free form)
00087         bipMaster->UnifyMotion()
00088         bipMaster->EndModes(BMODE_MOTIONFLOW);
00089         return;
00090 
00091 */
00092 
00093 
00094 // Forward Declarations--note that there are certain classes here (class BipMaster, class animal, etc..)
00095 // which are internal classes.
00096 class Snippet;    
00097 class MoFlow;
00098 //Internal classes
00099 class BipMaster;
00100 class Transition;
00101 class MFL_IOProcessor;
00102 class animal;
00103 class Layer;
00104 class path_properties;
00105 class crowd_script;
00106 
00107 //The Transistion Info. Each transistion contains an array of traninfos.
00108 class TranInfo: public MaxHeapOperators {
00109 
00110   public:
00111     //data
00112     int start1,start2,length;  //source and dest start, and length
00113     int rolling1,rolling2,randpct;
00114     int transFocus;
00115     float angle,easein,easeout,cost;
00116     float flow_angle;
00117     path_properties prop;
00118     MCHAR note[MAXTRANNOTE];
00119 
00120     BIPExport       TranInfo();
00121     BIPExport       TranInfo& operator=(const TranInfo& TI);
00122     BIPExport       int operator==(const TranInfo& TI);
00123     
00124     //internal I/O functions
00125     void            Output(MFL_IOProcessor *IOProc);
00126     void            Input(MFL_IOProcessor *IOProc);
00127 
00128     inline int      GetSourceStart() const  { return start1; }
00129     inline void     SetSourceStart(int s)   { start1 = s; }
00130     inline int      GetDestStart() const    { return start2; }
00131     inline void     SetDestStart(int s)     { start2 = s; } 
00132     inline int      GetSourceState() const  { return rolling1; }
00133     inline void     SetSourceState(int s)   { rolling1 = s; }
00134     inline int      GetDestState()  const   { return rolling2; }
00135     inline void     SetDestState(int s)     { rolling2 = s; }
00136     inline int      GetLength()  const      { return length; }
00137     inline void     SetLength(int l)        { length = l; }
00138     inline float    GetAngle()  const       { return angle; }
00139     inline void     SetAngle(float a)       { angle = a; }
00140     inline int      GetRandPercent() const  { return randpct; }
00141     inline void     SetRandPercent(float r) { randpct = (int)r; }
00142     inline float    GetCost() const         { return cost; }
00143     inline void     SetCost(float c)        { cost = c; }
00144     inline float    GetEaseIn()  const      { return easein; }
00145     inline void     SetEaseIn(float in)     { easein = in; }
00146     inline float    GetEaseOut() const      { return easeout; }
00147     inline void     SetEaseOut(float out)   { easeout = out; }
00148     inline int      GetTransFocus()  const  { return transFocus; }
00149     inline void     SetTransFocus(int t)    { transFocus = t; }
00150     inline const    MCHAR* GetNote() const  { return note; }
00151     inline void     SetNote(const MCHAR* n) { _tcscpy_s(note, MAXTRANNOTE, n); }
00152 };
00153 
00154 class Transition: public MaxHeapOperators {
00155   public:
00156     Snippet *from;
00157     Snippet *to;
00158     int active;
00159     int selected;
00160     int start1,start2,length;
00161     int rolling1,rolling2;
00162     float easein,easeout;
00163     float angle;
00164     float flow_angle;
00165     int transFocus;
00166     path_properties prop;
00167     int randpct;
00168     float cost;
00169     int index;
00170     int nTransInfo;
00171     int maxTransInfo;
00172     TranInfo *TranList;
00173     MCHAR note[MAXTRANNOTE];
00174     HWND hwnd;
00175 
00176     BIPExport       Transition();
00177     BIPExport       ~Transition();
00178     BIPExport Transition& operator=(const Transition& T);
00179     BIPExport void  SetTranListSize(int NUM);
00180     BIPExport void  InfoToTransition(int i);
00181     BIPExport void  TransitionToInfo(int i);
00182     BIPExport int   NewTranInfo(); // returns the index of the inserted item
00183     BIPExport void  ShiftTranInfo(int TIindex, int storecurrent);
00184     BIPExport void  DeleteTranInfo(int TIindex);
00185     void            Output(MFL_IOProcessor *IOProc);
00186     void            Input(MFL_IOProcessor *IOProc);
00187 
00188     inline int      GetRandPercent() const  { return randpct; }
00189     inline void     SetRandPercent(float r) { randpct = (int)r; }
00190     inline float    GetCost() const         { return cost; }
00191     inline void     SetCost(float c)        { cost = c; }
00192     
00193     inline int      GetTranListSize() const { return nTransInfo; }
00194     inline Snippet* GetFromSnippet() const  { return from; } 
00195     inline void     SetFromSnippet(Snippet* s) { from = s; }
00196 
00197     inline Snippet* GetToSnippet() const    { return to; }
00198     inline void     SetToSnippet(Snippet* s){ to = s; }
00199 
00200     inline BOOL     GetActive() const       { return active; }
00201     inline void     SetActive(BOOL act)     { active = act; }
00202 
00203     inline BOOL     GetSelected() const     { return selected; }
00204     inline void     SetSelected(BOOL sel)   { selected = sel; }
00205 
00206     inline TranInfo GetTranInfo(int TIindex) const 
00207                         { assert (TIindex < nTransInfo); return TranList[TIindex]; }
00208     inline void     SetTranInfo(int TIindex, TranInfo ti) {
00209                         assert (TIindex < nTransInfo); 
00210                         TranList[TIindex] = ti; 
00211                         if (index == TIindex ) InfoToTransition(index); 
00212                     }
00213 
00214     BIPExport void  UpdateUI(bool redraw=false); 
00215 };
00216 
00217 class Script;
00218 
00219 class Snippet: public MaxHeapOperators {
00220   public:
00221     int visited; //used for depth first tranversal
00222     float distance_to_stop;
00223     float distance_to_loop;
00224     int start;
00225     int end;
00226     MCHAR sname[MAXNAME];
00227     MaxSDK::AssetManagement::AssetUser file;
00228     int posx;
00229     int posy;
00230     int width;
00231     int nTrans;
00232     int curtime; // set anytime you compute A for a particular frame (necessary for looping)
00233     int active;
00234     int validload; // currently loaded file, although possibly inactive, is still valid (or not)
00235     BOOL randstart;    // can this snippet start a random traversal script
00236     int  randstartpct; // percentage of time this snippet starts a random traversal script    
00237     Transition *Transitions;
00238     HWND hwnd;
00239     int orgposx; // for interaction - post-fido
00240     int orgposy; // for interaction - post-fido
00241 
00242   public:
00243     Snippet *next;
00244     animal *A;
00245   
00246     BIPExport       Snippet(); 
00247     BIPExport       virtual ~Snippet();
00248     BIPExport void  ClearActiveTransitions();
00249     BIPExport void  ClearSelectedTransitions();
00250     BIPExport int   ActivateTransitionTo(Snippet *toSN);
00251     BIPExport Transition *GetTransitionTo(Snippet *toSN);
00252     BIPExport int   GetTransitionIndex(Transition *theTR);
00253     BIPExport float AddTransition(Snippet *child,  int optimize); // returns the floating point cost
00254     BIPExport float ComputeTransitionPoints(Transition *nTR, Snippet *Src , Snippet *Dest, int preference, int optimize);
00255     BIPExport float ComputeOptimalTransitionPoints(BOOL SearchAll,int PrefTranLen,int SearchBefore, int SearchAfter, Transition *nTR, Snippet *Src , Snippet *Dst);
00256     BIPExport void  RecomputeTransitions();// recompute existing transitions from this new snippet
00257     BIPExport int   DeleteTransitionsTo(Snippet *child);
00258     BIPExport int   DeleteTransition(int index);
00259     BIPExport void  DeleteSelectedTransitions(MoFlow *MF);
00260     BIPExport int   IsChild(Snippet *Child);
00261     void            Paint(HDC hdc, int selected, int startnode, int editnode, int transhow);
00262     void            PaintTransitions(HDC hdc, BOOL transhow);
00263     void            ComputeWidth(HDC hdc, BOOL transhow);
00264     int             Inside(POINT &mp, float *dist);
00265     BIPExport void  UpdateUI();
00266     BIPExport Snippet& operator=(const Snippet& SN);    
00267     Transition*     TranHitTest(POINT &mp);
00268     void            TranRegionSelect(POINT &min, POINT &max, int set, int active);    
00269     void            Output(MFL_IOProcessor *IOProc);
00270     void            Input(MFL_IOProcessor *IOProc);
00271     BIPExport int   LoadFile(bool UpdateTheUI = true, int ErrorType = 3); // 3 => shows errors in dialog box
00272     Snippet         *GetNextRandScriptSnippet(int *transindex);
00273         
00274     
00276     Snippet *   NextRealTime(BipMaster *mc, Script *scr, animal *A, int frame, path_properties *desired_properties, int global_frame, int global_last_clip_start, int currentScript_index,  int *transindex, int *found);
00277 
00278     
00279 
00280     inline int      GetStart() const            { return start; }
00281     inline void     SetStart(int s)             { start = s; }
00282     inline int      GetEnd() const              { return end; }
00283     inline void     SetEnd(int e)               { end = e; }
00284     
00285     inline const    MCHAR* GetClipName() const  { return sname; }
00286     inline void     SetClipName(const MCHAR* n) { _tcscpy_s(sname, MAXNAME, n); }
00287 
00288     inline const    MaxSDK::AssetManagement::AssetUser&  GetFile() const    { return file; }
00289     inline void     SetFile(const MaxSDK::AssetManagement::AssetUser& assetUser) { file = assetUser; }
00290     
00291     inline virtual  IPoint2 GetPosition() const { return IPoint2(posx, posy); }
00292     inline virtual  void    SetPosition(IPoint2 p){ posx = p.x; posy = p.y; }
00293     inline BOOL     GetActive() const           { return active; }
00294     inline void     SetActive(BOOL act)         { active = act; }
00295 
00296     inline BOOL     GetRandStart() const        { return randstart; }
00297     inline void     SetRandStart(BOOL rs)       { randstart = rs; }
00298     inline BOOL     GetRandStartPercent() const { return randstartpct; }
00299     inline void     SetRandStartPercent(int rsp){ randstartpct = rsp; }
00300     
00301     inline int      NumTransitions()            { return nTrans; }
00302     inline Transition* GetTransition(int Tindex) const 
00303                         { assert (Tindex < nTrans); return &Transitions[Tindex]; }
00304     inline void SetTransition(int Tindex, Transition* ti) 
00305                         { assert (Tindex < nTrans); Transitions[Tindex] = *ti; }
00306 };
00307          
00308 
00309 class ScriptItem: public MaxHeapOperators { 
00310   public:
00311     Snippet     *snip;
00312     int         transindex;
00313     vector      flow_pos;
00314     vector2D    flow_pivot;
00315     float       flow_yaw;
00316     int         glob_snipstart;
00317     MCHAR       *tempSnipName; // for global moflow i/o
00318 
00319     //MG added for CS4
00320     int foothold_frame[2];
00321     int footlift_frame[2];
00322 
00323     inline Snippet* GetSnippet()            { return snip; }
00324     inline void     SetSnippet(Snippet* s)  { snip = s; }
00325     inline int      GetTransIndex() const   { return transindex; }
00326     inline void     SetTransIndex(int i)    { transindex = i; }
00327     inline vector   GetFlowPos() const      { return flow_pos; }
00328     inline void     SetFlowPos(vector p)    { flow_pos = p; }
00329     inline vector2D GetFlowPivot() const    { return flow_pivot; }
00330     inline void     SetFlowPivot(vector2D p){ flow_pivot = p; }
00331     inline float    GetFlowYaw() const      { return flow_yaw; }
00332     inline void     SetFlowYaw(float y)     { flow_yaw = y; }
00333     inline int      GetSnipStart() const    { return glob_snipstart; }
00334     inline void     SetSnipStart(int s)     { glob_snipstart = s; }
00335 
00336     
00337     BIPExport   ScriptItem();
00338     BIPExport   ScriptItem& operator=(const ScriptItem& SI);
00339 };
00340 
00341 class Script: public MaxHeapOperators {
00342   public:
00343     MCHAR name[MAXSCRIPTNAME];
00344     Point3 startpos;
00345     float startrot;
00346     int startframe;
00347     int nitems;
00348     int maxitems;
00349     ScriptItem *items;
00350     int nlayers;
00351     Layer *layers;
00352     Layer *EditLayer;
00353     Script *next;
00354 
00355     BIPExport       Script();
00356     BIPExport       ~Script();
00357     BIPExport Script& operator=(const Script& P);
00358     BIPExport int   AddSnippet(Snippet *SN);
00359     BIPExport int   InsertSnippet(int index, Snippet *SN);
00360     BIPExport int   DeleteSnippet(int index);
00361     BIPExport int   Get_Backtrackframe(); //mg
00362     BIPExport int   GetScriptItemIndex(ScriptItem *SI);
00363     BIPExport int   InsertScriptItem(int index, ScriptItem *SI);
00364     BIPExport int   GetLastTransitionIndex(); //MG m28
00365               Transition *GetLastTransition(); //S43
00366     inline    int   NumScriptItems() { return nitems; }
00367     inline    ScriptItem* GetScriptItem(int ind)
00368                         { assert (ind < nitems); return &items[ind]; }
00369     BIPExport int   IncludesSnippet(Snippet *SN);
00370     BIPExport int   IncludesTransition(Transition *TR);
00371               void  Output(MFL_IOProcessor *IOProc);
00372               void  Input(MFL_IOProcessor *IOProc, MoFlow *MF); 
00373     inline    int   GetStartFrame() { return startframe; }
00374     inline    void  SetStartFrame(int f) { startframe = f; }
00375     BIPExport void  Set_crowd_script(crowd_script *CS);
00376     inline   Point3 GetStartPos() { return startpos; }
00377     inline    void  SetStartPos(Point3 p) { startpos = p;}
00378     inline    float GetStartRot() { return startrot; }
00379     inline    void  SetStartRot(float r) { startrot = r;}
00380     inline    const MCHAR* GetName() const  { return name; }
00381     inline    void  SetName(const MCHAR* n) { _tcscpy_s(name, MAXSCRIPTNAME, n); }
00382 
00383     // The following functions are obsolete
00384     inline    int   NumLayers() { return nlayers; }
00385     inline    Layer *GetEditLayer() { return EditLayer; }
00386     BIPExport Layer *GetLayer(int index);
00387     BIPExport int   AddLayer(Script *SCR, TimeValue t);
00388     BIPExport void  DeleteLayer(Layer *delLY);
00389     BIPExport void  SortLayer(Layer *SORTLY);
00390     BIPExport int   GetLayerIndex(Layer *LYR);
00391     BIPExport int   InsertLayer(Layer *newLY);
00392     BIPExport void  CloneLayer(Layer *orgLY,TimeValue t);
00393     BIPExport void  DeleteAllLayers();
00394 };
00395 
00396 class IBipMaster;
00397 class IMoFlow: public MaxHeapOperators
00398 {
00399   public:
00400     virtual ~IMoFlow() {;}
00401     virtual int     NumScripts() const=0;
00402     virtual int     NumSelected() const=0;          
00403     virtual Script* GetScript(int index)=0;
00404     virtual Script* GetActiveScript()=0;
00405     virtual void    SetActiveScript(Script* scr)=0;
00406     virtual int     GetScriptIndex(Script *theSCR) const=0;
00407     virtual int     AddScript(const MCHAR *newscriptname, bool activate=true)=0;
00408     virtual void    DeleteScript(Script *delSCR)=0;
00409     virtual void    DeleteScript(int ind)=0;
00410     virtual void    DeleteAllScripts()=0;
00411 
00412     virtual int     NumSnippets() const=0;
00413     virtual const   BitArray& GetSelected() const=0;
00414     virtual void    SetSelected(const BitArray& sel)=0;
00415     virtual Snippet* GetSnip(int index)=0;
00416     virtual Snippet* GetSnip(const MCHAR *str)=0;       
00417     virtual int     GetIndex(Snippet *theSN) const=0;
00418     virtual int     LoadSnippetFiles(bool UpdateTheUI = true, int ErrorType = 3, bool UpdateSNLengths=false)=0;
00419     
00420     virtual int     GetActiveStartFrame() const=0;
00421     virtual int     GetActiveEndFrame() const=0;
00422     virtual int     Save(const MCHAR *fname)=0;
00423     virtual int     Load(const MCHAR *fname, bool Append=false, bool UpdateTheUI = true, int ErrorType = 3)=0;
00424     virtual IBipMaster* GetMaster()=0;
00425     virtual void    ComputeAnimation(int redraw, int globalsToo = FALSE)=0; //main function that computes the moflow
00426 
00427     //NOTE if you add a snippet to the script this function must be called to make sure that
00428     //the transistion are all active!
00429     virtual void CurscriptToActiveLinks() = 0;
00430     // Adds a new snippet(clip) to the motion flow network, if load==true it's immediately loaded(if not need to call LoadSnipetFiles,
00431     //if redraw is true then the moflow graph is redraw
00432     virtual Snippet* NewSnippet(const MCHAR *fname, POINT &mp, bool load=false, bool redraw=false)=0;
00433     virtual void    DeleteSnip(Snippet *DelSnip)=0;
00434 
00435     virtual void    RedrawGraphWnd()=0; // update just motion flow editor
00436     virtual void    UpdateUI()=0; // updates the motionflow script dialog and motion flow editor
00437     virtual void    HoldTrack(int HoldAnimals, BOOL HoldScriptOffsetOnly = false)=0; // HoldAll, holds all biped limbs.use to hold the biped.
00438                     
00439 };
00440 
00441 BIPExport void UpdateMotionFlowGraph(); // updates the motionflow graph should be called after mf functions
00442                                         // that manipulate transistions
00443