~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/bullet/src/BulletSoftBody/btSoftBody.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
//#ifdef BT_USE_DOUBLE_PRECISION
 
31
//#define btRigidBodyData       btRigidBodyDoubleData
 
32
//#define btRigidBodyDataName   "btRigidBodyDoubleData"
 
33
//#else
 
34
#define btSoftBodyData  btSoftBodyFloatData
 
35
#define btSoftBodyDataName      "btSoftBodyFloatData"
 
36
//#endif //BT_USE_DOUBLE_PRECISION
 
37
 
 
38
class btBroadphaseInterface;
 
39
class btDispatcher;
 
40
class btSoftBodySolver;
 
41
 
 
42
/* btSoftBodyWorldInfo  */ 
 
43
struct  btSoftBodyWorldInfo
 
44
{
 
45
        btScalar                                air_density;
 
46
        btScalar                                water_density;
 
47
        btScalar                                water_offset;
 
48
        btVector3                               water_normal;
 
49
        btBroadphaseInterface*  m_broadphase;
 
50
        btDispatcher*   m_dispatcher;
 
51
        btVector3                               m_gravity;
 
52
        btSparseSdf<3>                  m_sparsesdf;
 
53
 
 
54
        btSoftBodyWorldInfo()
 
55
                :air_density((btScalar)1.2),
 
56
                water_density(0),
 
57
                water_offset(0),
 
58
                water_normal(0,0,0),
 
59
                m_broadphase(0),
 
60
                m_dispatcher(0),
 
61
                m_gravity(0,-10,0)
 
62
        {
 
63
        }
 
64
};      
 
65
 
 
66
 
 
67
///The btSoftBody is an class to simulate cloth and volumetric soft bodies. 
 
68
///There is two-way interaction between btSoftBody and btRigidBody/btCollisionObject.
 
69
class   btSoftBody : public btCollisionObject
 
70
{
 
71
public:
 
72
        btAlignedObjectArray<class btCollisionObject*> m_collisionDisabledObjects;
 
73
 
 
74
        // The solver object that handles this soft body
 
75
        btSoftBodySolver *m_softBodySolver;
 
76
 
 
77
        //
 
78
        // Enumerations
 
79
        //
 
80
 
 
81
        ///eAeroModel 
 
82
        struct eAeroModel { enum _ {
 
83
                V_Point,        ///Vertex normals are oriented toward velocity
 
84
                V_TwoSided,     ///Vertex normals are fliped to match velocity  
 
85
                V_OneSided,     ///Vertex normals are taken as it is    
 
86
                F_TwoSided,     ///Face normals are fliped to match velocity
 
87
                F_OneSided,     ///Face normals are taken as it is
 
88
                END
 
89
        };};
 
90
 
 
91
        ///eVSolver : velocities solvers
 
92
        struct  eVSolver { enum _ {
 
93
                Linear,         ///Linear solver
 
94
                END
 
95
        };};
 
96
 
 
97
        ///ePSolver : positions solvers
 
98
        struct  ePSolver { enum _ {
 
99
                Linear,         ///Linear solver
 
100
                Anchors,        ///Anchor solver
 
101
                RContacts,      ///Rigid contacts solver
 
102
                SContacts,      ///Soft contacts solver
 
103
                END
 
104
        };};
 
105
 
 
106
        ///eSolverPresets
 
107
        struct  eSolverPresets { enum _ {
 
108
                Positions,
 
109
                Velocities,
 
110
                Default =       Positions,
 
111
                END
 
112
        };};
 
113
 
 
114
        ///eFeature
 
115
        struct  eFeature { enum _ {
 
116
                None,
 
117
                Node,
 
118
                Link,
 
119
                Face,
 
120
                END
 
121
        };};
 
122
 
 
123
        typedef btAlignedObjectArray<eVSolver::_>       tVSolverArray;
 
124
        typedef btAlignedObjectArray<ePSolver::_>       tPSolverArray;
 
125
 
 
126
        //
 
127
        // Flags
 
128
        //
 
129
 
 
130
        ///fCollision
 
131
        struct fCollision { enum _ {
 
132
                RVSmask =       0x000f, ///Rigid versus soft mask
 
133
                SDF_RS  =       0x0001, ///SDF based rigid vs soft
 
134
                CL_RS   =       0x0002, ///Cluster vs convex rigid vs soft
 
135
 
 
136
                SVSmask =       0x0030, ///Rigid versus soft mask               
 
137
                VF_SS   =       0x0010, ///Vertex vs face soft vs soft handling
 
138
                CL_SS   =       0x0020, ///Cluster vs cluster soft vs soft handling
 
139
                CL_SELF =       0x0040, ///Cluster soft body self collision
 
140
                /* presets      */ 
 
141
                Default =       SDF_RS,
 
142
                END
 
143
        };};
 
144
 
 
145
        ///fMaterial
 
146
        struct fMaterial { enum _ {
 
147
                DebugDraw       =       0x0001, /// Enable debug draw
 
148
                /* presets      */ 
 
149
                Default         =       DebugDraw,
 
150
                END
 
151
        };};
 
152
 
 
153
        //
 
154
        // API Types
 
155
        //
 
156
 
 
157
        /* sRayCast             */ 
 
158
        struct sRayCast
 
159
        {
 
160
                btSoftBody*     body;           /// soft body
 
161
                eFeature::_     feature;        /// feature type
 
162
                int                     index;          /// feature index
 
163
                btScalar        fraction;               /// time of impact fraction (rayorg+(rayto-rayfrom)*fraction)
 
164
        };
 
165
 
 
166
        /* ImplicitFn   */ 
 
167
        struct  ImplicitFn
 
168
        {
 
169
                virtual btScalar        Eval(const btVector3& x)=0;
 
170
        };
 
171
 
 
172
        //
 
173
        // Internal types
 
174
        //
 
175
 
 
176
        typedef btAlignedObjectArray<btScalar>  tScalarArray;
 
177
        typedef btAlignedObjectArray<btVector3> tVector3Array;
 
178
 
 
179
        /* sCti is Softbody contact info        */ 
 
180
        struct  sCti
 
181
        {
 
182
                btCollisionObject*      m_colObj;               /* Rigid body                   */ 
 
183
                btVector3               m_normal;       /* Outward normal               */ 
 
184
                btScalar                m_offset;       /* Offset from origin   */ 
 
185
        };      
 
186
 
 
187
        /* sMedium              */ 
 
188
        struct  sMedium
 
189
        {
 
190
                btVector3               m_velocity;     /* Velocity                             */ 
 
191
                btScalar                m_pressure;     /* Pressure                             */ 
 
192
                btScalar                m_density;      /* Density                              */ 
 
193
        };
 
194
 
 
195
        /* Base type    */ 
 
196
        struct  Element
 
197
        {
 
198
                void*                   m_tag;                  // User data
 
199
                Element() : m_tag(0) {}
 
200
        };
 
201
        /* Material             */ 
 
202
        struct  Material : Element
 
203
        {
 
204
                btScalar                                m_kLST;                 // Linear stiffness coefficient [0,1]
 
205
                btScalar                                m_kAST;                 // Area/Angular stiffness coefficient [0,1]
 
206
                btScalar                                m_kVST;                 // Volume stiffness coefficient [0,1]
 
207
                int                                             m_flags;                // Flags
 
208
        };
 
209
 
 
210
        /* Feature              */ 
 
211
        struct  Feature : Element
 
212
        {
 
213
                Material*                               m_material;             // Material
 
214
        };
 
215
        /* Node                 */ 
 
216
        struct  Node : Feature
 
217
        {
 
218
                btVector3                               m_x;                    // Position
 
219
                btVector3                               m_q;                    // Previous step position
 
220
                btVector3                               m_v;                    // Velocity
 
221
                btVector3                               m_f;                    // Force accumulator
 
222
                btVector3                               m_n;                    // Normal
 
223
                btScalar                                m_im;                   // 1/mass
 
224
                btScalar                                m_area;                 // Area
 
225
                btDbvtNode*                             m_leaf;                 // Leaf data
 
226
                int                                             m_battach:1;    // Attached
 
227
        };
 
228
        /* Link                 */ 
 
229
        struct  Link : Feature
 
230
        {
 
231
                Node*                                   m_n[2];                 // Node pointers
 
232
                btScalar                                m_rl;                   // Rest length          
 
233
                int                                             m_bbending:1;   // Bending link
 
234
                btScalar                                m_c0;                   // (ima+imb)*kLST
 
235
                btScalar                                m_c1;                   // rl^2
 
236
                btScalar                                m_c2;                   // |gradient|^2/c0
 
237
                btVector3                               m_c3;                   // gradient
 
238
        };
 
239
        /* Face                 */ 
 
240
        struct  Face : Feature
 
241
        {
 
242
                Node*                                   m_n[3];                 // Node pointers
 
243
                btVector3                               m_normal;               // Normal
 
244
                btScalar                                m_ra;                   // Rest area
 
245
                btDbvtNode*                             m_leaf;                 // Leaf data
 
246
        };
 
247
        /* Tetra                */ 
 
248
        struct  Tetra : Feature
 
249
        {
 
250
                Node*                                   m_n[4];                 // Node pointers                
 
251
                btScalar                                m_rv;                   // Rest volume
 
252
                btDbvtNode*                             m_leaf;                 // Leaf data
 
253
                btVector3                               m_c0[4];                // gradients
 
254
                btScalar                                m_c1;                   // (4*kVST)/(im0+im1+im2+im3)
 
255
                btScalar                                m_c2;                   // m_c1/sum(|g0..3|^2)
 
256
        };
 
257
        /* RContact             */ 
 
258
        struct  RContact
 
259
        {
 
260
                sCti            m_cti;                  // Contact infos
 
261
                Node*                                   m_node;                 // Owner node
 
262
                btMatrix3x3                             m_c0;                   // Impulse matrix
 
263
                btVector3                               m_c1;                   // Relative anchor
 
264
                btScalar                                m_c2;                   // ima*dt
 
265
                btScalar                                m_c3;                   // Friction
 
266
                btScalar                                m_c4;                   // Hardness
 
267
        };
 
268
        /* SContact             */ 
 
269
        struct  SContact
 
270
        {
 
271
                Node*                                   m_node;                 // Node
 
272
                Face*                                   m_face;                 // Face
 
273
                btVector3                               m_weights;              // Weigths
 
274
                btVector3                               m_normal;               // Normal
 
275
                btScalar                                m_margin;               // Margin
 
276
                btScalar                                m_friction;             // Friction
 
277
                btScalar                                m_cfm[2];               // Constraint force mixing
 
278
        };
 
279
        /* Anchor               */ 
 
280
        struct  Anchor
 
281
        {
 
282
                Node*                                   m_node;                 // Node pointer
 
283
                btVector3                               m_local;                // Anchor position in body space
 
284
                btRigidBody*                    m_body;                 // Body
 
285
                btScalar                                m_influence;
 
286
                btMatrix3x3                             m_c0;                   // Impulse matrix
 
287
                btVector3                               m_c1;                   // Relative anchor
 
288
                btScalar                                m_c2;                   // ima*dt
 
289
        };
 
290
        /* Note                 */ 
 
291
        struct  Note : Element
 
292
        {
 
293
                const char*                             m_text;                 // Text
 
294
                btVector3                               m_offset;               // Offset
 
295
                int                                             m_rank;                 // Rank
 
296
                Node*                                   m_nodes[4];             // Nodes
 
297
                btScalar                                m_coords[4];    // Coordinates
 
298
        };      
 
299
        /* Pose                 */ 
 
300
        struct  Pose
 
301
        {
 
302
                bool                                    m_bvolume;              // Is valid
 
303
                bool                                    m_bframe;               // Is frame
 
304
                btScalar                                m_volume;               // Rest volume
 
305
                tVector3Array                   m_pos;                  // Reference positions
 
306
                tScalarArray                    m_wgh;                  // Weights
 
307
                btVector3                               m_com;                  // COM
 
308
                btMatrix3x3                             m_rot;                  // Rotation
 
309
                btMatrix3x3                             m_scl;                  // Scale
 
310
                btMatrix3x3                             m_aqq;                  // Base scaling
 
311
        };
 
312
        /* Cluster              */ 
 
313
        struct  Cluster
 
314
        {
 
315
                tScalarArray                            m_masses;
 
316
                btAlignedObjectArray<Node*>     m_nodes;                
 
317
                tVector3Array                           m_framerefs;
 
318
                btTransform                                     m_framexform;
 
319
                btScalar                                        m_idmass;
 
320
                btScalar                                        m_imass;
 
321
                btMatrix3x3                                     m_locii;
 
322
                btMatrix3x3                                     m_invwi;
 
323
                btVector3                                       m_com;
 
324
                btVector3                                       m_vimpulses[2];
 
325
                btVector3                                       m_dimpulses[2];
 
326
                int                                                     m_nvimpulses;
 
327
                int                                                     m_ndimpulses;
 
328
                btVector3                                       m_lv;
 
329
                btVector3                                       m_av;
 
330
                btDbvtNode*                                     m_leaf;
 
331
                btScalar                                        m_ndamping;     /* Node damping         */ 
 
332
                btScalar                                        m_ldamping;     /* Linear damping       */ 
 
333
                btScalar                                        m_adamping;     /* Angular damping      */ 
 
334
                btScalar                                        m_matching;
 
335
                btScalar                                        m_maxSelfCollisionImpulse;
 
336
                btScalar                                        m_selfCollisionImpulseFactor;
 
337
                bool                                            m_containsAnchor;
 
338
                bool                                            m_collide;
 
339
                int                                                     m_clusterIndex;
 
340
                Cluster() : m_leaf(0),m_ndamping(0),m_ldamping(0),m_adamping(0),m_matching(0) 
 
341
                ,m_maxSelfCollisionImpulse(100.f),
 
342
                m_selfCollisionImpulseFactor(0.01f),
 
343
                m_containsAnchor(false)
 
344
                {}
 
345
        };
 
346
        /* Impulse              */ 
 
347
        struct  Impulse
 
348
        {
 
349
                btVector3                                       m_velocity;
 
350
                btVector3                                       m_drift;
 
351
                int                                                     m_asVelocity:1;
 
352
                int                                                     m_asDrift:1;
 
353
                Impulse() : m_velocity(0,0,0),m_drift(0,0,0),m_asVelocity(0),m_asDrift(0)       {}
 
354
                Impulse                                         operator -() const
 
355
                {
 
356
                        Impulse i=*this;
 
357
                        i.m_velocity=-i.m_velocity;
 
358
                        i.m_drift=-i.m_drift;
 
359
                        return(i);
 
360
                }
 
361
                Impulse                                         operator*(btScalar x) const
 
362
                {
 
363
                        Impulse i=*this;
 
364
                        i.m_velocity*=x;
 
365
                        i.m_drift*=x;
 
366
                        return(i);
 
367
                }
 
368
        };
 
369
        /* Body                 */ 
 
370
        struct  Body
 
371
        {
 
372
                Cluster*                        m_soft;
 
373
                btRigidBody*            m_rigid;
 
374
                btCollisionObject*      m_collisionObject;
 
375
 
 
376
                Body() : m_soft(0),m_rigid(0),m_collisionObject(0)                              {}
 
377
                Body(Cluster* p) : m_soft(p),m_rigid(0),m_collisionObject(0)    {}
 
378
                Body(btCollisionObject* colObj) : m_soft(0),m_collisionObject(colObj)
 
379
                {
 
380
                        m_rigid = btRigidBody::upcast(m_collisionObject);
 
381
                }
 
382
 
 
383
                void                                            activate() const
 
384
                {
 
385
                        if(m_rigid) 
 
386
                                m_rigid->activate();
 
387
                        if (m_collisionObject)
 
388
                                m_collisionObject->activate();
 
389
 
 
390
                }
 
391
                const btMatrix3x3&                      invWorldInertia() const
 
392
                {
 
393
                        static const btMatrix3x3        iwi(0,0,0,0,0,0,0,0,0);
 
394
                        if(m_rigid) return(m_rigid->getInvInertiaTensorWorld());
 
395
                        if(m_soft)      return(m_soft->m_invwi);
 
396
                        return(iwi);
 
397
                }
 
398
                btScalar                                        invMass() const
 
399
                {
 
400
                        if(m_rigid) return(m_rigid->getInvMass());
 
401
                        if(m_soft)      return(m_soft->m_imass);
 
402
                        return(0);
 
403
                }
 
404
                const btTransform&                      xform() const
 
405
                {
 
406
                        static const btTransform        identity=btTransform::getIdentity();            
 
407
                        if(m_collisionObject) return(m_collisionObject->getWorldTransform());
 
408
                        if(m_soft)      return(m_soft->m_framexform);
 
409
                        return(identity);
 
410
                }
 
411
                btVector3                                       linearVelocity() const
 
412
                {
 
413
                        if(m_rigid) return(m_rigid->getLinearVelocity());
 
414
                        if(m_soft)      return(m_soft->m_lv);
 
415
                        return(btVector3(0,0,0));
 
416
                }
 
417
                btVector3                                       angularVelocity(const btVector3& rpos) const
 
418
                {                       
 
419
                        if(m_rigid) return(btCross(m_rigid->getAngularVelocity(),rpos));
 
420
                        if(m_soft)      return(btCross(m_soft->m_av,rpos));
 
421
                        return(btVector3(0,0,0));
 
422
                }
 
423
                btVector3                                       angularVelocity() const
 
424
                {                       
 
425
                        if(m_rigid) return(m_rigid->getAngularVelocity());
 
426
                        if(m_soft)      return(m_soft->m_av);
 
427
                        return(btVector3(0,0,0));
 
428
                }
 
429
                btVector3                                       velocity(const btVector3& rpos) const
 
430
                {
 
431
                        return(linearVelocity()+angularVelocity(rpos));
 
432
                }
 
433
                void                                            applyVImpulse(const btVector3& impulse,const btVector3& rpos) const
 
434
                {
 
435
                        if(m_rigid)     m_rigid->applyImpulse(impulse,rpos);
 
436
                        if(m_soft)      btSoftBody::clusterVImpulse(m_soft,rpos,impulse);
 
437
                }
 
438
                void                                            applyDImpulse(const btVector3& impulse,const btVector3& rpos) const
 
439
                {
 
440
                        if(m_rigid)     m_rigid->applyImpulse(impulse,rpos);
 
441
                        if(m_soft)      btSoftBody::clusterDImpulse(m_soft,rpos,impulse);
 
442
                }               
 
443
                void                                            applyImpulse(const Impulse& impulse,const btVector3& rpos) const
 
444
                {
 
445
                        if(impulse.m_asVelocity)        
 
446
                        {
 
447
//                              printf("impulse.m_velocity = %f,%f,%f\n",impulse.m_velocity.getX(),impulse.m_velocity.getY(),impulse.m_velocity.getZ());
 
448
                                applyVImpulse(impulse.m_velocity,rpos);
 
449
                        }
 
450
                        if(impulse.m_asDrift)           
 
451
                        {
 
452
//                              printf("impulse.m_drift = %f,%f,%f\n",impulse.m_drift.getX(),impulse.m_drift.getY(),impulse.m_drift.getZ());
 
453
                                applyDImpulse(impulse.m_drift,rpos);
 
454
                        }
 
455
                }
 
456
                void                                            applyVAImpulse(const btVector3& impulse) const
 
457
                {
 
458
                        if(m_rigid)     m_rigid->applyTorqueImpulse(impulse);
 
459
                        if(m_soft)      btSoftBody::clusterVAImpulse(m_soft,impulse);
 
460
                }
 
461
                void                                            applyDAImpulse(const btVector3& impulse) const
 
462
                {
 
463
                        if(m_rigid)     m_rigid->applyTorqueImpulse(impulse);
 
464
                        if(m_soft)      btSoftBody::clusterDAImpulse(m_soft,impulse);
 
465
                }
 
466
                void                                            applyAImpulse(const Impulse& impulse) const
 
467
                {
 
468
                        if(impulse.m_asVelocity)        applyVAImpulse(impulse.m_velocity);
 
469
                        if(impulse.m_asDrift)           applyDAImpulse(impulse.m_drift);
 
470
                }
 
471
                void                                            applyDCImpulse(const btVector3& impulse) const
 
472
                {
 
473
                        if(m_rigid)     m_rigid->applyCentralImpulse(impulse);
 
474
                        if(m_soft)      btSoftBody::clusterDCImpulse(m_soft,impulse);
 
475
                }
 
476
        };
 
477
        /* Joint                */ 
 
478
        struct  Joint
 
479
        {
 
480
                struct eType { enum _ {
 
481
                        Linear=0,
 
482
                        Angular,
 
483
                        Contact
 
484
                };};
 
485
                struct Specs
 
486
                {
 
487
                        Specs() : erp(1),cfm(1),split(1) {}
 
488
                        btScalar        erp;
 
489
                        btScalar        cfm;
 
490
                        btScalar        split;
 
491
                };
 
492
                Body                                            m_bodies[2];
 
493
                btVector3                                       m_refs[2];
 
494
                btScalar                                        m_cfm;
 
495
                btScalar                                        m_erp;
 
496
                btScalar                                        m_split;
 
497
                btVector3                                       m_drift;
 
498
                btVector3                                       m_sdrift;
 
499
                btMatrix3x3                                     m_massmatrix;
 
500
                bool                                            m_delete;
 
501
                virtual                                         ~Joint() {}
 
502
                Joint() : m_delete(false) {}
 
503
                virtual void                            Prepare(btScalar dt,int iterations);
 
504
                virtual void                            Solve(btScalar dt,btScalar sor)=0;
 
505
                virtual void                            Terminate(btScalar dt)=0;
 
506
                virtual eType::_                        Type() const=0;
 
507
        };
 
508
        /* LJoint               */ 
 
509
        struct  LJoint : Joint
 
510
        {
 
511
                struct Specs : Joint::Specs
 
512
                {
 
513
                        btVector3       position;
 
514
                };              
 
515
                btVector3                                       m_rpos[2];
 
516
                void                                            Prepare(btScalar dt,int iterations);
 
517
                void                                            Solve(btScalar dt,btScalar sor);
 
518
                void                                            Terminate(btScalar dt);
 
519
                eType::_                                        Type() const { return(eType::Linear); }
 
520
        };
 
521
        /* AJoint               */ 
 
522
        struct  AJoint : Joint
 
523
        {
 
524
                struct IControl
 
525
                {
 
526
                        virtual void                    Prepare(AJoint*)                                {}
 
527
                        virtual btScalar                Speed(AJoint*,btScalar current) { return(current); }
 
528
                        static IControl*                Default()                                               { static IControl def;return(&def); }
 
529
                };
 
530
                struct Specs : Joint::Specs
 
531
                {
 
532
                        Specs() : icontrol(IControl::Default()) {}
 
533
                        btVector3       axis;
 
534
                        IControl*       icontrol;
 
535
                };              
 
536
                btVector3                                       m_axis[2];
 
537
                IControl*                                       m_icontrol;
 
538
                void                                            Prepare(btScalar dt,int iterations);
 
539
                void                                            Solve(btScalar dt,btScalar sor);
 
540
                void                                            Terminate(btScalar dt);
 
541
                eType::_                                        Type() const { return(eType::Angular); }
 
542
        };
 
543
        /* CJoint               */ 
 
544
        struct  CJoint : Joint
 
545
        {               
 
546
                int                                                     m_life;
 
547
                int                                                     m_maxlife;
 
548
                btVector3                                       m_rpos[2];
 
549
                btVector3                                       m_normal;
 
550
                btScalar                                        m_friction;
 
551
                void                                            Prepare(btScalar dt,int iterations);
 
552
                void                                            Solve(btScalar dt,btScalar sor);
 
553
                void                                            Terminate(btScalar dt);
 
554
                eType::_                                        Type() const { return(eType::Contact); }
 
555
        };
 
556
        /* Config               */ 
 
557
        struct  Config
 
558
        {
 
559
                eAeroModel::_                   aeromodel;              // Aerodynamic model (default: V_Point)
 
560
                btScalar                                kVCF;                   // Velocities correction factor (Baumgarte)
 
561
                btScalar                                kDP;                    // Damping coefficient [0,1]
 
562
                btScalar                                kDG;                    // Drag coefficient [0,+inf]
 
563
                btScalar                                kLF;                    // Lift coefficient [0,+inf]
 
564
                btScalar                                kPR;                    // Pressure coefficient [-inf,+inf]
 
565
                btScalar                                kVC;                    // Volume conversation coefficient [0,+inf]
 
566
                btScalar                                kDF;                    // Dynamic friction coefficient [0,1]
 
567
                btScalar                                kMT;                    // Pose matching coefficient [0,1]              
 
568
                btScalar                                kCHR;                   // Rigid contacts hardness [0,1]
 
569
                btScalar                                kKHR;                   // Kinetic contacts hardness [0,1]
 
570
                btScalar                                kSHR;                   // Soft contacts hardness [0,1]
 
571
                btScalar                                kAHR;                   // Anchors hardness [0,1]
 
572
                btScalar                                kSRHR_CL;               // Soft vs rigid hardness [0,1] (cluster only)
 
573
                btScalar                                kSKHR_CL;               // Soft vs kinetic hardness [0,1] (cluster only)
 
574
                btScalar                                kSSHR_CL;               // Soft vs soft hardness [0,1] (cluster only)
 
575
                btScalar                                kSR_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
 
576
                btScalar                                kSK_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
 
577
                btScalar                                kSS_SPLT_CL;    // Soft vs rigid impulse split [0,1] (cluster only)
 
578
                btScalar                                maxvolume;              // Maximum volume ratio for pose
 
579
                btScalar                                timescale;              // Time scale
 
580
                int                                             viterations;    // Velocities solver iterations
 
581
                int                                             piterations;    // Positions solver iterations
 
582
                int                                             diterations;    // Drift solver iterations
 
583
                int                                             citerations;    // Cluster solver iterations
 
584
                int                                             collisions;             // Collisions flags
 
585
                tVSolverArray                   m_vsequence;    // Velocity solvers sequence
 
586
                tPSolverArray                   m_psequence;    // Position solvers sequence
 
587
                tPSolverArray                   m_dsequence;    // Drift solvers sequence
 
588
        };
 
589
        /* SolverState  */ 
 
590
        struct  SolverState
 
591
        {
 
592
                btScalar                                sdt;                    // dt*timescale
 
593
                btScalar                                isdt;                   // 1/sdt
 
594
                btScalar                                velmrg;                 // velocity margin
 
595
                btScalar                                radmrg;                 // radial margin
 
596
                btScalar                                updmrg;                 // Update margin
 
597
        };      
 
598
        /// RayFromToCaster takes a ray from, ray to (instead of direction!)
 
599
        struct  RayFromToCaster : btDbvt::ICollide
 
600
        {
 
601
                btVector3                       m_rayFrom;
 
602
                btVector3                       m_rayTo;
 
603
                btVector3                       m_rayNormalizedDirection;
 
604
                btScalar                        m_mint;
 
605
                Face*                           m_face;
 
606
                int                                     m_tests;
 
607
                RayFromToCaster(const btVector3& rayFrom,const btVector3& rayTo,btScalar mxt);
 
608
                void                                    Process(const btDbvtNode* leaf);
 
609
 
 
610
                static inline btScalar  rayFromToTriangle(const btVector3& rayFrom,
 
611
                        const btVector3& rayTo,
 
612
                        const btVector3& rayNormalizedDirection,
 
613
                        const btVector3& a,
 
614
                        const btVector3& b,
 
615
                        const btVector3& c,
 
616
                        btScalar maxt=SIMD_INFINITY);
 
617
        };
 
618
 
 
619
        //
 
620
        // Typedefs
 
621
        //
 
622
 
 
623
        typedef void                                                            (*psolver_t)(btSoftBody*,btScalar,btScalar);
 
624
        typedef void                                                            (*vsolver_t)(btSoftBody*,btScalar);
 
625
        typedef btAlignedObjectArray<Cluster*>          tClusterArray;
 
626
        typedef btAlignedObjectArray<Note>                      tNoteArray;
 
627
        typedef btAlignedObjectArray<Node>                      tNodeArray;
 
628
        typedef btAlignedObjectArray<btDbvtNode*>       tLeafArray;
 
629
        typedef btAlignedObjectArray<Link>                      tLinkArray;
 
630
        typedef btAlignedObjectArray<Face>                      tFaceArray;
 
631
        typedef btAlignedObjectArray<Tetra>                     tTetraArray;
 
632
        typedef btAlignedObjectArray<Anchor>            tAnchorArray;
 
633
        typedef btAlignedObjectArray<RContact>          tRContactArray;
 
634
        typedef btAlignedObjectArray<SContact>          tSContactArray;
 
635
        typedef btAlignedObjectArray<Material*>         tMaterialArray;
 
636
        typedef btAlignedObjectArray<Joint*>            tJointArray;
 
637
        typedef btAlignedObjectArray<btSoftBody*>       tSoftBodyArray; 
 
638
 
 
639
        //
 
640
        // Fields
 
641
        //
 
642
 
 
643
        Config                                  m_cfg;                  // Configuration
 
644
        SolverState                             m_sst;                  // Solver state
 
645
        Pose                                    m_pose;                 // Pose
 
646
        void*                                   m_tag;                  // User data
 
647
        btSoftBodyWorldInfo*    m_worldInfo;    // World info
 
648
        tNoteArray                              m_notes;                // Notes
 
649
        tNodeArray                              m_nodes;                // Nodes
 
650
        tLinkArray                              m_links;                // Links
 
651
        tFaceArray                              m_faces;                // Faces
 
652
        tTetraArray                             m_tetras;               // Tetras
 
653
        tAnchorArray                    m_anchors;              // Anchors
 
654
        tRContactArray                  m_rcontacts;    // Rigid contacts
 
655
        tSContactArray                  m_scontacts;    // Soft contacts
 
656
        tJointArray                             m_joints;               // Joints
 
657
        tMaterialArray                  m_materials;    // Materials
 
658
        btScalar                                m_timeacc;              // Time accumulator
 
659
        btVector3                               m_bounds[2];    // Spatial bounds       
 
660
        bool                                    m_bUpdateRtCst; // Update runtime constants
 
661
        btDbvt                                  m_ndbvt;                // Nodes tree
 
662
        btDbvt                                  m_fdbvt;                // Faces tree
 
663
        btDbvt                                  m_cdbvt;                // Clusters tree
 
664
        tClusterArray                   m_clusters;             // Clusters
 
665
 
 
666
        btAlignedObjectArray<bool>m_clusterConnectivity;//cluster connectivity, for self-collision
 
667
 
 
668
        btTransform                     m_initialWorldTransform;
 
669
 
 
670
        btVector3                       m_windVelocity;
 
671
        //
 
672
        // Api
 
673
        //
 
674
 
 
675
        /* ctor                                                                                                                                 */ 
 
676
        btSoftBody(     btSoftBodyWorldInfo* worldInfo,int node_count,          const btVector3* x,             const btScalar* m);
 
677
 
 
678
        /* ctor                                                                                                                                 */ 
 
679
        btSoftBody(     btSoftBodyWorldInfo* worldInfo);
 
680
 
 
681
        void    initDefaults();
 
682
 
 
683
        /* dtor                                                                                                                                 */ 
 
684
        virtual ~btSoftBody();
 
685
        /* Check for existing link                                                                                              */ 
 
686
 
 
687
        btAlignedObjectArray<int>       m_userIndexMapping;
 
688
 
 
689
        btSoftBodyWorldInfo*    getWorldInfo()
 
690
        {
 
691
                return m_worldInfo;
 
692
        }
 
693
 
 
694
        ///@todo: avoid internal softbody shape hack and move collision code to collision library
 
695
        virtual void    setCollisionShape(btCollisionShape* collisionShape)
 
696
        {
 
697
                
 
698
        }
 
699
 
 
700
        bool                            checkLink(      int node0,
 
701
                int node1) const;
 
702
        bool                            checkLink(      const Node* node0,
 
703
                const Node* node1) const;
 
704
        /* Check for existring face                                                                                             */ 
 
705
        bool                            checkFace(      int node0,
 
706
                int node1,
 
707
                int node2) const;
 
708
        /* Append material                                                                                                              */ 
 
709
        Material*                       appendMaterial();
 
710
        /* Append note                                                                                                                  */ 
 
711
        void                            appendNote(     const char* text,
 
712
                const btVector3& o,
 
713
                const btVector4& c=btVector4(1,0,0,0),
 
714
                Node* n0=0,
 
715
                Node* n1=0,
 
716
                Node* n2=0,
 
717
                Node* n3=0);
 
718
        void                            appendNote(     const char* text,
 
719
                const btVector3& o,
 
720
                Node* feature);
 
721
        void                            appendNote(     const char* text,
 
722
                const btVector3& o,
 
723
                Link* feature);
 
724
        void                            appendNote(     const char* text,
 
725
                const btVector3& o,
 
726
                Face* feature);
 
727
        /* Append node                                                                                                                  */ 
 
728
        void                            appendNode(     const btVector3& x,btScalar m);
 
729
        /* Append link                                                                                                                  */ 
 
730
        void                            appendLink(int model=-1,Material* mat=0);
 
731
        void                            appendLink(     int node0,
 
732
                int node1,
 
733
                Material* mat=0,
 
734
                bool bcheckexist=false);
 
735
        void                            appendLink(     Node* node0,
 
736
                Node* node1,
 
737
                Material* mat=0,
 
738
                bool bcheckexist=false);
 
739
        /* Append face                                                                                                                  */ 
 
740
        void                            appendFace(int model=-1,Material* mat=0);
 
741
        void                            appendFace(     int node0,
 
742
                int node1,
 
743
                int node2,
 
744
                Material* mat=0);
 
745
        void                    appendTetra(int model,Material* mat);
 
746
        //
 
747
        void                    appendTetra(int node0,
 
748
                                                                                int node1,
 
749
                                                                                int node2,
 
750
                                                                                int node3,
 
751
                                                                                Material* mat=0);
 
752
 
 
753
 
 
754
        /* Append anchor                                                                                                                */ 
 
755
        void                            appendAnchor(   int node,
 
756
                btRigidBody* body, bool disableCollisionBetweenLinkedBodies=false,btScalar influence = 1);
 
757
        void                    appendAnchor(int node,btRigidBody* body, const btVector3& localPivot,bool disableCollisionBetweenLinkedBodies=false,btScalar influence = 1);
 
758
        /* Append linear joint                                                                                                  */ 
 
759
        void                            appendLinearJoint(const LJoint::Specs& specs,Cluster* body0,Body body1);
 
760
        void                            appendLinearJoint(const LJoint::Specs& specs,Body body=Body());
 
761
        void                            appendLinearJoint(const LJoint::Specs& specs,btSoftBody* body);
 
762
        /* Append linear joint                                                                                                  */ 
 
763
        void                            appendAngularJoint(const AJoint::Specs& specs,Cluster* body0,Body body1);
 
764
        void                            appendAngularJoint(const AJoint::Specs& specs,Body body=Body());
 
765
        void                            appendAngularJoint(const AJoint::Specs& specs,btSoftBody* body);
 
766
        /* Add force (or gravity) to the entire body                                                    */ 
 
767
        void                            addForce(               const btVector3& force);
 
768
        /* Add force (or gravity) to a node of the body                                                 */ 
 
769
        void                            addForce(               const btVector3& force,
 
770
                int node);
 
771
        /* Add velocity to the entire body                                                                              */ 
 
772
        void                            addVelocity(    const btVector3& velocity);
 
773
 
 
774
        /* Set velocity for the entire body                                                                             */ 
 
775
        void                            setVelocity(    const btVector3& velocity);
 
776
 
 
777
        /* Add velocity to a node of the body                                                                   */ 
 
778
        void                            addVelocity(    const btVector3& velocity,
 
779
                int node);
 
780
        /* Set mass                                                                                                                             */ 
 
781
        void                            setMass(                int node,
 
782
                btScalar mass);
 
783
        /* Get mass                                                                                                                             */ 
 
784
        btScalar                        getMass(                int node) const;
 
785
        /* Get total mass                                                                                                               */ 
 
786
        btScalar                        getTotalMass() const;
 
787
        /* Set total mass (weighted by previous masses)                                                 */ 
 
788
        void                            setTotalMass(   btScalar mass,
 
789
                bool fromfaces=false);
 
790
        /* Set total density                                                                                                    */ 
 
791
        void                            setTotalDensity(btScalar density);
 
792
        /* Set volume mass (using tetrahedrons)                                                                 */
 
793
        void                            setVolumeMass(          btScalar mass);
 
794
        /* Set volume density (using tetrahedrons)                                                              */
 
795
        void                            setVolumeDensity(       btScalar density);
 
796
        /* Transform                                                                                                                    */ 
 
797
        void                            transform(              const btTransform& trs);
 
798
        /* Translate                                                                                                                    */ 
 
799
        void                            translate(              const btVector3& trs);
 
800
        /* Rotate                                                                                                                       */ 
 
801
        void                            rotate( const btQuaternion& rot);
 
802
        /* Scale                                                                                                                                */ 
 
803
        void                            scale(  const btVector3& scl);
 
804
        /* Set current state as pose                                                                                    */ 
 
805
        void                            setPose(                bool bvolume,
 
806
                bool bframe);
 
807
        /* Return the volume                                                                                                    */ 
 
808
        btScalar                        getVolume() const;
 
809
        /* Cluster count                                                                                                                */ 
 
810
        int                                     clusterCount() const;
 
811
        /* Cluster center of mass                                                                                               */ 
 
812
        static btVector3        clusterCom(const Cluster* cluster);
 
813
        btVector3                       clusterCom(int cluster) const;
 
814
        /* Cluster velocity at rpos                                                                                             */ 
 
815
        static btVector3        clusterVelocity(const Cluster* cluster,const btVector3& rpos);
 
816
        /* Cluster impulse                                                                                                              */ 
 
817
        static void                     clusterVImpulse(Cluster* cluster,const btVector3& rpos,const btVector3& impulse);
 
818
        static void                     clusterDImpulse(Cluster* cluster,const btVector3& rpos,const btVector3& impulse);
 
819
        static void                     clusterImpulse(Cluster* cluster,const btVector3& rpos,const Impulse& impulse);
 
820
        static void                     clusterVAImpulse(Cluster* cluster,const btVector3& impulse);
 
821
        static void                     clusterDAImpulse(Cluster* cluster,const btVector3& impulse);
 
822
        static void                     clusterAImpulse(Cluster* cluster,const Impulse& impulse);
 
823
        static void                     clusterDCImpulse(Cluster* cluster,const btVector3& impulse);
 
824
        /* Generate bending constraints based on distance in the adjency graph  */ 
 
825
        int                                     generateBendingConstraints(     int distance,
 
826
                Material* mat=0);
 
827
        /* Randomize constraints to reduce solver bias                                                  */ 
 
828
        void                            randomizeConstraints();
 
829
        /* Release clusters                                                                                                             */ 
 
830
        void                            releaseCluster(int index);
 
831
        void                            releaseClusters();
 
832
        /* Generate clusters (K-mean)                                                                                   */ 
 
833
        ///generateClusters with k=0 will create a convex cluster for each tetrahedron or triangle
 
834
        ///otherwise an approximation will be used (better performance)
 
835
        int                                     generateClusters(int k,int maxiterations=8192);
 
836
        /* Refine                                                                                                                               */ 
 
837
        void                            refine(ImplicitFn* ifn,btScalar accurary,bool cut);
 
838
        /* CutLink                                                                                                                              */ 
 
839
        bool                            cutLink(int node0,int node1,btScalar position);
 
840
        bool                            cutLink(const Node* node0,const Node* node1,btScalar position);
 
841
 
 
842
        ///Ray casting using rayFrom and rayTo in worldspace, (not direction!)
 
843
        bool                            rayTest(const btVector3& rayFrom,
 
844
                const btVector3& rayTo,
 
845
                sRayCast& results);
 
846
        /* Solver presets                                                                                                               */ 
 
847
        void                            setSolver(eSolverPresets::_ preset);
 
848
        /* predictMotion                                                                                                                */ 
 
849
        void                            predictMotion(btScalar dt);
 
850
        /* solveConstraints                                                                                                             */ 
 
851
        void                            solveConstraints();
 
852
        /* staticSolve                                                                                                                  */ 
 
853
        void                            staticSolve(int iterations);
 
854
        /* solveCommonConstraints                                                                                               */ 
 
855
        static void                     solveCommonConstraints(btSoftBody** bodies,int count,int iterations);
 
856
        /* solveClusters                                                                                                                */ 
 
857
        static void                     solveClusters(const btAlignedObjectArray<btSoftBody*>& bodies);
 
858
        /* integrateMotion                                                                                                              */ 
 
859
        void                            integrateMotion();
 
860
        /* defaultCollisionHandlers                                                                                             */ 
 
861
        void                            defaultCollisionHandler(btCollisionObject* pco);
 
862
        void                            defaultCollisionHandler(btSoftBody* psb);
 
863
 
 
864
 
 
865
 
 
866
        //
 
867
        // Functionality to deal with new accelerated solvers.
 
868
        //
 
869
 
 
870
        /**
 
871
         * Set a wind velocity for interaction with the air.
 
872
         */
 
873
        void setWindVelocity( const btVector3 &velocity );
 
874
 
 
875
 
 
876
        /**
 
877
         * Return the wind velocity for interaction with the air.
 
878
         */
 
879
        const btVector3& getWindVelocity();
 
880
 
 
881
        //
 
882
        // Set the solver that handles this soft body
 
883
        // Should not be allowed to get out of sync with reality
 
884
        // Currently called internally on addition to the world
 
885
        void setSoftBodySolver( btSoftBodySolver *softBodySolver )
 
886
        {
 
887
                m_softBodySolver = softBodySolver;
 
888
        }
 
889
 
 
890
        //
 
891
        // Return the solver that handles this soft body
 
892
        // 
 
893
        btSoftBodySolver *getSoftBodySolver()
 
894
        {
 
895
                return m_softBodySolver;
 
896
        }
 
897
 
 
898
        //
 
899
        // Return the solver that handles this soft body
 
900
        // 
 
901
        btSoftBodySolver *getSoftBodySolver() const
 
902
        {
 
903
                return m_softBodySolver;
 
904
        }
 
905
 
 
906
 
 
907
        //
 
908
        // Cast
 
909
        //
 
910
 
 
911
        static const btSoftBody*        upcast(const btCollisionObject* colObj)
 
912
        {
 
913
                if (colObj->getInternalType()==CO_SOFT_BODY)
 
914
                        return (const btSoftBody*)colObj;
 
915
                return 0;
 
916
        }
 
917
        static btSoftBody*                      upcast(btCollisionObject* colObj)
 
918
        {
 
919
                if (colObj->getInternalType()==CO_SOFT_BODY)
 
920
                        return (btSoftBody*)colObj;
 
921
                return 0;
 
922
        }
 
923
 
 
924
        //
 
925
        // ::btCollisionObject
 
926
        //
 
927
 
 
928
        virtual void getAabb(btVector3& aabbMin,btVector3& aabbMax) const
 
929
        {
 
930
                aabbMin = m_bounds[0];
 
931
                aabbMax = m_bounds[1];
 
932
        }
 
933
        //
 
934
        // Private
 
935
        //
 
936
        void                            pointersToIndices();
 
937
        void                            indicesToPointers(const int* map=0);
 
938
 
 
939
        int                                     rayTest(const btVector3& rayFrom,const btVector3& rayTo,
 
940
                btScalar& mint,eFeature::_& feature,int& index,bool bcountonly) const;
 
941
        void                            initializeFaceTree();
 
942
        btVector3                       evaluateCom() const;
 
943
        bool                            checkContact(btCollisionObject* colObj,const btVector3& x,btScalar margin,btSoftBody::sCti& cti) const;
 
944
        void                            updateNormals();
 
945
        void                            updateBounds();
 
946
        void                            updatePose();
 
947
        void                            updateConstants();
 
948
        void                            initializeClusters();
 
949
        void                            updateClusters();
 
950
        void                            cleanupClusters();
 
951
        void                            prepareClusters(int iterations);
 
952
        void                            solveClusters(btScalar sor);
 
953
        void                            applyClusters(bool drift);
 
954
        void                            dampClusters();
 
955
        void                            applyForces();  
 
956
        static void                     PSolve_Anchors(btSoftBody* psb,btScalar kst,btScalar ti);
 
957
        static void                     PSolve_RContacts(btSoftBody* psb,btScalar kst,btScalar ti);
 
958
        static void                     PSolve_SContacts(btSoftBody* psb,btScalar,btScalar ti);
 
959
        static void                     PSolve_Links(btSoftBody* psb,btScalar kst,btScalar ti);
 
960
        static void                     VSolve_Links(btSoftBody* psb,btScalar kst);
 
961
        static psolver_t        getSolver(ePSolver::_ solver);
 
962
        static vsolver_t        getSolver(eVSolver::_ solver);
 
963
 
 
964
 
 
965
        virtual int     calculateSerializeBufferSize()  const;
 
966
 
 
967
        ///fills the dataBuffer and returns the struct name (and 0 on failure)
 
968
        virtual const char*     serialize(void* dataBuffer,  class btSerializer* serializer) const;
 
969
 
 
970
        //virtual void serializeSingleObject(class btSerializer* serializer) const;
 
971
 
 
972
 
 
973
};
 
974
 
 
975
 
 
976
 
 
977
 
 
978
#endif //_BT_SOFT_BODY_H