Blender  V2.59
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                 Material (){}
00205                 btScalar                                m_kLST;                 // Linear stiffness coefficient [0,1]
00206                 btScalar                                m_kAST;                 // Area/Angular stiffness coefficient [0,1]
00207                 btScalar                                m_kVST;                 // Volume stiffness coefficient [0,1]
00208                 int                                             m_flags;                // Flags
00209         };
00210 
00211         /* Feature              */ 
00212         struct  Feature : Element
00213         {
00214                 Material*                               m_material;             // Material
00215         };
00216         /* Node                 */ 
00217         struct  Node : Feature
00218         {
00219                 Node (){}
00220                 btVector3                               m_x;                    // Position
00221                 btVector3                               m_q;                    // Previous step position
00222                 btVector3                               m_v;                    // Velocity
00223                 btVector3                               m_f;                    // Force accumulator
00224                 btVector3                               m_n;                    // Normal
00225                 btScalar                                m_im;                   // 1/mass
00226                 btScalar                                m_area;                 // Area
00227                 btDbvtNode*                             m_leaf;                 // Leaf data
00228                 int                                             m_battach:1;    // Attached
00229         };
00230         /* Link                 */ 
00231         struct  Link : Feature
00232         {
00233                 Link (){}
00234                 Node*                                   m_n[2];                 // Node pointers
00235                 btScalar                                m_rl;                   // Rest length          
00236                 int                                             m_bbending:1;   // Bending link
00237                 btScalar                                m_c0;                   // (ima+imb)*kLST
00238                 btScalar                                m_c1;                   // rl^2
00239                 btScalar                                m_c2;                   // |gradient|^2/c0
00240                 btVector3                               m_c3;                   // gradient
00241         };
00242         /* Face                 */ 
00243         struct  Face : Feature
00244         {
00245                 Face (){} 
00246                 Node*                                   m_n[3];                 // Node pointers
00247                 btVector3                               m_normal;               // Normal
00248                 btScalar                                m_ra;                   // Rest area
00249                 btDbvtNode*                             m_leaf;                 // Leaf data
00250         };
00251         /* Tetra                */ 
00252         struct  Tetra : Feature
00253         {
00254                 Node*                                   m_n[4];                 // Node pointers                
00255                 btScalar                                m_rv;                   // Rest volume
00256                 btDbvtNode*                             m_leaf;                 // Leaf data
00257                 btVector3                               m_c0[4];                // gradients
00258                 btScalar                                m_c1;                   // (4*kVST)/(im0+im1+im2+im3)
00259                 btScalar                                m_c2;                   // m_c1/sum(|g0..3|^2)
00260         };
00261         /* RContact             */ 
00262         struct  RContact
00263         {
00264                 sCti            m_cti;                  // Contact infos
00265                 Node*                                   m_node;                 // Owner node
00266                 btMatrix3x3                             m_c0;                   // Impulse matrix
00267                 btVector3                               m_c1;                   // Relative anchor
00268                 btScalar                                m_c2;                   // ima*dt
00269                 btScalar                                m_c3;                   // Friction
00270                 btScalar                                m_c4;                   // Hardness
00271         };
00272         /* SContact             */ 
00273         struct  SContact
00274         {
00275                 Node*                                   m_node;                 // Node
00276                 Face*                                   m_face;                 // Face
00277                 btVector3                               m_weights;              // Weigths
00278                 btVector3                               m_normal;               // Normal
00279                 btScalar                                m_margin;               // Margin
00280                 btScalar                                m_friction;             // Friction
00281                 btScalar                                m_cfm[2];               // Constraint force mixing
00282         };
00283         /* Anchor               */ 
00284         struct  Anchor
00285         {
00286                 Node*                                   m_node;                 // Node pointer
00287                 btVector3                               m_local;                // Anchor position in body space
00288                 btRigidBody*                    m_body;                 // Body
00289                 btMatrix3x3                             m_c0;                   // Impulse matrix
00290                 btVector3                               m_c1;                   // Relative anchor
00291                 btScalar                                m_c2;                   // ima*dt
00292         };
00293         /* Note                 */ 
00294         struct  Note : Element
00295         {
00296                 Note (){}
00297                 const char*                             m_text;                 // Text
00298                 btVector3                               m_offset;               // Offset
00299                 int                                             m_rank;                 // Rank
00300                 Node*                                   m_nodes[4];             // Nodes
00301                 btScalar                                m_coords[4];    // Coordinates
00302         };      
00303         /* Pose                 */ 
00304         struct  Pose
00305         {
00306                 bool                                    m_bvolume;              // Is valid
00307                 bool                                    m_bframe;               // Is frame
00308                 btScalar                                m_volume;               // Rest volume
00309                 tVector3Array                   m_pos;                  // Reference positions
00310                 tScalarArray                    m_wgh;                  // Weights
00311                 btVector3                               m_com;                  // COM
00312                 btMatrix3x3                             m_rot;                  // Rotation
00313                 btMatrix3x3                             m_scl;                  // Scale
00314                 btMatrix3x3                             m_aqq;                  // Base scaling
00315         };
00316         /* Cluster              */ 
00317         struct  Cluster
00318         {
00319                 tScalarArray                            m_masses;
00320                 btAlignedObjectArray<Node*>     m_nodes;                
00321                 tVector3Array                           m_framerefs;
00322                 btTransform                                     m_framexform;
00323                 btScalar                                        m_idmass;
00324                 btScalar                                        m_imass;
00325                 btMatrix3x3                                     m_locii;
00326                 btMatrix3x3                                     m_invwi;
00327                 btVector3                                       m_com;
00328                 btVector3                                       m_vimpulses[2];
00329                 btVector3                                       m_dimpulses[2];
00330                 int                                                     m_nvimpulses;
00331                 int                                                     m_ndimpulses;
00332                 btVector3                                       m_lv;
00333                 btVector3                                       m_av;
00334                 btDbvtNode*                                     m_leaf;
00335                 btScalar                                        m_ndamping;     /* Node damping         */ 
00336                 btScalar                                        m_ldamping;     /* Linear damping       */ 
00337                 btScalar                                        m_adamping;     /* Angular damping      */ 
00338                 btScalar                                        m_matching;
00339                 btScalar                                        m_maxSelfCollisionImpulse;
00340                 btScalar                                        m_selfCollisionImpulseFactor;
00341                 bool                                            m_containsAnchor;
00342                 bool                                            m_collide;
00343                 int                                                     m_clusterIndex;
00344                 Cluster() : m_leaf(0),m_ndamping(0),m_ldamping(0),m_adamping(0),m_matching(0) 
00345                 ,m_maxSelfCollisionImpulse(100.f),
00346                 m_selfCollisionImpulseFactor(0.01f),
00347                 m_containsAnchor(false)
00348                 {}
00349         };
00350         /* Impulse              */ 
00351         struct  Impulse
00352         {
00353                 btVector3                                       m_velocity;
00354                 btVector3                                       m_drift;
00355                 int                                                     m_asVelocity:1;
00356                 int                                                     m_asDrift:1;
00357                 Impulse() : m_velocity(0,0,0),m_drift(0,0,0),m_asVelocity(0),m_asDrift(0)       {}
00358                 Impulse                                         operator -() const
00359                 {
00360                         Impulse i=*this;
00361                         i.m_velocity=-i.m_velocity;
00362                         i.m_drift=-i.m_drift;
00363                         return(i);
00364                 }
00365                 Impulse                                         operator*(btScalar x) const
00366                 {
00367                         Impulse i=*this;
00368                         i.m_velocity*=x;
00369                         i.m_drift*=x;
00370                         return(i);
00371                 }
00372         };
00373         /* Body                 */ 
00374         struct  Body
00375         {
00376                 Cluster*                        m_soft;
00377                 btRigidBody*            m_rigid;
00378                 btCollisionObject*      m_collisionObject;
00379 
00380                 Body() : m_soft(0),m_rigid(0),m_collisionObject(0)                              {}
00381                 Body(Cluster* p) : m_soft(p),m_rigid(0),m_collisionObject(0)    {}
00382                 Body(btCollisionObject* colObj) : m_soft(0),m_collisionObject(colObj)
00383                 {
00384                         m_rigid = btRigidBody::upcast(m_collisionObject);
00385                 }
00386 
00387                 void                                            activate() const
00388                 {
00389                         if(m_rigid) 
00390                                 m_rigid->activate();
00391                         if (m_collisionObject)
00392                                 m_collisionObject->activate();
00393 
00394                 }
00395                 const btMatrix3x3&                      invWorldInertia() const
00396                 {
00397                         static const btMatrix3x3        iwi(0,0,0,0,0,0,0,0,0);
00398                         if(m_rigid) return(m_rigid->getInvInertiaTensorWorld());
00399                         if(m_soft)      return(m_soft->m_invwi);
00400                         return(iwi);
00401                 }
00402                 btScalar                                        invMass() const
00403                 {
00404                         if(m_rigid) return(m_rigid->getInvMass());
00405                         if(m_soft)      return(m_soft->m_imass);
00406                         return(0);
00407                 }
00408                 const btTransform&                      xform() const
00409                 {
00410                         static const btTransform        identity=btTransform::getIdentity();            
00411                         if(m_collisionObject) return(m_collisionObject->getWorldTransform());
00412                         if(m_soft)      return(m_soft->m_framexform);
00413                         return(identity);
00414                 }
00415                 btVector3                                       linearVelocity() const
00416                 {
00417                         if(m_rigid) return(m_rigid->getLinearVelocity());
00418                         if(m_soft)      return(m_soft->m_lv);
00419                         return(btVector3(0,0,0));
00420                 }
00421                 btVector3                                       angularVelocity(const btVector3& rpos) const
00422                 {                       
00423                         if(m_rigid) return(btCross(m_rigid->getAngularVelocity(),rpos));
00424                         if(m_soft)      return(btCross(m_soft->m_av,rpos));
00425                         return(btVector3(0,0,0));
00426                 }
00427                 btVector3                                       angularVelocity() const
00428                 {                       
00429                         if(m_rigid) return(m_rigid->getAngularVelocity());
00430                         if(m_soft)      return(m_soft->m_av);
00431                         return(btVector3(0,0,0));
00432                 }
00433                 btVector3                                       velocity(const btVector3& rpos) const
00434                 {
00435                         return(linearVelocity()+angularVelocity(rpos));
00436                 }
00437                 void                                            applyVImpulse(const btVector3& impulse,const btVector3& rpos) const
00438                 {
00439                         if(m_rigid)     m_rigid->applyImpulse(impulse,rpos);
00440                         if(m_soft)      btSoftBody::clusterVImpulse(m_soft,rpos,impulse);
00441                 }
00442                 void                                            applyDImpulse(const btVector3& impulse,const btVector3& rpos) const
00443                 {
00444                         if(m_rigid)     m_rigid->applyImpulse(impulse,rpos);
00445                         if(m_soft)      btSoftBody::clusterDImpulse(m_soft,rpos,impulse);
00446                 }               
00447                 void                                            applyImpulse(const Impulse& impulse,const btVector3& rpos) const
00448                 {
00449                         if(impulse.m_asVelocity)        
00450                         {
00451 //                              printf("impulse.m_velocity = %f,%f,%f\n",impulse.m_velocity.getX(),impulse.m_velocity.getY(),impulse.m_velocity.getZ());
00452                                 applyVImpulse(impulse.m_velocity,rpos);
00453                         }
00454                         if(impulse.m_asDrift)           
00455                         {
00456 //                              printf("impulse.m_drift = %f,%f,%f\n",impulse.m_drift.getX(),impulse.m_drift.getY(),impulse.m_drift.getZ());
00457                                 applyDImpulse(impulse.m_drift,rpos);
00458                         }
00459                 }
00460                 void                                            applyVAImpulse(const btVector3& impulse) const
00461                 {
00462                         if(m_rigid)     m_rigid->applyTorqueImpulse(impulse);
00463                         if(m_soft)      btSoftBody::clusterVAImpulse(m_soft,impulse);
00464                 }
00465                 void                                            applyDAImpulse(const btVector3& impulse) const
00466                 {
00467                         if(m_rigid)     m_rigid->applyTorqueImpulse(impulse);
00468                         if(m_soft)      btSoftBody::clusterDAImpulse(m_soft,impulse);
00469                 }
00470                 void                                            applyAImpulse(const Impulse& impulse) const
00471                 {
00472                         if(impulse.m_asVelocity)        applyVAImpulse(impulse.m_velocity);
00473                         if(impulse.m_asDrift)           applyDAImpulse(impulse.m_drift);
00474                 }
00475                 void                                            applyDCImpulse(const btVector3& impulse) const
00476                 {
00477                         if(m_rigid)     m_rigid->applyCentralImpulse(impulse);
00478                         if(m_soft)      btSoftBody::clusterDCImpulse(m_soft,impulse);
00479                 }
00480         };
00481         /* Joint                */ 
00482         struct  Joint
00483         {
00484                 struct eType { enum _ {
00485                         Linear=0,
00486                         Angular,
00487                         Contact
00488                 };};
00489                 struct Specs
00490                 {
00491                         Specs() : erp(1),cfm(1),split(1) {}
00492                         btScalar        erp;
00493                         btScalar        cfm;
00494                         btScalar        split;
00495                 };
00496                 Body                                            m_bodies[2];
00497                 btVector3                                       m_refs[2];
00498                 btScalar                                        m_cfm;
00499                 btScalar                                        m_erp;
00500                 btScalar                                        m_split;
00501                 btVector3                                       m_drift;
00502                 btVector3                                       m_sdrift;
00503                 btMatrix3x3                                     m_massmatrix;
00504                 bool                                            m_delete;
00505                 virtual                                         ~Joint() {}
00506                 Joint() : m_delete(false) {}
00507                 virtual void                            Prepare(btScalar dt,int iterations);
00508                 virtual void                            Solve(btScalar dt,btScalar sor)=0;
00509                 virtual void                            Terminate(btScalar dt)=0;
00510                 virtual eType::_                        Type() const=0;
00511         };
00512         /* LJoint               */ 
00513         struct  LJoint : Joint
00514         {
00515                 struct Specs : Joint::Specs
00516                 {
00517                         btVector3       position;
00518                 };              
00519                 btVector3                                       m_rpos[2];
00520                 void                                            Prepare(btScalar dt,int iterations);
00521                 void                                            Solve(btScalar dt,btScalar sor);
00522                 void                                            Terminate(btScalar dt);
00523                 eType::_                                        Type() const { return(eType::Linear); }
00524         };
00525         /* AJoint               */ 
00526         struct  AJoint : Joint
00527         {
00528                 struct IControl
00529                 {
00530                         virtual void                    Prepare(AJoint*)                                {}
00531                         virtual btScalar                Speed(AJoint*,btScalar current) { return(current); }
00532                         static IControl*                Default()                                               { static IControl def;return(&def); }
00533                 };
00534                 struct Specs : Joint::Specs
00535                 {
00536                         Specs() : icontrol(IControl::Default()) {}
00537                         btVector3       axis;
00538                         IControl*       icontrol;
00539                 };              
00540                 btVector3                                       m_axis[2];
00541                 IControl*                                       m_icontrol;
00542                 void                                            Prepare(btScalar dt,int iterations);
00543                 void                                            Solve(btScalar dt,btScalar sor);
00544                 void                                            Terminate(btScalar dt);
00545                 eType::_                                        Type() const { return(eType::Angular); }
00546         };
00547         /* CJoint               */ 
00548         struct  CJoint : Joint
00549         {               
00550                 int                                                     m_life;
00551                 int                                                     m_maxlife;
00552                 btVector3                                       m_rpos[2];
00553                 btVector3                                       m_normal;
00554                 btScalar                                        m_friction;
00555                 void                                            Prepare(btScalar dt,int iterations);
00556                 void                                            Solve(btScalar dt,btScalar sor);
00557                 void                                            Terminate(btScalar dt);
00558                 eType::_                                        Type() const { return(eType::Contact); }
00559         };
00560         /* Config               */ 
00561         struct  Config
00562         {
00563                 eAeroModel::_                   aeromodel;              // Aerodynamic model (default: V_Point)
00564                 btScalar                                kVCF;                   // Velocities correction factor (Baumgarte)
00565                 btScalar                                kDP;                    // Damping coefficient [0,1]
00566                 btScalar                                kDG;                    // Drag coefficient [0,+inf]
00567                 btScalar                                kLF;                    // Lift coefficient [0,+inf]
00568                 btScalar                                kPR;                    // Pressure coefficient [-inf,+inf]
00569                 btScalar                                kVC;                    // Volume conversation coefficient [0,+inf]
00570                 btScalar                                kDF;                    // Dynamic friction coefficient [0,1]
00571                 btScalar                                kMT;                    // Pose matching coefficient [0,1]              
00572                 btScalar                                kCHR;                   // Rigid contacts hardness [0,1]
00573                 btScalar                                kKHR;                   // Kinetic contacts hardness [0,1]
00574                 btScalar                                kSHR;                   // Soft contacts hardness [0,1]
00575                 btScalar                                kAHR;                   // Anchors hardness [0,1]
00576                 btScalar                                kSRHR_CL;               // Soft vs rigid hardness [0,1] (cluster only)
00577                 btScalar                                kSKHR_CL;               // Soft vs kinetic hardness [0,1] (cluster only)
00578                 btScalar                                kSSHR_CL;               // Soft vs soft hardness [0,1] (cluster only)
00579                 btScalar                                kSR_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
00580                 btScalar                                kSK_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
00581                 btScalar                                kSS_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
00582                 btScalar                                maxvolume;              // Maximum volume ratio for pose
00583                 btScalar                                timescale;              // Time scale
00584                 int                                             viterations;    // Velocities solver iterations
00585                 int                                             piterations;    // Positions solver iterations
00586                 int                                             diterations;    // Drift solver iterations
00587                 int                                             citerations;    // Cluster solver iterations
00588                 int                                             collisions;             // Collisions flags
00589                 tVSolverArray                   m_vsequence;    // Velocity solvers sequence
00590                 tPSolverArray                   m_psequence;    // Position solvers sequence
00591                 tPSolverArray                   m_dsequence;    // Drift solvers sequence
00592         };
00593         /* SolverState  */ 
00594         struct  SolverState
00595         {
00596                 btScalar                                sdt;                    // dt*timescale
00597                 btScalar                                isdt;                   // 1/sdt
00598                 btScalar                                velmrg;                 // velocity margin
00599                 btScalar                                radmrg;                 // radial margin
00600                 btScalar                                updmrg;                 // Update margin
00601         };      
00603         struct  RayFromToCaster : btDbvt::ICollide
00604         {
00605                 btVector3                       m_rayFrom;
00606                 btVector3                       m_rayTo;
00607                 btVector3                       m_rayNormalizedDirection;
00608                 btScalar                        m_mint;
00609                 Face*                           m_face;
00610                 int                                     m_tests;
00611                 RayFromToCaster(const btVector3& rayFrom,const btVector3& rayTo,btScalar mxt);
00612                 void                                    Process(const btDbvtNode* leaf);
00613 
00614                 static inline btScalar  rayFromToTriangle(const btVector3& rayFrom,
00615                         const btVector3& rayTo,
00616                         const btVector3& rayNormalizedDirection,
00617                         const btVector3& a,
00618                         const btVector3& b,
00619                         const btVector3& c,
00620                         btScalar maxt=SIMD_INFINITY);
00621         };
00622 
00623         //
00624         // Typedefs
00625         //
00626 
00627         typedef void                                                            (*psolver_t)(btSoftBody*,btScalar,btScalar);
00628         typedef void                                                            (*vsolver_t)(btSoftBody*,btScalar);
00629         typedef btAlignedObjectArray<Cluster*>          tClusterArray;
00630         typedef btAlignedObjectArray<Note>                      tNoteArray;
00631         typedef btAlignedObjectArray<Node>                      tNodeArray;
00632         typedef btAlignedObjectArray<btDbvtNode*>       tLeafArray;
00633         typedef btAlignedObjectArray<Link>                      tLinkArray;
00634         typedef btAlignedObjectArray<Face>                      tFaceArray;
00635         typedef btAlignedObjectArray<Tetra>                     tTetraArray;
00636         typedef btAlignedObjectArray<Anchor>            tAnchorArray;
00637         typedef btAlignedObjectArray<RContact>          tRContactArray;
00638         typedef btAlignedObjectArray<SContact>          tSContactArray;
00639         typedef btAlignedObjectArray<Material*>         tMaterialArray;
00640         typedef btAlignedObjectArray<Joint*>            tJointArray;
00641         typedef btAlignedObjectArray<btSoftBody*>       tSoftBodyArray; 
00642 
00643         //
00644         // Fields
00645         //
00646 
00647         Config                                  m_cfg;                  // Configuration
00648         SolverState                             m_sst;                  // Solver state
00649         Pose                                    m_pose;                 // Pose
00650         void*                                   m_tag;                  // User data
00651         btSoftBodyWorldInfo*    m_worldInfo;    // World info
00652         tNoteArray                              m_notes;                // Notes
00653         tNodeArray                              m_nodes;                // Nodes
00654         tLinkArray                              m_links;                // Links
00655         tFaceArray                              m_faces;                // Faces
00656         tTetraArray                             m_tetras;               // Tetras
00657         tAnchorArray                    m_anchors;              // Anchors
00658         tRContactArray                  m_rcontacts;    // Rigid contacts
00659         tSContactArray                  m_scontacts;    // Soft contacts
00660         tJointArray                             m_joints;               // Joints
00661         tMaterialArray                  m_materials;    // Materials
00662         btScalar                                m_timeacc;              // Time accumulator
00663         btVector3                               m_bounds[2];    // Spatial bounds       
00664         bool                                    m_bUpdateRtCst; // Update runtime constants
00665         btDbvt                                  m_ndbvt;                // Nodes tree
00666         btDbvt                                  m_fdbvt;                // Faces tree
00667         btDbvt                                  m_cdbvt;                // Clusters tree
00668         tClusterArray                   m_clusters;             // Clusters
00669 
00670         btAlignedObjectArray<bool>m_clusterConnectivity;//cluster connectivity, for self-collision
00671 
00672         btTransform                     m_initialWorldTransform;
00673 
00674         btVector3                       m_windVelocity;
00675         //
00676         // Api
00677         //
00678 
00679         /* ctor                                                                                                                                 */ 
00680         btSoftBody(     btSoftBodyWorldInfo* worldInfo,int node_count,          const btVector3* x,             const btScalar* m);
00681 
00682         /* ctor                                                                                                                                 */ 
00683         btSoftBody(     btSoftBodyWorldInfo* worldInfo);
00684 
00685         void    initDefaults();
00686 
00687         /* dtor                                                                                                                                 */ 
00688         virtual ~btSoftBody();
00689         /* Check for existing link                                                                                              */ 
00690 
00691         btAlignedObjectArray<int>       m_userIndexMapping;
00692 
00693         btSoftBodyWorldInfo*    getWorldInfo()
00694         {
00695                 return m_worldInfo;
00696         }
00697 
00699         virtual void    setCollisionShape(btCollisionShape* collisionShape)
00700         {
00701                 
00702         }
00703 
00704         bool                            checkLink(      int node0,
00705                 int node1) const;
00706         bool                            checkLink(      const Node* node0,
00707                 const Node* node1) const;
00708         /* Check for existring face                                                                                             */ 
00709         bool                            checkFace(      int node0,
00710                 int node1,
00711                 int node2) const;
00712         /* Append material                                                                                                              */ 
00713         Material*                       appendMaterial();
00714         /* Append note                                                                                                                  */ 
00715         void                            appendNote(     const char* text,
00716                 const btVector3& o,
00717                 const btVector4& c=btVector4(1,0,0,0),
00718                 Node* n0=0,
00719                 Node* n1=0,
00720                 Node* n2=0,
00721                 Node* n3=0);
00722         void                            appendNote(     const char* text,
00723                 const btVector3& o,
00724                 Node* feature);
00725         void                            appendNote(     const char* text,
00726                 const btVector3& o,
00727                 Link* feature);
00728         void                            appendNote(     const char* text,
00729                 const btVector3& o,
00730                 Face* feature);
00731         /* Append node                                                                                                                  */ 
00732         void                            appendNode(     const btVector3& x,btScalar m);
00733         /* Append link                                                                                                                  */ 
00734         void                            appendLink(int model=-1,Material* mat=0);
00735         void                            appendLink(     int node0,
00736                 int node1,
00737                 Material* mat=0,
00738                 bool bcheckexist=false);
00739         void                            appendLink(     Node* node0,
00740                 Node* node1,
00741                 Material* mat=0,
00742                 bool bcheckexist=false);
00743         /* Append face                                                                                                                  */ 
00744         void                            appendFace(int model=-1,Material* mat=0);
00745         void                            appendFace(     int node0,
00746                 int node1,
00747                 int node2,
00748                 Material* mat=0);
00749         void                    appendTetra(int model,Material* mat);
00750         //
00751         void                    appendTetra(int node0,
00752                                                                                 int node1,
00753                                                                                 int node2,
00754                                                                                 int node3,
00755                                                                                 Material* mat=0);
00756 
00757 
00758         /* Append anchor                                                                                                                */ 
00759         void                            appendAnchor(   int node,
00760                 btRigidBody* body, bool disableCollisionBetweenLinkedBodies=false);
00761         void                    appendAnchor(int node,btRigidBody* body, const btVector3& localPivot,bool disableCollisionBetweenLinkedBodies=false);
00762         /* Append linear joint                                                                                                  */ 
00763         void                            appendLinearJoint(const LJoint::Specs& specs,Cluster* body0,Body body1);
00764         void                            appendLinearJoint(const LJoint::Specs& specs,Body body=Body());
00765         void                            appendLinearJoint(const LJoint::Specs& specs,btSoftBody* body);
00766         /* Append linear joint                                                                                                  */ 
00767         void                            appendAngularJoint(const AJoint::Specs& specs,Cluster* body0,Body body1);
00768         void                            appendAngularJoint(const AJoint::Specs& specs,Body body=Body());
00769         void                            appendAngularJoint(const AJoint::Specs& specs,btSoftBody* body);
00770         /* Add force (or gravity) to the entire body                                                    */ 
00771         void                            addForce(               const btVector3& force);
00772         /* Add force (or gravity) to a node of the body                                                 */ 
00773         void                            addForce(               const btVector3& force,
00774                 int node);
00775         /* Add velocity to the entire body                                                                              */ 
00776         void                            addVelocity(    const btVector3& velocity);
00777 
00778         /* Set velocity for the entire body                                                                             */ 
00779         void                            setVelocity(    const btVector3& velocity);
00780 
00781         /* Add velocity to a node of the body                                                                   */ 
00782         void                            addVelocity(    const btVector3& velocity,
00783                 int node);
00784         /* Set mass                                                                                                                             */ 
00785         void                            setMass(                int node,
00786                 btScalar mass);
00787         /* Get mass                                                                                                                             */ 
00788         btScalar                        getMass(                int node) const;
00789         /* Get total mass                                                                                                               */ 
00790         btScalar                        getTotalMass() const;
00791         /* Set total mass (weighted by previous masses)                                                 */ 
00792         void                            setTotalMass(   btScalar mass,
00793                 bool fromfaces=false);
00794         /* Set total density                                                                                                    */ 
00795         void                            setTotalDensity(btScalar density);
00796         /* Set volume mass (using tetrahedrons)                                                                 */
00797         void                            setVolumeMass(          btScalar mass);
00798         /* Set volume density (using tetrahedrons)                                                              */
00799         void                            setVolumeDensity(       btScalar density);
00800         /* Transform                                                                                                                    */ 
00801         void                            transform(              const btTransform& trs);
00802         /* Translate                                                                                                                    */ 
00803         void                            translate(              const btVector3& trs);
00804         /* Rotate                                                                                                                       */ 
00805         void                            rotate( const btQuaternion& rot);
00806         /* Scale                                                                                                                                */ 
00807         void                            scale(  const btVector3& scl);
00808         /* Set current state as pose                                                                                    */ 
00809         void                            setPose(                bool bvolume,
00810                 bool bframe);
00811         /* Return the volume                                                                                                    */ 
00812         btScalar                        getVolume() const;
00813         /* Cluster count                                                                                                                */ 
00814         int                                     clusterCount() const;
00815         /* Cluster center of mass                                                                                               */ 
00816         static btVector3        clusterCom(const Cluster* cluster);
00817         btVector3                       clusterCom(int cluster) const;
00818         /* Cluster velocity at rpos                                                                                             */ 
00819         static btVector3        clusterVelocity(const Cluster* cluster,const btVector3& rpos);
00820         /* Cluster impulse                                                                                                              */ 
00821         static void                     clusterVImpulse(Cluster* cluster,const btVector3& rpos,const btVector3& impulse);
00822         static void                     clusterDImpulse(Cluster* cluster,const btVector3& rpos,const btVector3& impulse);
00823         static void                     clusterImpulse(Cluster* cluster,const btVector3& rpos,const Impulse& impulse);
00824         static void                     clusterVAImpulse(Cluster* cluster,const btVector3& impulse);
00825         static void                     clusterDAImpulse(Cluster* cluster,const btVector3& impulse);
00826         static void                     clusterAImpulse(Cluster* cluster,const Impulse& impulse);
00827         static void                     clusterDCImpulse(Cluster* cluster,const btVector3& impulse);
00828         /* Generate bending constraints based on distance in the adjency graph  */ 
00829         int                                     generateBendingConstraints(     int distance,
00830                 Material* mat=0);
00831         /* Randomize constraints to reduce solver bias                                                  */ 
00832         void                            randomizeConstraints();
00833         /* Release clusters                                                                                                             */ 
00834         void                            releaseCluster(int index);
00835         void                            releaseClusters();
00836         /* Generate clusters (K-mean)                                                                                   */ 
00839         int                                     generateClusters(int k,int maxiterations=8192);
00840         /* Refine                                                                                                                               */ 
00841         void                            refine(ImplicitFn* ifn,btScalar accurary,bool cut);
00842         /* CutLink                                                                                                                              */ 
00843         bool                            cutLink(int node0,int node1,btScalar position);
00844         bool                            cutLink(const Node* node0,const Node* node1,btScalar position);
00845 
00847         bool                            rayTest(const btVector3& rayFrom,
00848                 const btVector3& rayTo,
00849                 sRayCast& results);
00850         /* Solver presets                                                                                                               */ 
00851         void                            setSolver(eSolverPresets::_ preset);
00852         /* predictMotion                                                                                                                */ 
00853         void                            predictMotion(btScalar dt);
00854         /* solveConstraints                                                                                                             */ 
00855         void                            solveConstraints();
00856         /* staticSolve                                                                                                                  */ 
00857         void                            staticSolve(int iterations);
00858         /* solveCommonConstraints                                                                                               */ 
00859         static void                     solveCommonConstraints(btSoftBody** bodies,int count,int iterations);
00860         /* solveClusters                                                                                                                */ 
00861         static void                     solveClusters(const btAlignedObjectArray<btSoftBody*>& bodies);
00862         /* integrateMotion                                                                                                              */ 
00863         void                            integrateMotion();
00864         /* defaultCollisionHandlers                                                                                             */ 
00865         void                            defaultCollisionHandler(btCollisionObject* pco);
00866         void                            defaultCollisionHandler(btSoftBody* psb);
00867 
00868 
00869 
00870         //
00871         // Functionality to deal with new accelerated solvers.
00872         //
00873 
00877         void setWindVelocity( const btVector3 &velocity );
00878 
00879 
00883         const btVector3& getWindVelocity();
00884 
00885         //
00886         // Set the solver that handles this soft body
00887         // Should not be allowed to get out of sync with reality
00888         // Currently called internally on addition to the world
00889         void setSoftBodySolver( btSoftBodySolver *softBodySolver )
00890         {
00891                 m_softBodySolver = softBodySolver;
00892         }
00893 
00894         //
00895         // Return the solver that handles this soft body
00896         // 
00897         btSoftBodySolver *getSoftBodySolver()
00898         {
00899                 return m_softBodySolver;
00900         }
00901 
00902         //
00903         // Return the solver that handles this soft body
00904         // 
00905         btSoftBodySolver *getSoftBodySolver() const
00906         {
00907                 return m_softBodySolver;
00908         }
00909 
00910 
00911         //
00912         // Cast
00913         //
00914 
00915         static const btSoftBody*        upcast(const btCollisionObject* colObj)
00916         {
00917                 if (colObj->getInternalType()==CO_SOFT_BODY)
00918                         return (const btSoftBody*)colObj;
00919                 return 0;
00920         }
00921         static btSoftBody*                      upcast(btCollisionObject* colObj)
00922         {
00923                 if (colObj->getInternalType()==CO_SOFT_BODY)
00924                         return (btSoftBody*)colObj;
00925                 return 0;
00926         }
00927 
00928         //
00929         // ::btCollisionObject
00930         //
00931 
00932         virtual void getAabb(btVector3& aabbMin,btVector3& aabbMax) const
00933         {
00934                 aabbMin = m_bounds[0];
00935                 aabbMax = m_bounds[1];
00936         }
00937         //
00938         // Private
00939         //
00940         void                            pointersToIndices();
00941         void                            indicesToPointers(const int* map=0);
00942 
00943         int                                     rayTest(const btVector3& rayFrom,const btVector3& rayTo,
00944                 btScalar& mint,eFeature::_& feature,int& index,bool bcountonly) const;
00945         void                            initializeFaceTree();
00946         btVector3                       evaluateCom() const;
00947         bool                            checkContact(btCollisionObject* colObj,const btVector3& x,btScalar margin,btSoftBody::sCti& cti) const;
00948         void                            updateNormals();
00949         void                            updateBounds();
00950         void                            updatePose();
00951         void                            updateConstants();
00952         void                            initializeClusters();
00953         void                            updateClusters();
00954         void                            cleanupClusters();
00955         void                            prepareClusters(int iterations);
00956         void                            solveClusters(btScalar sor);
00957         void                            applyClusters(bool drift);
00958         void                            dampClusters();
00959         void                            applyForces();  
00960         static void                     PSolve_Anchors(btSoftBody* psb,btScalar kst,btScalar ti);
00961         static void                     PSolve_RContacts(btSoftBody* psb,btScalar kst,btScalar ti);
00962         static void                     PSolve_SContacts(btSoftBody* psb,btScalar,btScalar ti);
00963         static void                     PSolve_Links(btSoftBody* psb,btScalar kst,btScalar ti);
00964         static void                     VSolve_Links(btSoftBody* psb,btScalar kst);
00965         static psolver_t        getSolver(ePSolver::_ solver);
00966         static vsolver_t        getSolver(eVSolver::_ solver);
00967 
00968 
00969         virtual int     calculateSerializeBufferSize()  const;
00970 
00972         virtual const char*     serialize(void* dataBuffer,  class btSerializer* serializer) const;
00973 
00974         //virtual void serializeSingleObject(class btSerializer* serializer) const;
00975 
00976 
00977 };
00978 
00979 
00980 
00981 
00982 #endif //_BT_SOFT_BODY_H