OpenMS
DPosition.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg$
6 // $Authors: Marc Sturm, Stephan Aiche $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/CONCEPT/Macros.h>
12 #include <OpenMS/CONCEPT/Types.h>
14 
15 #include <algorithm>
16 #include <cmath> // for std::abs on integrals and floats
17 #include <limits>
18 #include <ostream>
19 
20 namespace OpenMS
21 {
27  template <UInt D, typename TCoordinateType = double>
28  class DPosition
29  {
30 public:
31 
33  typedef TCoordinateType CoordinateType;
37  typedef const CoordinateType* ConstIterator;
39  enum
40  {
41  DIMENSION = D
42  };
53 
63  DPosition() = default;
64 
67  {
68  std::fill(&(coordinate_[0]), &(coordinate_[D]), x);
69  }
70 
73  {
74  static_assert(D == 2, "DPosition<D, TCoordinateType>:DPosition(x,y): index overflow!");
75  coordinate_[0] = x;
76  coordinate_[1] = y;
77  }
78 
81  {
82  static_assert(D == 3, "DPosition<D, TCoordinateType>:DPosition(x,y,z): index overflow!");
83  coordinate_[0] = x;
84  coordinate_[1] = y;
85  coordinate_[2] = z;
86  }
87 
89  DPosition(const DPosition& pos) = default;
90 
92  DPosition(DPosition&& rhs) noexcept = default;
93 
95  DPosition& operator=(const DPosition& source) = default;
96 
98  DPosition& operator=(DPosition&& source) noexcept = default;
99 
101  ~DPosition() noexcept = default;
102 
104 
106  void swap(DPosition& rhs) noexcept
107  {
108  for (Size i = 0; i < D; ++i)
109  {
110  std::swap(coordinate_[i], rhs.coordinate_[i]);
111  }
112  }
113 
115  DPosition& abs() noexcept
116  {
117  for (Size i = 0; i < D; ++i)
118  {
119  coordinate_[i] = std::abs(coordinate_[i]);
120  }
121  return *this;
122  }
123 
126 
129  {
130  OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
131  return coordinate_[index];
132  }
133 
136  {
137  OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
138  return coordinate_[index];
139  }
140 
143  {
144  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getX(): index overflow!");
145  return coordinate_[0];
146  }
147 
150  {
151  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getY(): index overflow!");
152  return coordinate_[1];
153  }
154 
157  {
158  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setX(): index overflow!");
159  coordinate_[0] = c;
160  }
161 
164  {
165  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setY(): index overflow!");
166  coordinate_[1] = c;
167  }
168 
170  bool operator==(const DPosition& point) const
171  {
172  for (Size i = 0; i < D; i++)
173  {
174 #pragma clang diagnostic push
175 #pragma clang diagnostic ignored "-Wfloat-equal"
176  if (coordinate_[i] != point.coordinate_[i]) return false;
177 
178 #pragma clang diagnostic pop
179  }
180  return true;
181  }
182 
184  bool operator!=(const DPosition& point) const
185  {
186  return !(operator==(point));
187  }
188 
193  bool operator<(const DPosition& point) const
194  {
195  for (Size i = 0; i < D; i++)
196  {
197  if (coordinate_[i] < point.coordinate_[i]) return true;
198 
199  if (coordinate_[i] > point.coordinate_[i]) return false;
200  }
201  return false;
202  }
203 
205  bool operator<=(const DPosition& point) const
206  {
207  for (Size i = 0; i < D; i++)
208  {
209  if (coordinate_[i] < point.coordinate_[i]) return true;
210 
211  if (coordinate_[i] > point.coordinate_[i]) return false;
212  }
213  return true;
214  }
215 
217  bool spatiallyLessEqual(const DPosition& point) const
218  {
219  for (Size i = 0; i < D; i++)
220  {
221  if (coordinate_[i] > point.coordinate_[i]) return false;
222  }
223  return true;
224  }
225 
227  bool spatiallyGreaterEqual(const DPosition& point) const
228  {
229  for (Size i = 0; i < D; i++)
230  {
231  if (coordinate_[i] < point.coordinate_[i]) return false;
232  }
233  return true;
234  }
235 
237  bool operator>(const DPosition& point) const
238  {
239  return !(operator<=(point));
240  }
241 
243  bool operator>=(const DPosition& point) const
244  {
245  return !operator<(point);
246  }
247 
249  DPosition operator+(const DPosition& point) const
250  {
251  DPosition result(*this);
252  for (Size i = 0; i < D; ++i)
253  {
254  result.coordinate_[i] += point.coordinate_[i];
255  }
256  return result;
257  }
258 
261  {
262  for (Size i = 0; i < D; ++i)
263  {
264  coordinate_[i] += point.coordinate_[i];
265  }
266  return *this;
267  }
268 
270  DPosition operator-(const DPosition& point) const
271  {
272  DPosition result(*this);
273  for (Size i = 0; i < D; ++i)
274  {
275  result.coordinate_[i] -= point.coordinate_[i];
276  }
277  return result;
278  }
279 
282  {
283  for (Size i = 0; i < D; ++i)
284  {
285  coordinate_[i] -= point.coordinate_[i];
286  }
287  return *this;
288  }
289 
292  {
293  DPosition<D, CoordinateType> result(*this);
294  for (Size i = 0; i < D; ++i)
295  {
296  result.coordinate_[i] = -result.coordinate_[i];
297  }
298  return result;
299  }
300 
302  CoordinateType operator*(const DPosition& point) const
303  {
304  CoordinateType prod(0);
305  for (Size i = 0; i < D; ++i)
306  {
307  prod += (point.coordinate_[i] * coordinate_[i]);
308  }
309  return prod;
310  }
311 
314  {
315  for (Size i = 0; i < D; ++i)
316  {
317  coordinate_[i] *= scalar;
318  }
319  return *this;
320  }
321 
324  {
325  for (Size i = 0; i < D; ++i)
326  {
327  coordinate_[i] /= scalar;
328  }
329  return *this;
330  }
331 
333  constexpr static Size size()
334  {
335  return D;
336  }
337 
339  void clear()
340  {
341  for (Size i = 0; i < D; ++i)
342  {
343  coordinate_[i] = static_cast<CoordinateType>(0);
344  }
345  }
346 
348 
352  inline static constexpr DPosition zero()
353  {
354  return DPosition(0);
355  }
356 
358  inline static constexpr DPosition minPositive()
359  {
360  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::min)());
361  }
362 
364  inline static constexpr DPosition minNegative()
365  {
366  return DPosition(std::numeric_limits<typename DPosition::CoordinateType>::lowest());
367  }
368 
370  inline static constexpr DPosition maxPositive()
371  {
372  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::max)());
373  }
374 
376 
381  {
382  return &(coordinate_[0]);
383  }
384 
387  {
388  return &(coordinate_[0]) + D;
389  }
390 
393  {
394  return &(coordinate_[0]);
395  }
396 
399  {
400  return &(coordinate_[0]) + D;
401  }
402 
404 
405 protected:
407  }; // DPosition
408 
410  template <UInt D, typename TCoordinateType>
412  {
413  for (Size i = 0; i < D; ++i)
414  {
415  position[i] *= scalar;
416  }
417  return position;
418  }
419 
421  template <UInt D, typename TCoordinateType>
423  {
424  for (Size i = 0; i < D; ++i)
425  {
426  position[i] *= scalar;
427  }
428  return position;
429  }
430 
432  template <UInt D, typename TCoordinateType>
434  {
435  for (Size i = 0; i < D; ++i)
436  {
437  position[i] /= scalar;
438  }
439  return position;
440  }
441 
443  template <UInt D, typename TCoordinateType>
444  std::ostream& operator<<(std::ostream& os, const DPosition<D, TCoordinateType>& pos)
445  {
446  os << precisionWrapper(pos[0]);
447  for (UInt i = 1; i < D; ++i)
448  {
449  os << ' ' << precisionWrapper(pos[i]);
450  }
451  return os;
452  }
453 
454 } // namespace OpenMS
455 
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:29
constexpr static Size size()
Returns the number of dimensions.
Definition: DPosition.h:333
@ DIMENSION
Definition: DPosition.h:41
CoordinateType & operator[](Size index)
Accessor for the dimensions.
Definition: DPosition.h:135
static constexpr DPosition minPositive()
smallest positive
Definition: DPosition.h:358
CoordinateType value_type
Definition: DPosition.h:47
bool operator==(const DPosition &point) const
Equality operator.
Definition: DPosition.h:170
Iterator begin()
Mutable begin iterator.
Definition: DPosition.h:392
DPosition operator+(const DPosition &point) const
Addition (a bit inefficient)
Definition: DPosition.h:249
const CoordinateType * ConstIterator
Non-mutable iterator.
Definition: DPosition.h:37
bool operator<=(const DPosition &point) const
Lexicographical greater less or equal operator.
Definition: DPosition.h:205
DPosition(DPosition &&rhs) noexcept=default
Move constructor.
DPosition & operator=(DPosition &&source) noexcept=default
Move Assignment operator.
DPosition(CoordinateType x)
Constructor that fills all dimensions with the value x.
Definition: DPosition.h:66
DPosition & operator*=(CoordinateType scalar)
Scalar multiplication.
Definition: DPosition.h:313
DPosition(CoordinateType x, CoordinateType y)
Constructor only for DPosition<2> that takes two Coordinates.
Definition: DPosition.h:72
CoordinateType & reference
Definition: DPosition.h:48
CoordinateType operator[](Size index) const
Const accessor for the dimensions.
Definition: DPosition.h:128
bool spatiallyLessEqual(const DPosition &point) const
Spatially (geometrically) less or equal operator. All coordinates must be "<=".
Definition: DPosition.h:217
DPosition(CoordinateType x, CoordinateType y, CoordinateType z)
Constructor only for DPosition<3> that takes three Coordinates.
Definition: DPosition.h:80
DPosition()=default
Default constructor.
ConstIterator end() const
Non-mutable end iterator.
Definition: DPosition.h:386
DPosition(const DPosition &pos)=default
Copy constructor.
CoordinateType * iterator
Definition: DPosition.h:50
void setX(CoordinateType c)
Name mutator for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:156
bool operator<(const DPosition &point) const
Lexicographical less than operator. Lexicographical comparison from dimension 0 to dimension D-1 is d...
Definition: DPosition.h:193
CoordinateType getY() const
Name accessor for the second dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:149
bool operator>=(const DPosition &point) const
Lexicographical greater or equal operator.
Definition: DPosition.h:243
static constexpr DPosition maxPositive()
largest positive
Definition: DPosition.h:370
bool operator!=(const DPosition &point) const
Equality operator.
Definition: DPosition.h:184
bool operator>(const DPosition &point) const
Lexicographical greater than operator.
Definition: DPosition.h:237
DPosition operator-(const DPosition &point) const
Subtraction (a bit inefficient)
Definition: DPosition.h:270
CoordinateType getX() const
Name accessor for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:142
static constexpr DPosition zero()
all zero
Definition: DPosition.h:352
DPosition & abs() noexcept
Make all dimension values positive.
Definition: DPosition.h:115
const CoordinateType * const_iterator
Definition: DPosition.h:51
Iterator end()
Mutable end iterator.
Definition: DPosition.h:398
CoordinateType operator*(const DPosition &point) const
Inner product.
Definition: DPosition.h:302
TCoordinateType CoordinateType
Coordinate type.
Definition: DPosition.h:33
DPosition & operator-=(const DPosition &point)
Subtraction.
Definition: DPosition.h:281
static constexpr DPosition minNegative()
smallest negative
Definition: DPosition.h:364
CoordinateType coordinate_[D]
Definition: DPosition.h:406
void clear()
Set all dimensions to zero.
Definition: DPosition.h:339
void swap(DPosition &rhs) noexcept
Swap the two objects.
Definition: DPosition.h:106
~DPosition() noexcept=default
Destructor (not-virtual as this will save a lot of space!)
ConstIterator begin() const
Non-mutable begin iterator.
Definition: DPosition.h:380
bool spatiallyGreaterEqual(const DPosition &point) const
Spatially (geometrically) greater or equal operator. All coordinates must be ">=".
Definition: DPosition.h:227
CoordinateType * pointer
Definition: DPosition.h:49
CoordinateType * Iterator
Mutable iterator.
Definition: DPosition.h:35
DPosition & operator+=(const DPosition &point)
Addition.
Definition: DPosition.h:260
DPosition & operator=(const DPosition &source)=default
Assignment operator.
DPosition & operator/=(CoordinateType scalar)
Scalar division.
Definition: DPosition.h:323
void setY(CoordinateType c)
Name mutator for the second dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:163
DPosition operator-() const
Negation (a bit inefficient)
Definition: DPosition.h:291
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
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:94
const double c
Definition: Constants.h:188
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
DPosition< D, TCoordinateType > operator*(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:411
DPosition< D, TCoordinateType > operator/(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:433
const PrecisionWrapper< FloatingPointType > precisionWrapper(const FloatingPointType rhs)
Wrapper function that sets the appropriate precision for output temporarily. The original precision i...
Definition: PrecisionWrapper.h:69