MultiMatrix.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef SOFA_CORE_COMPONENTMODEL_BEHAVIOR_MULTIMATRIX_H
00028 #define SOFA_CORE_COMPONENTMODEL_BEHAVIOR_MULTIMATRIX_H
00029
00030 #include <sofa/core/componentmodel/behavior/MultiVector.h>
00031
00032 namespace sofa
00033 {
00034
00035 namespace core
00036 {
00037
00038 namespace componentmodel
00039 {
00040
00041 namespace behavior
00042 {
00043
00046 class MechanicalMatrix
00047 {
00048 protected:
00049 enum { MFACT = 0, BFACT = 1, KFACT = 2 };
00050 defaulttype::Vec<3,double> factors;
00051 public:
00052 MechanicalMatrix(double m, double b, double k) : factors(m,b,k) {}
00053 explicit MechanicalMatrix(const defaulttype::Vec<3,double>& f) : factors(f) {}
00054
00055 static const MechanicalMatrix M;
00056 static const MechanicalMatrix B;
00057 static const MechanicalMatrix K;
00058
00059 double getMFact() const { return factors[MFACT]; }
00060 double getBFact() const { return factors[BFACT]; }
00061 double getKFact() const { return factors[KFACT]; }
00062
00063 MechanicalMatrix operator + (const MechanicalMatrix& m2) const { return MechanicalMatrix(factors + m2.factors); }
00064 MechanicalMatrix operator - (const MechanicalMatrix& m2) const { return MechanicalMatrix(factors - m2.factors); }
00065 MechanicalMatrix operator - () const { return MechanicalMatrix(- factors); }
00066 MechanicalMatrix operator * (double f) const { return MechanicalMatrix(factors * f); }
00067
00068 MechanicalMatrix operator / (double f) const { return MechanicalMatrix(factors / f); }
00069 friend std::ostream& operator << (std::ostream& out, const MechanicalMatrix& m )
00070 {
00071 out << '(';
00072 bool first = true;
00073 for (unsigned int i=0;i<m.factors.size();++i)
00074 {
00075 double f = m.factors[i];
00076 if (f!=0.0)
00077 {
00078 if (!first) out << ' ';
00079 if (f == -1.0) out << '-';
00080 else if (f < 0) out << f << ' ';
00081 else
00082 {
00083 if (!first) out << '+';
00084 if (f != 1.0) out << f << ' ';
00085 }
00086 out << ("MBK")[i];
00087 first = false;
00088 }
00089 }
00090 out << ')';
00091 return out;
00092 }
00093 };
00094
00098 template<class Parent>
00099 class MultiMatrix
00100 {
00101 public:
00102 typedef BaseMechanicalState::VecId VecId;
00103
00104 protected:
00106 Parent* parent;
00107
00109 VecId v;
00110
00112 MultiMatrix(const MultiMatrix<Parent>&);
00113
00114 public:
00115
00116 MultiMatrix(Parent* parent) : parent(parent)
00117 {
00118 }
00119
00120 ~MultiMatrix()
00121 {
00122 }
00123
00125 void clear()
00126 {
00127 parent->m_resetSystem();
00128 }
00129
00131 void reset()
00132 {
00133 parent->m_resetSystem();
00134 }
00135
00137 void operator=(const MechanicalMatrix& m)
00138 {
00139 parent->m_setSystemMBKMatrix(m.getMFact(), m.getBFact(), m.getKFact());
00140 }
00141
00142 void solve(MultiVector<Parent>& solution, MultiVector<Parent>& rh)
00143 {
00144 parent->m_setSystemRHVector(rh);
00145 parent->m_setSystemLHVector(solution);
00146 parent->m_solveSystem();
00147 }
00148 friend std::ostream& operator << (std::ostream& out, const MultiMatrix<Parent>& m )
00149 {
00150 m.parent->m_print(out);
00151 return out;
00152 }
00153 };
00154
00155 }
00156
00157 }
00158
00159 }
00160
00161 }
00162
00163 #endif