diff --git a/FileIO/VtkIO/VtuInterface.cpp b/FileIO/VtkIO/VtuInterface.cpp index 7d5b0c51558bd49e1802daa7cfb65201b9822630..964e19867e4cb8d2cd989098980f74568fe782fa 100644 --- a/FileIO/VtkIO/VtuInterface.cpp +++ b/FileIO/VtkIO/VtuInterface.cpp @@ -15,9 +15,12 @@ #include "logog/include/logog.hpp" +#include <vtkNew.h> #include <vtkXMLUnstructuredGridReader.h> +#include <vtkXMLUnstructuredGridWriter.h> #include <vtkSmartPointer.h> +#include "InSituLib/VtkMappedMeshSource.h" #include "Mesh.h" #include "MeshGenerators/VtkMeshConverter.h" @@ -32,7 +35,7 @@ VtuInterface::VtuInterface() : VtuInterface::~VtuInterface() {} -MeshLib::Mesh* VtuInterface::readVTUFile(const std::string &file_name) +MeshLib::Mesh* VtuInterface::readVTUFile(std::string const &file_name) { vtkSmartPointer<vtkXMLUnstructuredGridReader> reader = vtkXMLUnstructuredGridReader::New(); @@ -55,16 +58,28 @@ void VtuInterface::setMesh(const MeshLib::Mesh* mesh) this->_mesh = const_cast<MeshLib::Mesh*>(mesh); }; -bool VtuInterface::write() +int VtuInterface::writeToFile(std::string const &file_name) { if(!_mesh) { ERR("VtuInterface::write(): No mesh specified."); - return false; + return 0; } - // TODO write with MappedMesh - return false; + vtkNew<InSituLib::VtkMappedMeshSource> vtkSource; + vtkSource->SetMesh(_mesh); + + vtkSmartPointer<vtkXMLUnstructuredGridWriter> vtuWriter = + vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New(); + vtuWriter->SetInputConnection(vtkSource->GetOutputPort()); + if(_use_compressor) + vtuWriter->SetCompressorTypeToZLib(); + + // Setting binary file mode, otherwise corrupted output due to VTK bug + // See http://www.paraview.org/Bug/view.php?id=13382 + vtuWriter->SetDataModeToBinary(); + vtuWriter->SetFileName(file_name.c_str()); + return vtuWriter->Write(); } } diff --git a/FileIO/VtkIO/VtuInterface.h b/FileIO/VtkIO/VtuInterface.h index d5daebeef75a727af5debc7b34245e5bfdda9092..c93d4fea454379fcc369daf68cacc97c063d0fff 100644 --- a/FileIO/VtkIO/VtuInterface.h +++ b/FileIO/VtkIO/VtuInterface.h @@ -28,15 +28,17 @@ namespace FileIO /** * \brief Reads and writes VtkXMLUnstructuredGrid-files (vtu) to and from OGS data structures. + * This class is currently not inherited from Writer because VTK will implement + * writing to a string from 6.2 onwards. */ -class VtuInterface : public Writer +class VtuInterface { public: VtuInterface(); ~VtuInterface(); /// Read an unstructured grid from a VTU file - static MeshLib::Mesh* readVTUFile(const std::string &file_name); + static MeshLib::Mesh* readVTUFile(std::string const &file_name); /// Decide if the mesh data should be written compressed (default is false). void setCompressData(bool flag=true) { _use_compressor = flag; } @@ -44,8 +46,9 @@ public: /// Sets the mesh to write. void setMesh(const MeshLib::Mesh* mesh); + int writeToFile(std::string const &filen_name); + private: - bool write(); MeshLib::Mesh* _mesh; bool _use_compressor; diff --git a/FileIO/Writer.h b/FileIO/Writer.h index a46f2d456f78d99bee3defb8d7f2bc87611e02d6..98b1dd0b1fe426e8707fee772b40a5e3273e64a0 100644 --- a/FileIO/Writer.h +++ b/FileIO/Writer.h @@ -26,7 +26,8 @@ namespace FileIO /// or file. Also formatting (precision, scientific notation of decimal values) /// can be set. /// -/// When subclassing you only need to implement void write(std::ostream& stream). +/// When subclassing you only need to implement void write() in which you have +/// to write to _out. class Writer { public: diff --git a/InSituLib/VtkMappedMesh.cpp b/InSituLib/VtkMappedMesh.cpp index a533a0e2dee08b98527da09ac0398bebac1c1f7d..ef3299bee86fe6c296976fbba8b4ec7df18dea49 100644 --- a/InSituLib/VtkMappedMesh.cpp +++ b/InSituLib/VtkMappedMesh.cpp @@ -37,7 +37,6 @@ vtkStandardNewMacro(VtkMappedMeshImpl) void VtkMappedMeshImpl::PrintSelf(ostream &os, vtkIndent indent) { this->Superclass::PrintSelf(os, indent); - // TODO os << indent << "NumberOfCells: " << this->NumberOfCells << endl; } diff --git a/Tests/InSituLib/TestVtkMappedMeshSource.cpp b/Tests/InSituLib/TestVtkMappedMeshSource.cpp index 058c7e98054ee901fe96caf4954b68db801852f7..a5561687fcce8c73d3ba97c1f52070e40c3e417e 100644 --- a/Tests/InSituLib/TestVtkMappedMeshSource.cpp +++ b/Tests/InSituLib/TestVtkMappedMeshSource.cpp @@ -13,14 +13,17 @@ */ #include "BaseLib/BuildInfo.h" + +#include "FileIO/VtkIO/VtuInterface.h" + +#include "InSituLib/VtkMappedMesh.h" +#include "InSituLib/VtkMappedMeshSource.h" + #include "MeshLib/Elements/Element.h" #include "MeshLib/Mesh.h" #include "MeshLib/MeshGenerators/MeshGenerator.h" #include "MeshLib/MeshGenerators/VtkMeshConverter.h" -#include "InSituLib/VtkMappedMesh.h" -#include "InSituLib/VtkMappedMeshSource.h" - #include "gtest/gtest.h" #include <vtkNew.h> @@ -103,14 +106,10 @@ TEST_F(InSituMesh, MappedMeshSourceRoundtrip) ASSERT_EQ((unsigned)range[1], 0); // -- Write VTK mesh to file - vtkSmartPointer<vtkXMLUnstructuredGridWriter> vtuWriter = - vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New(); - // Setting binary file mode, otherwise corrupted output due to VTK bug - // See http://www.paraview.org/Bug/view.php?id=13382 - vtuWriter->SetDataModeToBinary(); - vtuWriter->SetFileName(test_data_file.c_str()); - vtuWriter->SetInputConnection(vtkSource->GetOutputPort()); - vtuWriter->Write(); + FileIO::VtuInterface vtuInterface; + vtuInterface.setCompressData(true); + vtuInterface.setMesh(mesh); + vtuInterface.writeToFile(test_data_file); // -- Read back VTK mesh vtkSmartPointer<vtkXMLUnstructuredGridReader> reader = @@ -122,6 +121,7 @@ TEST_F(InSituMesh, MappedMeshSourceRoundtrip) // Both VTK meshes should be identical ASSERT_EQ(vtkMesh->GetNumberOfPoints(), output->GetNumberOfPoints()); ASSERT_EQ(vtkMesh->GetNumberOfCells(), output->GetNumberOfCells()); + ASSERT_EQ(vtkMesh->GetCellData()->GetScalars("MaterialIDs")->GetNumberOfTuples(), matIdsArray->GetNumberOfTuples()); // Both OGS meshes should be identical MeshLib::Mesh* newMesh = MeshLib::VtkMeshConverter::convertUnstructuredGrid(vtkMesh);