InterpolationData1D.cpp

Go to the documentation of this file.
00001 // InterpolationData1D.cpp: implementation of the InterpolationData class.
00002 //
00004 
00005 #ifdef WIN32 
00006 #pragma warning(disable:4786)
00007 #endif
00008 #include "InterpolationData1D.h"
00009 #include "NotAllowedOperationException.h"
00010 #include <iostream>
00012 // Construction/Destruction
00014 
00015 namespace Interpolator
00016 {
00018 InterpolationData1D::InterpolationData1D() : _sXName(""),_sYName(""),_lNbData(0),_mXValues(0),_mYValues(0)
00019 {
00020 
00021 }
00022 
00024 InterpolationData1D::~InterpolationData1D() 
00025 {
00026         if (_mXValues)
00027         {
00028                 delete [] _mXValues;
00029                 _mXValues = 0;  
00030         }
00031         
00032         if (_mYValues)
00033         {
00034                 delete [] _mYValues;
00035                 _mYValues = 0;  
00036         }
00037 }
00038 
00045 InterpolationData1D::InterpolationData1D(       std::string sXName, std::string sYName, 
00046                                                                                         long lNbData,
00047                                                                                         double* mXValues, double* mYValues) : 
00048 _sXName(sXName),_sYName(sYName),_lNbData(lNbData),_mXValues(0),_mYValues(0)
00049 {                       
00050                 _mXValues = new double[_lNbData];
00051                 _mYValues = new double[_lNbData];
00052 
00053                 setValues(lNbData,mXValues,mYValues);
00054 }
00055 
00057 std::string InterpolationData1D::getXName() const
00058 {
00059         return _sXName;
00060 }
00061 
00063 std::string InterpolationData1D::getYName() const
00064 {
00065         return _sYName;
00066 }
00067 
00071 double InterpolationData1D::getXValue(int i) const // throw (IndexOutOfBoundException,NullPointerException)
00072 {
00073         
00074         if ((i < 0) || (i >= getNbData())) throw IndexOutOfBoundException("index",i,0,getNbData(),"InterpolationData1D::getXValue(int i)",__FILE__,__LINE__); 
00075         if (_mXValues == 0) throw NullPointerException("_mXValues","InterpolationData1D::getXValue(int i)",__FILE__,__LINE__);
00076         return _mXValues[i];
00077 }
00078 
00082 double InterpolationData1D::getYValue(int i) const // throw (IndexOutOfBoundException,NullPointerException)
00083 {
00084         if ((i < 0) || (i >= getNbData())) throw IndexOutOfBoundException("index",i,0,getNbData(),"InterpolationData1D::getYValue(int i)",__FILE__,__LINE__); 
00085         if (_mYValues == 0) throw NullPointerException("_mYValues","InterpolationData1D::getYValue(int i)",__FILE__,__LINE__);
00086         return _mYValues[i];
00087 }
00088 
00090 long InterpolationData1D::getNbData() const
00091 {
00092         return _lNbData;
00093 }
00094 
00095 
00100 void InterpolationData1D::setXValue(int i,double dNewValue) // throw (IndexOutOfBoundException,NullPointerException)
00101 {
00102         if ((i < 0) || (i >= getNbData())) throw IndexOutOfBoundException("index",i,0,getNbData(),"InterpolationData1D::setXValue(int i,double dNewValue)",__FILE__,__LINE__); 
00103         if (_mXValues == 0) throw NullPointerException("_mXValues","InterpolationData1D::setXValue(int i,double dNewValue)",__FILE__,__LINE__);
00104         _mXValues[i] = dNewValue;
00105 }
00106 
00110 void InterpolationData1D::setYValue(int i,double dNewValue) // throw (IndexOutOfBoundException,NullPointerException)
00111 {
00112         if ((i < 0) || (i >= getNbData())) throw IndexOutOfBoundException("index",i,0,getNbData(),"InterpolationData1D::setYValue(int i,double dNewValue)",__FILE__,__LINE__); 
00113         if (_mYValues == 0) throw NullPointerException("_mYValues","InterpolationData1D::setYValue(int i,double dNewValue)",__FILE__,__LINE__);
00114         _mYValues[i] = dNewValue;
00115 }
00116 
00117 
00120 double* InterpolationData1D::getXValues() const // throw (NullPointerException)
00121 {
00122         if (_mXValues == 0) throw NullPointerException("_mXValues","InterpolationData1D::getXValues()",__FILE__,__LINE__);
00123         return _mXValues;
00124 }
00125 
00128 double* InterpolationData1D::getYValues() const // throw (NullPointerException)
00129 {
00130         if (_mYValues == 0) throw NullPointerException("_mYValues","InterpolationData1D::getYValues()",__FILE__,__LINE__);
00131         return _mYValues;
00132 }
00133 
00134 
00138 void InterpolationData1D::setXValues(double* dNewValues)
00139 {
00140         for (int i=0;i<_lNbData;i++)
00141         {
00142                 setXValue(i,*(dNewValues+i));
00143         }
00144 }
00145 
00148 void InterpolationData1D::setYValues(double* dNewValues)
00149 {   
00150     for (int i=0;i<_lNbData;i++)
00151         {
00152                 setYValue(i,*(dNewValues+i));
00153         }   
00154 }
00155 
00156 
00160 void InterpolationData1D::setXValuesInInverseOrder(double* dNewValues)
00161 {
00162         for (int i=0;i<_lNbData;i++)
00163         {
00164                 setXValue(i,*(dNewValues + _lNbData - i - 1));
00165         }
00166 }
00167 
00170 void InterpolationData1D::setYValuesInInverseOrder(double* dNewValues)
00171 {   
00172     for (int i=0;i<_lNbData;i++)
00173         {
00174                 setYValue(i,*(dNewValues + _lNbData - i - 1));
00175         }   
00176 }
00177 
00178 
00179 
00180 
00183 void InterpolationData1D::setValues(long lNbData,double* dNewXValues,double* dNewYValues)
00184 {
00185 
00186 switch(checkDataValidity(lNbData,dNewXValues))
00187         {
00188         case -1 :       //the data are ok but decreasingly ordered (must be reorder in order to work) 
00189                 checkSize(lNbData);
00190                 if(Interpolator::_verb == true) cout << "DATA NOT ORDERED --> ORDERING FOR YOU !!!" << endl;
00191                 setXValuesInInverseOrder(dNewXValues);
00192                 setYValuesInInverseOrder(dNewYValues);
00193                 break;
00194         case 1 :        //the data are ok and in increasingly ordered
00195 
00196                 setXValues(dNewXValues);
00197                 setYValues(dNewYValues);
00198                 break;
00199         case 0 :        //the data are not ok they are not ordered
00200                 {
00201                 std::string sTableName = "(";
00202                                         sTableName+=getXName();
00203                                         sTableName+=" --> ";
00204                                         sTableName+=getYName();
00205                                         sTableName+=")";
00206 
00207                 throw NotAllowedOperationException(     sTableName,
00208                                                                                         "The values of the table are not ordered !!!  --> Check the table values",
00209                                                                                         "void InterpolationData1D::setValues(long lNbData,double* dNewXValues,double* dNewYValues)",
00210                                                                                         __FILE__,
00211                                                                                         __LINE__);                                                                      
00212                 break;
00213                 }
00214         case -2 :       //two data are identical (must remove one)
00215                 {
00216                         std::string sTableName = "(";
00217                                         sTableName+=getXName();
00218                                         sTableName+=" --> ";
00219                                         sTableName+=getYName();
00220                                         sTableName+=")";
00221                 throw NotAllowedOperationException(     sTableName,
00222                                                                                         "Two values of the entry column are the same !!! --> Check the table values",
00223                                                                                         "void InterpolationData1D::setValues(long lNbData,double* dNewXValues,double* dNewYValues)",
00224                                                                                         __FILE__,
00225                                                                                         __LINE__);
00226 
00227                 break;
00228                 }
00229         default :
00230                 {
00231                 std::string sTableName = "(";
00232                                         sTableName+=getXName();
00233                                         sTableName+=" --> ";
00234                                         sTableName+=getYName();
00235                                         sTableName+=")";
00236                 throw NotAllowedOperationException(     sTableName,
00237                                                                                         "Unknow Exception during the set values of the table --> Check the table values",
00238                                                                                         "void InterpolationData1D::setValues(long lNbData,double* dNewXValues,double* dNewYValues)",
00239                                                                                         __FILE__,
00240                                                                                         __LINE__);
00241                 }
00242         }
00243 }
00244 
00245 
00246 void InterpolationData1D::checkSize(long lNbData)
00247 {
00248         if (lNbData != getNbData()) //It is necessary to reallocate the arrays
00249     { 
00250                 delete [] _mXValues;
00251                 delete [] _mYValues;
00252                 
00253                 _lNbData = lNbData;
00254 
00255                 _mXValues = new double[_lNbData];
00256                 _mYValues = new double[_lNbData];
00257     }
00258 }
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00274 int InterpolationData1D::checkDataValidity(long lNbData,double* dNewXValues)
00275 {
00276         double dFirstValue      = *dNewXValues;
00277         double dSecondValue = *(dNewXValues+1);
00278         int i;
00279         bool    bIsIncreasing = true;
00280 
00281         //Check the possible ordering
00282 
00283         if (dFirstValue == dSecondValue)                //The two first values are identical : problem !!!
00284         {
00285                 return -2;
00286         }
00287         else if (dFirstValue < dSecondValue)    //All the values must be in increasing order
00288         {
00289                 bIsIncreasing = true;
00290         }
00291         else                                                                    //All the values must be in decreasing order
00292         {
00293                 bIsIncreasing = false;
00294         }
00295         
00296         double dLastValue = dSecondValue;
00297         double dNewValue;
00298 
00299         //Check the ordering of the values
00300         for (i=2;i<lNbData;i++)
00301         {
00302                 dNewValue = *(dNewXValues+i);
00303                 
00304                 if (bIsIncreasing)
00305                 {
00306                         if (dNewValue < dLastValue) return 0;
00307                         else if (dNewValue == dLastValue) return -2;
00308                 }
00309                 else
00310                 {
00311                         if (dNewValue > dLastValue) return 0;
00312                         else if (dNewValue == dLastValue) return -2;
00313                 }
00314 
00315                 dLastValue = dNewValue;
00316         }       
00317 
00318         //In this case it's ok the data are ordered and with the boolean i know if i need to sort or no the values
00319         if (i == lNbData)
00320         {
00321                 if (bIsIncreasing) return 1;
00322                 else return -1;
00323         }
00324         
00325         return 2; //This case must never happen
00326 }
00327 
00328 
00329 
00331 void InterpolationData1D::DisplayData()
00332 {
00333         if(Interpolator::_verb == true) cout << "\t" << getXName() << "\t\t " << getYName() << endl;
00334         for (int i=0;i<getNbData();i++)
00335         {
00336                 if(Interpolator::_verb == true) cout << i << "\t" << getXValue(i) << "\t\t " << getYValue(i) << endl;           
00337         }
00338 }
00339 
00340 
00341 
00342 
00343 
00344 }
00345 
00346 
00347 
00348 
00349 

Generated on Tue Apr 14 09:50:27 2009 for Interpolator Library by  doxygen 1.4.5