OpenMS  2.4.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-2018.
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(const Base& range) :
116  Base(range)
117  {
118  }
119 
122  {
123  OPENMS_PRECONDITION(D == 2, "DRange<D>:DRange(minx, miny, maxx, maxy): index overflow!");
124  min_[0] = minx;
125  min_[1] = miny;
126  max_[0] = maxx;
127  max_[1] = maxy;
128  }
129 
131  DRange& operator=(const DRange& rhs)
132  {
133  Base::operator=(rhs);
134  return *this;
135  }
136 
138  DRange& operator=(const Base& rhs)
139  {
140  Base::operator=(rhs);
141  return *this;
142  }
143 
146  {
147  }
148 
150 
153  bool operator==(const DRange& rhs) const
155  {
156  return Base::operator==(rhs);
157  }
158 
160  bool operator==(const Base& rhs) const
161  {
162  return Base::operator==(rhs);
163  }
164 
171  bool encloses(const PositionType& position) const
172  {
173  for (UInt i = 0; i != D; i++)
174  {
175  if (position[i] < min_[i]) return false;
176 
177  if (position[i] >= max_[i]) return false;
178  }
179  return true;
180  }
181 
184  {
185  if (x < min_[0]) return false;
186 
187  if (x >= max_[0]) return false;
188 
189  if (y < min_[1]) return false;
190 
191  if (y >= max_[1]) return false;
192 
193  return true;
194  }
195 
197  DRange united(const DRange<D>& other_range) const
198  {
199  PositionType united_min;
200  PositionType united_max;
201  DRange<D> united_range = DRange<D>::empty;
202 
203  PositionType other_min = other_range.minPosition();
204  PositionType other_max = other_range.maxPosition();
205 
206  for (Size i = 0; i != D; ++i)
207  {
208  united_min[i] = min_[i] < other_min[i] ? min_[i] : other_min[i];
209  united_max[i] = max_[i] > other_max[i] ? max_[i] : other_max[i];
210  }
211  united_range.setMinMax(united_min, united_max);
212 
213  return united_range;
214  }
215 
222  {
223  //check if r.min_ is in this area
224  if (encloses(range.min_))
225  {
226  //check if r.max_ in this area => Inside / Intersects
227  for (Size i = 0; i != D; i++)
228  {
229  if (range.max_[i] > max_[i])
230  {
231  return Intersects;
232  }
233  }
234  return Inside;
235  }
236  // => r.min_ is not inside this area
237  //check if any r.min_ >= max_ => Disjoint
238  for (Size i = 0; i != D; i++)
239  {
240  if (range.min_[i] >= max_[i])
241  {
242  return Disjoint;
243  }
244  }
245  // => some coordinate of r.min_ has to be smaller than the one of min_
246  //check if all coords of r are smaller than the those of the range
247  for (Size i = 0; i != D; i++)
248  {
249  if (range.max_[i] <= min_[i])
250  {
251  return Disjoint;
252  }
253  }
254  return Intersects;
255  }
256 
263  bool isIntersected(const DRange& range) const
264  {
265  //check if r.min_ is in this area
266  if (encloses(range.min_))
267  {
268  return true;
269  }
270 
271  // => r.min_ is not inside this area
272  //check if any r.min_ >= max_ => Disjoint
273  for (Size i = 0; i != D; i++)
274  {
275  if (range.min_[i] >= max_[i])
276  {
277  return false;
278  }
279  }
280  // => some coordinate of r.min_ has to be smaller than the one of min_
281  //check if all coords of r are smaller than the those of the range
282  for (Size i = 0; i != D; i++)
283  {
284  if (range.max_[i] <= min_[i])
285  {
286  return false;
287  }
288  }
289  return true;
290  }
291 
293  bool isEmpty() const
294  {
295  for (UInt i = 0; i != D; i++)
296  {
297  if (max_[i] <= min_[i])
298  {
299  return true;
300  }
301  }
302  return false;
303  }
304 
316  void extend(double factor)
317  {
318  if (factor < 0)
319  {
320  throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "DRange::extend(): factor must not be negative!");
321  }
322 
323  for (UInt i = 0; i != D; ++i)
324  {
325  Internal::DIntervalBase<1>::CoordinateType extra = (max_[i] - min_[i]) / 2.0 * (factor - 1);
326  min_[i] -= extra;
327  max_[i] += extra;
328  }
329  }
330 
331 
333  };
334 
336  template <UInt D>
337  std::ostream& operator<<(std::ostream& os, const DRange<D>& area)
338  {
339  os << "--DRANGE BEGIN--" << std::endl;
340  os << "MIN --> " << area.min_ << std::endl;
341  os << "MAX --> " << area.max_ << std::endl;
342  os << "--DRANGE END--" << std::endl;
343  return os;
344  }
345 
346 } // namespace OpenMS
347 
DRange united(const DRange< D > &other_range) const
Returns the smallest range containing this range and other_range.
Definition: DRange.h:197
Base::PositionType PositionType
Position type.
Definition: DRange.h:74
No intersection.
Definition: DRange.h:80
DRange(CoordinateType minx, CoordinateType miny, CoordinateType maxx, CoordinateType maxy)
Convenient constructor for DRange<2>
Definition: DRange.h:121
PositionType min_
lower left point
Definition: DIntervalBase.h:308
bool encloses(const PositionType &position) const
Checks whether this range contains a certain point.
Definition: DRange.h:171
Size< TNeedle >::Type position(const PatternAuxData< TNeedle > &dh)
Definition: AhoCorasickAmbiguous.h:561
Internal::DIntervalBase< D > Base
Base class type.
Definition: DRange.h:72
PositionType min_
lower left point
Definition: DIntervalBase.h:308
bool operator==(const DRange &rhs) const
Equality operator.
Definition: DRange.h:154
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:106
DIntervalBase & operator=(const DIntervalBase &rhs)
Assignment operator.
Definition: DIntervalBase.h:93
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
void extend(double factor)
Extends the range in all dimensions by a certain multiplier.
Definition: DRange.h:316
DRangeIntersection intersects(const DRange &range) const
Checks how this range intersects with another range.
Definition: DRange.h:221
bool operator==(const Base &rhs) const
Equality operator.
Definition: DRange.h:160
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
PositionType max_
upper right point
Definition: DIntervalBase.h:311
PositionType const & maxPosition() const
Accessor to maximum position.
Definition: DIntervalBase.h:127
DRange()
Default constructor.
Definition: DRange.h:97
DRange & operator=(const DRange &rhs)
Assignment operator.
Definition: DRange.h:131
bool encloses(CoordinateType x, CoordinateType y) const
2D-version of encloses for convenience only
Definition: DRange.h:183
DRange(const DRange &range)
Copy constructor.
Definition: DRange.h:109
Base::CoordinateType CoordinateType
Coordinate type of the positions.
Definition: DRange.h:76
bool isIntersected(const DRange &range) const
Checks whether this range intersects with another range.
Definition: DRange.h:263
DRange(const Base &range)
Copy constructor for the base class.
Definition: DRange.h:115
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition: Exception.h:347
DRange & operator=(const Base &rhs)
Assignment operator for the base class.
Definition: DRange.h:138
A D-dimensional half-open interval.
Definition: DRange.h:60
PositionType max_
upper right point
Definition: DIntervalBase.h:311
~DRange()
Destructor.
Definition: DRange.h:145
Definition: DRange.h:70
bool isEmpty() const
Checks if the range is empty.
Definition: DRange.h:293
A base class for D-dimensional interval.
Definition: DIntervalBase.h:55
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
DRangeIntersection
Types that describe the kind of intersection between two ranges.
Definition: DRange.h:78
void setMinMax(PositionType const &min, PositionType const &max)
Mutator for minimum and maximum position.
Definition: DIntervalBase.h:165
bool operator==(const DIntervalBase &rhs) const
Equality operator.
Definition: DIntervalBase.h:192
Intersection.
Definition: DRange.h:81
DRange(const PositionType &lower, const PositionType &upper)
Constructor that takes two Points and constructs a range.
Definition: DRange.h:103
One contains the other.
Definition: DRange.h:82
PositionType const & minPosition() const
Accessor to minimum position.
Definition: DIntervalBase.h:121