Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
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-2017.
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 #ifndef OPENMS_DATASTRUCTURES_DPOSITION_H
36 #define OPENMS_DATASTRUCTURES_DPOSITION_H
37 
38 #include <OpenMS/CONCEPT/Macros.h>
39 #include <OpenMS/CONCEPT/Types.h>
41 
42 #include <algorithm>
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;
61  typedef CoordinateType* Iterator;
63  typedef const CoordinateType* ConstIterator;
65  enum
66  {
68  };
73  typedef CoordinateType value_type;
74  typedef CoordinateType& reference;
75  typedef CoordinateType* pointer;
76  typedef CoordinateType* iterator;
77  typedef const CoordinateType* const_iterator;
79 
90  {
91  clear();
92  }
93 
96  {
97  }
98 
100  DPosition(CoordinateType x)
101  {
102  std::fill(&(coordinate_[0]), &(coordinate_[D]), x);
103  }
104 
106  DPosition(const DPosition& pos)
107  {
108  std::copy(&(pos.coordinate_[0]), &(pos.coordinate_[D]),
109  &(coordinate_[0]));
110  }
111 
113  DPosition(CoordinateType x, CoordinateType y)
114  {
115  OPENMS_PRECONDITION(D == 2, "DPosition<D, TCoordinateType>:DPosition(x,y): index overflow!");
116  coordinate_[0] = x;
117  coordinate_[1] = y;
118  }
119 
121  DPosition& operator=(const DPosition& source)
122  {
123  if (&source == this) return *this;
124 
125  std::copy(&(source.coordinate_[0]), &(source.coordinate_[D]),
126  &(coordinate_[0]));
127 
128  return *this;
129  }
130 
132 
135 
137  CoordinateType operator[](Size index) const
138  {
139  OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
140  return coordinate_[index];
141  }
142 
144  CoordinateType& operator[](Size index)
145  {
146  OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
147  return coordinate_[index];
148  }
149 
151  CoordinateType getX() const
152  {
153  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getX(): index overflow!");
154  return coordinate_[0];
155  }
156 
158  CoordinateType getY() const
159  {
160  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getY(): index overflow!");
161  return coordinate_[1];
162  }
163 
165  void setX(CoordinateType c)
166  {
167  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setX(): index overflow!");
168  coordinate_[0] = c;
169  }
170 
172  void setY(CoordinateType c)
173  {
174  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setY(): index overflow!");
175  coordinate_[1] = c;
176  }
177 
179  bool operator==(const DPosition& point) const
180  {
181  for (Size i = 0; i < D; i++)
182  {
183 #pragma clang diagnostic push
184 #pragma clang diagnostic ignored "-Wfloat-equal"
185  if (coordinate_[i] != point.coordinate_[i]) return false;
186 
187 #pragma clang diagnostic pop
188  }
189  return true;
190  }
191 
193  bool operator!=(const DPosition& point) const
194  {
195  return !(operator==(point));
196  }
197 
202  bool operator<(const DPosition& point) const
203  {
204  for (Size i = 0; i < D; i++)
205  {
206  if (coordinate_[i] < point.coordinate_[i]) return true;
207 
208  if (coordinate_[i] > point.coordinate_[i]) return false;
209  }
210  return false;
211  }
212 
214  bool operator<=(const DPosition& point) const
215  {
216  for (Size i = 0; i < D; i++)
217  {
218  if (coordinate_[i] < point.coordinate_[i]) return true;
219 
220  if (coordinate_[i] > point.coordinate_[i]) return false;
221  }
222  return true;
223  }
224 
226  bool spatiallyLessEqual(const DPosition& point) const
227  {
228  for (Size i = 0; i < D; i++)
229  {
230  if (coordinate_[i] > point.coordinate_[i]) return false;
231  }
232  return true;
233  }
234 
236  bool spatiallyGreaterEqual(const DPosition& point) const
237  {
238  for (Size i = 0; i < D; i++)
239  {
240  if (coordinate_[i] < point.coordinate_[i]) return false;
241  }
242  return true;
243  }
244 
246  bool operator>(const DPosition& point) const
247  {
248  return !(operator<=(point));
249  }
250 
252  bool operator>=(const DPosition& point) const
253  {
254  return !operator<(point);
255  }
256 
258  DPosition operator+(const DPosition& point) const
259  {
260  DPosition result(*this);
261  for (Size i = 0; i < D; ++i)
262  {
263  result.coordinate_[i] += point.coordinate_[i];
264  }
265  return result;
266  }
267 
270  {
271  for (Size i = 0; i < D; ++i)
272  {
273  coordinate_[i] += point.coordinate_[i];
274  }
275  return *this;
276  }
277 
279  DPosition operator-(const DPosition& point) const
280  {
281  DPosition result(*this);
282  for (Size i = 0; i < D; ++i)
283  {
284  result.coordinate_[i] -= point.coordinate_[i];
285  }
286  return result;
287  }
288 
291  {
292  for (Size i = 0; i < D; ++i)
293  {
294  coordinate_[i] -= point.coordinate_[i];
295  }
296  return *this;
297  }
298 
301  {
302  DPosition<D, CoordinateType> result(*this);
303  for (Size i = 0; i < D; ++i)
304  {
305  result.coordinate_[i] = -result.coordinate_[i];
306  }
307  return result;
308  }
309 
311  CoordinateType operator*(const DPosition& point) const
312  {
313  CoordinateType prod(0);
314  for (Size i = 0; i < D; ++i)
315  {
316  prod += (point.coordinate_[i] * coordinate_[i]);
317  }
318  return prod;
319  }
320 
322  DPosition& operator*=(CoordinateType scalar)
323  {
324  for (Size i = 0; i < D; ++i)
325  {
326  coordinate_[i] *= scalar;
327  }
328  return *this;
329  }
330 
332  DPosition& operator/=(CoordinateType scalar)
333  {
334  for (Size i = 0; i < D; ++i)
335  {
336  coordinate_[i] /= scalar;
337  }
338  return *this;
339  }
340 
342  static Size size()
343  {
344  return D;
345  }
346 
348  void clear()
349  {
350  for (Size i = 0; i < D; ++i)
351  {
352  coordinate_[i] = static_cast<CoordinateType>(0);
353  }
354  }
355 
357 
360  inline static const DPosition zero()
362  {
363  return DPosition(0);
364  }
365 
367  inline static const DPosition minPositive()
368  {
369  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::min)());
370  }
371 
373  inline static const DPosition minNegative()
374  {
375  return DPosition(-(std::numeric_limits<typename DPosition::CoordinateType>::max)());
376  }
377 
379  inline static const DPosition maxPositive()
380  {
381  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::max)());
382  }
383 
385 
388  ConstIterator begin() const
390  {
391  return &(coordinate_[0]);
392  }
393 
395  ConstIterator end() const
396  {
397  return &(coordinate_[0]) + D;
398  }
399 
401  Iterator begin()
402  {
403  return &(coordinate_[0]);
404  }
405 
407  Iterator end()
408  {
409  return &(coordinate_[0]) + D;
410  }
411 
413 
414 protected:
415  CoordinateType coordinate_[D];
416 
417  }; // DPosition
418 
420  template <UInt D, typename TCoordinateType>
422  {
423  for (Size i = 0; i < D; ++i)
424  {
425  position[i] *= scalar;
426  }
427  return position;
428  }
429 
431  template <UInt D, typename TCoordinateType>
433  {
434  for (Size i = 0; i < D; ++i)
435  {
436  position[i] *= scalar;
437  }
438  return position;
439  }
440 
442  template <UInt D, typename TCoordinateType>
444  {
445  for (Size i = 0; i < D; ++i)
446  {
447  position[i] /= scalar;
448  }
449  return position;
450  }
451 
453  template <UInt D, typename TCoordinateType>
454  std::ostream& operator<<(std::ostream& os, const DPosition<D, TCoordinateType>& pos)
455  {
456  os << precisionWrapper(pos[0]);
457  for (UInt i = 1; i < D; ++i)
458  {
459  os << ' ' << precisionWrapper(pos[i]);
460  }
461  return os;
462  }
463 
464 } // namespace OpenMS
465 
466 #endif // OPENMS_DATASTRUCTURES_DPOSITION_H
const CoordinateType * const_iterator
Definition: DPosition.h:77
CoordinateType operator*(const DPosition &point) const
Inner product.
Definition: DPosition.h:311
DPosition operator+(const DPosition &point) const
Addition (a bit inefficient)
Definition: DPosition.h:258
bool operator!=(const DPosition &point) const
Equality operator.
Definition: DPosition.h:193
Iterator begin()
Mutable begin iterator.
Definition: DPosition.h:401
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:107
unsigned int UInt
Unsigned integer type.
Definition: Types.h:95
bool operator<(const DPosition &point) const
Lexicographical less than operator. Lexicographical comparison from dimension 0 to dimension D-1 is d...
Definition: DPosition.h:202
Definition: DPosition.h:67
DPosition(CoordinateType x, CoordinateType y)
Constructor only for DPosition<2> that takes two Coordinates.
Definition: DPosition.h:113
DPosition & operator*=(CoordinateType scalar)
Scalar multiplication.
Definition: DPosition.h:322
DPosition operator-() const
Negation (a bit inefficient)
Definition: DPosition.h:300
CoordinateType * Iterator
Mutable iterator.
Definition: DPosition.h:61
void clear()
Set all dimensions to zero.
Definition: DPosition.h:348
DPosition< D, TCoordinateType > operator/(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:443
const CoordinateType * ConstIterator
Non-mutable iterator.
Definition: DPosition.h:63
CoordinateType getY() const
Name accessor for the second dimension. Only for DPosition<2>, for visualization. ...
Definition: DPosition.h:158
const double c
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
void setY(CoordinateType c)
Name mutator for the second dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:172
DPosition & operator+=(const DPosition &point)
Addition.
Definition: DPosition.h:269
static Size size()
Returns the number of dimensions.
Definition: DPosition.h:342
static const DPosition minPositive()
smallest positive
Definition: DPosition.h:367
DPosition(const DPosition &pos)
Copy constructor.
Definition: DPosition.h:106
CoordinateType * pointer
Definition: DPosition.h:75
DPosition operator-(const DPosition &point) const
Subtraction (a bit inefficient)
Definition: DPosition.h:279
bool spatiallyLessEqual(const DPosition &point) const
Spatially (geometrically) less or equal operator. All coordinates must be "<=".
Definition: DPosition.h:226
CoordinateType getX() const
Name accessor for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:151
CoordinateType & operator[](Size index)
Accessor for the dimensions.
Definition: DPosition.h:144
bool spatiallyGreaterEqual(const DPosition &point) const
Spatially (geometrically) greater or equal operator. All coordinates must be ">=".
Definition: DPosition.h:236
CoordinateType * iterator
Definition: DPosition.h:76
CoordinateType coordinate_[D]
Definition: DPosition.h:415
const PrecisionWrapper< FloatingPointType > precisionWrapper(const FloatingPointType rhs)
Wrapper function that sets the appropriate precision for output temporarily. The original precision i...
Definition: PrecisionWrapper.h:97
void setX(CoordinateType c)
Name mutator for the first dimension. Only for DPosition<2>, for visualization.
Definition: DPosition.h:165
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:54
static const DPosition zero()
all zero
Definition: DPosition.h:361
bool operator>=(const DPosition &point) const
Lexicographical greater or equal operator.
Definition: DPosition.h:252
~DPosition()
Destructor (not-virtual as this will save a lot of space!)
Definition: DPosition.h:95
DPosition & operator/=(CoordinateType scalar)
Scalar division.
Definition: DPosition.h:332
bool operator==(const DPosition &point) const
Equality operator.
Definition: DPosition.h:179
DPosition & operator-=(const DPosition &point)
Subtraction.
Definition: DPosition.h:290
DPosition()
Default constructor.
Definition: DPosition.h:89
CoordinateType value_type
Definition: DPosition.h:73
ConstIterator begin() const
Non-mutable begin iterator.
Definition: DPosition.h:389
static const DPosition maxPositive()
largest positive
Definition: DPosition.h:379
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
DPosition & operator=(const DPosition &source)
Assignment operator.
Definition: DPosition.h:121
Iterator end()
Mutable end iterator.
Definition: DPosition.h:407
static const DPosition minNegative()
smallest negative
Definition: DPosition.h:373
CoordinateType & reference
Definition: DPosition.h:74
bool operator<=(const DPosition &point) const
Lexicographical greater less or equal operator.
Definition: DPosition.h:214
DPosition(CoordinateType x)
Constructor that fills all dimensions with the value x.
Definition: DPosition.h:100
TCoordinateType CoordinateType
Coordinate type.
Definition: DPosition.h:59
bool operator>(const DPosition &point) const
Lexicographical greater than operator.
Definition: DPosition.h:246
CoordinateType operator[](Size index) const
Const accessor for the dimensions.
Definition: DPosition.h:137
ConstIterator end() const
Non-mutable end iterator.
Definition: DPosition.h:395

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:21:59 using doxygen 1.8.13