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-2013.
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: Stephan Aiche$
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_DATASTRUCTURES_DRANGE_H
36 #define OPENMS_DATASTRUCTURES_DRANGE_H
37 
39 
40 namespace OpenMS
41 {
58  template <UInt D>
59  class DRange :
60  public Internal::DIntervalBase<D>
61  {
62 public:
63 
68  enum {DIMENSION = D};
73  typedef typename Base::PositionType PositionType;
78  {
82  };
83 
85 
86  using Base::min_;
87  using Base::max_;
88 
96  DRange() :
97  Base()
98  {
99  }
100 
102  DRange(const PositionType & lower, const PositionType & upper) :
103  Base(lower, upper)
104  {
105  }
106 
108  DRange(const DRange & range) :
109  Base(range)
110  {
111  }
112 
114  DRange(const Base & range) :
115  Base(range)
116  {
117  }
118 
121  {
122  OPENMS_PRECONDITION(D == 2, "DRange<D>:DRange(minx, miny, maxx, maxy): index overflow!");
123  min_[0] = minx;
124  min_[1] = miny;
125  max_[0] = maxx;
126  max_[1] = maxy;
127  }
128 
130  DRange & operator=(const DRange & rhs)
131  {
132  Base::operator=(rhs);
133  return *this;
134  }
135 
137  DRange & operator=(const Base & rhs)
138  {
139  Base::operator=(rhs);
140  return *this;
141  }
142 
145  {
146  }
147 
149 
152  bool operator==(const DRange & rhs) const
154  {
155  return Base::operator==(rhs);
156  }
157 
159  bool operator==(const Base & rhs) const
160  {
161  return Base::operator==(rhs);
162  }
163 
170  bool encloses(const PositionType & position) const
171  {
172  for (UInt i = 0; i != D; i++)
173  {
174  if (position[i] < min_[i]) return false;
175 
176  if (position[i] >= max_[i]) return false;
177  }
178  return true;
179  }
180 
183  {
184  if (x < min_[0]) return false;
185 
186  if (x >= max_[0]) return false;
187 
188  if (y < min_[1]) return false;
189 
190  if (y >= max_[1]) return false;
191 
192  return true;
193  }
194 
196  DRange united(const DRange<D> & other_range) const
197  {
198  PositionType united_min;
199  PositionType united_max;
200  DRange<D> united_range = DRange<D>::empty;
201 
202  PositionType other_min = other_range.minPosition();
203  PositionType other_max = other_range.maxPosition();
204 
205  for (Size i = 0; i != D; ++i)
206  {
207  united_min[i] = min_[i] < other_min[i] ? min_[i] : other_min[i];
208  united_max[i] = max_[i] > other_max[i] ? max_[i] : other_max[i];
209  }
210  united_range.setMinMax(united_min, united_max);
211 
212  return united_range;
213  }
214 
220  DRangeIntersection intersects(const DRange & range) const
221  {
222  //check if r.min_ is in this area
223  if (encloses(range.min_))
224  {
225  //check if r.max_ in this area => Inside / Intersects
226  for (Size i = 0; i != D; i++)
227  {
228  if (range.max_[i] > max_[i])
229  {
230  return Intersects;
231  }
232  }
233  return Inside;
234  }
235  // => r.min_ is not inside this area
236  //check if any r.min_ >= max_ => Disjoint
237  for (Size i = 0; i != D; i++)
238  {
239  if (range.min_[i] >= max_[i])
240  {
241  return Disjoint;
242  }
243  }
244  // => some coordinate of r.min_ has to be smaller than the one of min_
245  //check if all coords of r are smaller than the those of the range
246  for (Size i = 0; i != D; i++)
247  {
248  if (range.max_[i] <= min_[i])
249  {
250  return Disjoint;
251  }
252  }
253  return Intersects;
254  }
255 
262  bool isIntersected(const DRange & range) const
263  {
264  //check if r.min_ is in this area
265  if (encloses(range.min_))
266  {
267  return true;
268  }
269 
270  // => r.min_ is not inside this area
271  //check if any r.min_ >= max_ => Disjoint
272  for (Size i = 0; i != D; i++)
273  {
274  if (range.min_[i] >= max_[i])
275  {
276  return false;
277  }
278  }
279  // => some coordinate of r.min_ has to be smaller than the one of min_
280  //check if all coords of r are smaller than the those of the range
281  for (Size i = 0; i != D; i++)
282  {
283  if (range.max_[i] <= min_[i])
284  {
285  return false;
286  }
287  }
288  return true;
289  }
290 
292  bool isEmpty() const
293  {
294  for (UInt i = 0; i != D; i++)
295  {
296  if (max_[i] <= min_[i])
297  {
298  return true;
299  }
300  }
301  return false;
302  }
303 
305  };
306 
308  template <UInt D>
309  std::ostream & operator<<(std::ostream & os, const DRange<D> & area)
310  {
311  os << "--DRANGE BEGIN--" << std::endl;
312  os << "MIN --> " << area.min_ << std::endl;
313  os << "MAX --> " << area.max_ << std::endl;
314  os << "--DRANGE END--" << std::endl;
315  return os;
316  }
317 
318 } // namespace OpenMS
319 
320 #endif // OPENMS_DATASTRUCTURES_DRANGE_H
bool operator==(const DRange &rhs) const
Equality operator.
Definition: DRange.h:153
Base::PositionType PositionType
Position type.
Definition: DRange.h:73
Definition: DRange.h:69
No intersection.
Definition: DRange.h:79
DRange(CoordinateType minx, CoordinateType miny, CoordinateType maxx, CoordinateType maxy)
Convenient constructor for DRange&lt;2&gt;
Definition: DRange.h:120
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:170
bool operator==(const Base &rhs) const
Equality operator.
Definition: DRange.h:159
Internal::DIntervalBase< D > Base
Base class type.
Definition: DRange.h:71
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: Macros.h:107
DIntervalBase & operator=(const DIntervalBase &rhs)
Assignment operator.
Definition: DIntervalBase.h:93
bool operator==(const DIntervalBase &rhs) const
Equality operator.
Definition: DIntervalBase.h:192
DRange united(const DRange< D > &other_range) const
Returns the smallest range containing this range and other_range.
Definition: DRange.h:196
DRange()
Default constructor.
Definition: DRange.h:96
bool encloses(CoordinateType x, CoordinateType y) const
2D-version of encloses for convenience only
Definition: DRange.h:182
DRange & operator=(const DRange &rhs)
Assignement operator.
Definition: DRange.h:130
DRange(const DRange &range)
Copy constructor.
Definition: DRange.h:108
bool isIntersected(const DRange &range) const
Checks whether this range intersects with another range.
Definition: DRange.h:262
Base::CoordinateType CoordinateType
Coordinate type of the positions.
Definition: DRange.h:75
DRange(const Base &range)
Copy constructor for the base class.
Definition: DRange.h:114
DRange & operator=(const Base &rhs)
Assignement operator for the base class.
Definition: DRange.h:137
bool isEmpty() const
Checks if the range is empty.
Definition: DRange.h:292
A D-dimensional half-open interval.
Definition: DRange.h:59
PositionType max_
upper right point
Definition: DIntervalBase.h:311
~DRange()
Destuctor.
Definition: DRange.h:144
PositionType const & minPosition() const
Accessor to minimum position.
Definition: DIntervalBase.h:121
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:144
DRangeIntersection
Types that describe the kind of intersection between two ranges.
Definition: DRange.h:77
void setMinMax(PositionType const &min, PositionType const &max)
Mutator for minimum and maximum position.
Definition: DIntervalBase.h:165
PositionType const & maxPosition() const
Accessor to maximum position.
Definition: DIntervalBase.h:127
Intersection.
Definition: DRange.h:80
DRangeIntersection intersects(const DRange &range) const
Checks how this range intersects with another range.
Definition: DRange.h:220
DRange(const PositionType &lower, const PositionType &upper)
Constructor that takes two Points and constructs a range.
Definition: DRange.h:102
One contains the other.
Definition: DRange.h:81

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:12 using doxygen 1.8.5