forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkXMLPUnstructuredDataReader.cxx
411 lines (359 loc) · 12.9 KB
/
vtkXMLPUnstructuredDataReader.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
407
408
409
410
411
/*=========================================================================
Program: Visualization Toolkit
Module: vtkXMLPUnstructuredDataReader.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 "vtkXMLPUnstructuredDataReader.h"
#include "vtkXMLDataElement.h"
#include "vtkXMLUnstructuredDataReader.h"
#include "vtkPointSet.h"
#include "vtkCellArray.h"
#include "vtkInformation.h"
#include "vtkStreamingDemandDrivenPipeline.h"
//----------------------------------------------------------------------------
vtkXMLPUnstructuredDataReader::vtkXMLPUnstructuredDataReader()
{
this->TotalNumberOfPoints = 0;
this->TotalNumberOfCells = 0;
}
//----------------------------------------------------------------------------
vtkXMLPUnstructuredDataReader::~vtkXMLPUnstructuredDataReader()
{
}
//----------------------------------------------------------------------------
void vtkXMLPUnstructuredDataReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
vtkPointSet* vtkXMLPUnstructuredDataReader::GetOutputAsPointSet()
{
return vtkPointSet::SafeDownCast( this->GetOutputDataObject(0) );
}
//----------------------------------------------------------------------------
vtkPointSet* vtkXMLPUnstructuredDataReader::GetPieceInputAsPointSet(int piece)
{
vtkXMLDataReader* reader = this->PieceReaders[piece];
if(!reader) { return 0; }
if(reader->GetNumberOfOutputPorts() < 1) { return 0; }
return static_cast<vtkPointSet*>(reader->GetExecutive()->GetOutputData(0));
}
//----------------------------------------------------------------------------
void vtkXMLPUnstructuredDataReader::SetupOutputTotals()
{
int i;
this->TotalNumberOfPoints = 0;
for(i=this->StartPiece; i < this->EndPiece; ++i)
{
if(this->PieceReaders[i])
{
this->TotalNumberOfPoints += this->PieceReaders[i]->GetNumberOfPoints();
}
}
this->StartPoint = 0;
}
//----------------------------------------------------------------------------
void vtkXMLPUnstructuredDataReader::SetupNextPiece()
{
if(this->PieceReaders[this->Piece])
{
this->StartPoint += this->PieceReaders[this->Piece]->GetNumberOfPoints();
}
}
//----------------------------------------------------------------------------
vtkIdType vtkXMLPUnstructuredDataReader::GetNumberOfPoints()
{
return this->TotalNumberOfPoints;
}
//----------------------------------------------------------------------------
vtkIdType vtkXMLPUnstructuredDataReader::GetNumberOfCells()
{
return this->TotalNumberOfCells;
}
//----------------------------------------------------------------------------
vtkIdType vtkXMLPUnstructuredDataReader::GetNumberOfPointsInPiece(int piece)
{
if(this->PieceReaders[piece])
{
return this->PieceReaders[piece]->GetNumberOfPoints();
}
else
{
return 0;
}
}
//----------------------------------------------------------------------------
vtkIdType vtkXMLPUnstructuredDataReader::GetNumberOfCellsInPiece(int piece)
{
if(this->PieceReaders[piece])
{
return this->PieceReaders[piece]->GetNumberOfCells();
}
else
{
return 0;
}
}
//----------------------------------------------------------------------------
void vtkXMLPUnstructuredDataReader::SetupEmptyOutput()
{
this->GetCurrentOutput()->Initialize();
}
//----------------------------------------------------------------------------
// Note that any changes (add or removing information) made to this method
// should be replicated in CopyOutputInformation
void vtkXMLPUnstructuredDataReader::SetupOutputInformation(vtkInformation *outInfo)
{
this->Superclass::SetupOutputInformation(outInfo);
// Set the maximum number of pieces that can be provided by this
// reader.
outInfo->Set(vtkStreamingDemandDrivenPipeline::MAXIMUM_NUMBER_OF_PIECES(), this->NumberOfPieces);
}
//----------------------------------------------------------------------------
void vtkXMLPUnstructuredDataReader::CopyOutputInformation(vtkInformation *outInfo, int port)
{
this->Superclass::CopyOutputInformation(outInfo, port);
vtkInformation *localInfo =
this->GetExecutive()->GetOutputInformation( port );
outInfo->CopyEntry(localInfo,
vtkStreamingDemandDrivenPipeline::MAXIMUM_NUMBER_OF_PIECES() );
}
//----------------------------------------------------------------------------
void vtkXMLPUnstructuredDataReader::SetupOutputData()
{
this->Superclass::SetupOutputData();
// Create the points array.
vtkPoints* points = vtkPoints::New();
if(this->PPointsElement)
{
vtkAbstractArray* aa = this->CreateArray(
this->PPointsElement->GetNestedElement(0));
vtkDataArray* a = vtkDataArray::SafeDownCast(aa);
if(a)
{
a->SetNumberOfTuples(this->GetNumberOfPoints());
points->SetData(a);
a->Delete();
}
else
{
if (aa) { aa->Delete(); }
this->DataError = 1;
}
}
vtkPointSet::SafeDownCast(this->GetCurrentOutput())->SetPoints(points);
points->Delete();
}
//----------------------------------------------------------------------------
void vtkXMLPUnstructuredDataReader::SetupUpdateExtent(int piece,
int numberOfPieces,
int ghostLevel)
{
this->UpdatePiece = piece;
this->UpdateNumberOfPieces = numberOfPieces;
this->UpdateGhostLevel = ghostLevel;
// If more pieces are requested than available, just return empty
// pieces for the extra ones.
if(this->UpdateNumberOfPieces > this->NumberOfPieces)
{
this->UpdateNumberOfPieces = this->NumberOfPieces;
}
// Find the range of pieces to read.
if(this->UpdatePiece < this->UpdateNumberOfPieces)
{
this->StartPiece = ((this->UpdatePiece*this->NumberOfPieces) /
this->UpdateNumberOfPieces);
this->EndPiece = (((this->UpdatePiece+1)*this->NumberOfPieces) /
this->UpdateNumberOfPieces);
}
else
{
this->StartPiece = 0;
this->EndPiece = 0;
}
// Update the information of the pieces we need.
int i;
for(i=this->StartPiece; i < this->EndPiece; ++i)
{
if(this->CanReadPiece(i))
{
this->PieceReaders[i]->UpdateInformation();
vtkXMLUnstructuredDataReader* pReader =
static_cast<vtkXMLUnstructuredDataReader*>(this->PieceReaders[i]);
pReader->SetupUpdateExtent(0, 1, this->UpdateGhostLevel);
}
}
// Find the total size of the output.
this->SetupOutputTotals();
}
//----------------------------------------------------------------------------
int
vtkXMLPUnstructuredDataReader::ReadPrimaryElement(vtkXMLDataElement* ePrimary)
{
if(!this->Superclass::ReadPrimaryElement(ePrimary)) { return 0; }
// Find the PPoints element.
this->PPointsElement = 0;
int i;
int numNested = ePrimary->GetNumberOfNestedElements();
for(i=0;i < numNested; ++i)
{
vtkXMLDataElement* eNested = ePrimary->GetNestedElement(i);
if((strcmp(eNested->GetName(), "PPoints") == 0) &&
(eNested->GetNumberOfNestedElements() == 1))
{
this->PPointsElement = eNested;
}
}
// If PPoints element was not found, we must assume there are 0
// points. If there are found to be points later, the error will be
// reported by ReadPieceData.
return 1;
}
//----------------------------------------------------------------------------
void vtkXMLPUnstructuredDataReader::ReadXMLData()
{
// Get the update request.
int piece;
int numberOfPieces;
int ghostLevel;
vtkInformation* outInfo = this->GetCurrentOutputInformation();
piece = outInfo->Get(
vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
numberOfPieces = outInfo->Get(
vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
ghostLevel = outInfo->Get(
vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS());
vtkDebugMacro("Updating piece " << piece << " of " << numberOfPieces
<< " with ghost level " << ghostLevel);
// Setup the range of pieces that will be read.
this->SetupUpdateExtent(piece, numberOfPieces, ghostLevel);
// If there are no data to read, stop now.
if(this->StartPiece == this->EndPiece)
{
return;
}
vtkDebugMacro("Reading piece range [" << this->StartPiece
<< ", " << this->EndPiece << ") from file.");
// Let superclasses read data. This also allocates output data.
this->Superclass::ReadXMLData();
// Split current progress range based on fraction contributed by
// each piece.
float progressRange[2] = {0,0};
this->GetProgressRange(progressRange);
// Calculate the cumulative fraction of data contributed by each
// piece (for progress).
float* fractions = new float[this->EndPiece-this->StartPiece+1];
int i;
fractions[0] = 0;
for(i=this->StartPiece; i < this->EndPiece; ++i)
{
int index = i-this->StartPiece;
fractions[index+1] = (fractions[index] +
this->GetNumberOfPointsInPiece(i) +
this->GetNumberOfCellsInPiece(i));
}
if(fractions[this->EndPiece-this->StartPiece] == 0)
{
fractions[this->EndPiece-this->StartPiece] = 1;
}
for(i=this->StartPiece; i < this->EndPiece; ++i)
{
int index = i-this->StartPiece;
fractions[index+1] = fractions[index+1] / fractions[this->EndPiece-this->StartPiece];
}
// Read the data needed from each piece.
for(i=this->StartPiece; (i < this->EndPiece && !this->AbortExecute &&
!this->DataError); ++i)
{
// Set the range of progress for this piece.
this->SetProgressRange(progressRange, i-this->StartPiece, fractions);
if(!this->Superclass::ReadPieceData(i))
{
// An error occurred while reading the piece.
this->DataError = 1;
}
this->SetupNextPiece();
}
delete [] fractions;
}
//----------------------------------------------------------------------------
int vtkXMLPUnstructuredDataReader::ReadPieceData()
{
// Use the internal reader to read the piece.
vtkPointSet* input = this->GetPieceInputAsPointSet(this->Piece);
input->SetUpdateExtent(0, 1, this->UpdateGhostLevel);
input->Update();
vtkPointSet* output = vtkPointSet::SafeDownCast(this->GetCurrentOutput());
// If there are some points, but no PPoints element, report the
// error.
if(!this->PPointsElement && (this->GetNumberOfPoints() > 0))
{
vtkErrorMacro("Could not find PPoints element with 1 array.");
return 0;
}
if (!input->GetPoints())
{
return 0;
}
// Copy the points array.
this->CopyArrayForPoints(input->GetPoints()->GetData(),
output->GetPoints()->GetData());
// Let the superclass read the data it wants.
return this->Superclass::ReadPieceData();
}
//----------------------------------------------------------------------------
void vtkXMLPUnstructuredDataReader::CopyArrayForPoints(vtkDataArray* inArray,
vtkDataArray* outArray)
{
if(!this->PieceReaders[this->Piece])
{
return;
}
if (inArray == NULL || outArray == NULL)
{
return;
}
vtkIdType numPoints = this->PieceReaders[this->Piece]->GetNumberOfPoints();
vtkIdType components = outArray->GetNumberOfComponents();
vtkIdType tupleSize = inArray->GetDataTypeSize()*components;
memcpy(outArray->GetVoidPointer(this->StartPoint*components),
inArray->GetVoidPointer(0), numPoints*tupleSize);
}
//----------------------------------------------------------------------------
void vtkXMLPUnstructuredDataReader::CopyCellArray(vtkIdType totalNumberOfCells,
vtkCellArray* inCells,
vtkCellArray* outCells)
{
// Allocate memory in the output connectivity array.
vtkIdType curSize = 0;
if(outCells->GetData())
{
curSize = outCells->GetData()->GetNumberOfTuples();
}
vtkIdTypeArray* inData = inCells->GetData();
vtkIdType newSize = curSize+inData->GetNumberOfTuples();
vtkIdType* in = inData->GetPointer(0);
vtkIdType* end = inData->GetPointer(inData->GetNumberOfTuples());
vtkIdType* out = outCells->WritePointer(totalNumberOfCells, newSize);
out += curSize;
// Copy the connectivity data.
while(in < end)
{
vtkIdType length = *in++;
*out++ = length;
// Copy the point indices, but increment them for the appended
// version's index.
vtkIdType j;
for(j=0;j < length; ++j)
{
out[j] = in[j]+this->StartPoint;
}
in += length;
out += length;
}
}