-
Notifications
You must be signed in to change notification settings - Fork 0
/
MC2vti.cpp
152 lines (130 loc) · 4.58 KB
/
MC2vti.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// MC2vti.cpp
// Convert grids in MMSP MC format to VTK image data format
// Questions/comments to jgruber@andrew.cmu.edu (Jason Gruber)
#include"MCgrid.hpp"
#include<string>
#include<vector>
using namespace std;
using namespace MMSP;
void tokenize(const string& str,
std::vector<string>& tokens,
const string& delimiters = " ")
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
int main(int argc, char* argv[])
{
string outputname;
if (argc==2) {
// Change the name of the input filename for outout
string filename = argv[1];
std::vector<string> tokens;
string delimeters = "."; // Only a period
tokenize(filename, tokens, delimeters);
outputname = tokens[0] + ".vti";
}else if(argc==3){
outputname = argv[2];
}else if(argc<2) {
std::cout<<"Usage: "<<argv[0]<<" file.MC file.vti\n";
std::cout<<"\tor" << argv[0] << "file.MC" << endl;
exit(-1);
}
std::ifstream input(argv[1]);
if (!input) {
std::cout<<argv[0]<<": File read error. Could not open file"<<argv[1]<<".\n";
exit(-2);
}
int dim;
input.read(reinterpret_cast<char*>(&dim),sizeof(dim));
input.close();
std::ofstream output(outputname.c_str());
if (!output) {
std::cout<<argv[0]<<": File write error. Could not open file"<< outputname <<".\n";
exit(-2);
}
std::string byte_order;
if (0x01 & static_cast<int>(1)) byte_order = "LittleEndian";
else byte_order = "BigEndian";
if (dim==1) {
MCgrid1D grid(argv[1]);
int nx = grid.size(0);
float dx = grid.spacing(0);
output<<"<?xml version=\"1.0\"?>\n";
output<<"<VTKFile type=\"ImageData\" version=\"0.1\" byte_order=\""<<byte_order<<"\">\n";
output<<" <ImageData WholeExtent=\""<<0<<" "<<nx<<" 0 0 0 0\"";
output<<" Origin=\"0 0 0\" Spacing=\""<<dx<<" 0 0\">\n";
output<<" <Piece Extent=\""<<0<<" "<<nx<<" "<<" 0 0 0 0\">\n";
output<<" <CellData Scalars=\"ID\">\n";
output<<" <DataArray Name=\"ID\" type=\"Int32\" format=\"ascii\">\n";
for (int x=0; x<nx; x++)
output<<grid[x]<<" ";
output<<"\n";
output<<" </DataArray>\n";
output<<" </CellData>\n";
output<<" </Piece>\n";
output<<" </ImageData>\n";
output<<"</VTKFile>\n";
}
if (dim==2) {
MCgrid2D grid(argv[1]);
int nx = grid.size(0);
int ny = grid.size(1);
float dx = grid.spacing(0);
float dy = grid.spacing(1);
output<<"<?xml version=\"1.0\"?>\n";
output<<"<VTKFile type=\"ImageData\" version=\"0.1\" byte_order=\""<<byte_order<<"\">\n";
output<<" <ImageData WholeExtent=\""<<0<<" "<<nx<<" "<<0<<" "<<ny<<" 0 0\"";
output<<" Origin=\"0 0 0\" Spacing=\""<<dx<<" "<<dy<<" 0\">\n";
output<<" <Piece Extent=\""<<0<<" "<<nx<<" "<<0<<" "<<ny<<" 0 0\">\n";
output<<" <CellData Scalars=\"ID\">\n";
output<<" <DataArray Name=\"ID\" type=\"Int32\" format=\"ascii\">\n";
for (int y=0; y<ny; y++)
for (int x=0; x<nx; x++)
output<<grid[x][y]<<" ";
output<<"\n";
output<<" </DataArray>\n";
output<<" </CellData>\n";
output<<" </Piece>\n";
output<<" </ImageData>\n";
output<<"</VTKFile>\n";
}
if (dim==3) {
MCgrid3D grid(argv[1]);
int nx = grid.size(0);
int ny = grid.size(1);
int nz = grid.size(2);
float dx = grid.spacing(0);
float dy = grid.spacing(1);
float dz = grid.spacing(2);
output<<"<?xml version=\"1.0\"?>\n";
output<<"<VTKFile type=\"ImageData\" version=\"0.1\" byte_order=\""<<byte_order<<"\">\n";
output<<" <ImageData WholeExtent=\""<<0<<" "<<nx<<" "<<0<<" "<<ny<<" "<<0<<" "<<nz<<"\"";
output<<" Origin=\"0 0 0\" Spacing=\""<<dx<<" "<<dy<<" "<<dz<<"\">\n";
output<<" <Piece Extent=\""<<0<<" "<<nx<<" "<<0<<" "<<ny<<" "<<0<<" "<<nz<<"\">\n";
output<<" <CellData Scalars=\"ID\">\n";
output<<" <DataArray Name=\"ID\" type=\"Int32\" format=\"ascii\">\n";
for (int z=0; z<nz; z++)
for (int y=0; y<ny; y++)
for (int x=0; x<nx; x++)
output<<grid[x][y][z]<<" ";
output<<"\n";
output<<" </DataArray>\n";
output<<" </CellData>\n";
output<<" </Piece>\n";
output<<" </ImageData>\n";
output<<"</VTKFile>\n";
}
output.close();
}