OpenMS
DRange.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg$
6 // $Authors: Marc Sturm $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
12 #include <OpenMS/CONCEPT/Macros.h>
13 #include <OpenMS/CONCEPT/Types.h>
14 
15 namespace OpenMS
16 {
33  template <UInt D>
34  class DRange :
35  public Internal::DIntervalBase<D>
36  {
37 public:
38 
44  enum {DIMENSION = D};
48  typedef typename Base::PositionType PositionType;
53  {
56  Inside
57  };
58 
60 
61  using Base::min_;
62  using Base::max_;
63 
71  DRange() :
72  Base()
73  {
74  }
75 
77  DRange(const PositionType& lower, const PositionType& upper) :
78  Base(lower, upper)
79  {
80  }
81 
83  DRange(const DRange& range) :
84  Base(range)
85  {
86  }
87 
89  DRange(DRange&&) noexcept = default;
90 
92  DRange(const Base& range) :
93  Base(range)
94  {
95  }
96 
99  {
100  static_assert(D == 2);
101  min_[0] = minx;
102  min_[1] = miny;
103  max_[0] = maxx;
104  max_[1] = maxy;
106  }
107 
109  DRange& operator=(const DRange& rhs)
110  {
111  Base::operator=(rhs);
112  return *this;
113  }
114 
116  DRange& operator=(const Base& rhs)
117  {
118  Base::operator=(rhs);
119  return *this;
120  }
121 
124  {
125  }
126 
128 
132  bool operator==(const DRange& rhs) const
133  {
134  return Base::operator==(rhs);
135  }
136 
138  bool operator==(const Base& rhs) const
139  {
140  return Base::operator==(rhs);
141  }
142 
149  bool encloses(const PositionType& position) const
150  {
151  for (UInt i = 0; i != D; i++)
152  {
153  if (position[i] < min_[i]) return false;
154 
155  if (position[i] >= max_[i]) return false;
156  }
157  return true;
158  }
159 
162  {
163  if (x < min_[0]) return false;
164 
165  if (x >= max_[0]) return false;
166 
167  if (y < min_[1]) return false;
168 
169  if (y >= max_[1]) return false;
170 
171  return true;
172  }
173 
175  DRange united(const DRange<D>& other_range) const
176  {
177  PositionType united_min;
178  PositionType united_max;
179  DRange<D> united_range = DRange<D>::empty;
180 
181  PositionType other_min = other_range.minPosition();
182  PositionType other_max = other_range.maxPosition();
183 
184  for (Size i = 0; i != D; ++i)
185  {
186  united_min[i] = min_[i] < other_min[i] ? min_[i] : other_min[i];
187  united_max[i] = max_[i] > other_max[i] ? max_[i] : other_max[i];
188  }
189  united_range.setMinMax(united_min, united_max);
190 
191  return united_range;
192  }
193 
200  {
201  //check if r.min_ is in this area
202  if (encloses(range.min_))
203  {
204  //check if r.max_ in this area => Inside / Intersects
205  for (Size i = 0; i != D; i++)
206  {
207  if (range.max_[i] > max_[i])
208  {
209  return Intersects;
210  }
211  }
212  return Inside;
213  }
214  // => r.min_ is not inside this area
215  //check if any r.min_ >= max_ => Disjoint
216  for (Size i = 0; i != D; i++)
217  {
218  if (range.min_[i] >= max_[i])
219  {
220  return Disjoint;
221  }
222  }
223  // => some coordinate of r.min_ has to be smaller than the one of min_
224  //check if all coords of r are smaller than the those of the range
225  for (Size i = 0; i != D; i++)
226  {
227  if (range.max_[i] <= min_[i])
228  {
229  return Disjoint;
230  }
231  }
232  return Intersects;
233  }
234 
241  bool isIntersected(const DRange& range) const
242  {
243  //check if r.min_ is in this area
244  if (encloses(range.min_))
245  {
246  return true;
247  }
248 
249  // => r.min_ is not inside this area
250  //check if any r.min_ >= max_ => Disjoint
251  for (Size i = 0; i != D; i++)
252  {
253  if (range.min_[i] >= max_[i])
254  {
255  return false;
256  }
257  }
258  // => some coordinate of r.min_ has to be smaller than the one of min_
259  //check if all coords of r are smaller than the those of the range
260  for (Size i = 0; i != D; i++)
261  {
262  if (range.max_[i] <= min_[i])
263  {
264  return false;
265  }
266  }
267  return true;
268  }
269 
282  DRange<D>& extend(double factor)
283  {
284  if (factor < 0)
285  {
286  throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "DRange::extend(): factor must not be negative!");
287  }
288 
289  for (UInt i = 0; i != D; ++i)
290  {
291  Internal::DIntervalBase<1>::CoordinateType extra = (max_[i] - min_[i]) / 2.0 * (factor - 1);
292  min_[i] -= extra;
293  max_[i] += extra;
294  }
295  return *this;
296  }
297 
310  DRange<D>& extend(typename Base::PositionType addition)
311  {
312  addition /= 2;
313  min_ -= addition;
314  max_ += addition;
315  for (UInt i = 0; i != D; ++i)
316  {
317  // invalid range --> reduce to single center point
318  if (min_[i] > max_[i]) min_[i] = max_[i] = (min_[i] + max_[i]) / 2;
319  }
320  return *this;
321  }
322 
324  {
325  typename Base::PositionType extend_by {};
326  for (UInt i = 0; i != D; ++i)
327  {
328  // invalid range --> reduce to single center point
329  if (max_[i] - min_[i] < min_span[i])
330  {
331  extend_by[i] = min_span[i] - (max_[i] - min_[i]); // add whatever is missing to get to min_span
332  }
333  }
334  extend(extend_by);
335  return *this;
336  }
337 
340  {
341  static_assert(D==2);
342  std::swap(min_[0], min_[1]);
343  std::swap(max_[0], max_[1]);
344  return *this;
345  }
346 
351  void pullIn(DPosition<D>& point) const
352  {
353  for (UInt i = 0; i != D; ++i)
354  {
355  point[i] = std::max(min_[i], std::min(point[i], max_[i]));
356  }
357  }
358 
360  };
361 
363  template <UInt D>
364  std::ostream& operator<<(std::ostream& os, const DRange<D>& area)
365  {
366  os << "--DRANGE BEGIN--\n";
367  os << "MIN --> " << area.min_ << '\n';
368  os << "MAX --> " << area.max_ << '\n';
369  os << "--DRANGE END--\n";
370  return os;
371  }
372 
373 } // namespace OpenMS
374 
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:29
A D-dimensional half-open interval.
Definition: DRange.h:36
PositionType min_
lower left point
Definition: DIntervalBase.h:336
bool encloses(const PositionType &position) const
Checks whether this range contains a certain point.
Definition: DRange.h:149
DRange< D > & ensureMinSpan(typename Base::PositionType min_span)
Definition: DRange.h:323
Base::PositionType PositionType
Position type.
Definition: DRange.h:48
DRange< D > & extend(double factor)
Extends the range in all dimensions by a certain multiplier.
Definition: DRange.h:282
PositionType max_
upper right point
Definition: DIntervalBase.h:339
DRangeIntersection
Types that describe the kind of intersection between two ranges.
Definition: DRange.h:53
@ Disjoint
No intersection.
Definition: DRange.h:54
@ Inside
One contains the other.
Definition: DRange.h:56
@ Intersects
Intersection.
Definition: DRange.h:55
DRange united(const DRange< D > &other_range) const
Returns the smallest range containing this range and other_range.
Definition: DRange.h:175
bool encloses(CoordinateType x, CoordinateType y) const
2D-version of encloses for convenience only
Definition: DRange.h:161
DRange()
Default constructor.
Definition: DRange.h:71
bool operator==(const Base &rhs) const
Equality operator.
Definition: DRange.h:138
DRange(const PositionType &lower, const PositionType &upper)
Constructor that takes two Points and constructs a range.
Definition: DRange.h:77
DRange(DRange &&) noexcept=default
Move constructor.
~DRange()
Destructor.
Definition: DRange.h:123
DRange & operator=(const Base &rhs)
Assignment operator for the base class.
Definition: DRange.h:116
DRangeIntersection intersects(const DRange &range) const
Checks how this range intersects with another range.
Definition: DRange.h:199
DRange(const DRange &range)
Copy constructor.
Definition: DRange.h:83
Internal::DIntervalBase< D > Base
Base class type.
Definition: DRange.h:46
Base::CoordinateType CoordinateType
Coordinate type of the positions.
Definition: DRange.h:50
DRange< D > & swapDimensions()
swaps dimensions for 2D data (i.e. x and y coordinates)
Definition: DRange.h:339
DRange(CoordinateType minx, CoordinateType miny, CoordinateType maxx, CoordinateType maxy)
Convenient constructor for DRange<2>
Definition: DRange.h:98
void pullIn(DPosition< D > &point) const
Make sure point is inside the current area.
Definition: DRange.h:351
DRange< D > & extend(typename Base::PositionType addition)
Extends the range in all dimensions by a certain amount.
Definition: DRange.h:310
@ DIMENSION
Definition: DRange.h:44
bool operator==(const DRange &rhs) const
Equality operator.
Definition: DRange.h:132
DRange & operator=(const DRange &rhs)
Assignment operator.
Definition: DRange.h:109
bool isIntersected(const DRange &range) const
Checks whether this range intersects with another range.
Definition: DRange.h:241
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition: Exception.h:316
A base class for D-dimensional interval.
Definition: DIntervalBase.h:30
PositionType min_
lower left point
Definition: DIntervalBase.h:336
DIntervalBase & operator=(const DIntervalBase &rhs)
Assignment operator.
Definition: DIntervalBase.h:70
PositionType::CoordinateType CoordinateType
Coordinate type of the positions.
Definition: DIntervalBase.h:42
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
DPosition< D > PositionType
Position type.
Definition: DIntervalBase.h:40
void setMinMax(PositionType const &min, PositionType const &max)
Mutator for minimum and maximum position.
Definition: DIntervalBase.h:142
void normalize_()
normalization to keep all dimensions in the right geometrical order (min_[X] < max_[X])
Definition: DIntervalBase.h:342
PositionType const & minPosition() const
Accessor to minimum position.
Definition: DIntervalBase.h:98
unsigned int UInt
Unsigned integer type.
Definition: Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)