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

DRange.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Stephan Aiche$
00032 // $Authors: Marc Sturm $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_DATASTRUCTURES_DRANGE_H
00036 #define OPENMS_DATASTRUCTURES_DRANGE_H
00037 
00038 #include <OpenMS/DATASTRUCTURES/DIntervalBase.h>
00039 
00040 namespace OpenMS
00041 {
00058   template <UInt D>
00059   class DRange :
00060     public Internal::DIntervalBase<D>
00061   {
00062 public:
00063 
00068 
00069     enum {DIMENSION = D};
00071     typedef Internal::DIntervalBase<D> Base;
00073     typedef typename Base::PositionType PositionType;
00075     typedef typename Base::CoordinateType CoordinateType;
00077     enum DRangeIntersection
00078     {
00079       Disjoint,         
00080       Intersects,       
00081       Inside            
00082     };
00083 
00085 
00086     using Base::min_;
00087     using Base::max_;
00088 
00096     DRange() :
00097       Base()
00098     {
00099     }
00100 
00102     DRange(const PositionType & lower, const PositionType & upper) :
00103       Base(lower, upper)
00104     {
00105     }
00106 
00108     DRange(const DRange & range) :
00109       Base(range)
00110     {
00111     }
00112 
00114     DRange(const Base & range) :
00115       Base(range)
00116     {
00117     }
00118 
00120     DRange(CoordinateType minx, CoordinateType miny, CoordinateType maxx, CoordinateType maxy)
00121     {
00122       OPENMS_PRECONDITION(D == 2, "DRange<D>:DRange(minx, miny, maxx, maxy): index overflow!");
00123       min_[0] = minx;
00124       min_[1] = miny;
00125       max_[0] = maxx;
00126       max_[1] = maxy;
00127     }
00128 
00130     DRange & operator=(const DRange & rhs)
00131     {
00132       Base::operator=(rhs);
00133       return *this;
00134     }
00135 
00137     DRange & operator=(const Base & rhs)
00138     {
00139       Base::operator=(rhs);
00140       return *this;
00141     }
00142 
00144     ~DRange()
00145     {
00146     }
00147 
00149 
00152 
00153     bool operator==(const DRange & rhs) const
00154     {
00155       return Base::operator==(rhs);
00156     }
00157 
00159     bool operator==(const Base & rhs) const
00160     {
00161       return Base::operator==(rhs);
00162     }
00163 
00170     bool encloses(const PositionType & position) const
00171     {
00172       for (UInt i = 0; i != D; i++)
00173       {
00174         if (position[i] < min_[i]) return false;
00175 
00176         if (position[i] >= max_[i]) return false;
00177       }
00178       return true;
00179     }
00180 
00182     bool encloses(CoordinateType x, CoordinateType y) const
00183     {
00184       if (x < min_[0]) return false;
00185 
00186       if (x >= max_[0]) return false;
00187 
00188       if (y < min_[1]) return false;
00189 
00190       if (y >= max_[1]) return false;
00191 
00192       return true;
00193     }
00194 
00196     DRange united(const DRange<D> & other_range) const
00197     {
00198       PositionType united_min;
00199       PositionType united_max;
00200       DRange<D> united_range = DRange<D>::empty;
00201 
00202       PositionType other_min = other_range.minPosition();
00203       PositionType other_max = other_range.maxPosition();
00204 
00205       for (Size i = 0; i != D; ++i)
00206       {
00207         united_min[i] = min_[i] < other_min[i] ? min_[i] : other_min[i];
00208         united_max[i] = max_[i] > other_max[i] ? max_[i] : other_max[i];
00209       }
00210       united_range.setMinMax(united_min, united_max);
00211 
00212       return united_range;
00213     }
00214 
00220     DRangeIntersection intersects(const DRange & range) const
00221     {
00222       //check if r.min_ is in this area
00223       if (encloses(range.min_))
00224       {
00225         //check if r.max_ in this area => Inside / Intersects
00226         for (Size i = 0; i != D; i++)
00227         {
00228           if (range.max_[i] > max_[i])
00229           {
00230             return Intersects;
00231           }
00232         }
00233         return Inside;
00234       }
00235       // => r.min_ is not inside this area
00236       //check if any r.min_ >= max_ => Disjoint
00237       for (Size i = 0; i != D; i++)
00238       {
00239         if (range.min_[i] >= max_[i])
00240         {
00241           return Disjoint;
00242         }
00243       }
00244       // => some coordinate of r.min_ has to be smaller than the one of min_
00245       //check if all coords of r are smaller than the those of the range
00246       for (Size i = 0; i != D; i++)
00247       {
00248         if (range.max_[i] <= min_[i])
00249         {
00250           return Disjoint;
00251         }
00252       }
00253       return Intersects;
00254     }
00255 
00262     bool isIntersected(const DRange & range) const
00263     {
00264       //check if r.min_ is in this area
00265       if (encloses(range.min_))
00266       {
00267         return true;
00268       }
00269 
00270       // => r.min_ is not inside this area
00271       //check if any r.min_ >= max_ => Disjoint
00272       for (Size i = 0; i != D; i++)
00273       {
00274         if (range.min_[i] >= max_[i])
00275         {
00276           return false;
00277         }
00278       }
00279       // => some coordinate of r.min_ has to be smaller than the one of min_
00280       //check if all coords of r are smaller than the those of the range
00281       for (Size i = 0; i != D; i++)
00282       {
00283         if (range.max_[i] <= min_[i])
00284         {
00285           return false;
00286         }
00287       }
00288       return true;
00289     }
00290 
00292     bool isEmpty() const
00293     {
00294       for (UInt i = 0; i != D; i++)
00295       {
00296         if (max_[i] <= min_[i])
00297         {
00298           return true;
00299         }
00300       }
00301       return false;
00302     }
00303 
00305   };
00306 
00308   template <UInt D>
00309   std::ostream & operator<<(std::ostream & os, const DRange<D> & area)
00310   {
00311     os << "--DRANGE BEGIN--" << std::endl;
00312     os << "MIN --> " << area.min_ << std::endl;
00313     os << "MAX --> " << area.max_ << std::endl;
00314     os << "--DRANGE END--" << std::endl;
00315     return os;
00316   }
00317 
00318 } // namespace OpenMS
00319 
00320 #endif // OPENMS_DATASTRUCTURES_DRANGE_H

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:38 using doxygen 1.7.1