Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
kome::core::XYData Class Referenceabstract

abstraction class of two dimention coordinate data More...

#include <XYData.h>

Inheritance diagram for kome::core::XYData:
Inheritance graph
[legend]

Public Member Functions

 XYData ()
 constructor
 
virtual ~XYData ()
 destructor
 
double getMinX ()
 gets minimum x More...
 
double getMaxX ()
 gets maximum x More...
 
double getMinY ()
 gets minimum y More...
 
double getMaxY ()
 gets maximum y More...
 
void clearPoints ()
 clear all data points
 
void addPoint (const double x, const double y)
 adds point More...
 
void insertPoint (const unsigned int index, const double x, const double y)
 insertts point More...
 
void updatePoint (const unsigned int index, const double x, const double y)
 updates points More...
 
void deletePoint (const unsigned int index)
 delete point More...
 
unsigned int getLength ()
 gets the number of points @return the number of points
 
double getX (const unsigned int index)
 gets x coordinate More...
 
double getY (const unsigned int index)
 gets y coordinate More...
 
void reserve (const unsigned int num)
 reserves enough contiguous memory of array More...
 
unsigned long getVersion ()
 gets the version More...
 
int searchIndex (const double x)
 searches index of specified x value. More...
 
int getNearestIndex (const double x)
 gets nearest index More...
 
void filter (const double absY=0.0, const double relY=0.0)
 executes filter More...
 
void getPoints (std::vector< Point< double > > &points, const bool ySort, const bool desc)
 gets points array More...
 
bool importData (boost::function< int(void *, int) > readFun)
 imports data More...
 
bool exportData (boost::function< int(void *, int) > writeFun)
 exports data More...
 

Protected Member Functions

void updateRange ()
 updates range
 
virtual bool onLoadData (boost::function< int(void *, int) > readFun)
 loads data from file More...
 
virtual bool onSaveData (boost::function< int(void *, int) > writeFun)
 saves data to file More...
 
virtual void onClearPoints ()=0
 This method is called by clearPoints method. (abstract method)
 
virtual void onAddPoint (const double x, const double y)=0
 This method is called by addPoint method. (abstract method) More...
 
virtual void onInsertPoint (const unsigned int index, const double x, const double y)=0
 This method is called by insertPoint method. (abstract method) More...
 
virtual void onDeletePoint (const unsigned int index)=0
 This method is called by deletePoint method. (abstract method) More...
 
virtual unsigned int onGetLength ()=0
 this method is called by getLength method (abstract method) More...
 
virtual double onGetX (const unsigned int index)=0
 This method is called by getX method. (abstract method) More...
 
virtual double onGetY (const unsigned int index)=0
 This method is called by getY method. (abstract method) More...
 
virtual void onReserve (const unsigned int num)=0
 This method is called by reserve method. (abstract method) More...
 

Protected Attributes

bool m_updated
 
double m_minX
 
double m_maxX
 
double m_minY
 
double m_maxY
 
unsigned long m_version
 

Static Protected Attributes

static unsigned long m_currentVersion = 0
 version More...
 

Detailed Description

abstraction class of two dimention coordinate data

Definition at line 34 of file XYData.h.

Member Function Documentation

void kome::core::XYData::addPoint ( const double  x,
const double  y 
)

adds point

Parameters
[in]xx coordinate of point to be added
[in]yy coordinate of point to be added

Definition at line 149 of file XYData.cpp.

149  {
150  // recover data
151  recoverData();
152 
153  // add point
154  onAddPoint( x, y );
155 
156  // flag
157  m_updated = true;
158 }
virtual void onAddPoint(const double x, const double y)=0
This method is called by addPoint method. (abstract method)
void kome::core::XYData::deletePoint ( const unsigned int  index)

delete point

Parameters
[in]indexpoint index

Definition at line 199 of file XYData.cpp.

199  {
200  // recover data
201  recoverData();
202 
203  // check the parameter
204  if( index >= getLength() ) {
205  return;
206  }
207 
208  // on delete
209  onDeletePoint( index );
210 
211  // flag
212  m_updated = true;
213 }
virtual void onDeletePoint(const unsigned int index)=0
This method is called by deletePoint method. (abstract method)
unsigned int getLength()
gets the number of points @return the number of points
Definition: XYData.cpp:216
bool kome::core::XYData::exportData ( boost::function< int(void *, int) >  writeFun)

exports data

Parameters
[in]writeFunwrite function

Definition at line 414 of file XYData.cpp.

414  {
415  // update range
416  updateRange();
417 
418  // export
419  bool ret = onSaveData( writeFun );
420  return ret;
421 }
virtual bool onSaveData(boost::function< int(void *, int) > writeFun)
saves data to file
Definition: XYData.cpp:503
void updateRange()
updates range
Definition: XYData.cpp:438
void kome::core::XYData::filter ( const double  absY = 0.0,
const double  relY = 0.0 
)

executes filter

Parameters
[in]absYabsolute y value
[in]relYrelative y value

Definition at line 345 of file XYData.cpp.

345  {
346  // recover data
347  recoverData();
348 
349  // length
350  int len = (int)getLength();
351  if( len <= 0 ) {
352  return;
353  }
354 
355  // threshold
356  double minY = std::max( fabs( m_minY ), fabs( m_maxY ) ) * relY / 100.0;
357  minY = std::max( absY, minY );
358 
359  // filter
360  for( int i = len - 1; i >= 0; i-- ) {
361  double y = getY( i );
362  if( y < minY ) {
363  onDeletePoint( i );
364  }
365  }
366 
367  // flag
368  m_updated = true;
369 }
virtual void onDeletePoint(const unsigned int index)=0
This method is called by deletePoint method. (abstract method)
double getY(const unsigned int index)
gets y coordinate
Definition: XYData.cpp:243
double m_maxY
Definition: XYData.h:62
double m_minY
Definition: XYData.h:59
unsigned int getLength()
gets the number of points @return the number of points
Definition: XYData.cpp:216
double kome::core::XYData::getMaxX ( )

gets maximum x

Returns
maximum x

Definition at line 119 of file XYData.cpp.

119  {
120  updateRange();
121  return m_maxX;
122 }
double m_maxX
Definition: XYData.h:56
void updateRange()
updates range
Definition: XYData.cpp:438
double kome::core::XYData::getMaxY ( )

gets maximum y

Returns
maximum y

Definition at line 131 of file XYData.cpp.

131  {
132  updateRange();
133  return m_maxY;
134 }
double m_maxY
Definition: XYData.h:62
void updateRange()
updates range
Definition: XYData.cpp:438
double kome::core::XYData::getMinX ( )

gets minimum x

Returns
minimum x

Definition at line 113 of file XYData.cpp.

113  {
114  updateRange();
115  return m_minX;
116 }
double m_minX
Definition: XYData.h:53
void updateRange()
updates range
Definition: XYData.cpp:438
double kome::core::XYData::getMinY ( )

gets minimum y

Returns
minimum y

Definition at line 125 of file XYData.cpp.

125  {
126  updateRange();
127  return m_minY;
128 }
double m_minY
Definition: XYData.h:59
void updateRange()
updates range
Definition: XYData.cpp:438
int kome::core::XYData::getNearestIndex ( const double  x)

gets nearest index

Parameters
[in]xx value

Definition at line 313 of file XYData.cpp.

313  {
314  // length
315  int len = (int)getLength();
316  if( len == 0 ) {
317  return -1;
318  }
319 
320  // search
321  int idx = searchIndex( x );
322 
323  if( idx >= 0 ) {
324  return idx;
325  }
326 
327  int idx0 = - idx - 2;
328  int idx1 = - idx - 1;
329 
330  if( idx0 < 0 ) {
331  return idx1;
332  }
333  if( idx1 >= len ) {
334  return idx0;
335  }
336 
337  // distance
338  double d0 = fabs( getX( idx0 ) - x );
339  double d1 = fabs( getX( idx1 ) - x );
340 
341  return ( d1 < d0 ? idx1 : idx0 );
342 }
double getX(const unsigned int index)
gets x coordinate
Definition: XYData.cpp:224
int searchIndex(const double x)
searches index of specified x value.
Definition: XYData.cpp:276
unsigned int getLength()
gets the number of points @return the number of points
Definition: XYData.cpp:216
void kome::core::XYData::getPoints ( std::vector< Point< double > > &  points,
const bool  ySort,
const bool  desc 
)

gets points array

Parameters
[in]pointspoints array to store points data
[in]ySort
  • true: sort by x element
  • false: sort by y element
[in]descdescending order flag

Definition at line 372 of file XYData.cpp.

372  {
373  // recover data
374  recoverData();
375 
376  // definition
377  typedef Point< double > pt;
378 
379  // initialize
380  points.clear();
381 
382  // set data
383  unsigned int len = getLength();
384  if( len == 0 ) {
385  return;
386  }
387  points.resize( len );
388 
389  for( unsigned int i = 0; i < len; i++ ) {
390  points[ i ].px = onGetX( i );
391  points[ i ].py = onGetY( i );
392  }
393 
394  // sort
395  std::sort(
396  points.begin(),
397  points.end(),
398  ySort ? ( desc ? pt::greaterY : pt::lessY ) : ( desc ? pt::greaterX : pt::lessX )
399  );
400 }
virtual double onGetX(const unsigned int index)=0
This method is called by getX method. (abstract method)
virtual double onGetY(const unsigned int index)=0
This method is called by getY method. (abstract method)
unsigned int getLength()
gets the number of points @return the number of points
Definition: XYData.cpp:216
unsigned long kome::core::XYData::getVersion ( )

gets the version

Returns
version

Definition at line 270 of file XYData.cpp.

270  {
271  updateRange();
272  return m_version;
273 }
unsigned long m_version
Definition: XYData.h:65
void updateRange()
updates range
Definition: XYData.cpp:438
double kome::core::XYData::getX ( const unsigned int  index)

gets x coordinate

Parameters
[in]indexthe index of point
Returns
x coorsinate

Definition at line 224 of file XYData.cpp.

224  {
225  // recover data
226  recoverData();
227 
228  if( index >= getLength() ) {
229  std::string msg = FMT(
230  "The point index(%d) is larger than max index(%d).",
231  index,
232  getLength() - 1
233  );
234 
235  LOG_WARN( msg );
236  throw msg;
237  }
238 
239  return onGetX( index );
240 }
virtual double onGetX(const unsigned int index)=0
This method is called by getX method. (abstract method)
unsigned int getLength()
gets the number of points @return the number of points
Definition: XYData.cpp:216
double kome::core::XYData::getY ( const unsigned int  index)

gets y coordinate

Parameters
[in]indexthe index of point
Returns
y coordinate

Definition at line 243 of file XYData.cpp.

243  {
244  // recover data
245  recoverData();
246 
247  if( index >= getLength() ) {
248  std::string msg = FMT(
249  "The point index(%d) is larger than max index(%d).",
250  index,
251  getLength() - 1
252  );
253 
254  LOG_WARN( msg );
255  throw msg;
256  }
257 
258  return onGetY( index );
259 }
virtual double onGetY(const unsigned int index)=0
This method is called by getY method. (abstract method)
unsigned int getLength()
gets the number of points @return the number of points
Definition: XYData.cpp:216
bool kome::core::XYData::importData ( boost::function< int(void *, int) >  readFun)

imports data

Parameters
[in]readFunread function

Definition at line 403 of file XYData.cpp.

403  {
404  // clear
405  onClearPoints();
406 
407  // import
408  bool ret = onLoadData( readFun );
409 
410  return ret;
411 }
virtual bool onLoadData(boost::function< int(void *, int) > readFun)
loads data from file
Definition: XYData.cpp:481
virtual void onClearPoints()=0
This method is called by clearPoints method. (abstract method)
void kome::core::XYData::insertPoint ( const unsigned int  index,
const double  x,
const double  y 
)

insertts point

Parameters
[in]indexinsert position
[in]xx coordinate of point
[in]yy coordinate of point

Definition at line 161 of file XYData.cpp.

161  {
162  // recover data
163  recoverData();
164 
165  // insert point
166  onInsertPoint( std::min( index, getLength() ), x, y );
167 
168  // flag
169  m_updated = true;
170 }
virtual void onInsertPoint(const unsigned int index, const double x, const double y)=0
This method is called by insertPoint method. (abstract method)
unsigned int getLength()
gets the number of points @return the number of points
Definition: XYData.cpp:216
void kome::core::XYData::onAddPoint ( const double  x,
const double  y 
)
protectedpure virtual

This method is called by addPoint method. (abstract method)

Parameters
[in]xx coordinate of point to be added
[in]yy coordinate of point to be added

Implemented in kome::objects::Peaks, kome::core::DataPoints, and kome::core::PointArray.

void kome::core::XYData::onDeletePoint ( const unsigned int  index)
protectedpure virtual

This method is called by deletePoint method. (abstract method)

Parameters
[in]indexpoint index

Implemented in kome::objects::Peaks, kome::core::DataPoints, and kome::core::PointArray.

unsigned int kome::core::XYData::onGetLength ( )
protectedpure virtual

this method is called by getLength method (abstract method)

Returns
the number of points

Implemented in kome::objects::Peaks, kome::core::DataPoints, and kome::core::PointArray.

double kome::core::XYData::onGetX ( const unsigned int  index)
protectedpure virtual

This method is called by getX method. (abstract method)

Parameters
[in]indexthe index of point
Returns
x coordinate

Implemented in kome::objects::Peaks, kome::core::DataPoints, and kome::core::PointArray.

double kome::core::XYData::onGetY ( const unsigned int  index)
protectedpure virtual

This method is called by getY method. (abstract method)

Parameters
[in]indexthe index of point
Returns
y coordiante

Implemented in kome::objects::Peaks, kome::core::DataPoints, and kome::core::PointArray.

void kome::core::XYData::onInsertPoint ( const unsigned int  index,
const double  x,
const double  y 
)
protectedpure virtual

This method is called by insertPoint method. (abstract method)

Parameters
[in]indexinsert position
[in]xx coordinate of point
[in]yy coordinate of point

Implemented in kome::objects::Peaks, kome::core::DataPoints, and kome::core::PointArray.

bool kome::core::XYData::onLoadData ( boost::function< int(void *, int) >  readFun)
protectedvirtual

loads data from file

Parameters
[in]readFunread function
Returns
If true, it succeeded to load the data.

Reimplemented in kome::objects::Peaks.

Definition at line 481 of file XYData.cpp.

481  {
482  // length
483  unsigned long len = 0;
484  readFun( &len, sizeof( len ) );
485 
486  // read data
487  if( len > 0 ) {
488  double* arr = new double[ len * 2 ];
489  readFun( arr, sizeof( double ) * len * 2 );
490 
491  onReserve( len );
492  for( unsigned int i = 0; i < len; i++ ) {
493  onAddPoint( arr[ i * 2 ], arr[ i * 2 + 1 ] );
494  }
495 
496  delete[] arr;
497  }
498 
499  return true;
500 }
virtual void onAddPoint(const double x, const double y)=0
This method is called by addPoint method. (abstract method)
virtual void onReserve(const unsigned int num)=0
This method is called by reserve method. (abstract method)
void kome::core::XYData::onReserve ( const unsigned int  num)
protectedpure virtual

This method is called by reserve method. (abstract method)

Parameters
[in]numof points to be reserved

Implemented in kome::objects::Peaks, kome::core::DataPoints, and kome::core::PointArray.

bool kome::core::XYData::onSaveData ( boost::function< int(void *, int) >  writeFun)
protectedvirtual

saves data to file

Parameters
[in]writeFunwrite function
Returns
If true, it succeeded to save the data

Reimplemented in kome::objects::Peaks.

Definition at line 503 of file XYData.cpp.

503  {
504  // length
505  unsigned long len = onGetLength();
506  writeFun( &len, sizeof( len ) );
507 
508  // write data
509  if( len > 0 ) {
510  double* arr = new double[ len * 2 ];
511  for( unsigned int i = 0; i < len; i++ ) {
512  arr[ i * 2 ] = onGetX( i );
513  arr[ i * 2 + 1 ] = onGetY( i );
514  }
515 
516  writeFun( arr, sizeof( double ) * len * 2 );
517 
518  delete[] arr;
519  }
520 
521  return true;
522 }
virtual double onGetX(const unsigned int index)=0
This method is called by getX method. (abstract method)
virtual double onGetY(const unsigned int index)=0
This method is called by getY method. (abstract method)
virtual unsigned int onGetLength()=0
this method is called by getLength method (abstract method)
void kome::core::XYData::reserve ( const unsigned int  num)

reserves enough contiguous memory of array

Parameters
[in]numthe number of points to be reserved

Definition at line 262 of file XYData.cpp.

262  {
263  // recover data
264  recoverData();
265 
266  onReserve( num );
267 }
virtual void onReserve(const unsigned int num)=0
This method is called by reserve method. (abstract method)
int kome::core::XYData::searchIndex ( const double  x)

searches index of specified x value.

Returns
index (If there isn't specified x, this method returns negative value. (See SearchTool::binarySearch. SearchTool class exists in AlgorithmTools library.)

Definition at line 276 of file XYData.cpp.

276  {
277  // recover data
278  recoverData();
279 
280  // comparator
281  class SearchHelper {
282  public:
283  // compare to search
284  static int compare( double x0, double x1 ) {
285  if( x0 < x1 ) {
286  return -1;
287  }
288  if( x0 > x1 ) {
289  return 1;
290  }
291  return 0;
292  }
293 
294  // get x
295  static double getX( int index, XYData* xyData ) {
296  return xyData->onGetX( index );
297  }
298  };
299 
300  // search
301  int idx = -1;
302  idx = SearchTool::binarySearch< double, double >(
303  x,
304  boost::bind( SearchHelper::getX, _1, this ),
305  SearchHelper::compare,
306  getLength()
307  );
308 
309  return idx;
310 }
abstraction class of two dimention coordinate data
Definition: XYData.h:34
double getX(const unsigned int index)
gets x coordinate
Definition: XYData.cpp:224
virtual double onGetX(const unsigned int index)=0
This method is called by getX method. (abstract method)
unsigned int getLength()
gets the number of points @return the number of points
Definition: XYData.cpp:216

Here is the call graph for this function:

void kome::core::XYData::updatePoint ( const unsigned int  index,
const double  x,
const double  y 
)

updates points

Parameters
[in]indexupdate position
[in]xx coordinate of point
[in]yy coordinate of point

Definition at line 173 of file XYData.cpp.

173  {
174  // recover data
175  recoverData();
176 
177  // check the index
178  if( index >= getLength() ) {
179  return;
180  }
181 
182  // delete
183  deletePoint( index );
184 
185  // search
186  int idx = searchIndex( x );
187  if( idx < 0 ) {
188  idx = - idx - 1;
189  }
190 
191  // insert
192  onInsertPoint( idx, x, y );
193 
194  // flag
195  m_updated = true;
196 }
virtual void onInsertPoint(const unsigned int index, const double x, const double y)=0
This method is called by insertPoint method. (abstract method)
void deletePoint(const unsigned int index)
delete point
Definition: XYData.cpp:199
int searchIndex(const double x)
searches index of specified x value.
Definition: XYData.cpp:276
unsigned int getLength()
gets the number of points @return the number of points
Definition: XYData.cpp:216

Member Data Documentation

unsigned long XYData::m_currentVersion = 0
staticprotected

version

version

Definition at line 80 of file XYData.h.

double kome::core::XYData::m_maxX
protected

max x

Definition at line 56 of file XYData.h.

double kome::core::XYData::m_maxY
protected

max y

Definition at line 62 of file XYData.h.

double kome::core::XYData::m_minX
protected

min x

Definition at line 53 of file XYData.h.

double kome::core::XYData::m_minY
protected

min y

Definition at line 59 of file XYData.h.

bool kome::core::XYData::m_updated
protected

updated flag

Definition at line 50 of file XYData.h.

unsigned long kome::core::XYData::m_version
protected

version

Definition at line 65 of file XYData.h.


The documentation for this class was generated from the following files: