BilinearInterpolator2D.cpp

Go to the documentation of this file.
00001 // BilinearInterpolator2D.cpp: implementation of the BilinearInterpolator2D class.
00002 //
00004 
00005 #ifdef WIN32 
00006 #pragma warning(disable:4786)
00007 #endif
00008 #include "BilinearInterpolator2D.h"
00009 
00010 #include <iostream>
00011 
00013 // Construction/Destruction
00015 namespace Interpolator
00016 {
00018 BilinearInterpolator2D::BilinearInterpolator2D()
00019 {
00020 
00021 }
00022 
00024 BilinearInterpolator2D::~BilinearInterpolator2D()
00025 {
00026 }
00027 
00028 
00033 BilinearInterpolator2D::BilinearInterpolator2D( std::string sName, 
00034                                                                                                 std::string sDescription, 
00035                                                                                                 InterpolationData2D* mInterpolationData) : 
00036 Interpolator2D(sName,sDescription,"Bilinear",mInterpolationData)
00037 {
00038 }
00039 
00041 double BilinearInterpolator2D::getInterpolatedValue(double dXValue,double dYValue)
00042 {
00043         double z = compute(dXValue,dYValue);
00044         return z;
00045 }
00046 
00048 double BilinearInterpolator2D::compute(double dXValue,double dYValue)
00049 {
00050         //Faire algo quicksort pour plus de rapidité et faire les verifs sur les index calculés et sur le z si possible 
00051         int iXIndex = findXIndex(dXValue);
00052         int iYIndex = findYIndex(dYValue);
00053         
00054         double z,z1,z2,z3,z4,x1,x3,y1,y2;
00055         double a,b,c,d;
00056 
00057         //The 4 cases where the indexes for X and Y are outside the arrays (four corners cases)
00058         if ((iXIndex >= _mInterpolationData->getNbXData()) && (iYIndex >= _mInterpolationData->getNbYData()))
00059         {
00060                 if(Interpolator::_verb == true) cout << "case 1" << endl;
00061                 z = _mInterpolationData->getZValue(iXIndex-1,iYIndex-1);
00062         }
00063         
00064         else if ((iXIndex == -1) && (iYIndex == -1))
00065         {       
00066                 if(Interpolator::_verb == true) cout << "case 2" << endl;
00067                 z = _mInterpolationData->getZValue(0,0);
00068         }
00069 
00070         else if ((iXIndex == -1) && (iYIndex >= _mInterpolationData->getNbYData()))
00071         {
00072                 if(Interpolator::_verb == true) cout << "case 3" << endl;
00073                 z = _mInterpolationData->getZValue(0,iYIndex-1);
00074         }
00075 
00076         else if ((iXIndex >= _mInterpolationData->getNbXData()) && (iYIndex == -1))
00077         {
00078                 if(Interpolator::_verb == true) cout << "case 4" << endl;
00079                 z = _mInterpolationData->getZValue(iXIndex-1,0);
00080         }
00081 
00082 
00083         //The case when the X index is outside (bigger than the last index) of the array
00084         else if (iXIndex >= _mInterpolationData->getNbXData()) 
00085         {
00086                 if(Interpolator::_verb == true) cout << "case 5" << endl;
00087                  x1     =  _mInterpolationData->getXValue(iXIndex-1);
00088                  
00089                  y1 =  _mInterpolationData->getYValue(iYIndex);
00090                  y2 =  _mInterpolationData->getYValue(iYIndex+1);
00091 
00092                  z1     = _mInterpolationData->getZValue(iXIndex-1,iYIndex);
00093                  z2     = _mInterpolationData->getZValue(iXIndex-1,iYIndex+1);          
00094                 
00095                 if(Interpolator::_verb == true) cout << "(iX-1="         << iXIndex-1   << "|iY="   << iYIndex   << ")     --> (" << x1  << "," << y1  << "," << z1  << ")" << endl;
00096                 if(Interpolator::_verb == true) cout << "(iX=" << iXIndex << "|iY+1=" << iYIndex+1 << ") --> (" << x1 << "," << y2 << "," << z2  << ")" << endl;
00097 
00098                 double y1Moinsy2        = y1-y2;
00099                                 
00100                  a = (z2*y1 - z1*y2)/y1Moinsy2;
00101                  b = (z1 - z2)/y1Moinsy2;
00102                  
00103                  z = a + b*dYValue;
00104         }
00105         
00106         //The case when the X index is outside (lower than the first index) of the array
00107         else if (iXIndex == -1) 
00108         {
00109                 if(Interpolator::_verb == true) cout << "case 6" << endl;
00110                  x1     =  _mInterpolationData->getXValue(iXIndex+1);
00111                  
00112                  y1 =  _mInterpolationData->getYValue(iYIndex);
00113                  y2 =  _mInterpolationData->getYValue(iYIndex+1);
00114 
00115                  z1     = _mInterpolationData->getZValue(iXIndex+1,iYIndex);
00116                  z2     = _mInterpolationData->getZValue(iXIndex+1,iYIndex+1);          
00117                 
00118                 if(Interpolator::_verb == true) cout << "(iX="   << iXIndex   << "|iY="   << iYIndex   << ")     --> (" << x1  << "," << y1  << "," << z1  << ")" << endl;
00119                 if(Interpolator::_verb == true) cout << "(iX+1=" << iXIndex+1 << "|iY+1=" << iYIndex+1 << ") --> (" << x1 << "," << y2 << "," << z2  << ")" << endl;
00120 
00121                 double y1Moinsy2        = y1-y2;
00122                                 
00123                  a = (z2*y1 - z1*y2)/y1Moinsy2;
00124                  b = (z1 - z2)/y1Moinsy2;
00125                  
00126                  z = a + b*dYValue;
00127         }
00128 
00129         //The case when the Y index is outside (bigger than the last index) of the array
00130         else if (iYIndex >= _mInterpolationData->getNbYData())
00131         {
00132                 if(Interpolator::_verb == true) cout << "case 7" << endl;
00133                  y1 =  _mInterpolationData->getYValue(iYIndex-1);
00134 
00135                  z1     = _mInterpolationData->getZValue(iXIndex,iYIndex-1);
00136                  z3     = _mInterpolationData->getZValue(iXIndex+1,iYIndex-1);          
00137         
00138                  x1     =  _mInterpolationData->getXValue(iXIndex);
00139                  x3     =  _mInterpolationData->getXValue(iXIndex+1);
00140                 
00141                 if(Interpolator::_verb == true) cout << "(iX="   << iXIndex   << "|iY-1="   << iYIndex-1   << ")     --> (" << x1  << "," << y1  << "," << z1  << ")" << endl;
00142                 if(Interpolator::_verb == true) cout << "(iX+1=" << iXIndex+1 << "|iY=" << iYIndex << ") --> (" << x3 << "," << y1 << "," << z3  << ")" << endl;
00143 
00144                 double x3Moinsx1        = x3-x1;
00145                                 
00146                  a = (z1*x3 - z3*x1)/x3Moinsx1;
00147                  b = (z3 - z1)/x3Moinsx1;
00148                  
00149                  z = a + b*dXValue;
00150         }
00151         
00152                 
00153         //The case when the Y index is outside (lower than the first index) of the array
00154         else if (iYIndex == -1 )
00155         {
00156                 if(Interpolator::_verb == true) cout << "case 8" << endl;
00157                  y1 =  _mInterpolationData->getYValue(iYIndex+1);
00158 
00159                  z1     = _mInterpolationData->getZValue(iXIndex,iYIndex+1);
00160                  z3     = _mInterpolationData->getZValue(iXIndex+1,iYIndex+1);          
00161         
00162                  x1     =  _mInterpolationData->getXValue(iXIndex);
00163                  x3     =  _mInterpolationData->getXValue(iXIndex+1);
00164                 
00165                 if(Interpolator::_verb == true) cout << "(iX="   << iXIndex   << "|iY="   << iYIndex   << ")     --> (" << x1  << "," << y1  << "," << z1  << ")" << endl;
00166                 if(Interpolator::_verb == true) cout << "(iX+1=" << iXIndex+1 << "|iY+1=" << iYIndex+1 << ") --> (" << x3 << "," << y1 << "," << z3  << ")" << endl;
00167 
00168                 double x3Moinsx1        = x3-x1;
00169                                 
00170                  a = (z1*x3 - z3*x1)/x3Moinsx1;
00171                  b = (z3 - z1)/x3Moinsx1;
00172                  
00173                  z = a + b*dXValue;
00174         }
00175 
00176 
00177         //Normal case
00178         else 
00179         {
00180                 if(Interpolator::_verb == true) cout << "case 9" << endl;
00181                  z1     = _mInterpolationData->getZValue(iXIndex,iYIndex);
00182                  z2     = _mInterpolationData->getZValue(iXIndex,iYIndex+1);    
00183                  z3     = _mInterpolationData->getZValue(iXIndex+1,iYIndex+1);  
00184                  z4     = _mInterpolationData->getZValue(iXIndex+1,iYIndex);    
00185                 
00186                  x1     =  _mInterpolationData->getXValue(iXIndex);
00187                  x3     =  _mInterpolationData->getXValue(iXIndex+1);
00188                  y1     =  _mInterpolationData->getYValue(iYIndex);
00189                  y2     =  _mInterpolationData->getYValue(iYIndex+1);   
00190                  
00191                 if(Interpolator::_verb == true) cout << "(iX="   << iXIndex   << "|iY="   << iYIndex   << ")     --> (" << x1  << "," << y1  << "," << z1  << ")" << endl;
00192                 if(Interpolator::_verb == true) cout << "(iX+1=" << iXIndex+1 << "|iY="   << iYIndex   << ")   --> (" << x3 << "," << y1  << "," << z4  << ")" << endl;
00193                 if(Interpolator::_verb == true) cout << "(iX+1=" << iXIndex+1 << "|iY+1=" << iYIndex+1 << ") --> (" << x3 << "," << y2 << "," << z3  << ")" << endl;
00194                 if(Interpolator::_verb == true) cout << "(iX="   << iXIndex << "|iY+1="   << iYIndex+1 << ")   --> (" << x1  << "," << y2 << "," << z2  << ")" << endl;
00195 
00196                 double x3Moinsx1        = x3-x1;
00197                 double y2Moinsy1        = y2-y1;
00198                 double dDeno    = x3Moinsx1*y2Moinsy1;
00199                 
00200                 
00201                  a = (x3*(y2*z1 - y1*z2)+x1*(y1*z3 - y2*z4))/dDeno;
00202                  b = (y1*(z2 - z3) + y2*(z4 - z1))/dDeno;
00203                  c = (x1*(z4 - z3) + x3*(z2 - z1))/dDeno;
00204                  d = (z1 - z2 +z3 - z4)/dDeno;
00205                  
00206                  z              = a + b*dXValue + c*dYValue + d*dXValue*dYValue;
00207         }
00208         if(Interpolator::_verb == true) cout << _mInterpolationData->getZName() << "=f(" << _mInterpolationData->getXName() << "," << _mInterpolationData->getYName() << ")=f("<< dXValue << "," << dYValue<< ")=" <<  z << endl;
00209         //if(Interpolator::_verb == true) cout << "z=f("<< dXValue << "," << dYValue<< ")=" <<  z << endl;
00210         return z;
00211 }
00212 }
00213 
00214 

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