OpenMS
DRange.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg$
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
38 #include <OpenMS/CONCEPT/Macros.h>
39 #include <OpenMS/CONCEPT/Types.h>
40 
41 namespace OpenMS
42 {
59  template <UInt D>
60  class DRange :
61  public Internal::DIntervalBase<D>
62  {
63 public:
64 
70  enum {DIMENSION = D};
74  typedef typename Base::PositionType PositionType;
79  {
82  Inside
83  };
84 
86 
87  using Base::min_;
88  using Base::max_;
89 
97  DRange() :
98  Base()
99  {
100  }
101 
103  DRange(const PositionType& lower, const PositionType& upper) :
104  Base(lower, upper)
105  {
106  }
107 
109  DRange(const DRange& range) :
110  Base(range)
111  {
112  }
113 
115  DRange(DRange&&) noexcept = default;
116 
118  DRange(const Base& range) :
119  Base(range)
120  {
121  }
122 
125  {
126  static_assert(D == 2);
127  min_[0] = minx;
128  min_[1] = miny;
129  max_[0] = maxx;
130  max_[1] = maxy;
132  }
133 
135  DRange& operator=(const DRange& rhs)
136  {
137  Base::operator=(rhs);
138  return *this;
139  }
140 
142  DRange& operator=(const Base& rhs)
143  {
144  Base::operator=(rhs);
145  return *this;
146  }
147 
150  {
151  }
152 
154 
158  bool operator==(const DRange& rhs) const
159  {
160  return Base::operator==(rhs);
161  }
162 
164  bool operator==(const Base& rhs) const
165  {
166  return Base::operator==(rhs);
167  }
168 
175  bool encloses(const PositionType& position) const
176  {
177  for (UInt i = 0; i != D; i++)
178  {
179  if (position[i] < min_[i]) return false;
180 
181  if (position[i] >= max_[i]) return false;
182  }
183  return true;
184  }
185 
188  {
189  if (x < min_[0]) return false;
190 
191  if (x >= max_[0]) return false;
192 
193  if (y < min_[1]) return false;
194 
195  if (y >= max_[1]) return false;
196 
197  return true;
198  }
199 
201  DRange united(const DRange<D>& other_range) const
202  {
203  PositionType united_min;
204  PositionType united_max;
205  DRange<D> united_range = DRange<D>::empty;
206 
207  PositionType other_min = other_range.minPosition();
208  PositionType other_max = other_range.maxPosition();
209 
210  for (Size i = 0; i != D; ++i)
211  {
212  united_min[i] = min_[i] < other_min[i] ? min_[i] : other_min[i];
213  united_max[i] = max_[i] > other_max[i] ? max_[i] : other_max[i];
214  }
215  united_range.setMinMax(united_min, united_max);
216 
217  return united_range;
218  }
219 
226  {
227  //check if r.min_ is in this area
228  if (encloses(range.min_))
229  {
230  //check if r.max_ in this area => Inside / Intersects
231  for (Size i = 0; i != D; i++)
232  {
233  if (range.max_[i] > max_[i])
234  {
235  return Intersects;
236  }
237  }
238  return Inside;
239  }
240  // => r.min_ is not inside this area
241  //check if any r.min_ >= max_ => Disjoint
242  for (Size i = 0; i != D; i++)
243  {
244  if (range.min_[i] >= max_[i])
245  {
246  return Disjoint;
247  }
248  }
249  // => some coordinate of r.min_ has to be smaller than the one of min_
250  //check if all coords of r are smaller than the those of the range
251  for (Size i = 0; i != D; i++)
252  {
253  if (range.max_[i] <= min_[i])
254  {
255  return Disjoint;
256  }
257  }
258  return Intersects;
259  }
260 
267  bool isIntersected(const DRange& range) const
268  {
269  //check if r.min_ is in this area
270  if (encloses(range.min_))
271  {
272  return true;
273  }
274 
275  // => r.min_ is not inside this area
276  //check if any r.min_ >= max_ => Disjoint
277  for (Size i = 0; i != D; i++)
278  {
279  if (range.min_[i] >= max_[i])
280  {
281  return false;
282  }
283  }
284  // => some coordinate of r.min_ has to be smaller than the one of min_
285  //check if all coords of r are smaller than the those of the range
286  for (Size i = 0; i != D; i++)
287  {
288  if (range.max_[i] <= min_[i])
289  {
290  return false;
291  }
292  }
293  return true;
294  }
295 
308  DRange<D>& extend(double factor)
309  {
310  if (factor < 0)
311  {
312  throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "DRange::extend(): factor must not be negative!");
313  }
314 
315  for (UInt i = 0; i != D; ++i)
316  {
317  Internal::DIntervalBase<1>::CoordinateType extra = (max_[i] - min_[i]) / 2.0 * (factor - 1);
318  min_[i] -= extra;
319  max_[i] += extra;
320  }
321  return *this;
322  }
323 
336  DRange<D>& extend(typename Base::PositionType addition)
337  {
338  addition /= 2;
339  min_ -= addition;
340  max_ += addition;
341  for (UInt i = 0; i != D; ++i)
342  {
343  // invalid range --> reduce to single center point
344  if (min_[i] > max_[i]) min_[i] = max_[i] = (min_[i] + max_[i]) / 2;
345  }
346  return *this;
347  }
348 
350  {
351  typename Base::PositionType extend_by {};
352  for (UInt i = 0; i != D; ++i)
353  {
354  // invalid range --> reduce to single center point
355  if (max_[i] - min_[i] < min_span[i])
356  {
357  extend_by[i] = min_span[i] - (max_[i] - min_[i]); // add whatever is missing to get to min_span
358  }
359  }
360  extend(extend_by);
361  return *this;
362  }
363 
366  {
367  static_assert(D==2);
368  std::swap(min_[0], min_[1]);
369  std::swap(max_[0], max_[1]);
370  return *this;
371  }
372 
377  void pullIn(DPosition<D>& point) const
378  {
379  for (UInt i = 0; i != D; ++i)
380  {
381  point[i] = std::max(min_[i], std::min(point[i], max_[i]));
382  }
383  }
384 
386  };
387 
389  template <UInt D>
390  std::ostream& operator<<(std::ostream& os, const DRange<D>& area)
391  {
392  os << "--DRANGE BEGIN--\n";
393  os << "MIN --> " << area.min_ << '\n';
394  os << "MAX --> " << area.max_ << '\n';
395  os << "--DRANGE END--\n";
396  return os;
397  }
398 
399 } // namespace OpenMS
400 
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:55
A D-dimensional half-open interval.
Definition: DRange.h:62
PositionType min_
lower left point
Definition: DIntervalBase.h:362
bool encloses(const PositionType &position) const
Checks whether this range contains a certain point.
Definition: DRange.h:175
DRange< D > & ensureMinSpan(typename Base::PositionType min_span)
Definition: DRange.h:349
Base::PositionType PositionType
Position type.
Definition: DRange.h:74
DRange< D > & extend(double factor)
Extends the range in all dimensions by a certain multiplier.
Definition: DRange.h:308
PositionType max_
upper right point
Definition: DIntervalBase.h:365
DRangeIntersection
Types that describe the kind of intersection between two ranges.
Definition: DRange.h:79
@ Disjoint
No intersection.
Definition: DRange.h:80
@ Inside
One contains the other.
Definition: DRange.h:82
@ Intersects
Intersection.
Definition: DRange.h:81
DRange united(const DRange< D > &other_range) const
Returns the smallest range containing this range and other_range.
Definition: DRange.h:201
bool encloses(CoordinateType x, CoordinateType y) const
2D-version of encloses for convenience only
Definition: DRange.h:187
DRange()
Default constructor.
Definition: DRange.h:97
bool operator==(const Base &rhs) const
Equality operator.
Definition: DRange.h:164
DRange(const PositionType &lower, const PositionType &upper)
Constructor that takes two Points and constructs a range.
Definition: DRange.h:103
DRange(DRange &&) noexcept=default
Move constructor.
~DRange()
Destructor.
Definition: DRange.h:149
DRange & operator=(const Base &rhs)
Assignment operator for the base class.
Definition: DRange.h:142
DRangeIntersection intersects(const DRange &range) const
Checks how this range intersects with another range.
Definition: DRange.h:225
DRange(const DRange &range)
Copy constructor.
Definition: DRange.h:109
Internal::DIntervalBase< D > Base
Base class type.
Definition: DRange.h:72
Base::CoordinateType CoordinateType
Coordinate type of the positions.
Definition: DRange.h:76
DRange< D > & swapDimensions()
swaps dimensions for 2D data (i.e. x and y coordinates)
Definition: DRange.h:365
DRange(CoordinateType minx, CoordinateType miny, CoordinateType maxx, CoordinateType maxy)
Convenient constructor for DRange<2>
Definition: DRange.h:124
void pullIn(DPosition< D > &point) const
Make sure point is inside the current area.
Definition: DRange.h:377
DRange< D > & extend(typename Base::PositionType addition)
Extends the range in all dimensions by a certain amount.
Definition: DRange.h:336
@ DIMENSION
Definition: DRange.h:70
bool operator==(const DRange &rhs) const
Equality operator.
Definition: DRange.h:158
DRange & operator=(const DRange &rhs)
Assignment operator.
Definition: DRange.h:135
bool isIntersected(const DRange &range) const
Checks whether this range intersects with another range.
Definition: DRange.h:267
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition: Exception.h:341
A base class for D-dimensional interval.
Definition: DIntervalBase.h:56
PositionType min_
lower left point
Definition: DIntervalBase.h:362
DIntervalBase & operator=(const DIntervalBase &rhs)
Assignment operator.
Definition: DIntervalBase.h:96
PositionType::CoordinateType CoordinateType
Coordinate type of the positions.
Definition: DIntervalBase.h:68
PositionType const & maxPosition() const
Accessor to maximum position.
Definition: DIntervalBase.h:130
PositionType max_
upper right point
Definition: DIntervalBase.h:365
bool operator==(const DIntervalBase &rhs) const
Equality operator.
Definition: DIntervalBase.h:195
DPosition< D > PositionType
Position type.
Definition: DIntervalBase.h:66
void setMinMax(PositionType const &min, PositionType const &max)
Mutator for minimum and maximum position.
Definition: DIntervalBase.h:168
void normalize_()
normalization to keep all dimensions in the right geometrical order (min_[X] < max_[X])
Definition: DIntervalBase.h:368
PositionType const & minPosition() const
Accessor to minimum position.
Definition: DIntervalBase.h:124
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)