initial upload

This commit is contained in:
2024-09-10 20:25:18 +08:00
parent b8de03ee4f
commit f1cc876972
377 changed files with 2721267 additions and 34 deletions

33
archive/fractopo/.gitignore vendored Normal file
View File

@@ -0,0 +1,33 @@
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
*.ex

View File

@@ -0,0 +1,525 @@
#ifndef _FRACTAL_TOPO_H
#define _FRACTAL_TOPO_H
#include <iostream>
#include <sstream>
#include <fstream>
#include <math.h>
#include <string.h>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>
#include "ctime"
#include "unistd.h"
#include "ctype.h"
#define max(a,b) a>b?a:b;
#define PARA "-p"
#define RANGE "-r"
#define PREDEFINED "-t"
#define GAUSS "-g"
#define OUTPUT "-o"
#define BOLDRED "\033[1m\033[31m"
#define RESET "\033[0m"
#define pi (4.0*atan(1.0))
using namespace std;
struct posi
{
double x,y,v;
};
//滤波器函数参数组
struct elliFilter
{
double muX,muY,sigma,theta,Lx,Ly,magnify,rho;
};
double sign(double input)
{
if (input >= 0.0) return 1.0;
else return -1.0;
}
double ElliFilter(int type,double x,double y,elliFilter elf)
{
double xd,yd,filter;
xd = x - elf.muX; yd = y - elf.muY;
double xyModule = sqrt(pow(xd,2)+pow(yd,2)+1e-20);
double t = sign(yd)*acos(xd/xyModule);
double weight = sqrt(pow(elf.Lx*cos(t - elf.theta*pi/180.0),2) + pow(elf.Ly*sin(t - elf.theta*pi/180.0),2));
if(type) //如果type为真则使用arctan构造加权函数
{
filter = pi - 2.0*atan(elf.rho*weight*xyModule - elf.rho*elf.sigma);
filter = filter/(pi + 2.0*atan(elf.rho*elf.sigma));
}
//type为假则使用以e为底的幂函数构造加权函数
else filter = elf.magnify*pow(2.0,-1.0*weight*weight*xyModule*xyModule/(elf.sigma*elf.sigma));
return filter;
}
class FracTopo
{
public:
FracTopo();
~FracTopo();
int topo_gener_regular();//生成随机地形
int output_regular();//输出文件
int gauss_filter_regular();
int topo_gener_random();
int output_random();
int gauss_filter_random();
int run(double*,double*,double*,double*,char*);
private:
double xmin,xmax,dx,ymin,ymax,dy;
double ld,lu,rd,ru;
int xnum,ynum,order_x,order_y,imax;
double H;
double randmax;
elliFilter filter_N;
int ntotal;
int random_num;
double** topo;
double** topo_range;
posi* topo_random;
int ifarctan;
};
FracTopo::FracTopo()
{
topo = NULL;
topo_range = NULL;
topo_random = NULL;
}
FracTopo::~FracTopo()
{
if (topo!=NULL)
{
for(int j=0; j<ntotal; j++) delete[] topo[j];
delete[] topo;
}
if (topo_range!=NULL)
{
for (int i = 0; i < xnum; i++) delete[] topo_range[i];
delete[] topo_range;
}
if (topo_random!=NULL)
{
delete[] topo_random;
}
}
int FracTopo::topo_gener_regular()
{
int i,j,m,n,R,jmax;
double temp = 1e10;
double random;
xnum = (xmax-xmin)/dx+1;
ynum = (ymax-ymin)/dy+1;
xmax = xmin+(xnum-1)*dx;
ymax = ymin+(ynum-1)*dy;
order_x = ceil(log(xnum)/log(2));
order_y = ceil(log(ynum)/log(2));
imax = max(order_x,order_y);
ntotal = (int)pow(2.0,imax)+1; //总数据点数为2的imax次方加1
topo = new double* [ntotal];//开辟二维动态数组
for(i=0; i<ntotal; i++) topo[i] = new double [ntotal];
topo_range = new double* [xnum];
for(i=0; i<xnum; i++) topo_range[i] = new double [ynum];
for (i=0; i<ntotal; i++)//设定地形数据初始值,角点数据必须给出
{
for (j=0; j<ntotal; j++)
{
if (i==0&&j==0)
{
topo[i][j]=ld;//角点初始值;
}
else if (i==ntotal-1&&j==0)
{
topo[i][j]=lu;//角点初始值;
}
else if (i==0&&j==ntotal-1)
{
topo[i][j]=rd;//角点初始值;
}
else if (i==ntotal-1&&j==ntotal-1)
{
topo[i][j]=ru;//角点初始值;
}
else
{
topo[i][j]=temp;//非角点数值初始化为1e10
}
}
}
srand((unsigned) time(NULL));
for (i=1; i<=imax; i++)//开始迭代,生成正方形区域随机地形
{
R=int(double(ntotal-1)/pow(2.0,i));
jmax=int(pow(4.0,i-1));
for (j=1; j<=jmax; j++)
{
random=2*randmax*rand()/RAND_MAX-randmax;
m=2*R*(j-(ceil((double)j/pow(2.0,i-1))-1) * pow(2.0,i-1))-R;
n=2*R*(ceil((double)j/pow(2.0,i-1)))-R;
topo[m][n]=(topo[m-R][n-R]+topo[m+R][n-R]+topo[m-R][n+R]+topo[m+R][n+R])/4+random;
}
for (j=1; j<=jmax; j++)
{
m=2*R*(j-(ceil((double)j/pow(2.0,i-1))-1)* pow(2.0,i-1))-R;
n=2*R*(ceil((double)j/pow(2.0,i-1)))-R;
if (topo[m][n-R]==temp)
{
random=2*randmax*rand()/RAND_MAX-randmax;
if ((n-R)!=0)
{
topo[m][n-R]=(topo[m][n-2*R]+topo[m-R][n-R]+topo[m+R][n-R]+topo[m][n])/4+random;
}
else
{
topo[m][n-R]=(topo[m-R][n-R]+topo[m+R][n-R]+topo[m][n])/3+random;
}
}
if (topo[m-R][n]==temp)
{
random=2*randmax*rand()/RAND_MAX-randmax;
if ((m-R)!=0)
{
topo[m-R][n]=(topo[m-R][n-R]+topo[m-2*R][n]+topo[m][n]+topo[m-R][n+R])/4+random;
}
else
{
topo[m-R][n]=(topo[m-R][n-R]+topo[m][n]+topo[m-R][n+R])/3+random;
}
}
if (topo[m+R][n]==temp)
{
random=2*randmax*rand()/RAND_MAX-randmax;
if ((m+R)!=(ntotal-1))
{
topo[m+R][n]=(topo[m+R][n-R]+topo[m][n]+topo[m+2*R][n]+topo[m+R][n+R])/4+random;
}
else
{
topo[m+R][n]=(topo[m+R][n-R]+topo[m][n]+topo[m+R][n+R])/3+random;
}
}
if (topo[m][n+R]==temp)
{
random=2*randmax*rand()/RAND_MAX-randmax;
if ((n+R)!=(ntotal-1))
{
topo[m][n+R]=(topo[m][n]+topo[m-R][n+R]+topo[m+R][n+R]+topo[m][n+2*R])/4+random;
}
else
{
topo[m][n+R]=(topo[m][n]+topo[m-R][n+R]+topo[m+R][n+R])/3+random;
}
}
}
randmax=pow(2.0,-H)*randmax;
}
for (int j=0; j<ntotal; j++)//按预设区域剪裁数组
{
for (int i=0; i<ntotal; i++)
{
if (i<xnum&&j<ynum)
{
topo_range[i][j]=topo[i][j];
}
}
}
return 0;
}
int FracTopo::topo_gener_random()
{
int i,j,m,n,R,jmax;
double temp = 1e+10;
double random;
dx=dy=ceil(sqrt((xmax-xmin)*(ymax-ymin)/random_num));//按照随机点数预估节点步长
xnum = (xmax-xmin)/dx+1;
ynum = (ymax-ymin)/dy+1;
xmax = xmin+(xnum-1)*dx;
ymax = ymin+(ynum-1)*dy;
order_x = ceil(log(xnum)/log(2));
order_y = ceil(log(ynum)/log(2));
imax = max(order_x,order_y);
ntotal = (int)pow(2.0,imax)+1; //总数据点数为2的imax次方加1
topo = new double* [ntotal];//开辟二维动态数组
for(i=0; i<ntotal; i++) topo[i] = new double [ntotal];
topo_random = new posi [random_num];
for (i=0; i<ntotal; i++)//设定地形数据初始值,角点数据必须给出
{
for (j=0; j<ntotal; j++)
{
if (i==0&&j==0)
{
topo[i][j]=ld;//角点初始值;
}
else if (i==ntotal-1&&j==0)
{
topo[i][j]=lu;//角点初始值;
}
else if (i==0&&j==ntotal-1)
{
topo[i][j]=rd;//角点初始值;
}
else if (i==ntotal-1&&j==ntotal-1)
{
topo[i][j]=ru;//角点初始值;
}
else
{
topo[i][j]=temp;//非角点数值初始化为1e10
}
}
}
srand((unsigned) time(NULL));
for (i=1; i<=imax; i++)//开始迭代,生成正方形区域随机地形
{
R=int(double(ntotal-1)/pow(2.0,i));
jmax=int(pow(4.0,i-1));
for (j=1; j<=jmax; j++)
{
random=2*randmax*rand()/RAND_MAX-randmax;
m=2*R*(j-(ceil((double)j/pow(2.0,i-1))-1) * pow(2.0,i-1))-R;
n=2*R*(ceil((double)j/pow(2.0,i-1)))-R;
topo[m][n]=(topo[m-R][n-R]+topo[m+R][n-R]+topo[m-R][n+R]+topo[m+R][n+R])/4+random;
}
for (j=1; j<=jmax; j++)
{
m=2*R*(j-(ceil((double)j/pow(2.0,i-1))-1)* pow(2.0,i-1))-R;
n=2*R*(ceil((double)j/pow(2.0,i-1)))-R;
if (topo[m][n-R]==temp)
{
random=2*randmax*rand()/RAND_MAX-randmax;
if ((n-R)!=0)
{
topo[m][n-R]=(topo[m][n-2*R]+topo[m-R][n-R]+topo[m+R][n-R]+topo[m][n])/4+random;
}
else
{
topo[m][n-R]=(topo[m-R][n-R]+topo[m+R][n-R]+topo[m][n])/3+random;
}
}
if (topo[m-R][n]==temp)
{
random=2*randmax*rand()/RAND_MAX-randmax;
if ((m-R)!=0)
{
topo[m-R][n]=(topo[m-R][n-R]+topo[m-2*R][n]+topo[m][n]+topo[m-R][n+R])/4+random;
}
else
{
topo[m-R][n]=(topo[m-R][n-R]+topo[m][n]+topo[m-R][n+R])/3+random;
}
}
if (topo[m+R][n]==temp)
{
random=2*randmax*rand()/RAND_MAX-randmax;
if ((m+R)!=(ntotal-1))
{
topo[m+R][n]=(topo[m+R][n-R]+topo[m][n]+topo[m+2*R][n]+topo[m+R][n+R])/4+random;
}
else
{
topo[m+R][n]=(topo[m+R][n-R]+topo[m][n]+topo[m+R][n+R])/3+random;
}
}
if (topo[m][n+R]==temp)
{
random=2*randmax*rand()/RAND_MAX-randmax;
if ((n+R)!=(ntotal-1))
{
topo[m][n+R]=(topo[m][n]+topo[m-R][n+R]+topo[m+R][n+R]+topo[m][n+2*R])/4+random;
}
else
{
topo[m][n+R]=(topo[m][n]+topo[m-R][n+R]+topo[m+R][n+R])/3+random;
}
}
}
randmax=pow(2.0,-H)*randmax;
}
srand((unsigned) time(NULL));
int temp_m,temp_n;
double len_ld,len_lu,len_ru,len_rd;
for (int i = 0; i < random_num; i++)
{
topo_random[i].x = (xmax-xmin)*rand()/RAND_MAX+xmin;
topo_random[i].y = (ymax-ymin)*rand()/RAND_MAX+ymin;
temp_m = floor((topo_random[i].x - xmin)/dx);
temp_n = floor((topo_random[i].y - ymin)/dy);
len_ld = sqrt(pow(xmin+dx*temp_m - topo_random[i].x,2)+pow(ymin+dy*temp_n - topo_random[i].y,2));
len_lu = sqrt(pow(xmin+dx*temp_m - topo_random[i].x,2)+pow(ymin+dy*(temp_n+1) - topo_random[i].y,2));
len_ru = sqrt(pow(xmin+dx*(temp_m+1) - topo_random[i].x,2)+pow(ymin+dy*(temp_n+1) - topo_random[i].y,2));
len_rd = sqrt(pow(xmin+dx*(temp_m+1) - topo_random[i].x,2)+pow(ymin+dy*temp_n - topo_random[i].y,2));
topo_random[i].v = (topo[temp_m][temp_n]*len_ld+topo[temp_m+1][temp_n]*len_rd+topo[temp_m+1][temp_n+1]*len_ru+topo[temp_m][temp_n+1]*len_lu)/(len_lu+len_ru+len_rd+len_ld);
}
return 0;
}
int FracTopo::output_regular()
{
time_t now = time(0);
char* dt = ctime(&now);
cout << "# A fractal terrain data generated by fractopo on " << dt;
cout << "# For more information please connect zhangyi.cugwuhan@gmail.com" << endl;
cout << "# use -h option to see help information" << endl;
cout << "# range=" << xmin << "/" << dx << "/" << xmax << "/" << ymin << "/" << dy << "/" << ymax << endl;
cout << "# x y elevation" << endl;
for (int j=0; j<ynum; j++)
{
for (int i=0; i<xnum; i++)
{
cout<<xmin+i*dx<<" "<<ymin+j*dy<<" "<<topo_range[i][j]<<endl;
}
}
return 0;
}
int FracTopo::output_random()
{
time_t now = time(0);
char* dt = ctime(&now);
cout << "# A fractal terrain data generated by fractopo on " << dt;
cout << "# For more information please connect zhangyi.cugwuhan@gmail.com" << endl;
cout << "# use -h option to see help information" << endl;
cout << "# x y elevation" << endl;
for (int i = 0; i < random_num; i++)
{
cout<<topo_random[i].x<<" "<<topo_random[i].y<<" "<<topo_random[i].v<<endl;
}
return 0;
}
int FracTopo::gauss_filter_regular()
{
for (int j=0; j<ynum; j++)
{
for (int i=0; i<xnum; i++)
{
topo_range[i][j] *= ElliFilter(ifarctan,xmin+i*dx,ymin+j*dy,filter_N);
}
}
return 0;
}
int FracTopo::gauss_filter_random()
{
for (int i = 0; i < random_num; i++)
{
topo_random[i].v *= ElliFilter(ifarctan,topo_random[i].x,topo_random[i].y,filter_N);
}
return 0;
}
int FracTopo::run(double* range,double* topo,double* para,double* gauss,char* filterType)
{
if (!strcmp(filterType,"pow"))
ifarctan = 0;
else if (!strcmp(filterType,"atan"))
ifarctan = 1;
if (*(range+5)==1e+30)
{
xmin = *range; xmax = *(range+1);
ymin = *(range+2); ymax = *(range+3);
random_num = *(range+4);
ld = *topo; lu = *(topo+1); ru = *(topo+2); rd = *(topo+3);
H = *para; randmax = *(para+1);
topo_gener_random();
if (*gauss!=1e+30)
{
filter_N.muX = *gauss;
filter_N.muY = *(gauss+1);
filter_N.sigma = *(gauss+2);
filter_N.theta = *(gauss+3);
filter_N.Lx = *(gauss+4);
filter_N.Ly = *(gauss+5);
filter_N.magnify = *(gauss+6);
filter_N.rho = *(gauss+7);
gauss_filter_random();
}
output_random();
}
else
{
xmin = *range; xmax = *(range+1);
ymin = *(range+2); ymax = *(range+3);
dx = *(range+4); dy = *(range+5);
ld = *topo; lu = *(topo+1); ru = *(topo+2); rd = *(topo+3);
H = *para; randmax = *(para+1);
topo_gener_regular();
if (*gauss!=1e+30)
{
filter_N.muX = *gauss;
filter_N.muY = *(gauss+1);
filter_N.sigma = *(gauss+2);
filter_N.theta = *(gauss+3);
filter_N.Lx = *(gauss+4);
filter_N.Ly = *(gauss+5);
filter_N.magnify = *(gauss+6);
filter_N.rho = *(gauss+7);
gauss_filter_regular();
}
output_regular();
}
return 0;
}
#endif

384
archive/fractopo/dispHelp.h Normal file
View File

@@ -0,0 +1,384 @@
#ifndef _DISPHELP_H
#define _DISPHELP_H
#include <iostream>
#include <sstream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <iomanip>
#include <sys/ioctl.h>
#include "vector"
using namespace std;
typedef vector<string> strArray;
struct option
{
string flag_s,flag_l;
string message;
strArray sec_message;
option()
{
flag_s = flag_l = message = "";
}
};
typedef vector<option> opArray;
class dispHelp
{
public:
dispHelp(){
front_space = 0;
back_space = 10;
ex_name = "Execuable";
version = "0.0.1";
descript = "Brief information about this command.";
author = "Author's information.";
}
~dispHelp(){}
void addHeadInfo(string,string,string,string);
void addUsage(string);
void addOption(string,string,string);
void addOptionSec(string,int);
void addExample(string);
void changeLayerOut(int,int);
void show();
private:
string ex_name,version,descript,author;
int front_space,back_space;
opArray options;
strArray examples;
strArray usages;
};
void dispHelp::addHeadInfo(string s1,string s2,string s3,string s4)
{
ex_name = s1; version = s2; descript = s3; author = s4;
return;
}
void dispHelp::addUsage(string usg)
{
usages.push_back(usg);
return;
}
void dispHelp::addOption(string msg,string sflag,string lflag = "")
{
option tmp_option;
tmp_option.message = msg; tmp_option.flag_s = sflag; tmp_option.flag_l = lflag;
options.push_back(tmp_option);
return;
}
void dispHelp::addOptionSec(string msg,int index = -1)
{
if (index < 0)
{
options.back().sec_message.push_back(msg);
}
else options[index].sec_message.push_back(msg);
return;
}
void dispHelp::addExample(string ex)
{
examples.push_back(ex);
return;
}
void dispHelp::changeLayerOut(int left,int right)
{
front_space = left; back_space = right;
return;
}
void dispHelp::show()
{
int line_length;
string segment,full_message;
stringstream ss_message;
//获取终端窗口的行列数
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
//显示头信息
full_message = ex_name + " " + version + " - " + descript;
ss_message.clear(); ss_message.str(full_message);
line_length = front_space + back_space;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space + back_space))
{
for (int i = 0; i < front_space; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+4; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+9+front_space+back_space);
}
}
clog << endl;
ss_message.clear(); ss_message.str(author);
line_length = front_space + back_space;;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space + back_space))
{
for (int i = 0; i < front_space; i++) clog << " ";
clog << "Author: " << segment << " ";
line_length += (segment.length()+9);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+4; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+9+front_space+back_space);
}
}
clog << endl;
if (!usages.empty())
{
for (int i = 0; i < front_space; i++) clog << " ";
clog << "Usage:" << endl;
for (int i = 0; i < usages.size(); i++)
{
ss_message.clear(); ss_message.str(usages[i]);
line_length = front_space + back_space + 4;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space+back_space+4))
{
for (int i = 0; i < front_space+4; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+9; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+10+front_space+back_space);
}
}
clog << endl;
}
}
if (!options.empty())
{
for (int i = 0; i < front_space; i++) clog << " ";
clog << "Options:" << endl;
for (int i = 0; i < options.size(); i++)
{
if (options[i].flag_l == "")
{
full_message = options[i].flag_s+" : "+options[i].message;
ss_message.clear(); ss_message.str(full_message);
line_length = front_space + back_space + 4;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space+back_space+4))
{
for (int i = 0; i < front_space+4; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+9; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+10+front_space+back_space);
}
}
clog << endl;
if (!options[i].sec_message.empty())
{
for (int j = 0; j < options[i].sec_message.size(); j++)
{
ss_message.clear(); ss_message.str(options[i].sec_message[j]);
line_length = front_space + back_space + 9;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space+back_space+9))
{
for (int i = 0; i < front_space+9; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+13; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+14+front_space+back_space);
}
}
clog << endl;
}
}
}
else
{
full_message = options[i].flag_s+" | "+options[i].flag_l+" : "+options[i].message;
ss_message.clear(); ss_message.str(full_message);
line_length = front_space + back_space + 4;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space+back_space+4))
{
for (int i = 0; i < front_space+4; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+9; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+10+front_space+back_space);
}
}
clog << endl;
if (!options[i].sec_message.empty())
{
for (int j = 0; j < options[i].sec_message.size(); j++)
{
ss_message.clear(); ss_message.str(options[i].sec_message[j]);
line_length = front_space + back_space + 9;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space+back_space+9))
{
for (int i = 0; i < front_space+9; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+13; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+14+front_space+back_space);
}
}
clog << endl;
}
}
}
}
}
if (!examples.empty())
{
for (int i = 0; i < front_space; i++) clog << " ";
clog << "Examples:" << endl;
for (int i = 0; i < examples.size(); i++)
{
ss_message.clear(); ss_message.str(examples[i]);
line_length = front_space + back_space + 4;
while(ss_message >> segment)
{
if ((line_length+segment.length()+1) <= w.ws_col)
{
if (line_length == (front_space+back_space+4))
{
for (int i = 0; i < front_space+4; i++) clog << " ";
clog << segment << " ";
line_length += (segment.length()+1);
}
else
{
clog << segment << " ";
line_length += (segment.length()+1);
}
}
else
{
clog << endl;
for (int i = 0; i < front_space+9; i++) clog << " ";
clog << segment << " ";
line_length = (segment.length()+10+front_space+back_space);
}
}
clog << endl;
}
}
return;
}
#endif

167
archive/fractopo/main.cpp Normal file
View File

@@ -0,0 +1,167 @@
#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;
}

15
archive/fractopo/makefile Normal file
View File

@@ -0,0 +1,15 @@
CC = g++-8
PROM = /usr/local/sbin/fractopo
CFLAGS = -I.
DEPS = $(shell find . -name "*.h")
SRC = $(shell find . -name "*.cpp")
OBJ = $(SRC:%.cpp=%.o)
$(PROM): $(OBJ)
$(CC) -o $(PROM) $(OBJ) $(CFLAGS)
%.o:%.cpp $(DEPS)
$(CC) -c $< -o $@ $(CFLAGS)
clean:
rm -rf $(OBJ)