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

DRange.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_DRANGE_H
00028 #define OPENMS_DATASTRUCTURES_DRANGE_H
00029 
00030 #include <OpenMS/DATASTRUCTURES/DIntervalBase.h>
00031 
00032 namespace OpenMS
00033 {
00050   template <UInt D>
00051   class DRange
00052     : public Internal::DIntervalBase<D>
00053   {
00054    public:
00055 
00060 
00061     enum { DIMENSION = D };
00063     typedef Internal::DIntervalBase<D> Base; 
00065     typedef typename Base::PositionType PositionType;
00067     typedef typename Base::CoordinateType CoordinateType;
00069     enum DRangeIntersection 
00070     {
00071       Disjoint,   
00072       Intersects, 
00073       Inside      
00074     };
00075 
00077     
00078     using Base::min_;
00079     using Base::max_;
00080 
00088     DRange()
00089       : Base()
00090     {
00091       
00092     }
00093     
00095     DRange(const PositionType& lower, const PositionType& upper)
00096       :Base(lower, upper)
00097     {
00098       
00099     }
00100     
00102     DRange(const DRange& range)
00103       : Base(range)
00104     {
00105       
00106     }
00107 
00109     DRange(const Base& range)
00110       : Base(range)
00111     {
00112       
00113     }
00114 
00116     DRange(CoordinateType minx,CoordinateType miny, CoordinateType maxx,CoordinateType maxy)
00117     {
00118       OPENMS_PRECONDITION(D == 2, "DRange<D>:DRange(minx, miny, maxx, maxy): index overflow!");
00119       min_[0]=minx;
00120       min_[1]=miny;
00121       max_[0]=maxx;
00122       max_[1]=maxy;
00123     }
00124 
00126     DRange & operator=(const DRange& rhs)
00127     {
00128       Base::operator=(rhs);
00129       return *this;
00130     }
00131 
00133     DRange & operator=(const Base& rhs)
00134     {
00135       Base::operator=(rhs);
00136       return *this;
00137     }
00138 
00140     ~DRange() {}
00142 
00145 
00146     bool operator == (const DRange& rhs) const throw()
00147     {
00148       return Base::operator==(rhs);
00149     }
00151     bool operator == (const Base& rhs) const throw()
00152     {
00153       return Base::operator==(rhs);
00154     }
00155 
00162     bool encloses(const PositionType& position) const
00163     {
00164       for(UInt i = 0; i != D; i++)
00165       {
00166         if (position[i]<min_[i]) return false;
00167         if (position[i]>=max_[i]) return false;
00168       }
00169       return true;
00170     }
00171 
00173     bool encloses(CoordinateType x, CoordinateType y) const
00174     {
00175       if (x<min_[0]) return false;
00176       if (x>=max_[0]) return false;
00177       if (y<min_[1]) return false;
00178       if (y>=max_[1]) return false;
00179       return true;
00180     }
00181     
00187     DRangeIntersection intersects(const DRange& range) const
00188     {
00189       //check if r.min_ is in this area
00190       if (encloses(range.min_))
00191       {
00192         //check if r.max_ in this area => Inside / Intersects
00193         for(UInt i = 0; i != D; i++)
00194         {
00195           if (range.max_[i]>max_[i])
00196           {
00197             return Intersects;
00198           }
00199         }
00200         return Inside;
00201       }
00202       // => r.min_ is not inside this area
00203       //check if any r.min_ >= max_ => Disjoint
00204       for(UInt i = 0; i != D; i++)
00205       {
00206         if (range.min_[i]>=max_[i])
00207         {
00208           return Disjoint;
00209         }
00210       }
00211       // => some coordinate of r.min_ has to be smaller than the one of min_
00212       //check if all coords of r are smaller than the those of the range
00213       for(UInt i = 0; i != D; i++)
00214       {
00215         if (range.max_[i]<=min_[i])
00216         {
00217           return Disjoint;
00218         }
00219       }
00220       return Intersects;
00221     }         
00222 
00229     bool isIntersected(const DRange& range) const
00230     {
00231       //check if r.min_ is in this area
00232       if (encloses(range.min_))
00233       {
00234         return true;
00235       }
00236       
00237       // => r.min_ is not inside this area
00238       //check if any r.min_ >= max_ => Disjoint
00239       for(UInt i = 0; i != D; i++)
00240       {
00241         if (range.min_[i]>=max_[i])
00242         {
00243           return false;
00244         }
00245       }
00246       // => some coordinate of r.min_ has to be smaller than the one of min_
00247       //check if all coords of r are smaller than the those of the range
00248       for(UInt i = 0; i != D; i++)
00249       {
00250         if (range.max_[i]<=min_[i])
00251         {
00252           return false;
00253         }
00254       }
00255       return true;
00256     }
00257     
00259     bool isEmpty() const 
00260     { 
00261       for(UInt i = 0; i != D; i++)
00262       {
00263         if (max_[i]<=min_[i])
00264         {
00265           return true;
00266         }
00267       }
00268       return false; 
00269     }
00270     
00272   };
00273 
00275   template <UInt D>
00276   std::ostream& operator << (std::ostream& os, const DRange<D>& area)
00277   {
00278     os << "--DRANGE BEGIN--"<<std::endl;
00279     os << "MIN --> " << area.min_ << std::endl;
00280     os << "MAX --> " << area.max_ << std::endl;
00281     os << "--DRANGE END--"<<std::endl;
00282     return os;
00283   }
00284 
00285   
00286 } // namespace OpenMS
00287 
00288 #endif // OPENMS_KERNEL_DRANGE_H

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