You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kev/Drawer/GVision/WinIDW/-IDWCalculation.cpp

88 lines
1.9 KiB
C++

#include "pch.h"
#include "IDWCalculation.h"
#include <array>
IDWCalculation::~IDWCalculation()
{
if (m_bCreated) {
delete m_pX;
m_pX = nullptr;
delete m_pY;
m_pY = nullptr;
delete m_pZ;
m_pZ = nullptr;
}
}
void IDWCalculation::SetData(vector<double>& vecX, vector<double>& vecY, vector<double>& vecZ)
{
this->m_pX = &vecX;
this->m_pY = &vecY;
this->m_pZ = &vecZ;
}
void IDWCalculation::SetData(vector<array<double, 3>> data)
{
m_pX = new vector<double>;
m_pY = new vector<double>;
m_pZ = new vector<double>;
size_t len = data.size();
for (int i=0;i<len;i++)
{
m_pX->push_back(data[i][0]);
m_pY->push_back(data[i][1]);
m_pZ->push_back(data[i][2]);
}
m_bCreated = TRUE;
}
double IDWCalculation::GetValue(double x, double y)
{
double dFactor = -m_factor / 2;
// 计算距离
vector<double> vecDist;
//vector<double> vecDist;
size_t len = m_pZ->size();
double dDist = 0,dDistPow = 0;
for (size_t i = 0; i < len; i++)
{
//dDist = sqrt((x - m_pX->at(i))*(x - m_pX->at(i)) + (y - m_pY->at(i)) *(y - m_pY->at(i)));
dDist = (x - m_pX->at(i))*(x - m_pX->at(i)) + (y - m_pY->at(i)) *(y - m_pY->at(i));
if (abs(dDist) < 1.0e-10)
{
return m_pZ->at(i);
}
//TRACE("space:x=%lf\n", dDist);_finite
dDistPow = pow(dDist, dFactor);
//if (_isnanf(dDistPow)) {
// return m_pZ->at(i);
//}
vecDist.push_back(dDistPow);
}
// 距离总和
double dSigmaDis = 0;
for (size_t i = 0; i < len; i++)
{
/*dSigmaDis += pow(1.0 / vecDist[i], m_factor);*/
dSigmaDis += vecDist[i];
}
// 计算权重
//vector<double> vecWeight;
// 最终结果
double dValue = 0;
double dWeight = 0;
for (size_t i = 0; i < len; i++)
{
//dWeight = pow(1.0 / vecDist[i], m_factor)/ dSigmaDis;
dWeight = vecDist[i] / dSigmaDis;
//vecWeight.push_back(dWeight);
dValue += m_pZ->at(i)*dWeight;
}
//// 计算最终结果
//double dValue = 0;
//for (size_t i = 0; i < len; i++)
//{
// dValue += m_pZ->at(i)*vecWeight[i];
//}
return dValue;
}