forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkXMLMultiBlockDataReader.cxx
347 lines (322 loc) · 10.6 KB
/
vtkXMLMultiBlockDataReader.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
/*=========================================================================
Program: ParaView
Module: vtkXMLMultiBlockDataReader.cxx
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html 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 "vtkXMLMultiBlockDataReader.h"
#include "vtkCompositeDataSet.h"
#include "vtkCompositeDataPipeline.h"
#include "vtkDataSet.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkMultiPieceDataSet.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include "vtkXMLDataElement.h"
vtkStandardNewMacro(vtkXMLMultiBlockDataReader);
//----------------------------------------------------------------------------
vtkXMLMultiBlockDataReader::vtkXMLMultiBlockDataReader()
{
}
//----------------------------------------------------------------------------
vtkXMLMultiBlockDataReader::~vtkXMLMultiBlockDataReader()
{
}
//----------------------------------------------------------------------------
void vtkXMLMultiBlockDataReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
int vtkXMLMultiBlockDataReader::FillOutputPortInformation(
int vtkNotUsed(port), vtkInformation* info)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkMultiBlockDataSet");
return 1;
}
//----------------------------------------------------------------------------
const char* vtkXMLMultiBlockDataReader::GetDataSetName()
{
return "vtkMultiBlockDataSet";
}
//----------------------------------------------------------------------------
// This version does not support multiblock of multiblocks, so our work is
// simple.
void vtkXMLMultiBlockDataReader::ReadVersion0(vtkXMLDataElement* element,
vtkCompositeDataSet* composite, const char* filePath,
unsigned int &dataSetIndex)
{
vtkMultiBlockDataSet* mblock = vtkMultiBlockDataSet::SafeDownCast(composite);
unsigned int numElems = element->GetNumberOfNestedElements();
for (unsigned int cc=0; cc < numElems; ++cc)
{
vtkXMLDataElement* childXML = element->GetNestedElement(cc);
if (!childXML || !childXML->GetName() ||
strcmp(childXML->GetName(), "DataSet") != 0)
{
continue;
}
int group = 0;
int index = 0;
if (childXML->GetScalarAttribute("group", group) &&
childXML->GetScalarAttribute("dataset", index))
{
vtkSmartPointer<vtkDataSet> dataset;
if (this->ShouldReadDataSet(dataSetIndex))
{
dataset.TakeReference(this->ReadDataset(childXML, filePath));
}
vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(
mblock->GetBlock(group));
if (!block)
{
block = vtkMultiBlockDataSet::New();
mblock->SetBlock(group, block);
block->Delete();
}
block->SetBlock(index, dataset);
}
dataSetIndex++;
}
}
//----------------------------------------------------------------------------
void vtkXMLMultiBlockDataReader::ReadComposite(vtkXMLDataElement* element,
vtkCompositeDataSet* composite, const char* filePath,
unsigned int &dataSetIndex)
{
vtkMultiBlockDataSet* mblock = vtkMultiBlockDataSet::SafeDownCast(composite);
vtkMultiPieceDataSet* mpiece = vtkMultiPieceDataSet::SafeDownCast(composite);
if (!mblock && !mpiece)
{
vtkErrorMacro("Unsuported composite dataset.");
return;
}
if (this->GetFileMajorVersion() < 1)
{
// Read legacy file.
this->ReadVersion0(element, composite, filePath, dataSetIndex);
return;
}
unsigned int maxElems = element->GetNumberOfNestedElements();
for (unsigned int cc=0; cc < maxElems; ++cc)
{
vtkXMLDataElement* childXML = element->GetNestedElement(cc);
if (!childXML || !childXML->GetName())
{
continue;
}
int index = 0;
if (!childXML->GetScalarAttribute("index", index))
// if index not in the structure file, then
// set up to add at the end
{
if (mblock)
{
index = mblock->GetNumberOfBlocks();
}
else if (mpiece)
{
index = mpiece->GetNumberOfPieces();
}
}
// child is a leaf node, read and insert.
const char* tagName = childXML->GetName();
if (strcmp(tagName, "DataSet") == 0)
{
vtkSmartPointer<vtkDataSet> childDS;
const char* name = 0;
if (this->ShouldReadDataSet(dataSetIndex))
{
// Read
childDS.TakeReference(this->ReadDataset(childXML, filePath));
name = childXML->GetAttribute("name");
}
// insert
if (mblock)
{
mblock->SetBlock(index, childDS);
mblock->GetMetaData(index)->Set(vtkCompositeDataSet::NAME(), name);
}
else if (mpiece)
{
mpiece->SetPiece(index, childDS);
mpiece->GetMetaData(index)->Set(vtkCompositeDataSet::NAME(), name);
}
dataSetIndex++;
}
// Child is a multiblock dataset itself. Create it.
else if (mblock != 0
&& strcmp(tagName, "Block") == 0)
{
vtkMultiBlockDataSet* childDS = vtkMultiBlockDataSet::New();;
this->ReadComposite(childXML, childDS, filePath, dataSetIndex);
const char* name = childXML->GetAttribute("name");
mblock->SetBlock(index, childDS);
mblock->GetMetaData(index)->Set(vtkCompositeDataSet::NAME(), name);
childDS->Delete();
}
// Child is a multipiece dataset. Create it.
else if (mblock!=0
&& strcmp(tagName, "Piece") == 0)
{
vtkMultiPieceDataSet* childDS = vtkMultiPieceDataSet::New();;
this->ReadComposite(childXML, childDS, filePath, dataSetIndex);
const char* name = childXML->GetAttribute("name");
mblock->SetBlock(index, childDS);
mblock->GetMetaData(index)->Set(vtkCompositeDataSet::NAME(), name);
childDS->Delete();
}
else
{
vtkErrorMacro("Syntax error in file.");
return;
}
}
}
namespace
{
vtkInformation* CreateMetaDataIfNecessary(vtkMultiBlockDataSet* mblock,
vtkMultiPieceDataSet* mpiece,
int index)
{
vtkInformation* piece_metadata = 0;
if (mblock)
{
mblock->SetBlock(index, NULL);
piece_metadata = mblock->GetMetaData(index);
}
else if (mpiece)
{
mpiece->SetPiece(index, NULL);
piece_metadata = mpiece->GetMetaData(index);
}
return piece_metadata;
}
}
//----------------------------------------------------------------------------
int vtkXMLMultiBlockDataReader::FillMetaData(vtkCompositeDataSet* metadata,
vtkXMLDataElement* element,
unsigned int &dataSetIndex)
{
vtkMultiBlockDataSet* mblock = vtkMultiBlockDataSet::SafeDownCast(metadata);
vtkMultiPieceDataSet* mpiece = vtkMultiPieceDataSet::SafeDownCast(metadata);
unsigned int maxElems = element->GetNumberOfNestedElements();
for (unsigned int cc=0; cc < maxElems; ++cc)
{
vtkXMLDataElement* childXML = element->GetNestedElement(cc);
if (!childXML || !childXML->GetName())
{
continue;
}
int index = 0;
if (!childXML->GetScalarAttribute("index", index))
// if index not in the structure file, then
// set up to add at the end
{
if (mblock)
{
index = mblock->GetNumberOfBlocks();
}
else if (mpiece)
{
index = mpiece->GetNumberOfPieces();
}
}
// child is a leaf node, read and insert.
const char* tagName = childXML->GetName();
if (strcmp(tagName, "DataSet") == 0)
{
vtkInformation* piece_metadata = CreateMetaDataIfNecessary(mblock, mpiece, index);
double bounding_box[6];
if (childXML->GetVectorAttribute("bounding_box", 6, bounding_box) == 6)
{
if (piece_metadata)
{
piece_metadata->Set(
vtkStreamingDemandDrivenPipeline::PIECE_BOUNDING_BOX(),
bounding_box, 6);
}
}
int extent[6];
if (childXML->GetVectorAttribute("extent", 6, extent) == 6)
{
if (piece_metadata)
{
piece_metadata->Set(
vtkDataObject::PIECE_EXTENT(),
extent, 6);
}
}
dataSetIndex++;
}
// Child is a multiblock dataset itself. Create it.
else if (mblock != 0
&& strcmp(tagName, "Block") == 0)
{
vtkMultiBlockDataSet* childDS = vtkMultiBlockDataSet::New();
this->FillMetaData(childDS, childXML, dataSetIndex);
if (mblock)
{
mblock->SetBlock(index, childDS);
}
else if (mpiece)
{
vtkErrorMacro("Multipiece data can't have composite children.");
return 0;
}
childDS->Delete();
}
// Child is a multipiece dataset. Create it.
else if (mblock!=0
&& strcmp(tagName, "Piece") == 0)
{
vtkMultiPieceDataSet* childDS = vtkMultiPieceDataSet::New();;
this->FillMetaData(childDS, childXML, dataSetIndex);
mblock->SetBlock(index, childDS);
childDS->Delete();
int whole_extent[6];
if (childXML->GetVectorAttribute("whole_extent", 6, whole_extent) == 6)
{
vtkInformation* piece_metadata = mblock->GetMetaData(index);
piece_metadata->Set(
vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
whole_extent, 6);
}
}
else
{
vtkErrorMacro("Syntax error in file.");
return 0;
}
}
return 1;
}
//----------------------------------------------------------------------------
int vtkXMLMultiBlockDataReader::RequestInformation(
vtkInformation *request,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
this->Superclass::RequestInformation(request, inputVector, outputVector);
if (this->GetFileMajorVersion() < 1)
{
return 1;
}
vtkInformation* info = outputVector->GetInformationObject(0);
vtkSmartPointer<vtkMultiBlockDataSet> metadata =
vtkSmartPointer<vtkMultiBlockDataSet>::New();
unsigned int dataSetIndex = 0;
if (!this->FillMetaData(metadata, this->GetPrimaryElement(), dataSetIndex))
{
return 0;
}
info->Set(vtkCompositeDataPipeline::COMPOSITE_DATA_META_DATA(),
metadata);
return 1;
}