Hopefully a working merge with trunk (could be one error left in raytrace.c - will...
[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/btTransform.h"
22 #include "LinearMath/btIDebugDraw.h"
23 #include "BulletDynamics/Dynamics/btRigidBody.h"
24
25 #include "BulletCollision/CollisionShapes/btConcaveShape.h"
26 #include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h"
27 #include "btSparseSDF.h"
28 #include "BulletCollision/BroadphaseCollision/btDbvt.h"
29
30 class btBroadphaseInterface;
31 class btDispatcher;
32
33 /* btSoftBodyWorldInfo  */ 
34 struct  btSoftBodyWorldInfo
35 {
36         btScalar                                air_density;
37         btScalar                                water_density;
38         btScalar                                water_offset;
39         btVector3                               water_normal;
40         btBroadphaseInterface*  m_broadphase;
41         btDispatcher*   m_dispatcher;
42         btVector3                               m_gravity;
43         btSparseSdf<3>                  m_sparsesdf;
44 };      
45
46
47 ///The btSoftBody is an class to simulate cloth and volumetric soft bodies. 
48 ///There is two-way interaction between btSoftBody and btRigidBody/btCollisionObject.
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        fraction;               /// time of impact fraction (rayorg+(rayto-rayfrom)*fraction)
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                 btCollisionObject*      m_colObj;               /* 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;     /* Node damping         */ 
297                 btScalar                                        m_ldamping;     /* Linear damping       */ 
298                 btScalar                                        m_adamping;     /* Angular damping      */ 
299                 btScalar                                        m_matching;
300                 bool                                            m_collide;
301                 int                                                     m_clusterIndex;
302                 Cluster() : m_leaf(0),m_ndamping(0),m_ldamping(0),m_adamping(0),m_matching(0) {}
303         };
304         /* Impulse              */ 
305         struct  Impulse
306         {
307                 btVector3                                       m_velocity;
308                 btVector3                                       m_drift;
309                 int                                                     m_asVelocity:1;
310                 int                                                     m_asDrift:1;
311                 Impulse() : m_velocity(0,0,0),m_drift(0,0,0),m_asVelocity(0),m_asDrift(0)       {}
312                 Impulse                                         operator -() const
313                 {
314                         Impulse i=*this;
315                         i.m_velocity=-i.m_velocity;
316                         i.m_drift=-i.m_drift;
317                         return(i);
318                 }
319                 Impulse                                         operator*(btScalar x) const
320                 {
321                         Impulse i=*this;
322                         i.m_velocity*=x;
323                         i.m_drift*=x;
324                         return(i);
325                 }
326         };
327         /* Body                 */ 
328         struct  Body
329         {
330                 Cluster*                        m_soft;
331                 btRigidBody*            m_rigid;
332                 btCollisionObject*      m_collisionObject;
333
334                 Body() : m_soft(0),m_rigid(0),m_collisionObject(0)                              {}
335                 Body(Cluster* p) : m_soft(p),m_rigid(0),m_collisionObject(0)    {}
336                 Body(btCollisionObject* colObj) : m_soft(0),m_collisionObject(colObj)
337                 {
338                         m_rigid = btRigidBody::upcast(m_collisionObject);
339                 }
340
341                 void                                            activate() const
342                 {
343                         if(m_rigid) m_rigid->activate();
344                 }
345                 const btMatrix3x3&                      invWorldInertia() const
346                 {
347                         static const btMatrix3x3        iwi(0,0,0,0,0,0,0,0,0);
348                         if(m_rigid) return(m_rigid->getInvInertiaTensorWorld());
349                         if(m_soft)      return(m_soft->m_invwi);
350                         return(iwi);
351                 }
352                 btScalar                                        invMass() const
353                 {
354                         if(m_rigid) return(m_rigid->getInvMass());
355                         if(m_soft)      return(m_soft->m_imass);
356                         return(0);
357                 }
358                 const btTransform&                      xform() const
359                 {
360                         static const btTransform        identity=btTransform::getIdentity();            
361                         if(m_collisionObject) return(m_collisionObject->getInterpolationWorldTransform());
362                         if(m_soft)      return(m_soft->m_framexform);
363                         return(identity);
364                 }
365                 btVector3                                       linearVelocity() const
366                 {
367                         if(m_rigid) return(m_rigid->getLinearVelocity());
368                         if(m_soft)      return(m_soft->m_lv);
369                         return(btVector3(0,0,0));
370                 }
371                 btVector3                                       angularVelocity(const btVector3& rpos) const
372                 {                       
373                         if(m_rigid) return(cross(m_rigid->getAngularVelocity(),rpos));
374                         if(m_soft)      return(cross(m_soft->m_av,rpos));
375                         return(btVector3(0,0,0));
376                 }
377                 btVector3                                       angularVelocity() const
378                 {                       
379                         if(m_rigid) return(m_rigid->getAngularVelocity());
380                         if(m_soft)      return(m_soft->m_av);
381                         return(btVector3(0,0,0));
382                 }
383                 btVector3                                       velocity(const btVector3& rpos) const
384                 {
385                         return(linearVelocity()+angularVelocity(rpos));
386                 }
387                 void                                            applyVImpulse(const btVector3& impulse,const btVector3& rpos) const
388                 {
389                         if(m_rigid)     m_rigid->applyImpulse(impulse,rpos);
390                         if(m_soft)      btSoftBody::clusterVImpulse(m_soft,rpos,impulse);
391                 }
392                 void                                            applyDImpulse(const btVector3& impulse,const btVector3& rpos) const
393                 {
394                         if(m_rigid)     m_rigid->applyImpulse(impulse,rpos);
395                         if(m_soft)      btSoftBody::clusterDImpulse(m_soft,rpos,impulse);
396                 }               
397                 void                                            applyImpulse(const Impulse& impulse,const btVector3& rpos) const
398                 {
399                         if(impulse.m_asVelocity)        applyVImpulse(impulse.m_velocity,rpos);
400                         if(impulse.m_asDrift)           applyDImpulse(impulse.m_drift,rpos);
401                 }
402                 void                                            applyVAImpulse(const btVector3& impulse) const
403                 {
404                         if(m_rigid)     m_rigid->applyTorqueImpulse(impulse);
405                         if(m_soft)      btSoftBody::clusterVAImpulse(m_soft,impulse);
406                 }
407                 void                                            applyDAImpulse(const btVector3& impulse) const
408                 {
409                         if(m_rigid)     m_rigid->applyTorqueImpulse(impulse);
410                         if(m_soft)      btSoftBody::clusterDAImpulse(m_soft,impulse);
411                 }
412                 void                                            applyAImpulse(const Impulse& impulse) const
413                 {
414                         if(impulse.m_asVelocity)        applyVAImpulse(impulse.m_velocity);
415                         if(impulse.m_asDrift)           applyDAImpulse(impulse.m_drift);
416                 }
417                 void                                            applyDCImpulse(const btVector3& impulse) const
418                 {
419                         if(m_rigid)     m_rigid->applyCentralImpulse(impulse);
420                         if(m_soft)      btSoftBody::clusterDCImpulse(m_soft,impulse);
421                 }
422         };
423         /* Joint                */ 
424         struct  Joint
425         {
426                 struct eType { enum _ {
427                         Linear,
428                         Angular,
429                         Contact
430                 };};
431                 struct Specs
432                 {
433                         Specs() : erp(1),cfm(1),split(1) {}
434                         btScalar        erp;
435                         btScalar        cfm;
436                         btScalar        split;
437                 };
438                 Body                                            m_bodies[2];
439                 btVector3                                       m_refs[2];
440                 btScalar                                        m_cfm;
441                 btScalar                                        m_erp;
442                 btScalar                                        m_split;
443                 btVector3                                       m_drift;
444                 btVector3                                       m_sdrift;
445                 btMatrix3x3                                     m_massmatrix;
446                 bool                                            m_delete;
447                 virtual                                         ~Joint() {}
448                 Joint() : m_delete(false) {}
449                 virtual void                            Prepare(btScalar dt,int iterations);
450                 virtual void                            Solve(btScalar dt,btScalar sor)=0;
451                 virtual void                            Terminate(btScalar dt)=0;
452                 virtual eType::_                        Type() const=0;
453         };
454         /* LJoint               */ 
455         struct  LJoint : Joint
456         {
457                 struct Specs : Joint::Specs
458                 {
459                         btVector3       position;
460                 };              
461                 btVector3                                       m_rpos[2];
462                 void                                            Prepare(btScalar dt,int iterations);
463                 void                                            Solve(btScalar dt,btScalar sor);
464                 void                                            Terminate(btScalar dt);
465                 eType::_                                        Type() const { return(eType::Linear); }
466         };
467         /* AJoint               */ 
468         struct  AJoint : Joint
469         {
470                 struct IControl
471                 {
472                         virtual void                    Prepare(AJoint*)                                {}
473                         virtual btScalar                Speed(AJoint*,btScalar current) { return(current); }
474                         static IControl*                Default()                                               { static IControl def;return(&def); }
475                 };
476                 struct Specs : Joint::Specs
477                 {
478                         Specs() : icontrol(IControl::Default()) {}
479                         btVector3       axis;
480                         IControl*       icontrol;
481                 };              
482                 btVector3                                       m_axis[2];
483                 IControl*                                       m_icontrol;
484                 void                                            Prepare(btScalar dt,int iterations);
485                 void                                            Solve(btScalar dt,btScalar sor);
486                 void                                            Terminate(btScalar dt);
487                 eType::_                                        Type() const { return(eType::Angular); }
488         };
489         /* CJoint               */ 
490         struct  CJoint : Joint
491         {               
492                 int                                                     m_life;
493                 int                                                     m_maxlife;
494                 btVector3                                       m_rpos[2];
495                 btVector3                                       m_normal;
496                 btScalar                                        m_friction;
497                 void                                            Prepare(btScalar dt,int iterations);
498                 void                                            Solve(btScalar dt,btScalar sor);
499                 void                                            Terminate(btScalar dt);
500                 eType::_                                        Type() const { return(eType::Contact); }
501         };
502         /* Config               */ 
503         struct  Config
504         {
505                 eAeroModel::_                   aeromodel;              // Aerodynamic model (default: V_Point)
506                 btScalar                                kVCF;                   // Velocities correction factor (Baumgarte)
507                 btScalar                                kDP;                    // Damping coefficient [0,1]
508                 btScalar                                kDG;                    // Drag coefficient [0,+inf]
509                 btScalar                                kLF;                    // Lift coefficient [0,+inf]
510                 btScalar                                kPR;                    // Pressure coefficient [-inf,+inf]
511                 btScalar                                kVC;                    // Volume conversation coefficient [0,+inf]
512                 btScalar                                kDF;                    // Dynamic friction coefficient [0,1]
513                 btScalar                                kMT;                    // Pose matching coefficient [0,1]              
514                 btScalar                                kCHR;                   // Rigid contacts hardness [0,1]
515                 btScalar                                kKHR;                   // Kinetic contacts hardness [0,1]
516                 btScalar                                kSHR;                   // Soft contacts hardness [0,1]
517                 btScalar                                kAHR;                   // Anchors hardness [0,1]
518                 btScalar                                kSRHR_CL;               // Soft vs rigid hardness [0,1] (cluster only)
519                 btScalar                                kSKHR_CL;               // Soft vs kinetic hardness [0,1] (cluster only)
520                 btScalar                                kSSHR_CL;               // Soft vs soft hardness [0,1] (cluster only)
521                 btScalar                                kSR_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
522                 btScalar                                kSK_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
523                 btScalar                                kSS_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
524                 btScalar                                maxvolume;              // Maximum volume ratio for pose
525                 btScalar                                timescale;              // Time scale
526                 int                                             viterations;    // Velocities solver iterations
527                 int                                             piterations;    // Positions solver iterations
528                 int                                             diterations;    // Drift solver iterations
529                 int                                             citerations;    // Cluster solver iterations
530                 int                                             collisions;             // Collisions flags
531                 tVSolverArray                   m_vsequence;    // Velocity solvers sequence
532                 tPSolverArray                   m_psequence;    // Position solvers sequence
533                 tPSolverArray                   m_dsequence;    // Drift solvers sequence
534         };
535         /* SolverState  */ 
536         struct  SolverState
537         {
538                 btScalar                                sdt;                    // dt*timescale
539                 btScalar                                isdt;                   // 1/sdt
540                 btScalar                                velmrg;                 // velocity margin
541                 btScalar                                radmrg;                 // radial margin
542                 btScalar                                updmrg;                 // Update margin
543         };      
544         /// RayFromToCaster takes a ray from, ray to (instead of direction!)
545         struct  RayFromToCaster : btDbvt::ICollide
546         {
547                 btVector3                       m_rayFrom;
548                 btVector3                       m_rayTo;
549                 btVector3                       m_rayNormalizedDirection;
550                 btScalar                        m_mint;
551                 Face*                           m_face;
552                 int                                     m_tests;
553                 RayFromToCaster(const btVector3& rayFrom,const btVector3& rayTo,btScalar mxt);
554                 void                                    Process(const btDbvtNode* leaf);
555
556                 static inline btScalar  rayFromToTriangle(const btVector3& rayFrom,
557                         const btVector3& rayTo,
558                         const btVector3& rayNormalizedDirection,
559                         const btVector3& a,
560                         const btVector3& b,
561                         const btVector3& c,
562                         btScalar maxt=SIMD_INFINITY);
563         };
564
565         //
566         // Typedef's
567         //
568
569         typedef void                                                            (*psolver_t)(btSoftBody*,btScalar,btScalar);
570         typedef void                                                            (*vsolver_t)(btSoftBody*,btScalar);
571         typedef btAlignedObjectArray<Cluster*>          tClusterArray;
572         typedef btAlignedObjectArray<Note>                      tNoteArray;
573         typedef btAlignedObjectArray<Node>                      tNodeArray;
574         typedef btAlignedObjectArray<btDbvtNode*>       tLeafArray;
575         typedef btAlignedObjectArray<Link>                      tLinkArray;
576         typedef btAlignedObjectArray<Face>                      tFaceArray;
577         typedef btAlignedObjectArray<Anchor>            tAnchorArray;
578         typedef btAlignedObjectArray<RContact>          tRContactArray;
579         typedef btAlignedObjectArray<SContact>          tSContactArray;
580         typedef btAlignedObjectArray<Material*>         tMaterialArray;
581         typedef btAlignedObjectArray<Joint*>            tJointArray;
582         typedef btAlignedObjectArray<btSoftBody*>       tSoftBodyArray; 
583
584         //
585         // Fields
586         //
587
588         Config                                  m_cfg;                  // Configuration
589         SolverState                             m_sst;                  // Solver state
590         Pose                                    m_pose;                 // Pose
591         void*                                   m_tag;                  // User data
592         btSoftBodyWorldInfo*    m_worldInfo;    // World info
593         tNoteArray                              m_notes;                // Notes
594         tNodeArray                              m_nodes;                // Nodes
595         tLinkArray                              m_links;                // Links
596         tFaceArray                              m_faces;                // Faces
597         tAnchorArray                    m_anchors;              // Anchors
598         tRContactArray                  m_rcontacts;    // Rigid contacts
599         tSContactArray                  m_scontacts;    // Soft contacts
600         tJointArray                             m_joints;               // Joints
601         tMaterialArray                  m_materials;    // Materials
602         btScalar                                m_timeacc;              // Time accumulator
603         btVector3                               m_bounds[2];    // Spatial bounds       
604         bool                                    m_bUpdateRtCst; // Update runtime constants
605         btDbvt                                  m_ndbvt;                // Nodes tree
606         btDbvt                                  m_fdbvt;                // Faces tree
607         btDbvt                                  m_cdbvt;                // Clusters tree
608         tClusterArray                   m_clusters;             // Clusters
609
610         btAlignedObjectArray<bool>m_clusterConnectivity;//cluster connectivity, for self-collision
611
612         btTransform                     m_initialWorldTransform;
613
614         //
615         // Api
616         //
617
618         /* ctor                                                                                                                                 */ 
619         btSoftBody(     btSoftBodyWorldInfo* worldInfo,int node_count,
620                 const btVector3* x,
621                 const btScalar* m);
622         /* dtor                                                                                                                                 */ 
623         virtual ~btSoftBody();
624         /* Check for existing link                                                                                              */ 
625
626         btAlignedObjectArray<int>       m_userIndexMapping;
627
628         btSoftBodyWorldInfo*    getWorldInfo()
629         {
630                 return m_worldInfo;
631         }
632
633         ///@todo: avoid internal softbody shape hack and move collision code to collision library
634         virtual void    setCollisionShape(btCollisionShape* collisionShape)
635         {
636                 
637         }
638
639         bool                            checkLink(      int node0,
640                 int node1) const;
641         bool                            checkLink(      const Node* node0,
642                 const Node* node1) const;
643         /* Check for existring face                                                                                             */ 
644         bool                            checkFace(      int node0,
645                 int node1,
646                 int node2) const;
647         /* Append material                                                                                                              */ 
648         Material*                       appendMaterial();
649         /* Append note                                                                                                                  */ 
650         void                            appendNote(     const char* text,
651                 const btVector3& o,
652                 const btVector4& c=btVector4(1,0,0,0),
653                 Node* n0=0,
654                 Node* n1=0,
655                 Node* n2=0,
656                 Node* n3=0);
657         void                            appendNote(     const char* text,
658                 const btVector3& o,
659                 Node* feature);
660         void                            appendNote(     const char* text,
661                 const btVector3& o,
662                 Link* feature);
663         void                            appendNote(     const char* text,
664                 const btVector3& o,
665                 Face* feature);
666         /* Append node                                                                                                                  */ 
667         void                            appendNode(     const btVector3& x,btScalar m);
668         /* Append link                                                                                                                  */ 
669         void                            appendLink(int model=-1,Material* mat=0);
670         void                            appendLink(     int node0,
671                 int node1,
672                 Material* mat=0,
673                 bool bcheckexist=false);
674         void                            appendLink(     Node* node0,
675                 Node* node1,
676                 Material* mat=0,
677                 bool bcheckexist=false);
678         /* Append face                                                                                                                  */ 
679         void                            appendFace(int model=-1,Material* mat=0);
680         void                            appendFace(     int node0,
681                 int node1,
682                 int node2,
683                 Material* mat=0);
684         /* Append anchor                                                                                                                */ 
685         void                            appendAnchor(   int node,
686                 btRigidBody* body, bool disableCollisionBetweenLinkedBodies=false);
687         /* Append linear joint                                                                                                  */ 
688         void                            appendLinearJoint(const LJoint::Specs& specs,Cluster* body0,Body body1);
689         void                            appendLinearJoint(const LJoint::Specs& specs,Body body=Body());
690         void                            appendLinearJoint(const LJoint::Specs& specs,btSoftBody* body);
691         /* Append linear joint                                                                                                  */ 
692         void                            appendAngularJoint(const AJoint::Specs& specs,Cluster* body0,Body body1);
693         void                            appendAngularJoint(const AJoint::Specs& specs,Body body=Body());
694         void                            appendAngularJoint(const AJoint::Specs& specs,btSoftBody* body);
695         /* Add force (or gravity) to the entire body                                                    */ 
696         void                            addForce(               const btVector3& force);
697         /* Add force (or gravity) to a node of the body                                                 */ 
698         void                            addForce(               const btVector3& force,
699                 int node);
700         /* Add velocity to the entire body                                                                              */ 
701         void                            addVelocity(    const btVector3& velocity);
702
703         /* Set velocity for the entire body                                                                             */ 
704         void                            setVelocity(    const btVector3& velocity);
705
706         /* Add velocity to a node of the body                                                                   */ 
707         void                            addVelocity(    const btVector3& velocity,
708                 int node);
709         /* Set mass                                                                                                                             */ 
710         void                            setMass(                int node,
711                 btScalar mass);
712         /* Get mass                                                                                                                             */ 
713         btScalar                        getMass(                int node) const;
714         /* Get total mass                                                                                                               */ 
715         btScalar                        getTotalMass() const;
716         /* Set total mass (weighted by previous masses)                                                 */ 
717         void                            setTotalMass(   btScalar mass,
718                 bool fromfaces=false);
719         /* Set total density                                                                                                    */ 
720         void                            setTotalDensity(btScalar density);
721         /* Transform                                                                                                                    */ 
722         void                            transform(              const btTransform& trs);
723         /* Translate                                                                                                                    */ 
724         void                            translate(              const btVector3& trs);
725         /* Rotate                                                                                                                       */ 
726         void                            rotate( const btQuaternion& rot);
727         /* Scale                                                                                                                                */ 
728         void                            scale(  const btVector3& scl);
729         /* Set current state as pose                                                                                    */ 
730         void                            setPose(                bool bvolume,
731                 bool bframe);
732         /* Return the volume                                                                                                    */ 
733         btScalar                        getVolume() const;
734         /* Cluster count                                                                                                                */ 
735         int                                     clusterCount() const;
736         /* Cluster center of mass                                                                                               */ 
737         static btVector3        clusterCom(const Cluster* cluster);
738         btVector3                       clusterCom(int cluster) const;
739         /* Cluster velocity at rpos                                                                                             */ 
740         static btVector3        clusterVelocity(const Cluster* cluster,const btVector3& rpos);
741         /* Cluster impulse                                                                                                              */ 
742         static void                     clusterVImpulse(Cluster* cluster,const btVector3& rpos,const btVector3& impulse);
743         static void                     clusterDImpulse(Cluster* cluster,const btVector3& rpos,const btVector3& impulse);
744         static void                     clusterImpulse(Cluster* cluster,const btVector3& rpos,const Impulse& impulse);
745         static void                     clusterVAImpulse(Cluster* cluster,const btVector3& impulse);
746         static void                     clusterDAImpulse(Cluster* cluster,const btVector3& impulse);
747         static void                     clusterAImpulse(Cluster* cluster,const Impulse& impulse);
748         static void                     clusterDCImpulse(Cluster* cluster,const btVector3& impulse);
749         /* Generate bending constraints based on distance in the adjency graph  */ 
750         int                                     generateBendingConstraints(     int distance,
751                 Material* mat=0);
752         /* Randomize constraints to reduce solver bias                                                  */ 
753         void                            randomizeConstraints();
754         /* Release clusters                                                                                                             */ 
755         void                            releaseCluster(int index);
756         void                            releaseClusters();
757         /* Generate clusters (K-mean)                                                                                   */ 
758         int                                     generateClusters(int k,int maxiterations=8192);
759         /* Refine                                                                                                                               */ 
760         void                            refine(ImplicitFn* ifn,btScalar accurary,bool cut);
761         /* CutLink                                                                                                                              */ 
762         bool                            cutLink(int node0,int node1,btScalar position);
763         bool                            cutLink(const Node* node0,const Node* node1,btScalar position);
764
765         ///Ray casting using rayFrom and rayTo in worldspace, (not direction!)
766         bool                            rayTest(const btVector3& rayFrom,
767                 const btVector3& rayTo,
768                 sRayCast& results);
769         /* Solver presets                                                                                                               */ 
770         void                            setSolver(eSolverPresets::_ preset);
771         /* predictMotion                                                                                                                */ 
772         void                            predictMotion(btScalar dt);
773         /* solveConstraints                                                                                                             */ 
774         void                            solveConstraints();
775         /* staticSolve                                                                                                                  */ 
776         void                            staticSolve(int iterations);
777         /* solveCommonConstraints                                                                                               */ 
778         static void                     solveCommonConstraints(btSoftBody** bodies,int count,int iterations);
779         /* solveClusters                                                                                                                */ 
780         static void                     solveClusters(const btAlignedObjectArray<btSoftBody*>& bodies);
781         /* integrateMotion                                                                                                              */ 
782         void                            integrateMotion();
783         /* defaultCollisionHandlers                                                                                             */ 
784         void                            defaultCollisionHandler(btCollisionObject* pco);
785         void                            defaultCollisionHandler(btSoftBody* psb);
786
787         //
788         // Cast
789         //
790
791         static const btSoftBody*        upcast(const btCollisionObject* colObj)
792         {
793                 if (colObj->getInternalType()==CO_SOFT_BODY)
794                         return (const btSoftBody*)colObj;
795                 return 0;
796         }
797         static btSoftBody*                      upcast(btCollisionObject* colObj)
798         {
799                 if (colObj->getInternalType()==CO_SOFT_BODY)
800                         return (btSoftBody*)colObj;
801                 return 0;
802         }
803
804         //
805         // ::btCollisionObject
806         //
807
808         virtual void getAabb(btVector3& aabbMin,btVector3& aabbMax) const
809         {
810                 aabbMin = m_bounds[0];
811                 aabbMax = m_bounds[1];
812         }
813         //
814         // Private
815         //
816         void                            pointersToIndices();
817         void                            indicesToPointers(const int* map=0);
818
819         int                                     rayTest(const btVector3& rayFrom,const btVector3& rayTo,
820                 btScalar& mint,eFeature::_& feature,int& index,bool bcountonly) const;
821         void                            initializeFaceTree();
822         btVector3                       evaluateCom() const;
823         bool                            checkContact(btCollisionObject* colObj,const btVector3& x,btScalar margin,btSoftBody::sCti& cti) const;
824         void                            updateNormals();
825         void                            updateBounds();
826         void                            updatePose();
827         void                            updateConstants();
828         void                            initializeClusters();
829         void                            updateClusters();
830         void                            cleanupClusters();
831         void                            prepareClusters(int iterations);
832         void                            solveClusters(btScalar sor);
833         void                            applyClusters(bool drift);
834         void                            dampClusters();
835         void                            applyForces();  
836         static void                     PSolve_Anchors(btSoftBody* psb,btScalar kst,btScalar ti);
837         static void                     PSolve_RContacts(btSoftBody* psb,btScalar kst,btScalar ti);
838         static void                     PSolve_SContacts(btSoftBody* psb,btScalar,btScalar ti);
839         static void                     PSolve_Links(btSoftBody* psb,btScalar kst,btScalar ti);
840         static void                     VSolve_Links(btSoftBody* psb,btScalar kst);
841         static psolver_t        getSolver(ePSolver::_ solver);
842         static vsolver_t        getSolver(eVSolver::_ solver);
843
844 };
845
846
847
848 #endif //_BT_SOFT_BODY_H