RasterProcessTool/GF3CalibrationAndOrthLib/GF3CalibrationAndOrthLib.cpp

124 lines
4.2 KiB
C++

#include "GF3CalibrationAndOrthLib.h"
#include "BaseConstVariable.h"
#include "RasterToolBase.h"
#include "ImageOperatorBase.h"
#include <QDebug>
#include <QFile>
#include <memory>
#include "FileOperator.h"
#include "GPUBaseTool.h"
#include "GPUTool.cuh"
GF3CALIBRATIONANDORTHLIB_EXPORT int GF3_Sigma0_HH2VV(QString in_SigmaHHRasterPath, QString in_IncidencAngleRasterPath, QString out_SigmaVVRasterPath)
{
// step 1 检查输入合法性
{
// 1. 检查输入合法性
if (in_SigmaHHRasterPath.isEmpty() || in_IncidencAngleRasterPath.isEmpty() || out_SigmaVVRasterPath.isEmpty())
{
qDebug() << "Input or output file path is empty.";
throw std::invalid_argument("Input or output file path is empty.");
return 1; // 代表输入或输出文件路径为空
}
// 2. 检查输入影像是否存在
if (!QFile::exists(in_SigmaHHRasterPath) || !QFile::exists(in_IncidencAngleRasterPath))
{
qDebug() << "Input file does not exist.";
throw std::runtime_error("Input file does not exist.");
return 2; // 代表输入文件不存在
}
gdalImage inSigmaHHRaster(in_SigmaHHRasterPath);
gdalImage inIncidencAngleRaster(in_IncidencAngleRasterPath);
// 3. 检查输入影像大小是否一致
if (inSigmaHHRaster.width != inIncidencAngleRaster.width || inSigmaHHRaster.height != inIncidencAngleRaster.height)
{
qDebug() << "Input images have different sizes.";
throw std::runtime_error("Input images have different sizes.");
return 3; // 代表输入影像大小不一致
}
// 复制输入文件到输出文件
copyFile(in_SigmaHHRasterPath, out_SigmaVVRasterPath);
// 4. 检查输出文件是否存在
if (!QFile::exists(out_SigmaVVRasterPath))
{
qDebug() << "Output file does not exist.";
throw std::runtime_error("Output file does not exist.");
return 4; // 代表输出文件不存在
}
}
// step 2 执行操作
{
gdalImage inSigmaHHRaster(in_SigmaHHRasterPath);
gdalImage inIncidencAngleRaster(in_IncidencAngleRasterPath);
gdalImage outSigmaVVRaster(out_SigmaVVRasterPath);
long width = inSigmaHHRaster.width;
long height = inSigmaHHRaster.height;
int64_t pixel_count64 = static_cast<int64_t>(height) * static_cast<int64_t>(width);
// 读取输入影像数据
std::shared_ptr<double> sigmaHHRasterData = readDataArr<double>(inSigmaHHRaster, 0, 0, height, width, 1, GDALREADARRCOPYMETHOD::VARIABLEMETHOD);
std::shared_ptr<double> incidenceAngleData = readDataArr<double>(inIncidencAngleRaster, 0, 0, height, width, 1, GDALREADARRCOPYMETHOD::VARIABLEMETHOD);
std::shared_ptr<double> outSigmaVVRasterData = readDataArr<double>(outSigmaVVRaster, 0, 0, height, width, 1, GDALREADARRCOPYMETHOD::VARIABLEMETHOD);
// 分块执行数据
int64_t block_count = Memory1MB / 8 * 500; // 500M内存 作为数据分块大小
// 创建交换内存
//double* sigmaHHRasterDataPtr_H = (double*)mallocCUDAHost(sizeof(double) * block_count); // 主机内存
double* sigmaHHRasterDataPtr_D = (double*)mallocCUDADevice(sizeof(double) * block_count);// 设备内存
//double* incidenceAngleDataPtr_H = (double*)mallocCUDAHost(sizeof(double) * block_count); // 主机内存
double* incidenceAngleDataPtr_D = (double*)mallocCUDADevice(sizeof(double) * block_count);// 设备内存
//double* outSigmaVVRasterDataPtr_H = (double*)mallocCUDAHost(sizeof(double) * block_count); // 主机内存
double* outSigmaVVRasterDataPtr_D = (double*)mallocCUDADevice(sizeof(double) * block_count);// 设备内存
for (int64_t i = 0; i < pixel_count64; i += block_count)
{
int64_t block_size = (i + block_count) > pixel_count64 ? pixel_count64 - i : block_count;
//采用内存拷贝 从主内存到分块内存
HostToDevice(sigmaHHRasterDataPtr_D, sigmaHHRasterData.get() + i, sizeof(double) * block_size);
HostToDevice(incidenceAngleDataPtr_D, incidenceAngleData.get() + i, sizeof(double) * block_size);
// 执行GPU计算
// 从设备内存到主机内存
DeviceToHost(outSigmaVVRasterDataPtr_D, outSigmaVVRasterData.get() + i, sizeof(double) * block_size);
}
// 强制释放内存
FreeCUDADevice(sigmaHHRasterDataPtr_D);
//FreeCUDAHost(sigmaHHRasterDataPtr_H);
FreeCUDADevice(incidenceAngleDataPtr_D);
//FreeCUDAHost(incidenceAngleDataPtr_H);
FreeCUDADevice(outSigmaVVRasterDataPtr_D);
//FreeCUDAHost(outSigmaVVRasterDataPtr_H);
sigmaHHRasterData.reset();
incidenceAngleData.reset();
outSigmaVVRasterData.reset();
}
return -1;// 代表执行成功
}