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/mshinterpolate/.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,228 @@
#ifndef _DATASTRUCT_H
#define _DATASTRUCT_H
#include "sysDefine.h"
//直角坐标系下的一个点
struct cpoint
{
double x,y,z;
cpoint(){x = y = z = MAX_DBL;}
};
typedef vector<cpoint> cpointArray;
//直角坐标点的一些数学运算
cpoint operator -(cpoint a, cpoint b)
{
cpoint m;
m.x=a.x-b.x;
m.y=a.y-b.y;
m.z=a.z-b.z;
return m;
}
cpoint operator +(cpoint a, cpoint b) //矢量加法
{
cpoint m;
m.x=a.x+b.x;
m.y=a.y+b.y;
m.z=a.z+b.z;
return m;
}
cpoint operator *(double sign,cpoint b) //矢量乘法
{
cpoint m;
m.x=sign*b.x;
m.y=sign*b.y;
m.z=sign*b.z;
return m;
}
//重载逻辑等操作符作用于矢量,判断两个直角点是否相等
bool operator ==(cpoint a, cpoint b)
{
if(fabs(a.x-b.x)<ZERO&&fabs(a.y-b.y)<ZERO&&fabs(a.z-b.z)<ZERO)
{
return 1;
}
else return 0;
}
double dot(cpoint a, cpoint b) //矢量点乘
{
return a.x*b.x+a.y*b.y+a.z*b.z;
}
cpoint cross(cpoint a,cpoint b) //矢量叉乘
{
cpoint v;
v.x = a.y*b.z-a.z*b.y;
v.y = a.z*b.x-a.x*b.z;
v.z = a.x*b.y-a.y*b.x;
return v;
}
//返回两个直角坐标点的中点位置
cpoint middleCpoint(cpoint a,cpoint b)
{
cpoint c;
c.x = 0.5*(a.x + b.x);
c.y = 0.5*(a.y + b.y);
c.z = 0.5*(a.z + b.z);
return c;
}
//返回两点之间的一个点 以第一个点为参考点 第三个参数为相对于原线段的比例
cpoint scaleCpoint(cpoint a,cpoint b,double scale)
{
cpoint c;
c.x = a.x + (b.x - a.x)*scale;
c.y = a.y + (b.y - a.y)*scale;
c.z = a.z + (b.z - a.z)*scale;
return c;
}
cpoint rescaleCpoint(cpoint a,double refr)
{
cpoint c;
double m = sqrt(a.x*a.x+a.y*a.y+a.z*a.z);
c.x = a.x*refr/m;
c.y = a.y*refr/m;
c.z = a.z*refr/m;
return c;
}
double module(cpoint a)
{
return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);
}
double distanceCpoint(cpoint a, cpoint b)
{
cpoint m;
double d;
m.x=a.x-b.x;
m.y=a.y-b.y;
m.z=a.z-b.z;
d = sqrt(m.x*m.x + m.y*m.y + m.z*m.z);
return d;
}
double sphAngle(cpoint a,cpoint b) //两个矢量的球心角 注意返回值为弧度
{
double m1,m2;
m1 = sqrt(a.x*a.x+a.y*a.y+a.z*a.z);
m2 = sqrt(b.x*b.x+b.y*b.y+b.z*b.z);
return acos((a.x*b.x+a.y*b.y+a.z*b.z)/(m1*m2));
}
//球坐标系下的一个点
struct spoint
{
double lon,lat,rad;
spoint(){lon = lat = rad = MAX_DBL;}
};
typedef vector<spoint> spointArray;
/*直角坐标与球坐标相互转换函数 注意这里使用的球坐标是地理坐标范围 即经度为-180~180 纬度为-90~90*/
cpoint s2c(spoint s)
{
cpoint c;
c.x = s.rad*sin((0.5 - s.lat/180.0)*pi)*cos((2.0 + s.lon/180.0)*pi);
c.y = s.rad*sin((0.5 - s.lat/180.0)*pi)*sin((2.0 + s.lon/180.0)*pi);
c.z = s.rad*cos((0.5 - s.lat/180.0)*pi);
return c;
}
spoint c2s(cpoint c)
{
spoint s;
s.rad = sqrt(pow(c.x,2)+pow(c.y,2)+pow(c.z,2));
if (fabs(s.rad)<ZERO) //点距离原点极近 将点置于原点
{
s.lat = s.lon = s.rad = 0.0;
}
else
{
s.lat = 90.0 - acos(c.z/s.rad)*180.0/pi;
s.lon = atan2(c.y,c.x)*180.0/pi;
}
return s;
}
//顶点
struct vertex
{
int id; //索引
cpoint posic; //直角坐标系位置
spoint posis; //球坐标系位置
vertex()
{
id = -1; //初始化顶点索引值为-1 这里不需要初始化坐标位置 因为已经由相应的初始化函数完成了初始化
}
void set(int i) //设置索引值
{
id = i;
}
void set(cpoint c) //从直角坐标位置初始化
{
posic.x = c.x; posic.y = c.y; posic.z = c.z;
posis = c2s(posic);
}
void set(spoint s) //从球坐标位置初始化
{
posis.lon = s.lon; posis.lat = s.lat; posis.rad = s.rad;
posic = s2c(posis);
}
void info() //显示顶点信息
{
cout << id << " " << setprecision(16) << posic.x << " " << posic.y << " " << posic.z << " " << posis.lon << " " << posis.lat << " " << posis.rad << endl;
}
};
typedef vector<vertex> vertexArray;
typedef map<int,vertex> idMap; //顶点索引值映射 用于通过索引值寻找相应顶点
typedef map<string,vertex> strMap; //顶点位置映射 用于通过顶点位置寻找相应顶点
typedef map<int,int> outIdMap; //输出msh文件时重新索引三角形顶点集
//三角形信息结构体,包含三角形的三个顶点索引,逆时针排序
struct triangle
{
int id;
int vec[3];//三角形顶点
int phys; //三角形的物理属性组
triangle() //初始化顶点索引
{
phys = 0; //默认的物理属性组为0
vec[0] = vec[1] = vec[2] = -1;
}
};
typedef vector<triangle> triangleArray;
//矢量与平面的交点
cpoint lineOnPlane(cpoint c,cpoint normal,cpoint p)
{
cpoint m;
m.x = 0; m.y = 0; m.z = 0;
double t;
if (dot(normal,p) != 0) //平面与矢量平行
{
t = dot(normal,c)/dot(normal,p);
m.x += p.x*t;
m.y += p.y*t;
m.z += p.z*t;
}
return m;
}
//细分三角形面积比形式的三角形内插值函数
double triInterp_area(cpoint p,cpoint p1,cpoint p2,cpoint p3,double d1,double d2,double d3)
{
cpoint pp1 = p1 - p;
cpoint pp2 = p2 - p;
cpoint pp3 = p3 - p;
//三角形的面积等于叉乘的1/2 这里只计算比值 所以不需要乘以1/2
double a1 = module(cross(pp2,pp3));
double a2 = module(cross(pp3,pp1));
double a3 = module(cross(pp1,pp2));
return (d1*a1+d2*a2+d3*a3)/(a1+a2+a3);;
}
#endif

View File

@@ -0,0 +1,104 @@
#include "mshInterpolate.h"
void disp_help()
{
cout << "mshInterpolate - v0.1 msh file interpolation" << endl
<< "Author: zhangyi.cugwuhan@gmail.com" << endl << endl
<< "usage: mshInterpolate -i<input-msh> -d<data-name> -l<list-file> [-t<type>] [-s<refr>/<refR>] [-h] > out-file" << endl
<< "-i\tinput Gmsh file." << endl
<< "-d\tnode data name for interpolation." << endl
<< "-l\tlist table generated by the stt program" << endl
<< "-s\treference system, this must be set for topography interpolation" << endl
<< "-t\tinterpolation type, 'linear' for linear interpolation (default) and 'topo' for topography" << endl
<< "-h\tshow this info" << endl;
}
int main(int argc, char** argv)
{
char infilename[1024] = "NULL";
char nodename[1024] = "NULL";
char listname[1024] = "NULL";
char referSystem[1024] = "NULL";
char calType[1024] = "linear";
opterr = 0; //内置参数 若不为0则会在发生遭遇错误时输出一条信息到屏幕
int curr;
/*循环拾取参数 最后一个参数为-1 需要变量的参数后跟一个冒号 可有可无参数跟两个冒号*/
while((curr = getopt(argc,argv,"hi:d:l:s:t:")) != -1)
{
/*匹配命令*/
switch (curr)
{
case 'h': //显示帮助信息
disp_help();
break;
case 'i':
if (1!=sscanf(optarg,"%[^\0]",infilename))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case 'd':
if (1!=sscanf(optarg,"%[^\0]",nodename))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case 'l':
if (1!=sscanf(optarg,"%[^\0]",listname))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case 's':
if (1!=sscanf(optarg,"%[^\0]",referSystem))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case 't':
if (1!=sscanf(optarg,"%[^\0]",calType))
{
cout << "error ==> wrong format of " << optarg << endl;
}
break;
case '?': //处理未定义或错误参数
if (optopt == 'i' || optopt == 'd' || optopt == 'l' || optopt == 's' || optopt == 't')
{
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
return -1;
}
else if (isprint(optopt))
{
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
return -1;
}
else
{
fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
return -1;
}
break;
default:
abort();
}
}
mshInterpolate mi;
if (mi.readMsh(infilename,nodename)) return -1;
if (mi.readList(listname)) return -1;
if (!strcmp(calType,"linear"))
mi.interpolate_linear();
else if (!strcmp(calType,"topo"))
{
if (mi.initRef(referSystem)) return -1;
mi.interpolate_topo();
}
else
{
cout << "error ==> wrong interpolation type: " << calType << endl;
}
mi.outList();
return 0;
}

View File

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

View File

@@ -0,0 +1,224 @@
#ifndef _MSHINTERPOLATE_H
#define _MSHINTERPOLATE_H
#include "dataStruct.h"
class mshInterpolate
{
public:
mshInterpolate(){}
~mshInterpolate(){}
int routine();
int readMsh(char*,char*);
int readList(char*);
int initRef(char*);
void interpolate_topo();
void interpolate_linear();
int outList();
private:
vertexArray mshVert;
triangleArray mshTri;
_1dArray vertVal;
vertexArray listVert;
_1iArray listIds;
_1dArray listVal;
double refr,refR;
};
int mshInterpolate::readMsh(char* filename,char* valName)
{
int temp_int,ele_type,attri_num,temp_attri,temp_id;
double temp_val;
vertex temp_vert;
triangle temp_tri;
string temp_str;
stringstream temp_ss;
ifstream mshin;
if (open_infile(mshin,filename)) return -1;
while(getline(mshin,temp_str))
{
if (temp_str == "$Nodes")
{
getline(mshin,temp_str);
temp_ss.str("");
temp_ss.clear();
temp_ss << temp_str;
temp_ss >> temp_int;
for (int i = 0; i < temp_int; i++)
{
getline(mshin,temp_str);
temp_ss.str(""); temp_ss.clear(); temp_ss << temp_str;
temp_ss >> temp_vert.id >> temp_vert.posic.x >> temp_vert.posic.y >> temp_vert.posic.z;
temp_vert.set(temp_vert.posic);
mshVert.push_back(temp_vert);
}
//顶点读入完成 初始化数组
vertVal.resize(mshVert.size(),0.0);
}
else if (temp_str == "$Elements")
{
getline(mshin,temp_str);
temp_ss.str(""); temp_ss.clear(); temp_ss << temp_str;
temp_ss >> temp_int;
for (int i = 0; i < temp_int; i++)
{
getline(mshin,temp_str);
temp_ss.str(""); temp_ss.clear(); temp_ss << temp_str;
temp_ss >> temp_tri.id >> ele_type; //这里temp_int是元素的id
if (ele_type == 2) //只读入三角形
{
temp_ss >> attri_num;
//第一个属性为物理组
temp_ss >> temp_tri.phys;
for (int a = 0; a < attri_num-1; a++) //忽略后面的属性值
temp_ss >> temp_attri;
//读入三棱锥顶面三角形顶点索引
temp_ss >> temp_tri.vec[0]
>> temp_tri.vec[1]
>> temp_tri.vec[2];
mshTri.push_back(temp_tri);
}
}
}
else if (temp_str == "$NodeData")
{
for (int i = 0; i < 2; i++) //先读入元素块的名称 按照关键字解析不同类型的元素值
getline(mshin,temp_str);
if (!strcmp(temp_str.c_str(),valName))
{
for (int i = 0; i < 6; i++) //跳过元素属性前面的值 最后一次为当前元素块的个数
getline(mshin,temp_str);
temp_ss.str(""); temp_ss.clear(); temp_ss << temp_str;
temp_ss >> temp_int;
for (int i = 0; i < temp_int; i++)
{
getline(mshin,temp_str);
temp_ss.str(""); temp_ss.clear(); temp_ss << temp_str;
temp_ss >> temp_id >> temp_val;
vertVal[temp_id] = temp_val;
}
}
}
else continue;
}
mshin.close();
return 0;
}
int mshInterpolate::readList(char* filename)
{
int temp_id;
vertex temp_vert;
string temp_str;
stringstream temp_ss;
ifstream infile;
if (open_infile(infile,filename)) return -1;
while(getline(infile,temp_str))
{
if (*(temp_str.begin()) == '#') continue;
temp_ss.str(""); temp_ss.clear(); temp_ss << temp_str;
temp_ss >> temp_vert.posis.lon >> temp_vert.posis.lat >> temp_id;
temp_vert.posis.rad = defaultR;
temp_vert.set(temp_vert.posis);
listVert.push_back(temp_vert);
listIds.push_back(temp_id); //list id 可能不止一个 但是我们暂时只用一个就行了
}
infile.close();
return 0;
}
int mshInterpolate::initRef(char* para)
{
//首先匹配预定义类型
if (!strcmp(para,"WGS84"))
{
refr = pole_radius;
refR = equator_radius;
}
else if (!strcmp(para,"EarthRadius"))
{
refr = EarthRadius;
refR = EarthRadius;
}
//匹配格式化参数
else if (2 == sscanf(para,"%lf/%lf",&refr,&refR))
{
if (refr <= 0 || refR <= 0)
{
cout << BOLDRED << "Error ==> " << RESET << "fail to initial reference system" << endl;
return -1;
}
}
else
{
cout << BOLDRED << "Error ==> " << RESET << "fail to initial reference system" << endl;
return -1;
}
return 0;
}
void mshInterpolate::interpolate_topo()
{
vertex cross_point;
vertex temp_vert[3];
cpoint temp_nor;
listVal.resize(listVert.size(),MAX_DBL);
for (int i = 0; i < listVert.size(); i++)
{
for (int j = 0; j < 3; j++)
{
temp_vert[j] = mshVert[mshTri[listIds[i]].vec[j]];
temp_vert[j].posis.rad += vertVal[mshTri[listIds[i]].vec[j]];
temp_vert[j].set(temp_vert[j].posis);
}
temp_nor = cross(temp_vert[1].posic-temp_vert[0].posic,temp_vert[2].posic-temp_vert[0].posic);
cross_point.posic = lineOnPlane(temp_vert[0].posic,temp_nor,listVert[i].posic);
cross_point.set(cross_point.posic);
listVal[i] = cross_point.posis.rad - REF_r(cross_point.posis.lat,refr,refR);
}
return;
}
void mshInterpolate::interpolate_linear()
{
vertex cross_point;
vertex temp_vert[3];
cpoint temp_nor;
listVal.resize(listVert.size(),MAX_DBL);
for (int i = 0; i < listVert.size(); i++)
{
for (int j = 0; j < 3; j++)
{
temp_vert[j] = mshVert[mshTri[listIds[i]].vec[j]];
}
temp_nor = cross(temp_vert[1].posic-temp_vert[0].posic,temp_vert[2].posic-temp_vert[0].posic);
cross_point.posic = lineOnPlane(temp_vert[0].posic,temp_nor,listVert[i].posic);
cross_point.set(cross_point.posic);
listVal[i] = triInterp_area(cross_point.posic,temp_vert[0].posic,temp_vert[1].posic,temp_vert[2].posic,
vertVal[mshTri[listIds[i]].vec[0]],
vertVal[mshTri[listIds[i]].vec[1]],
vertVal[mshTri[listIds[i]].vec[2]]);
}
return;
}
int mshInterpolate::outList()
{
for (int i = 0; i < listVert.size(); i++)
{
cout << listVert[i].posis.lon << " " << listVert[i].posis.lat << " " << setprecision(16) << listVal[i] << endl;
}
return 0;
}
#endif

View File

@@ -0,0 +1,93 @@
#ifndef _SYSDEFINE_H
#define _SYSDEFINE_H
#include "iostream"
#include "fstream"
#include "sstream"
#include "string.h"
#include "cmath"
#include "iomanip"
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "vector"
#include "map"
#include "algorithm"
#include "ctime"
#define MAX_DBL 1.0e+30
#define MIN_BDL -1.0e+30
#define ZERO 1.0e-20
#define pole_radius 6351251.669//WGS84椭球极半径
#define equator_radius 6378137//WGS84椭球长半径
#define EarthRadius 6371000.8
#define pi (4.0*atan(1.0))
#define golden_mean (sqrt(5.0)+1)/2//黄金比例
#define defaultR 1e+5
#define BOLDRED "\033[1m\033[31m"
#define RESET "\033[0m"
using namespace std;
typedef vector<int> _1iArray;
typedef vector<vector<int> > _2iArray;
typedef vector<double> _1dArray;
typedef vector<vector<double> > _2dArray;
//操作计时
clock_t start,finish;
//以度计算的正弦函数
inline double sind(double degree)
{
return sin(degree*pi/180.0);
}
//以度计算的余弦函数
inline double cosd(double degree)
{
return cos(degree*pi/180.0);
}
//全局函数
int open_infile(ifstream &infile,char* filename)
{
infile.open(filename);
if (!infile)
{
cerr << BOLDRED << "error ==> " << RESET << "file not found: " << filename << endl;
return -1;
}
return 0;
}
int open_outfile(ofstream &outfile,char* filename)
{
outfile.open(filename);
if (!outfile)
{
cerr << BOLDRED << "error ==> " << RESET << "fail to create the file: " << filename << endl;
return -1;
}
return 0;
}
//计算一个参考椭球或者参考球在纬度位置的半径
double REF_r(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));
}
// 球面双线性插值函数 以度为单位的版本
double SphBiInterp_deg(double CoLat1,double CoLat2,double Lon1,double Lon2,double CoLat0,double Lon0,double h11,double h12,double h21,double h22)
{
double Delta=(Lon2-Lon1)*(cosd(CoLat2)-cosd(CoLat1));
double A=(Lon1*(h12-h22)+Lon2*(h21-h11))/Delta;
double B=(cosd(CoLat1)*(h21-h22)+cosd(CoLat2)*(h12-h11))/Delta;
double C=(h11+h22-h21-h12)/Delta;
double D=(Lon2*cosd(CoLat2)*h11-Lon2*cosd(CoLat1)*h21+Lon1*cosd(CoLat1)*h22-Lon1*cosd(CoLat2)*h12)/Delta;
double h0=A*cosd(CoLat0)+B*Lon0+C*Lon0*cosd(CoLat0)+D;
return h0;
}
#endif