OpenMS
DIntervalBase.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-2023.
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: Clemens Groepl, Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
38 #include <OpenMS/CONCEPT/Types.h>
39 
40 #include <utility>
41 
42 namespace OpenMS
43 {
44  namespace Internal
45  {
54  template <UInt D>
56  {
57 public:
58 
64  enum {DIMENSION = D};
68  typedef typename PositionType::CoordinateType CoordinateType;
70 
73 
80  min_(PositionType::maxPositive()),
81  max_(PositionType::minNegative())
82  {
83  }
84 
87  min_(rhs.min_),
88  max_(rhs.max_)
89  {
90  }
91 
93  DIntervalBase(DIntervalBase&&) noexcept = default;
94 
96  DIntervalBase& operator=(const DIntervalBase& rhs)
97  {
98  min_ = rhs.min_;
99  max_ = rhs.max_;
100  return *this;
101  }
102 
105  {
106  }
107 
111  DIntervalBase(PositionType const& minimum, PositionType const& maximum) :
112  min_(minimum),
113  max_(maximum)
114  {
115  normalize_();
116  }
117 
119 
122 
124  inline PositionType const& minPosition() const
125  {
126  return min_;
127  }
128 
130  inline PositionType const& maxPosition() const
131  {
132  return max_;
133  }
134 
141  void setMin(PositionType const& position)
142  {
143  min_ = position;
144  for (UInt i = 0; i < DIMENSION; ++i)
145  {
146  if (min_[i] > max_[i]) max_[i] = min_[i];
147  }
148  }
149 
156  void setMax(PositionType const& position)
157  {
158  max_ = position;
159  for (UInt i = 0; i < DIMENSION; ++i)
160  {
161  if (min_[i] > max_[i]) min_[i] = max_[i];
162  }
163  }
164 
168  void setMinMax(PositionType const& min, PositionType const& max)
169  {
170  min_ = min;
171  max_ = max;
172  normalize_();
173  }
174 
180  template <UInt D2>
181  void assign(const DIntervalBase<D2> rhs)
182  {
183  for (UInt i = 0; i < std::min(D, D2); ++i)
184  {
185  min_[i] = rhs.minPosition()[i];
186  max_[i] = rhs.maxPosition()[i];
187  }
188  }
189 
190  //}@
191 
195  bool operator==(const DIntervalBase& rhs) const
196  {
197  return (min_ == rhs.min_) && (max_ == rhs.max_);
198  }
199 
201  bool operator!=(const DIntervalBase& rhs) const
202  {
203  return !(operator==(rhs));
204  }
205 
207  {
208  DIntervalBase result(*this);
209  result += point;
210  return result;
211  }
212 
214  {
215  this->min_ += point;
216  this->max_ += point;
217  return *this;
218  }
219 
221  {
222  DIntervalBase result(*this);
223  result -= point;
224  return result;
225  }
226 
228  {
229  this->min_ -= point;
230  this->max_ -= point;
231  return *this;
232  }
233 
235  inline void clear()
236  {
237  *this = empty;
238  }
239 
242  bool isEmpty() const
243  {
244  return *this == empty;
245  }
246 
248  bool isEmpty(UInt dim) const
249  {
250  return DIntervalBase<1>(std::make_pair(min_[dim], max_[dim])) == DIntervalBase<1>::empty;
251  }
252 
254  void setDimMinMax(UInt dim, const DIntervalBase<1>& min_max)
255  {
256  min_[dim] = min_max.min_[0];
257  max_[dim] = min_max.max_[0];
258  }
259 
260  // make all other templates friends to allow accessing min_ and max_ in setDimMinMax
261  template<UInt> friend class DIntervalBase;
262 
264 
267 
270  {
272  center += max_;
273  center /= 2;
274  return center;
275  }
276 
279  {
280  return max_ - min_;
281  }
282 
284  static DIntervalBase const empty;
286  static DIntervalBase const zero;
287 
288  //}@
289 
292 
294  inline CoordinateType minX() const
295  {
296  return min_[0];
297  }
298 
300  inline CoordinateType minY() const
301  {
302  return min_[1];
303  }
304 
306  inline CoordinateType maxX() const
307  {
308  return max_[0];
309  }
310 
312  inline CoordinateType maxY() const
313  {
314  return max_[1];
315  }
316 
319  {
320  min_[0] = c;
321  if (min_[0] > max_[0]) max_[0] = min_[0];
322  }
323 
326  {
327  min_[1] = c;
328  if (min_[1] > max_[1]) max_[1] = min_[1];
329  }
330 
333  {
334  max_[0] = c;
335  if (min_[0] > max_[0]) min_[0] = max_[0];
336  }
337 
340  {
341  max_[1] = c;
342  if (min_[1] > max_[1]) min_[1] = max_[1];
343  }
344 
346  inline CoordinateType width() const
347  {
348  return max_[0] - min_[0];
349  }
350 
352  inline CoordinateType height() const
353  {
354  return max_[1] - min_[1];
355  }
356 
358 
359 protected:
360 
363 
366 
368  void normalize_()
369  {
370  for (UInt i = 0; i < DIMENSION; ++i)
371  {
372  if (min_[i] > max_[i])
373  {
374  std::swap(min_[i], max_[i]);
375  }
376  }
377  }
378 
380  DIntervalBase(const std::pair<PositionType, PositionType>& pair) :
381  min_(pair.first),
382  max_(pair.second)
383  {
384 
385  }
386 
387  };
388 
389  template <UInt D>
392 
393  template <UInt D>
396 
398  template <UInt D>
399  std::ostream& operator<<(std::ostream& os, const DIntervalBase<D>& rhs)
400  {
401  os << "--DIntervalBase BEGIN--" << std::endl;
402  os << "MIN --> " << rhs.minPosition() << std::endl;
403  os << "MAX --> " << rhs.maxPosition() << std::endl;
404  os << "--DIntervalBase END--" << std::endl;
405  return os;
406  }
407 
408  } // namespace Internal
409 
410 } // namespace OpenMS
411 
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:55
A base class for D-dimensional interval.
Definition: DIntervalBase.h:56
bool isEmpty(UInt dim) const
Is the dimension dim empty? If min==max, the interval is NOT empty!
Definition: DIntervalBase.h:248
void setDimMinMax(UInt dim, const DIntervalBase< 1 > &min_max)
only set interval for a single dimension
Definition: DIntervalBase.h:254
PositionType min_
lower left point
Definition: DIntervalBase.h:362
static DIntervalBase const zero
instance with all positions zero
Definition: DIntervalBase.h:286
PositionType::CoordinateType CoordinateType
Coordinate type of the positions.
Definition: DIntervalBase.h:68
CoordinateType minX() const
Accessor for min_ coordinate minimum.
Definition: DIntervalBase.h:294
PositionType const & maxPosition() const
Accessor to maximum position.
Definition: DIntervalBase.h:130
PositionType max_
upper right point
Definition: DIntervalBase.h:365
bool operator==(const DIntervalBase &rhs) const
Equality operator.
Definition: DIntervalBase.h:195
CoordinateType height() const
Returns the height of the area i.e. the difference of dimension one (Y).
Definition: DIntervalBase.h:352
void setMinY(CoordinateType const c)
Mutator for max_ coordinate of the smaller point.
Definition: DIntervalBase.h:325
DPosition< D > PositionType
Position type.
Definition: DIntervalBase.h:66
DIntervalBase(const DIntervalBase &rhs)
Copy constructor.
Definition: DIntervalBase.h:86
void setMin(PositionType const &position)
Mutator for minimum position.
Definition: DIntervalBase.h:141
CoordinateType width() const
Returns the width of the area i.e. the difference of dimension zero (X).
Definition: DIntervalBase.h:346
CoordinateType maxX() const
Accessor for min_ coordinate maximum.
Definition: DIntervalBase.h:306
void setMaxY(CoordinateType const c)
Mutator for max_ coordinate of the larger point.
Definition: DIntervalBase.h:339
@ DIMENSION
Definition: DIntervalBase.h:64
void assign(const DIntervalBase< D2 > rhs)
Assignment from a DIntervalBase of different dimensions.
Definition: DIntervalBase.h:181
void setMinMax(PositionType const &min, PositionType const &max)
Mutator for minimum and maximum position.
Definition: DIntervalBase.h:168
void setMax(PositionType const &position)
Mutator for maximum position.
Definition: DIntervalBase.h:156
CoordinateType maxY() const
Accessor for max_ coordinate maximum.
Definition: DIntervalBase.h:312
PositionType diagonal() const
Returns the diagonal of the area, i.e. max_ - min_.
Definition: DIntervalBase.h:278
PositionType center() const
Returns the center of the interval.
Definition: DIntervalBase.h:269
DIntervalBase(DIntervalBase &&) noexcept=default
Move constructor.
bool operator!=(const DIntervalBase &rhs) const
Equality operator.
Definition: DIntervalBase.h:201
void setMinX(CoordinateType const c)
Mutator for min_ coordinate of the smaller point.
Definition: DIntervalBase.h:318
DIntervalBase & operator-=(const PositionType &point)
Definition: DIntervalBase.h:227
DIntervalBase operator-(const PositionType &point) const
Definition: DIntervalBase.h:220
~DIntervalBase()
Destructor.
Definition: DIntervalBase.h:104
void normalize_()
normalization to keep all dimensions in the right geometrical order (min_[X] < max_[X])
Definition: DIntervalBase.h:368
void clear()
Make the interval empty.
Definition: DIntervalBase.h:235
DIntervalBase(const std::pair< PositionType, PositionType > &pair)
Protected constructor for the construction of static instances.
Definition: DIntervalBase.h:380
bool isEmpty() const
Definition: DIntervalBase.h:242
DIntervalBase()
Default constructor.
Definition: DIntervalBase.h:79
DIntervalBase & operator+=(const PositionType &point)
Definition: DIntervalBase.h:213
void setMaxX(CoordinateType const c)
Mutator for min_ coordinate of the larger point.
Definition: DIntervalBase.h:332
static DIntervalBase const empty
empty instance
Definition: DIntervalBase.h:284
DIntervalBase operator+(const PositionType &point) const
Definition: DIntervalBase.h:206
CoordinateType minY() const
Accessor for max_ coordinate minimum.
Definition: DIntervalBase.h:300
PositionType const & minPosition() const
Accessor to minimum position.
Definition: DIntervalBase.h:124
DIntervalBase(PositionType const &minimum, PositionType const &maximum)
This constructor sets min_ and max_ directly.
Definition: DIntervalBase.h:111
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
const double c
Definition: Constants.h:214
std::ostream & operator<<(std::ostream &os, const DIntervalBase< D > &rhs)
Print the contents to a stream.
Definition: DIntervalBase.h:399
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48