Bullet Collision Detection & Physics Library
btKrylovSolver.h
Go to the documentation of this file.
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>
23#include <LinearMath/btScalar.h>
25
26template <class MatrixX>
28{
30
31public:
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
59 {
60 return dot(a, a);
61 }
62
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
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
103 {
104 m_tolerance = tolerance;
105 }
106};
107#endif /* BT_KRYLOV_SOLVER_H */
const T & btMax(const T &a, const T &b)
Definition: btMinMax.h:27
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
btScalar btFabs(btScalar x)
Definition: btScalar.h:497
#define SIMD_FORCE_INLINE
Definition: btScalar.h:98
#define btAssert(x)
Definition: btScalar.h:153
int size() const
return the number of elements in the array
void resize(int newsize, const T &fillData=T())
virtual ~btKrylovSolver()
virtual btScalar norm(const TVStack &a)
virtual btScalar squaredNorm(const TVStack &a)
virtual btScalar dot(const TVStack &a, const TVStack &b)
virtual int solve(MatrixX &A, TVStack &x, const TVStack &b, bool verbose=false)=0
virtual TVStack sub(const TVStack &a, const TVStack &b)
virtual void setTolerance(btScalar tolerance)
virtual void reinitialize(const TVStack &b)=0
btScalar m_tolerance
btKrylovSolver(int maxIterations, btScalar tolerance)
btAlignedObjectArray< btVector3 > TVStack
virtual void multAndAddTo(btScalar s, const TVStack &a, TVStack &result)
virtual TVStack multAndAdd(btScalar s, const TVStack &a, const TVStack &b)