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-2013.
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: Stephan Aiche$
32 // $Authors: Marc Sturm, Stephan Aiche $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_DATASTRUCTURES_DPOSITION_H
36 #define OPENMS_DATASTRUCTURES_DPOSITION_H
37 
38 #include <OpenMS/config.h>
39 #include <OpenMS/CONCEPT/Macros.h>
40 
41 #include <algorithm>
42 #include <limits>
43 
44 namespace OpenMS
45 {
51  template <UInt D, typename TCoordinateType = DoubleReal>
52  class DPosition
53  {
54 public:
55 
57  typedef TCoordinateType CoordinateType;
61  typedef const CoordinateType * ConstIterator;
63  enum
64  {
66  };
75  typedef const CoordinateType * const_iterator;
77 
88  {
89  clear();
90  }
91 
94  {
95  }
96 
99  {
100  std::fill(&(coordinate_[0]), &(coordinate_[D]), x);
101  }
102 
104  DPosition(const DPosition & pos)
105  {
106  std::copy(&(pos.coordinate_[0]), &(pos.coordinate_[D]),
107  &(coordinate_[0]));
108  }
109 
112  {
113  OPENMS_PRECONDITION(D == 2, "DPosition<D, TCoordinateType>:DPosition(x,y): index overflow!");
114  coordinate_[0] = x;
115  coordinate_[1] = y;
116  }
117 
119  DPosition & operator=(const DPosition & source)
120  {
121  if (&source == this) return *this;
122 
123  std::copy(&(source.coordinate_[0]), &(source.coordinate_[D]),
124  &(coordinate_[0]));
125 
126  return *this;
127  }
128 
130 
133 
136  {
137  OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
138  return coordinate_[index];
139  }
140 
143  {
144  OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
145  return coordinate_[index];
146  }
147 
150  {
151  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getX(): index overflow!");
152  return coordinate_[0];
153  }
154 
157  {
158  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getY(): index overflow!");
159  return coordinate_[1];
160  }
161 
164  {
165  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setX(): index overflow!");
166  coordinate_[0] = c;
167  }
168 
171  {
172  OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setY(): index overflow!");
173  coordinate_[1] = c;
174  }
175 
177  bool operator==(const DPosition & point) const
178  {
179  for (Size i = 0; i < D; i++)
180  {
181  if (coordinate_[i] != point.coordinate_[i]) return false;
182  }
183  return true;
184  }
185 
187  bool operator!=(const DPosition & point) const
188  {
189  return !(operator==(point));
190  }
191 
196  bool operator<(const DPosition & point) const
197  {
198  for (Size i = 0; i < D; i++)
199  {
200  if (coordinate_[i] < point.coordinate_[i]) return true;
201 
202  if (coordinate_[i] > point.coordinate_[i]) return false;
203  }
204  return false;
205  }
206 
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 true;
217  }
218 
220  bool spatiallyLessEqual(const DPosition & point) const
221  {
222  for (Size i = 0; i < D; i++)
223  {
224  if (coordinate_[i] > point.coordinate_[i]) return false;
225  }
226  return true;
227  }
228 
230  bool spatiallyGreaterEqual(const DPosition & point) const
231  {
232  for (Size i = 0; i < D; i++)
233  {
234  if (coordinate_[i] < point.coordinate_[i]) return false;
235  }
236  return true;
237  }
238 
240  bool operator>(const DPosition & point) const
241  {
242  return !(operator<=(point));
243  }
244 
246  bool operator>=(const DPosition & point) const
247  {
248  return !operator<(point);
249  }
250 
252  DPosition operator+(const DPosition & point) const
253  {
254  DPosition result(*this);
255  for (Size i = 0; i < D; ++i)
256  {
257  result.coordinate_[i] += point.coordinate_[i];
258  }
259  return result;
260  }
261 
263  DPosition & operator+=(const DPosition & point)
264  {
265  for (Size i = 0; i < D; ++i)
266  {
267  coordinate_[i] += point.coordinate_[i];
268  }
269  return *this;
270  }
271 
273  DPosition operator-(const DPosition & point) const
274  {
275  DPosition result(*this);
276  for (Size i = 0; i < D; ++i)
277  {
278  result.coordinate_[i] -= point.coordinate_[i];
279  }
280  return result;
281  }
282 
284  DPosition & operator-=(const DPosition & point)
285  {
286  for (Size i = 0; i < D; ++i)
287  {
288  coordinate_[i] -= point.coordinate_[i];
289  }
290  return *this;
291  }
292 
295  {
296  DPosition<D, CoordinateType> result(*this);
297  for (Size i = 0; i < D; ++i)
298  {
299  result.coordinate_[i] = -result.coordinate_[i];
300  }
301  return result;
302  }
303 
305  CoordinateType operator*(const DPosition & point) const
306  {
307  CoordinateType prod(0);
308  for (Size i = 0; i < D; ++i)
309  {
310  prod += (point.coordinate_[i] * coordinate_[i]);
311  }
312  return prod;
313  }
314 
317  {
318  for (Size i = 0; i < D; ++i)
319  {
320  coordinate_[i] *= scalar;
321  }
322  return *this;
323  }
324 
327  {
328  for (Size i = 0; i < D; ++i)
329  {
330  coordinate_[i] /= scalar;
331  }
332  return *this;
333  }
334 
336  static Size size()
337  {
338  return D;
339  }
340 
342  void clear()
343  {
344  for (Size i = 0; i < D; ++i)
345  {
346  coordinate_[i] = (CoordinateType) 0;
347  }
348  }
349 
351 
354  inline static const DPosition zero()
356  {
357  return DPosition(0);
358  }
359 
361  inline static const DPosition minPositive()
362  {
363  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::min)());
364  }
365 
367  inline static const DPosition minNegative()
368  {
369  return DPosition(-(std::numeric_limits<typename DPosition::CoordinateType>::max)());
370  }
371 
373  inline static const DPosition maxPositive()
374  {
375  return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::max)());
376  }
377 
379 
382  ConstIterator begin() const
384  {
385  return &(coordinate_[0]);
386  }
387 
390  {
391  return &(coordinate_[0]) + D;
392  }
393 
396  {
397  return &(coordinate_[0]);
398  }
399 
402  {
403  return &(coordinate_[0]) + D;
404  }
405 
407 
408 protected:
410 
411  }; // DPosition
412 
414  template <UInt D, typename TCoordinateType>
416  {
417  for (Size i = 0; i < D; ++i)
418  {
419  position[i] *= scalar;
420  }
421  return position;
422  }
423 
425  template <UInt D, typename TCoordinateType>
427  {
428  for (Size i = 0; i < D; ++i)
429  {
430  position[i] *= scalar;
431  }
432  return position;
433  }
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>
448  std::ostream & operator<<(std::ostream & os, const DPosition<D, TCoordinateType> & pos)
449  {
450  os << precisionWrapper(pos[0]);
451  for (UInt i = 1; i < D; ++i)
452  {
453  os << ' ' << precisionWrapper(pos[i]);
454  }
455  return os;
456  }
457 
458 } // namespace OpenMS
459 
460 #endif // OPENMS_DATASTRUCTURES_DPOSITION_H
CoordinateType operator[](Size index) const
Const accessor for the dimensions.
Definition: DPosition.h:135
bool operator==(const DPosition &point) const
Equality operator.
Definition: DPosition.h:177
CoordinateType operator*(const DPosition &point) const
Inner product.
Definition: DPosition.h:305
bool operator>(const DPosition &point) const
Lexicographical greater than operator.
Definition: DPosition.h:240
const CoordinateType * const_iterator
Definition: DPosition.h:75
Iterator begin()
Mutable begin iterator.
Definition: DPosition.h:395
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: Macros.h:107
DPosition(CoordinateType x, CoordinateType y)
Constructor only for DPosition&lt;2&gt; that takes two Coordinates.
Definition: DPosition.h:111
bool operator<(const DPosition &point) const
Lexicographical less than operator. Lexicographical comparison from dimension 0 to dimension D-1 is d...
Definition: DPosition.h:196
DPosition & operator*=(CoordinateType scalar)
Scalar multiplication.
Definition: DPosition.h:316
CoordinateType * Iterator
Mutable iterator.
Definition: DPosition.h:59
void clear()
Set all dimensions to zero.
Definition: DPosition.h:342
DPosition< D, TCoordinateType > operator/(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:437
DPosition operator-(const DPosition &point) const
Subtraction (a bit inefficient)
Definition: DPosition.h:273
const CoordinateType * ConstIterator
Non-mutable iterator.
Definition: DPosition.h:61
const double c
DPosition operator+(const DPosition &point) const
Addition (a bit inefficient)
Definition: DPosition.h:252
void setY(CoordinateType c)
Name mutator for the second dimension. Only for DPosition&lt;2&gt;, for visualization.
Definition: DPosition.h:170
DPosition & operator+=(const DPosition &point)
Addition.
Definition: DPosition.h:263
static Size size()
Returns the number of dimensions.
Definition: DPosition.h:336
static const DPosition minPositive()
smallest positive
Definition: DPosition.h:361
bool spatiallyLessEqual(const DPosition &point) const
Spatially (geometrically) less or equal operator. All coordinates must be &quot;&lt;=&quot;.
Definition: DPosition.h:220
DPosition(const DPosition &pos)
Copy constructor.
Definition: DPosition.h:104
CoordinateType * pointer
Definition: DPosition.h:73
bool spatiallyGreaterEqual(const DPosition &point) const
Spatially (geometrically) greater or equal operator. All coordinates must be &quot;&gt;=&quot;.
Definition: DPosition.h:230
CoordinateType & operator[](Size index)
Accessor for the dimensions.
Definition: DPosition.h:142
ConstIterator end() const
Non-mutable end iterator.
Definition: DPosition.h:389
CoordinateType * iterator
Definition: DPosition.h:74
CoordinateType coordinate_[D]
Definition: DPosition.h:409
const PrecisionWrapper< FloatingPointType > precisionWrapper(const FloatingPointType rhs)
Wrapper function that sets the appropriate precision for output temporarily. The original precision i...
Definition: Types.h:358
void setX(CoordinateType c)
Name mutator for the first dimension. Only for DPosition&lt;2&gt;, for visualization.
Definition: DPosition.h:163
Representation of a coordinate in D-dimensional space.
Definition: DPosition.h:52
static const DPosition zero()
all zero
Definition: DPosition.h:355
bool operator<=(const DPosition &point) const
Lexicographical greater less or equal operator.
Definition: DPosition.h:208
DPosition< D, TCoordinateType > operator*(DPosition< D, TCoordinateType > position, typename DPosition< D, TCoordinateType >::CoordinateType scalar)
Scalar multiplication (a bit inefficient)
Definition: DPosition.h:415
bool operator!=(const DPosition &point) const
Equality operator.
Definition: DPosition.h:187
~DPosition()
Destructor (not-virtual as this will save a lot of space!)
Definition: DPosition.h:93
DPosition & operator/=(CoordinateType scalar)
Scalar division.
Definition: DPosition.h:326
DPosition & operator-=(const DPosition &point)
Subtraction.
Definition: DPosition.h:284
DPosition()
Default constructor.
Definition: DPosition.h:87
CoordinateType value_type
Definition: DPosition.h:71
static const DPosition maxPositive()
largest positive
Definition: DPosition.h:373
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:144
Definition: DPosition.h:65
DPosition & operator=(const DPosition &source)
Assignment operator.
Definition: DPosition.h:119
Iterator end()
Mutable end iterator.
Definition: DPosition.h:401
DPosition operator-() const
Negation (a bit inefficient)
Definition: DPosition.h:294
static const DPosition minNegative()
smallest negative
Definition: DPosition.h:367
CoordinateType & reference
Definition: DPosition.h:72
CoordinateType getY() const
Name accessor for the second dimension. Only for DPosition&lt;2&gt;, for visualization. ...
Definition: DPosition.h:156
DPosition(CoordinateType x)
Constructor that fills all dimensions with the value x.
Definition: DPosition.h:98
TCoordinateType CoordinateType
Coordinate type.
Definition: DPosition.h:57
ConstIterator begin() const
Non-mutable begin iterator.
Definition: DPosition.h:383
bool operator>=(const DPosition &point) const
Lexicographical greater or equal operator.
Definition: DPosition.h:246
CoordinateType getX() const
Name accessor for the first dimension. Only for DPosition&lt;2&gt;, for visualization.
Definition: DPosition.h:149

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:12 using doxygen 1.8.5