forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkUnstructuredGridReader.cxx
406 lines (355 loc) · 11.2 KB
/
vtkUnstructuredGridReader.cxx
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*=========================================================================
Program: Visualization Toolkit
Module: vtkUnstructuredGridReader.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkUnstructuredGridReader.h"
#include "vtkByteSwap.h"
#include "vtkCellArray.h"
#include "vtkFieldData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkUnstructuredGrid.h"
vtkStandardNewMacro(vtkUnstructuredGridReader);
#ifdef read
#undef read
#endif
//----------------------------------------------------------------------------
vtkUnstructuredGridReader::vtkUnstructuredGridReader()
{
vtkUnstructuredGrid *output = vtkUnstructuredGrid::New();
this->SetOutput(output);
// Releasing data for pipeline parallism.
// Filters will know it is empty.
output->ReleaseData();
output->Delete();
}
//----------------------------------------------------------------------------
vtkUnstructuredGridReader::~vtkUnstructuredGridReader()
{
}
//----------------------------------------------------------------------------
vtkUnstructuredGrid* vtkUnstructuredGridReader::GetOutput()
{
return this->GetOutput(0);
}
//----------------------------------------------------------------------------
vtkUnstructuredGrid* vtkUnstructuredGridReader::GetOutput(int idx)
{
return vtkUnstructuredGrid::SafeDownCast(this->GetOutputDataObject(idx));
}
//----------------------------------------------------------------------------
void vtkUnstructuredGridReader::SetOutput(vtkUnstructuredGrid *output)
{
this->GetExecutive()->SetOutputData(0, output);
}
//----------------------------------------------------------------------------
// I do not think this should be here, but I do not want to remove it now.
int vtkUnstructuredGridReader::RequestUpdateExtent(
vtkInformation *,
vtkInformationVector **,
vtkInformationVector *outputVector)
{
vtkInformation *outInfo = outputVector->GetInformationObject(0);
int piece, numPieces;
piece = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
numPieces = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
// make sure piece is valid
if (piece < 0 || piece >= numPieces)
{
return 1;
}
return 1;
}
//----------------------------------------------------------------------------
int vtkUnstructuredGridReader::RequestData(
vtkInformation *,
vtkInformationVector **,
vtkInformationVector *outputVector)
{
vtkInformation *outInfo = outputVector->GetInformationObject(0);
int i, numPts=0, numCells=0;
char line[256];
int npts, size, ncells=0;
int piece, numPieces, skip1, read2, skip3, tmp;
vtkCellArray *cells=NULL;
int *types=NULL;
int done=0;
vtkUnstructuredGrid *output = vtkUnstructuredGrid::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
int *tempArray;
vtkIdType *idArray;
// All of the data in the first piece.
if (outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()) > 0)
{
return 1;
}
vtkDebugMacro(<<"Reading vtk unstructured grid...");
if (!this->OpenVTKFile() || !this->ReadHeader())
{
return 1;
}
// Read unstructured grid specific stuff
//
if (!this->ReadString(line))
{
vtkErrorMacro(<<"Data file ends prematurely!");
this->CloseVTKFile ();
return 1;
}
if ( !strncmp(this->LowerCase(line),"dataset",(unsigned long)7) )
{
// Make sure we're reading right type of geometry
//
if (!this->ReadString(line))
{
vtkErrorMacro(<<"Data file ends prematurely!");
this->CloseVTKFile ();
return 1;
}
if ( strncmp(this->LowerCase(line),"unstructured_grid",17) )
{
vtkErrorMacro(<< "Cannot read dataset type: " << line);
this->CloseVTKFile ();
return 1;
}
// Might find points, cells, and cell types
//
while (!done)
{
if (!this->ReadString(line))
{
break;
}
if (! strncmp(this->LowerCase(line), "field", 5))
{
vtkFieldData* fd = this->ReadFieldData();
output->SetFieldData(fd);
fd->Delete(); // ?
}
else if ( ! strncmp(line, "points",6) )
{
if (!this->Read(&numPts))
{
vtkErrorMacro(<<"Cannot read number of points!");
this->CloseVTKFile ();
return 1;
}
if (!this->ReadPoints(output, numPts))
{
this->CloseVTKFile ();
return 1;
}
}
else if ( !strncmp(line,"cells",5))
{
piece = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
numPieces = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
if (!(this->Read(&ncells) && this->Read(&size)))
{
vtkErrorMacro(<<"Cannot read cells!");
this->CloseVTKFile ();
return 1;
}
// the number of ints to read befor we get to the piece.
skip1 = piece * ncells / numPieces;
// the number of ints to read as part of piece.
read2 = ((piece+1) * ncells / numPieces) - skip1;
// the number of ints after the piece
skip3 = ncells - skip1 - read2;
cells = vtkCellArray::New();
tempArray = new int[size];
idArray = cells->WritePointer(ncells, size);
// if (!this->ReadCells(size, cells->WritePointer(read2,size),
// skip1, read2, skip3) )
if (!this->ReadCells(size, tempArray, skip1, read2, skip3) )
{
this->CloseVTKFile ();
delete [] tempArray;
return 1;
}
for (i = 0; i < size; i++)
{
idArray[i] = tempArray[i];
}
delete [] tempArray;
if (cells && types)
{
output->SetCells(types, cells);
}
}
else if (!strncmp(line,"cell_types",10))
{
piece = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
numPieces = outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
if (!this->Read(&ncells))
{
vtkErrorMacro(<<"Cannot read cell types!");
this->CloseVTKFile ();
return 1;
}
// the number of ints to read befor we get to the piece.
skip1 = piece * ncells / numPieces;
// the number of ints to read as part of piece.
read2 = ((piece+1) * ncells / numPieces) - skip1;
// the number of ints after the piece
skip3 = ncells - skip1 - read2;
//cerr << skip1 << " --- " << read2 << " --- " << skip3 << endl;
// allocate array for piece cell types
types = new int[read2];
if (this->GetFileType() == VTK_BINARY)
{
// suck up newline
this->GetIStream()->getline(line,256);
// skip
if (skip1 != 0)
{
this->GetIStream()
->seekg((long)sizeof(int)*skip1, ios::cur);
}
this->GetIStream()->read((char *)types,sizeof(int)*read2);
// skip
if (skip3 != 0)
{
this->GetIStream()
->seekg((long)sizeof(int)*skip3, ios::cur);
}
if (this->GetIStream()->eof())
{
vtkErrorMacro(<<"Error reading binary cell types!");
this->CloseVTKFile ();
return 1;
}
vtkByteSwap::Swap4BERange(types,read2);
}
else //ascii
{
// skip types before piece
for (i=0; i<skip1; i++)
{
if (!this->Read(&tmp))
{
vtkErrorMacro(<<"Error reading cell types!");
this->CloseVTKFile ();
return 1;
}
}
// read types for piece
for (i=0; i<read2; i++)
{
if (!this->Read(types+i))
{
vtkErrorMacro(<<"Error reading cell types!");
this->CloseVTKFile ();
return 1;
}
}
// skip types after piece
for (i=0; i<skip3; i++)
{
if (!this->Read(&tmp))
{
vtkErrorMacro(<<"Error reading cell types!");
this->CloseVTKFile ();
return 1;
}
}
}
if ( cells && types )
{
output->SetCells(types, cells);
}
}
else if ( ! strncmp(line, "cell_data", 9) )
{
if (!this->Read(&numCells))
{
vtkErrorMacro(<<"Cannot read cell data!");
this->CloseVTKFile ();
return 1;
}
if ( ncells != numCells )
{
vtkErrorMacro(<<"Number of cells don't match!");
this->CloseVTKFile ();
return 1;
}
this->ReadCellData(output, ncells);
break; //out of this loop
}
else if ( ! strncmp(line, "point_data", 10) )
{
if (!this->Read(&npts))
{
vtkErrorMacro(<<"Cannot read point data!");
this->CloseVTKFile ();
return 1;
}
if ( npts != numPts )
{
vtkErrorMacro(<<"Number of points don't match!");
this->CloseVTKFile ();
return 1;
}
this->ReadPointData(output, npts);
break; //out of this loop
}
else
{
vtkErrorMacro(<< "Unrecognized keyword: " << line);
this->CloseVTKFile ();
return 1;
}
}
if ( ! output->GetPoints() ) vtkWarningMacro(<<"No points read!");
//if ( ! (cells && types) ) vtkWarningMacro(<<"No topology read!");
}
else if ( !strncmp(line, "point_data", 10) )
{
vtkWarningMacro(<<"No geometry defined in data file!");
if (!this->Read(&numPts))
{
vtkErrorMacro(<<"Cannot read point data!");
this->CloseVTKFile ();
return 1;
}
this->ReadPointData(output, numPts);
}
else
{
vtkErrorMacro(<< "Unrecognized keyword: " << line);
}
// Clean-up and get out
//
if (types)
{
delete [] types;
}
if (cells)
{
cells->Delete();
}
vtkDebugMacro(<<"Read " <<output->GetNumberOfPoints() <<" points,"
<<output->GetNumberOfCells() <<" cells.\n");
this->CloseVTKFile ();
return 1;
}
//----------------------------------------------------------------------------
int vtkUnstructuredGridReader::FillOutputPortInformation(int,
vtkInformation* info)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkUnstructuredGrid");
return 1;
}
//----------------------------------------------------------------------------
void vtkUnstructuredGridReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}