#include
#include
#include
#include
#include
using namespace std;
template
vector<vector> DataOut(ifstream& fin, int n, int size)
{
string line;
stringstream lineStream;
vector<vector> Data;
vector vecTmp;
vecTmp.resize(size);
for (int i = 0; i < n; i++)
{
getline(fin, line);
lineStream << line;
for (int j = 0; j < size; j++)
lineStream >> vecTmp[j];
Data.push_back(vecTmp);
lineStream.clear();
}
return Data;
}
vector<vector> Read_Vtk
(
int frame,
string partName,
string inVariableName,
int setType,//0表示set为节点,1表示单元集合
int partId//看输入是否有,不是一定有
)
{
/*
//输入
int frame = 1;
string partName = “KEY_R_05MM”;
string inVariableName = “U”;
int setType = 1;//0表示set为节点,1表示单元集合
int partId = 1; //看输入是否有,不是一定有
*/
for (int i = 0; i < inVariableName.size(); i++)
{
inVariableName[i]=tolower(inVariableName[i]);
}
if (inVariableName.size() > 0)
inVariableName[0]=toupper(inVariableName[0]);
//判断寻找变量在VTK中的名字
string judgeInVariableName;
if (inVariableName == "U")
judgeInVariableName = "Displacement";
else if (inVariableName == "Coord")
judgeInVariableName = "Coord";
else if (inVariableName == "Density")
judgeInVariableName = "density";
else if (inVariableName == "Resistivity")
judgeInVariableName = "resistivity";
else if (inVariableName == "Temperature")
judgeInVariableName = "temperature";
else if (inVariableName == "Permittivity")
judgeInVariableName = "permittivity";
//result.2_1_RIGID
//result.1_KEY_R_05MM.vtk
string inFile = "result.";
inFile = inFile + to_string(frame) + "_" + partName + ".vtk"; //切忌一定要把 后缀vtk带上,不然就是错误
ifstream fin;
fin.open(inFile.c_str(), ios::in);
// POINTS 8 double
int n_points; //节点总数
string pointsDatetype;//节点数据类型
vector<vector<int>> pointDataInt;//读入数据
vector<vector<float>> pointDataFloat;//读入数据
vector<vector<double>> pointDataDouble;//读入数据
//CELLS 1 9
int n_cells; //单元总数
int elementsDateblockNum; //这一块包含的数据总数
vector<vector<int>> elementNodeIdArray;
// CELL_TYPES 1
//单元类型
vector<int> elementType;
// POINT_DATA 3782
// CELL_DATA 1
int dateTypeFlag = -1;//0:point数据 1:cell数据
int dataSize;
//SCALARS density double 1
//LOOKUP_TABLE table1
string physicalType;// //SCALAR,VECTOR或TENSOR,不同类型的数据需要不同的格式
string varieableName;//变量名
string dataType;//数据块数据类型
int numComp; //分量个数
vector<vector<int>> phyDataInt;//读入数据
vector<vector<float>> phyDataFloat;//读入数据
vector<vector<double>> phyDataDouble;//读入数据
stringstream lineStream;
string line;
//line = " POINTS 8 double";
string temp;
string temp1;
if (!fin.is_open())
{
cout << "File open fail" << endl;
return pointDataDouble;
}
if (fin)
{
while (getline(fin, line))
{
//清除每行段前空格
while (line[0] == '\t')
line.erase(0, 1);
//读取节点数
if ((line[0] >= 'a' & line[0] <= 'z') || (line[0] >= 'A' && line[0] <= 'Z'))
{
// POINTS 8 double
if (line.substr(0, 6) == "POINTS")
{
lineStream << line;
lineStream >> temp;
lineStream >> n_points;
lineStream >> pointsDatetype;
lineStream.clear();
if (pointsDatetype == "int")
{
pointDataInt = DataOut<int>(fin, n_points, 3);
}
else if (pointsDatetype == "double" || pointsDatetype == "float")
{
if ((judgeInVariableName == "Coord") && (setType == 0))
{
pointDataDouble = DataOut<double>(fin, n_points, 3);
return pointDataDouble;
}
else
{
for (int i = 0; i < n_points; i++)
{
getline(fin, line);//跳过这些行
}
}
}
else if (pointsDatetype == "double")
{
pointDataDouble = DataOut<double>(fin, n_points, 3);
}
else if (pointsDatetype == "float")
{
pointDataFloat = DataOut<float>(fin