Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

DPosition.h (Maintainer: Marc Sturm)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework 
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Marc Sturm $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_DATASTRUCTURES_DPOSITION_H
00028 #define OPENMS_DATASTRUCTURES_DPOSITION_H
00029 
00030 #include <OpenMS/config.h>
00031 #include <OpenMS/CONCEPT/Macros.h>
00032 
00033 #include <algorithm>
00034 #include <limits>
00035 
00036 namespace OpenMS
00037 {
00043   template <UInt D>
00044   class DPosition
00045   {
00046    public:
00047 
00049     typedef DoubleReal CoordinateType;
00051     typedef CoordinateType* Iterator;
00053     typedef const CoordinateType* ConstIterator;
00055     enum { DIMENSION = D };
00060     typedef CoordinateType value_type;
00061     typedef CoordinateType& reference;
00062     typedef CoordinateType* pointer;
00063     typedef CoordinateType* iterator;
00064     typedef const CoordinateType* const_iterator;
00066     
00076     DPosition() { clear();  }
00077     
00079     ~DPosition() {}
00080 
00082     DPosition(CoordinateType x)
00083     {
00084       std::fill(&(coordinate_[0]), &(coordinate_[D]), x);
00085     }
00086 
00088     DPosition(const DPosition& pos)
00089     {
00090       std::copy(&(pos.coordinate_[0]), &(pos.coordinate_[D]), &(coordinate_[0]));
00091     }
00092     
00094     DPosition(CoordinateType x, CoordinateType y)
00095     {
00096       OPENMS_PRECONDITION(D == 2, "DPosition<D>:DPosition(x,y): index overflow!");
00097       coordinate_[0]=x;
00098       coordinate_[1]=y;
00099     }
00100     
00102     DPosition& operator = (const DPosition& source)
00103     {
00104       if (&source==this)
00105         return *this;
00106 
00107       std::copy(&(source.coordinate_[0]), &(source.coordinate_[D]), &(coordinate_[0]));
00108 
00109       return *this;
00110     }
00111 
00113 
00116     
00118     CoordinateType operator [] (UInt index) const
00119     {
00120       OPENMS_PRECONDITION(index < D, "DPosition<D>:operator [] (Position): index overflow!");
00121       return coordinate_[index];
00122     }
00123 
00125     CoordinateType& operator [] (UInt index)
00126     {
00127       OPENMS_PRECONDITION(index < D, "DPosition<D>:operator [] (Position): index overflow!");
00128       return coordinate_[index];
00129     }
00130     
00132     CoordinateType getX() const
00133     {
00134       OPENMS_PRECONDITION(D == 2, "DPosition<D>:getX(): index overflow!");
00135       return coordinate_[0];
00136     }
00137     
00139     CoordinateType getY() const
00140     {
00141       OPENMS_PRECONDITION(D == 2, "DPosition<D>:getY(): index overflow!");
00142       return coordinate_[1];
00143     }
00144     
00146     void setX(CoordinateType c) 
00147     {
00148       OPENMS_PRECONDITION(D == 2, "DPosition<D>:setX(): index overflow!");
00149       coordinate_[0] = c;
00150     }
00151     
00153     void setY(CoordinateType c) 
00154     {
00155       OPENMS_PRECONDITION(D == 2, "DPosition<D>:setY(): index overflow!");
00156       coordinate_[1] = c;
00157     }
00158 
00160     bool operator == (const DPosition& point) const throw()
00161     {
00162       for (UInt i = 0; i < D; i++)
00163       {
00164         if (coordinate_[i] != point.coordinate_[i]) return false;
00165       }
00166       return true;
00167     }
00168 
00170     bool operator != (const DPosition& point) const throw()
00171     {
00172       return !( operator==(point) );
00173     }
00174 
00180     bool operator < (const DPosition& point) const throw()
00181     {
00182       for (UInt i = 0; i < D; i++)
00183       {
00184         if (coordinate_[i] < point.coordinate_[i]) return true;
00185         if (coordinate_[i] > point.coordinate_[i]) return false;
00186       }
00187       return false;
00188     }
00189     
00191     bool operator <= (const DPosition& point) const throw()
00192     {
00193       for (UInt i = 0; i < D; i++)
00194       {
00195         if (coordinate_[i] < point.coordinate_[i]) return true;
00196         if (coordinate_[i] > point.coordinate_[i]) return false;
00197       }
00198       return true;
00199     }
00200     
00202     bool spatiallyLessEqual(const DPosition& point) const throw()
00203     {
00204       for (UInt i = 0; i < D; i++)
00205       {
00206         if (coordinate_[i] > point.coordinate_[i]) return false;
00207       }
00208       return true;
00209     }
00210     
00212     bool spatiallyGreaterEqual(const DPosition& point) const throw()
00213     {
00214       for (UInt i = 0; i < D; i++)
00215       {
00216         if (coordinate_[i] < point.coordinate_[i]) return false;
00217       }
00218       return true;
00219     }
00220     
00222     bool operator > (const DPosition& point) const throw()
00223     {
00224       return !(operator<=(point));
00225     }
00226     
00228     bool operator >= (const DPosition& point) const throw()
00229     {
00230       return !operator<(point);
00231     }
00232 
00234     DPosition operator + (const DPosition& point) const throw()
00235     {
00236       DPosition result(*this);
00237       for (UInt i = 0; i < D; result.coordinate_[i] += point.coordinate_[i], ++i);
00238       return result;
00239     }
00240     
00242     DPosition & operator += (const DPosition& point) throw()
00243     {
00244       for (UInt i = 0; i < D; coordinate_[i] += point.coordinate_[i], ++i);
00245       return *this;
00246     }
00247     
00249     DPosition operator - (const DPosition& point) const throw()
00250     {
00251       DPosition result(*this);
00252       for (UInt i = 0; i < D; result.coordinate_[i] -= point.coordinate_[i], ++i);
00253       return result;
00254     }
00255 
00257     DPosition & operator -= (const DPosition& point) throw()
00258     {
00259       for (UInt i = 0; i < D; coordinate_[i] -= point.coordinate_[i], ++i);
00260       return *this;
00261     }
00262     
00264     DPosition operator - () const throw()
00265     {
00266       DPosition<D> result(*this);
00267       for (UInt i=0; i < D; result.coordinate_[i] = -result.coordinate_[i] , ++i);
00268       return result;      
00269     }
00270           
00272     CoordinateType operator * (const DPosition& point) const throw()
00273     {
00274       CoordinateType prod(0);
00275       for (UInt i = 0; i < D; prod += (point.coordinate_[i] * coordinate_[i]), ++i);
00276       return prod;
00277     }
00278     
00280     DPosition & operator *= (CoordinateType scalar) throw()
00281     {
00282       for (UInt i = 0; i < D; coordinate_[i] *= scalar, ++i);
00283       return *this;
00284     }
00285 
00287     DPosition & operator /= (CoordinateType scalar) throw()
00288     {
00289       for (UInt i = 0; i < D; coordinate_[i] /= scalar, ++i);
00290       return *this;
00291     }
00292 
00294     static UInt size() { return D; }
00295         
00297     void clear() { for (UInt i = 0; i < D; coordinate_[i++] = (CoordinateType)0); }
00299 
00302 
00303     static const DPosition zero;
00305     static const DPosition min;
00307     static const DPosition min_negative;
00309     static const DPosition max;
00311 
00314 
00315     ConstIterator begin() const throw() { return &(coordinate_[0]); }
00317     ConstIterator end() const throw() { return &(coordinate_[0]) + D; }
00319     Iterator begin() throw() { return &(coordinate_[0]); }
00321     Iterator end() throw() { return &(coordinate_[0]) + D; }
00323 
00324    protected:
00325     CoordinateType coordinate_[D];
00326 
00327   };  // DPosition
00328 
00330   template <UInt D>
00331   DPosition<D> operator * (DPosition<D> position, typename DPosition<D>::CoordinateType scalar) throw()
00332   {
00333     for (UInt i = 0; i < D; position[i] *= scalar,++i) ;
00334     return position;
00335   }
00336   
00338   template <UInt D>
00339   DPosition<D> operator * (typename DPosition<D>::CoordinateType scalar, DPosition<D> position) throw()
00340   {
00341     for (UInt i = 0; i < D; position[i] *= scalar,++i) ;
00342     return position;
00343   }
00344 
00345   template <UInt D>
00346   const DPosition<D> DPosition<D>::zero 
00347   = DPosition<D>(0);
00348 
00349   template <UInt D>
00350   const DPosition<D> DPosition<D>::min 
00351   = DPosition<D>(std::numeric_limits<typename DPosition<D>::CoordinateType>::min());
00352 
00353   template <UInt D>
00354   const DPosition<D> DPosition<D>::max 
00355   = DPosition<D>(std::numeric_limits<typename DPosition<D>::CoordinateType>::max());
00356 
00357   template <UInt D>
00358   const DPosition<D> DPosition<D>::min_negative
00359   = DPosition<D>(-std::numeric_limits<typename DPosition<D>::CoordinateType>::max());
00360 
00362   template <UInt D>
00363   std::ostream& operator << (std::ostream& os, const DPosition<D>& pos)
00364   {
00365     os << pos[0];
00366     for (UInt i=1; i < D; ++i)
00367     {
00368       os << ' ' << pos[i];
00369     }
00370     
00371     return os;
00372   }
00373 
00374   
00375 } // namespace OpenMS
00376 
00377 #endif // OPENMS_DATASTRUCTURES_DPOSITION_H

Generated Tue Apr 1 15:36:33 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1