Bullet Collision Detection & Physics Library: btKrylovSolver.h Source File

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>
21#include <LinearMath/btAlignedObjectArray.h>
22#include <LinearMath/btVector3.h>
23#include <LinearMath/btScalar.h>
24#include "LinearMath/btQuickprof.h"
25
26template <class MatrixX>
27 class btKrylovSolver
28{
29 typedef btAlignedObjectArray<btVector3> TVStack;
30
31public:
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 */
btAlignedObjectArray.h
btMax
const T & btMax(const T &a, const T &b)
Definition btMinMax.h:27
btQuickprof.h
btScalar.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:314
btFabs
btScalar btFabs(btScalar x)
Definition btScalar.h:497
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition btScalar.h:98
btAssert
#define btAssert(x)
Definition btScalar.h:153
btVector3.h
btAlignedObjectArray
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
Definition btAlignedObjectArray.h:46
btAlignedObjectArray::size
int size() const
return the number of elements in the array
Definition btAlignedObjectArray.h:142
btAlignedObjectArray::resize
void resize(int newsize, const T &fillData=T())
Definition btAlignedObjectArray.h:203
btKrylovSolver::~btKrylovSolver
virtual ~btKrylovSolver()
Definition btKrylovSolver.h:39
btKrylovSolver::norm
virtual btScalar norm(const TVStack &a)
Definition btKrylovSolver.h:63
btKrylovSolver::squaredNorm
virtual btScalar squaredNorm(const TVStack &a)
Definition btKrylovSolver.h:58
btKrylovSolver::dot
virtual btScalar dot(const TVStack &a, const TVStack &b)
Definition btKrylovSolver.h:76
btKrylovSolver::solve
virtual int solve(MatrixX &A, TVStack &x, const TVStack &b, bool verbose=false)=0
btKrylovSolver::sub
virtual TVStack sub(const TVStack &a, const TVStack &b)
Definition btKrylovSolver.h:45
btKrylovSolver::setTolerance
virtual void setTolerance(btScalar tolerance)
Definition btKrylovSolver.h:102
btKrylovSolver::reinitialize
virtual void reinitialize(const TVStack &b)=0
btKrylovSolver::m_tolerance
btScalar m_tolerance
Definition btKrylovSolver.h:33
btKrylovSolver::btKrylovSolver
btKrylovSolver(int maxIterations, btScalar tolerance)
Definition btKrylovSolver.h:34
btKrylovSolver::TVStack
btAlignedObjectArray< btVector3 > TVStack
Definition btKrylovSolver.h:29
btKrylovSolver::multAndAddTo
virtual void multAndAddTo(btScalar s, const TVStack &a, TVStack &result)
Definition btKrylovSolver.h:84
btKrylovSolver::m_maxIterations
int m_maxIterations
Definition btKrylovSolver.h:32
btKrylovSolver::multAndAdd
virtual TVStack multAndAdd(btScalar s, const TVStack &a, const TVStack &b)
Definition btKrylovSolver.h:92