BGE bug #17858 fixed: [SHOWSTOPPER] BGE Memory leak. The memory leak has been identi...
[blender.git] / extern / bullet2 / src / BulletSoftBody / btSoftBody.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 ///btSoftBody implementation by Nathanael Presson
16
17 #ifndef _BT_SOFT_BODY_H
18 #define _BT_SOFT_BODY_H
19
20 #include "LinearMath/btAlignedObjectArray.h"
21 #include "LinearMath/btPoint3.h"
22 #include "LinearMath/btTransform.h"
23 #include "LinearMath/btIDebugDraw.h"
24 #include "BulletDynamics/Dynamics/btRigidBody.h"
25
26 #include "BulletCollision/CollisionShapes/btConcaveShape.h"
27 #include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h"
28 #include "btSparseSDF.h"
29 #include "BulletCollision/BroadphaseCollision/btDbvt.h"
30
31 class btBroadphaseInterface;
32 class btDispatcher;
33
34 /* btSoftBodyWorldInfo  */ 
35 struct  btSoftBodyWorldInfo
36 {
37         btScalar                                air_density;
38         btScalar                                water_density;
39         btScalar                                water_offset;
40         btVector3                               water_normal;
41         btBroadphaseInterface*  m_broadphase;
42         btDispatcher*   m_dispatcher;
43         btVector3                               m_gravity;
44         btSparseSdf<3>                  m_sparsesdf;
45 };      
46
47
48 /// btSoftBody is work-in-progress
49 class   btSoftBody : public btCollisionObject
50 {
51 public:
52         btAlignedObjectArray<class btCollisionObject*> m_collisionDisabledObjects;
53
54         //
55         // Enumerations
56         //
57
58         ///eAeroModel 
59         struct eAeroModel { enum _ {
60                 V_Point,        ///Vertex normals are oriented toward velocity
61                 V_TwoSided,     ///Vertex normals are fliped to match velocity  
62                 V_OneSided,     ///Vertex normals are taken as it is    
63                 F_TwoSided,     ///Face normals are fliped to match velocity
64                 F_OneSided,     ///Face normals are taken as it is
65                 END
66         };};
67         
68         ///eVSolver : velocities solvers
69         struct  eVSolver { enum _ {
70                 Linear,         ///Linear solver
71                 END
72         };};
73         
74         ///ePSolver : positions solvers
75         struct  ePSolver { enum _ {
76                 Linear,         ///Linear solver
77                 Anchors,        ///Anchor solver
78                 RContacts,      ///Rigid contacts solver
79                 SContacts,      ///Soft contacts solver
80                 END
81         };};
82         
83         ///eSolverPresets
84         struct  eSolverPresets { enum _ {
85                 Positions,
86                 Velocities,
87                 Default =       Positions,
88                 END
89         };};
90         
91         ///eFeature
92         struct  eFeature { enum _ {
93                 None,
94                 Node,
95                 Link,
96                 Face,
97                 END
98         };};
99         
100         typedef btAlignedObjectArray<eVSolver::_>       tVSolverArray;
101         typedef btAlignedObjectArray<ePSolver::_>       tPSolverArray;
102         
103         //
104         // Flags
105         //
106         
107         ///fCollision
108         struct fCollision { enum _ {
109                 RVSmask =       0x000f, ///Rigid versus soft mask
110                 SDF_RS  =       0x0001, ///SDF based rigid vs soft
111                 CL_RS   =       0x0002, ///Cluster vs convex rigid vs soft
112                 
113                 SVSmask =       0x00f0, ///Rigid versus soft mask               
114                 VF_SS   =       0x0010, ///Vertex vs face soft vs soft handling
115                 CL_SS   =       0x0020, ///Cluster vs cluster soft vs soft handling
116                 /* presets      */ 
117                 Default =       SDF_RS,
118                 END
119         };};
120         
121         ///fMaterial
122         struct fMaterial { enum _ {
123                 DebugDraw       =       0x0001, /// Enable debug draw
124                 /* presets      */ 
125                 Default         =       DebugDraw,
126                 END
127         };};
128                 
129         //
130         // API Types
131         //
132         
133         /* sRayCast             */ 
134         struct sRayCast
135         {
136                 btSoftBody*     body;           /// soft body
137                 eFeature::_     feature;        /// feature type
138                 int                     index;          /// feature index
139                 btScalar        time;           /// time of impact (rayorg+raydir*time)
140         };
141         
142         /* ImplicitFn   */ 
143         struct  ImplicitFn
144         {
145                 virtual btScalar        Eval(const btVector3& x)=0;
146         };
147         
148         //
149         // Internal types
150         //
151
152         typedef btAlignedObjectArray<btScalar>  tScalarArray;
153         typedef btAlignedObjectArray<btVector3> tVector3Array;
154
155         /* sCti is Softbody contact info        */ 
156         struct  sCti
157         {
158                 btRigidBody*    m_body;         /* Rigid body                   */ 
159                 btVector3               m_normal;       /* Outward normal               */ 
160                 btScalar                m_offset;       /* Offset from origin   */ 
161         };      
162
163         /* sMedium              */ 
164         struct  sMedium
165         {
166                 btVector3               m_velocity;     /* Velocity                             */ 
167                 btScalar                m_pressure;     /* Pressure                             */ 
168                 btScalar                m_density;      /* Density                              */ 
169         };
170
171         /* Base type    */ 
172         struct  Element
173         {
174                 void*                   m_tag;                  // User data
175                 Element() : m_tag(0) {}
176         };
177         /* Material             */ 
178         struct  Material : Element
179         {
180                 btScalar                                m_kLST;                 // Linear stiffness coefficient [0,1]
181                 btScalar                                m_kAST;                 // Area/Angular stiffness coefficient [0,1]
182                 btScalar                                m_kVST;                 // Volume stiffness coefficient [0,1]
183                 int                                             m_flags;                // Flags
184         };
185                 
186         /* Feature              */ 
187         struct  Feature : Element
188         {
189                 Material*                               m_material;             // Material
190         };
191         /* Node                 */ 
192         struct  Node : Feature
193         {
194                 btVector3                               m_x;                    // Position
195                 btVector3                               m_q;                    // Previous step position
196                 btVector3                               m_v;                    // Velocity
197                 btVector3                               m_f;                    // Force accumulator
198                 btVector3                               m_n;                    // Normal
199                 btScalar                                m_im;                   // 1/mass
200                 btScalar                                m_area;                 // Area
201                 btDbvtNode*                             m_leaf;                 // Leaf data
202                 int                                             m_battach:1;    // Attached
203         };
204         /* Link                 */ 
205         struct  Link : Feature
206         {
207                 Node*                                   m_n[2];                 // Node pointers
208                 btScalar                                m_rl;                   // Rest length          
209                 int                                             m_bbending:1;   // Bending link
210                 btScalar                                m_c0;                   // (ima+imb)*kLST
211                 btScalar                                m_c1;                   // rl^2
212                 btScalar                                m_c2;                   // |gradient|^2/c0
213                 btVector3                               m_c3;                   // gradient
214         };
215         /* Face                 */ 
216         struct  Face : Feature
217         {
218                 Node*                                   m_n[3];                 // Node pointers
219                 btVector3                               m_normal;               // Normal
220                 btScalar                                m_ra;                   // Rest area
221                 btDbvtNode*                             m_leaf;                 // Leaf data
222         };
223         /* RContact             */ 
224         struct  RContact
225         {
226                 sCti            m_cti;                  // Contact infos
227                 Node*                                   m_node;                 // Owner node
228                 btMatrix3x3                             m_c0;                   // Impulse matrix
229                 btVector3                               m_c1;                   // Relative anchor
230                 btScalar                                m_c2;                   // ima*dt
231                 btScalar                                m_c3;                   // Friction
232                 btScalar                                m_c4;                   // Hardness
233         };
234         /* SContact             */ 
235         struct  SContact
236         {
237                 Node*                                   m_node;                 // Node
238                 Face*                                   m_face;                 // Face
239                 btVector3                               m_weights;              // Weigths
240                 btVector3                               m_normal;               // Normal
241                 btScalar                                m_margin;               // Margin
242                 btScalar                                m_friction;             // Friction
243                 btScalar                                m_cfm[2];               // Constraint force mixing
244         };
245         /* Anchor               */ 
246         struct  Anchor
247         {
248                 Node*                                   m_node;                 // Node pointer
249                 btVector3                               m_local;                // Anchor position in body space
250                 btRigidBody*                    m_body;                 // Body
251                 btMatrix3x3                             m_c0;                   // Impulse matrix
252                 btVector3                               m_c1;                   // Relative anchor
253                 btScalar                                m_c2;                   // ima*dt
254         };
255         /* Note                 */ 
256         struct  Note : Element
257         {
258                 const char*                             m_text;                 // Text
259                 btVector3                               m_offset;               // Offset
260                 int                                             m_rank;                 // Rank
261                 Node*                                   m_nodes[4];             // Nodes
262                 btScalar                                m_coords[4];    // Coordinates
263         };      
264         /* Pose                 */ 
265         struct  Pose
266         {
267                 bool                                    m_bvolume;              // Is valid
268                 bool                                    m_bframe;               // Is frame
269                 btScalar                                m_volume;               // Rest volume
270                 tVector3Array                   m_pos;                  // Reference positions
271                 tScalarArray                    m_wgh;                  // Weights
272                 btVector3                               m_com;                  // COM
273                 btMatrix3x3                             m_rot;                  // Rotation
274                 btMatrix3x3                             m_scl;                  // Scale
275                 btMatrix3x3                             m_aqq;                  // Base scaling
276         };
277         /* Cluster              */ 
278         struct  Cluster
279         {               
280                 btAlignedObjectArray<Node*>     m_nodes;                
281                 tScalarArray                            m_masses;
282                 tVector3Array                           m_framerefs;
283                 btTransform                                     m_framexform;
284                 btScalar                                        m_idmass;
285                 btScalar                                        m_imass;
286                 btMatrix3x3                                     m_locii;
287                 btMatrix3x3                                     m_invwi;
288                 btVector3                                       m_com;
289                 btVector3                                       m_vimpulses[2];
290                 btVector3                                       m_dimpulses[2];
291                 int                                                     m_nvimpulses;
292                 int                                                     m_ndimpulses;
293                 btVector3                                       m_lv;
294                 btVector3                                       m_av;
295                 btDbvtNode*                                     m_leaf;
296                 btScalar                                        m_ndamping;
297                 btScalar                                        m_ldamping;
298                 btScalar                                        m_adamping;
299                 btScalar                                        m_matching;
300                 bool                                            m_collide;
301                                                                         Cluster() : m_leaf(0),m_ndamping(0),m_ldamping(0),m_adamping(0),m_matching(0) {}
302         };
303         /* Impulse              */ 
304         struct  Impulse
305         {
306                 btVector3                                       m_velocity;
307                 btVector3                                       m_drift;
308                 int                                                     m_asVelocity:1;
309                 int                                                     m_asDrift:1;
310                 Impulse() : m_velocity(0,0,0),m_drift(0,0,0),m_asVelocity(0),m_asDrift(0)       {}
311                 Impulse                                         operator -() const
312                         {
313                         Impulse i=*this;
314                         i.m_velocity=-i.m_velocity;
315                         i.m_drift=-i.m_drift;
316                         return(i);
317                         }
318                 Impulse                                         operator*(btScalar x) const
319                         {
320                         Impulse i=*this;
321                         i.m_velocity*=x;
322                         i.m_drift*=x;
323                         return(i);
324                         }
325         };
326         /* Body                 */ 
327         struct  Body
328         {
329                 Cluster*                m_soft;
330                 btRigidBody*                            m_rigid;
331                                                                         Body() : m_soft(0),m_rigid(0)                           {}
332                                                                         Body(Cluster* p) : m_soft(p),m_rigid(0) {}
333                                                                         Body(btRigidBody* p) : m_soft(0),m_rigid(p)     {}
334                 void                                            activate() const
335                         {
336                         if(m_rigid) m_rigid->activate();
337                         }
338                 const btMatrix3x3&                      invWorldInertia() const
339                         {
340                         static const btMatrix3x3        iwi(0,0,0,0,0,0,0,0,0);
341                         if(m_rigid) return(m_rigid->getInvInertiaTensorWorld());
342                         if(m_soft)      return(m_soft->m_invwi);
343                         return(iwi);
344                         }
345                 btScalar                                        invMass() const
346                         {
347                         if(m_rigid) return(m_rigid->getInvMass());
348                         if(m_soft)      return(m_soft->m_imass);
349                         return(0);
350                         }
351                 const btTransform&                      xform() const
352                         {
353                         static const btTransform        identity=btTransform::getIdentity();            
354                         if(m_rigid) return(m_rigid->getInterpolationWorldTransform());
355                         if(m_soft)      return(m_soft->m_framexform);
356                         return(identity);
357                         }
358                 btVector3                                       linearVelocity() const
359                         {
360                         if(m_rigid) return(m_rigid->getLinearVelocity());
361                         if(m_soft)      return(m_soft->m_lv);
362                         return(btVector3(0,0,0));
363                         }
364                 btVector3                                       angularVelocity(const btVector3& rpos) const
365                         {                       
366                         if(m_rigid) return(cross(m_rigid->getAngularVelocity(),rpos));
367                         if(m_soft)      return(cross(m_soft->m_av,rpos));
368                         return(btVector3(0,0,0));
369                         }
370                 btVector3                                       angularVelocity() const
371                         {                       
372                         if(m_rigid) return(m_rigid->getAngularVelocity());
373                         if(m_soft)      return(m_soft->m_av);
374                         return(btVector3(0,0,0));
375                         }
376                 btVector3                                       velocity(const btVector3& rpos) const
377                         {
378                         return(linearVelocity()+angularVelocity(rpos));
379                         }
380                 void                                            applyVImpulse(const btVector3& impulse,const btVector3& rpos) const
381                         {
382                         if(m_rigid)     m_rigid->applyImpulse(impulse,rpos);
383                         if(m_soft)      btSoftBody::clusterVImpulse(m_soft,rpos,impulse);
384                         }
385                 void                                            applyDImpulse(const btVector3& impulse,const btVector3& rpos) const
386                         {
387                         if(m_rigid)     m_rigid->applyImpulse(impulse,rpos);
388                         if(m_soft)      btSoftBody::clusterDImpulse(m_soft,rpos,impulse);
389                         }               
390                 void                                            applyImpulse(const Impulse& impulse,const btVector3& rpos) const
391                         {
392                         if(impulse.m_asVelocity)        applyVImpulse(impulse.m_velocity,rpos);
393                         if(impulse.m_asDrift)           applyDImpulse(impulse.m_drift,rpos);
394                         }
395                 void                                            applyVAImpulse(const btVector3& impulse) const
396                         {
397                         if(m_rigid)     m_rigid->applyTorqueImpulse(impulse);
398                         if(m_soft)      btSoftBody::clusterVAImpulse(m_soft,impulse);
399                         }
400                 void                                            applyDAImpulse(const btVector3& impulse) const
401                         {
402                         if(m_rigid)     m_rigid->applyTorqueImpulse(impulse);
403                         if(m_soft)      btSoftBody::clusterDAImpulse(m_soft,impulse);
404                         }
405                 void                                            applyAImpulse(const Impulse& impulse) const
406                         {
407                         if(impulse.m_asVelocity)        applyVAImpulse(impulse.m_velocity);
408                         if(impulse.m_asDrift)           applyDAImpulse(impulse.m_drift);
409                         }
410                 void                                            applyDCImpulse(const btVector3& impulse) const
411                         {
412                         if(m_rigid)     m_rigid->applyCentralImpulse(impulse);
413                         if(m_soft)      btSoftBody::clusterDCImpulse(m_soft,impulse);
414                         }
415         };
416         /* Joint                */ 
417         struct  Joint
418         {
419                 struct eType { enum _ {
420                         Linear,
421                         Angular,
422                         Contact,
423                 };};
424                 struct Specs
425                         {
426                                                 Specs() : erp(1),cfm(1),split(1) {}
427                         btScalar        erp;
428                         btScalar        cfm;
429                         btScalar        split;
430                         };
431                 Body                                            m_bodies[2];
432                 btVector3                                       m_refs[2];
433                 btScalar                                        m_cfm;
434                 btScalar                                        m_erp;
435                 btScalar                                        m_split;
436                 btVector3                                       m_drift;
437                 btVector3                                       m_sdrift;
438                 btMatrix3x3                                     m_massmatrix;
439                 bool                                            m_delete;
440                 virtual                                         ~Joint() {}
441                                                                         Joint() : m_delete(false) {}
442                 virtual void                            Prepare(btScalar dt,int iterations);
443                 virtual void                            Solve(btScalar dt,btScalar sor)=0;
444                 virtual void                            Terminate(btScalar dt)=0;
445                 virtual eType::_                        Type() const=0;
446         };
447         /* LJoint               */ 
448         struct  LJoint : Joint
449         {
450                 struct Specs : Joint::Specs
451                         {
452                         btVector3       position;
453                         };              
454                 btVector3                                       m_rpos[2];
455                 void                                            Prepare(btScalar dt,int iterations);
456                 void                                            Solve(btScalar dt,btScalar sor);
457                 void                                            Terminate(btScalar dt);
458                 eType::_                                        Type() const { return(eType::Linear); }
459         };
460         /* AJoint               */ 
461         struct  AJoint : Joint
462         {
463                 struct IControl
464                         {
465                         virtual void                    Prepare(AJoint*)                                {}
466                         virtual btScalar                Speed(AJoint*,btScalar current) { return(current); }
467                         static IControl*                Default()                                               { static IControl def;return(&def); }
468                         };
469                 struct Specs : Joint::Specs
470                         {
471                                                 Specs() : icontrol(IControl::Default()) {}
472                         btVector3       axis;
473                         IControl*       icontrol;
474                         };              
475                 btVector3                                       m_axis[2];
476                 IControl*                                       m_icontrol;
477                 void                                            Prepare(btScalar dt,int iterations);
478                 void                                            Solve(btScalar dt,btScalar sor);
479                 void                                            Terminate(btScalar dt);
480                 eType::_                                        Type() const { return(eType::Angular); }
481         };
482         /* CJoint               */ 
483         struct  CJoint : Joint
484         {               
485                 int                                                     m_life;
486                 int                                                     m_maxlife;
487                 btVector3                                       m_rpos[2];
488                 btVector3                                       m_normal;
489                 btScalar                                        m_friction;
490                 void                                            Prepare(btScalar dt,int iterations);
491                 void                                            Solve(btScalar dt,btScalar sor);
492                 void                                            Terminate(btScalar dt);
493                 eType::_                                        Type() const { return(eType::Contact); }
494         };
495         /* Config               */ 
496         struct  Config
497         {
498                 eAeroModel::_                   aeromodel;              // Aerodynamic model (default: V_Point)
499                 btScalar                                kVCF;                   // Velocities correction factor (Baumgarte)
500                 btScalar                                kDP;                    // Damping coefficient [0,1]
501                 btScalar                                kDG;                    // Drag coefficient [0,+inf]
502                 btScalar                                kLF;                    // Lift coefficient [0,+inf]
503                 btScalar                                kPR;                    // Pressure coefficient [-inf,+inf]
504                 btScalar                                kVC;                    // Volume conversation coefficient [0,+inf]
505                 btScalar                                kDF;                    // Dynamic friction coefficient [0,1]
506                 btScalar                                kMT;                    // Pose matching coefficient [0,1]              
507                 btScalar                                kCHR;                   // Rigid contacts hardness [0,1]
508                 btScalar                                kKHR;                   // Kinetic contacts hardness [0,1]
509                 btScalar                                kSHR;                   // Soft contacts hardness [0,1]
510                 btScalar                                kAHR;                   // Anchors hardness [0,1]
511                 btScalar                                kSRHR_CL;               // Soft vs rigid hardness [0,1] (cluster only)
512                 btScalar                                kSKHR_CL;               // Soft vs kinetic hardness [0,1] (cluster only)
513                 btScalar                                kSSHR_CL;               // Soft vs soft hardness [0,1] (cluster only)
514                 btScalar                                kSR_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
515                 btScalar                                kSK_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
516                 btScalar                                kSS_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
517                 btScalar                                maxvolume;              // Maximum volume ratio for pose
518                 btScalar                                timescale;              // Time scale
519                 int                                             viterations;    // Velocities solver iterations
520                 int                                             piterations;    // Positions solver iterations
521                 int                                             diterations;    // Drift solver iterations
522                 int                                             citerations;    // Cluster solver iterations
523                 int                                             collisions;             // Collisions flags
524                 tVSolverArray                   m_vsequence;    // Velocity solvers sequence
525                 tPSolverArray                   m_psequence;    // Position solvers sequence
526                 tPSolverArray                   m_dsequence;    // Drift solvers sequence
527         };
528         /* SolverState  */ 
529         struct  SolverState
530         {
531                 btScalar                                sdt;                    // dt*timescale
532                 btScalar                                isdt;                   // 1/sdt
533                 btScalar                                velmrg;                 // velocity margin
534                 btScalar                                radmrg;                 // radial margin
535                 btScalar                                updmrg;                 // Update margin
536         };      
537         /* RayCaster    */ 
538         struct  RayCaster : btDbvt::ICollide
539                 {
540                 btVector3                       o;
541                 btVector3                       d;
542                 btScalar                        mint;
543                 Face*   face;
544                 int                                     tests;
545                                                                 RayCaster(const btVector3& org,const btVector3& dir,btScalar mxt);
546                 void                                    Process(const btDbvtNode* leaf);
547                 static inline btScalar  rayTriangle(const btVector3& org,
548                                                                                         const btVector3& dir,
549                                                                                         const btVector3& a,
550                                                                                         const btVector3& b,
551                                                                                         const btVector3& c,
552                                                                                         btScalar maxt=SIMD_INFINITY);
553                 };
554
555         //
556         // Typedef's
557         //
558
559         typedef void                                                            (*psolver_t)(btSoftBody*,btScalar,btScalar);
560         typedef void                                                            (*vsolver_t)(btSoftBody*,btScalar);
561         typedef btAlignedObjectArray<Cluster*>          tClusterArray;
562         typedef btAlignedObjectArray<Note>                      tNoteArray;
563         typedef btAlignedObjectArray<Node>                      tNodeArray;
564         typedef btAlignedObjectArray<btDbvtNode*>       tLeafArray;
565         typedef btAlignedObjectArray<Link>                      tLinkArray;
566         typedef btAlignedObjectArray<Face>                      tFaceArray;
567         typedef btAlignedObjectArray<Anchor>            tAnchorArray;
568         typedef btAlignedObjectArray<RContact>          tRContactArray;
569         typedef btAlignedObjectArray<SContact>          tSContactArray;
570         typedef btAlignedObjectArray<Material*>         tMaterialArray;
571         typedef btAlignedObjectArray<Joint*>            tJointArray;
572         typedef btAlignedObjectArray<btSoftBody*>       tSoftBodyArray; 
573
574         //
575         // Fields
576         //
577
578         Config                                  m_cfg;                  // Configuration
579         SolverState                             m_sst;                  // Solver state
580         Pose                                    m_pose;                 // Pose
581         void*                                   m_tag;                  // User data
582         btSoftBodyWorldInfo*    m_worldInfo;    // World info
583         tNoteArray                              m_notes;                // Notes
584         tNodeArray                              m_nodes;                // Nodes
585         tLinkArray                              m_links;                // Links
586         tFaceArray                              m_faces;                // Faces
587         tAnchorArray                    m_anchors;              // Anchors
588         tRContactArray                  m_rcontacts;    // Rigid contacts
589         tSContactArray                  m_scontacts;    // Soft contacts
590         tJointArray                             m_joints;               // Joints
591         tMaterialArray                  m_materials;    // Materials
592         btScalar                                m_timeacc;              // Time accumulator
593         btVector3                               m_bounds[2];    // Spatial bounds       
594         bool                                    m_bUpdateRtCst; // Update runtime constants
595         btDbvt                                  m_ndbvt;                // Nodes tree
596         btDbvt                                  m_fdbvt;                // Faces tree
597         btDbvt                                  m_cdbvt;                // Clusters tree
598         tClusterArray                   m_clusters;             // Clusters
599         
600         btTransform                             m_initialWorldTransform; //used to attach constraints etc.
601         //
602         // Api
603         //
604         
605         /* ctor                                                                                                                                 */ 
606         btSoftBody(     btSoftBodyWorldInfo* worldInfo,int node_count,
607                                 const btVector3* x,
608                                 const btScalar* m);
609         /* dtor                                                                                                                                 */ 
610         virtual ~btSoftBody();
611         /* Check for existing link                                                                                              */ 
612
613         btAlignedObjectArray<int>       m_userIndexMapping;
614
615         btSoftBodyWorldInfo*    getWorldInfo()
616         {
617                 return m_worldInfo;
618         }
619
620         virtual void    setCollisionShape(btCollisionShape* collisionShape)
621         {
622                 //don't do anything, due to the internal shape hack: todo: fix this
623         }
624
625         bool                            checkLink(      int node0,
626                 int node1) const;
627         bool                            checkLink(      const Node* node0,
628                                                                         const Node* node1) const;
629         /* Check for existring face                                                                                             */ 
630         bool                            checkFace(      int node0,
631                                                                         int node1,
632                                                                         int node2) const;
633         /* Append material                                                                                                              */ 
634         Material*                       appendMaterial();
635         /* Append note                                                                                                                  */ 
636         void                            appendNote(     const char* text,
637                                                                         const btVector3& o,
638                                                                         const btVector4& c=btVector4(1,0,0,0),
639                                                                         Node* n0=0,
640                                                                         Node* n1=0,
641                                                                         Node* n2=0,
642                                                                         Node* n3=0);
643         void                            appendNote(     const char* text,
644                                                                         const btVector3& o,
645                                                                         Node* feature);
646         void                            appendNote(     const char* text,
647                                                                         const btVector3& o,
648                                                                         Link* feature);
649         void                            appendNote(     const char* text,
650                                                                         const btVector3& o,
651                                                                         Face* feature);
652         /* Append node                                                                                                                  */ 
653         void                            appendNode(     const btVector3& x,btScalar m);
654         /* Append link                                                                                                                  */ 
655         void                            appendLink(int model=-1,Material* mat=0);
656         void                            appendLink(     int node0,
657                                                                         int node1,
658                                                                         Material* mat=0,
659                                                                         bool bcheckexist=false);
660         void                            appendLink(     Node* node0,
661                                                                         Node* node1,
662                                                                         Material* mat=0,
663                                                                         bool bcheckexist=false);
664         /* Append face                                                                                                                  */ 
665         void                            appendFace(int model=-1,Material* mat=0);
666         void                            appendFace(     int node0,
667                                                                         int node1,
668                                                                         int node2,
669                                                                         Material* mat=0);
670         /* Append anchor                                                                                                                */ 
671         void                            appendAnchor(   int node,
672                                                                                 btRigidBody* body,bool disableCollision);
673         /* Append linear joint                                                                                                  */ 
674         void                            appendLinearJoint(const LJoint::Specs& specs,Cluster* body0,Body body1);
675         void                            appendLinearJoint(const LJoint::Specs& specs,Body body=Body());
676         void                            appendLinearJoint(const LJoint::Specs& specs,btSoftBody* body);
677         /* Append linear joint                                                                                                  */ 
678         void                            appendAngularJoint(const AJoint::Specs& specs,Cluster* body0,Body body1);
679         void                            appendAngularJoint(const AJoint::Specs& specs,Body body=Body());
680         void                            appendAngularJoint(const AJoint::Specs& specs,btSoftBody* body);
681         /* Add force (or gravity) to the entire body                                                    */ 
682         void                            addForce(               const btVector3& force);
683         /* Add force (or gravity) to a node of the body                                                 */ 
684         void                            addForce(               const btVector3& force,
685                                                                                 int node);
686         /* Add velocity to the entire body                                                                              */ 
687         void                            addVelocity(    const btVector3& velocity);
688
689         /* Set velocity for the entire body                                                                             */ 
690         void                            setVelocity(    const btVector3& velocity);
691
692         /* Add velocity to a node of the body                                                                   */ 
693         void                            addVelocity(    const btVector3& velocity,
694                                                                                 int node);
695         /* Set mass                                                                                                                             */ 
696         void                            setMass(                int node,
697                                                                                 btScalar mass);
698         /* Get mass                                                                                                                             */ 
699         btScalar                        getMass(                int node) const;
700         /* Get total mass                                                                                                               */ 
701         btScalar                        getTotalMass() const;
702         /* Set total mass (weighted by previous masses)                                                 */ 
703         void                            setTotalMass(   btScalar mass,
704                                                                                 bool fromfaces=false);
705         /* Set total density                                                                                                    */ 
706         void                            setTotalDensity(btScalar density);
707         /* Transform                                                                                                                    */ 
708         void                            transform(              const btTransform& trs);
709         /* Translate                                                                                                                    */ 
710         void                            translate(              const btVector3& trs);
711         /* Rotate                                                                                                                       */ 
712         void                            rotate( const btQuaternion& rot);
713         /* Scale                                                                                                                                */ 
714         void                            scale(  const btVector3& scl);
715         /* Set current state as pose                                                                                    */ 
716         void                            setPose(                bool bvolume,
717                                                                                 bool bframe);
718         /* Return the volume                                                                                                    */ 
719         btScalar                        getVolume() const;
720         /* Cluster count                                                                                                                */ 
721         int                                     clusterCount() const;
722         /* Cluster center of mass                                                                                               */ 
723         static btVector3        clusterCom(const Cluster* cluster);
724         btVector3                       clusterCom(int cluster) const;
725         /* Cluster velocity at rpos                                                                                             */ 
726         static btVector3        clusterVelocity(const Cluster* cluster,const btVector3& rpos);
727         /* Cluster impulse                                                                                                              */ 
728         static void                     clusterVImpulse(Cluster* cluster,const btVector3& rpos,const btVector3& impulse);
729         static void                     clusterDImpulse(Cluster* cluster,const btVector3& rpos,const btVector3& impulse);
730         static void                     clusterImpulse(Cluster* cluster,const btVector3& rpos,const Impulse& impulse);
731         static void                     clusterVAImpulse(Cluster* cluster,const btVector3& impulse);
732         static void                     clusterDAImpulse(Cluster* cluster,const btVector3& impulse);
733         static void                     clusterAImpulse(Cluster* cluster,const Impulse& impulse);
734         static void                     clusterDCImpulse(Cluster* cluster,const btVector3& impulse);
735         /* Generate bending constraints based on distance in the adjency graph  */ 
736         int                                     generateBendingConstraints(     int distance,
737                                                                                                         Material* mat=0);
738         /* Randomize constraints to reduce solver bias                                                  */ 
739         void                            randomizeConstraints();
740         /* Release clusters                                                                                                             */ 
741         void                            releaseCluster(int index);
742         void                            releaseClusters();
743         /* Generate clusters (K-mean)                                                                                   */ 
744         int                                     generateClusters(int k,int maxiterations=8192);
745         /* Refine                                                                                                                               */ 
746         void                            refine(ImplicitFn* ifn,btScalar accurary,bool cut);
747         /* CutLink                                                                                                                              */ 
748         bool                            cutLink(int node0,int node1,btScalar position);
749         bool                            cutLink(const Node* node0,const Node* node1,btScalar position);
750         /* Ray casting                                                                                                                  */ 
751         bool                            rayCast(const btVector3& org,
752                                                                 const btVector3& dir,
753                                                                 sRayCast& results,
754                                                                 btScalar maxtime=SIMD_INFINITY);
755         /* Solver presets                                                                                                               */ 
756         void                            setSolver(eSolverPresets::_ preset);
757         /* predictMotion                                                                                                                */ 
758         void                            predictMotion(btScalar dt);
759         /* solveConstraints                                                                                                             */ 
760         void                            solveConstraints();
761         /* staticSolve                                                                                                                  */ 
762         void                            staticSolve(int iterations);
763         /* solveCommonConstraints                                                                                               */ 
764         static void                     solveCommonConstraints(btSoftBody** bodies,int count,int iterations);
765         /* solveClusters                                                                                                                */ 
766         static void                     solveClusters(const btAlignedObjectArray<btSoftBody*>& bodies);
767         /* integrateMotion                                                                                                              */ 
768         void                            integrateMotion();
769         /* defaultCollisionHandlers                                                                                             */ 
770         void                            defaultCollisionHandler(btCollisionObject* pco);
771         void                            defaultCollisionHandler(btSoftBody* psb);
772                 
773         //
774         // Cast
775         //
776                 
777         static const btSoftBody*        upcast(const btCollisionObject* colObj)
778         {
779                 if (colObj->getInternalType()==CO_SOFT_BODY)
780                         return (const btSoftBody*)colObj;
781                 return 0;
782         }
783         static btSoftBody*                      upcast(btCollisionObject* colObj)
784         {
785                 if (colObj->getInternalType()==CO_SOFT_BODY)
786                         return (btSoftBody*)colObj;
787                 return 0;
788         }
789
790         //
791         // ::btCollisionObject
792         //
793
794         virtual void getAabb(btVector3& aabbMin,btVector3& aabbMax) const
795         {
796                 aabbMin = m_bounds[0];
797                 aabbMax = m_bounds[1];
798         }
799         //
800         // Private
801         //
802         void                            pointersToIndices();
803         void                            indicesToPointers(const int* map=0);
804         int                                     rayCast(const btVector3& org,const btVector3& dir,
805                                                                 btScalar& mint,eFeature::_& feature,int& index,bool bcountonly) const;
806         void                            initializeFaceTree();
807         btVector3                       evaluateCom() const;
808         bool                            checkContact(btRigidBody* prb,const btVector3& x,btScalar margin,btSoftBody::sCti& cti) const;
809         void                            updateNormals();
810         void                            updateBounds();
811         void                            updatePose();
812         void                            updateConstants();
813         void                            initializeClusters();
814         void                            updateClusters();
815         void                            cleanupClusters();
816         void                            prepareClusters(int iterations);
817         void                            solveClusters(btScalar sor);
818         void                            applyClusters(bool drift);
819         void                            dampClusters();
820         void                            applyForces();  
821         static void                     PSolve_Anchors(btSoftBody* psb,btScalar kst,btScalar ti);
822         static void                     PSolve_RContacts(btSoftBody* psb,btScalar kst,btScalar ti);
823         static void                     PSolve_SContacts(btSoftBody* psb,btScalar,btScalar ti);
824         static void                     PSolve_Links(btSoftBody* psb,btScalar kst,btScalar ti);
825         static void                     VSolve_Links(btSoftBody* psb,btScalar kst);
826         static psolver_t        getSolver(ePSolver::_ solver);
827         static vsolver_t        getSolver(eVSolver::_ solver);
828         
829 };
830
831
832
833 #endif //_BT_SOFT_BODY_H