forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkXMLPartitionedDataSetCollectionReader.cxx
170 lines (150 loc) · 5.68 KB
/
vtkXMLPartitionedDataSetCollectionReader.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
/*=========================================================================
Program: ParaView
Module: vtkXMLPartitionedDataSetCollectionReader.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 "vtkXMLPartitionedDataSetCollectionReader.h"
#include "vtkBase64Utilities.h"
#include "vtkCompositeDataPipeline.h"
#include "vtkCompositeDataSet.h"
#include "vtkDataAssembly.h"
#include "vtkDataSet.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPartitionedDataSet.h"
#include "vtkPartitionedDataSetCollection.h"
#include "vtkSmartPointer.h"
#include "vtkXMLDataElement.h"
#include <cctype> // for std::isspace
namespace
{
vtkSmartPointer<vtkDataAssembly> ReadDataAssembly(
vtkXMLDataElement* elem, vtkXMLPartitionedDataSetCollectionReader* self)
{
if (elem->GetAttribute("encoding") == nullptr ||
strcmp(elem->GetAttribute("encoding"), "base64") != 0 || elem->GetCharacterData() == nullptr)
{
vtkWarningWithObjectMacro(self, "Unsupported DataAssembly encoding. Ignoring.");
return nullptr;
}
vtkNew<vtkDataAssembly> assembly;
const char* encoded_buffer = elem->GetCharacterData();
size_t len_encoded_data = strlen(encoded_buffer);
char* decoded_buffer = new char[len_encoded_data];
// remove leading whitespace, if any.
while (std::isspace(static_cast<int>(*encoded_buffer)))
{
++encoded_buffer;
--len_encoded_data;
}
auto decoded_buffer_len =
vtkBase64Utilities::DecodeSafely(reinterpret_cast<const unsigned char*>(encoded_buffer),
len_encoded_data, reinterpret_cast<unsigned char*>(decoded_buffer), len_encoded_data);
decoded_buffer[decoded_buffer_len] = '\0';
assembly->InitializeFromXML(decoded_buffer);
delete[] decoded_buffer;
return assembly;
}
}
vtkStandardNewMacro(vtkXMLPartitionedDataSetCollectionReader);
//------------------------------------------------------------------------------
vtkXMLPartitionedDataSetCollectionReader::vtkXMLPartitionedDataSetCollectionReader() = default;
//------------------------------------------------------------------------------
vtkXMLPartitionedDataSetCollectionReader::~vtkXMLPartitionedDataSetCollectionReader() = default;
//------------------------------------------------------------------------------
void vtkXMLPartitionedDataSetCollectionReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//------------------------------------------------------------------------------
int vtkXMLPartitionedDataSetCollectionReader::FillOutputPortInformation(
int vtkNotUsed(port), vtkInformation* info)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPartitionedDataSetCollection");
return 1;
}
//------------------------------------------------------------------------------
const char* vtkXMLPartitionedDataSetCollectionReader::GetDataSetName()
{
return "vtkPartitionedDataSetCollection";
}
//------------------------------------------------------------------------------
void vtkXMLPartitionedDataSetCollectionReader::ReadComposite(vtkXMLDataElement* element,
vtkCompositeDataSet* composite, const char* filePath, unsigned int& dataSetIndex)
{
vtkPartitionedDataSetCollection* col = vtkPartitionedDataSetCollection::SafeDownCast(composite);
vtkPartitionedDataSet* ds = vtkPartitionedDataSet::SafeDownCast(composite);
if (!col && !ds)
{
vtkErrorMacro("Unsupported composite dataset.");
return;
}
// count partitions to guide partition allocation when reading in parallel.
const unsigned int numberOfParitions =
ds ? vtkXMLCompositeDataReader::CountNestedElements(element, "DataSet") : 0;
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 (col)
{
index = col->GetNumberOfPartitionedDataSets();
}
else if (ds)
{
index = ds->GetNumberOfPartitions();
}
}
// child is a leaf node, read and insert.
const char* tagName = childXML->GetName();
if (strcmp(tagName, "DataSet") == 0)
{
vtkSmartPointer<vtkDataObject> childDS;
if (this->ShouldReadDataSet(dataSetIndex, index, numberOfParitions))
{
// Read
childDS.TakeReference(this->ReadDataObject(childXML, filePath));
}
// insert
ds->SetPartition(index, childDS);
dataSetIndex++;
}
else if (col != nullptr && strcmp(tagName, "Partitions") == 0)
{
vtkPartitionedDataSet* childDS = vtkPartitionedDataSet::New();
this->ReadComposite(childXML, childDS, filePath, dataSetIndex);
col->SetPartitionedDataSet(index, childDS);
childDS->Delete();
// if XML node has name, set read that.
if (auto name = childXML->GetAttribute("name"))
{
col->GetMetaData(index)->Set(vtkCompositeDataSet::NAME(), name);
}
}
else if (col != nullptr && strcmp(tagName, "DataAssembly") == 0)
{
auto assembly = ::ReadDataAssembly(childXML, this);
col->SetDataAssembly(assembly);
}
else
{
vtkErrorMacro("Syntax error in file.");
return;
}
}
}