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

View File

@@ -0,0 +1,5 @@
add_executable(msh2vtk main.cpp)
# 第二种方式指定目标文件的路径 这个方式目标文件夹中将只包含目标文件而没有中间文件
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
# 设置安装地址
install(TARGETS msh2vtk RUNTIME DESTINATION selfpro)

View File

@@ -0,0 +1,256 @@
#ifndef _FUNC_MSH2VTK_H
#define _FUNC_MSH2VTK_H
#include "sysDefine_msh2vtk.h"
class mshvtk
{
private:
nodeArray nArray;
elementArray eArray;
nodeDataArray ndArray;
elementDataArray edArray;
typesMap msh2vtk_type;
typesMap type_nodenum;
typesMap::iterator itm;
int nodenum;
int elenum;
strArray outNames;
public:
mshvtk(){
msh2vtk_type[1] = 3;
msh2vtk_type[2] = 5;
msh2vtk_type[3] = 9;
msh2vtk_type[4] = 10;
msh2vtk_type[5] = 12;
msh2vtk_type[6] = 13; //注意三棱柱顶点序列gmsh和vtk貌似有点区别 上下底面的三角形顶点排序刚好相反
type_nodenum[1] = 2;
type_nodenum[2] = 3;
type_nodenum[3] = 4;
type_nodenum[4] = 4;
type_nodenum[5] = 8;
type_nodenum[6] = 6;
}
~mshvtk(){}
int runtine(char*);
int addname(char*);
int readmsh(char*); //注意这里的文件名都是不带.msh后缀的
int writevtk(char*); //注意这里的文件名需要添加.vtk后缀
};
int mshvtk::runtine(char* parachar)
{
if(readmsh(parachar)) return -1;
if(writevtk(parachar)) return -1;
nArray.clear();
ndArray.clear();
eArray.clear();
edArray.clear();
return 0;
}
int mshvtk::addname(char* onename)
{
string s = onename;
s = "\""+s+"\"";
outNames.push_back(s);
return 0;
}
int mshvtk::readmsh(char* inname)
{
int temp_int,ele_num;
node temp_node;
element temp_element;
nodeData temp_node_data;
elementData temp_element_data;
string temp_str;
stringstream temp_ss;
char filename[1024];
strcpy(filename,inname);
strcat(filename,".msh");
ifstream infile;
if(open_infile(infile,filename)) return -1;
while(getline(infile,temp_str))
{
if (temp_str == "$Nodes")
{
getline(infile,temp_str);
temp_ss.clear(); temp_ss.str(""); temp_ss << temp_str;
temp_ss >> nodenum;
for (int i = 0; i < nodenum; i++)
{
getline(infile,temp_str);
temp_ss.clear();
temp_ss << temp_str;
temp_ss >> temp_node.id >> temp_node.x >> temp_node.y >> temp_node.z;
nArray.push_back(temp_node);
}
}
else if (temp_str == "$Elements")
{
getline(infile,temp_str);
temp_ss.clear(); temp_ss.str(""); temp_ss << temp_str;
temp_ss >> ele_num;
for (int i = 0; i < ele_num; i++)
{
temp_element.msh_info.clear();
temp_element.nodes_index.clear();
getline(infile,temp_str);
temp_ss.clear(); temp_ss.str(""); temp_ss << temp_str;
temp_ss >> temp_element.id >> temp_element.msh_type >> temp_element.msh_info_num;
for (int j = 0; j < temp_element.msh_info_num; j++)
{
temp_ss >> temp_int;
temp_element.msh_info.push_back(temp_int);
}
for (int j = 0; j < type_nodenum[temp_element.msh_type]; j++)
{
temp_ss >> temp_int;
if (nArray[0].id == 1)
{
temp_element.nodes_index.push_back(temp_int-1);
}
else temp_element.nodes_index.push_back(temp_int);
}
temp_element.vtk_type = msh2vtk_type[temp_element.msh_type];
eArray.push_back(temp_element);
}
}
else if (temp_str == "$ElementData")
{
temp_element_data.ids.clear();
temp_element_data.val.clear();
getline(infile,temp_str);
temp_ss.clear(); temp_ss.str(""); temp_ss << temp_str;
temp_ss >> temp_int;
for (int i = 0; i < temp_int; i++)
{
getline(infile,temp_str);
temp_element_data.name = temp_str;
}
//跳过6行
for (int i = 0; i < 6; i++)
getline(infile,temp_str);
temp_ss.clear(); temp_ss.str(""); temp_ss << temp_str;
temp_ss >> temp_int;
temp_element_data.ids.resize(temp_int);
temp_element_data.val.resize(temp_int);
for (int i = 0; i < temp_int; i++)
{
getline(infile,temp_str);
temp_ss.clear(); temp_ss.str(""); temp_ss << temp_str;
temp_ss >> temp_element_data.ids[i] >> temp_element_data.val[i];
}
edArray.push_back(temp_element_data);
}
else if (temp_str == "$NodeData")
{
temp_node_data.ids.clear();
temp_node_data.val.clear();
getline(infile,temp_str);
temp_ss.clear(); temp_ss.str(""); temp_ss << temp_str;
temp_ss >> temp_int;
for (int i = 0; i < temp_int; i++)
{
getline(infile,temp_str);
temp_node_data.name = temp_str;
}
//跳过6行
for (int i = 0; i < 6; i++)
getline(infile,temp_str);
temp_ss.clear(); temp_ss.str(""); temp_ss << temp_str;
temp_ss >> temp_int;
temp_node_data.ids.resize(temp_int);
temp_node_data.val.resize(temp_int);
for (int i = 0; i < temp_int; i++)
{
getline(infile,temp_str);
temp_ss.clear(); temp_ss.str(""); temp_ss << temp_str;
temp_ss >> temp_node_data.ids[i] >> temp_node_data.val[i];
}
ndArray.push_back(temp_node_data);
}
}
infile.close();
return 0;
}
int mshvtk::writevtk(char* inname)
{
int temp_int;
string temp_dataName;
int totalCellNum = 0;
char filename[1024];
strcpy(filename,inname);
strcat(filename,".vtk");
ofstream outfile;
if(open_outfile(outfile,filename)) return -1;
outfile<<"# vtk DataFile Version 2.0"<<endl<<"Generated by msh2vtk"<<endl<<"ASCII"<<endl<<"DATASET UNSTRUCTURED_GRID"<<endl<<"POINTS "<<nArray.size()<<" FLOAT"<<endl;
for (int i = 0; i < nArray.size(); i++)
{
outfile << setprecision(16) << nArray.at(i).x << " " << nArray.at(i).y << " " << nArray.at(i).z << endl;
}
//计算一下CELLS的总长 注意类型名算一个 所以要加1
for (int i = 0; i < eArray.size(); i++)
{
totalCellNum += type_nodenum[eArray.at(i).msh_type] + 1;
}
outfile<<"CELLS "<<eArray.size()<<" "<<totalCellNum<<endl;
for (int i = 0; i < eArray.size(); i++)
{
outfile << type_nodenum[eArray.at(i).msh_type] << " ";
for (int j = 0; j < eArray.at(i).nodes_index.size(); j++)
{
outfile << eArray.at(i).nodes_index.at(j) << " ";
}
outfile << endl;
}
outfile << "CELL_TYPES " << eArray.size() << endl;
for (int i = 0; i < eArray.size(); i++)
{
outfile << eArray.at(i).vtk_type << " ";
}
outfile << endl;
for (int i = 0; i < ndArray.size(); i++)
{
for (int n = 0; n < outNames.size(); n++)
{
if (outNames[n] == ndArray[i].name)
{
temp_dataName = replace_all(ndArray[i].name," ","_",temp_int);
outfile<<"POINT_DATA "<<ndArray[i].val.size()<<endl<<"SCALARS " << temp_dataName << " FLOAT"<<endl<<"LOOKUP_TABLE default"<<endl;
for (int j = 0; j < ndArray[i].val.size(); j++)
{
outfile<<setprecision(16)<<ndArray[i].val[j]<<endl;
}
}
}
}
for (int i = 0; i < edArray.size(); i++)
{
for (int n = 0; n < outNames.size(); n++)
{
if (outNames[n] == edArray[i].name)
{
temp_dataName = replace_all(edArray[i].name," ","_",temp_int);
outfile<<"CELL_DATA "<<edArray[i].val.size()<<endl<<"SCALARS " << temp_dataName << " FLOAT"<<endl<<"LOOKUP_TABLE default"<<endl;
for (int j = 0; j < edArray[i].val.size(); j++)
{
outfile<<setprecision(16)<<edArray[i].val[j]<<endl;
}
}
}
}
outfile.close();
return 0;
}
#endif

25
archive/msh2vtk/main.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include "func_msh2vtk.h"
int main(int argc, char* argv[])
{
mshvtk mv;
char mshname[1024];
char dataname[1024];
if (argc == 1)
{
cout << "msh2vtk <msh-file> <data-name> [<data-name> ...]" << endl;
}
else
{
sscanf(argv[1],"%[^.]",mshname);
for (int i = 2; i < argc; i++)
{
sscanf(argv[i],"%[^\0]",dataname);
mv.addname(dataname);
}
mv.runtine(mshname);
}
return 0;
}

15
archive/msh2vtk/makefile Normal file
View File

@@ -0,0 +1,15 @@
CC = g++-8
PROM = /usr/local/sbin/msh2vtk
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,97 @@
#ifndef _SYSDEFINE_MSH2VTK_H
#define _SYSDEFINE_MSH2VTK_H
#include "iostream"
#include "fstream"
#include "string.h"
#include "sstream"
#include "stdio.h"
#include "stdlib.h"
#include "iomanip"
#include "cmath"
#include "vector"
#include "map"
#define BOLDRED "\033[1m\033[31m"
#define RESET "\033[0m"
using namespace std;
typedef vector<int> _1iArray;
typedef vector<double> _1dArray;
typedef vector<string> strArray;
typedef map<int,int> typesMap;
struct node
{
int id;
double x, y, z;
};
typedef vector<node> nodeArray;
struct element
{
int id;
int msh_type;
int vtk_type;
int msh_info_num;
_1iArray msh_info;
_1iArray nodes_index;
};
typedef vector<element> elementArray;
struct elementData
{
_1iArray ids;
string name;
_1dArray val;
};
typedef vector<elementData> elementDataArray;
struct nodeData
{
_1iArray ids;
string name;
_1dArray val;
};
typedef vector<nodeData> nodeDataArray;
//全局函数
int open_infile(ifstream &infile,char* filename)
{
infile.open(filename);
if (!infile)
{
cout << BOLDRED << "error ==> " << RESET << "file not found: " << filename << endl;
return -1;
}
return 0;
}
int open_outfile(ofstream &outfile,char* filename)
{
outfile.open(filename);
if (!outfile)
{
cout << BOLDRED << "error ==> " << RESET << "fail to create the file: " << filename << endl;
return -1;
}
return 0;
}
//替换str中所有lod_value为new_value,返回被替换的old_value的个数
string& replace_all(string& str,const string& old_value,const string& new_value,int &num)
{
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;
}
#endif

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,5 @@
20 30 10
30 30 -1
20*1
30*1
10*0.5

19172
archive/msh2vtk/test/test.vtk Normal file

File diff suppressed because one or more lines are too long

19172
archive/msh2vtk/test/test2.vtk Normal file

File diff suppressed because one or more lines are too long

19180
archive/msh2vtk/test/test3.msh Normal file

File diff suppressed because it is too large Load Diff

19172
archive/msh2vtk/test/test3.vtk Normal file

File diff suppressed because one or more lines are too long

19180
archive/msh2vtk/test/test4.msh Normal file

File diff suppressed because it is too large Load Diff

19172
archive/msh2vtk/test/test4.vtk Normal file

File diff suppressed because one or more lines are too long