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

DPosition.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, Stephan Aiche $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_DATASTRUCTURES_DPOSITION_H
00036 #define OPENMS_DATASTRUCTURES_DPOSITION_H
00037 
00038 #include <OpenMS/config.h>
00039 #include <OpenMS/CONCEPT/Macros.h>
00040 
00041 #include <algorithm>
00042 #include <limits>
00043 
00044 namespace OpenMS
00045 {
00051   template <UInt D, typename TCoordinateType = DoubleReal>
00052   class DPosition
00053   {
00054 public:
00055 
00057     typedef TCoordinateType CoordinateType;
00059     typedef CoordinateType * Iterator;
00061     typedef const CoordinateType * ConstIterator;
00063     enum
00064     {
00065       DIMENSION = D
00066     };
00071     typedef CoordinateType value_type;
00072     typedef CoordinateType & reference;
00073     typedef CoordinateType * pointer;
00074     typedef CoordinateType * iterator;
00075     typedef const CoordinateType * const_iterator;
00077 
00087     DPosition()
00088     {
00089       clear();
00090     }
00091 
00093     ~DPosition()
00094     {
00095     }
00096 
00098     DPosition(CoordinateType x)
00099     {
00100       std::fill(&(coordinate_[0]), &(coordinate_[D]), x);
00101     }
00102 
00104     DPosition(const DPosition & pos)
00105     {
00106       std::copy(&(pos.coordinate_[0]), &(pos.coordinate_[D]),
00107                 &(coordinate_[0]));
00108     }
00109 
00111     DPosition(CoordinateType x, CoordinateType y)
00112     {
00113       OPENMS_PRECONDITION(D == 2, "DPosition<D, TCoordinateType>:DPosition(x,y): index overflow!");
00114       coordinate_[0] = x;
00115       coordinate_[1] = y;
00116     }
00117 
00119     DPosition & operator=(const DPosition & source)
00120     {
00121       if (&source == this) return *this;
00122 
00123       std::copy(&(source.coordinate_[0]), &(source.coordinate_[D]),
00124                 &(coordinate_[0]));
00125 
00126       return *this;
00127     }
00128 
00130 
00133 
00135     CoordinateType operator[](Size index) const
00136     {
00137       OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
00138       return coordinate_[index];
00139     }
00140 
00142     CoordinateType & operator[](Size index)
00143     {
00144       OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
00145       return coordinate_[index];
00146     }
00147 
00149     CoordinateType getX() const
00150     {
00151       OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getX(): index overflow!");
00152       return coordinate_[0];
00153     }
00154 
00156     CoordinateType getY() const
00157     {
00158       OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getY(): index overflow!");
00159       return coordinate_[1];
00160     }
00161 
00163     void setX(CoordinateType c)
00164     {
00165       OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setX(): index overflow!");
00166       coordinate_[0] = c;
00167     }
00168 
00170     void setY(CoordinateType c)
00171     {
00172       OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setY(): index overflow!");
00173       coordinate_[1] = c;
00174     }
00175 
00177     bool operator==(const DPosition & point) const
00178     {
00179       for (Size i = 0; i < D; i++)
00180       {
00181         if (coordinate_[i] != point.coordinate_[i]) return false;
00182       }
00183       return true;
00184     }
00185 
00187     bool operator!=(const DPosition & point) const
00188     {
00189       return !(operator==(point));
00190     }
00191 
00196     bool operator<(const DPosition & point) const
00197     {
00198       for (Size i = 0; i < D; i++)
00199       {
00200         if (coordinate_[i] < point.coordinate_[i]) return true;
00201 
00202         if (coordinate_[i] > point.coordinate_[i]) return false;
00203       }
00204       return false;
00205     }
00206 
00208     bool operator<=(const DPosition & point) const
00209     {
00210       for (Size i = 0; i < D; i++)
00211       {
00212         if (coordinate_[i] < point.coordinate_[i]) return true;
00213 
00214         if (coordinate_[i] > point.coordinate_[i]) return false;
00215       }
00216       return true;
00217     }
00218 
00220     bool spatiallyLessEqual(const DPosition & point) const
00221     {
00222       for (Size i = 0; i < D; i++)
00223       {
00224         if (coordinate_[i] > point.coordinate_[i]) return false;
00225       }
00226       return true;
00227     }
00228 
00230     bool spatiallyGreaterEqual(const DPosition & point) const
00231     {
00232       for (Size i = 0; i < D; i++)
00233       {
00234         if (coordinate_[i] < point.coordinate_[i]) return false;
00235       }
00236       return true;
00237     }
00238 
00240     bool operator>(const DPosition & point) const
00241     {
00242       return !(operator<=(point));
00243     }
00244 
00246     bool operator>=(const DPosition & point) const
00247     {
00248       return !operator<(point);
00249     }
00250 
00252     DPosition operator+(const DPosition & point) const
00253     {
00254       DPosition result(*this);
00255       for (Size i = 0; i < D; ++i)
00256       {
00257         result.coordinate_[i] += point.coordinate_[i];
00258       }
00259       return result;
00260     }
00261 
00263     DPosition & operator+=(const DPosition & point)
00264     {
00265       for (Size i = 0; i < D; ++i)
00266       {
00267         coordinate_[i] += point.coordinate_[i];
00268       }
00269       return *this;
00270     }
00271 
00273     DPosition operator-(const DPosition & point) const
00274     {
00275       DPosition result(*this);
00276       for (Size i = 0; i < D; ++i)
00277       {
00278         result.coordinate_[i] -= point.coordinate_[i];
00279       }
00280       return result;
00281     }
00282 
00284     DPosition & operator-=(const DPosition & point)
00285     {
00286       for (Size i = 0; i < D; ++i) 
00287       {
00288         coordinate_[i] -= point.coordinate_[i];
00289       }
00290       return *this;
00291     }
00292 
00294     DPosition   operator - () const
00295     {
00296       DPosition<D, CoordinateType> result(*this);
00297       for (Size i = 0; i < D; ++i)
00298       {
00299         result.coordinate_[i] = -result.coordinate_[i];
00300       }
00301       return result;
00302     }
00303 
00305     CoordinateType operator*(const DPosition & point) const
00306     {
00307       CoordinateType prod(0);
00308       for (Size i = 0; i < D; ++i)
00309       {
00310         prod += (point.coordinate_[i] * coordinate_[i]);
00311       }
00312       return prod;
00313     }
00314 
00316     DPosition & operator*=(CoordinateType scalar)
00317     {
00318       for (Size i = 0; i < D; ++i)
00319       {
00320         coordinate_[i] *= scalar;
00321       }
00322       return *this;
00323     }
00324 
00326     DPosition & operator/=(CoordinateType scalar)
00327     {
00328       for (Size i = 0; i < D; ++i)
00329       {
00330         coordinate_[i] /= scalar;
00331       }
00332       return *this;
00333     }
00334 
00336     static Size size()
00337     {
00338       return D;
00339     }
00340 
00342     void clear()
00343     {
00344       for (Size i = 0; i < D; ++i)
00345       {
00346         coordinate_[i] = (CoordinateType) 0;
00347       }
00348     }
00349 
00351 
00354 
00355     inline static const DPosition zero()
00356     {
00357       return DPosition(0);
00358     }
00359 
00361     inline static const DPosition minPositive()
00362     {
00363       return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::min)());
00364     }
00365 
00367     inline static const DPosition minNegative()
00368     {
00369       return DPosition(-(std::numeric_limits<typename DPosition::CoordinateType>::max)());
00370     }
00371 
00373     inline static const DPosition maxPositive()
00374     {
00375       return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::max)());
00376     }
00377 
00379 
00382 
00383     ConstIterator begin() const
00384     {
00385       return &(coordinate_[0]);
00386     }
00387 
00389     ConstIterator end() const
00390     {
00391       return &(coordinate_[0]) + D;
00392     }
00393 
00395     Iterator begin()
00396     {
00397       return &(coordinate_[0]);
00398     }
00399 
00401     Iterator end()
00402     {
00403       return &(coordinate_[0]) + D;
00404     }
00405 
00407 
00408 protected:
00409     CoordinateType coordinate_[D];
00410 
00411   };   // DPosition
00412 
00414   template <UInt D, typename TCoordinateType>
00415   DPosition<D, TCoordinateType> operator*(DPosition<D, TCoordinateType> position, typename DPosition<D, TCoordinateType>::CoordinateType scalar)
00416   {
00417     for (Size i = 0; i < D; ++i)
00418     {
00419       position[i] *= scalar;
00420     }
00421     return position;
00422   }
00423 
00425   template <UInt D, typename TCoordinateType>
00426   DPosition<D, TCoordinateType> operator*(typename DPosition<D, TCoordinateType>::CoordinateType scalar, DPosition<D, TCoordinateType> position)
00427   {
00428     for (Size i = 0; i < D; ++i) 
00429     {
00430       position[i] *= scalar;
00431     }
00432     return position;
00433   }
00434 
00436   template <UInt D, typename TCoordinateType>
00437   DPosition<D, TCoordinateType> operator/(DPosition<D, TCoordinateType> position, typename DPosition<D, TCoordinateType>::CoordinateType scalar)
00438   {
00439     for (Size i = 0; i < D; ++i)
00440     {
00441       position[i] /= scalar;
00442     }
00443     return position;
00444   }
00445 
00447   template <UInt D, typename TCoordinateType>
00448   std::ostream & operator<<(std::ostream & os, const DPosition<D, TCoordinateType> & pos)
00449   {
00450     os << precisionWrapper(pos[0]);
00451     for (UInt i = 1; i < D; ++i)
00452     {
00453       os << ' ' << precisionWrapper(pos[i]);
00454     }
00455     return os;
00456   }
00457 
00458 } // namespace OpenMS
00459 
00460 #endif // OPENMS_DATASTRUCTURES_DPOSITION_H

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