DinverCore/ParameterGrid.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of DinverCore.
00004 **
00005 **  This file may be distributed and/or modified under the terms of the
00006 **  GNU General Public License version 2 or 3 as published by the Free
00007 **  Software Foundation and appearing in the file LICENSE.GPL included
00008 **  in the packaging of this file.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
00013 **  more details.
00014 **
00015 **  You should have received a copy of the GNU General Public License
00016 **  along with this program. If not, see <http://www.gnu.org/licenses/>.
00017 **
00018 **  See http://www.geopsy.org for more information.
00019 **
00020 **  Created : 2009-05-17
00021 **  Authors:
00022 **    Marc Wathelet
00023 **    Marc Wathelet (LGIT, Grenoble, France)
00024 **
00025 ***************************************************************************/
00026 
00027 #ifndef PARAMETERGRID_H
00028 #define PARAMETERGRID_H
00029 
00030 #include <math.h>
00031 
00032 #include "DinverCoreDLLExport.h"
00033 
00034 namespace DinverCore {
00035 
00036 class DINVERCORE_EXPORT ParameterGrid
00037 {
00038 public:
00039   ParameterGrid();
00040   ~ParameterGrid();
00041 
00042   enum Scale {Linear, Log};
00043 
00044   void setScale(Scale s) {_scale=s;}
00045   void setPrecision(double p) {ASSERT(p >= 0.0); _precision=p;}
00046 
00047   void setMinimum(double m) {_minimum=m;}
00048   double minimum() const {return _minimum;}
00049 
00050   void setMaximum(double m) {_maximum=m;}
00051   double maximum() const {return _maximum;}
00052 
00053   bool init();
00054 
00055   int count() const {return _n;}
00056   inline double realValue(int index) const;
00057   inline double realValue(double index) const;
00058   inline int gridValue(double val) const;
00059   inline void getLimits(int& min, int& max, double dmin, double dmax) const;
00060   inline void getLimits(double& min, double& max, double dmin, double dmax) const;
00061 private:
00062   void cacheRealLinearValues();
00063   void cacheRealLogValues();
00064 
00065   Scale _scale;
00066   double _minimum;
00067   double _maximum;
00068   double _precision;
00069   double * _realValues;
00070   int _n;
00071   double _a, _b, _c;
00072 };
00073 
00074 inline double ParameterGrid::realValue(int index) const
00075 {
00076   return _realValues[ index ];
00077 }
00078 
00079 inline double ParameterGrid::realValue(double index) const
00080 {
00081   switch(_scale) {
00082   case Linear:
00083     break;
00084   case Log:
00085     return _minimum*pow(_c, index);
00086   }
00087   return _minimum + index * _b;
00088 }
00089 
00090 inline int ParameterGrid::gridValue(double val) const
00091 {
00092   switch(_scale) {
00093   case Linear:
00094     break;
00095   case Log:
00096     return (int) round(log( val * _a) * _b);
00097   }
00098   return (int) round(( val - _minimum) * _a);
00099 }
00100 
00101 inline void ParameterGrid::getLimits(int& min, int& max, double dmin, double dmax) const
00102 {
00103   switch (_scale) {
00104   case Linear:
00105     min=(int) ceil (( dmin - _minimum) * _a + 1e-10);
00106     max=(int) floor (( dmax - _minimum) * _a - 1e-10);
00107     break;
00108   case Log:
00109     min=(int) ceil(log( dmin * _a) * _b + 1e-10);
00110     max=(int) floor(log( dmax * _a) * _b - 1e-10);
00111     break;
00112   }
00113 }
00114 
00115 inline void ParameterGrid::getLimits(double& min, double& max, double dmin, double dmax) const
00116 {
00117   switch (_scale) {
00118   case Linear:
00119     min=(dmin - _minimum) * _a;
00120     max=(dmax - _minimum) * _a;
00121     break;
00122   case Log:
00123     min=log(dmin * _a) * _b;
00124     max=log(dmax * _a) * _b;
00125     break;
00126   }
00127 }
00128 
00129 } // namespace DinverCore
00130 
00131 #endif // PARAMETERGRID_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines