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

DIntervalBase.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: Clemens Groepl $
00032 // $Authors: Clemens Groepl, Marc Sturm $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_DATASTRUCTURES_DINTERVALBASE_H
00036 #define OPENMS_DATASTRUCTURES_DINTERVALBASE_H
00037 
00038 #include <OpenMS/DATASTRUCTURES/DPosition.h>
00039 
00040 #include <utility> // std::pair
00041 
00042 namespace OpenMS
00043 {
00044   namespace Internal
00045   {
00054     template <UInt D>
00055     class DIntervalBase
00056     {
00057 public:
00058 
00063 
00064       enum {DIMENSION = D};
00066       typedef DPosition<D>                            PositionType;
00068       typedef typename PositionType::CoordinateType CoordinateType;
00070 
00073 
00079       DIntervalBase() :
00080         min_(PositionType::maxPositive()),
00081         max_(PositionType::minNegative())
00082       {
00083       }
00084 
00086       DIntervalBase(const DIntervalBase & rhs) :
00087         min_(rhs.min_),
00088         max_(rhs.max_)
00089       {
00090       }
00091 
00093       DIntervalBase & operator=(const DIntervalBase & rhs)
00094       {
00095         min_ = rhs.min_;
00096         max_ = rhs.max_;
00097         return *this;
00098       }
00099 
00101       ~DIntervalBase()
00102       {
00103       }
00104 
00108       DIntervalBase(PositionType const & minimum, PositionType const & maximum) :
00109         min_(minimum),
00110         max_(maximum)
00111       {
00112         normalize_();
00113       }
00114 
00116 
00119 
00121       inline PositionType const & minPosition() const
00122       {
00123         return min_;
00124       }
00125 
00127       inline PositionType const & maxPosition() const
00128       {
00129         return max_;
00130       }
00131 
00138       void setMin(PositionType const & position)
00139       {
00140         min_ = position;
00141         for (UInt i = 0; i < DIMENSION; ++i)
00142         {
00143           if (min_[i] > max_[i]) max_[i] = min_[i];
00144         }
00145       }
00146 
00153       void setMax(PositionType const & position)
00154       {
00155         max_ = position;
00156         for (UInt i = 0; i < DIMENSION; ++i)
00157         {
00158           if (min_[i] > max_[i]) min_[i] = max_[i];
00159         }
00160       }
00161 
00165       void setMinMax(PositionType const & min, PositionType const & max)
00166       {
00167         min_ = min;
00168         max_ = max;
00169         normalize_();
00170       }
00171 
00177       template <UInt D2>
00178       void assign(const DIntervalBase<D2> rhs)
00179       {
00180         for (UInt i = 0; i < std::min(D, D2); ++i)
00181         {
00182           min_[i] = rhs.minPosition()[i];
00183           max_[i] = rhs.maxPosition()[i];
00184         }
00185       }
00186 
00187       //}@
00188 
00191 
00192       bool operator==(const DIntervalBase & rhs) const
00193       {
00194         return (min_ == rhs.min_) && (max_ == rhs.max_);
00195       }
00196 
00198       bool operator!=(const DIntervalBase & rhs) const
00199       {
00200         return !(operator==(rhs));
00201       }
00202 
00204       inline void clear()
00205       {
00206         *this = empty;
00207       }
00208 
00210 
00213 
00215       PositionType center() const
00216       {
00217         PositionType center(min_);
00218         center += max_;
00219         center /= 2;
00220         return center;
00221       }
00222 
00224       PositionType diagonal() const
00225       {
00226         return max_ - min_;
00227       }
00228 
00230       static DIntervalBase const empty;
00232       static DIntervalBase const zero;
00233 
00234       //}@
00235 
00238 
00240       inline CoordinateType minX() const
00241       {
00242         return min_[0];
00243       }
00244 
00246       inline CoordinateType minY() const
00247       {
00248         return min_[1];
00249       }
00250 
00252       inline CoordinateType maxX() const
00253       {
00254         return max_[0];
00255       }
00256 
00258       inline CoordinateType maxY() const
00259       {
00260         return max_[1];
00261       }
00262 
00264       void setMinX(CoordinateType const c)
00265       {
00266         min_[0] = c;
00267         if (min_[0] > max_[0]) max_[0] = min_[0];
00268       }
00269 
00271       void setMinY(CoordinateType const c)
00272       {
00273         min_[1] = c;
00274         if (min_[1] > max_[1]) max_[1] = min_[1];
00275       }
00276 
00278       void setMaxX(CoordinateType const c)
00279       {
00280         max_[0] = c;
00281         if (min_[0] > max_[0]) min_[0] = max_[0];
00282       }
00283 
00285       void setMaxY(CoordinateType const c)
00286       {
00287         max_[1] = c;
00288         if (min_[1] > max_[1]) min_[1] = max_[1];
00289       }
00290 
00292       inline CoordinateType width() const
00293       {
00294         return max_[0] - min_[0];
00295       }
00296 
00298       inline CoordinateType height() const
00299       {
00300         return max_[1] - min_[1];
00301       }
00302 
00304 
00305 protected:
00306 
00308       PositionType min_;
00309 
00311       PositionType max_;
00312 
00314       void normalize_()
00315       {
00316         for (UInt i = 0; i < DIMENSION; ++i)
00317         {
00318           if (min_[i] > max_[i])
00319           {
00320             std::swap(min_[i], max_[i]);
00321           }
00322         }
00323       }
00324 
00326       DIntervalBase(const std::pair<PositionType, PositionType> & pair) :
00327         min_(pair.first),
00328         max_(pair.second)
00329       {
00330 
00331       }
00332 
00333     };
00334 
00335     template <UInt D>
00336     DIntervalBase<D> const DIntervalBase<D>::zero
00337       = DIntervalBase<D>(DIntervalBase<D>::PositionType::zero(), DIntervalBase<D>::PositionType::zero());
00338 
00339     template <UInt D>
00340     DIntervalBase<D> const DIntervalBase<D>::empty
00341       = DIntervalBase<D>(std::make_pair(DIntervalBase<D>::PositionType::maxPositive(), DIntervalBase<D>::PositionType::minNegative()));
00342 
00344     template <UInt D>
00345     std::ostream & operator<<(std::ostream & os, const DIntervalBase<D> & rhs)
00346     {
00347       os << "--DIntervalBase BEGIN--" << std::endl;
00348       os << "MIN --> " << rhs.minPosition() << std::endl;
00349       os << "MAX --> " << rhs.maxPosition() << std::endl;
00350       os << "--DIntervalBase END--" << std::endl;
00351       return os;
00352     }
00353 
00354   }   // namespace Internal
00355 
00356 } // namespace OpenMS
00357 
00358 #endif // OPENMS_KERNEL_DINTERVALBASE_H

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