initial upload
This commit is contained in:
291
archive/grd2msh/func.h
Normal file
291
archive/grd2msh/func.h
Normal file
@@ -0,0 +1,291 @@
|
||||
#ifndef _FUNC_H
|
||||
#define _FUNC_H
|
||||
#include "struct.h"
|
||||
|
||||
class func
|
||||
{
|
||||
public:
|
||||
func();
|
||||
~func();
|
||||
int run(char*,char*,char*,double,int,double,double,int,int);
|
||||
int readgrd(char*);
|
||||
int readxyz(char*,double,double);
|
||||
int readxyz(char*);
|
||||
int changeToSph(char*);
|
||||
int outmsh(char*,double);
|
||||
private:
|
||||
posi* topo;
|
||||
int M,N;
|
||||
int posi_num;
|
||||
|
||||
PosiList list_posi;
|
||||
PosiList::iterator ip;
|
||||
};
|
||||
|
||||
func::func()
|
||||
{
|
||||
topo = NULL;
|
||||
}
|
||||
|
||||
func::~func()
|
||||
{
|
||||
if(topo!=NULL) delete[] topo;
|
||||
if(!list_posi.empty()) list_posi.clear();
|
||||
}
|
||||
|
||||
int func::run(char* filename,char* mshname,char* refsys,double factor,int type,double dx,double dy,int input_N,int input_M)
|
||||
{
|
||||
if (type == 1)
|
||||
{
|
||||
readgrd(filename);
|
||||
}
|
||||
else if (type == 2)
|
||||
{
|
||||
if (input_N==-1)
|
||||
{
|
||||
readxyz(filename,dx,dy);
|
||||
}
|
||||
else
|
||||
{
|
||||
readxyz(filename);
|
||||
M = input_M;
|
||||
N = input_N;
|
||||
}
|
||||
}
|
||||
if(strcmp(refsys,"NULL"))
|
||||
changeToSph(refsys);
|
||||
outmsh(mshname,factor);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int func::readgrd(char* inname)
|
||||
{
|
||||
double xmin,xmax,ymin,ymax,zmin,zmax;
|
||||
double dx,dy;
|
||||
string head_info = "";
|
||||
ifstream infile(inname);
|
||||
if (!infile)
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<inname<<" not found..."<<endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
infile>>head_info;
|
||||
if (head_info!="DSAA")
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<"The format of "<<inname<<" is not supported..."<<endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
infile>>N>>M>>xmin>>xmax>>ymin>>ymax>>zmin>>zmax;
|
||||
dx = (xmax-xmin)/(N-1);
|
||||
dy = (ymax-ymin)/(M-1);
|
||||
posi_num = M*N;
|
||||
topo = new posi [posi_num];
|
||||
for (int i = 0; i < posi_num; ++i)
|
||||
{
|
||||
topo[i].id = i+1;
|
||||
infile>>topo[i].z;
|
||||
topo[i].alti = topo[i].z;
|
||||
topo[i].x = xmin + dx*(i%N);
|
||||
topo[i].y = ymin + dy*(i/N);
|
||||
}
|
||||
infile.close();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int func::readxyz(char* inname,double dx,double dy)
|
||||
{
|
||||
int count;
|
||||
posi temp_posi;
|
||||
string line;
|
||||
stringstream sline;
|
||||
double xmax,ymax;
|
||||
double xmin,ymin;
|
||||
|
||||
ifstream infile(inname);
|
||||
if (!infile)
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<inname<<" not found..."<<endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
xmax = ymax = -1e+30;
|
||||
xmin = ymin = 1e+30;
|
||||
count = 1;
|
||||
while(getline(infile,line))
|
||||
{
|
||||
if (*(line.begin()) == '#') continue;
|
||||
if (line!="")
|
||||
{
|
||||
sline.clear();
|
||||
sline.str(line);
|
||||
temp_posi.id = count;
|
||||
sline >> temp_posi.x >> temp_posi.y >> temp_posi.z;
|
||||
list_posi.push_back(temp_posi);
|
||||
count++;
|
||||
|
||||
if (temp_posi.x>xmax) xmax = temp_posi.x;
|
||||
if (temp_posi.y>ymax) ymax = temp_posi.y;
|
||||
if (temp_posi.x<xmin) xmin = temp_posi.x;
|
||||
if (temp_posi.y<ymin) ymin = temp_posi.y;
|
||||
}
|
||||
}
|
||||
infile.close();
|
||||
|
||||
M = (ymax-ymin)/dy+1;
|
||||
N = (xmax-xmin)/dx+1;
|
||||
|
||||
posi_num = list_posi.size();
|
||||
topo = new posi [posi_num];
|
||||
|
||||
count = 0;
|
||||
for(ip=list_posi.begin();ip!=list_posi.end();++ip)
|
||||
{
|
||||
temp_posi = *ip;
|
||||
topo[count].id = temp_posi.id;
|
||||
topo[count].x = temp_posi.x;
|
||||
topo[count].y = temp_posi.y;
|
||||
topo[count].z = temp_posi.z;
|
||||
topo[count].alti = topo[count].z;
|
||||
count++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int func::readxyz(char* inname)
|
||||
{
|
||||
int count;
|
||||
posi temp_posi;
|
||||
string line;
|
||||
stringstream sline;
|
||||
double xmax,ymax;
|
||||
double xmin,ymin;
|
||||
|
||||
ifstream infile(inname);
|
||||
if (!infile)
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<inname<<" not found..."<<endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
count = 1;
|
||||
while(getline(infile,line))
|
||||
{
|
||||
if (*(line.begin()) == '#') continue;
|
||||
if (line!="")
|
||||
{
|
||||
sline.clear();
|
||||
sline.str(line);
|
||||
temp_posi.id = count;
|
||||
sline >> temp_posi.x >> temp_posi.y >> temp_posi.z;
|
||||
list_posi.push_back(temp_posi);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
infile.close();
|
||||
|
||||
posi_num = list_posi.size();
|
||||
topo = new posi [posi_num];
|
||||
|
||||
count = 0;
|
||||
for(ip=list_posi.begin();ip!=list_posi.end();++ip)
|
||||
{
|
||||
temp_posi = *ip;
|
||||
topo[count].id = temp_posi.id;
|
||||
topo[count].x = temp_posi.x;
|
||||
topo[count].y = temp_posi.y;
|
||||
topo[count].z = temp_posi.z;
|
||||
topo[count].alti = topo[count].z;
|
||||
count++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int func::changeToSph(char* refer)
|
||||
{
|
||||
double refr,refR;
|
||||
if (!strcmp(refer,"WGS84"))
|
||||
{
|
||||
refr = WGS84_PoleRadius;
|
||||
refR = WGS84_EquatorRadius;
|
||||
}
|
||||
else if (!strcmp(refer,"EarthR"))
|
||||
{
|
||||
refr = EarthRadius;
|
||||
refR = EarthRadius;
|
||||
}
|
||||
else if (2 != sscanf(refer,"%lf/%lf",&refr,&refR))
|
||||
{
|
||||
cout << BOLDRED <<"==> "<< RESET <<" wrong reference system: "<<refer<<endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
double lon,lat,rad;
|
||||
for (int i = 0; i < posi_num; ++i)
|
||||
{
|
||||
lon = topo[i].x;
|
||||
lat = topo[i].y;
|
||||
rad = topo[i].z + refRadius(lat,refr,refR);
|
||||
|
||||
topo[i].x = rad*sin((0.5 - lat/180.0)*Pi)*cos((2.0 + lon/180.0)*Pi);
|
||||
topo[i].y = rad*sin((0.5 - lat/180.0)*Pi)*sin((2.0 + lon/180.0)*Pi);
|
||||
topo[i].z = rad*cos((0.5 - lat/180.0)*Pi);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int func::outmsh(char* outname,double magify)
|
||||
{
|
||||
ofstream outfile(outname);
|
||||
if (!outfile)
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<"can not create "<<outname<<endl;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
outfile<<"$MeshFormat"<<endl<<"2.2 0 8"<<endl<<"$EndMeshFormat"<<endl<<"$Nodes"<<endl<<posi_num<<endl;
|
||||
for (int i = 0; i < posi_num; ++i)
|
||||
{
|
||||
outfile<<topo[i].id<<" "<<topo[i].x<<" "<<topo[i].y<<" "<<magify*topo[i].z<<endl;
|
||||
}
|
||||
outfile<<"$EndNodes"<<endl<<"$Elements"<<endl<<(M-1)*(N-1)*2<<endl;
|
||||
|
||||
int element_count = 1;
|
||||
for(int i=1;i<=M-1;i++)
|
||||
{
|
||||
for(int j=1;j<=N-1;j++)
|
||||
{
|
||||
outfile<<element_count<<" 2 1 10 "<<(i-1)*N+j<<" "<<(i-1)*N+j+1<<" "<<i*N+j+1<<endl;
|
||||
element_count++;
|
||||
}
|
||||
}
|
||||
for(int i=M;i>=2;i--)
|
||||
{
|
||||
for(int j=N;j>=2;j--)
|
||||
{
|
||||
outfile<<element_count<<" 2 1 10 "<<(i-1)*N+j<<" "<<(i-1)*N+j-1<<" "<<(i-2)*N+j-1<<endl;
|
||||
element_count++;
|
||||
}
|
||||
}
|
||||
outfile<<"$EndElements"<<endl<<"$NodeData"<<endl;
|
||||
outfile<<1<<endl<<"\"GRD TOPO\""<<endl<<1<<endl<<0.0<<endl<<3<<endl<<0<<endl<<1<<endl<<posi_num<<endl;
|
||||
for (int i = 0; i < posi_num; ++i)
|
||||
{
|
||||
outfile<<i+1<<" "<<setprecision(16)<<topo[i].alti<<endl;
|
||||
}
|
||||
outfile<<"$EndNodeData";
|
||||
outfile.close();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#endif
|
139
archive/grd2msh/main.cpp
Normal file
139
archive/grd2msh/main.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
#include "func.h"
|
||||
|
||||
void disp_help()
|
||||
{
|
||||
cout
|
||||
<<"grd2msh 1.0 - convert a Surfer6's grd file or a xyz file into Gmsh .msh file for 3D Mapping"<<endl<<endl
|
||||
<<"syntax: grd2msh <grd_file>|{<xyz_file> -i<x_interval>/<y_interval>}|{<xyz_file> -I<N>/<M>} [-rWGS84|EarthR|<ref-r>/<ref-R>] [-m<magnify_factor>] [-o<msh_file>]"<<endl
|
||||
<<"-i\tintervals in x and y directions for the xyz file"<<endl
|
||||
<<"-I\tmartax number in x and y directions for the xyz file"<<endl
|
||||
<<"-r\treference system of the input file, the data will be taken as altitudes with respect to the reference system"<<endl
|
||||
<<"-m\tmagnify factor of z values of the grid data"<<endl
|
||||
<<"-o\tGmsh's mesh file name. The input filename will be used if -o is absent"<<endl<<endl
|
||||
<<"example: grd2msh test.grd -m1.0 -otest.msh"<<endl;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char command_type[1024] = {0};
|
||||
char inputname[1024] = {0};
|
||||
char mshname[1024] = {0};
|
||||
char nametype[1024] = {0};
|
||||
char refSystem[1024] = "NULL";
|
||||
double mg_factor = 1.0;
|
||||
double dx = 0,dy = 0;
|
||||
int n_num = -1, m_num = -1;
|
||||
int Run = 1;
|
||||
int filetype;
|
||||
|
||||
if (argc==1)
|
||||
{
|
||||
disp_help();
|
||||
}
|
||||
else
|
||||
{
|
||||
sscanf(argv[1],"%[^.]%s",inputname,nametype);//按格式读入文件名与扩展名
|
||||
if(!strcmp(inputname,""))//检查文件名是否为空
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<argv[1]<<endl;
|
||||
Run = 0;
|
||||
}
|
||||
|
||||
if (!strcmp(nametype,".grd"))
|
||||
{
|
||||
filetype = 1;
|
||||
sscanf(argv[1],"%s",inputname);
|
||||
}
|
||||
else if (!strcmp(nametype,".dat")||!strcmp(nametype,".xyz"))
|
||||
{
|
||||
filetype = 2;
|
||||
sscanf(argv[1],"%s",inputname);
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<"unrecognized file type: "<<nametype<<endl;
|
||||
Run = 0;
|
||||
}
|
||||
|
||||
for (int i = 2; i < argc; i++)
|
||||
{
|
||||
sscanf(argv[i],"%2s",command_type);
|
||||
if (!strcmp(command_type,MAGNIFY))
|
||||
{
|
||||
if (1!=sscanf(argv[i],"%*2s%lf",&mg_factor))
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<argv[i]<<endl;
|
||||
Run = 0;
|
||||
}
|
||||
}
|
||||
else if(!strcmp(command_type,OUTMSH))//命令为文件名设置符
|
||||
{
|
||||
sscanf(argv[i],"%*2s%s",mshname);//按格式读入文件名与扩展名
|
||||
if(!strcmp(mshname,""))//检查文件名是否为空
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<argv[i]<<endl;
|
||||
Run = 0;
|
||||
}
|
||||
}
|
||||
else if (!strcmp(command_type,INTERVAL))
|
||||
{
|
||||
if(2!=sscanf(argv[i],"%*2s%lf/%lf",&dx,&dy))
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<argv[i]<<endl;
|
||||
Run = 0;
|
||||
}
|
||||
}
|
||||
else if (!strcmp(command_type,HL))
|
||||
{
|
||||
if(2!=sscanf(argv[i],"%*2s%d/%d",&n_num,&m_num))
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<argv[i]<<endl;
|
||||
Run = 0;
|
||||
}
|
||||
}
|
||||
else if (!strcmp(command_type,REFER))
|
||||
{
|
||||
if(1!=sscanf(argv[i],"%*2s%s",refSystem))
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<"bad syntax: "<<argv[i]<<endl;
|
||||
Run = 0;
|
||||
}
|
||||
}
|
||||
else //未定义的命令符
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<"unrecognized syntax: "<<argv[i]<<endl;
|
||||
Run = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!strcmp(inputname,""))
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<"error: bad syntax"<<endl;
|
||||
Run = 0;
|
||||
}
|
||||
else if (!strcmp(mshname,""))
|
||||
{
|
||||
char temp[1024] = {0};
|
||||
char temp2[1024] = {0};
|
||||
sscanf(inputname,"%[^.]%s",temp,temp2);
|
||||
strcpy(mshname,temp);
|
||||
strcat(mshname,".msh");
|
||||
}
|
||||
|
||||
if ((filetype==2&&dx==0)||(filetype==2&&dy==0))
|
||||
{
|
||||
if ((filetype==2&&n_num==-1)||(filetype==2&&m_num==-1))
|
||||
{
|
||||
cout<<BOLDRED<<"==> "<<RESET<<"error: bad syntax"<<endl;
|
||||
Run = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (Run)
|
||||
{
|
||||
func F1;
|
||||
F1.run(inputname,mshname,refSystem,mg_factor,filetype,dx,dy,n_num,m_num);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
39
archive/grd2msh/struct.h
Normal file
39
archive/grd2msh/struct.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef _STRUCT_H
|
||||
#define _STRUCT_H
|
||||
#include "stdio.h"
|
||||
#include "iostream"
|
||||
#include "iomanip"
|
||||
#include "fstream"
|
||||
#include "string.h"
|
||||
#include "sstream"
|
||||
#include "list"
|
||||
#include "cmath"
|
||||
#define RESET "\033[0m" /* reset */
|
||||
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
|
||||
#define INTERVAL "-i"
|
||||
#define HL "-I"
|
||||
#define REFER "-r"
|
||||
#define OUTMSH "-o"
|
||||
#define MAGNIFY "-m"
|
||||
#define Pi (4.0*atan(1.0))
|
||||
#define WGS84_PoleRadius 6356752.3//WGS84椭球极半径
|
||||
#define WGS84_EquatorRadius 6378137//WGS84椭球长半径
|
||||
#define EarthRadius 6371008.8
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct posi
|
||||
{
|
||||
int id;
|
||||
double x,y,z;
|
||||
double alti;
|
||||
};
|
||||
|
||||
typedef list<posi> PosiList;
|
||||
|
||||
//计算一个参考椭球或者参考球在纬度位置的半径
|
||||
double refRadius(double lati,double refr,double refR)
|
||||
{
|
||||
return refr*refR/sqrt(pow(refr,2)*pow(cos((double) lati*Pi/180.0),2)+pow(refR,2)*pow(sin((double) lati*Pi/180.0),2));
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user