OpenMS  2.7.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-2021.
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  {
66  DIMENSION = D
67  };
78 
89  {
90  clear();
91  }
92 
95  {
96  }
97 
100  {
101  std::fill(&(coordinate_[0]), &(coordinate_[D]), x);
102  }
103 
105  DPosition(const DPosition& pos)
106  {
107  std::copy(&(pos.coordinate_[0]), &(pos.coordinate_[D]),
108  &(coordinate_[0]));
109  }
110 
112  DPosition(DPosition&& rhs) noexcept
113  {
114  // NOTE: do not change this before testing with nightly Windows builds ( = default causes segfault)
115  std::move(std::begin(rhs.coordinate_), std::end(rhs.coordinate_), &coordinate_[0]);
116  }
117 
120  {
121  OPENMS_PRECONDITION(D == 2, "DPosition<D, TCoordinateType>:DPosition(x,y): index overflow!");
122  coordinate_[0] = x;
123  coordinate_[1] = y;
124  }
125 
127  DPosition& operator=(const DPosition& source)
128  {
129  if (&source == this) return *this;
130 
131  std::copy(&(source.coordinate_[0]), &(source.coordinate_[D]),
132  &(coordinate_[0]));
133 
134  return *this;
135  }
136 
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 
367  inline static const DPosition zero()
368  {
369  return DPosition(0);
370  }
371 
373  inline static const DPosition minPositive()
374  {
375  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::min)());
376  }
377 
379  inline static const DPosition minNegative()
380  {
381  return DPosition(-(std::numeric_limits<typename DPosition::CoordinateType>::max)());
382  }
383 
385  inline static const DPosition maxPositive()
386  {
387  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::max)());
388  }
389 
391 
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 
423  }; // DPosition
424 
426  template <UInt D, typename TCoordinateType>
428  {
429  for (Size i = 0; i < D; ++i)
430  {
431  position[i] *= scalar;
432  }
433  return position;
434  }
435 
437  template <UInt D, typename TCoordinateType>
439  {
440  for (Size i = 0; i < D; ++i)
441  {
442  position[i] *= scalar;
443  }
444  return position;
445  }
446 
448  template <UInt D, typename TCoordinateType>
450  {
451  for (Size i = 0; i < D; ++i)
452  {
453  position[i] /= scalar;
454  }
455  return position;
456  }
457 
459  template <UInt D, typename TCoordinateType>
460  std::ostream& operator<<(std::ostream& os, const DPosition<D, TCoordinateType>& pos)
461  {
462  os << precisionWrapper(pos[0]);
463  for (UInt i = 1; i < D; ++i)
464  {
465  os << ' ' << precisionWrapper(pos[i]);
466  }
467  return os;
468  }
469 
470 } // namespace OpenMS
471 
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:54
@ DIMENSION
Definition: DPosition.h:66
CoordinateType & operator[](Size index)
Accessor for the dimensions.
Definition: DPosition.h:150
CoordinateType value_type
Definition: DPosition.h:72
bool operator==(const DPosition &point) const
Equality operator.
Definition: DPosition.h:185
Iterator begin()
Mutable begin iterator.
Definition: DPosition.h:407
DPosition operator+(const DPosition &point) const
Addition (a bit inefficient)
Definition: DPosition.h:264
static const DPosition zero()
all zero
Definition: DPosition.h:367
const CoordinateType * ConstIterator
Non-mutable iterator.
Definition: DPosition.h:62
bool operator<=(const DPosition &point) const
Lexicographical greater less or equal operator.
Definition: DPosition.h:220
DPosition()
Default constructor.
Definition: DPosition.h:88
static const DPosition maxPositive()
largest positive
Definition: DPosition.h:385
DPosition(CoordinateType x)
Constructor that fills all dimensions with the value x.
Definition: DPosition.h:99
DPosition & operator*=(CoordinateType scalar)
Scalar multiplication.
Definition: DPosition.h:328
DPosition(CoordinateType x, CoordinateType y)
Constructor only for DPosition<2> that takes two Coordinates.
Definition: DPosition.h:119
CoordinateType & reference
Definition: DPosition.h:73
CoordinateType operator[](Size index) const
Const accessor for the dimensions.
Definition: DPosition.h:143
bool spatiallyLessEqual(const DPosition &point) const
Spatially (geometrically) less or equal operator. All coordinates must be "<=".
Definition: DPosition.h:232
ConstIterator end() const
Non-mutable end iterator.
Definition: DPosition.h:401
static const DPosition minPositive()
smallest positive
Definition: DPosition.h:373
CoordinateType * iterator
Definition: DPosition.h:75
void setX(CoordinateType c)
Name mutator for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:171
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
CoordinateType getY() const
Name accessor for the second dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:164
bool operator>=(const DPosition &point) const
Lexicographical greater or equal operator.
Definition: DPosition.h:258
bool operator!=(const DPosition &point) const
Equality operator.
Definition: DPosition.h:199
bool operator>(const DPosition &point) const
Lexicographical greater than operator.
Definition: DPosition.h:252
DPosition operator-(const DPosition &point) const
Subtraction (a bit inefficient)
Definition: DPosition.h:285
static const DPosition minNegative()
smallest negative
Definition: DPosition.h:379
CoordinateType getX() const
Name accessor for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:157
const CoordinateType * const_iterator
Definition: DPosition.h:76
Iterator end()
Mutable end iterator.
Definition: DPosition.h:413
CoordinateType operator*(const DPosition &point) const
Inner product.
Definition: DPosition.h:317
static Size size()
Returns the number of dimensions.
Definition: DPosition.h:348
TCoordinateType CoordinateType
Coordinate type.
Definition: DPosition.h:58
DPosition & operator-=(const DPosition &point)
Subtraction.
Definition: DPosition.h:296
~DPosition()
Destructor (not-virtual as this will save a lot of space!)
Definition: DPosition.h:94
CoordinateType coordinate_[D]
Definition: DPosition.h:421
void clear()
Set all dimensions to zero.
Definition: DPosition.h:354
ConstIterator begin() const
Non-mutable begin iterator.
Definition: DPosition.h:395
bool spatiallyGreaterEqual(const DPosition &point) const
Spatially (geometrically) greater or equal operator. All coordinates must be ">=".
Definition: DPosition.h:242
CoordinateType * pointer
Definition: DPosition.h:74
CoordinateType * Iterator
Mutable iterator.
Definition: DPosition.h:60
DPosition & operator+=(const DPosition &point)
Addition.
Definition: DPosition.h:275
DPosition(DPosition &&rhs) noexcept
Move constructor.
Definition: DPosition.h:112
DPosition(const DPosition &pos)
Copy constructor.
Definition: DPosition.h:105
DPosition & operator=(const DPosition &source)
Assignment operator.
Definition: DPosition.h:127
DPosition & operator/=(CoordinateType scalar)
Scalar division.
Definition: DPosition.h:338
void setY(CoordinateType c)
Name mutator for the second dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:178
DPosition operator-() const
Negation (a bit inefficient)
Definition: DPosition.h:306
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:209
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
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:427
DPosition< D, TCoordinateType > operator/(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:449
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
Size< TNeedle >::Type position(const PatternAuxData< TNeedle > &dh)
Definition: AhoCorasickAmbiguous.h:563