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

« back to all changes in this revision

Viewing changes to tests/bullet/src/BulletMultiThreaded/GpuSoftBodySolvers/CPU/btSoftBodySolver_CPU.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
 
 
16
#ifndef BT_ACCELERATED_SOFT_BODY_CPU_SOLVER_H
 
17
#define BT_ACCELERATED_SOFT_BODY_CPU_SOLVER_H
 
18
 
 
19
#include "vectormath/vmInclude.h"
 
20
#include "BulletSoftBody/btSoftBodySolvers.h"
 
21
#include "BulletSoftBody/btSoftBodySolverVertexBuffer.h"
 
22
#include "BulletMultiThreaded/GpuSoftBodySolvers/CPU/btSoftBodySolverData.h"
 
23
 
 
24
struct btCPUCollisionShapeDescription
 
25
{
 
26
        int softBodyIdentifier;
 
27
        int collisionShapeType;
 
28
        Vectormath::Aos::Transform3 shapeTransform;
 
29
        union
 
30
        {
 
31
                struct Sphere
 
32
                {
 
33
                        float radius;
 
34
                } sphere;
 
35
                struct Capsule
 
36
                {
 
37
                        float radius;
 
38
                        float halfHeight;
 
39
                        int upAxis;
 
40
                } capsule;
 
41
        } shapeInformation;
 
42
        
 
43
        float margin;
 
44
        float friction;
 
45
        Vectormath::Aos::Vector3 linearVelocity;
 
46
        Vectormath::Aos::Vector3 angularVelocity;
 
47
 
 
48
        btCPUCollisionShapeDescription()
 
49
        {
 
50
                collisionShapeType = 0;
 
51
                margin = 0;
 
52
                friction = 0;
 
53
        }
 
54
};
 
55
 
 
56
class btCPUSoftBodySolver : public btSoftBodySolver
 
57
{
 
58
protected:
 
59
        /**
 
60
         * Entry in the collision shape array.
 
61
         * Specifies the shape type, the transform matrix and the necessary details of the collisionShape.
 
62
         */
 
63
 
 
64
 
 
65
        // Public because output classes need it. This is a better encapsulation to break in the short term
 
66
        // Than having the solvers themselves need dependencies on DX, CL etc unnecessarily
 
67
public:
 
68
        
 
69
        struct CollisionObjectIndices
 
70
        {
 
71
                CollisionObjectIndices( int f, int e )
 
72
                {
 
73
                        firstObject = f;
 
74
                        endObject = e;
 
75
                }
 
76
 
 
77
                int firstObject;
 
78
                int endObject;
 
79
        };
 
80
 
 
81
        /**
 
82
         * SoftBody class to maintain information about a soft body instance
 
83
         * within a solver.
 
84
         * This data addresses the main solver arrays.
 
85
         */
 
86
        class btAcceleratedSoftBodyInterface
 
87
        {
 
88
        protected:
 
89
                /** Current number of vertices that are part of this cloth */
 
90
                int m_numVertices;
 
91
                /** Maximum number of vertices allocated to be part of this cloth */
 
92
                int m_maxVertices;
 
93
                /** Current number of triangles that are part of this cloth */
 
94
                int m_numTriangles;
 
95
                /** Maximum number of triangles allocated to be part of this cloth */
 
96
                int m_maxTriangles;
 
97
                /** Index of first vertex in the world allocated to this cloth */
 
98
                int m_firstVertex;
 
99
                /** Index of first triangle in the world allocated to this cloth */
 
100
                int m_firstTriangle;
 
101
                /** Index of first link in the world allocated to this cloth */
 
102
                int m_firstLink;
 
103
                /** Maximum number of links allocated to this cloth */
 
104
                int m_maxLinks;
 
105
                /** Current number of links allocated to this cloth */
 
106
                int m_numLinks;
 
107
 
 
108
                /** The actual soft body this data represents */
 
109
                btSoftBody *m_softBody;
 
110
 
 
111
 
 
112
        public:
 
113
                btAcceleratedSoftBodyInterface( btSoftBody *softBody ) :
 
114
                  m_softBody( softBody )
 
115
                {
 
116
                        m_numVertices = 0;
 
117
                        m_maxVertices = 0;
 
118
                        m_numTriangles = 0;
 
119
                        m_maxTriangles = 0;
 
120
                        m_firstVertex = 0;
 
121
                        m_firstTriangle = 0;
 
122
                        m_firstLink = 0;
 
123
                        m_maxLinks = 0;
 
124
                        m_numLinks = 0;
 
125
                }
 
126
                int getNumVertices() const
 
127
                {
 
128
                        return m_numVertices;
 
129
                }
 
130
 
 
131
                int getNumTriangles() const
 
132
                {
 
133
                        return m_numTriangles;
 
134
                }
 
135
 
 
136
                int getMaxVertices() const
 
137
                {
 
138
                        return m_maxVertices;
 
139
                }
 
140
 
 
141
                int getMaxTriangles() const
 
142
                {
 
143
                        return m_maxTriangles;
 
144
                }
 
145
 
 
146
                int getFirstVertex() const
 
147
                {
 
148
                        return m_firstVertex;
 
149
                }
 
150
 
 
151
                int getFirstTriangle() const
 
152
                {
 
153
                        return m_firstTriangle;
 
154
                }
 
155
 
 
156
                /**
 
157
                 * Update the bounds in the btSoftBody object
 
158
                 */
 
159
                void updateBounds( const btVector3 &lowerBound, const btVector3 &upperBound );
 
160
 
 
161
                // TODO: All of these set functions will have to do checks and
 
162
                // update the world because restructuring of the arrays will be necessary
 
163
                // Reasonable use of "friend"?
 
164
                void setNumVertices( int numVertices )
 
165
                {
 
166
                        m_numVertices = numVertices;
 
167
                }       
 
168
                
 
169
                void setNumTriangles( int numTriangles )
 
170
                {
 
171
                        m_numTriangles = numTriangles;
 
172
                }
 
173
 
 
174
                void setMaxVertices( int maxVertices )
 
175
                {
 
176
                        m_maxVertices = maxVertices;
 
177
                }
 
178
 
 
179
                void setMaxTriangles( int maxTriangles )
 
180
                {
 
181
                        m_maxTriangles = maxTriangles;
 
182
                }
 
183
 
 
184
                void setFirstVertex( int firstVertex )
 
185
                {
 
186
                        m_firstVertex = firstVertex;
 
187
                }
 
188
 
 
189
                void setFirstTriangle( int firstTriangle )
 
190
                {
 
191
                        m_firstTriangle = firstTriangle;
 
192
                }
 
193
 
 
194
                void setMaxLinks( int maxLinks )
 
195
                {
 
196
                        m_maxLinks = maxLinks;
 
197
                }
 
198
 
 
199
                void setNumLinks( int numLinks )
 
200
                {
 
201
                        m_numLinks = numLinks;
 
202
                }
 
203
 
 
204
                void setFirstLink( int firstLink )
 
205
                {
 
206
                        m_firstLink = firstLink;
 
207
                }
 
208
 
 
209
                int getMaxLinks() const
 
210
                {
 
211
                        return m_maxLinks;
 
212
                }
 
213
 
 
214
                int getNumLinks() const
 
215
                {
 
216
                        return m_numLinks;
 
217
                }
 
218
 
 
219
                int getFirstLink() const
 
220
                {
 
221
                        return m_firstLink;
 
222
                }
 
223
 
 
224
                btSoftBody* getSoftBody()
 
225
                {
 
226
                        return m_softBody;
 
227
                }
 
228
 
 
229
                const btSoftBody* const getSoftBody() const
 
230
                {
 
231
                        return m_softBody;
 
232
                }
 
233
        };
 
234
        
 
235
        btSoftBodyLinkData m_linkData;
 
236
        btSoftBodyVertexData m_vertexData;
 
237
        btSoftBodyTriangleData m_triangleData;
 
238
 
 
239
protected:
 
240
 
 
241
 
 
242
 
 
243
                
 
244
        /** Variable to define whether we need to update solver constants on the next iteration */
 
245
        bool m_updateSolverConstants;
 
246
 
 
247
        /** 
 
248
         * Cloths owned by this solver.
 
249
         * Only our cloths are in this array.
 
250
         */
 
251
        btAlignedObjectArray< btAcceleratedSoftBodyInterface * > m_softBodySet;
 
252
        
 
253
        /** Acceleration value to be applied to all non-static vertices in the solver. 
 
254
         * Index n is cloth n, array sized by number of cloths in the world not the solver. 
 
255
         */
 
256
        btAlignedObjectArray< Vectormath::Aos::Vector3 > m_perClothAcceleration;
 
257
 
 
258
        /** Wind velocity to be applied normal to all non-static vertices in the solver. 
 
259
         * Index n is cloth n, array sized by number of cloths in the world not the solver. 
 
260
         */
 
261
        btAlignedObjectArray< Vectormath::Aos::Vector3 > m_perClothWindVelocity;
 
262
 
 
263
        /** Velocity damping factor */
 
264
        btAlignedObjectArray< float > m_perClothDampingFactor;
 
265
 
 
266
        /** Velocity correction coefficient */
 
267
        btAlignedObjectArray< float > m_perClothVelocityCorrectionCoefficient;
 
268
 
 
269
        /** Lift parameter for wind effect on cloth. */
 
270
        btAlignedObjectArray< float > m_perClothLiftFactor;
 
271
        
 
272
        /** Drag parameter for wind effect on cloth. */
 
273
        btAlignedObjectArray< float > m_perClothDragFactor;
 
274
 
 
275
        /** Density of the medium in which each cloth sits */
 
276
        btAlignedObjectArray< float > m_perClothMediumDensity;
 
277
 
 
278
        /** 
 
279
         * Collision shape details: pair of index of first collision shape for the cloth and number of collision objects.
 
280
         */
 
281
        btAlignedObjectArray< CollisionObjectIndices > m_perClothCollisionObjects;
 
282
 
 
283
        /** 
 
284
         * Collision shapes being passed across to the cloths in this solver.
 
285
         */
 
286
        btAlignedObjectArray< btCPUCollisionShapeDescription > m_collisionObjectDetails;
 
287
 
 
288
 
 
289
        void prepareCollisionConstraints();
 
290
 
 
291
        Vectormath::Aos::Vector3 ProjectOnAxis( const Vectormath::Aos::Vector3 &v, const Vectormath::Aos::Vector3 &a );
 
292
 
 
293
        void ApplyClampedForce( float solverdt, const Vectormath::Aos::Vector3 &force, const Vectormath::Aos::Vector3 &vertexVelocity, float inverseMass, Vectormath::Aos::Vector3 &vertexForce );
 
294
        
 
295
        float computeTriangleArea( 
 
296
                const Vectormath::Aos::Point3 &vertex0,
 
297
                const Vectormath::Aos::Point3 &vertex1,
 
298
                const Vectormath::Aos::Point3 &vertex2 );
 
299
 
 
300
        void applyForces( float solverdt );
 
301
        void integrate( float solverdt );
 
302
        void updateConstants( float timeStep );
 
303
        int findSoftBodyIndex( const btSoftBody* const softBody );
 
304
        
 
305
        /** Update the bounds of the soft body objects in the solver */
 
306
        void updateBounds();
 
307
 
 
308
 
 
309
public:
 
310
        btCPUSoftBodySolver();
 
311
        
 
312
        virtual ~btCPUSoftBodySolver();
 
313
 
 
314
        
 
315
        virtual SolverTypes getSolverType() const
 
316
        {
 
317
                return CPU_SOLVER;
 
318
        }
 
319
 
 
320
 
 
321
        virtual btSoftBodyLinkData &getLinkData();
 
322
 
 
323
        virtual btSoftBodyVertexData &getVertexData();
 
324
 
 
325
        virtual btSoftBodyTriangleData &getTriangleData();
 
326
 
 
327
 
 
328
 
 
329
        btAcceleratedSoftBodyInterface *findSoftBodyInterface( const btSoftBody* const softBody );
 
330
        const btAcceleratedSoftBodyInterface * const findSoftBodyInterface( const btSoftBody* const softBody ) const;
 
331
 
 
332
 
 
333
 
 
334
        virtual bool checkInitialized();
 
335
 
 
336
        virtual void updateSoftBodies( );
 
337
 
 
338
        virtual void optimize( btAlignedObjectArray< btSoftBody * > &softBodies , bool forceUpdate=false);
 
339
 
 
340
        virtual void copyBackToSoftBodies();
 
341
 
 
342
        virtual void solveConstraints( float solverdt );
 
343
 
 
344
        virtual void predictMotion( float solverdt );
 
345
 
 
346
        virtual void processCollision( btSoftBody *, btCollisionObject* );
 
347
 
 
348
        virtual void processCollision( btSoftBody*, btSoftBody *);
 
349
 
 
350
};
 
351
 
 
352
 
 
353
/** 
 
354
 * Class to manage movement of data from a solver to a given target.
 
355
 * This version is the CPU to CPU generic version.
 
356
 */
 
357
class btSoftBodySolverOutputCPUtoCPU : public btSoftBodySolverOutput
 
358
{
 
359
protected:
 
360
 
 
361
public:
 
362
        btSoftBodySolverOutputCPUtoCPU()
 
363
        {
 
364
        }
 
365
 
 
366
        /** Output current computed vertex data to the vertex buffers for all cloths in the solver. */
 
367
        virtual void copySoftBodyToVertexBuffer( const btSoftBody * const softBody, btVertexBufferDescriptor *vertexBuffer );
 
368
};
 
369
 
 
370
#endif // #ifndef BT_ACCELERATED_SOFT_BODY_CPU_SOLVER_H
 
 
b'\\ No newline at end of file'