$search
00001 /*************************************************************************** 00002 tag: Peter Soetens Thu Oct 10 16:21:19 CEST 2002 MultiVector.hpp 00003 00004 MultiVector.hpp - description 00005 ------------------- 00006 begin : Thu October 10 2002 00007 copyright : (C) 2002 Peter Soetens 00008 email : peter.soetens@mech.kuleuven.ac.be 00009 00010 *************************************************************************** 00011 * This library is free software; you can redistribute it and/or * 00012 * modify it under the terms of the GNU General Public * 00013 * License as published by the Free Software Foundation; * 00014 * version 2 of the License. * 00015 * * 00016 * As a special exception, you may use this file as part of a free * 00017 * software library without restriction. Specifically, if other files * 00018 * instantiate templates or use macros or inline functions from this * 00019 * file, or you compile this file and link it with other files to * 00020 * produce an executable, this file does not by itself cause the * 00021 * resulting executable to be covered by the GNU General Public * 00022 * License. This exception does not however invalidate any other * 00023 * reasons why the executable file might be covered by the GNU General * 00024 * Public License. * 00025 * * 00026 * This library is distributed in the hope that it will be useful, * 00027 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00028 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00029 * Lesser General Public License for more details. * 00030 * * 00031 * You should have received a copy of the GNU General Public * 00032 * License along with this library; if not, write to the Free Software * 00033 * Foundation, Inc., 59 Temple Place, * 00034 * Suite 330, Boston, MA 02111-1307 USA * 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 * This is just plain wrong since assignment with integer 00343 * would call this method. 00344 * Actually, it's the standard that's wrong for doing 00345 * int == pointer 00346 * 00347 MultiVector& operator = (const DataType d) 00348 { 00349 // if (d != 0) 00350 for (int i=0; i<S; ++i) 00351 data[i] = d[i]; 00352 return *this; 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 //replaced int parameter with unsigned int to unconfuse MSVC 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