MultiMatrix.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                              SOFA :: Framework                              *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
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     //friend MechanicalMatrix operator * (double f, const MechanicalMatrix& m1) { return MechanicalMatrix(m1.factors * f); }
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 } // namespace behavior
00156 
00157 } // namespace componentmodel
00158 
00159 } // namespace core
00160 
00161 } // namespace sofa
00162 
00163 #endif

Generated on Sun Nov 23 02:29:12 2008 for Sofa by  doxygen 1.5.7.1