// PSTM_simulation_windows.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
#include <memory>
#include <medparam.h>
#include <malloc.h>
#include <string>
#include "ParameterInFile.h"
#include <time.h>　　
#include <crtdbg.h> 
//
// 引用变量的空间
//
using namespace std;


int test(ParameterInFile parmas) {
    cout << parmas.doppler_para[0] << endl;
    return 0;
}

/// <summary>
/// 检查环境，主要检查内存情况
/// </summary>
/// <param name="parmas"></param>
/// <returns></returns>
bool check(ParameterInFile& parmas) {

    return true;
}

sim_block testsimblock(int a = 1) {
    sim_block result(1, 2, 1, 2, 1, 1);
    return result;
}

int Fmaintest() {
    sim_block temp = testsimblock(1);
    point lla = point{ 110,22,33 };
    point xyz = point{ -2023567.6297546995,5559706.3694903487,2374425.2573203994 };
    point ttxyz = LLA2XYZ(lla);
    point ttlla = XYZ2LLA(xyz);
    VectorPoint v1 = getVector(xyz, ttxyz);
    VectorPoint v2 = getVector(lla, ttlla);
    cout << getModule(v1) << std::endl;
    cout << getModule(v2) << std::endl;
    return 0;
}




int main(int argc, char* argv[])
{    
 //   try {
        testPP();


        std::cout << "PSTM_simulation_windows.exe [mode] [pars_path] [resample_para] [--thead_num]" << endl;// 输出帮助文档
        std::cout << "[mode]: 调用模块 0,1,2 " << endl;
        std::cout << "        0:默认路径 " << endl;
        std::cout << "        1:计算正射模拟图 " << endl;
        std::cout << "        2:计算正射校正插值算法与强度图生成 " << endl;
        std::cout << "[para_path]:必选 正射模拟参数文件  " << endl;
        std::cout << "[resample_para]:当mode==2时，必选； 计算正射校正插值算法与强度图生成参数文件  " << endl;
        std::cout << "[--thead_num]:可选 线程数，默认是8" << endl;
        std::cout << "example:" << endl;
        std::cout << "PSTM_simulation_windows.exe 2 C:\\sim_sar_paras.txt D:\\resample_para.txt --thead_num 8" << endl;
        int mode = -1;
        int thread_num = 6;
        std::string pars_path = ""; //配置文件代码
        std::string resample_para = "";
        std::string thread_str = "--thead_num";
        try {
            if (argc < 3) {
                std::cout << "缺少参数" << endl;
                //return 0;
            }
            for (int i = 1; i < argc; i++) {
                if (i == 1) { 
                    mode = stoi(argv[1]);
                    if (mode == 0) {
                        pars_path = "D:\\otherSoftware\\Ortho\\Ortho\\ortho_indirect\\datafolder\\testworkspace4\\sim_sar_paras.txt";
                        resample_para = "D:\\otherSoftware\\Ortho\\Ortho\\ortho_indirect\\datafolder\\testworkspace4\\resample_para.txt";
                        mode = 2;
                        break;
                    }
                }
                if (i == 2) {
                    pars_path= argv[2];
                }
                if (i == 3) {
                    if (mode == 1) {
                        break;
                    }
                    else {
                        resample_para = argv[3];
                    }
                }
            }
            
            for (int i = 1; i < argc; i++) {
                std::string temp = argv[i];
               
                
                if (temp== thread_str) {
                    i = i + 1;
                    
                    if (i >= argc) { break; }
                    else {
                        thread_num = stoi(argv[i]);
                        
                    }
                }
            }
        }
        catch(exception ex) {
            std::cout << "参数解析错误" << endl;
            // 开始test模式

            return -1;
        
        }
        if (1) {
            pars_path = "D:\\MicroWorkspace\\C-SAR\\Ortho\\Temporary\\sim_sar_paras.txt";
            resample_para = "";// "D:\\otherSoftware\\Ortho\\Ortho\\ortho_indirect\\datafolder\\testworkspace\\resample_para.txt";
            mode = 1;
        }

        std::cout << "线程数：" << thread_num << endl;
        //Fmaintest();

        cout << mode << "\n";
        int d=round(3.13);
        if (mode == 1) {
            cout << "sim_sar program run....\n";
            cout << pars_path << "\n";
            ParameterInFile parmas(pars_path);
            //testPTSN(parmas);
            if (!check(parmas)) {
                throw "不符合运行条件";
                return 0;
            }
            //SimProcess(parmas, 32);
            SimProcess_LVY(parmas, thread_num);
           // ResamplingSim(parmas);
            // 检查解析结果
            cout << "programover" << "\n";
        }

        else if (mode == 2) {
            try {
                ConvertResampleParameter converPara(resample_para);
                ParameterInFile parmas(pars_path);
                testPTSN(pars_path);
                SimProcess_Calsim2ori(parmas, converPara, thread_num);
                SimProcess_ResamplingOri2Orth(parmas, converPara, thread_num);
                SimProcess_Calspow(parmas, converPara, thread_num);
            }
            catch(exception& ex) {
                std::cout << ex.what() << std::endl;
                return -1;
            }
            //SimProcess_CalXYZ(parmas, converPara,16);
            //ConverOri2Sim(parmas, converPara);
            //CalCoondinaryXYZOfSAR(parmas, converPara);
        }


  //  }
    //catch (exception ex) {
        // 防止内存泄露，保证内存能够被调用
     //   throw "error";
  //  }

}



// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
