29 SmartPointer<Mesh<T>> mesh =
nullptr;
33 unsigned vtk_nodes_for_cell_type[15] = {0, 1, 0, 2, 0, 3, 0, 0,
42 : mesh(passedMesh), fileName(passedFileName) {}
45 std::string passedFileName)
46 : mesh(passedMesh), fileFormat(passedFormat), fileName(passedFileName) {}
55 void setFileName(std::string passedFileName) { fileName = passedFileName; }
59 if (mesh ==
nullptr) {
61 .addWarning(
"No mesh was passed to VTKReader. Not reading.")
66 if (fileName.empty()) {
68 .addWarning(
"No file name specified for VTKReader. Not reading.")
74 auto dotPos = fileName.rfind(
'.');
75 if (dotPos == std::string::npos) {
77 .addWarning(
"No valid file format found based on the file ending "
78 "passed to VTKReader. Not reading.")
82 auto ending = fileName.substr(dotPos);
83 if (ending ==
".vtk") {
85 }
else if (ending ==
".vtp") {
87 }
else if (ending ==
".vtu") {
91 .addWarning(
"No valid file format found based on the file ending "
92 "passed to VTKReader. Not reading.")
101 readVTKLegacy(fileName);
103#ifdef VIENNALS_USE_VTK
113 Logger::getInstance()
115 "VTKReader was built without VTK support. Only VTK_LEGACY "
116 "can be used. File not read.")
120 Logger::getInstance()
121 .addWarning(
"No valid file format set for VTKReader. Not reading.")
127#ifdef VIENNALS_USE_VTK
128 void readVTP(std::string filename) {
129 if (mesh ==
nullptr) {
130 Logger::getInstance()
131 .addWarning(
"No mesh was passed to VTKReader.")
137 vtkSmartPointer<vtkXMLPolyDataReader> pReader =
138 vtkSmartPointer<vtkXMLPolyDataReader>::New();
139 pReader->SetFileName(filename.c_str());
142 vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
143 polyData = pReader->GetOutput();
145 mesh->nodes.resize(polyData->GetNumberOfPoints());
146 for (
unsigned i = 0; i < mesh->nodes.size(); ++i) {
147 std::array<double, 3> coords;
148 polyData->GetPoint(i, coords.data());
149 mesh->nodes[i] = coords;
152 vtkSmartPointer<vtkCellArray> cellArray =
153 vtkSmartPointer<vtkCellArray>::New();
156 mesh->vertices.reserve(polyData->GetNumberOfVerts());
157 cellArray = polyData->GetVerts();
158 cellArray->InitTraversal();
159 vtkIdList *pointList = vtkIdList::New();
160 while (cellArray->GetNextCell(pointList)) {
161 std::array<unsigned, 1> cell;
162 cell[0] = pointList->GetId(0);
163 mesh->vertices.push_back(cell);
169 mesh->lines.reserve(polyData->GetNumberOfLines());
170 cellArray = polyData->GetLines();
171 cellArray->InitTraversal();
172 vtkIdList *pointList = vtkIdList::New();
173 while (cellArray->GetNextCell(pointList)) {
174 std::array<unsigned, 2> cell;
175 for (
unsigned i = 0; i < 2; ++i) {
176 cell[i] = pointList->GetId(i);
178 mesh->lines.push_back(cell);
184 mesh->triangles.reserve(polyData->GetNumberOfPolys());
185 cellArray = polyData->GetPolys();
186 cellArray->InitTraversal();
187 vtkIdList *pointList = vtkIdList::New();
188 while (cellArray->GetNextCell(pointList)) {
189 std::array<unsigned, 3> cell;
190 for (
unsigned i = 0; i < 3; ++i) {
191 cell[i] = pointList->GetId(i);
193 mesh->triangles.push_back(cell);
198 vtkSmartPointer<vtkPointData> pointData =
199 vtkSmartPointer<vtkPointData>::New();
200 pointData = polyData->GetPointData();
203 i < static_cast<unsigned>(pointData->GetNumberOfArrays()); ++i) {
204 vtkDataArray *dataArray;
205 dataArray = pointData->GetArray(i);
206 if (dataArray->GetNumberOfComponents() == 1) {
207 mesh->pointData.insertNextScalarData(
209 std::string(pointData->GetArrayName(i)));
210 auto &scalars = *(
mesh->pointData.getScalarData(i));
211 scalars.resize(pointData->GetNumberOfTuples());
212 for (
unsigned j = 0; j < dataArray->GetNumberOfTuples(); ++j) {
213 scalars[j] = dataArray->GetTuple1(j);
215 }
else if (dataArray->GetNumberOfComponents() == 3) {
216 mesh->pointData.insertNextVectorData(
218 std::string(pointData->GetArrayName(i)));
219 auto &vectors = *(
mesh->pointData.getVectorData(i));
220 vectors.resize(pointData->GetNumberOfTuples());
221 for (
unsigned j = 0; j < dataArray->GetNumberOfTuples(); ++j) {
222 std::array<double, 3> vector;
223 dataArray->GetTuple(j, &(vector[0]));
230 vtkSmartPointer<vtkCellData> cellData = vtkSmartPointer<vtkCellData>::New();
231 cellData = polyData->GetCellData();
234 i < static_cast<unsigned>(cellData->GetNumberOfArrays()); ++i) {
235 vtkDataArray *dataArray;
236 dataArray = cellData->GetArray(i);
237 if (cellData->GetNumberOfComponents() == 1) {
238 mesh->cellData.insertNextScalarData(
240 std::string(cellData->GetArrayName(i)));
241 auto &scalars = *(
mesh->cellData.getScalarData(i));
242 scalars.resize(cellData->GetNumberOfTuples());
243 for (
unsigned j = 0; j < dataArray->GetNumberOfTuples(); ++j) {
244 scalars[j] = dataArray->GetTuple1(j);
246 }
else if (cellData->GetNumberOfComponents() == 3) {
247 mesh->cellData.insertNextVectorData(
249 std::string(cellData->GetArrayName(i)));
250 auto &vectors = *(
mesh->cellData.getVectorData(i));
251 vectors.resize(cellData->GetNumberOfTuples());
252 for (
unsigned j = 0; j < dataArray->GetNumberOfTuples(); ++j) {
253 std::array<double, 3> vector;
254 dataArray->GetTuple(j, &(vector[0]));
261 void readVTU(std::string filename) {
262 if (mesh ==
nullptr) {
263 Logger::getInstance()
264 .addWarning(
"No mesh was passed to VTKReader.")
271 vtkSmartPointer<vtkXMLUnstructuredGridReader> greader =
272 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
273 greader->SetFileName(filename.c_str());
276 vtkSmartPointer<vtkUnstructuredGrid> ugrid =
277 vtkSmartPointer<vtkUnstructuredGrid>::New();
278 ugrid = greader->GetOutput();
281 mesh->nodes.resize(ugrid->GetNumberOfPoints());
282 for (
unsigned i = 0; i <
mesh->nodes.size(); ++i) {
283 std::array<double, 3> coords;
284 ugrid->GetPoint(i, &(coords[0]));
285 mesh->nodes[i] = coords;
289 for (
unsigned i = 0; i < ugrid->GetNumberOfCells(); ++i) {
290 vtkIdList *pointList = vtkIdList::New();
291 ugrid->GetCellPoints(i, pointList);
293 switch (ugrid->GetCellType(i)) {
296 std::array<unsigned, 1> vert;
297 vert[0] = pointList->GetId(0);
298 mesh->vertices.push_back(vert);
302 std::array<unsigned, 2> elements;
303 for (
unsigned j = 0; j < 2; ++j) {
304 elements[j] = pointList->GetId(j);
306 mesh->lines.push_back(elements);
310 std::array<unsigned, 3> elements;
311 for (
unsigned j = 0; j < 3; ++j) {
312 elements[j] = pointList->GetId(j);
314 mesh->triangles.push_back(elements);
318 std::array<unsigned, 4> elements;
319 for (
unsigned j = 0; j < 4; ++j) {
320 elements[j] = pointList->GetId(j);
322 mesh->tetras.push_back(elements);
326 std::array<unsigned, 8> elements;
327 for (
unsigned j = 0; j < 8; ++j) {
328 elements[j] = pointList->GetId(j);
330 mesh->hexas.push_back(elements);
336 vtkSmartPointer<vtkPointData> pointData =
337 vtkSmartPointer<vtkPointData>::New();
338 pointData = ugrid->GetPointData();
341 i < static_cast<unsigned>(pointData->GetNumberOfArrays()); ++i) {
342 vtkDataArray *dataArray;
343 dataArray = pointData->GetArray(i);
344 if (dataArray->GetNumberOfComponents() == 1) {
345 mesh->pointData.insertNextScalarData(
347 std::string(pointData->GetArrayName(i)));
348 auto &scalars = *(
mesh->pointData.getScalarData(i));
349 scalars.resize(pointData->GetNumberOfTuples());
350 for (
unsigned j = 0; j < dataArray->GetNumberOfTuples(); ++j) {
351 scalars[j] = dataArray->GetTuple1(j);
353 }
else if (dataArray->GetNumberOfComponents() == 3) {
354 mesh->pointData.insertNextVectorData(
356 std::string(pointData->GetArrayName(i)));
357 auto &vectors = *(
mesh->pointData.getVectorData(i));
358 vectors.resize(pointData->GetNumberOfTuples());
359 for (
unsigned j = 0; j < dataArray->GetNumberOfTuples(); ++j) {
360 std::array<double, 3> vector;
361 dataArray->GetTuple(j, &(vector[0]));
368 vtkSmartPointer<vtkCellData> cellData = vtkSmartPointer<vtkCellData>::New();
369 cellData = ugrid->GetCellData();
372 i < static_cast<unsigned>(cellData->GetNumberOfArrays()); ++i) {
373 vtkDataArray *dataArray;
374 dataArray = cellData->GetArray(i);
375 if (cellData->GetNumberOfComponents() == 1) {
376 mesh->cellData.insertNextScalarData(
378 std::string(cellData->GetArrayName(i)));
379 auto &scalars = *(
mesh->cellData.getScalarData(i));
380 scalars.resize(cellData->GetNumberOfTuples());
381 for (
unsigned j = 0; j < dataArray->GetNumberOfTuples(); ++j) {
382 scalars[j] = dataArray->GetTuple1(j);
384 }
else if (cellData->GetNumberOfComponents() == 3) {
385 mesh->cellData.insertNextVectorData(
387 std::string(cellData->GetArrayName(i)));
388 auto &vectors = *(
mesh->cellData.getVectorData(i));
389 vectors.resize(cellData->GetNumberOfTuples());
390 for (
unsigned j = 0; j < dataArray->GetNumberOfTuples(); ++j) {
391 std::array<double, 3> vector;
392 dataArray->GetTuple(j, &(vector[0]));
401 void readVTKLegacy(std::string filename) {
402 if (mesh ==
nullptr) {
403 Logger::getInstance()
404 .addWarning(
"No mesh was passed to VTKReader.")
411 std::ifstream f(filename.c_str());
413 Logger::getInstance().addError(
"Could not open geometry file!");
417 while (std::getline(f, temp)) {
418 if (temp.find(
"DATASET") != std::string::npos)
421 if (temp.find(
"UNSTRUCTURED_GRID") == std::string::npos) {
422 Logger::getInstance().addError(
"DATASET is not an UNSTRUCTURED_GRID!");
426 while (std::getline(f, temp)) {
427 if (temp.find(
"POINTS") != std::string::npos)
430 int num_nodes = atoi(&temp[temp.find(
" ") + 1]);
432 mesh->nodes.resize(num_nodes);
434 for (
int i = 0; i < num_nodes; i++) {
437 for (
int j = 0; j < 3; j++)
439 for (
int j = 0; j < 3; j++) {
440 mesh->nodes[i][j] = coords[j];
452 while (std::getline(f, temp)) {
453 if (temp.find(
"CELLS") == 0)
457 int num_elems = atoi(&temp[temp.find(
" ") + 1]);
459 std::ifstream f_ct(filename.c_str());
464 while (std::getline(f_ct, temp)) {
465 if (temp.find(
"CELL_TYPES") == 0)
468 int num_cell_types = atoi(&temp[temp.find(
" ") + 1]);
470 if (num_elems != num_cell_types) {
471 Logger::getInstance().addError(
472 "Corrupt input geometry! Number of CELLS and CELL_TYPES "
476 bool is_material =
true;
478 while (std::getline(f_m, temp)) {
479 if (temp.find(
"CELL_DATA") != std::string::npos) {
480 std::getline(f_m, temp);
481 if ((temp.find(
"SCALARS material") != std::string::npos) ||
482 (temp.find(
"SCALARS Material") != std::string::npos)) {
483 std::getline(f_m, temp);
493 std::vector<hrleVectorType<unsigned int, 4>> elements;
494 elements.reserve(num_elems);
496 std::vector<double> materials;
500 unsigned cell_material;
501 for (
int i = 0; i < num_elems; i++) {
505 f_m >> cell_material;
511 unsigned number_nodes = vtk_nodes_for_cell_type[cell_type];
512 if (number_nodes == elems_fake || number_nodes == 0) {
516 std::array<unsigned, 1> elem;
518 mesh->template getElements<1>().push_back(elem);
519 materials.push_back(cell_material);
523 std::array<unsigned, 2> elem;
524 for (
unsigned j = 0; j < number_nodes; ++j) {
527 mesh->template getElements<2>().push_back(elem);
528 materials.push_back(cell_material);
533 std::array<unsigned, 3> elem;
534 for (
unsigned j = 0; j < number_nodes; ++j) {
537 mesh->template getElements<3>().push_back(elem);
538 materials.push_back(cell_material);
544 std::array<unsigned, 4> elem;
545 for (
unsigned j = 0; j < number_nodes; ++j) {
548 mesh->template getElements<4>().push_back(elem);
549 materials.push_back(cell_material);
555 std::array<unsigned, 3> elem;
556 for (
unsigned j = 0; j < 3; ++j) {
559 mesh->template getElements<3>().push_back(
561 materials.push_back(cell_material);
564 mesh->template getElements<3>().push_back(elem);
565 materials.push_back(cell_material);
570 std::ostringstream oss;
571 oss <<
"VTK Cell type " << cell_type
572 <<
" is not supported. Cell ignored..." << std::endl;
573 Logger::getInstance().addWarning(oss.str()).print();
576 std::ostringstream oss;
577 oss <<
"INVALID CELL TYPE! Expected number of nodes: " << number_nodes
578 <<
", Found number of nodes: " << elems_fake
579 <<
"; Ignoring element...";
580 Logger::getInstance().addError(oss.str());
582 f.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
586 mesh->cellData.insertNextScalarData(materials,
"Material");
589 int num_cell_data = 0;
590 while (std::getline(f, temp)) {
591 if (temp.find(
"CELL_DATA") != std::string::npos) {
592 num_cell_data = atoi(&temp[temp.find(
" ") + 1]);
596 std::cout <<
"Read cell data: " << num_cell_data << std::endl;
599 std::cout <<
"reading scalar data" << std::endl;
600 while (std::getline(f, temp)) {
601 if (temp.find(
"SCALARS") != std::string::npos)
608 std::string scalarDataName;
610 int firstS = temp.find(
" ") + 1;
611 int secondS = temp.find(
" ", firstS + 1);
612 scalarDataName = temp.substr(firstS, secondS - firstS);
614 std::vector<double> scalarData;
617 std::getline(f, temp);
618 if (temp.compare(
"LOOKUP_TABLE default") != 0) {
619 Logger::getInstance()
620 .addWarning(
"Wrong lookup table for VTKLegacy: " + temp)
625 for (
int i = 0; i < num_cell_data; ++i) {
628 scalarData.push_back(data);
631 mesh->cellData.insertNextScalarData(scalarData, scalarDataName);