Alphabetical Class Index  Class Hierarchy   File Members   Compound Members   File List  

HBhvUtility.h

Go to the documentation of this file.
00001 //
00002 // Copyright (c) 2000 by Tech Soft 3D, LLC.
00003 // The information contained herein is confidential and proprietary to
00004 // Tech Soft 3D, LLC., and considered a trade secret as defined under
00005 // civil and criminal statutes.  Tech Soft 3D shall pursue its civil
00006 // and criminal remedies in the event of unauthorized use or misappropriation
00007 // of its trade secrets.  Use of this information by anyone other than
00008 // authorized employees of Tech Soft 3D, LLC. is granted only under a
00009 // written non-disclosure agreement, expressly prescribing the scope and
00010 // manner of such use.
00011 //
00012 //"Parts of HQuat Copyright (C) Jason Shankel, 2000"
00013 //
00014 // $Id: 9bce6c53e985720ae7ee56105e9327ab6b2a05c4 $
00015 //
00016 
00021 #ifndef _H_HBhvUtility_H
00022 #define _H_HBhvUtility_H
00023 
00024 #ifdef H_PACK_8
00025 #pragma pack(push)
00026 #pragma pack(8)
00027 #endif
00028 
00029 #include "HTools.h"
00030 
00031 #ifndef SWIG
00032 #include "BStream.h"
00033 #endif
00034 
00035 class HUtilityXMLTag;
00036 class HUtilityTextParser;
00037 class HBhvTimeline;
00038 class HBaseModel;
00039 class HStreamFileToolkit;
00040 class HBaseView;
00041 class HBaseModel;
00042 class HBhvAnimation;
00043 
00044 #define         BHV_MAX_NAME_LENGTH     256     
00045 #define         BHV_MAX_VERSION_LENGTH  256         
00046 #define         BHV_MAX_VENDOR_LENGTH   4096            
00047 #define         BHV_MAX_TYPE_LENGTH     4096        
00048 
00049 
00051 enum HANIKeyframeType {
00052     HANIChannel,            
00053     HANIRotation,           
00054     HANIString,             
00055     HANI3String             
00056 };
00057 
00058 
00060 enum HANIChannelType {
00061     HANILinear,             
00062     HANIHermiteSpline,      
00063     HANIBezierSpline,       
00064     HANIFollowPath,         
00065     HANIDiscrete            
00066 };
00067 
00068 
00070 enum HANIRotationType {     
00071     HANIAxisRotation,       
00072     HANIQuatSlerpRotation,  
00073     HANIQuatSquadRotation,  
00074     HANIEulerRotation       
00075 };
00076 
00077 
00078 
00079                     
00081 
00086 class MVO_API HQuat
00087 {
00088 public:
00089     float x;        
00090     float y;        
00091     float z;        
00092     float w;        
00093     const HQuat &Normalize();  
00095     HQuat() { x=0.0f;y=0.0f;z=0.0f; w=0;};
00097     HQuat(float X,float Y,float Z, float W) { x=X;y=Y;z=Z; w=W;};
00099     HQuat(HQuat const *p) { x=p->x;y=p->y;z=p->z; w=p->w;};
00101     HQuat(const HQuat &p) { x=p.x;y=p.y;z=p.z; w=p.w;};
00102     
00104     void Set(float X,float Y,float Z, float W) { x=X;y=Y;z=Z; w=W; };
00105 
00111     HQuat operator *(const HQuat &q2) const
00112     {
00113         
00114         return HQuat(y*q2.z - z*q2.y + w*q2.x + x*q2.w,
00115                         z*q2.x - x*q2.z + w*q2.y + y*q2.w,
00116                         x*q2.y - y*q2.x + w*q2.z + z*q2.w,
00117                         w*q2.w - x*q2.x - y*q2.y - z*q2.z);
00118     }
00119 
00125     HQuat operator *(float v) const
00126     {
00127         return HQuat(x*v, y*v, z*v, w*v);
00128     }
00129 
00136     friend HQuat operator* (float v, const HQuat& q)
00137     {
00138         return HQuat(v*q.x,v*q.y,v*q.z, v*q.w);
00139     }
00140 
00146     HQuat operator /(float v) const
00147     {
00148         return HQuat(x/v, y/v, z/v, w/v);
00149     }
00150         HQuat operator -(const HQuat &q2) const
00156     {
00157         return HQuat(x-q2.x, y-q2.y, z-q2.z, w-q2.w);
00158     }
00159 
00165     HQuat operator +(const HQuat &q2) const
00166     {
00167         return HQuat(x+q2.x, y+q2.y, z+q2.z, w+q2.w);
00168     }
00169     
00176     static HQuat Qlog(const HQuat &q);
00177  
00182     void ToMatrix(float matrix[16]);
00183     
00184 
00190     static HQuat Qexp(const HQuat &q);  
00198     static HQuat Qlerp(const HQuat &q1,const HQuat &q2,float fraction);
00206     static HQuat Qslerp(const HQuat &q1,const HQuat &q2,float fraction);
00215     static HQuat QslerpNoInvert(const HQuat &q1,const HQuat &q2,float fraction);
00224     static HQuat QslerpNoInvertExtraSpins(const HQuat &q1,const HQuat &q2,float fraction, int ExtraSpins);
00234     static HQuat Qsquad(const HQuat &q1,const HQuat &q2,const HQuat &a,const HQuat &b,float fraction);
00242     static HQuat Qspline(const HQuat &qnm1,const HQuat &qn,const HQuat &qnp1);
00247     static HQuat MatrixToQuaternion(float *matrix);
00248 
00249 };
00250 
00251  
00252 
00254 
00260 class MVO_API HKeyframe
00261 {
00262 public:
00266     HKeyframe()
00267     {
00268         m_bConstant = false;
00269         m_bEaseInOut = false;
00270     }
00271     virtual ~HKeyframe() {};
00272 
00278     virtual HKeyframe *Clone() { 
00279         HKeyframe *p = new HKeyframe;
00280         *p =  *this;
00281         return p;
00282     }
00283 
00287     virtual void Serialize(HUtilityXMLTag *xmlgen) {
00288         UNREFERENCED(xmlgen);
00289     }
00290  
00296     virtual void SetConstant(bool constant) { m_bConstant = constant; }
00302     virtual void SetEaseInOut(bool easeinout) { m_bEaseInOut = easeinout; }
00304     virtual void SetRelative(bool relative) { m_bRelative = relative; }
00305   
00306     HANIKeyframeType    m_type;         
00307     bool                m_bConstant;    
00308     bool                m_bRelative;    
00309     bool                m_bEaseInOut;   
00310 };
00311 
00312 
00314 
00317 class MVO_API HKeyframeChannel : public HKeyframe
00318 {
00319 public:
00320 
00324     HKeyframeChannel()
00325     {
00326         m_cp.Set(0.0f,0.0f,0.0f);
00327         m_type =  HANIChannel;
00328     }
00329     virtual ~HKeyframeChannel() {};
00330 
00336     virtual HKeyframe *Clone () 
00337     {
00338         HKeyframeChannel *p= new HKeyframeChannel;
00339         *p = *this;
00340         return p;
00341     }
00342 
00351     virtual void Interpolate(HKeyframeChannel **posarray, int keyframe, float fraction, int length, HPoint &res) {
00352         UNREFERENCED(posarray);
00353         UNREFERENCED(keyframe);
00354         UNREFERENCED(fraction);
00355         UNREFERENCED(length);
00356         UNREFERENCED(res);
00357     }
00358     
00359     HPoint          m_cp;                   
00360     HANIChannelType m_channeltype;          
00361 };
00362 
00363 
00364 
00365  
00367 class MVO_API HKeyframeChannelLinear : public HKeyframeChannel
00368 {
00369 public:
00370  
00374     HKeyframeChannelLinear(float x=0, float y=0,  float z=0) : HKeyframeChannel()
00375     {
00376         m_channeltype = HANILinear;
00377         m_cp.Set(x,y,z);
00378     }
00379     virtual ~HKeyframeChannelLinear() {};
00380 
00386     virtual HKeyframe *Clone () 
00387     {
00388         HKeyframeChannelLinear *p= new HKeyframeChannelLinear;
00389         *p = *this;
00390         return p;
00391     }
00392  
00401     virtual void Interpolate(HKeyframeChannel **posarray, int keyframe, float fraction, int length, HPoint &res);
00402 
00406     virtual void Serialize(HUtilityXMLTag *xmlgen);
00407 
00408 };
00409 
00411 
00414 class MVO_API HKeyframeChannelDiscrete : public HKeyframeChannel
00415 {
00416 public:
00417  
00421     HKeyframeChannelDiscrete(float x=0, float y=0, float z = 0) : HKeyframeChannel()
00422     {
00423         m_channeltype = HANIDiscrete;
00424         m_cp.Set(x,y,z);
00425 
00426     }
00427     virtual ~HKeyframeChannelDiscrete() {};
00428 
00434     virtual HKeyframe *Clone () 
00435     {
00436         HKeyframeChannelDiscrete *p= new HKeyframeChannelDiscrete;
00437         *p = *this;
00438         return p;
00439     }
00440 
00451     virtual void Interpolate(HKeyframeChannel **posarray, int keyframe, float fraction, int length, HPoint &res);
00452 
00456     virtual void Serialize(HUtilityXMLTag *xmlgen);
00457  
00458 };
00459 
00460 
00461 
00463 
00465 class MVO_API HKeyframeChannelCurve : public HKeyframeChannel
00466 {
00467 public:
00468 
00472     HKeyframeChannelCurve(float x = 0, float y = 0, float z = 0) : HKeyframeChannel()
00473     {
00474         m_channeltype = HANIHermiteSpline;
00475         m_cp.Set(x,y,z);
00476 
00477     }
00478     virtual ~HKeyframeChannelCurve() {};
00479 
00485     virtual HKeyframe *Clone () 
00486     {
00487         HKeyframeChannelCurve *p= new HKeyframeChannelCurve;
00488         *p = *this;
00489         return p;
00490     }
00491 
00498     void CalculateCurveFactor(HBhvTimeline *timeline, int keyframe);
00499 
00508     void CalculateHermiteTangents(HPoint p0, HPoint p2, HPoint p3, float a = 0.5f); 
00509 
00517     HPoint InterpolateHermiteSpline(float fraction, HPoint p1, HPoint p2);
00518 
00519 //  HPoint InterpolateBezierSpline(float fraction, HPoint p2);
00520 
00521 
00526     void CalculateCurveLength(HPoint p2);
00527 
00533     void DrawTangents(HPoint p2);
00534 
00543     virtual void Interpolate(HKeyframeChannel **posarray, int keyframe, float fraction, int length, HPoint &res);
00544 
00548     virtual void Serialize(HUtilityXMLTag *xmlgen);
00549     
00550     HPoint      m_tangent1;                     
00551     HPoint      m_tangent2;                     
00552     float       m_factor1;                      
00553     float       m_factor2;                      
00554     HPoint*     control_polygon;                
00555     float       ItemType;                       
00556     float       c;                              
00557     float       b;                              
00558     float       m_curvelength;                  
00560 };
00561 
00563 
00567 class MVO_API HKeyframeChannelFollowPath : public HKeyframeChannelCurve
00568 {
00569 
00570 public:
00571  
00575     HKeyframeChannelFollowPath() 
00576     {
00577         m_channeltype = HANIFollowPath;
00578         m_bLinear = false;
00579     }
00580     virtual ~HKeyframeChannelFollowPath() {};
00581 
00587     virtual HKeyframe *Clone () 
00588     {
00589         HKeyframeChannelFollowPath *p= new HKeyframeChannelFollowPath;
00590         *p = *this;
00591         return p;
00592     }
00593 
00602     virtual void Interpolate(HKeyframeChannel **posarray, int keyframe, float fraction, int length, HPoint &res);
00603     virtual void Serialize(HUtilityXMLTag *xmlgen);
00604 
00605 //  virtual void Serialize(HUtilityXMLTag *xmlgen);
00606     float tmatrix[16];              
00607     float tmatrix2[16];             
00608     bool m_bLinear;
00609     bool m_bNoRotate;
00610 };
00611 
00612 
00613  
00614 
00616 class MVO_API HKeyframeRotation : public HKeyframe
00617 {
00618 public:
00619  
00623     HKeyframeRotation()
00624     {
00625         m_type = HANIRotation;
00626         m_bLinear = false;
00627     }
00628     virtual ~HKeyframeRotation() {};
00629 
00635     virtual HKeyframe *Clone () 
00636     {
00637         HKeyframeRotation *p= new HKeyframeRotation;
00638         *p = *this;
00639         return p;
00640     } 
00641 
00642     HANIRotationType m_rotationtype;            
00643     bool m_bLinear;                             
00645 };
00646 
00647 
00648 
00650 class MVO_API HKeyframeAxisRotation : public HKeyframeRotation
00651 {
00652 public:
00653 
00654 
00659     HKeyframeAxisRotation(float angle = 0)
00660     {
00661         m_rotationtype = HANIAxisRotation;
00662         m_angle = angle;
00663 
00664     }
00665     virtual ~HKeyframeAxisRotation() {};
00666 
00672     virtual HKeyframe *Clone () 
00673     {
00674         HKeyframeAxisRotation *p= new HKeyframeAxisRotation;
00675         *p = *this;
00676         return p;
00677 
00678     } 
00679 
00683     virtual void Serialize(HUtilityXMLTag *xmlgen);
00684 
00685     float m_angle;              
00686 };
00687 
00688 
00690 class MVO_API HKeyframeQuatSlerp : public HKeyframeRotation
00691 {
00692 public:
00693 
00697     HKeyframeQuatSlerp(float x = 0, float y = 0, float z = 0, float w = 0)
00698     {
00699         m_rotationtype = HANIQuatSlerpRotation;
00700         m_quat.Set(x,y,z,w);
00701     }
00702 
00703     virtual ~HKeyframeQuatSlerp() {};
00704  
00710     virtual HKeyframe *Clone () 
00711     {
00712         HKeyframeQuatSlerp *p= new HKeyframeQuatSlerp;
00713         *p = *this;
00714         return p;
00715     }   
00716     
00717     HQuat m_quat;           
00718 };
00719 
00720 
00721 
00722 
00724 class MVO_API HKeyframeQuatSquad : public HKeyframeRotation
00725 {
00726 public:
00727 
00728  
00732     HKeyframeQuatSquad(float x = 0, float y = 0, float z = 0, float w = 0)
00733     {
00734         m_rotationtype = HANIQuatSquadRotation;
00735         m_quat.Set(x,y,z,w);
00736         m_ExtraSpins = 0;
00737 
00738     }
00739     virtual ~HKeyframeQuatSquad() {};
00740 
00746     virtual HKeyframe *Clone () 
00747     {
00748         HKeyframeQuatSquad *p= new HKeyframeQuatSquad;
00749         *p = *this;
00750         return p;
00751     } 
00752 
00756     virtual void Serialize(HUtilityXMLTag *xmlgen);
00757 
00758     HQuat m_quat;           
00759     int m_ExtraSpins;   
00760 
00761 };
00762 
00763 
00765 class MVO_API HKeyframeMatrix : public HKeyframeRotation
00766 {
00767 public:
00768 
00769  
00773     HKeyframeMatrix(float  * matrix = 0)
00774     {
00775         m_rotationtype = HANIQuatSquadRotation;
00776 
00777         if (matrix)
00778         {       
00779         for (int i=0;i<16;i++)
00780             m_matrix[i] = matrix[i];
00781         }
00782  
00783     }
00784     virtual ~HKeyframeMatrix() {};
00785 
00791     virtual HKeyframe *Clone () 
00792     {
00793         HKeyframeMatrix *p= new HKeyframeMatrix;
00794         *p = *this;
00795         return p;
00796     } 
00797 
00801     virtual void Serialize(HUtilityXMLTag *xmlgen);
00802 
00803     float m_matrix[16];         
00804     int m_ExtraSpins;   
00805 
00806 };
00807 
00808 
00809 
00810 
00811 
00813 
00817 class MVO_API HKeyframeString : public HKeyframe
00818 {
00819 public:
00820     
00824     HKeyframeString();
00825     virtual ~HKeyframeString() {};
00826 
00832     virtual HKeyframe *Clone () 
00833     {
00834         HKeyframeString *p= new HKeyframeString;
00835         *p = *this;
00836         return p;
00837     }
00838 
00842     virtual void Serialize(HUtilityXMLTag *xmlgen);
00843 
00844     
00849     void SetTarget(const char *target);
00850 
00853     char * GetTarget() { return m_target; }
00854 
00855     char m_target[MVO_BUFFER_SIZE];         
00856     bool m_bDiscrete;
00857 
00858 };
00859 
00860 
00862 
00866 class MVO_API HKeyframe3String : public HKeyframe
00867 {
00868 public:
00869     
00873     HKeyframe3String();
00874     virtual ~HKeyframe3String() {};
00875 
00881     virtual HKeyframe *Clone () 
00882     {
00883         HKeyframe3String *p= new HKeyframe3String;
00884         *p = *this;
00885         return p;
00886     }
00887 
00891     virtual void Serialize(HUtilityXMLTag *xmlgen);
00892 
00893     
00898     void SetTarget(const char *t1, const char *t2, const char *t3);
00899 
00902     char * GetTarget1() { return m_t1; }
00903     char * GetTarget2() { return m_t2; }
00904     char * GetTarget3() { return m_t3; }
00905 
00906     char m_t1[255];         
00907     char m_t2[255];         
00908     char m_t3[255];         
00910 };
00911 
00912 
00913 
00914 
00915 
00917 
00921 class MVO_API ShellMorphInfo {
00922 public:
00923 
00927     ShellMorphInfo();
00928     struct vlist_s* arrays_list;            
00929     HC_KEY original_key;                    
00930     HC_KEY temp_key;                        
00932 };
00933 
00934 
00935 
00936 #ifndef SWIG
00938 class MVO_API TK_Bhv_XML : public TK_XML
00939 {
00940 
00941  
00942 public:
00943 
00948     TK_Bhv_XML(HBaseModel * model) : TK_XML() 
00949     { 
00950         m_pModel = model;
00951     }
00952 
00954     TK_Status   Interpret(BStreamFileToolkit &tk, ID_Key key, int variant);
00956     TK_Status   Execute (BStreamFileToolkit & tk);
00957 
00958 private:
00959     HBaseModel *    m_pModel;   
00960 };
00961 #endif
00962 
00963 
00964 
00966 class MVO_API HBhvUtility
00967 {
00968 public:
00969     
00975     static void GetQuatPoint(HUtilityTextParser *tp, HQuat &quat);
00976     static void GetMatrix(HUtilityTextParser *tp, float *matrix);
00977 
00978 
00984     static void GetQuatPoint(const char *text, HQuat &quat);
00985 
00986     static void GetMatrix(const char *text, float *matrix);
00987 
00988 
00994     static void ConvertHSFtoMorph(const char *filename, HBaseModel *model);
00995 
01001     static void ConvertHSFtoSegSwitch(const char *filename, HBaseModel *model); 
01002 
01008     static void SetupAnimationReadFromHSF(HStreamFileToolkit *mytool, HBaseModel *model);
01009     
01015     static void SetupAnimationWriteFromHSF(HStreamFileToolkit *mytool, HBaseModel *model);
01016 
01021     static void ShowModellingMatrix(float *segment_modelling_matrix);
01022 
01023     static void MakeCameraKeyframe(HBaseView *view, float timediff, bool targetmode = false);
01024 
01025     static HBhvAnimation* AddAnimation(HBaseModel *model, char *name, char *targetpath, HPoint *pivot);
01026     static void AddPosKeyframe(HBaseModel *model, char *animname, int tick, HPoint pos, bool linear);
01027     static void AddRotKeyframe(HBaseModel *model, char *animname, int tick, HQuat pos, bool linear);
01028     static void AddPosRotKeyframe(HBaseModel *model, const char *animname, int tick, HPoint pivot, float *matrix);
01029     static void AddMatrixKeyframe(HBaseModel *model, HBhvAnimation *ani, int tick, float *matrix);
01030 
01031     static void AddPosRotKeyframe(HBaseModel *model, char *animname, int tick, HPoint pos, bool linear, HQuat quat, bool linear2);
01032     static void AddInstanceCreateKeyframe(HBaseModel *model, char *animname, int tick, char *path, char *include_path, char *color);
01033     static void AddSegmentMoveKeyframe(HBaseModel *model, HBhvAnimation *anim, int tick, char *path);
01034 
01035 
01036 
01037 
01038 
01039 private:        
01040     static void Move_Segments_To_Segment(HC_KEY target, HC_KEY destination);
01041     static void AddShellToMorphList(HC_KEY key, struct vlist_s *morph_data_list);       
01042     static bool KeyExists(HC_KEY key, struct vlist_s *morp_data_list);
01043     static void ResetMorphData(struct vlist_s *morp_data_list);
01044     static void CollectShellMorphData(HC_KEY key, struct vlist_s *morp_data_list);
01045     static void ClearAllAttributes(HC_KEY key);
01046     static int searchcounter;
01047 };
01048 
01049 typedef struct
01050 {
01051     HC_KEY segkey;
01052     float *matrix;
01053     HBhvAnimation *anim;
01054     HBhvAnimation *anim2;
01055     int tick;
01056     int tick2;
01057     char *path;
01058 }segmentanimation;
01059 
01060 
01061 class MVO_API HBhvSegmentTracker
01062 {
01063 public:
01064     HBhvSegmentTracker(HBaseModel *model);
01065     void MakeTreeSnapshotRecursive(HC_KEY segkey, HC_KEY newsegkey, MVO_POINTER_SIZED_INT &uid);
01066     void MakeTreeSnapshot();
01067 
01068     HC_KEY m_TrackerKey;
01069     HBaseModel *m_pModel;
01070     struct vhash_s *        m_NewIncludesHash;
01071 };
01072 
01073 class MVO_API HBhvKeyframeGenerator
01074 {
01075 public:
01076     HBhvKeyframeGenerator(HBaseModel * model);
01077 
01078     void NewFrame();
01079     void StartFrames();
01080     
01081     bool CompareMatrices(float *m1, float *m2);
01082 
01083 private:
01084     HBaseModel *m_pModel;
01085     struct vlist_s *        m_Anims;
01086     struct vhash_s *        m_Segments;
01087     int m_AnimCounter;
01088     int m_Tick;
01089     bool m_bFirstFrame;
01090     HBhvSegmentTracker *m_pSegmentTracker;
01091 };
01092 
01093 
01094     
01095 
01096 
01097 #ifdef H_PACK_8
01098 #pragma pack(pop)
01099 #endif
01100 
01101 #endif
01102 
01103 
01104