forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkXMLHyperOctreeWriter.cxx
371 lines (308 loc) · 10 KB
/
vtkXMLHyperOctreeWriter.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkXMLHyperOctreeWriter.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 "vtkXMLHyperOctreeWriter.h"
#include "vtkObjectFactory.h"
#include "vtkInformation.h"
#include "vtkCellData.h"
#include "vtkPointData.h"
#include "vtkHyperOctree.h"
#include "vtkHyperOctreeCursor.h"
#include "vtkIntArray.h"
#include "vtkErrorCode.h"
#define vtkOffsetsManager_DoNotInclude
#include "vtkOffsetsManagerArray.h"
#undef vtkOffsetsManager_DoNotInclude
vtkStandardNewMacro(vtkXMLHyperOctreeWriter);
//----------------------------------------------------------------------------
vtkXMLHyperOctreeWriter::vtkXMLHyperOctreeWriter()
{
this->TopologyArray = NULL;
this->TopologyOM = new OffsetsManagerGroup;
this->PointDataOM = new OffsetsManagerGroup;
this->CellDataOM = new OffsetsManagerGroup;
this->TopologyOM->Allocate(1,1);
}
//----------------------------------------------------------------------------
vtkXMLHyperOctreeWriter::~vtkXMLHyperOctreeWriter()
{
if (this->TopologyArray)
{
this->TopologyArray->Delete();
}
delete this->TopologyOM;
delete this->PointDataOM;
delete this->CellDataOM;
}
//----------------------------------------------------------------------------
void vtkXMLHyperOctreeWriter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
//----------------------------------------------------------------------------
vtkHyperOctree* vtkXMLHyperOctreeWriter::GetInput()
{
return static_cast<vtkHyperOctree*>(this->Superclass::GetInput());
}
//----------------------------------------------------------------------------
const char* vtkXMLHyperOctreeWriter::GetDefaultFileExtension()
{
return "vto";
}
//----------------------------------------------------------------------------
const char* vtkXMLHyperOctreeWriter::GetDataSetName()
{
return "HyperOctree";
}
//----------------------------------------------------------------------------
int vtkXMLHyperOctreeWriter::FillInputPortInformation(
int , vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkHyperOctree");
return 1;
}
//----------------------------------------------------------------------------
int vtkXMLHyperOctreeWriter::WriteData()
{
//write XML header and VTK file header and file attributes
if (!this->StartFile())
{
return 0;
}
vtkIndent indent = vtkIndent().GetNextIndent();
if (!this->StartPrimElement(indent))
{
return 0;
}
if (!this->WriteTopology(indent.GetNextIndent()))
{
return 0;
}
if (!this->WriteAttributeData(indent.GetNextIndent()))
{
return 0;
}
this->WriteFieldData(indent.GetNextIndent());
if (!this->FinishPrimElement(indent))
{
return 0;
}
if (this->GetDataMode() == vtkXMLWriter::Appended)
{
float progressRange[2] = {0,0};
this->GetProgressRange(progressRange);
//Part spent serializing and writing assumed to be roughly equal.
float fractions[5] =
{
0,
0.25,
0.5,
0.75,
1
};
this->SetProgressRange(progressRange, 0, fractions);
this->StartAppendedData();
//write out the data arrays in the appended data block while going back
//and filling in empty offset space in previously written entries
this->WriteArrayAppendedData
(this->TopologyArray,
this->TopologyOM->GetElement(0).GetPosition(0),
this->TopologyOM->GetElement(0).GetOffsetValue(0));
double *range = this->TopologyArray->GetRange(-1);
this->ForwardAppendedDataDouble
(this->TopologyOM->GetElement(0).GetRangeMinPosition(0),
range[0],"RangeMin" );
this->ForwardAppendedDataDouble
(this->TopologyOM->GetElement(0).GetRangeMaxPosition(0),
range[1],"RangeMax" );
this->SetProgressRange(progressRange, 1, fractions);
this->WritePointDataAppendedData(this->GetInput()->GetPointData(), 0, this->PointDataOM);
this->SetProgressRange(progressRange, 2, fractions);
this->WriteCellDataAppendedData(this->GetInput()->GetCellData(), 0, this->CellDataOM);
this->SetProgressRange(progressRange, 3, fractions);
this->WriteFieldDataAppendedData(this->GetInput()->GetFieldData(), 0, this->FieldDataOM);
this->EndAppendedData();
}
this->TopologyArray->Delete();
this->TopologyArray = NULL;
if (!this->EndFile())
{
return 0;
}
return 1;
}
//----------------------------------------------------------------------------
int vtkXMLHyperOctreeWriter::StartPrimElement(vtkIndent indent)
{
ostream& os = *(this->Stream);
if(!this->WritePrimaryElement(os, indent))
{
return 0;
}
return 1;
}
//----------------------------------------------------------------------------
void vtkXMLHyperOctreeWriter::WritePrimaryElementAttributes(ostream &os, vtkIndent indent)
{
this->Superclass::WritePrimaryElementAttributes(os, indent);
vtkHyperOctree* input = this->GetInput();
this->WriteScalarAttribute("Dimension", input->GetDimension());
this->WriteVectorAttribute("Size", 3, input->GetSize());
this->WriteVectorAttribute("Origin", 3, input->GetOrigin());
}
//----------------------------------------------------------------------------
int vtkXMLHyperOctreeWriter::WriteTopology(vtkIndent indent)
{
if (this->TopologyArray)
{
this->TopologyArray->Delete();
}
this->TopologyArray = vtkIntArray::New();
this->TopologyArray->SetNumberOfComponents(1);
vtkHyperOctree* input = this->GetInput();
vtkHyperOctreeCursor *cursor=input->NewCellCursor();
cursor->ToRoot();
//record structure into an array
float progressRange[2] = {0,0};
this->GetProgressRange(progressRange);
//Part spent serializing and writing assumed to be roughly equal.
float fractions[3] =
{
0,
0.5,
1
};
this->SetProgressRange(progressRange, 0, fractions);
this->SerializeTopology(cursor, cursor->GetNumberOfChildren());
//write out the array
this->SetProgressRange(progressRange, 1, fractions);
ostream& os = *(this->Stream);
os << indent << "<" << "Topology" << ">\n";
os.flush();
if (os.fail())
{
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return 0;
}
//...,1) to save length of array for easy recovery by reader
if (this->GetDataMode() == vtkXMLWriter::Appended)
{
this->WriteArrayAppended(this->TopologyArray,
indent.GetNextIndent(),
this->TopologyOM->GetElement(0),
"Topology", 1, 0);
}
else
{
this->WriteArrayInline(this->TopologyArray,
indent.GetNextIndent(), "Topology", 1);
}
os << indent << "</" << "Topology" << ">\n";
os.flush();
if (os.fail())
{
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return 0;
}
cursor->Delete();
return 1;
}
//----------------------------------------------------------------------------
void vtkXMLHyperOctreeWriter::SerializeTopology(vtkHyperOctreeCursor *cursor,
int nchildren)
{
if (cursor->CurrentIsLeaf())
{
//this node is a leaf, we must stop now
this->TopologyArray->InsertNextValue(1);
}
/*
else if (cursor->CurrentIsTerminalNode())
{
//this node has 'nchildren' children,
//all of which are leaves, so we can stop now too
this->TopologyArray->InsertNextValue(2);
}
*/
else
{
//this node has 'nchildren' children,
//some of which are internal nodes, so we must continue down
this->TopologyArray->InsertNextValue(0);
int i=0;
while(i<nchildren)
{
cursor->ToChild(i);
this->SerializeTopology(cursor, nchildren);
cursor->ToParent();
++i;
}
}
}
//----------------------------------------------------------------------------
int vtkXMLHyperOctreeWriter::WriteAttributeData(vtkIndent indent)
{
// Write the point data and cell data arrays.
vtkDataSet* input = this->GetInputAsDataSet();
// Split progress between point data and cell data arrays.
float progressRange[2] = {0,0};
this->GetProgressRange(progressRange);
int pdArrays = input->GetPointData()->GetNumberOfArrays();
int cdArrays = input->GetCellData()->GetNumberOfArrays();
int total = (pdArrays+cdArrays)? (pdArrays+cdArrays):1;
float fractions[3] =
{
0,
float(pdArrays)/total,
1
};
// Set the range of progress for the point data arrays.
this->SetProgressRange(progressRange, 0, fractions);
if (this->GetDataMode() == vtkXMLWriter::Appended)
{
this->WritePointDataAppended(input->GetPointData(), indent, this->PointDataOM);
}
else
{
this->WritePointDataInline(input->GetPointData(), indent);
}
if (this->ErrorCode == vtkErrorCode::OutOfDiskSpaceError)
{
return 0;
}
// Set the range of progress for the cell data arrays.
this->SetProgressRange(progressRange, 1, fractions);
if (this->GetDataMode() == vtkXMLWriter::Appended)
{
this->WriteCellDataAppended(input->GetCellData(), indent, this->CellDataOM);
}
else
{
this->WriteCellDataInline(input->GetCellData(), indent);
}
return 1;
}
//----------------------------------------------------------------------------
int vtkXMLHyperOctreeWriter::FinishPrimElement(vtkIndent indent)
{
ostream& os = *(this->Stream);
// End the primary element.
os << indent << "</" << this->GetDataSetName() << ">\n";
os.flush();
if (os.fail())
{
this->SetErrorCode(vtkErrorCode::OutOfDiskSpaceError);
return 0;
}
return 1;
}