OpenMS
DIntervalBase.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: Clemens Groepl, Marc Sturm $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
12 #include <OpenMS/CONCEPT/Types.h>
13 
14 #include <utility>
15 
16 namespace OpenMS
17 {
18  namespace Internal
19  {
28  template <UInt D>
30  {
31 public:
32 
38  enum {DIMENSION = D};
42  typedef typename PositionType::CoordinateType CoordinateType;
44 
47 
54  min_(PositionType::maxPositive()),
55  max_(PositionType::minNegative())
56  {
57  }
58 
61  min_(rhs.min_),
62  max_(rhs.max_)
63  {
64  }
65 
67  DIntervalBase(DIntervalBase&&) noexcept = default;
68 
70  DIntervalBase& operator=(const DIntervalBase& rhs)
71  {
72  min_ = rhs.min_;
73  max_ = rhs.max_;
74  return *this;
75  }
76 
79  {
80  }
81 
85  DIntervalBase(PositionType const& minimum, PositionType const& maximum) :
86  min_(minimum),
87  max_(maximum)
88  {
89  normalize_();
90  }
91 
93 
96 
98  inline PositionType const& minPosition() const
99  {
100  return min_;
101  }
102 
104  inline PositionType const& maxPosition() const
105  {
106  return max_;
107  }
108 
115  void setMin(PositionType const& position)
116  {
117  min_ = position;
118  for (UInt i = 0; i < DIMENSION; ++i)
119  {
120  if (min_[i] > max_[i]) max_[i] = min_[i];
121  }
122  }
123 
130  void setMax(PositionType const& position)
131  {
132  max_ = position;
133  for (UInt i = 0; i < DIMENSION; ++i)
134  {
135  if (min_[i] > max_[i]) min_[i] = max_[i];
136  }
137  }
138 
142  void setMinMax(PositionType const& min, PositionType const& max)
143  {
144  min_ = min;
145  max_ = max;
146  normalize_();
147  }
148 
154  template <UInt D2>
155  void assign(const DIntervalBase<D2> rhs)
156  {
157  for (UInt i = 0; i < std::min(D, D2); ++i)
158  {
159  min_[i] = rhs.minPosition()[i];
160  max_[i] = rhs.maxPosition()[i];
161  }
162  }
163 
164  //}@
165 
169  bool operator==(const DIntervalBase& rhs) const
170  {
171  return (min_ == rhs.min_) && (max_ == rhs.max_);
172  }
173 
175  bool operator!=(const DIntervalBase& rhs) const
176  {
177  return !(operator==(rhs));
178  }
179 
181  {
182  DIntervalBase result(*this);
183  result += point;
184  return result;
185  }
186 
188  {
189  this->min_ += point;
190  this->max_ += point;
191  return *this;
192  }
193 
195  {
196  DIntervalBase result(*this);
197  result -= point;
198  return result;
199  }
200 
202  {
203  this->min_ -= point;
204  this->max_ -= point;
205  return *this;
206  }
207 
209  inline void clear()
210  {
211  *this = empty;
212  }
213 
216  bool isEmpty() const
217  {
218  return *this == empty;
219  }
220 
222  bool isEmpty(UInt dim) const
223  {
224  return DIntervalBase<1>(std::make_pair(min_[dim], max_[dim])) == DIntervalBase<1>::empty;
225  }
226 
228  void setDimMinMax(UInt dim, const DIntervalBase<1>& min_max)
229  {
230  min_[dim] = min_max.min_[0];
231  max_[dim] = min_max.max_[0];
232  }
233 
234  // make all other templates friends to allow accessing min_ and max_ in setDimMinMax
235  template<UInt> friend class DIntervalBase;
236 
238 
241 
244  {
246  center += max_;
247  center /= 2;
248  return center;
249  }
250 
253  {
254  return max_ - min_;
255  }
256 
258  static DIntervalBase const empty;
260  static DIntervalBase const zero;
261 
262  //}@
263 
266 
268  inline CoordinateType minX() const
269  {
270  return min_[0];
271  }
272 
274  inline CoordinateType minY() const
275  {
276  return min_[1];
277  }
278 
280  inline CoordinateType maxX() const
281  {
282  return max_[0];
283  }
284 
286  inline CoordinateType maxY() const
287  {
288  return max_[1];
289  }
290 
293  {
294  min_[0] = c;
295  if (min_[0] > max_[0]) max_[0] = min_[0];
296  }
297 
300  {
301  min_[1] = c;
302  if (min_[1] > max_[1]) max_[1] = min_[1];
303  }
304 
307  {
308  max_[0] = c;
309  if (min_[0] > max_[0]) min_[0] = max_[0];
310  }
311 
314  {
315  max_[1] = c;
316  if (min_[1] > max_[1]) min_[1] = max_[1];
317  }
318 
320  inline CoordinateType width() const
321  {
322  return max_[0] - min_[0];
323  }
324 
326  inline CoordinateType height() const
327  {
328  return max_[1] - min_[1];
329  }
330 
332 
333 protected:
334 
337 
340 
342  void normalize_()
343  {
344  for (UInt i = 0; i < DIMENSION; ++i)
345  {
346  if (min_[i] > max_[i])
347  {
348  std::swap(min_[i], max_[i]);
349  }
350  }
351  }
352 
354  DIntervalBase(const std::pair<PositionType, PositionType>& pair) :
355  min_(pair.first),
356  max_(pair.second)
357  {
358 
359  }
360 
361  };
362 
363  template <UInt D>
366 
367  template <UInt D>
370 
372  template <UInt D>
373  std::ostream& operator<<(std::ostream& os, const DIntervalBase<D>& rhs)
374  {
375  os << "--DIntervalBase BEGIN--" << std::endl;
376  os << "MIN --> " << rhs.minPosition() << std::endl;
377  os << "MAX --> " << rhs.maxPosition() << std::endl;
378  os << "--DIntervalBase END--" << std::endl;
379  return os;
380  }
381 
382  } // namespace Internal
383 
384 } // namespace OpenMS
385 
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:29
A base class for D-dimensional interval.
Definition: DIntervalBase.h:30
bool isEmpty(UInt dim) const
Is the dimension dim empty? If min==max, the interval is NOT empty!
Definition: DIntervalBase.h:222
void setDimMinMax(UInt dim, const DIntervalBase< 1 > &min_max)
only set interval for a single dimension
Definition: DIntervalBase.h:228
PositionType min_
lower left point
Definition: DIntervalBase.h:336
static DIntervalBase const zero
instance with all positions zero
Definition: DIntervalBase.h:260
PositionType::CoordinateType CoordinateType
Coordinate type of the positions.
Definition: DIntervalBase.h:42
CoordinateType minX() const
Accessor for min_ coordinate minimum.
Definition: DIntervalBase.h:268
PositionType const & maxPosition() const
Accessor to maximum position.
Definition: DIntervalBase.h:104
PositionType max_
upper right point
Definition: DIntervalBase.h:339
bool operator==(const DIntervalBase &rhs) const
Equality operator.
Definition: DIntervalBase.h:169
CoordinateType height() const
Returns the height of the area i.e. the difference of dimension one (Y).
Definition: DIntervalBase.h:326
void setMinY(CoordinateType const c)
Mutator for max_ coordinate of the smaller point.
Definition: DIntervalBase.h:299
DPosition< D > PositionType
Position type.
Definition: DIntervalBase.h:40
DIntervalBase(const DIntervalBase &rhs)
Copy constructor.
Definition: DIntervalBase.h:60
void setMin(PositionType const &position)
Mutator for minimum position.
Definition: DIntervalBase.h:115
CoordinateType width() const
Returns the width of the area i.e. the difference of dimension zero (X).
Definition: DIntervalBase.h:320
CoordinateType maxX() const
Accessor for min_ coordinate maximum.
Definition: DIntervalBase.h:280
void setMaxY(CoordinateType const c)
Mutator for max_ coordinate of the larger point.
Definition: DIntervalBase.h:313
@ DIMENSION
Definition: DIntervalBase.h:38
void assign(const DIntervalBase< D2 > rhs)
Assignment from a DIntervalBase of different dimensions.
Definition: DIntervalBase.h:155
void setMinMax(PositionType const &min, PositionType const &max)
Mutator for minimum and maximum position.
Definition: DIntervalBase.h:142
void setMax(PositionType const &position)
Mutator for maximum position.
Definition: DIntervalBase.h:130
CoordinateType maxY() const
Accessor for max_ coordinate maximum.
Definition: DIntervalBase.h:286
PositionType diagonal() const
Returns the diagonal of the area, i.e. max_ - min_.
Definition: DIntervalBase.h:252
PositionType center() const
Returns the center of the interval.
Definition: DIntervalBase.h:243
DIntervalBase(DIntervalBase &&) noexcept=default
Move constructor.
bool operator!=(const DIntervalBase &rhs) const
Equality operator.
Definition: DIntervalBase.h:175
void setMinX(CoordinateType const c)
Mutator for min_ coordinate of the smaller point.
Definition: DIntervalBase.h:292
DIntervalBase & operator-=(const PositionType &point)
Definition: DIntervalBase.h:201
DIntervalBase operator-(const PositionType &point) const
Definition: DIntervalBase.h:194
~DIntervalBase()
Destructor.
Definition: DIntervalBase.h:78
void normalize_()
normalization to keep all dimensions in the right geometrical order (min_[X] < max_[X])
Definition: DIntervalBase.h:342
void clear()
Make the interval empty.
Definition: DIntervalBase.h:209
DIntervalBase(const std::pair< PositionType, PositionType > &pair)
Protected constructor for the construction of static instances.
Definition: DIntervalBase.h:354
bool isEmpty() const
Definition: DIntervalBase.h:216
DIntervalBase()
Default constructor.
Definition: DIntervalBase.h:53
DIntervalBase & operator+=(const PositionType &point)
Definition: DIntervalBase.h:187
void setMaxX(CoordinateType const c)
Mutator for min_ coordinate of the larger point.
Definition: DIntervalBase.h:306
static DIntervalBase const empty
empty instance
Definition: DIntervalBase.h:258
DIntervalBase operator+(const PositionType &point) const
Definition: DIntervalBase.h:180
CoordinateType minY() const
Accessor for max_ coordinate minimum.
Definition: DIntervalBase.h:274
PositionType const & minPosition() const
Accessor to minimum position.
Definition: DIntervalBase.h:98
DIntervalBase(PositionType const &minimum, PositionType const &maximum)
This constructor sets min_ and max_ directly.
Definition: DIntervalBase.h:85
unsigned int UInt
Unsigned integer type.
Definition: Types.h:64
const double c
Definition: Constants.h:188
std::ostream & operator<<(std::ostream &os, const DIntervalBase< D > &rhs)
Print the contents to a stream.
Definition: DIntervalBase.h:373
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19