Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

PiecewiseLinearFct.hpp

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00020 #ifndef _PiecewiseLinearFct_hpp
00021 #define _PiecewiseLinearFct_hpp
00022 
00024 template<class ValueType>
00025 class PiecewiseLinearFct
00026 {
00027     map<float,ValueType> points;
00028 public:
00029     ValueType Value(float x) const
00030     {
00031         float x0,x1;
00032         ValueType v0,v1;
00033         typename map<float,ValueType>::const_iterator lb=points.lower_bound(x);
00034         if(lb==points.end()  )
00035         {
00036             lb--;
00037             x1=(*lb).first;
00038             v1=(*lb).second;
00039             lb--;
00040             x0=(*lb).first;
00041             v0=(*lb).second;
00042         }
00043         else
00044         if(lb==points.begin())
00045         {
00046             x0=(*lb).first;
00047             v0=(*lb).second;
00048             lb++;
00049             x1=(*lb).first;
00050             v1=(*lb).second;
00051         }
00052         else
00053         {
00054             x1=(*lb).first;
00055             v1=(*lb).second;
00056             lb--;
00057             x0=(*lb).first;
00058             v0=(*lb).second;
00059         }
00060         return ((x-x0)/(x1-x0))*(v1-v0)+v0;
00061     }
00062     void Plot() const
00063     {
00064         vector<double> x,y;
00065 
00066         float x0=(*points.begin()).first;
00067         float x1=(*--points.end()).first;
00068         for(float xx=x0-.5*(x1-x0);xx<=x1+.5*(x1-x0);xx+=(x1-x0)/100.0)
00069         {
00070             x.push_back(xx);
00071             y.push_back((*this)(xx));
00072         }
00073         GnuPlot(x,y);
00074     }
00075     inline ValueType operator()(float x) const {return Value(x);}
00076     void AddValue(float x,const ValueType &y){points[x]=y;}
00077     PiecewiseLinearFct(){;}
00079     PiecewiseLinearFct(const ValueType *init,int n)
00080     {
00081         for(int i=0;i<n;i++)
00082         {
00083             AddValue((float)init[2*i],init[2*i+1]);
00084         }
00085     }
00086 };
00087 
00088 typedef  PiecewiseLinearFct<float> PiecewiseLinearFctf;
00089 
00090 #endif // _PiecewiseLinearFct_hpp

Generated on Fri Jun 17 13:36:06 2005 for ImLib3D by  doxygen 1.4.2