OpenMS
Loading...
Searching...
No Matches
DRange.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg$
6// $Authors: Marc Sturm $
7// --------------------------------------------------------------------------
8
9#pragma once
10
15
16#include <functional>
17
18namespace OpenMS
19{
36 template <UInt D>
37 class DRange :
39 {
40public:
41
47 enum {DIMENSION = D};
61
63
64 using Base::min_;
65 using Base::max_;
66
75 Base()
76 {
77 }
78
80 DRange(const PositionType& lower, const PositionType& upper) :
81 Base(lower, upper)
82 {
83 }
84
86 DRange(const DRange& range) :
87 Base(range)
88 {
89 }
90
92 DRange(DRange&&) noexcept = default;
93
95 DRange(const Base& range) :
96 Base(range)
97 {
98 }
99
102 {
103 static_assert(D == 2);
104 min_[0] = minx;
105 min_[1] = miny;
106 max_[0] = maxx;
107 max_[1] = maxy;
109 }
110
113 {
114 Base::operator=(rhs);
115 return *this;
116 }
117
119 DRange& operator=(const Base& rhs)
120 {
121 Base::operator=(rhs);
122 return *this;
123 }
124
127 {
128 }
129
131
135 bool operator==(const DRange& rhs) const
136 {
137 return Base::operator==(rhs);
138 }
139
141 bool operator==(const Base& rhs) const
142 {
143 return Base::operator==(rhs);
144 }
145
152 bool encloses(const PositionType& position) const
153 {
154 for (UInt i = 0; i != D; i++)
155 {
156 if (position[i] < min_[i]) return false;
157
158 if (position[i] >= max_[i]) return false;
159 }
160 return true;
161 }
162
165 {
166 if (x < min_[0]) return false;
167
168 if (x >= max_[0]) return false;
169
170 if (y < min_[1]) return false;
171
172 if (y >= max_[1]) return false;
173
174 return true;
175 }
176
178 DRange united(const DRange<D>& other_range) const
179 {
180 PositionType united_min;
181 PositionType united_max;
182 DRange<D> united_range = DRange<D>::empty;
183
184 PositionType other_min = other_range.minPosition();
185 PositionType other_max = other_range.maxPosition();
186
187 for (Size i = 0; i != D; ++i)
188 {
189 united_min[i] = min_[i] < other_min[i] ? min_[i] : other_min[i];
190 united_max[i] = max_[i] > other_max[i] ? max_[i] : other_max[i];
191 }
192 united_range.setMinMax(united_min, united_max);
193
194 return united_range;
195 }
196
203 {
204 //check if r.min_ is in this area
205 if (encloses(range.min_))
206 {
207 //check if r.max_ in this area => Inside / Intersects
208 for (Size i = 0; i != D; i++)
209 {
210 if (range.max_[i] > max_[i])
211 {
212 return Intersects;
213 }
214 }
215 return Inside;
216 }
217 // => r.min_ is not inside this area
218 //check if any r.min_ >= max_ => Disjoint
219 for (Size i = 0; i != D; i++)
220 {
221 if (range.min_[i] >= max_[i])
222 {
223 return Disjoint;
224 }
225 }
226 // => some coordinate of r.min_ has to be smaller than the one of min_
227 //check if all coords of r are smaller than the those of the range
228 for (Size i = 0; i != D; i++)
229 {
230 if (range.max_[i] <= min_[i])
231 {
232 return Disjoint;
233 }
234 }
235 return Intersects;
236 }
237
244 bool isIntersected(const DRange& range) const
245 {
246 //check if r.min_ is in this area
247 if (encloses(range.min_))
248 {
249 return true;
250 }
251
252 // => r.min_ is not inside this area
253 //check if any r.min_ >= max_ => Disjoint
254 for (Size i = 0; i != D; i++)
255 {
256 if (range.min_[i] >= max_[i])
257 {
258 return false;
259 }
260 }
261 // => some coordinate of r.min_ has to be smaller than the one of min_
262 //check if all coords of r are smaller than the those of the range
263 for (Size i = 0; i != D; i++)
264 {
265 if (range.max_[i] <= min_[i])
266 {
267 return false;
268 }
269 }
270 return true;
271 }
272
285 DRange<D>& extend(double factor)
286 {
287 if (factor < 0)
288 {
289 throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "DRange::extend(): factor must not be negative!");
290 }
291
292 for (UInt i = 0; i != D; ++i)
293 {
294 Internal::DIntervalBase<1>::CoordinateType extra = (max_[i] - min_[i]) / 2.0 * (factor - 1);
295 min_[i] -= extra;
296 max_[i] += extra;
297 }
298 return *this;
299 }
300
314 {
315 addition /= 2;
316 min_ -= addition;
317 max_ += addition;
318 for (UInt i = 0; i != D; ++i)
319 {
320 // invalid range --> reduce to single center point
321 if (min_[i] > max_[i]) min_[i] = max_[i] = (min_[i] + max_[i]) / 2;
322 }
323 return *this;
324 }
325
327 {
328 typename Base::PositionType extend_by {};
329 for (UInt i = 0; i != D; ++i)
330 {
331 // invalid range --> reduce to single center point
332 if (max_[i] - min_[i] < min_span[i])
333 {
334 extend_by[i] = min_span[i] - (max_[i] - min_[i]); // add whatever is missing to get to min_span
335 }
336 }
337 extend(extend_by);
338 return *this;
339 }
340
343 {
344 static_assert(D==2);
345 std::swap(min_[0], min_[1]);
346 std::swap(max_[0], max_[1]);
347 return *this;
348 }
349
354 void pullIn(DPosition<D>& point) const
355 {
356 for (UInt i = 0; i != D; ++i)
357 {
358 point[i] = std::max(min_[i], std::min(point[i], max_[i]));
359 }
360 }
361
363 };
364
366 template <UInt D>
367 std::ostream& operator<<(std::ostream& os, const DRange<D>& area)
368 {
369 os << "--DRANGE BEGIN--\n";
370 os << "MIN --> " << area.min_ << '\n';
371 os << "MAX --> " << area.max_ << '\n';
372 os << "--DRANGE END--\n";
373 return os;
374 }
375
376} // namespace OpenMS
377
378// Hash function specialization for DRange
379namespace std
380{
381 template<OpenMS::UInt D>
382 struct hash<OpenMS::DRange<D>>
383 {
384 std::size_t operator()(const OpenMS::DRange<D>& range) const noexcept
385 {
386 std::size_t seed = 0;
387 // Hash min_ position (all D coordinates)
388 for (OpenMS::UInt i = 0; i < D; ++i)
389 {
390 OpenMS::hash_combine(seed, OpenMS::hash_float(range.minPosition()[i]));
391 }
392 // Hash max_ position (all D coordinates)
393 for (OpenMS::UInt i = 0; i < D; ++i)
394 {
395 OpenMS::hash_combine(seed, OpenMS::hash_float(range.maxPosition()[i]));
396 }
397 return seed;
398 }
399 };
400} // namespace std
401
Representation of a coordinate in D-dimensional space.
Definition DPosition.h:32
A D-dimensional half-open interval.
Definition DRange.h:39
PositionType min_
lower left point
Definition DIntervalBase.h:336
bool encloses(const PositionType &position) const
Checks whether this range (half open interval!) contains a certain point.
Definition DRange.h:152
DRange< D > & extend(double factor)
Extends the range in all dimensions by a certain multiplier.
Definition DRange.h:285
Base::PositionType PositionType
Position type.
Definition DRange.h:51
PositionType max_
upper right point
Definition DIntervalBase.h:339
DRangeIntersection
Types that describe the kind of intersection between two ranges.
Definition DRange.h:56
@ Disjoint
No intersection.
Definition DRange.h:57
@ Inside
One contains the other.
Definition DRange.h:59
@ Intersects
Intersection.
Definition DRange.h:58
DRange united(const DRange< D > &other_range) const
Returns the smallest range containing this range and other_range.
Definition DRange.h:178
bool encloses(CoordinateType x, CoordinateType y) const
2D-version of encloses for convenience only
Definition DRange.h:164
DRange()
Default constructor.
Definition DRange.h:74
DRange & operator=(const DRange &rhs)
Assignment operator.
Definition DRange.h:112
bool operator==(const Base &rhs) const
Equality operator.
Definition DRange.h:141
DRange(const PositionType &lower, const PositionType &upper)
Constructor that takes two Points and constructs a range.
Definition DRange.h:80
DRange(DRange &&) noexcept=default
Move constructor.
~DRange()
Destructor.
Definition DRange.h:126
DRangeIntersection intersects(const DRange &range) const
Checks how this range intersects with another range.
Definition DRange.h:202
DRange(const DRange &range)
Copy constructor.
Definition DRange.h:86
DRange & operator=(const Base &rhs)
Assignment operator for the base class.
Definition DRange.h:119
Internal::DIntervalBase< D > Base
Base class type.
Definition DRange.h:49
Base::CoordinateType CoordinateType
Coordinate type of the positions.
Definition DRange.h:53
DRange< D > & swapDimensions()
swaps dimensions for 2D data (i.e. x and y coordinates)
Definition DRange.h:342
DRange(CoordinateType minx, CoordinateType miny, CoordinateType maxx, CoordinateType maxy)
Convenient constructor for DRange<2>
Definition DRange.h:101
void pullIn(DPosition< D > &point) const
Make sure point is inside the current area.
Definition DRange.h:354
DRange< D > & extend(typename Base::PositionType addition)
Extends the range in all dimensions by a certain amount.
Definition DRange.h:313
@ DIMENSION
Definition DRange.h:47
bool operator==(const DRange &rhs) const
Equality operator.
Definition DRange.h:135
DRange< D > & ensureMinSpan(typename Base::PositionType min_span)
Definition DRange.h:326
bool isIntersected(const DRange &range) const
Checks whether this range intersects with another range.
Definition DRange.h:244
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition Exception.h:317
A base class for D-dimensional interval.
Definition DIntervalBase.h:30
PositionType min_
lower left point
Definition DIntervalBase.h:336
PositionType const & maxPosition() const
Accessor to maximum position.
Definition DIntervalBase.h:104
PositionType::CoordinateType CoordinateType
Coordinate type of the positions.
Definition DIntervalBase.h:42
PositionType max_
upper right point
Definition DIntervalBase.h:339
PositionType const & minPosition() const
Accessor to minimum position.
Definition DIntervalBase.h:98
bool operator==(const DIntervalBase &rhs) const
Equality operator.
Definition DIntervalBase.h:169
DPosition< D > PositionType
Position type.
Definition DIntervalBase.h:40
void setMinMax(PositionType const &min, PositionType const &max)
Mutator for minimum and maximum position.
Definition DIntervalBase.h:142
DIntervalBase & operator=(const DIntervalBase &rhs)
Assignment operator.
Definition DIntervalBase.h:70
void normalize_()
normalization to keep all dimensions in the right geometrical order (min_[X] < max_[X])
Definition DIntervalBase.h:342
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
void hash_combine(std::size_t &seed, std::size_t value) noexcept
Combine a hash value with additional data using golden ratio mixing.
Definition HashUtils.h:87
std::size_t hash_float(T value) noexcept
Hash for a floating point type (float or double).
Definition HashUtils.h:142
STL namespace.
std::size_t operator()(const OpenMS::DRange< D > &range) const noexcept
Definition DRange.h:384