Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
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-2017.
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 #ifndef OPENMS_DATASTRUCTURES_DRANGE_H
36 #define OPENMS_DATASTRUCTURES_DRANGE_H
37 
39 #include <OpenMS/CONCEPT/Macros.h>
40 #include <OpenMS/CONCEPT/Types.h>
41 
42 namespace OpenMS
43 {
60  template <UInt D>
61  class DRange :
62  public Internal::DIntervalBase<D>
63  {
64 public:
65 
70  enum {DIMENSION = D};
75  typedef typename Base::PositionType PositionType;
80  {
84  };
85 
87 
88  using Base::min_;
89  using Base::max_;
90 
98  DRange() :
99  Base()
100  {
101  }
102 
104  DRange(const PositionType& lower, const PositionType& upper) :
105  Base(lower, upper)
106  {
107  }
108 
110  DRange(const DRange& range) :
111  Base(range)
112  {
113  }
114 
116  DRange(const Base& range) :
117  Base(range)
118  {
119  }
120 
122  DRange(CoordinateType minx, CoordinateType miny, CoordinateType maxx, CoordinateType maxy)
123  {
124  OPENMS_PRECONDITION(D == 2, "DRange<D>:DRange(minx, miny, maxx, maxy): index overflow!");
125  min_[0] = minx;
126  min_[1] = miny;
127  max_[0] = maxx;
128  max_[1] = maxy;
129  }
130 
132  DRange& operator=(const DRange& rhs)
133  {
134  Base::operator=(rhs);
135  return *this;
136  }
137 
139  DRange& operator=(const Base& rhs)
140  {
141  Base::operator=(rhs);
142  return *this;
143  }
144 
147  {
148  }
149 
151 
154  bool operator==(const DRange& rhs) const
156  {
157  return Base::operator==(rhs);
158  }
159 
161  bool operator==(const Base& rhs) const
162  {
163  return Base::operator==(rhs);
164  }
165 
172  bool encloses(const PositionType& position) const
173  {
174  for (UInt i = 0; i != D; i++)
175  {
176  if (position[i] < min_[i]) return false;
177 
178  if (position[i] >= max_[i]) return false;
179  }
180  return true;
181  }
182 
184  bool encloses(CoordinateType x, CoordinateType y) const
185  {
186  if (x < min_[0]) return false;
187 
188  if (x >= max_[0]) return false;
189 
190  if (y < min_[1]) return false;
191 
192  if (y >= max_[1]) return false;
193 
194  return true;
195  }
196 
198  DRange united(const DRange<D>& other_range) const
199  {
200  PositionType united_min;
201  PositionType united_max;
202  DRange<D> united_range = DRange<D>::empty;
203 
204  PositionType other_min = other_range.minPosition();
205  PositionType other_max = other_range.maxPosition();
206 
207  for (Size i = 0; i != D; ++i)
208  {
209  united_min[i] = min_[i] < other_min[i] ? min_[i] : other_min[i];
210  united_max[i] = max_[i] > other_max[i] ? max_[i] : other_max[i];
211  }
212  united_range.setMinMax(united_min, united_max);
213 
214  return united_range;
215  }
216 
223  {
224  //check if r.min_ is in this area
225  if (encloses(range.min_))
226  {
227  //check if r.max_ in this area => Inside / Intersects
228  for (Size i = 0; i != D; i++)
229  {
230  if (range.max_[i] > max_[i])
231  {
232  return Intersects;
233  }
234  }
235  return Inside;
236  }
237  // => r.min_ is not inside this area
238  //check if any r.min_ >= max_ => Disjoint
239  for (Size i = 0; i != D; i++)
240  {
241  if (range.min_[i] >= max_[i])
242  {
243  return Disjoint;
244  }
245  }
246  // => some coordinate of r.min_ has to be smaller than the one of min_
247  //check if all coords of r are smaller than the those of the range
248  for (Size i = 0; i != D; i++)
249  {
250  if (range.max_[i] <= min_[i])
251  {
252  return Disjoint;
253  }
254  }
255  return Intersects;
256  }
257 
264  bool isIntersected(const DRange& range) const
265  {
266  //check if r.min_ is in this area
267  if (encloses(range.min_))
268  {
269  return true;
270  }
271 
272  // => r.min_ is not inside this area
273  //check if any r.min_ >= max_ => Disjoint
274  for (Size i = 0; i != D; i++)
275  {
276  if (range.min_[i] >= max_[i])
277  {
278  return false;
279  }
280  }
281  // => some coordinate of r.min_ has to be smaller than the one of min_
282  //check if all coords of r are smaller than the those of the range
283  for (Size i = 0; i != D; i++)
284  {
285  if (range.max_[i] <= min_[i])
286  {
287  return false;
288  }
289  }
290  return true;
291  }
292 
294  bool isEmpty() const
295  {
296  for (UInt i = 0; i != D; i++)
297  {
298  if (max_[i] <= min_[i])
299  {
300  return true;
301  }
302  }
303  return false;
304  }
305 
317  void extend(double factor)
318  {
319  if (factor < 0)
320  {
321  throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "DRange::extend(): factor must not be negative!");
322  }
323 
324  for (UInt i = 0; i != D; ++i)
325  {
326  Internal::DIntervalBase<1>::CoordinateType extra = (max_[i] - min_[i]) / 2.0 * (factor - 1);
327  min_[i] -= extra;
328  max_[i] += extra;
329  }
330  }
331 
332 
334  };
335 
337  template <UInt D>
338  std::ostream& operator<<(std::ostream& os, const DRange<D>& area)
339  {
340  os << "--DRANGE BEGIN--" << std::endl;
341  os << "MIN --> " << area.min_ << std::endl;
342  os << "MAX --> " << area.max_ << std::endl;
343  os << "--DRANGE END--" << std::endl;
344  return os;
345  }
346 
347 } // namespace OpenMS
348 
349 #endif // OPENMS_DATASTRUCTURES_DRANGE_H
DRange united(const DRange< D > &other_range) const
Returns the smallest range containing this range and other_range.
Definition: DRange.h:198
Base::PositionType PositionType
Position type.
Definition: DRange.h:75
No intersection.
Definition: DRange.h:81
DRange(CoordinateType minx, CoordinateType miny, CoordinateType maxx, CoordinateType maxy)
Convenient constructor for DRange<2>
Definition: DRange.h:122
PositionType min_
lower left point
Definition: DIntervalBase.h:309
bool encloses(const PositionType &position) const
Checks whether this range contains a certain point.
Definition: DRange.h:172
Internal::DIntervalBase< D > Base
Base class type.
Definition: DRange.h:73
bool operator==(const DRange &rhs) const
Equality operator.
Definition: DRange.h:155
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:107
DIntervalBase & operator=(const DIntervalBase &rhs)
Assignment operator.
Definition: DIntervalBase.h:94
unsigned int UInt
Unsigned integer type.
Definition: Types.h:95
void extend(double factor)
Extends the range in all dimensions by a certain multiplier.
Definition: DRange.h:317
DRangeIntersection intersects(const DRange &range) const
Checks how this range intersects with another range.
Definition: DRange.h:222
bool operator==(const Base &rhs) const
Equality operator.
Definition: DRange.h:161
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
PositionType const & maxPosition() const
Accessor to maximum position.
Definition: DIntervalBase.h:128
DRange()
Default constructor.
Definition: DRange.h:98
DRange & operator=(const DRange &rhs)
Assignment operator.
Definition: DRange.h:132
bool encloses(CoordinateType x, CoordinateType y) const
2D-version of encloses for convenience only
Definition: DRange.h:184
DRange(const DRange &range)
Copy constructor.
Definition: DRange.h:110
Base::CoordinateType CoordinateType
Coordinate type of the positions.
Definition: DRange.h:77
bool isIntersected(const DRange &range) const
Checks whether this range intersects with another range.
Definition: DRange.h:264
DRange(const Base &range)
Copy constructor for the base class.
Definition: DRange.h:116
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition: Exception.h:348
DRange & operator=(const Base &rhs)
Assignment operator for the base class.
Definition: DRange.h:139
A D-dimensional half-open interval.
Definition: DRange.h:61
PositionType max_
upper right point
Definition: DIntervalBase.h:312
~DRange()
Destructor.
Definition: DRange.h:146
Definition: DRange.h:71
bool isEmpty() const
Checks if the range is empty.
Definition: DRange.h:294
A base class for D-dimensional interval.
Definition: DIntervalBase.h:56
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
DRangeIntersection
Types that describe the kind of intersection between two ranges.
Definition: DRange.h:79
void setMinMax(PositionType const &min, PositionType const &max)
Mutator for minimum and maximum position.
Definition: DIntervalBase.h:166
bool operator==(const DIntervalBase &rhs) const
Equality operator.
Definition: DIntervalBase.h:193
Intersection.
Definition: DRange.h:82
DRange(const PositionType &lower, const PositionType &upper)
Constructor that takes two Points and constructs a range.
Definition: DRange.h:104
One contains the other.
Definition: DRange.h:83
PositionType const & minPosition() const
Accessor to minimum position.
Definition: DIntervalBase.h:122

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:21:59 using doxygen 1.8.13