MultiVector.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_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 }
00181
00182 }
00183
00184 }
00185
00186 }
00187
00188 #endif