VtkReader.hh 8.69 KB
Newer Older
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
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/


/** \file VtuReader.hh */

// need some extension-methods/libs (pugixml, nanoflann)
#ifdef HAVE_EXTENSIONS

#include "Mesh.h"
#include "boost/filesystem.hpp"
#include "pugixml.hpp"
30
// extension {
31
32
#include "kdtree_nanoflann.h"
#include "VectorOperations.h"
33
// }
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "detail/VtkReader.h"

namespace AMDiS 
{  
  namespace io 
  {
    namespace VtkReader
    {
      
      template<typename T>
      void readByName(std::string filename,
		      std::vector<DOFVector<T>*> dofVectors,
		      std::vector<std::string> componentNames)
      {
48
	using namespace std;
49
50
51
52
53
54
55
56
57
58
59
60
	using namespace pugi;
	using namespace AMDiS::io::VtkReader;

	FUNCNAME("VtuReader::readValue()");
	
	TEST_EXIT(filename != "")("Filename not specified!\n");
	TEST_EXIT(dofVectors.size() > 0)("no DOF vectors specified\n");
	TEST_EXIT(componentNames.size() > 0)("no componentName specified\n");

	TEST_EXIT(boost::filesystem::exists(filename))((filename + " does not exist!\n").c_str());

	xml_document vtu;
61
62
// 	TEST_EXIT(vtu.load_file(filename.c_str()))("Could not load vtu file! Error in xml structure.\n");
	vtu.load_file(filename.c_str());
63
64

	xml_node VTKFile = vtu.child("VTKFile");
65
66
67
	string zlib = VTKFile.attribute("compressor").value();
	TEST_EXIT(zlib == "" || zlib == "vtkZLibDataCompressor")("The only supported compressor is Zlib.\n Should not happen.\n ");

68
69
70
	xml_node UnstructuredGrid = VTKFile.child("UnstructuredGrid");
	xml_node Piece = UnstructuredGrid.child("Piece");
	xml_node Points = Piece.child("Points");
71
72
73
74
	xml_node PointsDataArray = Points.child("DataArray");
	xml_node AppendedData = VTKFile.child("AppendedData");
	
	typedef vector<WorldVector<double> > PL;
75
	PL pointList;
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
	vector<vector<T> > valueList(dofVectors.size());
	
	if(!AppendedData) {
	  
	  xml_node PointData = Piece.child("PointData");
	  string points = Points.child_value("DataArray");
	  detail::string2pointList(points, pointList);
	  
	  T test;

	  for (xml_node DataArray = PointData.child("DataArray"); DataArray; DataArray = DataArray.next_sibling("DataArray")) {
	    string Name = DataArray.attribute("Name").value();
	    for (size_t i = 0; i < componentNames.size(); i++) {
	      if (Name == componentNames[i]) {
		string values = DataArray.last_child().value();
		int nComponents = -1;
		if (DataArray.attribute("NumberOfComponents"))
		  nComponents = DataArray.attribute("NumberOfComponents").as_int();
94
		TEST_EXIT(nComponents == -1 || static_cast<int>(size(test)) <= nComponents)
95
		  ("Can not read values in DOFVector with given value type. Too many components!\n");
96
		detail::string2valueList(values, valueList[i], size(test), nComponents);
97
98
99
100
101
		break;
	      }
	    }
	  }
	} else {
102
	  // Get offset of all data array
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
	  vector<int> offsetVec;
	  int pointsIndex = 0, index = 0;
	  vector<pair<xml_node, int> > pointDataIndex;
	  for (xml_node Parent = Piece.first_child(); Parent; Parent = Parent.next_sibling()) {
	    for(xml_node Data = Parent.child("DataArray"); Data; Data = Data.next_sibling("DataArray"), index++) {
	      string parentName = Data.parent().name();
	      if(parentName == "Points") 
		pointsIndex = index;
	      if(parentName == "PointData")
		pointDataIndex.push_back(make_pair(Data, index));
	      
	      offsetVec.push_back(Data.attribute("offset").as_int());
	    }
	  }
	  
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
	  // Get appended data
	  string encoding = AppendedData.attribute("encoding").value();
	  string appendedData = "";
	  if (encoding == "base64") {
	     appendedData = AppendedData.last_child().value();
	    int start = appendedData.find("_");
	    appendedData = appendedData.substr(start + 1);
	  } else if (encoding == "raw") {
	    // Binary AppendedData block cannot be regonized by pugixml. So we have to
	    // extract the data manually.
	    ifstream file(filename.c_str());
	    TEST_EXIT(file.is_open())("Cannot open file: %s\n", filename.c_str());
	    
	    long startPos = 0, endPos = 0;
	    string line = "";
	    while (getline(file, line))
	      if (line.find("<AppendedData") != std::string::npos)
		break;
	    
	    TEST_EXIT_DBG(!file.eof()) ("Should not happen.\n");
	    startPos = file.tellg();
	    file.seekg(offsetVec[offsetVec.size() - 1], ios_base::cur);
	    
	    while (getline(file, line))
	      if (line.find("</AppendedData") != std::string::npos)
		break;
	      
	    TEST_EXIT_DBG(!file.eof()) ("Should not happen.\n");
	    file.seekg(-line.length(), ios_base::cur);
	    endPos = file.tellg();
	    appendedData.resize(endPos - startPos);
	    file.seekg(startPos);
	    file.read(&appendedData[0], endPos - startPos);
	    int start = appendedData.find("_");
	    appendedData = appendedData.substr(start + 1);
	  } else
	    ERROR_EXIT("Currently the encoding of AppendedData only supports base64 or raw.\n");
	  
	  // Read appended data
157
158
159
160
161
162
163
164
165
166
	  string format = PointsDataArray.attribute("format").value();
	  string type = PointsDataArray.attribute("type").value();
	  TEST_EXIT(format == "appended")("The format of DataArray is not appended in appended mode. Should not happen.\n");
	  TEST_EXIT(type == "Float32" || type == "Float64")("Currently only supports Points with type Float32 and Float64.\n");
	  
	  int len = ((unsigned)(pointsIndex + 1) == offsetVec.size()) ? 
	    appendedData.length() - offsetVec[pointsIndex] :
	    offsetVec[pointsIndex + 1] - offsetVec[pointsIndex];
	    
	  string points = appendedData.substr(offsetVec[pointsIndex], len);
167
168
169
170
171
	  detail::binary2pointList(points, 
				   type, 
				   (zlib != ""), 
				   encoding == "base64", 
				   pointList);
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
	  
	  T test;
	  
	  for (size_t i = 0 ; i < pointDataIndex.size(); i++) {
	    string Name = pointDataIndex[i].first.attribute("Name").value();
	    for (size_t j = 0; j < componentNames.size(); j++) {
	      if(Name == componentNames[j]) {
		format = pointDataIndex[i].first.attribute("format").value();
		type = pointDataIndex[i].first.attribute("type").value();
		TEST_EXIT(format == "appended")("The format of DataArray is not appended in appended mode. Should not happen.\n");
		TEST_EXIT(type == "Float32" || type == "Float64")("Currently only supports PointData with type Float32 and Float64.\n");
		  
		len = ((unsigned)(pointDataIndex[i].second + 1) == offsetVec.size())?
		    appendedData.length() - offsetVec[pointDataIndex[i].second]:
		    offsetVec[pointDataIndex[i].second + 1] - offsetVec[pointDataIndex[i].second];
		    
		string values = appendedData.substr(offsetVec[pointDataIndex[i].second], len);
		
		int nComponents = -1;
		if (pointDataIndex[i].first.attribute("NumberOfComponents"))
		  nComponents = pointDataIndex[i].first.attribute("NumberOfComponents").as_int();
193
		TEST_EXIT(nComponents == -1 || static_cast<int>(size(test)) <= nComponents)
194
		  ("Can not read values in DOFVector with given value type. Too many components!\n");
195
196
197
198
199
200
		detail::binary2valueList(values, type, 
					 (zlib != ""), 
					 encoding == "base64", 
					 valueList[j], 
					 size(test), 
					 nComponents);
201
202
		break;
	      }
203
204
205
206
207
208
209
210
211
212
213
214
215
	    }
	  }
	}
	for (size_t i = 0; i < dofVectors.size(); i++) {
	  TEST_EXIT(valueList[i].size() != 0)
	    ("No values found for component name '%s'!\n", componentNames[i].c_str());
	  TEST_EXIT(pointList.size() == valueList[i].size())
	    ("Coordinates and values[%d] do not match!\n", i);
	}

	DOFVector<WorldVector<double> > coords(dofVectors[0]->getFeSpace(), "coords");
	dofVectors[0]->getFeSpace()->getMesh()->getDofIndexCoords(coords);

216
	extensions::KD_Tree tree(Global::getGeo(WORLD), pointList, 10);
217
218
219
	tree.index->buildIndex();

	DOFIterator<WorldVector<double> > it_coords(&coords, USED_DOFS);
220
	vector<DOFIterator<T>*> it_results;
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
	for (size_t i = 0; i < dofVectors.size(); i++) {
	  it_results.push_back(new DOFIterator<T>(dofVectors[i], USED_DOFS));
	  it_results[i]->reset();
	}
	for (it_coords.reset(); !it_coords.end(); ++it_coords) {
	  size_t idx = detail::getNearestIndex(tree, *it_coords);

	  for (size_t i = 0; i < dofVectors.size(); i++) {
	    if (valueList[i].size() > idx)
	      *(*it_results[i]) = valueList[i][idx];
	    (*it_results[i])++;
	  }
	}
	
	
	MSG("VTU file read from: %s\n", filename.c_str());
      }
      
    } // end namespace VtuReader
  } // end namespace io
} // end namespace

#endif // HAVE_EXTENSIONS