QGIS API Documentation  2.99.0-Master (a411669)
qgsidwinterpolator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsidwinterpolator.cpp
3  ----------------------
4  begin : Marco 10, 2008
5  copyright : (C) 2008 by Marco Hugentobler
6  email : marco dot hugentobler at karto dot baug dot ethz dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsidwinterpolator.h"
19 #include <cmath>
20 #include <limits>
21 
22 QgsIDWInterpolator::QgsIDWInterpolator( const QList<LayerData> &layerData )
23  : QgsInterpolator( layerData )
24 {
25 
26 }
27 
29  : QgsInterpolator( QList<LayerData>() )
30 {
31 
32 }
33 
34 int QgsIDWInterpolator::interpolatePoint( double x, double y, double &result )
35 {
36  if ( !mDataIsCached )
37  {
38  cacheBaseData();
39  }
40 
41  double currentWeight;
42  double distance;
43 
44  double sumCounter = 0;
45  double sumDenominator = 0;
46 
47  Q_FOREACH ( const vertexData &vertex_it, mCachedBaseData )
48  {
49  distance = std::sqrt( ( vertex_it.x - x ) * ( vertex_it.x - x ) + ( vertex_it.y - y ) * ( vertex_it.y - y ) );
50  if ( ( distance - 0 ) < std::numeric_limits<double>::min() )
51  {
52  result = vertex_it.z;
53  return 0;
54  }
55  currentWeight = 1 / ( std::pow( distance, mDistanceCoefficient ) );
56  sumCounter += ( currentWeight * vertex_it.z );
57  sumDenominator += currentWeight;
58  }
59 
60  if ( sumDenominator == 0.0 )
61  {
62  return 1;
63  }
64 
65  result = sumCounter / sumDenominator;
66  return 0;
67 }
QVector< vertexData > mCachedBaseData
Interface class for interpolations.
int interpolatePoint(double x, double y, double &result) override
Calculates interpolation value for map coordinates x, y.
QgsIDWInterpolator(const QList< QgsInterpolator::LayerData > &layerData)
bool mDataIsCached
Flag that tells if the cache already has been filled.
int cacheBaseData()
Caches the vertex and value data from the provider.