[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / BulletSoftBody / btDeformableGravityForce.h
1 /*
2  Written by Xuchen Han <xuchenhan2015@u.northwestern.edu>
3  
4  Bullet Continuous Collision Detection and Physics Library
5  Copyright (c) 2019 Google Inc. http://bulletphysics.org
6  This software is provided 'as-is', without any express or implied warranty.
7  In no event will the authors be held liable for any damages arising from the use of this software.
8  Permission is granted to anyone to use this software for any purpose,
9  including commercial applications, and to alter it and redistribute it freely,
10  subject to the following restrictions:
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_DEFORMABLE_GRAVITY_FORCE_H
17 #define BT_DEFORMABLE_GRAVITY_FORCE_H
18
19 #include "btDeformableLagrangianForce.h"
20
21 class btDeformableGravityForce : public btDeformableLagrangianForce
22 {
23 public:
24         typedef btAlignedObjectArray<btVector3> TVStack;
25         btVector3 m_gravity;
26
27         btDeformableGravityForce(const btVector3& g) : m_gravity(g)
28         {
29         }
30
31         virtual void addScaledForces(btScalar scale, TVStack& force)
32         {
33                 addScaledGravityForce(scale, force);
34         }
35
36         virtual void addScaledExplicitForce(btScalar scale, TVStack& force)
37         {
38                 addScaledGravityForce(scale, force);
39         }
40
41         virtual void addScaledDampingForce(btScalar scale, TVStack& force)
42         {
43         }
44
45         virtual void addScaledElasticForceDifferential(btScalar scale, const TVStack& dx, TVStack& df)
46         {
47         }
48
49         virtual void addScaledDampingForceDifferential(btScalar scale, const TVStack& dv, TVStack& df)
50         {
51         }
52
53         virtual void buildDampingForceDifferentialDiagonal(btScalar scale, TVStack& diagA) {}
54
55         virtual void addScaledGravityForce(btScalar scale, TVStack& force)
56         {
57                 int numNodes = getNumNodes();
58                 btAssert(numNodes <= force.size());
59                 for (int i = 0; i < m_softBodies.size(); ++i)
60                 {
61                         btSoftBody* psb = m_softBodies[i];
62                         if (!psb->isActive())
63                         {
64                                 continue;
65                         }
66                         for (int j = 0; j < psb->m_nodes.size(); ++j)
67                         {
68                                 btSoftBody::Node& n = psb->m_nodes[j];
69                                 size_t id = n.index;
70                                 btScalar mass = (n.m_im == 0) ? 0 : 1. / n.m_im;
71                                 btVector3 scaled_force = scale * m_gravity * mass * m_softBodies[i]->m_gravityFactor;
72                                 force[id] += scaled_force;
73                         }
74                 }
75         }
76
77         virtual btDeformableLagrangianForceType getForceType()
78         {
79                 return BT_GRAVITY_FORCE;
80         }
81
82         // the gravitational potential energy
83         virtual double totalEnergy(btScalar dt)
84         {
85                 double e = 0;
86                 for (int i = 0; i < m_softBodies.size(); ++i)
87                 {
88                         btSoftBody* psb = m_softBodies[i];
89                         if (!psb->isActive())
90                         {
91                                 continue;
92                         }
93                         for (int j = 0; j < psb->m_nodes.size(); ++j)
94                         {
95                                 const btSoftBody::Node& node = psb->m_nodes[j];
96                                 if (node.m_im > 0)
97                                 {
98                                         e -= m_gravity.dot(node.m_q) / node.m_im;
99                                 }
100                         }
101                 }
102                 return e;
103         }
104 };
105 #endif /* BT_DEFORMABLE_GRAVITY_FORCE_H */