00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef OPENMS_DATASTRUCTURES_DINTERVALBASE_H
00028 #define OPENMS_DATASTRUCTURES_DINTERVALBASE_H
00029
00030 #include <OpenMS/DATASTRUCTURES/DPosition.h>
00031
00032 #include <utility>
00033
00034 namespace OpenMS
00035 {
00036 namespace Internal
00037 {
00046 template <UInt D>
00047 class DIntervalBase
00048 {
00049 public:
00050
00055
00056 enum { DIMENSION = D };
00058 typedef DPosition<D> PositionType;
00060 typedef typename PositionType::CoordinateType CoordinateType;
00062
00065
00071 DIntervalBase()
00072 : min_(PositionType::max),
00073 max_(PositionType::min_negative )
00074 {
00075
00076 }
00077
00079 DIntervalBase(const DIntervalBase& rhs)
00080 : min_(rhs.min_),
00081 max_(rhs.max_)
00082 {
00083
00084 }
00085
00087 DIntervalBase & operator=(const DIntervalBase & rhs)
00088 {
00089 min_ = rhs.min_;
00090 max_ = rhs.max_;
00091 return *this;
00092 }
00093
00095 ~DIntervalBase()
00096 {
00097
00098 }
00099
00103 DIntervalBase( PositionType const & minimum, PositionType const & maximum )
00104 : min_(minimum),
00105 max_(maximum)
00106 {
00107 normalize_();
00108 }
00109
00111
00114
00116 PositionType const & min() const
00117 {
00118 return min_;
00119 }
00120
00122 PositionType const & max() const
00123 {
00124 return max_;
00125 }
00126
00133 void setMin(PositionType const & position)
00134 {
00135 min_ = position;
00136 for ( UInt i = 0; i < DIMENSION; ++i )
00137 {
00138 if (min_[i]>max_[i]) max_[i] = min_[i];
00139 }
00140 }
00141
00148 void setMax(PositionType const & position)
00149 {
00150 max_ = position;
00151 for ( UInt i = 0; i < DIMENSION; ++i )
00152 {
00153 if (min_[i]>max_[i]) min_[i] = max_[i];
00154 }
00155 }
00156
00160 void setMinMax(PositionType const & min, PositionType const & max)
00161 {
00162 min_ = min;
00163 max_ = max;
00164 normalize_();
00165 }
00166
00172 template <UInt D2>
00173 void assign(const DIntervalBase<D2> rhs)
00174 {
00175 for (UInt i=0; i<std::min(D,D2); ++i)
00176 {
00177 min_[i] = rhs.min()[i];
00178 max_[i] = rhs.max()[i];
00179 }
00180 }
00181
00182
00183
00186
00187 bool operator == (const DIntervalBase& rhs) const throw()
00188 {
00189 return (min_ == rhs.min_) && (max_ == rhs.max_);
00190 }
00191
00193 bool operator != (const DIntervalBase& rhs) const throw()
00194 {
00195 return !(operator==(rhs));
00196 }
00197
00199 inline void clear()
00200 {
00201 *this = empty;
00202 }
00203
00205
00208
00210 PositionType center() const
00211 {
00212 PositionType center(min_);
00213 center += max_;
00214 center /= 2;
00215 return center;
00216 }
00217
00219 PositionType diagonal() const
00220 {
00221 return max_ - min_;
00222 }
00223
00225 static DIntervalBase const empty;
00227 static DIntervalBase const zero;
00228
00229
00230
00233
00235 CoordinateType minX() const
00236 {
00237 return min_[0];
00238 }
00239
00241 CoordinateType minY() const
00242 {
00243 return min_[1];
00244 }
00245
00247 CoordinateType maxX() const
00248 {
00249 return max_[0];
00250 }
00251
00253 CoordinateType maxY() const
00254 {
00255 return max_[1];
00256 }
00257
00259 void setMinX(CoordinateType const c)
00260 {
00261 min_[0] = c;
00262 if (min_[0]>max_[0]) max_[0] = min_[0];
00263 }
00264
00266 void setMinY(CoordinateType const c)
00267 {
00268 min_[1] = c;
00269 if (min_[1]>max_[1]) max_[1] = min_[1];
00270 }
00271
00273 void setMaxX(CoordinateType const c)
00274 {
00275 max_[0] = c;
00276 if (min_[0]>max_[0]) min_[0] = max_[0];
00277 }
00278
00280 void setMaxY(CoordinateType const c)
00281 {
00282 max_[1] = c;
00283 if (min_[1]>max_[1]) min_[1] = max_[1];
00284 }
00285
00287 CoordinateType width() const
00288 {
00289 return max_[0] - min_[0];
00290 }
00291
00293 CoordinateType height() const
00294 {
00295 return max_[1] - min_[1];
00296 }
00297
00299
00300 protected:
00301
00303 PositionType min_;
00304
00306 PositionType max_;
00307
00309 void normalize_()
00310 {
00311 for ( UInt i = 0; i < DIMENSION; ++i )
00312 {
00313 if (min_[i]>max_[i])
00314 {
00315 std::swap(min_[i],max_[i]);
00316 }
00317 }
00318 }
00319
00321 DIntervalBase(const std::pair< PositionType , PositionType >& pair )
00322 : min_(pair.first),
00323 max_(pair.second)
00324 {
00325
00326 }
00327
00328 };
00329
00330 template <UInt D>
00331 DIntervalBase<D> const DIntervalBase<D>::zero
00332 = DIntervalBase<D>( DIntervalBase<D>::PositionType::zero,
00333 DIntervalBase<D>::PositionType::zero );
00334
00335 template <UInt D>
00336 DIntervalBase<D> const DIntervalBase<D>::empty
00337 = DIntervalBase<D>(std::make_pair(DIntervalBase<D>::PositionType::max, DIntervalBase<D>::PositionType::min_negative));
00338
00340 template <UInt D>
00341 std::ostream& operator << (std::ostream& os, const DIntervalBase<D>& rhs)
00342 {
00343 os << "--DIntervalBase BEGIN--"<<std::endl;
00344 os << "MIN --> " << rhs.min() << std::endl;
00345 os << "MAX --> " << rhs.max() << std::endl;
00346 os << "--DIntervalBase END--"<<std::endl;
00347 return os;
00348 }
00349
00350 }
00351
00352 }
00353
00354 #endif // OPENMS_KERNEL_DINTERVALBASE_H