OpenMS  2.7.0
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-2021.
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;
131  }
132 
134  DRange& operator=(const DRange& rhs)
135  {
136  Base::operator=(rhs);
137  return *this;
138  }
139 
141  DRange& operator=(const Base& rhs)
142  {
143  Base::operator=(rhs);
144  return *this;
145  }
146 
149  {
150  }
151 
153 
157  bool operator==(const DRange& rhs) const
158  {
159  return Base::operator==(rhs);
160  }
161 
163  bool operator==(const Base& rhs) const
164  {
165  return Base::operator==(rhs);
166  }
167 
174  bool encloses(const PositionType& position) const
175  {
176  for (UInt i = 0; i != D; i++)
177  {
178  if (position[i] < min_[i]) return false;
179 
180  if (position[i] >= max_[i]) return false;
181  }
182  return true;
183  }
184 
187  {
188  if (x < min_[0]) return false;
189 
190  if (x >= max_[0]) return false;
191 
192  if (y < min_[1]) return false;
193 
194  if (y >= max_[1]) return false;
195 
196  return true;
197  }
198 
200  DRange united(const DRange<D>& other_range) const
201  {
202  PositionType united_min;
203  PositionType united_max;
204  DRange<D> united_range = DRange<D>::empty;
205 
206  PositionType other_min = other_range.minPosition();
207  PositionType other_max = other_range.maxPosition();
208 
209  for (Size i = 0; i != D; ++i)
210  {
211  united_min[i] = min_[i] < other_min[i] ? min_[i] : other_min[i];
212  united_max[i] = max_[i] > other_max[i] ? max_[i] : other_max[i];
213  }
214  united_range.setMinMax(united_min, united_max);
215 
216  return united_range;
217  }
218 
225  {
226  //check if r.min_ is in this area
227  if (encloses(range.min_))
228  {
229  //check if r.max_ in this area => Inside / Intersects
230  for (Size i = 0; i != D; i++)
231  {
232  if (range.max_[i] > max_[i])
233  {
234  return Intersects;
235  }
236  }
237  return Inside;
238  }
239  // => r.min_ is not inside this area
240  //check if any r.min_ >= max_ => Disjoint
241  for (Size i = 0; i != D; i++)
242  {
243  if (range.min_[i] >= max_[i])
244  {
245  return Disjoint;
246  }
247  }
248  // => some coordinate of r.min_ has to be smaller than the one of min_
249  //check if all coords of r are smaller than the those of the range
250  for (Size i = 0; i != D; i++)
251  {
252  if (range.max_[i] <= min_[i])
253  {
254  return Disjoint;
255  }
256  }
257  return Intersects;
258  }
259 
266  bool isIntersected(const DRange& range) const
267  {
268  //check if r.min_ is in this area
269  if (encloses(range.min_))
270  {
271  return true;
272  }
273 
274  // => r.min_ is not inside this area
275  //check if any r.min_ >= max_ => Disjoint
276  for (Size i = 0; i != D; i++)
277  {
278  if (range.min_[i] >= max_[i])
279  {
280  return false;
281  }
282  }
283  // => some coordinate of r.min_ has to be smaller than the one of min_
284  //check if all coords of r are smaller than the those of the range
285  for (Size i = 0; i != D; i++)
286  {
287  if (range.max_[i] <= min_[i])
288  {
289  return false;
290  }
291  }
292  return true;
293  }
294 
296  bool isEmpty() const
297  {
298  for (UInt i = 0; i != D; i++)
299  {
300  if (max_[i] <= min_[i])
301  {
302  return true;
303  }
304  }
305  return false;
306  }
307 
319  void extend(double factor)
320  {
321  if (factor < 0)
322  {
323  throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "DRange::extend(): factor must not be negative!");
324  }
325 
326  for (UInt i = 0; i != D; ++i)
327  {
328  Internal::DIntervalBase<1>::CoordinateType extra = (max_[i] - min_[i]) / 2.0 * (factor - 1);
329  min_[i] -= extra;
330  max_[i] += extra;
331  }
332  }
333 
336  {
337  static_assert(D==2);
338  std::swap(min_[0], min_[1]);
339  std::swap(max_[0], max_[1]);
340  return *this;
341  }
342 
344  };
345 
347  template <UInt D>
348  std::ostream& operator<<(std::ostream& os, const DRange<D>& area)
349  {
350  os << "--DRANGE BEGIN--" << std::endl;
351  os << "MIN --> " << area.min_ << std::endl;
352  os << "MAX --> " << area.max_ << std::endl;
353  os << "--DRANGE END--" << std::endl;
354  return os;
355  }
356 
357 } // namespace OpenMS
358 
A D-dimensional half-open interval.
Definition: DRange.h:62
PositionType min_
lower left point
Definition: DIntervalBase.h:311
bool encloses(const PositionType &position) const
Checks whether this range contains a certain point.
Definition: DRange.h:174
Base::PositionType PositionType
Position type.
Definition: DRange.h:74
PositionType max_
upper right point
Definition: DIntervalBase.h:314
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:200
bool encloses(CoordinateType x, CoordinateType y) const
2D-version of encloses for convenience only
Definition: DRange.h:186
void extend(double factor)
Extends the range in all dimensions by a certain multiplier.
Definition: DRange.h:319
DRange()
Default constructor.
Definition: DRange.h:97
bool operator==(const Base &rhs) const
Equality operator.
Definition: DRange.h:163
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:148
DRange & operator=(const Base &rhs)
Assignment operator for the base class.
Definition: DRange.h:141
DRangeIntersection intersects(const DRange &range) const
Checks how this range intersects with another range.
Definition: DRange.h:224
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:335
DRange(CoordinateType minx, CoordinateType miny, CoordinateType maxx, CoordinateType maxy)
Convenient constructor for DRange<2>
Definition: DRange.h:124
@ DIMENSION
Definition: DRange.h:70
bool operator==(const DRange &rhs) const
Equality operator.
Definition: DRange.h:157
bool isEmpty() const
Checks if the range is empty.
Definition: DRange.h:296
DRange & operator=(const DRange &rhs)
Assignment operator.
Definition: DRange.h:134
bool isIntersected(const DRange &range) const
Checks whether this range intersects with another range.
Definition: DRange.h:266
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:311
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:314
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
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:47
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
Size< TNeedle >::Type position(const PatternAuxData< TNeedle > &dh)
Definition: AhoCorasickAmbiguous.h:563