MultiVector.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_MULTIVECTOR_H
00028 #define SOFA_CORE_COMPONENTMODEL_BEHAVIOR_MULTIVECTOR_H
00029 
00030 #include <sofa/core/objectmodel/BaseObject.h>
00031 #include <sofa/core/componentmodel/behavior/BaseMechanicalState.h>
00032 #include <sofa/defaulttype/BaseMatrix.h>
00033 #include <sofa/defaulttype/BaseVector.h>
00034 
00035 namespace sofa
00036 {
00037 
00038 namespace core
00039 {
00040 
00041 namespace componentmodel
00042 {
00043 
00044 namespace behavior
00045 {
00046 
00050 template<class Parent>
00051 class MultiVector
00052 {
00053 public:
00054     typedef BaseMechanicalState::VecId VecId;
00055 
00056 protected:
00058     Parent* parent;
00059 
00061     VecId v;
00062 
00064     bool dynamic;
00065 
00067     MultiVector(const MultiVector<Parent>& v);
00068 
00069 public:
00071     MultiVector(Parent* parent, VecId v) : parent(parent), v(v), dynamic(false)
00072     {}
00073 
00075     MultiVector(Parent* parent, VecId::Type t) : parent(parent), v(parent->v_alloc(t)), dynamic(true)
00076     {}
00077 
00078     ~MultiVector()
00079     {
00080         if (dynamic) parent->v_free(v);
00081     }
00082 
00084     operator VecId()
00085     {
00086         return v;
00087     }
00088 
00090     void clear()
00091     {
00092         parent->v_clear(v);
00093     }
00094 
00096     void eq(VecId a)
00097     {
00098         parent->v_eq(v, a);
00099     }
00100 
00102     void peq(VecId a, double f=1.0)
00103     {
00104         parent->v_peq(v, a, f);
00105     }
00107     void teq(double f)
00108     {
00109         parent->v_teq(v, f);
00110     }
00112     double dot(VecId a)
00113     {
00114         parent->v_dot(v, a);
00115         return parent->finish();
00116     }
00117     
00119     void threshold( double threshold ) 
00120     {
00121       parent->v_threshold(v, threshold);
00122     }
00123 
00125     double norm()
00126     {
00127         parent->v_dot(v, v);
00128         return sqrt( parent->finish() );
00129     }
00130 
00132     void operator=(VecId a)
00133     {
00134         eq(a);
00135     }
00136 
00138     void operator=(const MultiVector<Parent>& a)
00139     {
00140         eq(a.v);
00141     }
00142 
00144     void operator+=(VecId a)
00145     {
00146         peq(a);
00147     }
00148 
00150     void operator-=(VecId a)
00151     {
00152         peq(a,-1);
00153     }
00154 
00156     void operator*=(double f)
00157     {
00158         teq(f);
00159     }
00160 
00162     void operator/=(double f)
00163     {
00164         teq(1.0/f);
00165     }
00166 
00168     double operator*(VecId a)
00169     {
00170         return dot(a);
00171     }
00172 
00173     friend std::ostream& operator << (std::ostream& out, const MultiVector<Parent>& mv )
00174     {
00175         mv.parent->print(mv.v,out);
00176         return out;
00177     }
00178 };
00179 
00180 } // namespace behavior
00181 
00182 } // namespace componentmodel
00183 
00184 } // namespace core
00185 
00186 } // namespace sofa
00187 
00188 #endif

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