/
qgsidwinterpolator.cpp
72 lines (57 loc) · 2.25 KB
/
qgsidwinterpolator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/***************************************************************************
qgsidwinterpolator.cpp
----------------------
begin : Marco 10, 2008
copyright : (C) 2008 by Marco Hugentobler
email : marco dot hugentobler at karto dot baug dot ethz dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsidwinterpolator.h"
#include <cmath>
#include <limits>
QgsIDWInterpolator::QgsIDWInterpolator( const QList<LayerData>& layerData ): QgsInterpolator( layerData ), mDistanceCoefficient( 2.0 )
{
}
QgsIDWInterpolator::QgsIDWInterpolator(): QgsInterpolator( QList<LayerData>() ), mDistanceCoefficient( 2.0 )
{
}
QgsIDWInterpolator::~QgsIDWInterpolator()
{
}
int QgsIDWInterpolator::interpolatePoint( double x, double y, double& result )
{
if ( !mDataIsCached )
{
cacheBaseData();
}
double currentWeight;
double distance;
double sumCounter = 0;
double sumDenominator = 0;
QVector<vertexData>::iterator vertex_it = mCachedBaseData.begin();
for ( ; vertex_it != mCachedBaseData.end(); ++vertex_it )
{
distance = sqrt(( vertex_it->x - x ) * ( vertex_it->x - x ) + ( vertex_it->y - y ) * ( vertex_it->y - y ) );
if (( distance - 0 ) < std::numeric_limits<double>::min() )
{
result = vertex_it->z;
return 0;
}
currentWeight = 1 / ( pow( distance, mDistanceCoefficient ) );
sumCounter += ( currentWeight * vertex_it->z );
sumDenominator += currentWeight;
}
if ( sumDenominator == 0.0 )
{
return 1;
}
result = sumCounter / sumDenominator;
return 0;
}