00001
00002
00004 #ifdef WIN32
00005 #pragma warning(disable:4786)
00006 #endif
00007 #include "NearestNeighbourInterpolator2D.h"
00008 #include "Tools.h"
00009 #include <iostream>
00010 #include <vector>
00011 #include <algorithm>
00013
00015 namespace Interpolator
00016 {
00018 NearestNeighbourInterpolator2D::NearestNeighbourInterpolator2D()
00019 {
00020
00021 }
00022
00024 NearestNeighbourInterpolator2D::~NearestNeighbourInterpolator2D()
00025 {
00026
00027 }
00028
00034 NearestNeighbourInterpolator2D::NearestNeighbourInterpolator2D( std::string sName,
00035 std::string sDescription,
00036 std::string sInterpolationType,
00037 InterpolationData2D* mInterpolationData) :
00038 Interpolator2D(sName,sDescription,sInterpolationType,mInterpolationData)
00039 {
00040 }
00041
00042
00044 double NearestNeighbourInterpolator2D::getInterpolatedValue(double dXValue,double dYValue)
00045 {
00046 double z = compute(dXValue,dYValue);
00047 return z;
00048 }
00049
00051 double NearestNeighbourInterpolator2D::compute(double dXValue,double dYValue)
00052 {
00053
00054 int iXIndex = findXIndex(dXValue);
00055 int iYIndex = findYIndex(dYValue);
00056
00057 double z,z1,z2,z3,z4,x1,x3,y1,y2;
00058
00059 z = 0;
00060
00061 if ((iXIndex >= _mInterpolationData->getNbXData()) && (iYIndex >= _mInterpolationData->getNbYData()))
00062 {
00063 if(Interpolator::_verb == true) cout << "case 1" << endl;
00064 z = _mInterpolationData->getZValue(iXIndex-1,iYIndex-1);
00065 }
00066
00067 else if ((iXIndex == -1) && (iYIndex == -1))
00068 {
00069 if(Interpolator::_verb == true) cout << "case 2" << endl;
00070 z = _mInterpolationData->getZValue(0,0);
00071 }
00072
00073 else if ((iXIndex == -1) && (iYIndex >= _mInterpolationData->getNbYData()))
00074 {
00075 if(Interpolator::_verb == true) cout << "case 3" << endl;
00076 z = _mInterpolationData->getZValue(0,iYIndex-1);
00077 }
00078
00079 else if ((iXIndex >= _mInterpolationData->getNbXData()) && (iYIndex == -1))
00080 {
00081 if(Interpolator::_verb == true) cout << "case 4" << endl;
00082 z = _mInterpolationData->getZValue(iXIndex-1,0);
00083 }
00084
00085
00086
00087 else if (iXIndex >= _mInterpolationData->getNbXData())
00088 {
00089 if(Interpolator::_verb == true) cout << "case 5" << endl;
00090 x1 = _mInterpolationData->getXValue(iXIndex-1);
00091
00092 y1 = _mInterpolationData->getYValue(iYIndex);
00093 y2 = _mInterpolationData->getYValue(iYIndex+1);
00094
00095 z1 = _mInterpolationData->getZValue(iXIndex-1,iYIndex);
00096 z2 = _mInterpolationData->getZValue(iXIndex-1,iYIndex+1);
00097
00098 double dDistance1 = x1*x1 + y1*y1;
00099 double dDistance2 = x1*x1 + y2*y2;
00100
00101 if (isLessThan(dDistance1,dDistance2)) z = z1;
00102 else z = z2;
00103 }
00104
00105
00106 else if (iXIndex == -1)
00107 {
00108 if(Interpolator::_verb == true) cout << "case 6" << endl;
00109 x1 = _mInterpolationData->getXValue(iXIndex+1);
00110
00111 y1 = _mInterpolationData->getYValue(iYIndex);
00112 y2 = _mInterpolationData->getYValue(iYIndex+1);
00113
00114 z1 = _mInterpolationData->getZValue(iXIndex+1,iYIndex);
00115 z2 = _mInterpolationData->getZValue(iXIndex+1,iYIndex+1);
00116
00117 double dDistance1 = x1*x1 + y1*y1;
00118 double dDistance2 = x1*x1 + y2*y2;
00119
00120 if (isLessThan(dDistance1,dDistance2)) z = z1;
00121 else z = z2;
00122 }
00123
00124
00125 else if (iYIndex >= _mInterpolationData->getNbYData())
00126 {
00127 if(Interpolator::_verb == true) cout << "case 7" << endl;
00128 y1 = _mInterpolationData->getYValue(iYIndex-1);
00129
00130 z1 = _mInterpolationData->getZValue(iXIndex,iYIndex-1);
00131 z3 = _mInterpolationData->getZValue(iXIndex+1,iYIndex-1);
00132
00133 x1 = _mInterpolationData->getXValue(iXIndex);
00134 x3 = _mInterpolationData->getXValue(iXIndex+1);
00135
00136 double dDistance1 = x1*x1 + y1*y1;
00137 double dDistance2 = x3*x3 + y1*y1;
00138
00139 if (isLessThan(dDistance1,dDistance2)) z = z1;
00140 else z = z3;
00141 }
00142
00143
00144
00145 else if (iYIndex == -1 )
00146 {
00147 if(Interpolator::_verb == true) cout << "case 8" << endl;
00148 y1 = _mInterpolationData->getYValue(iYIndex+1);
00149
00150 z1 = _mInterpolationData->getZValue(iXIndex,iYIndex+1);
00151 z3 = _mInterpolationData->getZValue(iXIndex+1,iYIndex+1);
00152
00153 x1 = _mInterpolationData->getXValue(iXIndex);
00154 x3 = _mInterpolationData->getXValue(iXIndex+1);
00155
00156 double dDistance1 = x1*x1 + y1*y1;
00157 double dDistance2 = x3*x3 + y1*y1;
00158
00159 if (isLessThan(dDistance1,dDistance2)) z = z1;
00160 else z = z3;
00161 }
00162
00163
00164
00165 else
00166 {
00167 if(Interpolator::_verb == true) cout << "case 9" << endl;
00168 z1 = _mInterpolationData->getZValue(iXIndex,iYIndex);
00169 z2 = _mInterpolationData->getZValue(iXIndex,iYIndex+1);
00170 z3 = _mInterpolationData->getZValue(iXIndex+1,iYIndex+1);
00171 z4 = _mInterpolationData->getZValue(iXIndex+1,iYIndex);
00172
00173 x1 = _mInterpolationData->getXValue(iXIndex);
00174 x3 = _mInterpolationData->getXValue(iXIndex+1);
00175 y1 = _mInterpolationData->getYValue(iYIndex);
00176 y2 = _mInterpolationData->getYValue(iYIndex+1);
00177
00178 double dDistance1 = x1*x1 + y1*y1;
00179 double dDistance2 = x1*x1 + y2*y2;
00180 double dDistance3 = x3*x3 + y2*y2;
00181 double dDistance4 = x3*x3 + y1*y1;
00182
00183 std::vector<double> distanceVector;
00184 distanceVector.push_back(dDistance1);
00185 distanceVector.push_back(dDistance2);
00186 distanceVector.push_back(dDistance3);
00187 distanceVector.push_back(dDistance4);
00188 sort(distanceVector.begin(), distanceVector.end());
00189
00190 if (distanceVector[0] == dDistance1) z = z1;
00191 else if (distanceVector[0] == dDistance2) z = z2;
00192 else if (distanceVector[0] == dDistance3) z = z3;
00193 else if (distanceVector[0] == dDistance4) z = z4;
00194
00195 }
00196 if(Interpolator::_verb == true) cout << _mInterpolationData->getZName() << "=f(" << _mInterpolationData->getXName() << "," << _mInterpolationData->getYName() << ")=f("<< dXValue << "," << dYValue<< ")=" << z << endl;
00197
00198 return z;
00199 }
00200 }
00201
00202