Blender V2.61 - r43446

btSoftBody.h

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose,
00008 including commercial applications, and to alter it and redistribute it freely,
00009 subject to the following restrictions:
00010 
00011 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00016 
00017 #ifndef _BT_SOFT_BODY_H
00018 #define _BT_SOFT_BODY_H
00019 
00020 #include "LinearMath/btAlignedObjectArray.h"
00021 #include "LinearMath/btTransform.h"
00022 #include "LinearMath/btIDebugDraw.h"
00023 #include "BulletDynamics/Dynamics/btRigidBody.h"
00024 
00025 #include "BulletCollision/CollisionShapes/btConcaveShape.h"
00026 #include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h"
00027 #include "btSparseSDF.h"
00028 #include "BulletCollision/BroadphaseCollision/btDbvt.h"
00029 
00030 //#ifdef BT_USE_DOUBLE_PRECISION
00031 //#define btRigidBodyData   btRigidBodyDoubleData
00032 //#define btRigidBodyDataName   "btRigidBodyDoubleData"
00033 //#else
00034 #define btSoftBodyData  btSoftBodyFloatData
00035 #define btSoftBodyDataName  "btSoftBodyFloatData"
00036 //#endif //BT_USE_DOUBLE_PRECISION
00037 
00038 class btBroadphaseInterface;
00039 class btDispatcher;
00040 class btSoftBodySolver;
00041 
00042 /* btSoftBodyWorldInfo  */ 
00043 struct  btSoftBodyWorldInfo
00044 {
00045     btScalar                air_density;
00046     btScalar                water_density;
00047     btScalar                water_offset;
00048     btVector3               water_normal;
00049     btBroadphaseInterface*  m_broadphase;
00050     btDispatcher*   m_dispatcher;
00051     btVector3               m_gravity;
00052     btSparseSdf<3>          m_sparsesdf;
00053 
00054     btSoftBodyWorldInfo()
00055         :air_density((btScalar)1.2),
00056         water_density(0),
00057         water_offset(0),
00058         water_normal(0,0,0),
00059         m_broadphase(0),
00060         m_dispatcher(0),
00061         m_gravity(0,-10,0)
00062     {
00063     }
00064 };  
00065 
00066 
00069 class   btSoftBody : public btCollisionObject
00070 {
00071 public:
00072     btAlignedObjectArray<class btCollisionObject*> m_collisionDisabledObjects;
00073 
00074     // The solver object that handles this soft body
00075     btSoftBodySolver *m_softBodySolver;
00076 
00077     //
00078     // Enumerations
00079     //
00080 
00082     struct eAeroModel { enum _ {
00083         V_Point,    
00084         V_TwoSided, 
00085         V_OneSided, 
00086         F_TwoSided, 
00087         F_OneSided, 
00088         END
00089     };};
00090 
00092     struct  eVSolver { enum _ {
00093         Linear,     
00094         END
00095     };};
00096 
00098     struct  ePSolver { enum _ {
00099         Linear,     
00100         Anchors,    
00101         RContacts,  
00102         SContacts,  
00103         END
00104     };};
00105 
00107     struct  eSolverPresets { enum _ {
00108         Positions,
00109         Velocities,
00110         Default =   Positions,
00111         END
00112     };};
00113 
00115     struct  eFeature { enum _ {
00116         None,
00117         Node,
00118         Link,
00119         Face,
00120         END
00121     };};
00122 
00123     typedef btAlignedObjectArray<eVSolver::_>   tVSolverArray;
00124     typedef btAlignedObjectArray<ePSolver::_>   tPSolverArray;
00125 
00126     //
00127     // Flags
00128     //
00129 
00131     struct fCollision { enum _ {
00132         RVSmask =   0x000f, 
00133         SDF_RS  =   0x0001, 
00134         CL_RS   =   0x0002, 
00135 
00136         SVSmask =   0x0030, 
00137         VF_SS   =   0x0010, 
00138         CL_SS   =   0x0020, 
00139         CL_SELF =   0x0040, 
00140         /* presets  */ 
00141         Default =   SDF_RS,
00142         END
00143     };};
00144 
00146     struct fMaterial { enum _ {
00147         DebugDraw   =   0x0001, 
00148         /* presets  */ 
00149         Default     =   DebugDraw,
00150         END
00151     };};
00152 
00153     //
00154     // API Types
00155     //
00156 
00157     /* sRayCast     */ 
00158     struct sRayCast
00159     {
00160         btSoftBody* body;       
00161         eFeature::_ feature;    
00162         int         index;      
00163         btScalar    fraction;       
00164     };
00165 
00166     /* ImplicitFn   */ 
00167     struct  ImplicitFn
00168     {
00169         virtual btScalar    Eval(const btVector3& x)=0;
00170     };
00171 
00172     //
00173     // Internal types
00174     //
00175 
00176     typedef btAlignedObjectArray<btScalar>  tScalarArray;
00177     typedef btAlignedObjectArray<btVector3> tVector3Array;
00178 
00179     /* sCti is Softbody contact info    */ 
00180     struct  sCti
00181     {
00182         btCollisionObject*  m_colObj;       /* Rigid body           */ 
00183         btVector3       m_normal;   /* Outward normal       */ 
00184         btScalar        m_offset;   /* Offset from origin   */ 
00185     };  
00186 
00187     /* sMedium      */ 
00188     struct  sMedium
00189     {
00190         btVector3       m_velocity; /* Velocity             */ 
00191         btScalar        m_pressure; /* Pressure             */ 
00192         btScalar        m_density;  /* Density              */ 
00193     };
00194 
00195     /* Base type    */ 
00196     struct  Element
00197     {
00198         void*           m_tag;          // User data
00199         Element() : m_tag(0) {}
00200     };
00201     /* Material     */ 
00202     struct  Material : Element
00203     {
00204         btScalar                m_kLST;         // Linear stiffness coefficient [0,1]
00205         btScalar                m_kAST;         // Area/Angular stiffness coefficient [0,1]
00206         btScalar                m_kVST;         // Volume stiffness coefficient [0,1]
00207         int                     m_flags;        // Flags
00208     };
00209 
00210     /* Feature      */ 
00211     struct  Feature : Element
00212     {
00213         Material*               m_material;     // Material
00214     };
00215     /* Node         */ 
00216     struct  Node : Feature
00217     {
00218         btVector3               m_x;            // Position
00219         btVector3               m_q;            // Previous step position
00220         btVector3               m_v;            // Velocity
00221         btVector3               m_f;            // Force accumulator
00222         btVector3               m_n;            // Normal
00223         btScalar                m_im;           // 1/mass
00224         btScalar                m_area;         // Area
00225         btDbvtNode*             m_leaf;         // Leaf data
00226         int                     m_battach:1;    // Attached
00227     };
00228     /* Link         */ 
00229     struct  Link : Feature
00230     {
00231         Node*                   m_n[2];         // Node pointers
00232         btScalar                m_rl;           // Rest length      
00233         int                     m_bbending:1;   // Bending link
00234         btScalar                m_c0;           // (ima+imb)*kLST
00235         btScalar                m_c1;           // rl^2
00236         btScalar                m_c2;           // |gradient|^2/c0
00237         btVector3               m_c3;           // gradient
00238     };
00239     /* Face         */ 
00240     struct  Face : Feature
00241     {
00242         Node*                   m_n[3];         // Node pointers
00243         btVector3               m_normal;       // Normal
00244         btScalar                m_ra;           // Rest area
00245         btDbvtNode*             m_leaf;         // Leaf data
00246     };
00247     /* Tetra        */ 
00248     struct  Tetra : Feature
00249     {
00250         Node*                   m_n[4];         // Node pointers        
00251         btScalar                m_rv;           // Rest volume
00252         btDbvtNode*             m_leaf;         // Leaf data
00253         btVector3               m_c0[4];        // gradients
00254         btScalar                m_c1;           // (4*kVST)/(im0+im1+im2+im3)
00255         btScalar                m_c2;           // m_c1/sum(|g0..3|^2)
00256     };
00257     /* RContact     */ 
00258     struct  RContact
00259     {
00260         sCti        m_cti;          // Contact infos
00261         Node*                   m_node;         // Owner node
00262         btMatrix3x3             m_c0;           // Impulse matrix
00263         btVector3               m_c1;           // Relative anchor
00264         btScalar                m_c2;           // ima*dt
00265         btScalar                m_c3;           // Friction
00266         btScalar                m_c4;           // Hardness
00267     };
00268     /* SContact     */ 
00269     struct  SContact
00270     {
00271         Node*                   m_node;         // Node
00272         Face*                   m_face;         // Face
00273         btVector3               m_weights;      // Weigths
00274         btVector3               m_normal;       // Normal
00275         btScalar                m_margin;       // Margin
00276         btScalar                m_friction;     // Friction
00277         btScalar                m_cfm[2];       // Constraint force mixing
00278     };
00279     /* Anchor       */ 
00280     struct  Anchor
00281     {
00282         Node*                   m_node;         // Node pointer
00283         btVector3               m_local;        // Anchor position in body space
00284         btRigidBody*            m_body;         // Body
00285         btMatrix3x3             m_c0;           // Impulse matrix
00286         btVector3               m_c1;           // Relative anchor
00287         btScalar                m_c2;           // ima*dt
00288     };
00289     /* Note         */ 
00290     struct  Note : Element
00291     {
00292         const char*             m_text;         // Text
00293         btVector3               m_offset;       // Offset
00294         int                     m_rank;         // Rank
00295         Node*                   m_nodes[4];     // Nodes
00296         btScalar                m_coords[4];    // Coordinates
00297     };  
00298     /* Pose         */ 
00299     struct  Pose
00300     {
00301         bool                    m_bvolume;      // Is valid
00302         bool                    m_bframe;       // Is frame
00303         btScalar                m_volume;       // Rest volume
00304         tVector3Array           m_pos;          // Reference positions
00305         tScalarArray            m_wgh;          // Weights
00306         btVector3               m_com;          // COM
00307         btMatrix3x3             m_rot;          // Rotation
00308         btMatrix3x3             m_scl;          // Scale
00309         btMatrix3x3             m_aqq;          // Base scaling
00310     };
00311     /* Cluster      */ 
00312     struct  Cluster
00313     {
00314         tScalarArray                m_masses;
00315         btAlignedObjectArray<Node*> m_nodes;        
00316         tVector3Array               m_framerefs;
00317         btTransform                 m_framexform;
00318         btScalar                    m_idmass;
00319         btScalar                    m_imass;
00320         btMatrix3x3                 m_locii;
00321         btMatrix3x3                 m_invwi;
00322         btVector3                   m_com;
00323         btVector3                   m_vimpulses[2];
00324         btVector3                   m_dimpulses[2];
00325         int                         m_nvimpulses;
00326         int                         m_ndimpulses;
00327         btVector3                   m_lv;
00328         btVector3                   m_av;
00329         btDbvtNode*                 m_leaf;
00330         btScalar                    m_ndamping; /* Node damping     */ 
00331         btScalar                    m_ldamping; /* Linear damping   */ 
00332         btScalar                    m_adamping; /* Angular damping  */ 
00333         btScalar                    m_matching;
00334         btScalar                    m_maxSelfCollisionImpulse;
00335         btScalar                    m_selfCollisionImpulseFactor;
00336         bool                        m_containsAnchor;
00337         bool                        m_collide;
00338         int                         m_clusterIndex;
00339         Cluster() : m_leaf(0),m_ndamping(0),m_ldamping(0),m_adamping(0),m_matching(0) 
00340         ,m_maxSelfCollisionImpulse(100.f),
00341         m_selfCollisionImpulseFactor(0.01f),
00342         m_containsAnchor(false)
00343         {}
00344     };
00345     /* Impulse      */ 
00346     struct  Impulse
00347     {
00348         btVector3                   m_velocity;
00349         btVector3                   m_drift;
00350         int                         m_asVelocity:1;
00351         int                         m_asDrift:1;
00352         Impulse() : m_velocity(0,0,0),m_drift(0,0,0),m_asVelocity(0),m_asDrift(0)   {}
00353         Impulse                     operator -() const
00354         {
00355             Impulse i=*this;
00356             i.m_velocity=-i.m_velocity;
00357             i.m_drift=-i.m_drift;
00358             return(i);
00359         }
00360         Impulse                     operator*(btScalar x) const
00361         {
00362             Impulse i=*this;
00363             i.m_velocity*=x;
00364             i.m_drift*=x;
00365             return(i);
00366         }
00367     };
00368     /* Body         */ 
00369     struct  Body
00370     {
00371         Cluster*            m_soft;
00372         btRigidBody*        m_rigid;
00373         btCollisionObject*  m_collisionObject;
00374 
00375         Body() : m_soft(0),m_rigid(0),m_collisionObject(0)              {}
00376         Body(Cluster* p) : m_soft(p),m_rigid(0),m_collisionObject(0)    {}
00377         Body(btCollisionObject* colObj) : m_soft(0),m_collisionObject(colObj)
00378         {
00379             m_rigid = btRigidBody::upcast(m_collisionObject);
00380         }
00381 
00382         void                        activate() const
00383         {
00384             if(m_rigid) 
00385                 m_rigid->activate();
00386             if (m_collisionObject)
00387                 m_collisionObject->activate();
00388 
00389         }
00390         const btMatrix3x3&          invWorldInertia() const
00391         {
00392             static const btMatrix3x3    iwi(0,0,0,0,0,0,0,0,0);
00393             if(m_rigid) return(m_rigid->getInvInertiaTensorWorld());
00394             if(m_soft)  return(m_soft->m_invwi);
00395             return(iwi);
00396         }
00397         btScalar                    invMass() const
00398         {
00399             if(m_rigid) return(m_rigid->getInvMass());
00400             if(m_soft)  return(m_soft->m_imass);
00401             return(0);
00402         }
00403         const btTransform&          xform() const
00404         {
00405             static const btTransform    identity=btTransform::getIdentity();        
00406             if(m_collisionObject) return(m_collisionObject->getWorldTransform());
00407             if(m_soft)  return(m_soft->m_framexform);
00408             return(identity);
00409         }
00410         btVector3                   linearVelocity() const
00411         {
00412             if(m_rigid) return(m_rigid->getLinearVelocity());
00413             if(m_soft)  return(m_soft->m_lv);
00414             return(btVector3(0,0,0));
00415         }
00416         btVector3                   angularVelocity(const btVector3& rpos) const
00417         {           
00418             if(m_rigid) return(btCross(m_rigid->getAngularVelocity(),rpos));
00419             if(m_soft)  return(btCross(m_soft->m_av,rpos));
00420             return(btVector3(0,0,0));
00421         }
00422         btVector3                   angularVelocity() const
00423         {           
00424             if(m_rigid) return(m_rigid->getAngularVelocity());
00425             if(m_soft)  return(m_soft->m_av);
00426             return(btVector3(0,0,0));
00427         }
00428         btVector3                   velocity(const btVector3& rpos) const
00429         {
00430             return(linearVelocity()+angularVelocity(rpos));
00431         }
00432         void                        applyVImpulse(const btVector3& impulse,const btVector3& rpos) const
00433         {
00434             if(m_rigid) m_rigid->applyImpulse(impulse,rpos);
00435             if(m_soft)  btSoftBody::clusterVImpulse(m_soft,rpos,impulse);
00436         }
00437         void                        applyDImpulse(const btVector3& impulse,const btVector3& rpos) const
00438         {
00439             if(m_rigid) m_rigid->applyImpulse(impulse,rpos);
00440             if(m_soft)  btSoftBody::clusterDImpulse(m_soft,rpos,impulse);
00441         }       
00442         void                        applyImpulse(const Impulse& impulse,const btVector3& rpos) const
00443         {
00444             if(impulse.m_asVelocity)    
00445             {
00446 //              printf("impulse.m_velocity = %f,%f,%f\n",impulse.m_velocity.getX(),impulse.m_velocity.getY(),impulse.m_velocity.getZ());
00447                 applyVImpulse(impulse.m_velocity,rpos);
00448             }
00449             if(impulse.m_asDrift)       
00450             {
00451 //              printf("impulse.m_drift = %f,%f,%f\n",impulse.m_drift.getX(),impulse.m_drift.getY(),impulse.m_drift.getZ());
00452                 applyDImpulse(impulse.m_drift,rpos);
00453             }
00454         }
00455         void                        applyVAImpulse(const btVector3& impulse) const
00456         {
00457             if(m_rigid) m_rigid->applyTorqueImpulse(impulse);
00458             if(m_soft)  btSoftBody::clusterVAImpulse(m_soft,impulse);
00459         }
00460         void                        applyDAImpulse(const btVector3& impulse) const
00461         {
00462             if(m_rigid) m_rigid->applyTorqueImpulse(impulse);
00463             if(m_soft)  btSoftBody::clusterDAImpulse(m_soft,impulse);
00464         }
00465         void                        applyAImpulse(const Impulse& impulse) const
00466         {
00467             if(impulse.m_asVelocity)    applyVAImpulse(impulse.m_velocity);
00468             if(impulse.m_asDrift)       applyDAImpulse(impulse.m_drift);
00469         }
00470         void                        applyDCImpulse(const btVector3& impulse) const
00471         {
00472             if(m_rigid) m_rigid->applyCentralImpulse(impulse);
00473             if(m_soft)  btSoftBody::clusterDCImpulse(m_soft,impulse);
00474         }
00475     };
00476     /* Joint        */ 
00477     struct  Joint
00478     {
00479         struct eType { enum _ {
00480             Linear=0,
00481             Angular,
00482             Contact
00483         };};
00484         struct Specs
00485         {
00486             Specs() : erp(1),cfm(1),split(1) {}
00487             btScalar    erp;
00488             btScalar    cfm;
00489             btScalar    split;
00490         };
00491         Body                        m_bodies[2];
00492         btVector3                   m_refs[2];
00493         btScalar                    m_cfm;
00494         btScalar                    m_erp;
00495         btScalar                    m_split;
00496         btVector3                   m_drift;
00497         btVector3                   m_sdrift;
00498         btMatrix3x3                 m_massmatrix;
00499         bool                        m_delete;
00500         virtual                     ~Joint() {}
00501         Joint() : m_delete(false) {}
00502         virtual void                Prepare(btScalar dt,int iterations);
00503         virtual void                Solve(btScalar dt,btScalar sor)=0;
00504         virtual void                Terminate(btScalar dt)=0;
00505         virtual eType::_            Type() const=0;
00506     };
00507     /* LJoint       */ 
00508     struct  LJoint : Joint
00509     {
00510         struct Specs : Joint::Specs
00511         {
00512             btVector3   position;
00513         };      
00514         btVector3                   m_rpos[2];
00515         void                        Prepare(btScalar dt,int iterations);
00516         void                        Solve(btScalar dt,btScalar sor);
00517         void                        Terminate(btScalar dt);
00518         eType::_                    Type() const { return(eType::Linear); }
00519     };
00520     /* AJoint       */ 
00521     struct  AJoint : Joint
00522     {
00523         struct IControl
00524         {
00525             virtual void            Prepare(AJoint*)                {}
00526             virtual btScalar        Speed(AJoint*,btScalar current) { return(current); }
00527             static IControl*        Default()                       { static IControl def;return(&def); }
00528         };
00529         struct Specs : Joint::Specs
00530         {
00531             Specs() : icontrol(IControl::Default()) {}
00532             btVector3   axis;
00533             IControl*   icontrol;
00534         };      
00535         btVector3                   m_axis[2];
00536         IControl*                   m_icontrol;
00537         void                        Prepare(btScalar dt,int iterations);
00538         void                        Solve(btScalar dt,btScalar sor);
00539         void                        Terminate(btScalar dt);
00540         eType::_                    Type() const { return(eType::Angular); }
00541     };
00542     /* CJoint       */ 
00543     struct  CJoint : Joint
00544     {       
00545         int                         m_life;
00546         int                         m_maxlife;
00547         btVector3                   m_rpos[2];
00548         btVector3                   m_normal;
00549         btScalar                    m_friction;
00550         void                        Prepare(btScalar dt,int iterations);
00551         void                        Solve(btScalar dt,btScalar sor);
00552         void                        Terminate(btScalar dt);
00553         eType::_                    Type() const { return(eType::Contact); }
00554     };
00555     /* Config       */ 
00556     struct  Config
00557     {
00558         eAeroModel::_           aeromodel;      // Aerodynamic model (default: V_Point)
00559         btScalar                kVCF;           // Velocities correction factor (Baumgarte)
00560         btScalar                kDP;            // Damping coefficient [0,1]
00561         btScalar                kDG;            // Drag coefficient [0,+inf]
00562         btScalar                kLF;            // Lift coefficient [0,+inf]
00563         btScalar                kPR;            // Pressure coefficient [-inf,+inf]
00564         btScalar                kVC;            // Volume conversation coefficient [0,+inf]
00565         btScalar                kDF;            // Dynamic friction coefficient [0,1]
00566         btScalar                kMT;            // Pose matching coefficient [0,1]      
00567         btScalar                kCHR;           // Rigid contacts hardness [0,1]
00568         btScalar                kKHR;           // Kinetic contacts hardness [0,1]
00569         btScalar                kSHR;           // Soft contacts hardness [0,1]
00570         btScalar                kAHR;           // Anchors hardness [0,1]
00571         btScalar                kSRHR_CL;       // Soft vs rigid hardness [0,1] (cluster only)
00572         btScalar                kSKHR_CL;       // Soft vs kinetic hardness [0,1] (cluster only)
00573         btScalar                kSSHR_CL;       // Soft vs soft hardness [0,1] (cluster only)
00574         btScalar                kSR_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
00575         btScalar                kSK_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
00576         btScalar                kSS_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
00577         btScalar                maxvolume;      // Maximum volume ratio for pose
00578         btScalar                timescale;      // Time scale
00579         int                     viterations;    // Velocities solver iterations
00580         int                     piterations;    // Positions solver iterations
00581         int                     diterations;    // Drift solver iterations
00582         int                     citerations;    // Cluster solver iterations
00583         int                     collisions;     // Collisions flags
00584         tVSolverArray           m_vsequence;    // Velocity solvers sequence
00585         tPSolverArray           m_psequence;    // Position solvers sequence
00586         tPSolverArray           m_dsequence;    // Drift solvers sequence
00587     };
00588     /* SolverState  */ 
00589     struct  SolverState
00590     {
00591         btScalar                sdt;            // dt*timescale
00592         btScalar                isdt;           // 1/sdt
00593         btScalar                velmrg;         // velocity margin
00594         btScalar                radmrg;         // radial margin
00595         btScalar                updmrg;         // Update margin
00596     };  
00598     struct  RayFromToCaster : btDbvt::ICollide
00599     {
00600         btVector3           m_rayFrom;
00601         btVector3           m_rayTo;
00602         btVector3           m_rayNormalizedDirection;
00603         btScalar            m_mint;
00604         Face*               m_face;
00605         int                 m_tests;
00606         RayFromToCaster(const btVector3& rayFrom,const btVector3& rayTo,btScalar mxt);
00607         void                    Process(const btDbvtNode* leaf);
00608 
00609         static inline btScalar  rayFromToTriangle(const btVector3& rayFrom,
00610             const btVector3& rayTo,
00611             const btVector3& rayNormalizedDirection,
00612             const btVector3& a,
00613             const btVector3& b,
00614             const btVector3& c,
00615             btScalar maxt=SIMD_INFINITY);
00616     };
00617 
00618     //
00619     // Typedefs
00620     //
00621 
00622     typedef void                                (*psolver_t)(btSoftBody*,btScalar,btScalar);
00623     typedef void                                (*vsolver_t)(btSoftBody*,btScalar);
00624     typedef btAlignedObjectArray<Cluster*>      tClusterArray;
00625     typedef btAlignedObjectArray<Note>          tNoteArray;
00626     typedef btAlignedObjectArray<Node>          tNodeArray;
00627     typedef btAlignedObjectArray<btDbvtNode*>   tLeafArray;
00628     typedef btAlignedObjectArray<Link>          tLinkArray;
00629     typedef btAlignedObjectArray<Face>          tFaceArray;
00630     typedef btAlignedObjectArray<Tetra>         tTetraArray;
00631     typedef btAlignedObjectArray<Anchor>        tAnchorArray;
00632     typedef btAlignedObjectArray<RContact>      tRContactArray;
00633     typedef btAlignedObjectArray<SContact>      tSContactArray;
00634     typedef btAlignedObjectArray<Material*>     tMaterialArray;
00635     typedef btAlignedObjectArray<Joint*>        tJointArray;
00636     typedef btAlignedObjectArray<btSoftBody*>   tSoftBodyArray; 
00637 
00638     //
00639     // Fields
00640     //
00641 
00642     Config                  m_cfg;          // Configuration
00643     SolverState             m_sst;          // Solver state
00644     Pose                    m_pose;         // Pose
00645     void*                   m_tag;          // User data
00646     btSoftBodyWorldInfo*    m_worldInfo;    // World info
00647     tNoteArray              m_notes;        // Notes
00648     tNodeArray              m_nodes;        // Nodes
00649     tLinkArray              m_links;        // Links
00650     tFaceArray              m_faces;        // Faces
00651     tTetraArray             m_tetras;       // Tetras
00652     tAnchorArray            m_anchors;      // Anchors
00653     tRContactArray          m_rcontacts;    // Rigid contacts
00654     tSContactArray          m_scontacts;    // Soft contacts
00655     tJointArray             m_joints;       // Joints
00656     tMaterialArray          m_materials;    // Materials
00657     btScalar                m_timeacc;      // Time accumulator
00658     btVector3               m_bounds[2];    // Spatial bounds   
00659     bool                    m_bUpdateRtCst; // Update runtime constants
00660     btDbvt                  m_ndbvt;        // Nodes tree
00661     btDbvt                  m_fdbvt;        // Faces tree
00662     btDbvt                  m_cdbvt;        // Clusters tree
00663     tClusterArray           m_clusters;     // Clusters
00664 
00665     btAlignedObjectArray<bool>m_clusterConnectivity;//cluster connectivity, for self-collision
00666 
00667     btTransform         m_initialWorldTransform;
00668 
00669     btVector3           m_windVelocity;
00670     //
00671     // Api
00672     //
00673 
00674     /* ctor                                                                 */ 
00675     btSoftBody( btSoftBodyWorldInfo* worldInfo,int node_count,      const btVector3* x,     const btScalar* m);
00676 
00677     /* ctor                                                                 */ 
00678     btSoftBody( btSoftBodyWorldInfo* worldInfo);
00679 
00680     void    initDefaults();
00681 
00682     /* dtor                                                                 */ 
00683     virtual ~btSoftBody();
00684     /* Check for existing link                                              */ 
00685 
00686     btAlignedObjectArray<int>   m_userIndexMapping;
00687 
00688     btSoftBodyWorldInfo*    getWorldInfo()
00689     {
00690         return m_worldInfo;
00691     }
00692 
00694     virtual void    setCollisionShape(btCollisionShape* collisionShape)
00695     {
00696         
00697     }
00698 
00699     bool                checkLink(  int node0,
00700         int node1) const;
00701     bool                checkLink(  const Node* node0,
00702         const Node* node1) const;
00703     /* Check for existring face                                             */ 
00704     bool                checkFace(  int node0,
00705         int node1,
00706         int node2) const;
00707     /* Append material                                                      */ 
00708     Material*           appendMaterial();
00709     /* Append note                                                          */ 
00710     void                appendNote( const char* text,
00711         const btVector3& o,
00712         const btVector4& c=btVector4(1,0,0,0),
00713         Node* n0=0,
00714         Node* n1=0,
00715         Node* n2=0,
00716         Node* n3=0);
00717     void                appendNote( const char* text,
00718         const btVector3& o,
00719         Node* feature);
00720     void                appendNote( const char* text,
00721         const btVector3& o,
00722         Link* feature);
00723     void                appendNote( const char* text,
00724         const btVector3& o,
00725         Face* feature);
00726     /* Append node                                                          */ 
00727     void                appendNode( const btVector3& x,btScalar m);
00728     /* Append link                                                          */ 
00729     void                appendLink(int model=-1,Material* mat=0);
00730     void                appendLink( int node0,
00731         int node1,
00732         Material* mat=0,
00733         bool bcheckexist=false);
00734     void                appendLink( Node* node0,
00735         Node* node1,
00736         Material* mat=0,
00737         bool bcheckexist=false);
00738     /* Append face                                                          */ 
00739     void                appendFace(int model=-1,Material* mat=0);
00740     void                appendFace( int node0,
00741         int node1,
00742         int node2,
00743         Material* mat=0);
00744     void            appendTetra(int model,Material* mat);
00745     //
00746     void            appendTetra(int node0,
00747                                         int node1,
00748                                         int node2,
00749                                         int node3,
00750                                         Material* mat=0);
00751 
00752 
00753     /* Append anchor                                                        */ 
00754     void                appendAnchor(   int node,
00755         btRigidBody* body, bool disableCollisionBetweenLinkedBodies=false);
00756     void            appendAnchor(int node,btRigidBody* body, const btVector3& localPivot,bool disableCollisionBetweenLinkedBodies=false);
00757     /* Append linear joint                                                  */ 
00758     void                appendLinearJoint(const LJoint::Specs& specs,Cluster* body0,Body body1);
00759     void                appendLinearJoint(const LJoint::Specs& specs,Body body=Body());
00760     void                appendLinearJoint(const LJoint::Specs& specs,btSoftBody* body);
00761     /* Append linear joint                                                  */ 
00762     void                appendAngularJoint(const AJoint::Specs& specs,Cluster* body0,Body body1);
00763     void                appendAngularJoint(const AJoint::Specs& specs,Body body=Body());
00764     void                appendAngularJoint(const AJoint::Specs& specs,btSoftBody* body);
00765     /* Add force (or gravity) to the entire body                            */ 
00766     void                addForce(       const btVector3& force);
00767     /* Add force (or gravity) to a node of the body                         */ 
00768     void                addForce(       const btVector3& force,
00769         int node);
00770     /* Add velocity to the entire body                                      */ 
00771     void                addVelocity(    const btVector3& velocity);
00772 
00773     /* Set velocity for the entire body                                     */ 
00774     void                setVelocity(    const btVector3& velocity);
00775 
00776     /* Add velocity to a node of the body                                   */ 
00777     void                addVelocity(    const btVector3& velocity,
00778         int node);
00779     /* Set mass                                                             */ 
00780     void                setMass(        int node,
00781         btScalar mass);
00782     /* Get mass                                                             */ 
00783     btScalar            getMass(        int node) const;
00784     /* Get total mass                                                       */ 
00785     btScalar            getTotalMass() const;
00786     /* Set total mass (weighted by previous masses)                         */ 
00787     void                setTotalMass(   btScalar mass,
00788         bool fromfaces=false);
00789     /* Set total density                                                    */ 
00790     void                setTotalDensity(btScalar density);
00791     /* Set volume mass (using tetrahedrons)                                 */
00792     void                setVolumeMass(      btScalar mass);
00793     /* Set volume density (using tetrahedrons)                              */
00794     void                setVolumeDensity(   btScalar density);
00795     /* Transform                                                            */ 
00796     void                transform(      const btTransform& trs);
00797     /* Translate                                                            */ 
00798     void                translate(      const btVector3& trs);
00799     /* Rotate                                                           */ 
00800     void                rotate( const btQuaternion& rot);
00801     /* Scale                                                                */ 
00802     void                scale(  const btVector3& scl);
00803     /* Set current state as pose                                            */ 
00804     void                setPose(        bool bvolume,
00805         bool bframe);
00806     /* Return the volume                                                    */ 
00807     btScalar            getVolume() const;
00808     /* Cluster count                                                        */ 
00809     int                 clusterCount() const;
00810     /* Cluster center of mass                                               */ 
00811     static btVector3    clusterCom(const Cluster* cluster);
00812     btVector3           clusterCom(int cluster) const;
00813     /* Cluster velocity at rpos                                             */ 
00814     static btVector3    clusterVelocity(const Cluster* cluster,const btVector3& rpos);
00815     /* Cluster impulse                                                      */ 
00816     static void         clusterVImpulse(Cluster* cluster,const btVector3& rpos,const btVector3& impulse);
00817     static void         clusterDImpulse(Cluster* cluster,const btVector3& rpos,const btVector3& impulse);
00818     static void         clusterImpulse(Cluster* cluster,const btVector3& rpos,const Impulse& impulse);
00819     static void         clusterVAImpulse(Cluster* cluster,const btVector3& impulse);
00820     static void         clusterDAImpulse(Cluster* cluster,const btVector3& impulse);
00821     static void         clusterAImpulse(Cluster* cluster,const Impulse& impulse);
00822     static void         clusterDCImpulse(Cluster* cluster,const btVector3& impulse);
00823     /* Generate bending constraints based on distance in the adjency graph  */ 
00824     int                 generateBendingConstraints( int distance,
00825         Material* mat=0);
00826     /* Randomize constraints to reduce solver bias                          */ 
00827     void                randomizeConstraints();
00828     /* Release clusters                                                     */ 
00829     void                releaseCluster(int index);
00830     void                releaseClusters();
00831     /* Generate clusters (K-mean)                                           */ 
00834     int                 generateClusters(int k,int maxiterations=8192);
00835     /* Refine                                                               */ 
00836     void                refine(ImplicitFn* ifn,btScalar accurary,bool cut);
00837     /* CutLink                                                              */ 
00838     bool                cutLink(int node0,int node1,btScalar position);
00839     bool                cutLink(const Node* node0,const Node* node1,btScalar position);
00840 
00842     bool                rayTest(const btVector3& rayFrom,
00843         const btVector3& rayTo,
00844         sRayCast& results);
00845     /* Solver presets                                                       */ 
00846     void                setSolver(eSolverPresets::_ preset);
00847     /* predictMotion                                                        */ 
00848     void                predictMotion(btScalar dt);
00849     /* solveConstraints                                                     */ 
00850     void                solveConstraints();
00851     /* staticSolve                                                          */ 
00852     void                staticSolve(int iterations);
00853     /* solveCommonConstraints                                               */ 
00854     static void         solveCommonConstraints(btSoftBody** bodies,int count,int iterations);
00855     /* solveClusters                                                        */ 
00856     static void         solveClusters(const btAlignedObjectArray<btSoftBody*>& bodies);
00857     /* integrateMotion                                                      */ 
00858     void                integrateMotion();
00859     /* defaultCollisionHandlers                                             */ 
00860     void                defaultCollisionHandler(btCollisionObject* pco);
00861     void                defaultCollisionHandler(btSoftBody* psb);
00862 
00863 
00864 
00865     //
00866     // Functionality to deal with new accelerated solvers.
00867     //
00868 
00872     void setWindVelocity( const btVector3 &velocity );
00873 
00874 
00878     const btVector3& getWindVelocity();
00879 
00880     //
00881     // Set the solver that handles this soft body
00882     // Should not be allowed to get out of sync with reality
00883     // Currently called internally on addition to the world
00884     void setSoftBodySolver( btSoftBodySolver *softBodySolver )
00885     {
00886         m_softBodySolver = softBodySolver;
00887     }
00888 
00889     //
00890     // Return the solver that handles this soft body
00891     // 
00892     btSoftBodySolver *getSoftBodySolver()
00893     {
00894         return m_softBodySolver;
00895     }
00896 
00897     //
00898     // Return the solver that handles this soft body
00899     // 
00900     btSoftBodySolver *getSoftBodySolver() const
00901     {
00902         return m_softBodySolver;
00903     }
00904 
00905 
00906     //
00907     // Cast
00908     //
00909 
00910     static const btSoftBody*    upcast(const btCollisionObject* colObj)
00911     {
00912         if (colObj->getInternalType()==CO_SOFT_BODY)
00913             return (const btSoftBody*)colObj;
00914         return 0;
00915     }
00916     static btSoftBody*          upcast(btCollisionObject* colObj)
00917     {
00918         if (colObj->getInternalType()==CO_SOFT_BODY)
00919             return (btSoftBody*)colObj;
00920         return 0;
00921     }
00922 
00923     //
00924     // ::btCollisionObject
00925     //
00926 
00927     virtual void getAabb(btVector3& aabbMin,btVector3& aabbMax) const
00928     {
00929         aabbMin = m_bounds[0];
00930         aabbMax = m_bounds[1];
00931     }
00932     //
00933     // Private
00934     //
00935     void                pointersToIndices();
00936     void                indicesToPointers(const int* map=0);
00937 
00938     int                 rayTest(const btVector3& rayFrom,const btVector3& rayTo,
00939         btScalar& mint,eFeature::_& feature,int& index,bool bcountonly) const;
00940     void                initializeFaceTree();
00941     btVector3           evaluateCom() const;
00942     bool                checkContact(btCollisionObject* colObj,const btVector3& x,btScalar margin,btSoftBody::sCti& cti) const;
00943     void                updateNormals();
00944     void                updateBounds();
00945     void                updatePose();
00946     void                updateConstants();
00947     void                initializeClusters();
00948     void                updateClusters();
00949     void                cleanupClusters();
00950     void                prepareClusters(int iterations);
00951     void                solveClusters(btScalar sor);
00952     void                applyClusters(bool drift);
00953     void                dampClusters();
00954     void                applyForces();  
00955     static void         PSolve_Anchors(btSoftBody* psb,btScalar kst,btScalar ti);
00956     static void         PSolve_RContacts(btSoftBody* psb,btScalar kst,btScalar ti);
00957     static void         PSolve_SContacts(btSoftBody* psb,btScalar,btScalar ti);
00958     static void         PSolve_Links(btSoftBody* psb,btScalar kst,btScalar ti);
00959     static void         VSolve_Links(btSoftBody* psb,btScalar kst);
00960     static psolver_t    getSolver(ePSolver::_ solver);
00961     static vsolver_t    getSolver(eVSolver::_ solver);
00962 
00963 
00964     virtual int calculateSerializeBufferSize()  const;
00965 
00967     virtual const char* serialize(void* dataBuffer,  class btSerializer* serializer) const;
00968 
00969     //virtual void serializeSingleObject(class btSerializer* serializer) const;
00970 
00971 
00972 };
00973 
00974 
00975 
00976 
00977 #endif //_BT_SOFT_BODY_H