OpenMS
Loading...
Searching...
No Matches
DBoundingBox.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: $
7// --------------------------------------------------------------------------
8
9#pragma once
10
14
15#include <functional>
16
17namespace OpenMS
18{
19
27 template <UInt D>
30 {
31
32public:
33
39 enum {DIMENSION = D};
47
48
49 // for convenience
50 using Base::min_;
51 using Base::max_;
52
57 Base()
58 {
59 }
60
63 Base(rhs)
64 {
65 }
66
69 {
70 Base::operator=(rhs);
71 return *this;
72 }
73
76 {
77 Base::operator=(rhs);
78 return *this;
79 }
80
83 {
84 }
85
87 DBoundingBox(const PositionType& minimum, const PositionType& maximum) :
88 Base(minimum, maximum)
89 {
90 }
91
93
96
98 void enlarge(const PositionType& p)
99 {
100 for (UInt i = 0; i < DIMENSION; ++i)
101 {
102 if (p[i] < min_[i]) min_[i] = p[i];
103 if (p[i] > max_[i]) max_[i] = p[i];
104 }
105 }
106
109 {
110 enlarge(PositionType(x, y));
111 }
112
113 //}@
114
117
119 bool operator==(const DBoundingBox& rhs) const
120 {
121 return Base::operator==(rhs);
122 }
123
125 bool operator==(const Base& rhs) const
126 {
127 return Base::operator==(rhs);
128 }
129
136 bool encloses(const PositionType& position) const
137 {
138 for (UInt i = 0; i < DIMENSION; ++i)
139 {
140 if (position[i] < min_[i] || position[i] > max_[i])
141 {
142 return false;
143 }
144 }
145 return true;
146 }
147
150 {
151 return encloses(PositionType(x, y));
152 }
153
157 bool intersects(const DBoundingBox& bounding_box) const
158 {
159 for (UInt i = 0; i < DIMENSION; ++i)
160 {
161 if (bounding_box.min_[i] > max_[i]) return false;
162
163 if (bounding_box.max_[i] < min_[i]) return false;
164 }
165 return true;
166 }
167
169 bool isEmpty() const
170 {
171 for (UInt i = 0; i != D; i++)
172 {
173 if (max_[i] <= min_[i])
174 {
175 return true;
176 }
177 }
178 return false;
179 }
180
182
183
184 };
185
190 template <UInt D>
191 std::ostream& operator<<(std::ostream& os, const DBoundingBox<D>& bounding_box)
192 {
193 os << "--DBOUNDINGBOX BEGIN--" << std::endl;
194 os << "MIN --> " << bounding_box.minPosition() << std::endl;
195 os << "MAX --> " << bounding_box.maxPosition() << std::endl;
196 os << "--DBOUNDINGBOX END--" << std::endl;
197 return os;
198 }
199
200} // namespace OpenMS
201
202// Hash function specialization for DBoundingBox
203namespace std
204{
205 template<OpenMS::UInt D>
206 struct hash<OpenMS::DBoundingBox<D>>
207 {
208 std::size_t operator()(const OpenMS::DBoundingBox<D>& bb) const noexcept
209 {
210 // Hash both min_ and max_ positions (fields used in operator==)
211 std::size_t seed = 0;
212 // Hash minPosition
213 const auto& min_pos = bb.minPosition();
214 for (OpenMS::UInt i = 0; i < D; ++i)
215 {
216 OpenMS::hash_combine(seed, OpenMS::hash_float(min_pos[i]));
217 }
218 // Hash maxPosition
219 const auto& max_pos = bb.maxPosition();
220 for (OpenMS::UInt i = 0; i < D; ++i)
221 {
222 OpenMS::hash_combine(seed, OpenMS::hash_float(max_pos[i]));
223 }
224 return seed;
225 }
226 };
227} // namespace std
228
A D-dimensional bounding box.
Definition DBoundingBox.h:30
PositionType min_
lower left point
Definition DIntervalBase.h:336
DBoundingBox()
Default constructor.
Definition DBoundingBox.h:56
DBoundingBox & operator=(const Base &rhs)
Assignment operator for the base class.
Definition DBoundingBox.h:75
bool encloses(const PositionType &position) const
Checks whether this range contains a certain point.
Definition DBoundingBox.h:136
Base::PositionType PositionType
Position type.
Definition DBoundingBox.h:43
DBoundingBox(const DBoundingBox &rhs)
Copy constructor.
Definition DBoundingBox.h:62
std::ostream & operator<<(std::ostream &os, const DBoundingBox< D > &bounding_box)
Print the contents to a stream.
Definition DBoundingBox.h:191
PositionType max_
upper right point
Definition DIntervalBase.h:339
bool encloses(CoordinateType x, CoordinateType y) const
2D-version encloses(x,y) is for convenience only
Definition DBoundingBox.h:149
bool operator==(const DBoundingBox &rhs) const
Equality operator.
Definition DBoundingBox.h:119
bool operator==(const Base &rhs) const
Equality operator.
Definition DBoundingBox.h:125
DBoundingBox(const PositionType &minimum, const PositionType &maximum)
Constructor from two positions.
Definition DBoundingBox.h:87
@ DIMENSION
Definition DBoundingBox.h:39
Internal::DIntervalBase< D > Base
Base class type.
Definition DBoundingBox.h:41
Base::CoordinateType CoordinateType
Coordinate type of the positions.
Definition DBoundingBox.h:45
void enlarge(CoordinateType x, CoordinateType y)
Enlarges the bounding box such that it contains a position specified by two coordinates.
Definition DBoundingBox.h:108
DBoundingBox & operator=(const DBoundingBox &rhs)
Assignment operator.
Definition DBoundingBox.h:68
bool intersects(const DBoundingBox &bounding_box) const
Definition DBoundingBox.h:157
bool isEmpty() const
Test if bounding box is empty.
Definition DBoundingBox.h:169
~DBoundingBox()
Destructor.
Definition DBoundingBox.h:82
void enlarge(const PositionType &p)
Enlarges the bounding box such that it contains a position.
Definition DBoundingBox.h:98
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
DIntervalBase & operator=(const DIntervalBase &rhs)
Assignment operator.
Definition DIntervalBase.h:70
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
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::DBoundingBox< D > &bb) const noexcept
Definition DBoundingBox.h:208