OpenMS
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-2023.
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 <cmath> // for std::abs on integrals and floats
43 #include <limits>
44 #include <ostream>
45 
46 namespace OpenMS
47 {
53  template <UInt D, typename TCoordinateType = double>
54  class DPosition
55  {
56 public:
57 
59  typedef TCoordinateType CoordinateType;
63  typedef const CoordinateType* ConstIterator;
65  enum
66  {
67  DIMENSION = D
68  };
79 
89  DPosition() = default;
90 
93  {
94  std::fill(&(coordinate_[0]), &(coordinate_[D]), x);
95  }
96 
99  {
100  static_assert(D == 2, "DPosition<D, TCoordinateType>:DPosition(x,y): index overflow!");
101  coordinate_[0] = x;
102  coordinate_[1] = y;
103  }
104 
107  {
108  static_assert(D == 3, "DPosition<D, TCoordinateType>:DPosition(x,y,z): index overflow!");
109  coordinate_[0] = x;
110  coordinate_[1] = y;
111  coordinate_[2] = z;
112  }
113 
115  DPosition(const DPosition& pos) = default;
116 
118  DPosition(DPosition&& rhs) noexcept = default;
119 
121  DPosition& operator=(const DPosition& source) = default;
122 
124  DPosition& operator=(DPosition&& source) noexcept = default;
125 
127  ~DPosition() noexcept = default;
128 
130 
132  void swap(DPosition& rhs) noexcept
133  {
134  for (Size i = 0; i < D; ++i)
135  {
136  std::swap(coordinate_[i], rhs.coordinate_[i]);
137  }
138  }
139 
141  DPosition& abs() noexcept
142  {
143  for (Size i = 0; i < D; ++i)
144  {
145  coordinate_[i] = std::abs(coordinate_[i]);
146  }
147  return *this;
148  }
149 
152 
155  {
156  OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
157  return coordinate_[index];
158  }
159 
162  {
163  OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
164  return coordinate_[index];
165  }
166 
169  {
170  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getX(): index overflow!");
171  return coordinate_[0];
172  }
173 
176  {
177  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getY(): index overflow!");
178  return coordinate_[1];
179  }
180 
183  {
184  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setX(): index overflow!");
185  coordinate_[0] = c;
186  }
187 
190  {
191  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setY(): index overflow!");
192  coordinate_[1] = c;
193  }
194 
196  bool operator==(const DPosition& point) const
197  {
198  for (Size i = 0; i < D; i++)
199  {
200 #pragma clang diagnostic push
201 #pragma clang diagnostic ignored "-Wfloat-equal"
202  if (coordinate_[i] != point.coordinate_[i]) return false;
203 
204 #pragma clang diagnostic pop
205  }
206  return true;
207  }
208 
210  bool operator!=(const DPosition& point) const
211  {
212  return !(operator==(point));
213  }
214 
219  bool operator<(const DPosition& point) const
220  {
221  for (Size i = 0; i < D; i++)
222  {
223  if (coordinate_[i] < point.coordinate_[i]) return true;
224 
225  if (coordinate_[i] > point.coordinate_[i]) return false;
226  }
227  return false;
228  }
229 
231  bool operator<=(const DPosition& point) const
232  {
233  for (Size i = 0; i < D; i++)
234  {
235  if (coordinate_[i] < point.coordinate_[i]) return true;
236 
237  if (coordinate_[i] > point.coordinate_[i]) return false;
238  }
239  return true;
240  }
241 
243  bool spatiallyLessEqual(const DPosition& point) const
244  {
245  for (Size i = 0; i < D; i++)
246  {
247  if (coordinate_[i] > point.coordinate_[i]) return false;
248  }
249  return true;
250  }
251 
253  bool spatiallyGreaterEqual(const DPosition& point) const
254  {
255  for (Size i = 0; i < D; i++)
256  {
257  if (coordinate_[i] < point.coordinate_[i]) return false;
258  }
259  return true;
260  }
261 
263  bool operator>(const DPosition& point) const
264  {
265  return !(operator<=(point));
266  }
267 
269  bool operator>=(const DPosition& point) const
270  {
271  return !operator<(point);
272  }
273 
275  DPosition operator+(const DPosition& point) const
276  {
277  DPosition result(*this);
278  for (Size i = 0; i < D; ++i)
279  {
280  result.coordinate_[i] += point.coordinate_[i];
281  }
282  return result;
283  }
284 
287  {
288  for (Size i = 0; i < D; ++i)
289  {
290  coordinate_[i] += point.coordinate_[i];
291  }
292  return *this;
293  }
294 
296  DPosition operator-(const DPosition& point) const
297  {
298  DPosition result(*this);
299  for (Size i = 0; i < D; ++i)
300  {
301  result.coordinate_[i] -= point.coordinate_[i];
302  }
303  return result;
304  }
305 
308  {
309  for (Size i = 0; i < D; ++i)
310  {
311  coordinate_[i] -= point.coordinate_[i];
312  }
313  return *this;
314  }
315 
318  {
319  DPosition<D, CoordinateType> result(*this);
320  for (Size i = 0; i < D; ++i)
321  {
322  result.coordinate_[i] = -result.coordinate_[i];
323  }
324  return result;
325  }
326 
328  CoordinateType operator*(const DPosition& point) const
329  {
330  CoordinateType prod(0);
331  for (Size i = 0; i < D; ++i)
332  {
333  prod += (point.coordinate_[i] * coordinate_[i]);
334  }
335  return prod;
336  }
337 
340  {
341  for (Size i = 0; i < D; ++i)
342  {
343  coordinate_[i] *= scalar;
344  }
345  return *this;
346  }
347 
350  {
351  for (Size i = 0; i < D; ++i)
352  {
353  coordinate_[i] /= scalar;
354  }
355  return *this;
356  }
357 
359  static Size size()
360  {
361  return D;
362  }
363 
365  void clear()
366  {
367  for (Size i = 0; i < D; ++i)
368  {
369  coordinate_[i] = static_cast<CoordinateType>(0);
370  }
371  }
372 
374 
378  inline static constexpr DPosition zero()
379  {
380  return DPosition(0);
381  }
382 
384  inline static constexpr DPosition minPositive()
385  {
386  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::min)());
387  }
388 
390  inline static constexpr DPosition minNegative()
391  {
392  return DPosition(std::numeric_limits<typename DPosition::CoordinateType>::lowest());
393  }
394 
396  inline static constexpr DPosition maxPositive()
397  {
398  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::max)());
399  }
400 
402 
407  {
408  return &(coordinate_[0]);
409  }
410 
413  {
414  return &(coordinate_[0]) + D;
415  }
416 
419  {
420  return &(coordinate_[0]);
421  }
422 
425  {
426  return &(coordinate_[0]) + D;
427  }
428 
430 
431 protected:
433  }; // DPosition
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>
460  {
461  for (Size i = 0; i < D; ++i)
462  {
463  position[i] /= scalar;
464  }
465  return position;
466  }
467 
469  template <UInt D, typename TCoordinateType>
470  std::ostream& operator<<(std::ostream& os, const DPosition<D, TCoordinateType>& pos)
471  {
472  os << precisionWrapper(pos[0]);
473  for (UInt i = 1; i < D; ++i)
474  {
475  os << ' ' << precisionWrapper(pos[i]);
476  }
477  return os;
478  }
479 
480 } // namespace OpenMS
481 
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:55
@ DIMENSION
Definition: DPosition.h:67
CoordinateType & operator[](Size index)
Accessor for the dimensions.
Definition: DPosition.h:161
static constexpr DPosition minPositive()
smallest positive
Definition: DPosition.h:384
CoordinateType value_type
Definition: DPosition.h:73
bool operator==(const DPosition &point) const
Equality operator.
Definition: DPosition.h:196
Iterator begin()
Mutable begin iterator.
Definition: DPosition.h:418
DPosition operator+(const DPosition &point) const
Addition (a bit inefficient)
Definition: DPosition.h:275
const CoordinateType * ConstIterator
Non-mutable iterator.
Definition: DPosition.h:63
bool operator<=(const DPosition &point) const
Lexicographical greater less or equal operator.
Definition: DPosition.h:231
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:92
DPosition & operator*=(CoordinateType scalar)
Scalar multiplication.
Definition: DPosition.h:339
DPosition(CoordinateType x, CoordinateType y)
Constructor only for DPosition<2> that takes two Coordinates.
Definition: DPosition.h:98
CoordinateType & reference
Definition: DPosition.h:74
CoordinateType operator[](Size index) const
Const accessor for the dimensions.
Definition: DPosition.h:154
bool spatiallyLessEqual(const DPosition &point) const
Spatially (geometrically) less or equal operator. All coordinates must be "<=".
Definition: DPosition.h:243
DPosition(CoordinateType x, CoordinateType y, CoordinateType z)
Constructor only for DPosition<3> that takes three Coordinates.
Definition: DPosition.h:106
DPosition()=default
Default constructor.
ConstIterator end() const
Non-mutable end iterator.
Definition: DPosition.h:412
DPosition(const DPosition &pos)=default
Copy constructor.
CoordinateType * iterator
Definition: DPosition.h:76
void setX(CoordinateType c)
Name mutator for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:182
bool operator<(const DPosition &point) const
Lexicographical less than operator. Lexicographical comparison from dimension 0 to dimension D-1 is d...
Definition: DPosition.h:219
CoordinateType getY() const
Name accessor for the second dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:175
bool operator>=(const DPosition &point) const
Lexicographical greater or equal operator.
Definition: DPosition.h:269
static constexpr DPosition maxPositive()
largest positive
Definition: DPosition.h:396
bool operator!=(const DPosition &point) const
Equality operator.
Definition: DPosition.h:210
bool operator>(const DPosition &point) const
Lexicographical greater than operator.
Definition: DPosition.h:263
DPosition operator-(const DPosition &point) const
Subtraction (a bit inefficient)
Definition: DPosition.h:296
CoordinateType getX() const
Name accessor for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:168
static constexpr DPosition zero()
all zero
Definition: DPosition.h:378
DPosition & abs() noexcept
Make all dimension values positive.
Definition: DPosition.h:141
const CoordinateType * const_iterator
Definition: DPosition.h:77
Iterator end()
Mutable end iterator.
Definition: DPosition.h:424
CoordinateType operator*(const DPosition &point) const
Inner product.
Definition: DPosition.h:328
static Size size()
Returns the number of dimensions.
Definition: DPosition.h:359
TCoordinateType CoordinateType
Coordinate type.
Definition: DPosition.h:59
DPosition & operator-=(const DPosition &point)
Subtraction.
Definition: DPosition.h:307
static constexpr DPosition minNegative()
smallest negative
Definition: DPosition.h:390
CoordinateType coordinate_[D]
Definition: DPosition.h:432
void clear()
Set all dimensions to zero.
Definition: DPosition.h:365
void swap(DPosition &rhs) noexcept
Swap the two objects.
Definition: DPosition.h:132
~DPosition() noexcept=default
Destructor (not-virtual as this will save a lot of space!)
ConstIterator begin() const
Non-mutable begin iterator.
Definition: DPosition.h:406
bool spatiallyGreaterEqual(const DPosition &point) const
Spatially (geometrically) greater or equal operator. All coordinates must be ">=".
Definition: DPosition.h:253
CoordinateType * pointer
Definition: DPosition.h:75
CoordinateType * Iterator
Mutable iterator.
Definition: DPosition.h:61
DPosition & operator+=(const DPosition &point)
Addition.
Definition: DPosition.h:286
DPosition & operator=(const DPosition &source)=default
Assignment operator.
DPosition & operator/=(CoordinateType scalar)
Scalar division.
Definition: DPosition.h:349
void setY(CoordinateType c)
Name mutator for the second dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:189
DPosition operator-() const
Negation (a bit inefficient)
Definition: DPosition.h:317
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:120
const double c
Definition: Constants.h:214
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48
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:437
DPosition< D, TCoordinateType > operator/(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:459
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