259 lines
5.6 KiB
C
259 lines
5.6 KiB
C
|
#ifndef _3DTOOLS2MSH_H
|
|||
|
#define _3DTOOLS2MSH_H
|
|||
|
#include "iostream"
|
|||
|
#include "fstream"
|
|||
|
#include "sstream"
|
|||
|
#include "string.h"
|
|||
|
#include "iomanip"
|
|||
|
#define MESH "-M"
|
|||
|
#define VALUE "-V"
|
|||
|
#define OUTFILE "-G"
|
|||
|
#define FILTER "-F"
|
|||
|
using namespace std;
|
|||
|
|
|||
|
string& replace_all(string& str,const string& old_value,const string& new_value,int &num) //替换str中所有lod_value为new_value,返回被替换的lod_value的个数
|
|||
|
{
|
|||
|
int count = 0;
|
|||
|
for(string::size_type pos(0);pos!=string::npos;pos+=new_value.length()){
|
|||
|
if((pos=str.find(old_value,pos))!=string::npos){
|
|||
|
str.replace(pos,old_value.length(),new_value);
|
|||
|
count++;
|
|||
|
}
|
|||
|
else break;
|
|||
|
}
|
|||
|
num = count;
|
|||
|
return str;
|
|||
|
}
|
|||
|
|
|||
|
int typeget(char *str, const char* str2,string &str3)//提出头端的子句
|
|||
|
{
|
|||
|
char* strp = strstr(str, str2); // 从字符串str中查找str2,
|
|||
|
if (strp == NULL)
|
|||
|
{
|
|||
|
return 0; // 如果没有找到,返回
|
|||
|
}
|
|||
|
string temp = str;
|
|||
|
str3=temp.substr(strlen(str2));//提出除str2外的子句
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
int nameget(char *str, const char* str2,string &str3)//提出尾端的子句
|
|||
|
{
|
|||
|
char* strp = strstr(str, str2); // 从字符串str中查找str2,
|
|||
|
if (strp == NULL)
|
|||
|
{
|
|||
|
return 0; // 如果没有找到,返回
|
|||
|
}
|
|||
|
string temp = str;
|
|||
|
str3=temp.substr(0,strlen(str)-strlen(str2));//提出除str2外的子句
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
struct NODE
|
|||
|
{
|
|||
|
double x,y,z;
|
|||
|
};
|
|||
|
|
|||
|
struct CUBE
|
|||
|
{
|
|||
|
int index[8];
|
|||
|
double phys;
|
|||
|
};
|
|||
|
|
|||
|
class _3dtools2msh
|
|||
|
{
|
|||
|
public:
|
|||
|
_3dtools2msh();
|
|||
|
~_3dtools2msh();
|
|||
|
int meshin(string);
|
|||
|
int physin(string);
|
|||
|
int findindex();
|
|||
|
int mshout(string,double);
|
|||
|
private:
|
|||
|
double xst,yst,zst;
|
|||
|
int xnum,ynum,znum;
|
|||
|
double xinter,yinter,zinter;
|
|||
|
|
|||
|
NODE* node;
|
|||
|
CUBE* cube;
|
|||
|
int nodenum;
|
|||
|
int cubenum;
|
|||
|
};
|
|||
|
|
|||
|
_3dtools2msh::_3dtools2msh()
|
|||
|
{
|
|||
|
node = NULL;
|
|||
|
cube = NULL;
|
|||
|
}
|
|||
|
|
|||
|
_3dtools2msh::~_3dtools2msh()
|
|||
|
{
|
|||
|
if(node!=NULL) delete[] node;
|
|||
|
if(cube!=NULL) delete[] cube;
|
|||
|
}
|
|||
|
|
|||
|
int _3dtools2msh::meshin(string filename)
|
|||
|
{
|
|||
|
stringstream stemp;
|
|||
|
string temp;
|
|||
|
int space_num;
|
|||
|
int xnum_t,ynum_t,znum_t;
|
|||
|
const char* meshname = filename.c_str();
|
|||
|
ifstream inmesh;
|
|||
|
inmesh.open(meshname);
|
|||
|
if (!inmesh)
|
|||
|
{
|
|||
|
cout<<filename<<" not found!"<<endl;
|
|||
|
return 1;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
inmesh>>ynum>>xnum>>znum>>yst>>xst>>zst;
|
|||
|
inmesh>>temp;
|
|||
|
replace_all(temp,"*"," ",space_num);
|
|||
|
stemp.clear();
|
|||
|
stemp.str(temp);
|
|||
|
stemp>>ynum_t>>yinter;
|
|||
|
if (ynum!=ynum_t)
|
|||
|
{
|
|||
|
cout<<"syntax error: "<<filename<<endl;
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
inmesh>>temp;
|
|||
|
replace_all(temp,"*"," ",space_num);
|
|||
|
stemp.clear();
|
|||
|
stemp.str(temp);
|
|||
|
stemp>>xnum_t>>xinter;
|
|||
|
if (xnum!=xnum_t)
|
|||
|
{
|
|||
|
cout<<"syntax error: "<<filename<<endl;
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
inmesh>>temp;
|
|||
|
replace_all(temp,"*"," ",space_num);
|
|||
|
stemp.clear();
|
|||
|
stemp.str(temp);
|
|||
|
stemp>>znum_t>>zinter;
|
|||
|
if (znum!=znum_t)
|
|||
|
{
|
|||
|
cout<<"syntax error: "<<filename<<endl;
|
|||
|
return 1;
|
|||
|
}
|
|||
|
inmesh.close();
|
|||
|
|
|||
|
nodenum = (xnum+1)*(ynum+1)*(znum+1);
|
|||
|
node = new NODE [nodenum];
|
|||
|
for (int i = 0; i < nodenum; i++)
|
|||
|
{
|
|||
|
node[i].z = zst + zinter*(i%(znum+1));
|
|||
|
node[i].y = yst + yinter*((i/(znum+1))%(ynum+1));
|
|||
|
node[i].x = xst + xinter*(i/((znum+1)*(ynum+1)));
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
int _3dtools2msh::physin(string filename)
|
|||
|
{
|
|||
|
cubenum = xnum*ynum*znum;
|
|||
|
cube = new CUBE [cubenum];
|
|||
|
for (int i = 0; i < cubenum; i++)
|
|||
|
{
|
|||
|
cube[i].phys = 0.0;
|
|||
|
}
|
|||
|
|
|||
|
const char* cubename = filename.c_str();
|
|||
|
ifstream incube;
|
|||
|
incube.open(cubename);
|
|||
|
if (!incube)
|
|||
|
{
|
|||
|
cout<<filename<<" not found!"<<endl;
|
|||
|
return 1;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
for (int i = 0; i < cubenum; i++)
|
|||
|
{
|
|||
|
incube>>cube[i].phys;
|
|||
|
}
|
|||
|
incube.close();
|
|||
|
return 0;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
int _3dtools2msh::findindex()
|
|||
|
{
|
|||
|
for (int i = 0; i < xnum; i++)
|
|||
|
{
|
|||
|
for (int j = 0; j < ynum; j++)
|
|||
|
{
|
|||
|
for (int k = 0; k < znum; k++)
|
|||
|
{
|
|||
|
cube[k+j*znum+i*ynum*znum].index[0]=k+j*(znum+1)+i*(ynum+1)*(znum+1);
|
|||
|
cube[k+j*znum+i*ynum*znum].index[1]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+znum+1;
|
|||
|
cube[k+j*znum+i*ynum*znum].index[2]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+znum+1+(ynum+1)*(znum+1);
|
|||
|
cube[k+j*znum+i*ynum*znum].index[3]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+(ynum+1)*(znum+1);
|
|||
|
cube[k+j*znum+i*ynum*znum].index[4]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+1;
|
|||
|
cube[k+j*znum+i*ynum*znum].index[5]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+1+znum+1;
|
|||
|
cube[k+j*znum+i*ynum*znum].index[6]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+1+znum+1+(ynum+1)*(znum+1);
|
|||
|
cube[k+j*znum+i*ynum*znum].index[7]=k+j*(znum+1)+i*(ynum+1)*(znum+1)+1+(ynum+1)*(znum+1);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
int _3dtools2msh::mshout(string filename,double filter)
|
|||
|
{
|
|||
|
const char* mshname = filename.c_str();
|
|||
|
ofstream outmsh;
|
|||
|
outmsh.open(mshname);
|
|||
|
if (!outmsh)
|
|||
|
{
|
|||
|
cout<<"fail to create "<<filename<<endl;
|
|||
|
return 1;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
outmsh<<"$MeshFormat"<<endl<<"2.2 0 8"<<endl<<"$EndMeshFormat"<<endl<<"$Nodes"<<endl<<nodenum<<endl;
|
|||
|
for (int i = 0; i < nodenum; i++)
|
|||
|
{
|
|||
|
outmsh<<i<<" "<<setprecision(16)<<node[i].x<<" "<<node[i].y<<" "<<node[i].z<<endl;
|
|||
|
}
|
|||
|
outmsh<<"$EndNodes"<<endl<<"$Elements"<<endl<<cubenum<<endl;
|
|||
|
for (int i = 0; i < cubenum; i++)
|
|||
|
{
|
|||
|
outmsh<<i<<" 5 2 5 1 "<<cube[i].index[0]<<" "
|
|||
|
<<cube[i].index[1]<<" "
|
|||
|
<<cube[i].index[2]<<" "
|
|||
|
<<cube[i].index[3]<<" "
|
|||
|
<<cube[i].index[4]<<" "
|
|||
|
<<cube[i].index[5]<<" "
|
|||
|
<<cube[i].index[6]<<" "
|
|||
|
<<cube[i].index[7]<<endl;
|
|||
|
}
|
|||
|
int eleNum = 0;
|
|||
|
for (int i = 0; i < cubenum; i++)
|
|||
|
{
|
|||
|
if (cube[i].phys != filter)
|
|||
|
{
|
|||
|
eleNum++;
|
|||
|
}
|
|||
|
}
|
|||
|
outmsh<<"$EndElements"<<endl<<"$ElementData"<<endl;
|
|||
|
outmsh<<1<<endl<<"\"Recovered model\""<<endl<<1<<endl<<0.0<<endl<<3<<endl<<0<<endl<<1<<endl<<eleNum<<endl;
|
|||
|
for (int i = 0; i < cubenum; i++)
|
|||
|
{
|
|||
|
if (cube[i].phys != filter)
|
|||
|
{
|
|||
|
outmsh<<i<<" "<<setprecision(16)<<cube[i].phys<<endl;
|
|||
|
}
|
|||
|
}
|
|||
|
outmsh<<"$EndElementData";
|
|||
|
outmsh.close();
|
|||
|
return 0;
|
|||
|
}
|
|||
|
}
|
|||
|
#endif
|