OpenMS  3.0.0
DPosition.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-2022.
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: Marc Sturm, Stephan Aiche $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Macros.h>
38 #include <OpenMS/CONCEPT/Types.h>
40 
41 #include <algorithm>
42 #include <limits>
43 #include <ostream>
44 
45 namespace OpenMS
46 {
52  template <UInt D, typename TCoordinateType = double>
53  class DPosition
54  {
55 public:
56 
58  typedef TCoordinateType CoordinateType;
62  typedef const CoordinateType* ConstIterator;
64  enum
65  {
67  };
78 
88  DPosition() = default;
89 
92  {
93  std::fill(&(coordinate_[0]), &(coordinate_[D]), x);
94  }
95 
98  {
99  static_assert(D == 2, "DPosition<D, TCoordinateType>:DPosition(x,y): index overflow!");
100  coordinate_[0] = x;
101  coordinate_[1] = y;
102  }
103 
106  {
107  static_assert(D == 3, "DPosition<D, TCoordinateType>:DPosition(x,y,z): index overflow!");
108  coordinate_[0] = x;
109  coordinate_[1] = y;
110  coordinate_[2] = z;
111  }
112 
114  DPosition(const DPosition& pos) = default;
115 
117  DPosition(DPosition&& rhs) noexcept = default;
118 
120  DPosition& operator=(const DPosition& source) = default;
121 
123  DPosition& operator=(DPosition&& source) noexcept = default;
124 
126  ~DPosition() noexcept = default;
127 
129 
131  void swap(DPosition& rhs) noexcept
132  {
133  for (Size i = 0; i < D; i++)
134  {
135  std::swap(coordinate_[i], rhs.coordinate_[i]);
136  }
137  }
138 
141 
144  {
145  OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
146  return coordinate_[index];
147  }
148 
151  {
152  OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
153  return coordinate_[index];
154  }
155 
158  {
159  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getX(): index overflow!");
160  return coordinate_[0];
161  }
162 
165  {
166  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getY(): index overflow!");
167  return coordinate_[1];
168  }
169 
172  {
173  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setX(): index overflow!");
174  coordinate_[0] = c;
175  }
176 
179  {
180  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setY(): index overflow!");
181  coordinate_[1] = c;
182  }
183 
185  bool operator==(const DPosition& point) const
186  {
187  for (Size i = 0; i < D; i++)
188  {
189 #pragma clang diagnostic push
190 #pragma clang diagnostic ignored "-Wfloat-equal"
191  if (coordinate_[i] != point.coordinate_[i]) return false;
192 
193 #pragma clang diagnostic pop
194  }
195  return true;
196  }
197 
199  bool operator!=(const DPosition& point) const
200  {
201  return !(operator==(point));
202  }
203 
208  bool operator<(const DPosition& point) const
209  {
210  for (Size i = 0; i < D; i++)
211  {
212  if (coordinate_[i] < point.coordinate_[i]) return true;
213 
214  if (coordinate_[i] > point.coordinate_[i]) return false;
215  }
216  return false;
217  }
218 
220  bool operator<=(const DPosition& point) const
221  {
222  for (Size i = 0; i < D; i++)
223  {
224  if (coordinate_[i] < point.coordinate_[i]) return true;
225 
226  if (coordinate_[i] > point.coordinate_[i]) return false;
227  }
228  return true;
229  }
230 
232  bool spatiallyLessEqual(const DPosition& point) const
233  {
234  for (Size i = 0; i < D; i++)
235  {
236  if (coordinate_[i] > point.coordinate_[i]) return false;
237  }
238  return true;
239  }
240 
242  bool spatiallyGreaterEqual(const DPosition& point) const
243  {
244  for (Size i = 0; i < D; i++)
245  {
246  if (coordinate_[i] < point.coordinate_[i]) return false;
247  }
248  return true;
249  }
250 
252  bool operator>(const DPosition& point) const
253  {
254  return !(operator<=(point));
255  }
256 
258  bool operator>=(const DPosition& point) const
259  {
260  return !operator<(point);
261  }
262 
264  DPosition operator+(const DPosition& point) const
265  {
266  DPosition result(*this);
267  for (Size i = 0; i < D; ++i)
268  {
269  result.coordinate_[i] += point.coordinate_[i];
270  }
271  return result;
272  }
273 
276  {
277  for (Size i = 0; i < D; ++i)
278  {
279  coordinate_[i] += point.coordinate_[i];
280  }
281  return *this;
282  }
283 
285  DPosition operator-(const DPosition& point) const
286  {
287  DPosition result(*this);
288  for (Size i = 0; i < D; ++i)
289  {
290  result.coordinate_[i] -= point.coordinate_[i];
291  }
292  return result;
293  }
294 
297  {
298  for (Size i = 0; i < D; ++i)
299  {
300  coordinate_[i] -= point.coordinate_[i];
301  }
302  return *this;
303  }
304 
307  {
308  DPosition<D, CoordinateType> result(*this);
309  for (Size i = 0; i < D; ++i)
310  {
311  result.coordinate_[i] = -result.coordinate_[i];
312  }
313  return result;
314  }
315 
317  CoordinateType operator*(const DPosition& point) const
318  {
319  CoordinateType prod(0);
320  for (Size i = 0; i < D; ++i)
321  {
322  prod += (point.coordinate_[i] * coordinate_[i]);
323  }
324  return prod;
325  }
326 
329  {
330  for (Size i = 0; i < D; ++i)
331  {
332  coordinate_[i] *= scalar;
333  }
334  return *this;
335  }
336 
339  {
340  for (Size i = 0; i < D; ++i)
341  {
342  coordinate_[i] /= scalar;
343  }
344  return *this;
345  }
346 
348  static Size size()
349  {
350  return D;
351  }
352 
354  void clear()
355  {
356  for (Size i = 0; i < D; ++i)
357  {
358  coordinate_[i] = static_cast<CoordinateType>(0);
359  }
360  }
361 
363 
366  inline static constexpr DPosition zero()
368  {
369  return DPosition(0);
370  }
371 
373  inline static constexpr DPosition minPositive()
374  {
375  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::min)());
376  }
377 
379  inline static constexpr DPosition minNegative()
380  {
381  return DPosition(std::numeric_limits<typename DPosition::CoordinateType>::lowest());
382  }
383 
385  inline static constexpr DPosition maxPositive()
386  {
387  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::max)());
388  }
389 
391 
394  ConstIterator begin() const
396  {
397  return &(coordinate_[0]);
398  }
399 
402  {
403  return &(coordinate_[0]) + D;
404  }
405 
408  {
409  return &(coordinate_[0]);
410  }
411 
414  {
415  return &(coordinate_[0]) + D;
416  }
417 
419 
420 protected:
422  }; // DPosition
423 
425  template <UInt D, typename TCoordinateType>
427  {
428  for (Size i = 0; i < D; ++i)
429  {
430  position[i] *= scalar;
431  }
432  return position;
433  }
434 
436  template <UInt D, typename TCoordinateType>
438  {
439  for (Size i = 0; i < D; ++i)
440  {
441  position[i] *= scalar;
442  }
443  return position;
444  }
445 
447  template <UInt D, typename TCoordinateType>
449  {
450  for (Size i = 0; i < D; ++i)
451  {
452  position[i] /= scalar;
453  }
454  return position;
455  }
456 
458  template <UInt D, typename TCoordinateType>
459  std::ostream& operator<<(std::ostream& os, const DPosition<D, TCoordinateType>& pos)
460  {
461  os << precisionWrapper(pos[0]);
462  for (UInt i = 1; i < D; ++i)
463  {
464  os << ' ' << precisionWrapper(pos[i]);
465  }
466  return os;
467  }
468 
469 } // namespace OpenMS
470 
OpenMS::DPosition::clear
void clear()
Set all dimensions to zero.
Definition: DPosition.h:354
OpenMS::DPosition::DIMENSION
Definition: DPosition.h:66
OpenMS::DPosition::CoordinateType
TCoordinateType CoordinateType
Coordinate type.
Definition: DPosition.h:58
OpenMS::DPosition::getY
CoordinateType getY() const
Name accessor for the second dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:164
OpenMS::DPosition::maxPositive
static constexpr DPosition maxPositive()
largest positive
Definition: DPosition.h:385
OpenMS::DPosition::setX
void setX(CoordinateType c)
Name mutator for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:171
double
Types.h
PrecisionWrapper.h
OpenMS::DPosition::spatiallyGreaterEqual
bool spatiallyGreaterEqual(const DPosition &point) const
Spatially (geometrically) greater or equal operator. All coordinates must be ">=".
Definition: DPosition.h:242
OpenMS::operator*
DPosition< D, TCoordinateType > operator*(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:426
OpenMS::DPosition::operator==
bool operator==(const DPosition &point) const
Equality operator.
Definition: DPosition.h:185
OpenMS::DPosition::Iterator
CoordinateType * Iterator
Mutable iterator.
Definition: DPosition.h:60
OpenMS::DPosition::DPosition
DPosition(CoordinateType x, CoordinateType y)
Constructor only for DPosition<2> that takes two Coordinates.
Definition: DPosition.h:97
OpenMS::DPosition::size
static Size size()
Returns the number of dimensions.
Definition: DPosition.h:348
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::Constants::c
const double c
Definition: Constants.h:209
OpenMS::DPosition::getX
CoordinateType getX() const
Name accessor for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:157
OpenMS::operator/
DPosition< D, TCoordinateType > operator/(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:448
OpenMS::DPosition::iterator
CoordinateType * iterator
Definition: DPosition.h:75
OpenMS::DPosition::end
Iterator end()
Mutable end iterator.
Definition: DPosition.h:413
OpenMS::DPosition::operator-=
DPosition & operator-=(const DPosition &point)
Subtraction.
Definition: DPosition.h:296
OPENMS_PRECONDITION
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:120
OpenMS::DPosition::end
ConstIterator end() const
Non-mutable end iterator.
Definition: DPosition.h:401
OpenMS::DPosition::pointer
CoordinateType * pointer
Definition: DPosition.h:74
OpenMS::DPosition::spatiallyLessEqual
bool spatiallyLessEqual(const DPosition &point) const
Spatially (geometrically) less or equal operator. All coordinates must be "<=".
Definition: DPosition.h:232
OpenMS::DPosition::reference
CoordinateType & reference
Definition: DPosition.h:73
Macros.h
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
OpenMS::DPosition::operator>
bool operator>(const DPosition &point) const
Lexicographical greater than operator.
Definition: DPosition.h:252
OpenMS::DPosition::minNegative
static constexpr DPosition minNegative()
smallest negative
Definition: DPosition.h:379
OpenMS::precisionWrapper
const PrecisionWrapper< FloatingPointType > precisionWrapper(const FloatingPointType rhs)
Wrapper function that sets the appropriate precision for output temporarily. The original precision i...
Definition: PrecisionWrapper.h:95
OpenMS::DPosition::coordinate_
CoordinateType coordinate_[D]
Definition: DPosition.h:421
OpenMS::DPosition::operator!=
bool operator!=(const DPosition &point) const
Equality operator.
Definition: DPosition.h:199
OpenMS::DPosition::const_iterator
const typedef CoordinateType * const_iterator
Definition: DPosition.h:76
OpenMS::DPosition::operator*=
DPosition & operator*=(CoordinateType scalar)
Scalar multiplication.
Definition: DPosition.h:328
OpenMS::DPosition::begin
Iterator begin()
Mutable begin iterator.
Definition: DPosition.h:407
OpenMS::DPosition::value_type
CoordinateType value_type
Definition: DPosition.h:72
OpenMS::DPosition::setY
void setY(CoordinateType c)
Name mutator for the second dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:178
OpenMS::DPosition::operator[]
CoordinateType operator[](Size index) const
Const accessor for the dimensions.
Definition: DPosition.h:143
OpenMS::operator<<
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
OpenMS::DPosition
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:53
OpenMS::DPosition::minPositive
static constexpr DPosition minPositive()
smallest positive
Definition: DPosition.h:373
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
OpenMS::DPosition::DPosition
DPosition(CoordinateType x)
Constructor that fills all dimensions with the value x.
Definition: DPosition.h:91
OpenMS::DPosition::operator+
DPosition operator+(const DPosition &point) const
Addition (a bit inefficient)
Definition: DPosition.h:264
OpenMS::DPosition::DPosition
DPosition(CoordinateType x, CoordinateType y, CoordinateType z)
Constructor only for DPosition<3> that takes three Coordinates.
Definition: DPosition.h:105
OpenMS::DPosition::operator+=
DPosition & operator+=(const DPosition &point)
Addition.
Definition: DPosition.h:275
OpenMS::DPosition::~DPosition
~DPosition() noexcept=default
Destructor (not-virtual as this will save a lot of space!)
OpenMS::DPosition::operator-
DPosition operator-(const DPosition &point) const
Subtraction (a bit inefficient)
Definition: DPosition.h:285
OpenMS::DPosition::begin
ConstIterator begin() const
Non-mutable begin iterator.
Definition: DPosition.h:395
OpenMS::DPosition::operator[]
CoordinateType & operator[](Size index)
Accessor for the dimensions.
Definition: DPosition.h:150
OpenMS::DPosition::DPosition
DPosition()=default
Default constructor.
OpenMS::DPosition::swap
void swap(DPosition &rhs) noexcept
Swap the two points.
Definition: DPosition.h:131
OpenMS::DPosition::operator=
DPosition & operator=(const DPosition &source)=default
Assignment operator.
OpenMS::DPosition::operator<=
bool operator<=(const DPosition &point) const
Lexicographical greater less or equal operator.
Definition: DPosition.h:220
OpenMS::DPosition::zero
static constexpr DPosition zero()
all zero
Definition: DPosition.h:367
OpenMS::DPosition::operator>=
bool operator>=(const DPosition &point) const
Lexicographical greater or equal operator.
Definition: DPosition.h:258
OpenMS::DPosition::ConstIterator
const typedef CoordinateType * ConstIterator
Non-mutable iterator.
Definition: DPosition.h:62
OpenMS::DPosition::operator<
bool operator<(const DPosition &point) const
Lexicographical less than operator. Lexicographical comparison from dimension 0 to dimension D-1 is d...
Definition: DPosition.h:208
OpenMS::DPosition::operator-
DPosition operator-() const
Negation (a bit inefficient)
Definition: DPosition.h:306
OpenMS::DPosition::operator/=
DPosition & operator/=(CoordinateType scalar)
Scalar division.
Definition: DPosition.h:338
OpenMS::DPosition::operator*
CoordinateType operator*(const DPosition &point) const
Inner product.
Definition: DPosition.h:317