forked from ImperialCollegeLondon/multifluids_icferst
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvtkprojection.cpp
240 lines (188 loc) · 6.5 KB
/
vtkprojection.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* Copyright (C) 2006 Imperial College London and others.
Please see the AUTHORS file in the main source directory for a full list
of copyright holders.
Prof. C Pain
Applied Modelling and Computation Group
Department of Earth Science and Engineering
Imperial College London
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation,
version 2.1 of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
#include <confdefs.h>
#include <vtk.h>
#include <cmath>
#include <iostream>
#include <cassert>
#include <getopt.h>
#include <string>
#include <map>
#include <vector>
using namespace std;
// the function that actually does the work - in the femtools dir
extern int projections(int nPoints, double *x, double *y, double *z, string current_coord, string output_coord);
#define DEBUG(msg) if (OptionIsSet("verbose")) cerr << msg << endl;
void PrintUsageInformation(const char* executableName);
enum ExitCodes{
SUCCESS = 0,
BAD_ARGUMENTS = -1,
UNKNOWN_CELL_TYPE = -2
};
map<string, string> optionMap;
bool OptionIsSet(const string& option){
return optionMap.find(option) != optionMap.end();
}
void ParseArguments(int argc, char** argv){
struct option longOptions[] = {
{"help", 0, 0, 'h'},
{"incoords", 1, 0, 'I'},
{"outcoords", 1, 0, 'O'},
{"output", 1, 0, 'o'},
{"verbose", 0, 0, 'v'},
{0, 0, 0, 0}
};
int optionIndex = 0;
int c;
// Make sure we have enough options
if (argc>=5){
optionMap["infile"] = argv[argc-1];
argc--;
bool flag = false;
fstream fin;
fin.open(optionMap["infile"].c_str(),ios::in);
if(!fin.is_open()){
cerr<<"ERROR: no such file: "<<optionMap["infile"]<<endl;
flag=true;
}
fin.close();
}
// Parse remaining options
while (true){
c = getopt_long(argc, argv, "h:I:O:o:v", longOptions, &optionIndex);
if (c == -1) break;
switch (c){
case 'h':
PrintUsageInformation(argv[0]);
exit(SUCCESS);
case 'I':
optionMap["incoord"] = optarg;
break;
case 'O':
optionMap["outcoord"] = optarg;
break;
case 'o':
optionMap["outfile"] = optarg;
break;
case 'v':
optionMap["verbose"] = "true";
break;
case '?':
PrintUsageInformation(argv[0]);
exit(BAD_ARGUMENTS);
}
}
// Make sure we have the right options
if (!OptionIsSet("incoord")){
cout << "Coordinate type of input file not specified!" << endl;
PrintUsageInformation(argv[0]);
exit(BAD_ARGUMENTS);
}
if (!OptionIsSet("outcoord")){
cout << "Coordinate type of output not specified!" << endl;
PrintUsageInformation(argv[0]);
exit(BAD_ARGUMENTS);
}
}
void PrintUsageInformation(const char* executableName){
DEBUG("void PrintUsageInformation(const char* executableName)");
cerr << "Usage: " << executableName << "[OPTIONS] -I <in-coordinates> -O <out-coordinates> infile.vtu" << endl << endl
<< "Options:" << endl
<< "-h, --help" << endl
<< "\tPrint this usage information" << endl << endl
<< "-I, --incoords" << endl
<< "\tCoordinates of input file. Valid types are:" << endl
<< "\t type | description "<< endl
<< "\t-------------------------"<< endl
<< "\t cart | Cartesian (meters)" << endl
<< "\t spherical | Longitude/Latitude" << endl
<< "\t stereo | Stereographic Projection" << endl << endl
<< "-O, --outcoords" << endl
<< "\tCoordinates of output file. Valid types are:" << endl
<< "\t type | description "<< endl
<< "\t-------------------------"<< endl
<< "\t cart | Cartesian (meters)" << endl
<< "\t spherical | Longitude/Latitude" << endl
<< "\t stereo | Stereographic Projection" << endl << endl
<< "-o, --output=FILENAME.vtu" << endl
<< "\tFile that the result is outputed to. **The default is to overwrite the input file**" << endl << endl
<< "-v, --verbose" << endl
<< "\tBe verbose" << endl;
}
vtkUnstructuredGrid* ReadGrid(const string& filename){
DEBUG("vtkUnstructuredGrid* ReadGrid("<<filename<<")");
vtkXMLUnstructuredGridReader* reader = vtkXMLUnstructuredGridReader::New();
reader->SetFileName(filename.c_str());
reader->Update();
vtkUnstructuredGrid* grid = vtkUnstructuredGrid::New();
grid->DeepCopy(reader->GetOutput());
#if VTK_MAJOR_VERSION <= 5
grid->Update();
#endif
reader->Delete();
return grid;
}
void WriteGrid(vtkUnstructuredGrid* grid, const string& filename){
DEBUG("void WriteGrid(vtkUnstructuredGrid* grid, const string& filename)");
vtkXMLUnstructuredGridWriter* writer= vtkXMLUnstructuredGridWriter::New();
writer->SetFileName( filename.c_str() );
#if VTK_MAJOR_VERSION <= 5
writer->SetInput(grid);
#else
writer->SetInputData(grid);
#endif
vtkZLibDataCompressor* compressor = vtkZLibDataCompressor::New();
writer->SetCompressor(compressor);
writer->Write();
writer->Delete();
compressor->Delete();
}
int main(int argc, char** argv){
ParseArguments(argc, argv);
// Load
vtkUnstructuredGrid* grid = ReadGrid( optionMap["infile"] );
// Point definitions
vtkIdType npts = grid->GetNumberOfPoints();
cerr<<"num of points - "<<npts<<endl;
double *x = new double[npts];
double *y = new double[npts];
double *z = new double[npts];
for(vtkIdType i=0;i<npts;i++){
double xyz[3];
grid->GetPoint(i, xyz);
x[i] = xyz[0];
y[i] = xyz[1];
z[i] = xyz[2];
}
cout<<"Converting from : "<<optionMap["incoord"]<<" to: "<<optionMap["outcoord"]<<endl;
int err = projections(npts, x, y, z, optionMap["incoord"], optionMap["outcoord"]);
for(vtkIdType i=0;i<npts;i++){
grid->GetPoints()->SetPoint(i, x[i], y[i], z[i]);
}
// Write
if ( OptionIsSet("outfile") ){
WriteGrid( grid, optionMap["outfile"] );
}else{
WriteGrid( grid, optionMap["infile"] );
}
return SUCCESS;
}