EMAN2
xydata.h
Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033  *
00034  * */
00035 
00036 #ifndef eman__xydata_h__
00037 #define eman__xydata_h__ 1
00038 
00039 #include <string>
00040 #include <vector>
00041 
00042 using std::string;
00043 using std::vector;
00044 
00045 namespace EMAN
00046 {
00049         class XYData
00050         {
00051           public:
00052                 struct Pair
00053                 {
00054                         Pair(float xx, float yy)
00055                         :x(xx), y(yy)
00056                         {
00057                         }
00058 
00059                         bool operator<(const Pair & p) const
00060                         {
00061                                 return (x < p.x);
00062                         }
00063 
00064                         float x;
00065                         float y;
00066                 };
00067 
00068           public:
00069                 XYData();
00070                 virtual ~ XYData()
00071                 {
00072                 }
00073 
00074                 int read_file(const string & filename);
00075 
00076                 int write_file(const string & filename) const;
00077 
00078                 float calc_correlation(XYData * xy, float minx, float maxx) const;
00079 
00080                 void update();
00081 
00082                 float get_yatx(float x,bool outzero=true);              // if outzero is set, values outside the data domain will be 0, otherwise clamped at the edge
00083                 
00084                 float get_yatx_smooth(float x,int smoothing);   // This will use interpolation to smooth the returned values, and will continue interpolating outside the domain
00085                                                                                                                 // smoothing=1 (linear) is the only supported mode at the moment
00086                 
00087 
00088                 float get_x(size_t i) const
00089                 {
00090                         return data[i].x;
00091                 }
00092 
00093                 void set_x(size_t i, float x)
00094                 {
00095                         if (i>=data.size()) data.resize(i+1,Pair(0,0));
00096                         data[i].x = x;
00097                 }
00098 
00099                 float get_y(size_t i) const
00100                 {
00101                         return data[i].y;
00102                 }
00103 
00104                 void set_y(size_t i, float y)
00105                 {
00106                         if (i>=data.size()) data.resize(i+1,Pair(0,0));
00107                         data[i].y = y;
00108                 }
00109 
00110                 // inserts a new value in x-sorted position, lengthens vector by 1
00111                 void insort(float x, float y);
00112                 
00113                 // If any identical X values exist, y values will be averaged and a point removed
00114                 // data must be sorted on X for this to work (see update)
00115                 void dedupx();
00116                 
00117                 vector<float> get_xlist() const;
00118 
00119                 vector<float> get_ylist() const;
00120 
00121                 vector<float> get_state() const;
00122                 
00123                 void set_state(vector<float>);
00124 
00125                 void set_xy_list(const vector<float>& xlist, const vector<float>& ylist);
00126 
00127                 size_t get_size() const
00128                 {
00129                         return data.size();
00130                 }
00131 
00132                 void set_size(size_t n);
00133 
00134                 float get_miny()
00135                 {
00136 //                      update();
00137                         return ymin;
00138                 }
00139 
00140                 float get_maxy()
00141                 {
00142 //                      update();
00143                         return ymax;
00144                 }
00145 
00146                 bool is_validx(float x) const
00147                 {
00148                         if (x < data[0].x || x > data[data.size() - 1].x)
00149                         {
00150                                 return false;
00151                         }
00152                         return true;
00153                 }
00154 
00155           private:
00156                 vector < Pair > data;
00157                 float ymin;
00158                 float ymax;
00159                 float mean_x_spacing;
00160         };
00161 }
00162 
00163 
00164 #endif