OpenMS  2.7.0
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-2021.
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 
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 
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  inline void clear()
208  {
209  *this = empty;
210  }
211 
213 
216 
219  {
221  center += max_;
222  center /= 2;
223  return center;
224  }
225 
228  {
229  return max_ - min_;
230  }
231 
233  static DIntervalBase const empty;
235  static DIntervalBase const zero;
236 
237  //}@
238 
241 
243  inline CoordinateType minX() const
244  {
245  return min_[0];
246  }
247 
249  inline CoordinateType minY() const
250  {
251  return min_[1];
252  }
253 
255  inline CoordinateType maxX() const
256  {
257  return max_[0];
258  }
259 
261  inline CoordinateType maxY() const
262  {
263  return max_[1];
264  }
265 
268  {
269  min_[0] = c;
270  if (min_[0] > max_[0]) max_[0] = min_[0];
271  }
272 
275  {
276  min_[1] = c;
277  if (min_[1] > max_[1]) max_[1] = min_[1];
278  }
279 
282  {
283  max_[0] = c;
284  if (min_[0] > max_[0]) min_[0] = max_[0];
285  }
286 
289  {
290  max_[1] = c;
291  if (min_[1] > max_[1]) min_[1] = max_[1];
292  }
293 
295  inline CoordinateType width() const
296  {
297  return max_[0] - min_[0];
298  }
299 
301  inline CoordinateType height() const
302  {
303  return max_[1] - min_[1];
304  }
305 
307 
308 protected:
309 
312 
315 
317  void normalize_()
318  {
319  for (UInt i = 0; i < DIMENSION; ++i)
320  {
321  if (min_[i] > max_[i])
322  {
323  std::swap(min_[i], max_[i]);
324  }
325  }
326  }
327 
329  DIntervalBase(const std::pair<PositionType, PositionType>& pair) :
330  min_(pair.first),
331  max_(pair.second)
332  {
333 
334  }
335 
336  };
337 
338  template <UInt D>
341 
342  template <UInt D>
345 
347  template <UInt D>
348  std::ostream& operator<<(std::ostream& os, const DIntervalBase<D>& rhs)
349  {
350  os << "--DIntervalBase BEGIN--" << std::endl;
351  os << "MIN --> " << rhs.minPosition() << std::endl;
352  os << "MAX --> " << rhs.maxPosition() << std::endl;
353  os << "--DIntervalBase END--" << std::endl;
354  return os;
355  }
356 
357  } // namespace Internal
358 
359 } // namespace OpenMS
360 
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:54
A base class for D-dimensional interval.
Definition: DIntervalBase.h:56
PositionType min_
lower left point
Definition: DIntervalBase.h:311
static DIntervalBase const zero
instance with all positions zero
Definition: DIntervalBase.h:235
PositionType::CoordinateType CoordinateType
Coordinate type of the positions.
Definition: DIntervalBase.h:68
CoordinateType minX() const
Accessor for min_ coordinate minimum.
Definition: DIntervalBase.h:243
PositionType const & maxPosition() const
Accessor to maximum position.
Definition: DIntervalBase.h:130
PositionType max_
upper right point
Definition: DIntervalBase.h:314
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:301
void setMinY(CoordinateType const c)
Mutator for max_ coordinate of the smaller point.
Definition: DIntervalBase.h:274
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:295
CoordinateType maxX() const
Accessor for min_ coordinate maximum.
Definition: DIntervalBase.h:255
void setMaxY(CoordinateType const c)
Mutator for max_ coordinate of the larger point.
Definition: DIntervalBase.h:288
@ 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:261
PositionType diagonal() const
Returns the diagonal of the area, i.e. max_ - min_.
Definition: DIntervalBase.h:227
PositionType center() const
Returns the center of the interval.
Definition: DIntervalBase.h:218
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:267
~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:317
void clear()
Make the interval empty.
Definition: DIntervalBase.h:207
DIntervalBase(const std::pair< PositionType, PositionType > &pair)
Protected constructor for the construction of static instances.
Definition: DIntervalBase.h:329
DIntervalBase()
Default constructor.
Definition: DIntervalBase.h:79
void setMaxX(CoordinateType const c)
Mutator for min_ coordinate of the larger point.
Definition: DIntervalBase.h:281
static DIntervalBase const empty
empty instance
Definition: DIntervalBase.h:233
CoordinateType minY() const
Accessor for max_ coordinate minimum.
Definition: DIntervalBase.h:249
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:209
std::ostream & operator<<(std::ostream &os, const DIntervalBase< D > &rhs)
Print the contents to a stream.
Definition: DIntervalBase.h:348
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
Size< TNeedle >::Type position(const PatternAuxData< TNeedle > &dh)
Definition: AhoCorasickAmbiguous.h:563