Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
OverlappingSpectrum.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "OverlappingSpectrum.h"
14 
15 #include "DataGroupNode.h"
16 #include "Sample.h"
17 #include "SampleSet.h"
18 
19 #include <math.h>
20 
21 
22 using namespace kome::objects;
23 
24 
25 #include <crtdbg.h>
26 #ifdef _DEBUG
27  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
28  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
29 #endif // _DEBUG
30 
31 
32 
33 // constructor
35 
36  m_spectra.clear( );
37  m_dMinX.clear( );
38  m_dMaxX.clear( );
39  m_dMinY.clear( );
40  m_dMaxY.clear( );
41 
42  m_bFlags = NULL;
43  m_dInts = NULL;
44  m_nAllocLen = 0;
45 }
46 
47 // destructor
49  if ( m_bFlags )
50  {
51  delete[] m_bFlags;
52  m_bFlags = NULL;
53  }
54  if ( m_dInts )
55  {
56  delete[] m_dInts;
57  m_dInts = NULL;
58  }
59  m_nAllocLen = 0;
60 }
61 
62 // add spectrum
64  // check the spectrum
65  if( spec == NULL || searchSpectrum( spec ) >= 0 ) {
66  return;
67  }
68 
69  // add
70  m_spectra.push_back( spec );
71 
72  // set group
73  if( m_spectra.size() == 1 ) {
74  setGroup( m_spectra[ 0 ]->getGroup() );
75  }
76  else {
77  setGroup( NULL );
78  }
79 
80  // update range
81  double minX = double();
82  double maxX = double();
83 
84  onGetXRange( &minX, &maxX );
85  setXRange( minX, maxX );
86 }
87 
88 // remove spectrum
90  // search
91  int idx = searchSpectrum( spec );
92  if( idx < 0 ) {
93  return;
94  }
95 
96  // remove
97  m_spectra.erase( m_spectra.begin() + idx );
98 
99  // set group
100  if( m_spectra.size() == 1 ) {
101  setGroup( m_spectra[ 0 ]->getGroup() );
102  }
103  else {
104  setGroup( NULL );
105  }
106 
107  // update range
108  double minX = double();
109  double maxX = double();
110 
111  onGetXRange( &minX, &maxX );
112  setXRange( minX, maxX );
113 }
114 
115 // clear spectra
117  m_spectra.clear();
118  m_dMinX.clear( );
119  m_dMaxX.clear( );
120  m_dMinY.clear( );
121  m_dMaxY.clear( );
122  if ( m_bFlags )
123  {
124  delete[] m_bFlags;
125  m_bFlags = NULL;
126  }
127  if ( m_dInts )
128  {
129  delete[] m_dInts;
130  m_dInts = NULL;
131  }
132  m_nAllocLen = 0;
133 
134  setGroup( NULL );
135 
136  // update range
137  double minX = double();
138  double maxX = double();
139 
140  onGetXRange( &minX, &maxX );
141  setXRange( minX, maxX );
142 }
143 
144 // search spectra
146  // search
147  for( int i = 0; i < (int)m_spectra.size(); i++ ) {
148  if( spec == m_spectra[ i ] ) {
149  return i;
150  }
151  }
152  return -1;
153 }
154 
155 // get xy data
157  kome::core::XYData* const xyData,
158  const double minX,
159  const double maxX
160 ) {
161  // check the member
162  if( m_spectra.size() == 0 ) {
163  return;
164  }
165 
166  // min, max
167  double minMz = 0.0;
168  double maxMz = 0.0;
169 
170  double minInt = 0.0;
171  double maxInt = 0.0;
172 
173  // 登録されているspectrumのなかで、最大・最小をを求めます
174  //
175 
176  // 新規に登録されたspectrumの最大最小値をスタックして、
177  // スタックされている情報から最大最小値を求めます
178  // ※spectrum内の最大最小値を求める時間が省けます
179  //
180  if ( m_spectra.size( ) > m_dMinX.size( ) )
181  {
182  kome::objects::Spectrum* spec = m_spectra[ m_spectra.size( ) - 1 ];
183  // get data points
185  spec->getXYData( &dps, spec->getOperationFlag( ) );
186 
187  double tmpMinX = dps.getMinX( );
188  double tmpMaxX = dps.getMaxX( );
189  double tmpMinY = dps.getMinY( );
190  double tmpMaxY = dps.getMaxY( );
191 
192  m_dMinX.push_back( tmpMinX );
193  m_dMaxX.push_back( tmpMaxX );
194  m_dMinY.push_back( tmpMinY );
195  m_dMaxY.push_back( tmpMaxY );
196  }
197  for( unsigned int i = 0; i < m_spectra.size(); i++ ) {
198  double tmpMinX = m_dMinX[ i ];
199  double tmpMaxX = m_dMaxX[ i ];
200  double tmpMinY = m_dMinY[ i ];
201  double tmpMaxY = m_dMaxY[ i ];
202 
203  if( i == 0 || tmpMinX < minMz ) {
204  minMz = tmpMinX;
205  }
206  if( i == 0 || tmpMaxX > maxMz ) {
207  maxMz = tmpMaxX;
208  }
209  //
210  if( i == 0 || tmpMinY < minInt ) {
211  minInt = tmpMinY;
212  }
213  if( i == 0 || tmpMaxY > maxInt ) {
214  maxInt = tmpMaxY;
215  }
216  }
217 
218  // index
219  const double spacing = 0.2;
220  const int startIdx = std::max( 0, roundnum( minMz / spacing ) );
221  const int endIdx = std::max( 0, roundnum( maxMz / spacing ) );
222 
223  // length
224  const int len = endIdx - startIdx + 1;
225  if( len <= 0 ) {
226  return;
227  }
228 
229  // array
230 // static bool *flags = NULL;
231 // static double *ints = NULL;
232 
233  static int nPrevStartIdx = 0;
234  static int nPrevEndIdx = 0;
235  static double dPrevMinMz = 0.0;
236  static double dPrevMaxMz = 0.0;
237  if ( 0 == m_nAllocLen )
238  {
239  m_bFlags = new bool[ len ];
240  m_dInts = new double[ len ];
241  for ( int i = 0 ; i < len ; i++ )
242  {
243  m_bFlags[ i ] = false;
244  m_dInts[ i ] = 0.0;
245  }
246  m_nAllocLen = len;
247 
248  nPrevStartIdx = startIdx;
249  nPrevEndIdx = endIdx;
250  dPrevMinMz = minMz;
251  dPrevMaxMz = maxMz;
252  }
253  else if ( m_nAllocLen < len )
254  {
255  bool *bFlags = new bool[ len ];
256  double *dInts = new double[ len ];
257 
258  int nStoreMin = roundnum( dPrevMinMz / spacing );
259  nStoreMin = CLAMP( nStoreMin, startIdx, endIdx ) - startIdx;
260  for ( int i = 0 ; i < len ; i++ )
261  {
262  if ( i >= nStoreMin && i < ( nStoreMin + m_nAllocLen ) )
263  {
264  bFlags[i] = m_bFlags[i-nStoreMin];
265  dInts[i] = m_dInts[i-nStoreMin];
266  }
267  else
268  {
269  bFlags[ i ] = false;
270  dInts[ i ] = 0.0;
271  }
272  }
273  delete[] m_dInts;
274  delete[] m_bFlags;
275 
276  m_dInts = dInts;
277  m_bFlags = bFlags;
278  m_nAllocLen = len;
279  dPrevMinMz = minMz;
280  dPrevMaxMz = maxMz;
281  }
282 
283 
284 
285  // fill array
286 //
287 // 全スペクトラムの最大Intensityのデータを抽出しているようです
288 // 拡大したときに範囲内の最大値を求めるのに使用しています
289 //
290 
291  kome::objects::Spectrum* spec = m_spectra[ m_spectra.size( ) - 1 ];
292  // get data points
294  spec->getXYData( &dps, spec->getOperationFlag() );
295 
296  for ( unsigned int j = 0 ; j < dps.getLength( ) ; j++ )
297  {
298  const double x = dps.getX( j );
299  const double y = dps.getY( j );
300 
301  if ( ( minX < 0.0 || x >= minX ) && ( maxX < 0.0 || x <= maxX ) )
302  {
303  int idx = roundnum( x / spacing );
304  idx = CLAMP( idx, startIdx, endIdx ) - startIdx;
305 
306  m_bFlags[ idx ] = true;
307  if ( fabs( y ) > fabs( m_dInts[ idx ] ) )
308  {
309  m_dInts[ idx ] = y;
310  }
311  }
312  }
313  // add array
314  for( int i = 0; i < len; i++ ) {
315  if( m_bFlags[ i ] ) {
316  double x = spacing * ( i + startIdx );
317  double y = m_dInts[ i ];
318 
319  xyData->addPoint( x, y );
320  }
321  }
322 }
323 
324 // get x range
325 void OverlappingSpectrum::onGetXRange( double* minX, double* maxX ) {
326  double minXX = -1.0;
327  double maxXX = -1.0;
328 
329  // get x range
330  for( unsigned int i = 0; i < m_spectra.size(); i++ ) {
331  // spectrum
332  Spectrum* spec = m_spectra[ i ];
333 
334  double tmpMinX = spec->getMinX();
335  double tmpMaxX = spec->getMaxX();
336 
337  if( minXX < 0.0 || tmpMinX < minXX ) {
338  minXX = tmpMinX;
339  }
340  if( maxXX < 0.0 || tmpMaxX > maxXX ) {
341  maxXX = tmpMaxX;
342  }
343  }
344 
345  // store
346  *minX = minXX;
347  *maxX = maxXX;
348 }
349 
350 // get total intensity
351 double OverlappingSpectrum::onGetTotalIntensity( const double minX, const double maxX ) {
352  // get data points
354  getXYData( &dps, -1.0, -1.0, false );
355 
356  // get total intensity
357  double intensity = 0.0;
358  for( unsigned int i = 0; i < dps.getLength(); i++ ) {
359  intensity += dps.getY( i );
360  }
361 
362  return intensity;
363 }
364 
365 // get max intensity
366 double OverlappingSpectrum::onGetMaxIntensity( const double minX, const double maxX ) {
367  // get data points
369  getXYData( &dps, -1.0, -1.0, false );
370 
371  // get total intensity
372  double intensity = 0.0;
373  for( unsigned int i = 0; i < dps.getLength(); i++ ) {
374  intensity += std::max( dps.getY( i ), intensity );
375  }
376 
377  return intensity;
378 }
abstraction class of two dimention coordinate data
Definition: XYData.h:34
data points data of profile management class
Definition: DataPoints.h:25
double getMaxX()
gets maximum x
Definition: XYData.cpp:119
void removeSpectrum(Spectrum *spec)
removes spectrum
double getX(const unsigned int index)
gets x coordinate
Definition: XYData.cpp:224
virtual double onGetMaxIntensity(const double minX, const double maxX)
This method is called by getMaxIntensity method. (override method)
virtual void onGetXYData(kome::core::XYData *const xyData, const double minX, const double maxX)
This method is called by getXYData method. (override method)
interfaces of SampleSet class
double getMinY()
gets minimum y
Definition: XYData.cpp:125
double getY(const unsigned int index)
gets y coordinate
Definition: XYData.cpp:243
int roundnum(const double v)
gets the closest integer to the argument
virtual void onGetXRange(double *minX, double *maxX)
This method is called by getMinX or getMaxX method. (override method)
interfaces of DataGroupNode class
double getMinX()
gets min x
Definition: Spectrum.cpp:461
interfaces of Sample class
double getMaxX()
gets max x
Definition: Spectrum.cpp:489
#define NULL
Definition: CoreMacros.h:18
virtual double onGetTotalIntensity(const double minX, const double maxX)
This method is called by getTotalIntensity method. (override method)
double getMinX()
gets minimum x
Definition: XYData.cpp:113
kome::core::XYData * getXYData()
gets xy data from data manager
Definition: Spectrum.cpp:279
DataGroupNode * getGroup()
gets spectrum group
Definition: Spectrum.cpp:898
#define CLAMP(x, low, high)
Definition: CoreMacros.h:35
interfaces of OverlappingSpectrum class
std::vector< Spectrum * > m_spectra
spectrum information management class
Definition: Spectrum.h:30
int searchSpectrum(Spectrum *spec)
searches spectrum
void addPoint(const double x, const double y)
adds point
Definition: XYData.cpp:149
void setGroup(DataGroupNode *group)
sets spectrum group
Definition: Spectrum.cpp:893
double getMaxY()
gets maximum y
Definition: XYData.cpp:131
bool getOperationFlag()
gets the operation flag value
Definition: Spectrum.h:202
void addSpectrum(Spectrum *spec)
adds spectrum
void setXRange(const double minX, const double maxX)
sets x range
Definition: Spectrum.cpp:443
unsigned int getLength()
gets the number of points @return the number of points
Definition: XYData.cpp:216