[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / BulletSoftBody / btKrylovSolver.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_KRYLOV_SOLVER_H
17 #define BT_KRYLOV_SOLVER_H
18 #include <iostream>
19 #include <cmath>
20 #include <limits>
21 #include <LinearMath/btAlignedObjectArray.h>
22 #include <LinearMath/btVector3.h>
23 #include <LinearMath/btScalar.h>
24 #include "LinearMath/btQuickprof.h"
25
26 template <class MatrixX>
27 class btKrylovSolver
28 {
29         typedef btAlignedObjectArray<btVector3> TVStack;
30
31 public:
32         int m_maxIterations;
33         btScalar m_tolerance;
34         btKrylovSolver(int maxIterations, btScalar tolerance)
35                 : m_maxIterations(maxIterations), m_tolerance(tolerance)
36         {
37         }
38
39         virtual ~btKrylovSolver() {}
40
41         virtual int solve(MatrixX& A, TVStack& x, const TVStack& b, bool verbose = false) = 0;
42
43         virtual void reinitialize(const TVStack& b) = 0;
44
45         virtual SIMD_FORCE_INLINE TVStack sub(const TVStack& a, const TVStack& b)
46         {
47                 // c = a-b
48                 btAssert(a.size() == b.size());
49                 TVStack c;
50                 c.resize(a.size());
51                 for (int i = 0; i < a.size(); ++i)
52                 {
53                         c[i] = a[i] - b[i];
54                 }
55                 return c;
56         }
57
58         virtual SIMD_FORCE_INLINE btScalar squaredNorm(const TVStack& a)
59         {
60                 return dot(a, a);
61         }
62
63         virtual SIMD_FORCE_INLINE btScalar norm(const TVStack& a)
64         {
65                 btScalar ret = 0;
66                 for (int i = 0; i < a.size(); ++i)
67                 {
68                         for (int d = 0; d < 3; ++d)
69                         {
70                                 ret = btMax(ret, btFabs(a[i][d]));
71                         }
72                 }
73                 return ret;
74         }
75
76         virtual SIMD_FORCE_INLINE btScalar dot(const TVStack& a, const TVStack& b)
77         {
78                 btScalar ans(0);
79                 for (int i = 0; i < a.size(); ++i)
80                         ans += a[i].dot(b[i]);
81                 return ans;
82         }
83
84         virtual SIMD_FORCE_INLINE void multAndAddTo(btScalar s, const TVStack& a, TVStack& result)
85         {
86                 //        result += s*a
87                 btAssert(a.size() == result.size());
88                 for (int i = 0; i < a.size(); ++i)
89                         result[i] += s * a[i];
90         }
91
92         virtual SIMD_FORCE_INLINE TVStack multAndAdd(btScalar s, const TVStack& a, const TVStack& b)
93         {
94                 // result = a*s + b
95                 TVStack result;
96                 result.resize(a.size());
97                 for (int i = 0; i < a.size(); ++i)
98                         result[i] = s * a[i] + b[i];
99                 return result;
100         }
101
102         virtual SIMD_FORCE_INLINE void setTolerance(btScalar tolerance)
103         {
104                 m_tolerance = tolerance;
105         }
106 };
107 #endif /* BT_KRYLOV_SOLVER_H */