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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef MULTIVECTOR_HPP
00040 #define MULTIVECTOR_HPP
00041
00042 #include "../rtt-config.h"
00043
00044 #include <ostream>
00045 #ifdef OS_HAVE_STREAMS
00046 #include <sstream>
00047 #endif
00048
00049 #include <vector>
00050
00051 #ifdef ORO_PRAGMA_INTERFACE
00052 #pragma interface
00053 #endif
00054
00055 namespace RTT
00056 { namespace extras {
00057
00058
00071 template <unsigned S = 6, class T = double>
00072 struct MultiVector
00073 {
00077 enum Size {size = S};
00078
00083 typedef T DataType[ S ];
00084
00089 DataType data;
00090
00094 operator const T* () const
00095 {
00096 return data;
00097 }
00098
00102 operator T&()
00103 {
00104 return *data;
00105 }
00106
00113 MultiVector( const T d )
00114 {
00115 for ( unsigned int i = 0; i < S;++i )
00116 data[ i ] = d;
00117 }
00118
00122 MultiVector()
00123 {
00124 for ( unsigned int i = 0; i < S;++i )
00125 data[ i ] = 0;
00126 }
00127
00134 template<class Alloc>
00135 MultiVector(const std::vector<T,Alloc>& vect)
00136 {
00137 for ( unsigned int i = 0; i < S;++i )
00138 if ( i < vect.size() )
00139 data[ i ] = vect[i];
00140 else
00141 data[ i ] = 0;
00142 }
00143
00150 MultiVector& operator += ( const MultiVector& d )
00151 {
00152 for ( unsigned int i = 0; i < S; ++i )
00153 data[ i ] += d.data[ i ];
00154
00155 return *this;
00156 }
00157
00164 MultiVector& operator *= ( const MultiVector& d )
00165 {
00166 for ( unsigned int i = 0; i < S; ++i )
00167 data[ i ] *= d.data[ i ];
00168
00169 return *this;
00170 }
00171
00178 MultiVector& operator *= ( const T d )
00179 {
00180 for ( unsigned int i = 0; i < S; ++i )
00181 data[ i ] *= d;
00182
00183 return *this;
00184 }
00185
00192 MultiVector operator - ( const MultiVector& d ) const
00193 {
00194 MultiVector tmp;
00195
00196 for ( unsigned int i = 0; i < S; ++i )
00197 tmp.data[ i ] = data[ i ] - d.data[ i ];
00198
00199 return tmp;
00200 }
00201
00202 MultiVector operator - () const
00203 {
00204 MultiVector tmp;
00205
00206 for ( unsigned int i = 0; i < S; ++i )
00207 tmp.data[ i ] = - data[ i ];
00208
00209 return tmp;
00210 }
00211
00218 MultiVector operator + ( const MultiVector& d ) const
00219 {
00220 MultiVector tmp;
00221
00222 for ( unsigned int i = 0; i < S; ++i )
00223 tmp[ i ] = data[ i ] + d.data[ i ];
00224
00225 return tmp;
00226 }
00227
00234 MultiVector operator * ( const MultiVector& d ) const
00235 {
00236 MultiVector tmp;
00237
00238 for ( unsigned int i = 0; i < S; ++i )
00239 tmp[ i ] = data[ i ] * d.data[ i ];
00240
00241 return tmp;
00242 }
00243
00244 MultiVector operator / ( const T d ) const
00245 {
00246 MultiVector tmp;
00247
00248 for ( unsigned int i = 0; i < S; ++i )
00249 tmp[ i ] = data[ i ] / d;
00250
00251 return tmp;
00252 }
00253
00260 MultiVector operator * ( const T d ) const
00261 {
00262 MultiVector tmp;
00263
00264 for ( unsigned int i = 0; i < S; ++i )
00265 tmp[ i ] = d * data[ i ];
00266
00267 return tmp;
00268 }
00269
00277 MultiVector& operator = ( const MultiVector& d )
00278 {
00279 for ( unsigned int i = 0; i < S; ++i )
00280 data[ i ] = d.data[ i ];
00281
00282 return *this;
00283 }
00284
00292 bool operator == ( const MultiVector& d )
00293 {
00294 for ( unsigned int i = 0; i < S; ++i )
00295 if (data[ i ] != d.data[ i ])
00296 return false;
00297 return true;
00298 }
00299
00307 bool operator != ( const MultiVector& d )
00308 {
00309 for ( unsigned int i = 0; i < S; ++i )
00310 if (data[ i ] != d.data[ i ])
00311 return true;
00312 return false;
00313 }
00314
00319 template<class Alloc>
00320 void getVector( std::vector<T, Alloc>& vect ) const
00321 {
00322 vect.resize(S);
00323 for ( unsigned int i = 0; i < S; ++i )
00324 vect[i] = data[i];
00325 }
00326
00331 template<class Alloc>
00332 bool setVector(const std::vector<T, Alloc>& vect )
00333 {
00334 if ( vect.size() != S )
00335 return false;
00336 for ( unsigned int i = 0; i < S; ++i )
00337 data[i] = vect[i];
00338 return true;
00339 }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00364 MultiVector& operator = ( const T d )
00365 {
00366 for ( unsigned int i = 0; i < S; ++i )
00367 data[ i ] = d;
00368
00369 return *this;
00370 }
00371
00378
00379 T& operator[] ( unsigned int i )
00380 {
00381 return data[ i ];
00382 }
00383 };
00384
00385 template<unsigned S, typename D>
00386 MultiVector<S,D> operator * ( const D d, const MultiVector<S,D>& v )
00387 {
00388 return v*d;
00389 }
00390
00391 template<unsigned S, typename D>
00392 MultiVector<S,D> operator + ( const D d, const MultiVector<S,D>& v )
00393 {
00394 return v+d;
00395 }
00396
00397 template<unsigned S, typename D>
00398 MultiVector<S,D> operator - ( const D d, const MultiVector<S,D>& v )
00399 {
00400 return v-d;
00401 }
00402
00403
00407 typedef MultiVector<6, double> Double6D;
00408
00412 typedef MultiVector<6, int> Int6D;
00413
00417 typedef MultiVector<6, bool> Bool6D;
00418
00422 typedef MultiVector<6, long> Long6D;
00423
00427 template <unsigned int S, class T>
00428 std::ostream &operator<<(std::ostream &os, MultiVector<S,T> &q)
00429 {
00430 #ifdef OS_HAVE_STREAMS
00431 std::stringstream ss;
00432 ss << "(";
00433 for (unsigned int i = 0; i < (S - 1) ; i++) {
00434 ss << q[i] << ", ";
00435 }
00436 ss << q[ S - 1 ] << ")";
00437 os << ss.str();
00438 #endif
00439 return os;
00440 }
00441
00445 template <unsigned int S, class T>
00446 std::istream &operator>>(std::istream &os, MultiVector<S,T> &q)
00447 {
00448 #ifdef OS_HAVE_STREAMS
00449 MultiVector<S, T> p;
00450 char c;
00451 os >> c;
00452 for (unsigned int i = 0; i < (S - 1) ; i++) {
00453 os >> p[i];
00454 os >> c;
00455 os >> c;
00456 }
00457 os >> p[ S - 1 ];
00458 os >> c;
00459 if ( os )
00460 q = p;
00461 #endif
00462 return os;
00463 }
00464
00465 }}
00466
00467 #endif