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

« back to all changes in this revision

Viewing changes to include/OpenMS/KERNEL/RangeManager.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 $
 
26
// --------------------------------------------------------------------------
 
27
 
 
28
#ifndef OPENMS_KERNEL_RANGEMANAGER_H
 
29
#define OPENMS_KERNEL_RANGEMANAGER_H
 
30
 
 
31
#include <OpenMS/DATASTRUCTURES/DRange.h>
 
32
 
 
33
namespace OpenMS
 
34
{
 
35
        /**     
 
36
                @brief Handles the managment of a position and intensity range.
 
37
                
 
38
                This is needed for all peak and feature container like Spectrum, MSExperiment and FeatureMap.
 
39
        */
 
40
        template <UInt D>
 
41
        class RangeManager
 
42
        {
 
43
                public:
 
44
                        /// Dimension of the position range
 
45
                        enum { DIMENSION = D };
 
46
                        /// Position range type                 
 
47
                        typedef DRange<D> PositionRangeType;
 
48
                        /// Position Type
 
49
                        typedef DPosition<D> PositionType;
 
50
                        /// Intensity range type                        
 
51
                        typedef DRange<1> IntensityRangeType;
 
52
                        
 
53
                        /// Default constructor
 
54
                        RangeManager()
 
55
                                : int_range_(),
 
56
                                        pos_range_()
 
57
                        {
 
58
                        }
 
59
 
 
60
                        /// Copy constructor
 
61
                        RangeManager(const RangeManager& rhs)
 
62
                                : int_range_(rhs.int_range_),
 
63
                                        pos_range_(rhs.pos_range_)
 
64
                        {
 
65
                        }
 
66
                        
 
67
                        /// Destructor
 
68
                        virtual ~RangeManager()
 
69
                        {
 
70
                        }
 
71
        
 
72
                        /// Assignment operator
 
73
                        RangeManager& operator = (const RangeManager& rhs)
 
74
                        {
 
75
                                if (this==&rhs) return *this;
 
76
                                
 
77
                                int_range_ = rhs.int_range_;
 
78
                                pos_range_ = rhs.pos_range_;
 
79
                                
 
80
                                return *this;
 
81
                        }
 
82
        
 
83
                        /// Equality operator
 
84
                        bool operator == (const RangeManager& rhs) const
 
85
                        {
 
86
                                return
 
87
                                        int_range_ == rhs.int_range_ &&
 
88
                                        pos_range_ == rhs.pos_range_
 
89
                                        ;                               
 
90
                        }
 
91
                        
 
92
                        /// Equality operator
 
93
                        bool operator != (const RangeManager& rhs) const
 
94
                        {
 
95
                                return !(operator==(rhs));
 
96
                        }
 
97
 
 
98
                        
 
99
                        /**     
 
100
                                @name Range methods
 
101
                        
 
102
                                @note The range values are not updated automatically. Call updateRanges() to update the values!
 
103
                        */
 
104
                        //@{
 
105
        
 
106
                        /// Returns the minimum position
 
107
                        const PositionType& getMin() const      
 
108
                        { 
 
109
                                return pos_range_.minPosition(); 
 
110
                        }
 
111
                        /// Returns the maximum position
 
112
                  const PositionType& getMax() const 
 
113
                  { 
 
114
                        return pos_range_.maxPosition(); 
 
115
                  }
 
116
        
 
117
                        /// Returns the minimum intensity
 
118
                        DoubleReal getMinInt() const    
 
119
                        { 
 
120
                                return int_range_.minPosition()[0]; 
 
121
                        }
 
122
                        /// Returns the maximum intensity
 
123
                  DoubleReal getMaxInt() const 
 
124
                  { 
 
125
                        return int_range_.maxPosition()[0]; 
 
126
                  }
 
127
                        
 
128
                        /**
 
129
                                @brief Updates minimum and maximum position/intensity.
 
130
                                
 
131
                                This method is usually implemented by calling clearRanges() and
 
132
                                updateRanges_().
 
133
                        */
 
134
                        virtual void updateRanges() = 0;
 
135
 
 
136
                        /// Resets the ranges
 
137
                        void clearRanges()
 
138
                        {
 
139
                                int_range_ = IntensityRangeType::empty;
 
140
                                pos_range_ = PositionRangeType::empty;
 
141
                        }
 
142
 
 
143
                        //@}
 
144
 
 
145
                protected:
 
146
                        /// Intensity range (1-dimensional)
 
147
                        IntensityRangeType int_range_;
 
148
                        /// Position range (D-dimensional)
 
149
                        PositionRangeType pos_range_;
 
150
                        
 
151
                        /// Updates the range using data points in the iterator range. 
 
152
                        template <class PeakIteratorType>
 
153
                        void updateRanges_(const PeakIteratorType& begin, const PeakIteratorType& end)
 
154
                        {
 
155
                                
 
156
                                //prevent invalid range by empty container
 
157
                                if (begin==end)
 
158
                                {
 
159
                                        return;
 
160
                                }
 
161
                                
 
162
                                PositionType min = pos_range_.minPosition();
 
163
                                PositionType max = pos_range_.maxPosition();
 
164
                                
 
165
                                DoubleReal it_min = int_range_.minPosition()[0];
 
166
                                DoubleReal it_max = int_range_.maxPosition()[0];
 
167
                                
 
168
                                for (PeakIteratorType it = begin; it != end; ++it)
 
169
                                {
 
170
                                        //update position
 
171
                                        for (UInt i = 0; i < D; ++i)
 
172
                                        {
 
173
                                                DoubleReal tmp = it->getPosition()[i];
 
174
                                                if (tmp < min[i])
 
175
                                                {
 
176
                                                        min[i] = tmp;
 
177
                                                }
 
178
                                                if (tmp > max[i])
 
179
                                                {
 
180
                                                        max[i] = tmp;
 
181
                                                }
 
182
                                        }
 
183
                                
 
184
                                        //update intensity
 
185
                                        DoubleReal tmp = it->getIntensity();
 
186
                                        if (tmp < it_min)
 
187
                                        {
 
188
                                                it_min = tmp;
 
189
                                        }
 
190
                                        if (tmp > it_max)
 
191
                                        {
 
192
                                          it_max = tmp;
 
193
                                        }
 
194
                                }
 
195
                                
 
196
                                pos_range_.setMin(min);
 
197
                                pos_range_.setMax(max);
 
198
                                
 
199
                                int_range_.setMinX(it_min);
 
200
                                int_range_.setMaxX(it_max);
 
201
      }
 
202
      
 
203
        };  // class
 
204
  
 
205
}  // namespace OpenMS
 
206
 
 
207
#endif  // OPENMS_KERNEL_DRANGE_H