MultiVector.hpp

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 {
00057 
00070     template <unsigned S = 6, class T = double>
00071     struct MultiVector
00072     {
00076         enum Size {size = S};
00077 
00082         typedef T DataType[ S ];
00083 
00088         DataType data;
00089 
00093         operator const T* () const
00094         {
00095             return data;
00096         }
00097 
00101         operator T&()
00102         {
00103             return *data;
00104         }
00105 
00112         MultiVector( const T d )
00113         {
00114             for ( unsigned int i = 0; i < S;++i )
00115                 data[ i ] = d;
00116         }
00117 
00121         MultiVector()
00122         {
00123             for ( unsigned int i = 0; i < S;++i )
00124                 data[ i ] = 0;
00125         }
00126 
00133         template<class Alloc>
00134         MultiVector(const std::vector<T,Alloc>& vect)
00135         {
00136             for ( unsigned int i = 0; i < S;++i )
00137                 if ( i < vect.size() )
00138                     data[ i ] = vect[i];
00139                 else
00140                     data[ i ] = 0;
00141         }
00142 
00149         MultiVector& operator += ( const MultiVector& d )
00150         {
00151             for ( unsigned int i = 0; i < S; ++i )
00152                 data[ i ] += d.data[ i ];
00153 
00154             return *this;
00155         }
00156 
00163         MultiVector& operator *= ( const MultiVector& d )
00164         {
00165             for ( unsigned int i = 0; i < S; ++i )
00166                 data[ i ] *= d.data[ i ];
00167 
00168             return *this;
00169         }
00170 
00177         MultiVector& operator *= ( const T d )
00178         {
00179             for ( unsigned int i = 0; i < S; ++i )
00180                 data[ i ] *= d;
00181 
00182             return *this;
00183         }
00184 
00191         MultiVector operator - ( const MultiVector& d ) const
00192         {
00193             MultiVector tmp;
00194 
00195             for ( unsigned int i = 0; i < S; ++i )
00196                 tmp.data[ i ] = data[ i ] - d.data[ i ];
00197 
00198             return tmp;
00199         }
00200 
00201         MultiVector operator - () const
00202         {
00203             MultiVector tmp;
00204 
00205             for ( unsigned int i = 0; i < S; ++i )
00206                 tmp.data[ i ] = - data[ i ];
00207 
00208             return tmp;
00209         }
00210 
00217         MultiVector operator + ( const MultiVector& d ) const
00218         {
00219             MultiVector tmp;
00220 
00221             for ( unsigned int i = 0; i < S; ++i )
00222                 tmp[ i ] = data[ i ] + d.data[ i ];
00223 
00224             return tmp;
00225         }
00226 
00233         MultiVector operator * ( const MultiVector& d ) const
00234         {
00235             MultiVector tmp;
00236 
00237             for ( unsigned int i = 0; i < S; ++i )
00238                 tmp[ i ] = data[ i ] * d.data[ i ];
00239 
00240             return tmp;
00241         }
00242 
00243         MultiVector operator / ( const T d ) const
00244         {
00245             MultiVector tmp;
00246 
00247             for ( unsigned int i = 0; i < S; ++i )
00248                 tmp[ i ] = data[ i ] / d;
00249 
00250             return tmp;
00251         }
00252 
00259         MultiVector operator * ( const T d ) const
00260         {
00261             MultiVector tmp;
00262 
00263             for ( unsigned int i = 0; i < S; ++i )
00264                 tmp[ i ] = d * data[ i ];
00265 
00266             return tmp;
00267         }
00268 
00276         MultiVector& operator = ( const MultiVector& d )
00277         {
00278             for ( unsigned int i = 0; i < S; ++i )
00279                 data[ i ] = d.data[ i ];
00280 
00281             return *this;
00282         }
00283 
00291         bool operator == ( const MultiVector& d )
00292         {
00293             for ( unsigned int i = 0; i < S; ++i )
00294                 if (data[ i ] != d.data[ i ])
00295                     return false;
00296             return true;
00297         }
00298 
00306         bool operator != ( const MultiVector& d )
00307         {
00308             for ( unsigned int i = 0; i < S; ++i )
00309                 if (data[ i ] != d.data[ i ])
00310                     return true;
00311             return false;
00312         }
00313 
00318         template<class Alloc>
00319         void getVector( std::vector<T, Alloc>& vect ) const
00320         {
00321             vect.resize(S);
00322             for ( unsigned int i = 0; i < S; ++i )
00323                 vect[i] = data[i];
00324         }
00325 
00330         template<class Alloc>
00331         bool setVector(const std::vector<T, Alloc>& vect )
00332         {
00333             if ( vect.size() != S )
00334                 return false;
00335             for ( unsigned int i = 0; i < S; ++i )
00336                 data[i] = vect[i];
00337             return true;
00338         }
00339 
00340         /*
00341          * This is just plain wrong since assignment with integer
00342          * would call this method.
00343          * Actually, it's the standard that's wrong for doing
00344          * int == pointer
00345          *
00346             MultiVector& operator = (const DataType d)
00347             {
00348         //        if (d != 0)
00349                     for (int i=0; i<S; ++i)
00350                         data[i] = d[i];
00351                 return *this;
00352             }
00353          *
00354          */
00355 
00363         MultiVector& operator = ( const T d )
00364         {
00365             for ( unsigned int i = 0; i < S; ++i )
00366                 data[ i ] = d;
00367 
00368             return *this;
00369         }
00370 
00377         //replaced int parameter with unsigned int to unconfuse MSVC
00378         T& operator[] ( unsigned int i )
00379         {
00380             return data[ i ];
00381         }
00382     };
00383 
00384     template<unsigned S, typename D>
00385     MultiVector<S,D> operator * ( const D d, const MultiVector<S,D>& v )
00386     {
00387         return v*d;
00388     }
00389 
00390     template<unsigned S, typename D>
00391     MultiVector<S,D> operator + ( const D d, const MultiVector<S,D>& v )
00392     {
00393         return v+d;
00394     }
00395 
00396     template<unsigned S, typename D>
00397     MultiVector<S,D> operator - ( const D d, const MultiVector<S,D>& v )
00398     {
00399         return v-d;
00400     }
00401 
00402 
00406     typedef MultiVector<6, double> Double6D;
00407 
00411     typedef MultiVector<6, int> Int6D;
00412 
00416     typedef MultiVector<6, bool> Bool6D;
00417 
00421     typedef MultiVector<6, long> Long6D;
00422 
00423 #if !defined(ORO_EMBEDDED) && defined(__GNUC__)
00424     extern template class MultiVector<6, double>;
00425     extern template class MultiVector<6, int>;
00426     extern template class MultiVector<6, bool>;
00427     extern template class MultiVector<6, long>;
00428 #endif
00429 
00433     template <unsigned int S, class T>
00434     std::ostream &operator<<(std::ostream &os, MultiVector<S,T> &q)
00435     {
00436 #ifdef OS_HAVE_STREAMS
00437         std::stringstream ss;
00438         ss << "(";
00439         for (unsigned int i = 0; i < (S - 1) ; i++) {
00440             ss << q[i] << ", ";
00441         }
00442         ss << q[ S - 1 ] << ")";
00443         os << ss.str();
00444 #endif
00445         return os;
00446     }
00447 
00451     template <unsigned int S, class T>
00452     std::istream &operator>>(std::istream &os, MultiVector<S,T> &q)
00453     {
00454 #ifdef OS_HAVE_STREAMS
00455         MultiVector<S, T> p;
00456         char c;
00457         os >> c; // '('
00458         for (unsigned int i = 0; i < (S - 1) ; i++) {
00459             os >> p[i];
00460             os >> c; // ','
00461             os >> c; // ' '
00462         }
00463         os >> p[ S - 1 ];
00464         os >> c; // ')';
00465         if ( os )
00466             q = p;
00467 #endif
00468         return os;
00469     }
00470 
00471 }
00472 
00473 #endif
Generated on Thu Dec 23 13:22:38 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3