gctl_toolkits/archive/Tesseroid_creater/command.h
2024-09-10 20:25:18 +08:00

152 lines
6.4 KiB
C++

#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
#include <stdlib.h>
#include "stdio.h"
#include <iomanip>
#include <cmath>
#include "list"
#define MAX 1e+30
#define pi (4.0*atan(1.0))
#define min(a,b)(((a) < (b)) ? (a) : (b))
#define OUTPUT "-o"
#define AFFECTION "-a"
#define PARA "-p"
#define BOLDRED "\033[1m\033[31m"
#define RESET "\033[0m"
using namespace std;
struct Tess
{
char name[1024];
int id;
double xmin,xmax,ymin,ymax,r,R;
};
typedef list<Tess> TessList;
struct point3d
{
double x,y,z;
};
point3d SCS2CCS(double thet,double phi,double radius)//球坐标转直角坐标
{
point3d v;
v.x = radius*sin((0.5-thet/180.0)*pi)*cos((2.0+phi/180.0)*pi);
v.y = radius*sin((0.5-thet/180.0)*pi)*sin((2.0+phi/180.0)*pi);
v.z = radius*cos((0.5-thet/180.0)*pi);
return v;
}
void check(point3d &v)
{
if(abs(v.x)<1e-8){
v.x=0;
}
if(abs(v.y)<1e-8){
v.y=0;
}
if(abs(v.z)<1e-8){
v.z=0;
}
}
void subrutine(int order,ofstream& outfile,double r,double R,double longi_min,double longi_max,double lati_min,double lati_max)
{
double affection;
double c_length;
double depth;
double min_length=MAX;
point3d posi;
depth = R - r;
min_length = min(depth,min_length);
c_length = (lati_max-lati_min)*pi*r/180;
min_length = min(c_length,min_length);
c_length = (longi_max-longi_min)*r*cos(lati_max*pi/180.0)*pi/180;
min_length = min(c_length,min_length);
affection = min_length/10;
//string meshsize_name = to_string(order);
//outfile<<"lc"<<meshsize_name<<" = "<<affection<<";"<<endl;
posi = SCS2CCS(lati_min,longi_min,r);
check(posi);
outfile<<setprecision(16)<<"Point("<<order*12+2<<") = {"<<posi.x<<", "<<posi.y<<", "<<posi.z<<", lc*"<<affection<<"};"<<endl;
outfile<<setprecision(16)<<"Point("<<order*12+3<<") = {"<<0.0<<", "<<0.0<<", "<<posi.z<<", lc*"<<affection<<"};"<<endl;
posi = SCS2CCS(lati_min,longi_max,r);
check(posi);
outfile<<setprecision(16)<<"Point("<<order*12+4<<") = {"<<posi.x<<", "<<posi.y<<", "<<posi.z<<", lc*"<<affection<<"};"<<endl;
posi = SCS2CCS(lati_min,longi_min,R);
check(posi);
outfile<<setprecision(16)<<"Point("<<order*12+5<<") = {"<<posi.x<<", "<<posi.y<<", "<<posi.z<<", lc*"<<affection<<"};"<<endl;
outfile<<setprecision(16)<<"Point("<<order*12+6<<") = {"<<0.0<<", "<<0.0<<", "<<posi.z<<", lc*"<<affection<<"};"<<endl;
posi = SCS2CCS(lati_min,longi_max,R);
check(posi);
outfile<<setprecision(16)<<"Point("<<order*12+7<<") = {"<<posi.x<<", "<<posi.y<<", "<<posi.z<<", lc*"<<affection<<"};"<<endl;
posi = SCS2CCS(lati_max,longi_min,r);
check(posi);
outfile<<setprecision(16)<<"Point("<<order*12+8<<") = {"<<posi.x<<", "<<posi.y<<", "<<posi.z<<", lc*"<<affection<<"};"<<endl;
outfile<<setprecision(16)<<"Point("<<order*12+9<<") = {"<<0.0<<", "<<0.0<<", "<<posi.z<<", lc*"<<affection<<"};"<<endl;
posi = SCS2CCS(lati_max,longi_max,r);
check(posi);
outfile<<setprecision(16)<<"Point("<<order*12+10<<") = {"<<posi.x<<", "<<posi.y<<", "<<posi.z<<", lc*"<<affection<<"};"<<endl;
posi = SCS2CCS(lati_max,longi_min,R);
check(posi);
outfile<<setprecision(16)<<"Point("<<order*12+11<<") = {"<<posi.x<<", "<<posi.y<<", "<<posi.z<<", lc*"<<affection<<"};"<<endl;
outfile<<setprecision(16)<<"Point("<<order*12+12<<") = {"<<0.0<<", "<<0.0<<", "<<posi.z<<", lc*"<<affection<<"};"<<endl;
posi = SCS2CCS(lati_max,longi_max,R);
check(posi);
outfile<<setprecision(16)<<"Point("<<order*12+13<<") = {"<<posi.x<<", "<<posi.y<<", "<<posi.z<<", lc*"<<affection<<"};"<<endl;
outfile<<"Circle("<<order*26+1<<") = {"<<order*12+13<<", "<<order*12+12<<", "<<order*12+11<<"};"<<endl;
outfile<<"Circle("<<order*26+2<<") = {"<<order*12+10<<", "<<order*12+9<<", "<<order*12+8<<"};"<<endl;
outfile<<"Circle("<<order*26+3<<") = {"<<order*12+7<<", "<<order*12+6<<", "<<order*12+5<<"};"<<endl;
outfile<<"Circle("<<order*26+4<<") = {"<<order*12+4<<", "<<order*12+3<<", "<<order*12+2<<"};"<<endl;
outfile<<"Circle("<<order*26+5<<") = {"<<order*12+8<<", 1, "<<order*12+2<<"};"<<endl;
outfile<<"Circle("<<order*26+6<<") = {"<<order*12+11<<", 1, "<<order*12+5<<"};"<<endl;
outfile<<"Circle("<<order*26+7<<") = {"<<order*12+10<<", 1, "<<order*12+4<<"};"<<endl;
outfile<<"Circle("<<order*26+8<<") = {"<<order*12+13<<", 1, "<<order*12+7<<"};"<<endl;
outfile<<"Line("<<order*26+9<<") = {"<<order*12+5<<", "<<order*12+2<<"};"<<endl;
outfile<<"Line("<<order*26+10<<") = {"<<order*12+7<<", "<<order*12+4<<"};"<<endl;
outfile<<"Line("<<order*26+11<<") = {"<<order*12+11<<", "<<order*12+8<<"};"<<endl;
outfile<<"Line("<<order*26+12<<") = {"<<order*12+13<<", "<<order*12+10<<"};"<<endl;
outfile<<"Line Loop("<<order*26+13<<") = {"<<order*26+1<<", "<<order*26+6<<", -"<<order*26+3<<", -"<<order*26+8<<"};"<<endl;
outfile<<"Ruled Surface("<<order*26+14<<") = {"<<order*26+13<<"};"<<endl;
outfile<<"Line Loop("<<order*26+15<<") = {"<<order*26+2<<", "<<order*26+5<<", -"<<order*26+4<<", -"<<order*26+7<<"};"<<endl;
outfile<<"Ruled Surface("<<order*26+16<<") = {"<<order*26+15<<"};"<<endl;
outfile<<"Line Loop("<<order*26+17<<") = {"<<order*26+1<<", "<<order*26+11<<", -"<<order*26+2<<", -"<<order*26+12<<"};"<<endl;
outfile<<"Ruled Surface("<<order*26+18<<") = {"<<order*26+17<<"};"<<endl;
outfile<<"Line Loop("<<order*26+19<<") = {"<<order*26+3<<", "<<order*26+9<<", -"<<order*26+4<<", -"<<order*26+10<<"};"<<endl;
outfile<<"Ruled Surface("<<order*26+20<<") = {"<<order*26+19<<"};"<<endl;
outfile<<"Line Loop("<<order*26+21<<") = {"<<order*26+6<<", "<<order*26+9<<", -"<<order*26+5<<", -"<<order*26+11<<"};"<<endl;
outfile<<"Plane Surface("<<order*26+22<<") = {"<<order*26+21<<"};"<<endl;
outfile<<"Line Loop("<<order*26+23<<") = {"<<order*26+8<<", "<<order*26+10<<", -"<<order*26+7<<", -"<<order*26+12<<"};"<<endl;
outfile<<"Plane Surface("<<order*26+24<<") = {"<<order*26+23<<"};"<<endl;
outfile<<"Surface Loop("<<order*26+25<<") = {"<<order*26+24<<", "<<order*26+18<<", "<<order*26+14<<", "<<order*26+22<<", "<<order*26+16<<", "<<order*26+20<<"};"<<endl;
outfile<<"Volume("<<order*26+26<<") = {"<<order*26+25<<"};"<<endl;
}
int run(int total_num,Tess* tesseroids,char* filename)
{
ofstream outfile(filename);
if (!outfile)
{
cout<<BOLDRED<<"==> "<<RESET<<"Error: Can not create output file"<<endl;
return -1;
}
else
{
outfile<<"//this file is created by Tesseroid_creater"<<endl;
outfile<<"lc = 1.0;"<<endl;
outfile<<"Point(1) = {"<<0.0<<", "<<0.0<<", "<<0.0<<", lc};"<<endl;
for (int i = 0; i < total_num; i++)
{
subrutine(i,outfile,tesseroids[i].r,tesseroids[i].R,tesseroids[i].xmin,tesseroids[i].xmax,tesseroids[i].ymin,tesseroids[i].ymax);
}
outfile.close();
return 0;
}
}