1
// -*- mode: C++; tab-width: 2; -*-
4
// --------------------------------------------------------------------------
5
// OpenMS Mass Spectrometry Framework
6
// --------------------------------------------------------------------------
7
// Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
9
// This library is free software; you can redistribute it and/or
10
// modify it under the terms of the GNU Lesser General Public
11
// License as published by the Free Software Foundation; either
12
// version 2.1 of the License, or (at your option) any later version.
14
// This library is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
// Lesser General Public License for more details.
19
// You should have received a copy of the GNU Lesser General Public
20
// License along with this library; if not, write to the Free Software
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
// --------------------------------------------------------------------------
24
// $Maintainer: Stephan Aiche$
25
// $Authors: Marc Sturm, Stephan Aiche $
26
// --------------------------------------------------------------------------
28
#ifndef OPENMS_DATASTRUCTURES_DPOSITION_H
29
#define OPENMS_DATASTRUCTURES_DPOSITION_H
31
#include <OpenMS/config.h>
32
#include <OpenMS/CONCEPT/Macros.h>
40
@brief Representation of a coordinate in D-dimensional space.
42
@ingroup Datastructures
44
template < UInt D, typename TCoordinateType = DoubleReal>
50
typedef TCoordinateType CoordinateType;
52
typedef CoordinateType* Iterator;
53
/// Non-mutable iterator
54
typedef const CoordinateType* ConstIterator;
61
@name STL compatibility type definitions
64
typedef CoordinateType value_type;
65
typedef CoordinateType& reference;
66
typedef CoordinateType* pointer;
67
typedef CoordinateType* iterator;
68
typedef const CoordinateType* const_iterator;
72
@name Constructors and Destructor
76
@brief Default constructor.
78
Creates a position with all coordinates zero.
85
/// Destructor (not-virtual as this will save a lot of space!)
90
/// Constructor that fills all dimensions with the value @p x
91
DPosition( CoordinateType x )
93
std::fill(&( coordinate_[0] ), &( coordinate_[D] ), x);
97
DPosition( const DPosition& pos )
99
std::copy(&( pos.coordinate_[0] ), &( pos.coordinate_[D] ),
100
&( coordinate_[0] ));
103
/// Constructor only for DPosition<2> that takes two Coordinates.
104
DPosition( CoordinateType x, CoordinateType y )
106
OPENMS_PRECONDITION(D == 2, "DPosition<D, TCoordinateType>:DPosition(x,y): index overflow!");
111
/// Assignment operator
112
DPosition& operator =( const DPosition& source )
114
if ( &source == this ) return *this;
116
std::copy(&( source.coordinate_[0] ), &( source.coordinate_[D] ),
117
&( coordinate_[0] ));
124
/** @name Accessors */
127
///Const accessor for the dimensions
128
CoordinateType operator []( Size index ) const
130
OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
131
return coordinate_[index];
134
///Accessor for the dimensions
135
CoordinateType& operator []( Size index )
137
OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
138
return coordinate_[index];
141
///Name accessor for the first dimension. Only for DPosition<2>, for visualization.
142
CoordinateType getX() const
144
OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getX(): index overflow!");
145
return coordinate_[0];
148
///Name accessor for the second dimension. Only for DPosition<2>, for visualization.
149
CoordinateType getY() const
151
OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getY(): index overflow!");
152
return coordinate_[1];
155
///Name mutator for the first dimension. Only for DPosition<2>, for visualization.
156
void setX( CoordinateType c )
158
OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setX(): index overflow!");
162
///Name mutator for the second dimension. Only for DPosition<2>, for visualization.
163
void setY( CoordinateType c )
165
OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setY(): index overflow!");
169
/// Equality operator
170
bool operator ==( const DPosition& point ) const
172
for ( Size i = 0; i < D; i++ )
174
if ( coordinate_[i] != point.coordinate_[i] ) return false;
179
/// Equality operator
180
bool operator !=( const DPosition& point ) const
182
return !( operator==(point) );
186
@brief Lexicographical less than operator.
187
Lexicographical comparison from dimension 0 to dimension D-1 is done.
189
bool operator <( const DPosition& point ) const
191
for ( Size i = 0; i < D; i++ )
193
if ( coordinate_[i] < point.coordinate_[i] ) return true;
194
if ( coordinate_[i] > point.coordinate_[i] ) return false;
199
/// Lexicographical greater less or equal operator.
200
bool operator <=( const DPosition& point ) const
202
for ( Size i = 0; i < D; i++ )
204
if ( coordinate_[i] < point.coordinate_[i] ) return true;
205
if ( coordinate_[i] > point.coordinate_[i] ) return false;
210
/// Spatially (geometrically) less or equal operator. All coordinates must be "<=".
211
bool spatiallyLessEqual( const DPosition& point ) const
213
for ( Size i = 0; i < D; i++ )
215
if ( coordinate_[i] > point.coordinate_[i] ) return false;
220
/// Spatially (geometrically) greater or equal operator. All coordinates must be ">=".
221
bool spatiallyGreaterEqual( const DPosition& point ) const
223
for ( Size i = 0; i < D; i++ )
225
if ( coordinate_[i] < point.coordinate_[i] ) return false;
230
/// Lexicographical greater than operator.
231
bool operator>( const DPosition& point ) const
233
return !( operator<=(point) );
236
/// Lexicographical greater or equal operator.
237
bool operator >=( const DPosition& point ) const
239
return !operator<(point);
242
/// Addition (a bit inefficient)
243
DPosition operator +( const DPosition& point ) const
245
DPosition result(*this);
246
for ( Size i = 0; i < D; result.coordinate_[i]
247
+= point.coordinate_[i], ++i )
253
DPosition& operator +=( const DPosition& point )
255
for ( Size i = 0; i < D; coordinate_[i] += point.coordinate_[i], ++i )
260
/// Subtraction (a bit inefficient)
261
DPosition operator -( const DPosition& point ) const
263
DPosition result(*this);
264
for ( Size i = 0; i < D; result.coordinate_[i]
265
-= point.coordinate_[i], ++i )
271
DPosition& operator -=( const DPosition& point )
273
for ( Size i = 0; i < D; coordinate_[i] -= point.coordinate_[i], ++i )
278
/// Negation (a bit inefficient)
279
DPosition operator -() const
281
DPosition<D,CoordinateType> result(*this);
282
for ( Size i = 0; i < D; result.coordinate_[i]
283
= -result.coordinate_[i], ++i )
289
CoordinateType operator *( const DPosition& point ) const
291
CoordinateType prod(0);
292
for ( Size i = 0; i < D; prod += ( point.coordinate_[i]
293
* coordinate_[i] ), ++i )
298
/// Scalar multiplication
299
DPosition& operator *=( CoordinateType scalar )
301
for ( Size i = 0; i < D; coordinate_[i] *= scalar, ++i )
307
DPosition& operator /=( CoordinateType scalar )
309
for ( Size i = 0; i < D; coordinate_[i] /= scalar, ++i )
314
/// Returns the number of dimensions
320
/// Set all dimensions to zero
323
for ( Size i = 0; i < D; coordinate_[i++] = (CoordinateType) 0 )
328
/** @name Static values */
331
inline static const DPosition zero()
335
/// smallest positive
336
inline static const DPosition minPositive()
338
return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::min)());
340
/// smallest negative
341
inline static const DPosition minNegative()
343
return DPosition(-(std::numeric_limits<typename DPosition::CoordinateType>::max)());
346
inline static const DPosition maxPositive()
348
return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::max)());
352
/** @name Iteration */
354
/// Non-mutable begin iterator
355
ConstIterator begin() const
357
return &( coordinate_[0] );
359
/// Non-mutable end iterator
360
ConstIterator end() const
362
return &( coordinate_[0] ) + D;
364
/// Mutable begin iterator
367
return &( coordinate_[0] );
369
/// Mutable end iterator
372
return &( coordinate_[0] ) + D;
377
CoordinateType coordinate_[D];
381
/// Scalar multiplication (a bit inefficient)
382
template < UInt D, typename TCoordinateType >
383
DPosition<D,TCoordinateType> operator *( DPosition<D,TCoordinateType> position, typename DPosition<D,TCoordinateType>::CoordinateType scalar )
385
for ( Size i = 0; i < D; position[i] *= scalar, ++i )
390
/// Scalar multiplication (a bit inefficient)
391
template < UInt D, typename TCoordinateType >
392
DPosition<D,TCoordinateType> operator *( typename DPosition<D,TCoordinateType>::CoordinateType scalar, DPosition<D,TCoordinateType> position )
394
for ( Size i = 0; i < D; position[i] *= scalar, ++i )
399
/// Scalar multiplication (a bit inefficient)
400
template < UInt D, typename TCoordinateType >
401
DPosition<D,TCoordinateType> operator /( DPosition<D,TCoordinateType> position, typename DPosition<D,TCoordinateType>::CoordinateType scalar )
403
for ( Size i = 0; i < D; position[i] /= scalar, ++i )
408
/// Print the contents to a stream.
409
template < UInt D, typename TCoordinateType >
410
std::ostream& operator <<( std::ostream& os, const DPosition<D,TCoordinateType>& pos )
412
os << precisionWrapper(pos[0]);
413
for ( UInt i = 1; i < D; ++i )
415
os << ' ' << precisionWrapper(pos[i]);
419
} // namespace OpenMS
421
#endif // OPENMS_DATASTRUCTURES_DPOSITION_H