34 using MetaDataType = std::unordered_map<std::string, std::vector<double>>;
36 SmartPointer<Mesh<T>> mesh =
nullptr;
39 MetaDataType metaData;
41#ifdef VIENNALS_USE_VTK
42 template <
class In,
class Out>
43 void addDataFromMesh(
const In &inData, Out outData)
const {
45 for (
unsigned i = 0; i < inData.getScalarDataSize(); ++i) {
46 vtkSmartPointer<vtkFloatArray> pointData =
47 vtkSmartPointer<vtkFloatArray>::New();
48 pointData->SetNumberOfComponents(1);
49 pointData->SetName(inData.getScalarDataLabel(i).c_str());
50 auto scalars = *(inData.getScalarData(i));
51 for (
unsigned j = 0; j < inData.getScalarData(i)->size(); ++j) {
52 pointData->InsertNextValue(scalars[j]);
54 outData->AddArray(pointData);
58 for (
unsigned i = 0; i < inData.getVectorDataSize(); ++i) {
59 vtkSmartPointer<vtkFloatArray> vectorData =
60 vtkSmartPointer<vtkFloatArray>::New();
61 vectorData->SetNumberOfComponents(3);
62 vectorData->SetName(inData.getVectorDataLabel(i).c_str());
63 auto vectors = *(inData.getVectorData(i));
64 for (
unsigned j = 0; j < inData.getVectorData(i)->size(); ++j) {
65 vectorData->InsertNextTuple3(vectors[j][0], vectors[j][1],
68 outData->AddArray(vectorData);
72 void addMetaDataToVTK(vtkDataSet *data)
const {
73 if (metaData.empty()) {
78 vtkSmartPointer<vtkFieldData> fieldData = data->GetFieldData();
79 for (
const auto &meta : metaData) {
80 if (meta.second.empty())
83 vtkSmartPointer<vtkFloatArray> metaDataArray =
84 vtkSmartPointer<vtkFloatArray>::New();
85 metaDataArray->SetName(meta.first.c_str());
86 metaDataArray->SetNumberOfValues(meta.second.size());
87 for (
size_t i = 0; i < meta.second.size(); ++i) {
88 metaDataArray->SetValue(i, meta.second[i]);
90 fieldData->AddArray(metaDataArray);
101 : mesh(passedMesh), fileName(std::move(passedFileName)) {}
104 std::string passedFileName)
105 : mesh(passedMesh), fileFormat(passedFormat),
106 fileName(std::move(passedFileName)) {}
115 fileName = std::move(passedFileName);
119 metaData = passedMetaData;
123 metaData[key] = std::vector<double>{value};
126 void addMetaData(
const std::string &key,
const std::vector<double> &values) {
127 metaData[key] = values;
131 for (
const auto &pair : newMetaData) {
132 metaData[pair.first] = pair.second;
138 if (mesh ==
nullptr) {
139 Logger::getInstance()
140 .addError(
"No mesh was passed to VTKWriter.")
145 if (fileName.empty()) {
146 VIENNACORE_LOG_ERROR(
"No file name specified for VTKWriter.");
149 if (mesh->nodes.empty()) {
150 VIENNACORE_LOG_WARNING(
"Writing empty mesh.");
155 auto dotPos = fileName.rfind(
'.');
156 if (dotPos == std::string::npos) {
159 auto ending = fileName.substr(dotPos);
160 if (ending ==
".vtk") {
162 }
else if (ending ==
".vtp") {
164 }
else if (ending ==
".vtu") {
167 Logger::getInstance()
168 .addError(
"No valid file format found based on the file ending "
169 "passed to VTKWriter.")
177 switch (fileFormat) {
179 writeVTKLegacy(fileName);
181#ifdef VIENNALS_USE_VTK
191 VIENNACORE_LOG_WARNING(
192 "VTKWriter was built without VTK support. Falling back "
194 writeVTKLegacy(fileName);
198 VIENNACORE_LOG_ERROR(
"No valid file format set for VTKWriter.");
203#ifdef VIENNALS_USE_VTK
204 void writeVTP(std::string filename)
const {
205 if (mesh ==
nullptr) {
206 Logger::getInstance()
207 .addError(
"No mesh was passed to VTKWriter.")
212 if (filename.find(
".vtp") != filename.size() - 4)
214 vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
217 vtkSmartPointer<vtkPoints> polyPoints = vtkSmartPointer<vtkPoints>::New();
218 for (
auto it = mesh->getNodes().begin(); it != mesh->getNodes().end();
220 polyPoints->InsertNextPoint((*it)[0], (*it)[1], (*it)[2]);
222 polyData->SetPoints(polyPoints);
225 if (mesh->vertices.size() > 0) {
226 vtkSmartPointer<vtkCellArray> polyCells =
227 vtkSmartPointer<vtkCellArray>::New();
228 for (
auto it = mesh->vertices.begin(); it != mesh->vertices.end(); ++it) {
229 polyCells->InsertNextCell(1);
230 polyCells->InsertCellPoint((*it)[0]);
232 polyData->SetVerts(polyCells);
236 if (mesh->lines.size() > 0) {
237 vtkSmartPointer<vtkCellArray> polyCells =
238 vtkSmartPointer<vtkCellArray>::New();
239 for (
auto it = mesh->lines.begin(); it != mesh->lines.end(); ++it) {
240 polyCells->InsertNextCell(2);
241 for (
unsigned i = 0; i < 2; ++i) {
242 polyCells->InsertCellPoint((*it)[i]);
245 polyData->SetLines(polyCells);
249 if (mesh->triangles.size() > 0) {
250 vtkSmartPointer<vtkCellArray> polyCells =
251 vtkSmartPointer<vtkCellArray>::New();
252 for (
auto it = mesh->triangles.begin(); it != mesh->triangles.end();
254 polyCells->InsertNextCell(3);
255 for (
unsigned i = 0; i < 3; ++i) {
256 polyCells->InsertCellPoint((*it)[i]);
259 polyData->SetPolys(polyCells);
262 addDataFromMesh(mesh->pointData, polyData->GetPointData());
263 addDataFromMesh(mesh->cellData, polyData->GetCellData());
264 addMetaDataToVTK(polyData);
266 vtkSmartPointer<vtkXMLPolyDataWriter> pwriter =
267 vtkSmartPointer<vtkXMLPolyDataWriter>::New();
268 pwriter->SetFileName(filename.c_str());
269 pwriter->SetInputData(polyData);
273 void writeVTU(std::string filename)
const {
274 if (mesh ==
nullptr) {
275 Logger::getInstance()
276 .addError(
"No mesh was passed to VTKWriter.")
281 if (filename.find(
".vtu") != filename.size() - 4)
284 vtkSmartPointer<vtkUnstructuredGrid> uGrid =
285 vtkSmartPointer<vtkUnstructuredGrid>::New();
288 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
289 for (
auto it = mesh->getNodes().begin(); it != mesh->getNodes().end();
291 points->InsertNextPoint((*it)[0], (*it)[1], (*it)[2]);
293 uGrid->SetPoints(points);
296 vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
297 std::vector<int> cellTypes;
298 cellTypes.reserve(mesh->vertices.size() + mesh->lines.size() +
299 mesh->triangles.size() + mesh->tetras.size() +
303 if (mesh->vertices.size() > 0) {
304 for (
auto it = mesh->vertices.begin(); it != mesh->vertices.end(); ++it) {
305 cells->InsertNextCell(1);
306 cells->InsertCellPoint((*it)[0]);
307 cellTypes.push_back(1);
312 if (mesh->lines.size() > 0) {
313 for (
auto it = mesh->lines.begin(); it != mesh->lines.end(); ++it) {
314 cells->InsertNextCell(2);
315 for (
unsigned i = 0; i < 2; ++i) {
316 cells->InsertCellPoint((*it)[i]);
318 cellTypes.push_back(3);
323 if (mesh->triangles.size() > 0) {
324 for (
auto it = mesh->triangles.begin(); it != mesh->triangles.end();
326 cells->InsertNextCell(3);
327 for (
unsigned i = 0; i < 3; ++i) {
328 cells->InsertCellPoint((*it)[i]);
330 cellTypes.push_back(5);
335 if (mesh->tetras.size() > 0) {
336 for (
auto it = mesh->tetras.begin(); it != mesh->tetras.end(); ++it) {
337 cells->InsertNextCell(4);
338 for (
unsigned i = 0; i < 4; ++i) {
339 cells->InsertCellPoint((*it)[i]);
341 cellTypes.push_back(10);
346 if (mesh->hexas.size() > 0) {
347 for (
auto it = mesh->hexas.begin(); it != mesh->hexas.end(); ++it) {
348 cells->InsertNextCell(8);
349 for (
unsigned i = 0; i < 8; ++i) {
350 cells->InsertCellPoint((*it)[i]);
352 cellTypes.push_back(12);
357 uGrid->SetCells(&(cellTypes[0]), cells);
359 addDataFromMesh(mesh->pointData, uGrid->GetPointData());
360 addDataFromMesh(mesh->cellData, uGrid->GetCellData());
361 addMetaDataToVTK(uGrid);
390 vtkSmartPointer<vtkXMLUnstructuredGridWriter> owriter =
391 vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
392 owriter->SetFileName(filename.c_str());
393 owriter->SetInputData(uGrid);
399 void writeVTKLegacy(
const std::string &filename) {
400 if (mesh ==
nullptr) {
401 Logger::getInstance()
402 .addError(
"No mesh was passed to VTKWriter.")
407 std::ofstream f(filename.c_str());
409 f <<
"# vtk DataFile Version 2.0" << std::endl;
410 f << ((!mesh->lines.empty()) ? 2 : 3) <<
"D Surface" << std::endl;
411 f <<
"ASCII" << std::endl;
412 f <<
"DATASET UNSTRUCTURED_GRID" << std::endl;
413 f <<
"POINTS " << mesh->nodes.size() <<
" float" << std::endl;
416 for (
unsigned int i = 0; i < mesh->nodes.size(); i++) {
417 for (
int j = 0; j < 3; j++)
418 f <<
static_cast<float>(mesh->nodes[i][j]) <<
" ";
422 const unsigned numberOfCells = mesh->vertices.size() + mesh->lines.size() +
423 mesh->triangles.size() +
424 mesh->tetras.size() + mesh->hexas.size();
425 const unsigned cellDataSize =
426 2 * mesh->vertices.size() + 3 * mesh->lines.size() +
427 4 * mesh->triangles.size() + 5 * mesh->tetras.size() +
428 9 * mesh->hexas.size();
430 f <<
"CELLS " << numberOfCells <<
" " << cellDataSize << std::endl;
433 for (
unsigned int i = 0; i < mesh->vertices.size(); i++) {
434 f << 1 <<
" " << mesh->vertices[i][0] << std::endl;
436 for (
unsigned int i = 0; i < mesh->lines.size(); i++) {
438 for (
int j = 0; j < 2; j++)
439 f << mesh->lines[i][j] <<
" ";
443 for (
unsigned int i = 0; i < mesh->triangles.size(); i++) {
445 for (
int j = 0; j < 3; j++)
446 f << mesh->triangles[i][j] <<
" ";
450 for (
unsigned int i = 0; i < mesh->tetras.size(); i++) {
452 for (
int j = 0; j < 4; j++)
453 f << mesh->tetras[i][j] <<
" ";
457 for (
unsigned int i = 0; i < mesh->hexas.size(); i++) {
459 for (
int j = 0; j < 8; j++)
460 f << mesh->hexas[i][j] <<
" ";
464 f <<
"CELL_TYPES " << numberOfCells << std::endl;
465 for (
unsigned i = 0; i < mesh->vertices.size(); ++i)
468 for (
unsigned i = 0; i < mesh->lines.size(); ++i)
471 for (
unsigned i = 0; i < mesh->triangles.size(); ++i)
474 for (
unsigned i = 0; i < mesh->tetras.size(); ++i)
475 f << 10 << std::endl;
477 for (
unsigned i = 0; i < mesh->hexas.size(); ++i)
478 f << 12 << std::endl;
481 if (mesh->pointData.getScalarDataSize() ||
482 mesh->pointData.getVectorDataSize()) {
483 VIENNACORE_LOG_WARNING(
484 "Point data output not supported for legacy VTK output. "
485 "Point data is ignored.");
489 if (mesh->cellData.getScalarDataSize()) {
490 f <<
"CELL_DATA " << mesh->cellData.getScalarData(0)->size() << std::endl;
491 for (
unsigned i = 0; i < mesh->cellData.getScalarDataSize(); ++i) {
492 auto scalars = *(mesh->cellData.getScalarData(i));
493 f <<
"SCALARS " << mesh->cellData.getScalarDataLabel(i) <<
" float"
495 f <<
"LOOKUP_TABLE default" << std::endl;
496 for (
unsigned j = 0; j < scalars.size(); ++j) {
497 f << ((std::abs(scalars[j]) < 1e-6) ? 0.0 : scalars[j]) << std::endl;
503 if (mesh->cellData.getVectorDataSize()) {
504 if (!mesh->cellData.getScalarDataSize())
505 f <<
"CELL_DATA " << mesh->cellData.getVectorData(0)->size()
507 for (
unsigned i = 0; i < mesh->cellData.getVectorDataSize(); ++i) {
508 auto vectors = *(mesh->cellData.getVectorData(i));
509 f <<
"VECTORS " << mesh->cellData.getVectorDataLabel(i) <<
" float"
511 for (
unsigned j = 0; j < vectors.size(); ++j) {
512 for (
unsigned k = 0; k < 3; ++k) {
513 f << vectors[j][k] <<
" ";