Quantum GIS API Documentation  1.7.4
src/analysis/interpolation/qgsidwinterpolator.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                               qgsidwinterpolator.cpp
00003                               ----------------------
00004   begin                : Marco 10, 2008
00005   copyright            : (C) 2008 by Marco Hugentobler
00006   email                : marco dot hugentobler at karto dot baug dot ethz dot ch
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "qgsidwinterpolator.h"
00019 #include <cmath>
00020 #include <limits>
00021 
00022 QgsIDWInterpolator::QgsIDWInterpolator( const QList<LayerData>& layerData ): QgsInterpolator( layerData ), mDistanceCoefficient( 2.0 )
00023 {
00024 
00025 }
00026 
00027 QgsIDWInterpolator::QgsIDWInterpolator(): QgsInterpolator( QList<LayerData>() ), mDistanceCoefficient( 2.0 )
00028 {
00029 
00030 }
00031 
00032 QgsIDWInterpolator::~QgsIDWInterpolator()
00033 {
00034 
00035 }
00036 
00037 int QgsIDWInterpolator::interpolatePoint( double x, double y, double& result )
00038 {
00039   if ( !mDataIsCached )
00040   {
00041     cacheBaseData();
00042   }
00043 
00044   double currentWeight;
00045   double distance;
00046 
00047   double sumCounter = 0;
00048   double sumDenominator = 0;
00049 
00050   QVector<vertexData>::iterator vertex_it = mCachedBaseData.begin();
00051 
00052   for ( ; vertex_it != mCachedBaseData.end(); ++vertex_it )
00053   {
00054     distance = sqrt(( vertex_it->x - x ) * ( vertex_it->x - x ) + ( vertex_it->y - y ) * ( vertex_it->y - y ) );
00055     if (( distance - 0 ) < std::numeric_limits<double>::min() )
00056     {
00057       result = vertex_it->z;
00058       return 0;
00059     }
00060     currentWeight = 1 / ( pow( distance, mDistanceCoefficient ) );
00061     sumCounter += ( currentWeight * vertex_it->z );
00062     sumDenominator += currentWeight;
00063   }
00064 
00065   if ( sumDenominator == 0.0 )
00066   {
00067     return 1;
00068   }
00069 
00070   result = sumCounter / sumDenominator;
00071   return 0;
00072 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines