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.
207 lines
5.0 KiB
C++
207 lines
5.0 KiB
C++
#include "pch.h"
|
|
#include "IDWHelper.h"
|
|
#include <array>
|
|
#include <vector>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <omp.h>
|
|
#include "IDWCalculation.h"
|
|
|
|
void IDWHelper::Calculate(CMesh* pMesh, vector<array<double, 3>> wellData, LPCTSTR outFile, double factor)
|
|
{
|
|
long numx, numy; pMesh->GetNumber(numx, numy);
|
|
double ddx, ddy; pMesh->GetDelt(ddx, ddy);
|
|
double x0, y0; pMesh->GetOrg(x0, y0);
|
|
|
|
// 井点处的层位Z值
|
|
vector<double> dataWellOnLayer;
|
|
// 井点处层位与井的差值
|
|
vector<double> dataWellSpace;
|
|
size_t len = wellData.size();
|
|
for (size_t i = 0; i < len; i++)
|
|
{
|
|
array<double, 3> arrCur = wellData[i];
|
|
|
|
long iX = AfxGetPublicFunction()->FloatToLong((arrCur[0] - x0) / ddx);
|
|
long iY = AfxGetPublicFunction()->FloatToLong((arrCur[1] - y0) / ddy);
|
|
|
|
// 无效值
|
|
if (iX < 0 || iX >= numx || iY < 0 || iY >= numy) {
|
|
dataWellOnLayer.push_back(InvalidZ);
|
|
dataWellSpace.push_back(InvalidZ);
|
|
continue;
|
|
}
|
|
|
|
double dValue = pMesh->GetValue(iX, iY);
|
|
double dSpace = dValue - arrCur[2];
|
|
if (dValue > 1e30)
|
|
{
|
|
dSpace = InvalidZ;
|
|
}
|
|
//TRACE("space:x=%lf, y=%lf, z=%lf, space=%lf\n", arrCur[0], arrCur[1], dValue, dSpace);
|
|
dataWellOnLayer.push_back(dValue);
|
|
dataWellSpace.push_back(dSpace);
|
|
}
|
|
// 清理无效值
|
|
vector<array<double, 3>> vecValidSpace;
|
|
for (size_t i = 0; i < len; i++) {
|
|
if (dataWellOnLayer[i] > 1e30)
|
|
{
|
|
continue;
|
|
}
|
|
array<double, 3> nums = { 0 };
|
|
long iX = AfxGetPublicFunction()->FloatToLong((wellData[i][0] - x0) / ddx);
|
|
long iY = AfxGetPublicFunction()->FloatToLong((wellData[i][1] - y0) / ddy);
|
|
nums[0] = iX;
|
|
nums[1] = iY;
|
|
nums[2] = dataWellSpace[i];
|
|
vecValidSpace.push_back(nums);
|
|
}
|
|
// 反距离加权运算
|
|
CGrid* pGrd = (pMesh->GetMesh());
|
|
IDWCalculation idw;
|
|
idw.m_factor = factor;
|
|
idw.SetData(vecValidSpace);
|
|
#pragma omp parallel for
|
|
for (int i = 0; i < numx; i++)
|
|
{
|
|
for (int j = 0; j < numy; j++)
|
|
{
|
|
double dValueInter = 0, dValueResult = 0, dValueBefore = 0;
|
|
dValueBefore = pMesh->GetValue(i, j);
|
|
//TRACE("dValueBefore:x=%d, y=%d, z=%lf\n", i, j, dValueBefore);
|
|
if (dValueBefore > 1e30) {
|
|
dValueBefore = InvalidZ;
|
|
continue;
|
|
}
|
|
|
|
dValueInter = idw.GetValue(i, j);
|
|
dValueResult = dValueBefore - dValueInter;
|
|
//TRACE("space:x=%d, y=%d, z=%lf, space=%lf\n", i, j, dValueResult, dValueInter);
|
|
pGrd->SetValue(i, j, dValueResult);
|
|
//m_pMesh->SetZ(i, j, dValueResult);
|
|
}
|
|
}
|
|
//CString strResult = layerFile;
|
|
//strResult.Replace(".", "_idw.");
|
|
pMesh->WriteMesh(outFile, MESH_DFG);
|
|
}
|
|
void IDWHelper::Calculate(LPCTSTR layerFile, LPCTSTR wellFile, LPCTSTR outFile, double factor)
|
|
{
|
|
// 打开文件
|
|
if (OpenFile(layerFile) == FALSE) {
|
|
return;
|
|
}
|
|
CPositionList posListMesh;
|
|
CMesh* pMesh;
|
|
int nCount = m_pXy->GetElement(DOUBLEFOX_MESH, posListMesh);
|
|
|
|
POSITION posMesh = NULL;
|
|
if (nCount > 0)
|
|
{
|
|
posMesh = posListMesh.GetHead();
|
|
pMesh = (CMesh*)m_pXy->GetAtValue(posMesh);
|
|
}
|
|
else {
|
|
return;
|
|
}
|
|
if (posMesh == nullptr) return;
|
|
|
|
std::string strWellFile = string(wellFile);
|
|
vector<array<double, 3>> wellData;
|
|
ReadInToMatrix(strWellFile, wellData);
|
|
|
|
Calculate(pMesh, wellData, outFile, factor);
|
|
}
|
|
|
|
//#ifdef _DEBUG
|
|
//#define new DEBUG_NEW
|
|
//#endif
|
|
|
|
void IDWHelper::ReadInToMatrix(string FilePath, vector<array<double, 3>>& data)
|
|
{
|
|
ifstream in;
|
|
in.open(FilePath, ios::in);//打开一个file
|
|
if (!in.is_open()) {
|
|
//cout << "Can not find " << FilePath << endl;
|
|
system("pause");
|
|
}
|
|
// 以‘,’为分隔符拆分字符串
|
|
char seps[] = " ,\t\n";;
|
|
string buff;
|
|
|
|
while (getline(in, buff)) {
|
|
array<double, 3> nums = { 0 };
|
|
// string->char *
|
|
char *s_input = (char *)buff.c_str();
|
|
char *token = strtok(s_input, seps);
|
|
double a;
|
|
int i = 0;//行数i
|
|
while (token != NULL) {
|
|
a = atof(token);
|
|
nums[i] = a;
|
|
i++;
|
|
if (i > 2) break;
|
|
token = strtok(NULL, seps);
|
|
}//end while
|
|
if (i < 2) continue;
|
|
data.push_back(nums);
|
|
}//end while
|
|
in.close();
|
|
//cout << "get data" << endl;
|
|
}
|
|
|
|
BOOL IDWHelper::OpenFile(LPCTSTR lpszFileName)
|
|
{
|
|
if (m_pXy != NULL) {
|
|
m_pXy->Clear();
|
|
}
|
|
if (m_pXy == NULL)
|
|
m_pXy = new CXy;
|
|
if (m_pXy->ReadOtherWithExtension(lpszFileName))
|
|
{
|
|
return TRUE;
|
|
}
|
|
CFile file;
|
|
if (file.Open(lpszFileName, CFile::modeRead))
|
|
{
|
|
//m_FileName = lpszFileName;
|
|
CArchive ar(&file, CArchive::load);
|
|
Serialize(ar);
|
|
ar.Close();
|
|
}
|
|
file.Close();
|
|
return TRUE;
|
|
}
|
|
void IDWHelper::Serialize(CArchive& ar)
|
|
{
|
|
if (ar.IsStoring()) {}
|
|
else
|
|
{
|
|
if (m_pXy == NULL) m_pXy = new CXy;
|
|
//BeginProgress(IDS_STRING_OpenFile);
|
|
|
|
CString name = ar.GetFile()->GetFileName();
|
|
name.MakeLower();
|
|
CSplitPath sp(name);
|
|
|
|
CString ext = sp.GetExtension();
|
|
//ar.m_pDocument = this; // set back-pointer in archive
|
|
if (ext == _T(".dfb") ||
|
|
ext == _T(".dft"))
|
|
{
|
|
m_pXy->m_version = m_pXy->DFB_ReadVersion(ar);
|
|
m_pXy->DFB_ReadEncrypt(ar, m_pXy->m_version);
|
|
if (m_pXy->IsEncrypted()) {
|
|
}
|
|
m_pXy->DFB_Serialize(ar, m_pXy->m_version);
|
|
}
|
|
else if (ext == ".dml" || ext == ".xml")
|
|
m_pXy->DML_Read2(*(ar.GetFile()));
|
|
else if (ext == ".pcg")
|
|
m_pXy->PCG_Read2(*(ar.GetFile()));
|
|
else
|
|
m_pXy->DFD_Read2(*(ar.GetFile()));
|
|
}
|
|
} |