OpenMS  2.4.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-2018.
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 
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 
113  {
114  OPENMS_PRECONDITION(D == 2, "DPosition<D, TCoordinateType>:DPosition(x,y): index overflow!");
115  coordinate_[0] = x;
116  coordinate_[1] = y;
117  }
118 
120  DPosition& operator=(const DPosition& source)
121  {
122  if (&source == this) return *this;
123 
124  std::copy(&(source.coordinate_[0]), &(source.coordinate_[D]),
125  &(coordinate_[0]));
126 
127  return *this;
128  }
129 
131 
134 
137  {
138  OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
139  return coordinate_[index];
140  }
141 
144  {
145  OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
146  return coordinate_[index];
147  }
148 
151  {
152  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getX(): index overflow!");
153  return coordinate_[0];
154  }
155 
158  {
159  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getY(): index overflow!");
160  return coordinate_[1];
161  }
162 
165  {
166  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setX(): index overflow!");
167  coordinate_[0] = c;
168  }
169 
172  {
173  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setY(): index overflow!");
174  coordinate_[1] = c;
175  }
176 
178  bool operator==(const DPosition& point) const
179  {
180  for (Size i = 0; i < D; i++)
181  {
182 #pragma clang diagnostic push
183 #pragma clang diagnostic ignored "-Wfloat-equal"
184  if (coordinate_[i] != point.coordinate_[i]) return false;
185 
186 #pragma clang diagnostic pop
187  }
188  return true;
189  }
190 
192  bool operator!=(const DPosition& point) const
193  {
194  return !(operator==(point));
195  }
196 
201  bool operator<(const DPosition& point) const
202  {
203  for (Size i = 0; i < D; i++)
204  {
205  if (coordinate_[i] < point.coordinate_[i]) return true;
206 
207  if (coordinate_[i] > point.coordinate_[i]) return false;
208  }
209  return false;
210  }
211 
213  bool operator<=(const DPosition& point) const
214  {
215  for (Size i = 0; i < D; i++)
216  {
217  if (coordinate_[i] < point.coordinate_[i]) return true;
218 
219  if (coordinate_[i] > point.coordinate_[i]) return false;
220  }
221  return true;
222  }
223 
225  bool spatiallyLessEqual(const DPosition& point) const
226  {
227  for (Size i = 0; i < D; i++)
228  {
229  if (coordinate_[i] > point.coordinate_[i]) return false;
230  }
231  return true;
232  }
233 
235  bool spatiallyGreaterEqual(const DPosition& point) const
236  {
237  for (Size i = 0; i < D; i++)
238  {
239  if (coordinate_[i] < point.coordinate_[i]) return false;
240  }
241  return true;
242  }
243 
245  bool operator>(const DPosition& point) const
246  {
247  return !(operator<=(point));
248  }
249 
251  bool operator>=(const DPosition& point) const
252  {
253  return !operator<(point);
254  }
255 
257  DPosition operator+(const DPosition& point) const
258  {
259  DPosition result(*this);
260  for (Size i = 0; i < D; ++i)
261  {
262  result.coordinate_[i] += point.coordinate_[i];
263  }
264  return result;
265  }
266 
269  {
270  for (Size i = 0; i < D; ++i)
271  {
272  coordinate_[i] += point.coordinate_[i];
273  }
274  return *this;
275  }
276 
278  DPosition operator-(const DPosition& point) const
279  {
280  DPosition result(*this);
281  for (Size i = 0; i < D; ++i)
282  {
283  result.coordinate_[i] -= point.coordinate_[i];
284  }
285  return result;
286  }
287 
290  {
291  for (Size i = 0; i < D; ++i)
292  {
293  coordinate_[i] -= point.coordinate_[i];
294  }
295  return *this;
296  }
297 
300  {
301  DPosition<D, CoordinateType> result(*this);
302  for (Size i = 0; i < D; ++i)
303  {
304  result.coordinate_[i] = -result.coordinate_[i];
305  }
306  return result;
307  }
308 
310  CoordinateType operator*(const DPosition& point) const
311  {
312  CoordinateType prod(0);
313  for (Size i = 0; i < D; ++i)
314  {
315  prod += (point.coordinate_[i] * coordinate_[i]);
316  }
317  return prod;
318  }
319 
322  {
323  for (Size i = 0; i < D; ++i)
324  {
325  coordinate_[i] *= scalar;
326  }
327  return *this;
328  }
329 
332  {
333  for (Size i = 0; i < D; ++i)
334  {
335  coordinate_[i] /= scalar;
336  }
337  return *this;
338  }
339 
341  static Size size()
342  {
343  return D;
344  }
345 
347  void clear()
348  {
349  for (Size i = 0; i < D; ++i)
350  {
351  coordinate_[i] = static_cast<CoordinateType>(0);
352  }
353  }
354 
356 
359  inline static const DPosition zero()
361  {
362  return DPosition(0);
363  }
364 
366  inline static const DPosition minPositive()
367  {
368  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::min)());
369  }
370 
372  inline static const DPosition minNegative()
373  {
374  return DPosition(-(std::numeric_limits<typename DPosition::CoordinateType>::max)());
375  }
376 
378  inline static const DPosition maxPositive()
379  {
380  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::max)());
381  }
382 
384 
387  ConstIterator begin() const
389  {
390  return &(coordinate_[0]);
391  }
392 
395  {
396  return &(coordinate_[0]) + D;
397  }
398 
401  {
402  return &(coordinate_[0]);
403  }
404 
407  {
408  return &(coordinate_[0]) + D;
409  }
410 
412 
413 protected:
415 
416  }; // DPosition
417 
419  template <UInt D, typename TCoordinateType>
421  {
422  for (Size i = 0; i < D; ++i)
423  {
424  position[i] *= scalar;
425  }
426  return position;
427  }
428 
430  template <UInt D, typename TCoordinateType>
432  {
433  for (Size i = 0; i < D; ++i)
434  {
435  position[i] *= scalar;
436  }
437  return position;
438  }
439 
441  template <UInt D, typename TCoordinateType>
443  {
444  for (Size i = 0; i < D; ++i)
445  {
446  position[i] /= scalar;
447  }
448  return position;
449  }
450 
452  template <UInt D, typename TCoordinateType>
453  std::ostream& operator<<(std::ostream& os, const DPosition<D, TCoordinateType>& pos)
454  {
455  os << precisionWrapper(pos[0]);
456  for (UInt i = 1; i < D; ++i)
457  {
458  os << ' ' << precisionWrapper(pos[i]);
459  }
460  return os;
461  }
462 
463 } // namespace OpenMS
464 
const CoordinateType * const_iterator
Definition: DPosition.h:76
CoordinateType operator*(const DPosition &point) const
Inner product.
Definition: DPosition.h:310
DPosition operator+(const DPosition &point) const
Addition (a bit inefficient)
Definition: DPosition.h:257
bool operator!=(const DPosition &point) const
Equality operator.
Definition: DPosition.h:192
Size< TNeedle >::Type position(const PatternAuxData< TNeedle > &dh)
Definition: AhoCorasickAmbiguous.h:561
Iterator begin()
Mutable begin iterator.
Definition: DPosition.h:400
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:106
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
bool operator<(const DPosition &point) const
Lexicographical less than operator. Lexicographical comparison from dimension 0 to dimension D-1 is d...
Definition: DPosition.h:201
Definition: DPosition.h:66
DPosition(CoordinateType x, CoordinateType y)
Constructor only for DPosition<2> that takes two Coordinates.
Definition: DPosition.h:112
DPosition & operator*=(CoordinateType scalar)
Scalar multiplication.
Definition: DPosition.h:321
DPosition operator-() const
Negation (a bit inefficient)
Definition: DPosition.h:299
CoordinateType * Iterator
Mutable iterator.
Definition: DPosition.h:60
void clear()
Set all dimensions to zero.
Definition: DPosition.h:347
DPosition< D, TCoordinateType > operator/(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:442
const CoordinateType * ConstIterator
Non-mutable iterator.
Definition: DPosition.h:62
CoordinateType getY() const
Name accessor for the second dimension. Only for DPosition<2>, for visualization. ...
Definition: DPosition.h:157
const double c
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
void setY(CoordinateType c)
Name mutator for the second dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:171
DPosition & operator+=(const DPosition &point)
Addition.
Definition: DPosition.h:268
static Size size()
Returns the number of dimensions.
Definition: DPosition.h:341
static const DPosition minPositive()
smallest positive
Definition: DPosition.h:366
DPosition(const DPosition &pos)
Copy constructor.
Definition: DPosition.h:105
CoordinateType * pointer
Definition: DPosition.h:74
DPosition operator-(const DPosition &point) const
Subtraction (a bit inefficient)
Definition: DPosition.h:278
bool spatiallyLessEqual(const DPosition &point) const
Spatially (geometrically) less or equal operator. All coordinates must be "<=".
Definition: DPosition.h:225
CoordinateType getX() const
Name accessor for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:150
CoordinateType & operator[](Size index)
Accessor for the dimensions.
Definition: DPosition.h:143
bool spatiallyGreaterEqual(const DPosition &point) const
Spatially (geometrically) greater or equal operator. All coordinates must be ">=".
Definition: DPosition.h:235
CoordinateType * iterator
Definition: DPosition.h:75
CoordinateType coordinate_[D]
Definition: DPosition.h:414
const PrecisionWrapper< FloatingPointType > precisionWrapper(const FloatingPointType rhs)
Wrapper function that sets the appropriate precision for output temporarily. The original precision i...
Definition: PrecisionWrapper.h:96
void setX(CoordinateType c)
Name mutator for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:164
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:53
static const DPosition zero()
all zero
Definition: DPosition.h:360
DPosition< D, TCoordinateType > operator*(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:420
bool operator>=(const DPosition &point) const
Lexicographical greater or equal operator.
Definition: DPosition.h:251
~DPosition()
Destructor (not-virtual as this will save a lot of space!)
Definition: DPosition.h:94
DPosition & operator/=(CoordinateType scalar)
Scalar division.
Definition: DPosition.h:331
bool operator==(const DPosition &point) const
Equality operator.
Definition: DPosition.h:178
DPosition & operator-=(const DPosition &point)
Subtraction.
Definition: DPosition.h:289
DPosition()
Default constructor.
Definition: DPosition.h:88
CoordinateType value_type
Definition: DPosition.h:72
ConstIterator begin() const
Non-mutable begin iterator.
Definition: DPosition.h:388
static const DPosition maxPositive()
largest positive
Definition: DPosition.h:378
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
DPosition & operator=(const DPosition &source)
Assignment operator.
Definition: DPosition.h:120
Iterator end()
Mutable end iterator.
Definition: DPosition.h:406
static const DPosition minNegative()
smallest negative
Definition: DPosition.h:372
CoordinateType & reference
Definition: DPosition.h:73
bool operator<=(const DPosition &point) const
Lexicographical greater less or equal operator.
Definition: DPosition.h:213
DPosition(CoordinateType x)
Constructor that fills all dimensions with the value x.
Definition: DPosition.h:99
TCoordinateType CoordinateType
Coordinate type.
Definition: DPosition.h:58
bool operator>(const DPosition &point) const
Lexicographical greater than operator.
Definition: DPosition.h:245
CoordinateType operator[](Size index) const
Const accessor for the dimensions.
Definition: DPosition.h:136
ConstIterator end() const
Non-mutable end iterator.
Definition: DPosition.h:394