OpenMS  3.0.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-2022.
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 
69  enum {DIMENSION = D};
74  typedef typename Base::PositionType PositionType;
79  {
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 
156  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 
307  DRange<D>& extend(double factor)
308  {
309  if (factor < 0)
310  {
311  throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "DRange::extend(): factor must not be negative!");
312  }
313 
314  for (UInt i = 0; i != D; ++i)
315  {
316  Internal::DIntervalBase<1>::CoordinateType extra = (max_[i] - min_[i]) / 2.0 * (factor - 1);
317  min_[i] -= extra;
318  max_[i] += extra;
319  }
320  return *this;
321  }
322 
325  {
326  static_assert(D==2);
327  std::swap(min_[0], min_[1]);
328  std::swap(max_[0], max_[1]);
329  return *this;
330  }
331 
336  void pullIn(DPosition<D>& point) const
337  {
338  for (UInt i = 0; i != D; ++i)
339  {
340  point[i] = std::max(min_[i], std::min(point[i], max_[i]));
341  }
342  }
343 
345  };
346 
348  template <UInt D>
349  std::ostream& operator<<(std::ostream& os, const DRange<D>& area)
350  {
351  os << "--DRANGE BEGIN--\n";
352  os << "MIN --> " << area.min_ << '\n';
353  os << "MAX --> " << area.max_ << '\n';
354  os << "--DRANGE END--\n";
355  return os;
356  }
357 
358 } // namespace OpenMS
359 
OpenMS::DRange::united
DRange united(const DRange< D > &other_range) const
Returns the smallest range containing this range and other_range.
Definition: DRange.h:200
OpenMS::DRange::Inside
One contains the other.
Definition: DRange.h:82
OpenMS::DRange::Disjoint
No intersection.
Definition: DRange.h:80
double
OpenMS::DRange::max_
PositionType max_
upper right point
Definition: DIntervalBase.h:365
OpenMS::DRange::DRange
DRange(CoordinateType minx, CoordinateType miny, CoordinateType maxx, CoordinateType maxy)
Convenient constructor for DRange<2>
Definition: DRange.h:124
OpenMS::DRange::intersects
DRangeIntersection intersects(const DRange &range) const
Checks how this range intersects with another range.
Definition: DRange.h:224
Types.h
OpenMS::DRange::operator=
DRange & operator=(const DRange &rhs)
Assignment operator.
Definition: DRange.h:134
OpenMS::DRange
A D-dimensional half-open interval.
Definition: DRange.h:60
OpenMS::Internal::DIntervalBase::min_
PositionType min_
lower left point
Definition: DIntervalBase.h:362
OpenMS::Internal::DIntervalBase::operator=
DIntervalBase & operator=(const DIntervalBase &rhs)
Assignment operator.
Definition: DIntervalBase.h:96
OpenMS::DRange::encloses
bool encloses(const PositionType &position) const
Checks whether this range contains a certain point.
Definition: DRange.h:174
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::DRange::DIMENSION
Definition: DRange.h:70
OpenMS::DRange::operator==
bool operator==(const DRange &rhs) const
Equality operator.
Definition: DRange.h:157
OpenMS::DRange::~DRange
~DRange()
Destructor.
Definition: DRange.h:148
OpenMS::Exception::InvalidParameter
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition: Exception.h:339
OpenMS::Internal::DIntervalBase::maxPosition
PositionType const & maxPosition() const
Accessor to maximum position.
Definition: DIntervalBase.h:130
Macros.h
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
OpenMS::DRange::Intersects
Intersection.
Definition: DRange.h:81
DIntervalBase.h
OpenMS::DRange::DRange
DRange()
Default constructor.
Definition: DRange.h:97
OpenMS::Internal::DIntervalBase::setMinMax
void setMinMax(PositionType const &min, PositionType const &max)
Mutator for minimum and maximum position.
Definition: DIntervalBase.h:168
OpenMS::DRange::CoordinateType
Base::CoordinateType CoordinateType
Coordinate type of the positions.
Definition: DRange.h:76
OpenMS::Internal::DIntervalBase::max_
PositionType max_
upper right point
Definition: DIntervalBase.h:365
OpenMS::DRange::isIntersected
bool isIntersected(const DRange &range) const
Checks whether this range intersects with another range.
Definition: DRange.h:266
OpenMS::operator<<
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
OpenMS::DRange::operator==
bool operator==(const Base &rhs) const
Equality operator.
Definition: DRange.h:163
OpenMS::DPosition< D >
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
OpenMS::DRange::PositionType
Base::PositionType PositionType
Position type.
Definition: DRange.h:74
OpenMS::Internal::DIntervalBase
A base class for D-dimensional interval.
Definition: DIntervalBase.h:55
OpenMS::DRange::DRange
DRange(const PositionType &lower, const PositionType &upper)
Constructor that takes two Points and constructs a range.
Definition: DRange.h:103
OpenMS::Internal::DIntervalBase::minPosition
PositionType const & minPosition() const
Accessor to minimum position.
Definition: DIntervalBase.h:124
OpenMS::DRange< 3 >::DRangeIntersection
DRangeIntersection
Types that describe the kind of intersection between two ranges.
Definition: DRange.h:78
OpenMS::DRange::swapDimensions
DRange< D > & swapDimensions()
swaps dimensions for 2D data (i.e. x and y coordinates)
Definition: DRange.h:324
OpenMS::DRange::encloses
bool encloses(CoordinateType x, CoordinateType y) const
2D-version of encloses for convenience only
Definition: DRange.h:186
OpenMS::DRange::Base
Internal::DIntervalBase< D > Base
Base class type.
Definition: DRange.h:72
OpenMS::Internal::DIntervalBase::operator==
bool operator==(const DIntervalBase &rhs) const
Equality operator.
Definition: DIntervalBase.h:195
OpenMS::DRange::DRange
DRange(const DRange &range)
Copy constructor.
Definition: DRange.h:109
OpenMS::DRange::pullIn
void pullIn(DPosition< D > &point) const
Make sure point is inside the current area.
Definition: DRange.h:336
OpenMS::DRange::min_
PositionType min_
lower left point
Definition: DIntervalBase.h:362
OpenMS::DRange::extend
DRange< D > & extend(double factor)
Extends the range in all dimensions by a certain multiplier.
Definition: DRange.h:307
OpenMS::DRange::operator=
DRange & operator=(const Base &rhs)
Assignment operator for the base class.
Definition: DRange.h:141