~ubuntu-branches/ubuntu/wily/openms/wily

« back to all changes in this revision

Viewing changes to include/OpenMS/DATASTRUCTURES/DPosition.h

  • Committer: Package Import Robot
  • Author(s): Filippo Rusconi
  • Date: 2012-11-12 15:58:12 UTC
  • Revision ID: package-import@ubuntu.com-20121112155812-vr15wtg9b50cuesg
Tags: upstream-1.9.0
ImportĀ upstreamĀ versionĀ 1.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode: C++; tab-width: 2; -*-
 
2
// vi: set ts=2:
 
3
//
 
4
// --------------------------------------------------------------------------
 
5
//                   OpenMS Mass Spectrometry Framework
 
6
// --------------------------------------------------------------------------
 
7
//  Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
 
8
//
 
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.
 
13
//
 
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.
 
18
//
 
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
 
22
//
 
23
// --------------------------------------------------------------------------
 
24
// $Maintainer: Stephan Aiche$
 
25
// $Authors: Marc Sturm, Stephan Aiche $
 
26
// --------------------------------------------------------------------------
 
27
 
 
28
#ifndef OPENMS_DATASTRUCTURES_DPOSITION_H
 
29
#define OPENMS_DATASTRUCTURES_DPOSITION_H
 
30
 
 
31
#include <OpenMS/config.h>
 
32
#include <OpenMS/CONCEPT/Macros.h>
 
33
 
 
34
#include <algorithm>
 
35
#include <limits>
 
36
 
 
37
namespace OpenMS
 
38
{
 
39
        /**
 
40
   @brief Representation of a coordinate in D-dimensional space.
 
41
 
 
42
   @ingroup Datastructures
 
43
   */
 
44
        template < UInt D, typename TCoordinateType = DoubleReal>
 
45
        class DPosition
 
46
        {
 
47
        public:
 
48
 
 
49
                /// Coordinate type
 
50
                typedef TCoordinateType CoordinateType;
 
51
                /// Mutable iterator
 
52
                typedef CoordinateType* Iterator;
 
53
                /// Non-mutable iterator
 
54
                typedef const CoordinateType* ConstIterator;
 
55
                /// Dimensions
 
56
                enum
 
57
                {
 
58
                        DIMENSION = D
 
59
                };
 
60
                /**
 
61
                                         @name STL compatibility type definitions
 
62
                                         */
 
63
                //@{
 
64
                typedef CoordinateType value_type;
 
65
                typedef CoordinateType& reference;
 
66
                typedef CoordinateType* pointer;
 
67
                typedef CoordinateType* iterator;
 
68
                typedef const CoordinateType* const_iterator;
 
69
                //@}
 
70
 
 
71
                /**
 
72
                                         @name Constructors and Destructor
 
73
                                         */
 
74
                //@{
 
75
                /**
 
76
                                         @brief Default constructor.
 
77
 
 
78
                                         Creates a position with all coordinates zero.
 
79
                                         */
 
80
                DPosition()
 
81
                {
 
82
                        clear();
 
83
                }
 
84
 
 
85
                /// Destructor (not-virtual as this will save a lot of space!)
 
86
                ~DPosition()
 
87
                {
 
88
                }
 
89
 
 
90
                /// Constructor that fills all dimensions with the value @p x
 
91
                DPosition( CoordinateType x )
 
92
                {
 
93
                        std::fill(&( coordinate_[0] ), &( coordinate_[D] ), x);
 
94
                }
 
95
 
 
96
                /// Copy constructor
 
97
                DPosition( const DPosition& pos )
 
98
                {
 
99
                        std::copy(&( pos.coordinate_[0] ), &( pos.coordinate_[D] ),
 
100
                                                                &( coordinate_[0] ));
 
101
                }
 
102
 
 
103
                /// Constructor only for DPosition<2> that takes two Coordinates.
 
104
                DPosition( CoordinateType x, CoordinateType y )
 
105
                {
 
106
      OPENMS_PRECONDITION(D == 2, "DPosition<D, TCoordinateType>:DPosition(x,y): index overflow!");
 
107
                        coordinate_[0] = x;
 
108
                        coordinate_[1] = y;
 
109
                }
 
110
 
 
111
                /// Assignment operator
 
112
    DPosition& operator =( const DPosition& source )
 
113
                {
 
114
                        if ( &source == this ) return *this;
 
115
 
 
116
                        std::copy(&( source.coordinate_[0] ), &( source.coordinate_[D] ),
 
117
                                                                &( coordinate_[0] ));
 
118
 
 
119
                        return *this;
 
120
                }
 
121
 
 
122
                //@}
 
123
 
 
124
                /**     @name Accessors */
 
125
                //@{
 
126
 
 
127
                ///Const accessor for the dimensions
 
128
    CoordinateType operator []( Size index ) const
 
129
                {
 
130
      OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
 
131
                        return coordinate_[index];
 
132
                }
 
133
 
 
134
                ///Accessor for the dimensions
 
135
    CoordinateType& operator []( Size index )
 
136
                {
 
137
      OPENMS_PRECONDITION(index < D, "DPosition<D,TCoordinateType>:operator [] (Position): index overflow!");
 
138
                        return coordinate_[index];
 
139
                }
 
140
 
 
141
                ///Name accessor for the first dimension. Only for DPosition<2>, for visualization.
 
142
    CoordinateType getX() const
 
143
                {
 
144
      OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getX(): index overflow!");
 
145
                        return coordinate_[0];
 
146
                }
 
147
 
 
148
                ///Name accessor for the second dimension. Only for DPosition<2>, for visualization.
 
149
    CoordinateType getY() const
 
150
                {
 
151
      OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:getY(): index overflow!");
 
152
                        return coordinate_[1];
 
153
                }
 
154
 
 
155
                ///Name mutator for the first dimension. Only for DPosition<2>, for visualization.
 
156
    void setX( CoordinateType c )
 
157
                {
 
158
      OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setX(): index overflow!");
 
159
                        coordinate_[0] = c;
 
160
                }
 
161
 
 
162
                ///Name mutator for the second dimension. Only for DPosition<2>, for visualization.
 
163
    void setY( CoordinateType c )
 
164
                {
 
165
      OPENMS_PRECONDITION(D == 2, "DPosition<D,TCoordinateType>:setY(): index overflow!");
 
166
                        coordinate_[1] = c;
 
167
                }
 
168
 
 
169
                /// Equality operator
 
170
    bool operator ==( const DPosition& point ) const
 
171
                {
 
172
                        for ( Size i = 0; i < D; i++ )
 
173
                        {
 
174
                                if ( coordinate_[i] != point.coordinate_[i] ) return false;
 
175
                        }
 
176
                        return true;
 
177
                }
 
178
 
 
179
                /// Equality operator
 
180
    bool operator !=( const DPosition& point ) const
 
181
                {
 
182
                        return !( operator==(point) );
 
183
                }
 
184
 
 
185
                /**
 
186
      @brief Lexicographical less than operator.
 
187
      Lexicographical comparison from dimension 0 to dimension D-1 is done.
 
188
    */
 
189
    bool operator <( const DPosition& point ) const
 
190
                {
 
191
                        for ( Size i = 0; i < D; i++ )
 
192
                        {
 
193
                                if ( coordinate_[i] < point.coordinate_[i] ) return true;
 
194
                                if ( coordinate_[i] > point.coordinate_[i] ) return false;
 
195
      }
 
196
                        return false;
 
197
                }
 
198
 
 
199
                /// Lexicographical greater less or equal operator.
 
200
    bool operator <=( const DPosition& point ) const
 
201
                {
 
202
                        for ( Size i = 0; i < D; i++ )
 
203
                        {
 
204
                                if ( coordinate_[i] < point.coordinate_[i] ) return true;
 
205
                                if ( coordinate_[i] > point.coordinate_[i] ) return false;
 
206
                        }
 
207
                        return true;
 
208
                }
 
209
 
 
210
                /// Spatially (geometrically) less or equal operator.    All coordinates must be "<=".
 
211
    bool spatiallyLessEqual( const DPosition& point ) const
 
212
                {
 
213
                        for ( Size i = 0; i < D; i++ )
 
214
                        {
 
215
                                if ( coordinate_[i] > point.coordinate_[i] ) return false;
 
216
                        }
 
217
                        return true;
 
218
                }
 
219
 
 
220
                /// Spatially (geometrically) greater or equal operator. All coordinates must be ">=".
 
221
    bool spatiallyGreaterEqual( const DPosition& point ) const
 
222
                {
 
223
                        for ( Size i = 0; i < D; i++ )
 
224
                        {
 
225
                                if ( coordinate_[i] < point.coordinate_[i] ) return false;
 
226
                        }
 
227
                        return true;
 
228
                }
 
229
 
 
230
                /// Lexicographical greater than operator.
 
231
    bool operator>( const DPosition& point ) const
 
232
                {
 
233
                        return !( operator<=(point) );
 
234
                }
 
235
 
 
236
                /// Lexicographical greater or equal operator.
 
237
    bool operator >=( const DPosition& point ) const
 
238
                {
 
239
                        return !operator<(point);
 
240
                }
 
241
 
 
242
                /// Addition (a bit inefficient)
 
243
    DPosition operator +( const DPosition& point ) const
 
244
                {
 
245
                        DPosition result(*this);
 
246
                        for ( Size i = 0; i < D; result.coordinate_[i]
 
247
                                                += point.coordinate_[i], ++i )
 
248
                                ;
 
249
                        return result;
 
250
                }
 
251
 
 
252
                /// Addition
 
253
    DPosition&  operator +=( const DPosition& point )
 
254
                {
 
255
                        for ( Size i = 0; i < D; coordinate_[i] += point.coordinate_[i], ++i )
 
256
                                ;
 
257
                        return *this;
 
258
                }
 
259
 
 
260
                /// Subtraction (a bit inefficient)
 
261
    DPosition operator -( const DPosition& point ) const
 
262
                {
 
263
                        DPosition result(*this);
 
264
                        for ( Size i = 0; i < D; result.coordinate_[i]
 
265
                                                -= point.coordinate_[i], ++i )
 
266
                                ;
 
267
                        return result;
 
268
                }
 
269
 
 
270
                /// Subtraction
 
271
    DPosition& operator -=( const DPosition& point )
 
272
                {
 
273
                        for ( Size i = 0; i < D; coordinate_[i] -= point.coordinate_[i], ++i )
 
274
                                ;
 
275
                        return *this;
 
276
                }
 
277
 
 
278
                /// Negation (a bit inefficient)
 
279
    DPosition   operator -() const
 
280
                {
 
281
      DPosition<D,CoordinateType> result(*this);
 
282
                        for ( Size i = 0; i < D; result.coordinate_[i]
 
283
                                                = -result.coordinate_[i], ++i )
 
284
                                ;
 
285
                        return result;
 
286
                }
 
287
 
 
288
                /// Inner product
 
289
    CoordinateType operator *( const DPosition& point ) const
 
290
                {
 
291
                        CoordinateType prod(0);
 
292
                        for ( Size i = 0; i < D; prod += ( point.coordinate_[i]
 
293
                                                                                                                                                                 * coordinate_[i] ), ++i )
 
294
                                ;
 
295
                        return prod;
 
296
                }
 
297
 
 
298
                /// Scalar multiplication
 
299
    DPosition& operator *=( CoordinateType scalar )
 
300
                {
 
301
                        for ( Size i = 0; i < D; coordinate_[i] *= scalar, ++i )
 
302
                                ;
 
303
                        return *this;
 
304
                }
 
305
 
 
306
                /// Scalar division
 
307
    DPosition& operator /=( CoordinateType scalar )
 
308
                {
 
309
                        for ( Size i = 0; i < D; coordinate_[i] /= scalar, ++i )
 
310
                                ;
 
311
                        return *this;
 
312
                }
 
313
 
 
314
                /// Returns the number of dimensions
 
315
    static Size size()
 
316
                {
 
317
                        return D;
 
318
                }
 
319
 
 
320
                /// Set all dimensions to zero
 
321
    void clear()
 
322
                {
 
323
                        for ( Size i = 0; i < D; coordinate_[i++] = (CoordinateType) 0 )
 
324
                                ;
 
325
                }
 
326
                //@}
 
327
 
 
328
                /**     @name Static values */
 
329
                //@{
 
330
                /// all zero
 
331
    inline static const DPosition zero()
 
332
                {
 
333
                        return DPosition(0);
 
334
                }
 
335
                /// smallest positive
 
336
    inline static const DPosition       minPositive()
 
337
    {
 
338
                        return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::min)());
 
339
                }
 
340
                /// smallest negative
 
341
    inline static const DPosition minNegative()
 
342
                {
 
343
                        return DPosition(-(std::numeric_limits<typename DPosition::CoordinateType>::max)());
 
344
                }
 
345
                /// largest positive
 
346
    inline static const DPosition maxPositive()
 
347
                {
 
348
                        return DPosition((std::numeric_limits<typename DPosition::CoordinateType>::max)());
 
349
                }
 
350
                //@}
 
351
 
 
352
                /**     @name Iteration */
 
353
                //@{
 
354
                /// Non-mutable begin iterator
 
355
    ConstIterator       begin() const
 
356
                {
 
357
                        return &( coordinate_[0] );
 
358
                }
 
359
                /// Non-mutable end iterator
 
360
    ConstIterator end() const
 
361
                {
 
362
                        return &( coordinate_[0] ) + D;
 
363
                }
 
364
                /// Mutable begin iterator
 
365
    Iterator begin()
 
366
                {
 
367
                        return &( coordinate_[0] );
 
368
                }
 
369
                /// Mutable end iterator
 
370
    Iterator end()
 
371
                {
 
372
                        return &( coordinate_[0] ) + D;
 
373
                }
 
374
                //@}
 
375
 
 
376
        protected:
 
377
                CoordinateType coordinate_[D];
 
378
 
 
379
        }; // DPosition
 
380
 
 
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 )
 
384
  {
 
385
    for ( Size i = 0; i < D; position[i] *= scalar, ++i )
 
386
      ;
 
387
    return position;
 
388
  }
 
389
 
 
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 )
 
393
  {
 
394
    for ( Size i = 0; i < D; position[i] *= scalar, ++i )
 
395
      ;
 
396
    return position;
 
397
  }
 
398
 
 
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 )
 
402
  {
 
403
    for ( Size i = 0; i < D; position[i] /= scalar, ++i )
 
404
      ;
 
405
    return position;
 
406
  }
 
407
 
 
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 )
 
411
  {
 
412
    os << precisionWrapper(pos[0]);
 
413
    for ( UInt i = 1; i < D; ++i )
 
414
    {
 
415
      os << ' ' << precisionWrapper(pos[i]);
 
416
    }
 
417
    return os;
 
418
  }
 
419
} // namespace OpenMS
 
420
 
 
421
#endif // OPENMS_DATASTRUCTURES_DPOSITION_H