gctl_toolkits/archive/fractopo/main.cpp
2024-09-10 20:25:18 +08:00

167 lines
6.1 KiB
C++

#include "dispHelp.h"
#include "FractalTopo.h"
void disp_help(char* proname)
{
string exName = proname;
string exUsage = proname;
exUsage += " [-p<roughness>/<topo-range>] [-r<x-min>/<x-max>/<y-min>/<y-max>/[<d-x>/<d-y>|<point-num>]] [-t<lower-left-topo>/<upper-left-topo>/<upper-right-topo>/<lower-right-topo>] [-g<cx>/<cy>/<sigma>/<ang>/<Lx>/<Ly>/<magnify>[/rho]] [h] > output-file";
dispHelp dh;
dh.changeLayerOut(0,10);
dh.addHeadInfo(exName,"1.4.1","fractal topography generater.","Yi Zhang (zhangyi.cugwuhan@gmail.com)");
dh.addUsage(exUsage);
dh.addOption("Roughness and maximal changing range of the output topography data. The defaults are 1.2/600","-p");
dh.addOption("Calculation range, the program can output topography data under a rectangular grid or at random positions. The defaults are 0/1000/0/1000/10/10","-r");
dh.addOption("Altitudes at four corner's positions. The defaults are 0/0/0/0","-t");
dh.addOption("filter parameters","-g");
dh.addOptionSec("cx/cy : center coordinates of the filter");
dh.addOptionSec("sigma : radius of the shorter axis of the 0.5 filter amplitude");
dh.addOptionSec("ang : angle between the shorter axis of the filter and the x-axis");
dh.addOptionSec("Lx/Ly : length factor the long and short axises of the filter. usually we take Lx = 1 to ensure the radius of 0.5 filter amplitude equals \
the distance given by sigma in the direction of the shorter axis of the filter. a bigger L value indicates faster changing rate in the corresponding \
direction. For instance 1/0.5 yields an oval which flattening equals 2.0");
dh.addOptionSec("magnify : filter amplitude at the center");
dh.addOptionSec("rho : changing rate of the arctan function");
dh.addOption("choose weight function type, input 'pow' for power function (default) and 'atan' for arctan function","-f");
dh.addOption("print this information","-h");
dh.addExample("fractopo > regular.xyz");
dh.addExample("fractopo > -r0/1000/0/1000/5000 > random.xyz");
dh.addExample("fractopo > -g500/500/100/45/1/0.6/1 > regular_gauss.xyz");
dh.addExample("fractopo > -r0/1000/0/1000/5000 -g500/500/100/45/1/0.6/1 > random_gauss.xyz");
dh.show();
}
int main(int argc, char* argv[])
{
double Para[2] = {1.2,600};
double Range[6] = {0,1000,0,1000,10,10};
double Topo[4] = {0,0,0,0};
double Gauss[8] = {1e+30,1e+30,1e+30,1e+30,1e+30,1e+30,1e+30,1e+30};
char flType[1024] = "pow";
FracTopo FT;
opterr = 0;
int curr;
/*循环拾取参数 最后一个参数为-1 需要变量的参数后跟一个冒号 可有可无参数跟两个冒号*/
while((curr = getopt(argc,argv,"hf:p:r:t:g:")) != -1)
{
/*匹配命令*/
switch (curr)
{
case 'h': //显示帮助信息
disp_help(argv[0]);
return 0;
case 'f':
if (1!=sscanf(optarg,"%s",flType)) //格式化读入参数
{
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<optarg<<endl;
cout<<"use -h option to see help information" << endl;
return 0;
}
break;
case 'p':
if (2!=sscanf(optarg,"%lf/%lf",&Para[0],&Para[1])) //格式化读入参数
{
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<optarg<<endl;
cout<<"use -h option to see help information" << endl;
return 0;
}
break;
case 'r':
if (6!=sscanf(optarg,"%lf/%lf/%lf/%lf/%lf/%lf",&Range[0],&Range[1],&Range[2],&Range[3],&Range[4],&Range[5]))
{
if (5!=sscanf(optarg,"%lf/%lf/%lf/%lf/%lf",&Range[0],&Range[1],&Range[2],&Range[3],&Range[4]))
{
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<optarg<<endl;
cout<<"use -h option to see help information" << endl;
return 0;
}
else
{
//确定只有5个范围参数 将最后一个range参数设置为1e+30
Range[5] = 1e+30;
if (Range[0]>Range[1])
{
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<optarg<<endl;
cout<<"use -h option to see help information" << endl;
return 0;
}
else if (Range[2]>Range[3])
{
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<optarg<<endl;
cout<<"use -h option to see help information" << endl;
return 0;
}
else if (Range[4]<=0)
{
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<optarg<<endl;
cout<<"use -h option to see help information" << endl;
return 0;
}
}
}
else
{
if (Range[0]>Range[1]||(Range[0]+2*Range[4])>Range[1])
{
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<optarg<<endl;
cout<<"use -h option to see help information" << endl;
return 0;
}
else if (Range[2]>Range[3]||(Range[2]+2*Range[5])>Range[3])
{
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<optarg<<endl;
cout<<"use -h option to see help information" << endl;
return 0;
}
}
break;
case 't':
if (4!=sscanf(optarg,"%lf/%lf/%lf/%lf",&Topo[0],&Topo[1],&Topo[2],&Topo[3]))
{
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<optarg<<endl;
cout<<"use -h option to see help information" << endl;
return 0;
}
break;
case 'g':
if (8!=sscanf(optarg,"%lf/%lf/%lf/%lf/%lf/%lf/%lf/%lf",&Gauss[0],&Gauss[1],&Gauss[2],&Gauss[3],&Gauss[4],&Gauss[5],&Gauss[6],&Gauss[7]))
{
if (7!=sscanf(optarg,"%lf/%lf/%lf/%lf/%lf/%lf/%lfs",&Gauss[0],&Gauss[1],&Gauss[2],&Gauss[3],&Gauss[4],&Gauss[5],&Gauss[6]))
{
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<optarg<<endl;
cout<<"use -h option to see help information" << endl;
return 0;
}
}
break;
case '?': //处理未定义或错误参数
if (optopt == 'p' || optopt == 'r' || optopt == 't' || optopt == 'g')
{
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
cout<<"use -h option to see help information" << endl;
return 0;
}
else if (isprint(optopt))
{
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
cout<<"use -h option to see help information" << endl;
return 0;
}
else
{
fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
cout<<"use -h option to see help information" << endl;
return 0;
}
break;
default:
abort();
}
}
FT.run(Range,Topo,Para,Gauss,flType);
return 0;
}