00001 #include <cxxtls/spline.h> 00002 #include <iostream> 00003 #include <vector> 00004 00005 using namespace std; 00006 using namespace cxxtls; 00007 00008 00009 template<typename X> 00010 struct doubler 00012 { 00013 X operator() (typename spline<X,X>::sample_t const &r) const 00014 { 00015 return (r.second * 2); 00016 } 00017 }; 00018 00019 template<class X, class Y, class Z> 00020 ostream &operator<< (ostream &cout, spline<X,Y,Z> const &sf) 00021 { 00022 typedef typename spline<X,Y,Z>::const_iterator const_iterator; 00023 00024 const_iterator l = sf.begin(), g = sf.end(); 00025 00026 while(l != g) 00027 { 00028 cout << "(" << l->first << "," << l->second << ")"; 00029 ++l; 00030 } 00031 00032 return cout; 00033 } 00034 00035 template<class T> 00036 ostream& operator<< (ostream& cout, set<T> const &s) 00037 { 00038 typename set<T>::const_iterator l = s.begin(), g = s.end(); 00039 00040 while(l != g) 00041 { 00042 T const &r = *l++; 00043 00044 cout << r; 00045 00046 if( l != g ) 00047 cout << ", "; 00048 00049 } 00050 00051 return cout; 00052 } 00053 00054 int main() 00055 { 00056 typedef spline<float,float> Function; 00057 typedef Function::sample_t sample_t; 00058 typedef Function::const_iterator const_iterator; 00059 00060 Function sf; 00061 00062 sf.add_sample( sample_t(1,1) ); 00063 sf.add_sample( sample_t(2,2) ); 00064 sf.add_sample( sample_t(3,3) ); 00065 00066 cout << "Original Data: " << sf << std::endl; 00067 00068 sf.apply(doubler<float>()); 00069 00070 cout << "After doubling: " << sf << std::endl; 00071 00072 std::set<float> domain; sf.domain(&domain); 00073 00074 cout << "Sampled X's: " << domain << std::endl; 00075 00076 { 00077 Function t; 00078 00079 for(float x = 0; x < 5; x += .5) 00080 { 00081 sample_t tmp(x, sf.interpolate_step(x)); 00082 00083 t.add_sample(tmp); 00084 } 00085 00086 cout << "Interpolating stepwise[0-4.5 by .5]: " << t << std::endl; 00087 00088 } 00089 00090 { 00091 cout << "Floor and Ceil: "; 00092 00093 for(float x = 0; x < 5; x += .5) 00094 { 00095 const_iterator f = sf.floor(x); 00096 const_iterator l = sf.ceil(x); 00097 00098 float a; 00099 float b; 00100 00101 if(f == sf.end()) 00102 a = sf.front().first; 00103 else 00104 a = f->first; 00105 00106 if(l == sf.end()) 00107 b = sf.back().first; 00108 else 00109 b = l->first; 00110 00111 cout << x << "[" << a << "-" << b << "],"; 00112 00113 00114 } 00115 00116 cout << std::endl; 00117 00118 } 00119 00120 00121 { 00122 spline<float,float> t; 00123 00124 for(float x = 0; x < 5; x += .5) 00125 { 00126 sample_t tmp(x, sf.interpolate_line(x)); 00127 00128 t.add_sample(tmp); 00129 } 00130 00131 cout << "Interpolating linearly[0-4.5 by .5]:\n " << t << std::endl; 00132 00133 } 00134 00135 00136 { 00137 static std::pair<int,int> samples[] = 00138 { 00139 std::pair<int,int>(1,5), 00140 std::pair<int,int>(2,10), 00141 std::pair<int,int>(3,8) 00142 }; 00143 00144 Function func(samples, samples+3, &Function::interpolate_line); 00145 00146 cout << "construct from array test:\n "; 00147 00148 for(float f = 0; f < 5; f += 1) 00149 { 00150 cout << "(" << f << ", " << func(f) << ")"; 00151 00152 } 00153 00154 cout << std::endl; 00155 00156 static std::pair<int,int> samples_2[] = 00157 { 00158 std::pair<int,int>(4,7), 00159 std::pair<int,int>(5,6), 00160 std::pair<int,int>(6,5) 00161 }; 00162 00163 func.include(samples_2, samples_2+3); 00164 00165 cout << "including more points gives:\n " << func << std::endl; 00166 00167 spline<int,int> sint; 00168 sint.add_sample(spline<int,int>::sample_t(7,3)); 00169 sint.add_sample(spline<int,int>::sample_t(8,1)); 00170 00171 func.include(sint); 00172 00173 cout << "including another spline:\n " << func << std::endl; 00174 00175 spline<int,double> id(func,&spline<int,double>::interpolate_line); 00176 00177 cout << "copy constructor templates:\n " << id << std::endl; 00178 00179 spline<int,double> id2(id); 00180 00181 cout << "real copy constructor:\n " << id2 << std::endl; 00182 00183 spline<int,double> id3; 00184 00185 id3 = id2; 00186 00187 cout << "operator= gives:\n " << id3 << std::endl; 00188 00189 00190 } 00191 00192 { 00193 typedef spline<int,int> one_t; 00194 00195 one_t one(&one_t::interpolate_line); 00196 one.add_sample( one_t::sample_t(1,1) ); 00197 one.add_sample( one_t::sample_t(3,4) ); 00198 one.add_sample( one_t::sample_t(5,8) ); 00199 00200 one_t two(&one_t::interpolate_line); 00201 two.add_sample( one_t::sample_t(2,2) ); 00202 two.add_sample( one_t::sample_t(4,4) ); 00203 two.add_sample( one_t::sample_t(6,6) ); 00204 00205 one += two; 00206 00207 cout << "Adding (1,1)(3,4)(5,8) with (2,2)(4,4)(6,6) gives:\n" 00208 << " " 00209 << one 00210 << endl; 00211 00212 } 00213 00214 { 00215 typedef spline<int,int> one_t; 00216 00217 one_t one(&one_t::interpolate_line); 00218 one.add_sample( one_t::sample_t(1,4) ); 00219 one.add_sample( one_t::sample_t(3,8) ); 00220 one.add_sample( one_t::sample_t(5,12) ); 00221 00222 one_t two(&one_t::interpolate_line); 00223 two.add_sample( one_t::sample_t(0,2) ); 00224 two.add_sample( one_t::sample_t(6,2) ); 00225 00226 one /= two; 00227 00228 cout << "Dividing (1,4)(3,8)(5,12) by (0,2)(6,2) gives:\n" 00229 << " " 00230 << one 00231 << endl; 00232 00233 } 00234 00235 { 00236 typedef spline<int,int> one_t; 00237 00238 one_t one(&one_t::interpolate_line); 00239 one.add_sample( one_t::sample_t(1,4) ); 00240 one.add_sample( one_t::sample_t(3,8) ); 00241 one.add_sample( one_t::sample_t(5,12) ); 00242 00243 00244 vector< pair<int,int> > tmp(10); 00245 00246 one.evaluate(0,1, tmp.begin(), tmp.end()); 00247 00248 int i; 00249 00250 cout << "10 steps of the evaluate function give:\n "; 00251 00252 for(i=0; i < 10; ++i) 00253 cout << "(" << tmp[i].first << "," << tmp[i].second << ")"; 00254 00255 cout << endl; 00256 00257 } 00258 00259 { 00260 typedef spline<double,double> one_t; 00261 00262 one_t one(&one_t::interpolate_cubic); 00263 one.add_sample( one_t::sample_t(1,4) ); 00264 one.add_sample( one_t::sample_t(3,8) ); 00265 one.add_sample( one_t::sample_t(5,12) ); 00266 00267 00268 vector< pair<double,double> > tmp(10); 00269 00270 one.evaluate(0,1, tmp.begin(), tmp.end()); 00271 00272 int i; 00273 00274 cout << "10 steps of the cubic evaluate function give:\n "; 00275 00276 for(i=0; i < 10; ++i) 00277 cout << "(" << tmp[i].first << "," << tmp[i].second << ")"; 00278 00279 cout << endl; 00280 } 00281 00282 00283 return 0; 00284 }