forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkXMLHyperOctreeReader.cxx
316 lines (266 loc) · 8.78 KB
/
vtkXMLHyperOctreeReader.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkXMLHyperOctreeReader.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.
=========================================================================*/
//TODO:
// Add support for timesteps
// Add streaming support.
#include "vtkXMLHyperOctreeReader.h"
#include "vtkDataArray.h"
#include "vtkObjectFactory.h"
#include "vtkHyperOctree.h"
#include "vtkHyperOctreeCursor.h"
#include "vtkXMLDataElement.h"
#include "vtkXMLDataParser.h"
#include "vtkInformation.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkIntArray.h"
#include "vtkFieldData.h"
vtkStandardNewMacro(vtkXMLHyperOctreeReader);
//----------------------------------------------------------------------------
vtkXMLHyperOctreeReader::vtkXMLHyperOctreeReader()
{
}
//----------------------------------------------------------------------------
vtkXMLHyperOctreeReader::~vtkXMLHyperOctreeReader()
{
}
//----------------------------------------------------------------------------
void vtkXMLHyperOctreeReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
vtkHyperOctree* vtkXMLHyperOctreeReader::GetOutput()
{
return this->GetOutput(0);
}
//----------------------------------------------------------------------------
vtkHyperOctree* vtkXMLHyperOctreeReader::GetOutput(int idx)
{
return vtkHyperOctree::SafeDownCast( this->GetOutputDataObject(idx) );
}
//----------------------------------------------------------------------------
const char* vtkXMLHyperOctreeReader::GetDataSetName()
{
return "HyperOctree";
}
//----------------------------------------------------------------------------
void vtkXMLHyperOctreeReader::SetupEmptyOutput()
{
this->GetCurrentOutput()->Initialize();
}
//----------------------------------------------------------------------------
int vtkXMLHyperOctreeReader::FillOutputPortInformation(int, vtkInformation *info)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkHyperOctree");
return 1;
}
//----------------------------------------------------------------------------
vtkIdType vtkXMLHyperOctreeReader::GetNumberOfPoints()
{
vtkIdType numPts = 0;
vtkDataSet* output = vtkDataSet::SafeDownCast(this->GetCurrentOutput());
if (output)
{
numPts = output->GetNumberOfPoints();
}
return numPts;
}
//----------------------------------------------------------------------------
vtkIdType vtkXMLHyperOctreeReader::GetNumberOfCells()
{
vtkIdType numCells = 0;
vtkDataSet* output = vtkDataSet::SafeDownCast(this->GetCurrentOutput());
if (output)
{
numCells = output->GetNumberOfCells();
}
return numCells;
}
//----------------------------------------------------------------------------
int vtkXMLHyperOctreeReader::ReadArrayForPoints(vtkXMLDataElement* da,
vtkAbstractArray* outArray)
{
vtkIdType components = outArray->GetNumberOfComponents();
vtkIdType numberOfTuples = this->GetNumberOfPoints();
outArray->SetNumberOfTuples(numberOfTuples);
return this->ReadArrayValues(da, 0, outArray, 0, numberOfTuples*components);
}
//----------------------------------------------------------------------------
int vtkXMLHyperOctreeReader::ReadArrayForCells(vtkXMLDataElement* da,
vtkAbstractArray* outArray)
{
vtkIdType components = outArray->GetNumberOfComponents();
vtkIdType numberOfTuples = this->GetNumberOfCells();
outArray->SetNumberOfTuples(numberOfTuples);
return this->ReadArrayValues(da, 0, outArray, 0, numberOfTuples*components);
}
//----------------------------------------------------------------------------
void vtkXMLHyperOctreeReader::ReadXMLData()
{
//1) vtkXMLReader grandparent class checks if this timestep needs to SetupOutputData, and if so initializes the output.
//2) vtkXMLDataReader parent class reads fielddata
this->Superclass::ReadXMLData();
//3) For Other XML readers, vtkXMLUnstructuredDataReader or vtkXMLStructuredDataReader parent classes use pipeline info to determines what pieces to read and then ReadPieceData is called to read only part of the data. Since HyperOctree is not streamed yet, I'll just read the whole file here instead.
vtkXMLDataElement *ePrimary =
this->XMLParser->GetRootElement()->GetNestedElement(0);
int Dimension;
double Size[3];
double Origin[3];
if (!ePrimary->GetScalarAttribute("Dimension", Dimension))
{
Dimension = 3;
}
if(ePrimary->GetVectorAttribute("Size", 3, Size) != 3)
{
Size[0] = 1;
Size[1] = 1;
Size[2] = 1;
}
if(ePrimary->GetVectorAttribute("Origin", 3, Origin) != 3)
{
Origin[0] = 0;
Origin[1] = 0;
Origin[2] = 0;
}
vtkHyperOctree *output = vtkHyperOctree::SafeDownCast(
this->GetCurrentOutput());
output->SetDimension(Dimension);
output->SetSize(Size);
output->SetOrigin(Origin);
// Find the topology element, which defines the structure of the HyperOctree
// Rebuild the HyperOctree from that.
// This needs to happen before ReadPieceData so that numPoints and numCells
// will be defined.
int numNested = ePrimary->GetNumberOfNestedElements();
int i;
for(i=0; i < numNested; ++i)
{
vtkXMLDataElement* eNested = ePrimary->GetNestedElement(i);
if(strcmp(eNested->GetName(), "Topology") == 0)
{
this->ReadTopology(eNested);
break;
}
}
//Read the pointdata and celldata attribute data.
//We only have one piece so this will suffice.
this->ReadPieceData();
}
//----------------------------------------------------------------------------
void vtkXMLHyperOctreeReader::ReadTopology(vtkXMLDataElement *elem)
{
float progressRange[2] = {0,0};
this->GetProgressRange(progressRange);
//Part spent reading and reconstructing assumed to be roughly equal.
float fractions[3] =
{
0,
0.5,
1
};
this->SetProgressRange(progressRange, 0, fractions);
//Find the topology array and read it into a vtkIntArray
int numNested = elem->GetNumberOfNestedElements();
if (numNested != 1)
{
return;
}
vtkXMLDataElement* tElem = elem->GetNestedElement(0);
// Since topology is a vtkIntArray.
vtkAbstractArray* a = this->CreateArray(tElem);
vtkDataArray *tda = vtkDataArray::SafeDownCast(a);
if (!tda)
{
if (a)
{
a->Delete();
}
return;
}
int numTuples;
if (!tElem->GetScalarAttribute("NumberOfTuples", numTuples))
{
tda->Delete();
return;
}
tda->SetNumberOfTuples(numTuples);
if (!this->ReadArrayValues(tElem, 0, tda, 0, numTuples* tda->GetNumberOfComponents())
/* this->ReadData(tElem, tda->GetVoidPointer(0), tda->GetDataType(),
0, numTuples*tda->GetNumberOfComponents())*/)
{
tda->Delete();
return;
}
vtkIntArray *ta = NULL;
ta = vtkIntArray::SafeDownCast(tda);
if (!ta)
{
tda->Delete();
return;
}
this->SetProgressRange(progressRange, 1, fractions);
//Restore the topology from the vtkIntArray. Do it recursively, cell by cell.
vtkHyperOctreeCursor *cursor=vtkHyperOctree::SafeDownCast(
this->GetCurrentOutput())->NewCellCursor();
cursor->ToRoot();
//Where in the array we need to read from next.
this->ArrayIndex = 0;
if (!this->BuildNextCell(ta, cursor, cursor->GetNumberOfChildren()))
{
vtkErrorMacro( << "Problem reading topology. ");
ta->Delete();
return ;
}
//Cleanup
cursor->Delete();
ta->Delete();
}
//----------------------------------------------------------------------------
int vtkXMLHyperOctreeReader::BuildNextCell(
vtkIntArray *ta,
vtkHyperOctreeCursor *cursor,
int nchildren)
{
int nodeType = ta->GetValue(this->ArrayIndex);
if (nodeType == 1)
{
//leaf, stop now
}
/*
else if (nodeType == 2)
{
//terminal node
//subdivide but stop there
vtkHyperOctree::SafeDownCast(this->GetCurrentOutput())->SubdivideLeaf(cursor);
}
*/
else
{
//internal node
//subdivide
vtkHyperOctree::SafeDownCast(this->GetCurrentOutput())->SubdivideLeaf(cursor);
//then keep going down
int i=0;
while(i<nchildren)
{
cursor->ToChild(i);
this->ArrayIndex++;
if (!this->BuildNextCell(ta, cursor, nchildren))
{
//IO failure somewhere below
return 0;
}
cursor->ToParent();
++i;
}
}
return 1;
}