From 8657a9b3c88c5223ad02ebc05799f3a648dcf457 Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Tue, 19 May 2020 13:29:04 -0600 Subject: [PATCH] Update VTKDataSetWriter to compile into vtkm_io --- examples/clipping/CMakeLists.txt | 2 +- examples/hello_worklet/CMakeLists.txt | 2 +- examples/tetrahedra/CMakeLists.txt | 4 +- vtkm/io/CMakeLists.txt | 3 +- vtkm/io/VTKDataSetReaderBase.h | 3 - vtkm/io/VTKDataSetWriter.cxx | 407 ++++++++++++++++++++++++++ vtkm/io/VTKDataSetWriter.h | 388 +----------------------- vtkm/io/internal/VTKDataSetTypes.h | 4 + 8 files changed, 422 insertions(+), 391 deletions(-) create mode 100644 vtkm/io/VTKDataSetWriter.cxx diff --git a/examples/clipping/CMakeLists.txt b/examples/clipping/CMakeLists.txt index 6f26353ce..46bc5bf18 100644 --- a/examples/clipping/CMakeLists.txt +++ b/examples/clipping/CMakeLists.txt @@ -14,7 +14,7 @@ project(Clipping CXX) find_package(VTKm REQUIRED QUIET) add_executable(Clipping Clipping.cxx) -target_link_libraries(Clipping PRIVATE vtkm_filter) +target_link_libraries(Clipping PRIVATE vtkm_filter vtkm_io) vtkm_add_target_information(Clipping DROP_UNUSED_SYMBOLS MODIFY_CUDA_FLAGS diff --git a/examples/hello_worklet/CMakeLists.txt b/examples/hello_worklet/CMakeLists.txt index 806f69b57..eff9dfcae 100644 --- a/examples/hello_worklet/CMakeLists.txt +++ b/examples/hello_worklet/CMakeLists.txt @@ -14,7 +14,7 @@ project(HelloWorklet CXX) find_package(VTKm REQUIRED QUIET) add_executable(HelloWorklet HelloWorklet.cxx) -target_link_libraries(HelloWorklet PRIVATE vtkm_filter) +target_link_libraries(HelloWorklet PRIVATE vtkm_filter vtkm_io) vtkm_add_target_information(HelloWorklet DROP_UNUSED_SYMBOLS MODIFY_CUDA_FLAGS diff --git a/examples/tetrahedra/CMakeLists.txt b/examples/tetrahedra/CMakeLists.txt index c894daee5..7bd8a4e67 100644 --- a/examples/tetrahedra/CMakeLists.txt +++ b/examples/tetrahedra/CMakeLists.txt @@ -14,10 +14,10 @@ project(Tetrahedra CXX) find_package(VTKm REQUIRED QUIET) add_executable(Tetrahedralize Tetrahedralize.cxx) -target_link_libraries(Tetrahedralize PRIVATE vtkm_filter) +target_link_libraries(Tetrahedralize PRIVATE vtkm_filter vtkm_io) add_executable(Triangulate Triangulate.cxx) -target_link_libraries(Triangulate PRIVATE vtkm_filter) +target_link_libraries(Triangulate PRIVATE vtkm_filter vtkm_io) vtkm_add_target_information(Tetrahedralize Triangulate DROP_UNUSED_SYMBOLS diff --git a/vtkm/io/CMakeLists.txt b/vtkm/io/CMakeLists.txt index a8d63aea4..7b3e48d84 100644 --- a/vtkm/io/CMakeLists.txt +++ b/vtkm/io/CMakeLists.txt @@ -18,12 +18,12 @@ set(headers PixelTypes.h VTKDataSetReader.h VTKDataSetReaderBase.h + VTKDataSetWriter.h VTKPolyDataReader.h VTKRectilinearGridReader.h VTKStructuredGridReader.h VTKStructuredPointsReader.h VTKUnstructuredGridReader.h - VTKDataSetWriter.h ) set(template_sources @@ -37,6 +37,7 @@ set(sources EncodePNG.cxx VTKDataSetReader.cxx VTKDataSetReaderBase.cxx + VTKDataSetWriter.cxx VTKPolyDataReader.cxx VTKRectilinearGridReader.cxx VTKStructuredGridReader.cxx diff --git a/vtkm/io/VTKDataSetReaderBase.h b/vtkm/io/VTKDataSetReaderBase.h index 62a935315..245d46eb0 100644 --- a/vtkm/io/VTKDataSetReaderBase.h +++ b/vtkm/io/VTKDataSetReaderBase.h @@ -275,7 +275,4 @@ protected: } } // vtkm::io -VTKM_BASIC_TYPE_VECTOR(vtkm::io::internal::ColorChannel8) -VTKM_BASIC_TYPE_VECTOR(vtkm::io::internal::DummyBitType) - #endif // vtk_m_io_VTKDataSetReaderBase_h diff --git a/vtkm/io/VTKDataSetWriter.cxx b/vtkm/io/VTKDataSetWriter.cxx new file mode 100644 index 000000000..6f3eac1a3 --- /dev/null +++ b/vtkm/io/VTKDataSetWriter.cxx @@ -0,0 +1,407 @@ +//============================================================================ +// Copyright (c) Kitware, Inc. +// All rights reserved. +// See LICENSE.txt 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 + +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace +{ + +struct OutputPointsFunctor +{ +private: + std::ostream& out; + + template + VTKM_CONT void Output(const PortalType& portal) const + { + for (vtkm::Id index = 0; index < portal.GetNumberOfValues(); index++) + { + const int VTKDims = 3; // VTK files always require 3 dims for points + + using ValueType = typename PortalType::ValueType; + using VecType = typename vtkm::VecTraits; + + const ValueType& value = portal.Get(index); + + vtkm::IdComponent numComponents = VecType::GetNumberOfComponents(value); + for (vtkm::IdComponent c = 0; c < numComponents && c < VTKDims; c++) + { + out << (c == 0 ? "" : " ") << VecType::GetComponent(value, c); + } + for (vtkm::IdComponent c = numComponents; c < VTKDims; c++) + { + out << " 0"; + } + out << '\n'; + } + } + +public: + VTKM_CONT + OutputPointsFunctor(std::ostream& o) + : out(o) + { + } + + template + VTKM_CONT void operator()(const vtkm::cont::ArrayHandle& array) const + { + this->Output(array.ReadPortal()); + } +}; + +struct OutputFieldFunctor +{ +private: + std::ostream& out; + + template + VTKM_CONT void Output(const PortalType& portal) const + { + for (vtkm::Id index = 0; index < portal.GetNumberOfValues(); index++) + { + using ValueType = typename PortalType::ValueType; + using VecType = typename vtkm::VecTraits; + + const ValueType& value = portal.Get(index); + + vtkm::IdComponent numComponents = VecType::GetNumberOfComponents(value); + for (vtkm::IdComponent c = 0; c < numComponents; c++) + { + out << (c == 0 ? "" : " ") << VecType::GetComponent(value, c); + } + out << '\n'; + } + } + +public: + VTKM_CONT + OutputFieldFunctor(std::ostream& o) + : out(o) + { + } + + template + VTKM_CONT void operator()(const vtkm::cont::ArrayHandle& array) const + { + this->Output(array.ReadPortal()); + } +}; + +class GetDataTypeName +{ +public: + GetDataTypeName(std::string& name) + : Name(&name) + { + } + + template + void operator()(const ArrayHandleType&) const + { + using DataType = typename vtkm::VecTraits::ComponentType; + *this->Name = vtkm::io::internal::DataTypeName::Name(); + } + +private: + std::string* Name; +}; + +void WritePoints(std::ostream& out, const vtkm::cont::DataSet& dataSet) +{ + ///\todo: support other coordinate systems + int cindex = 0; + auto cdata = dataSet.GetCoordinateSystem(cindex).GetData(); + + vtkm::Id npoints = cdata.GetNumberOfValues(); + out << "POINTS " << npoints << " " << vtkm::io::internal::DataTypeName::Name() + << " " << '\n'; + + OutputPointsFunctor{ out }(cdata); +} + +template +void WriteExplicitCells(std::ostream& out, const CellSetType& cellSet) +{ + vtkm::Id nCells = cellSet.GetNumberOfCells(); + + vtkm::Id conn_length = 0; + for (vtkm::Id i = 0; i < nCells; ++i) + { + conn_length += 1 + cellSet.GetNumberOfPointsInCell(i); + } + + out << "CELLS " << nCells << " " << conn_length << '\n'; + + for (vtkm::Id i = 0; i < nCells; ++i) + { + vtkm::cont::ArrayHandle ids; + vtkm::Id nids = cellSet.GetNumberOfPointsInCell(i); + cellSet.GetIndices(i, ids); + out << nids; + auto IdPortal = ids.ReadPortal(); + for (int j = 0; j < nids; ++j) + out << " " << IdPortal.Get(j); + out << '\n'; + } + + out << "CELL_TYPES " << nCells << '\n'; + for (vtkm::Id i = 0; i < nCells; ++i) + { + vtkm::Id shape = cellSet.GetCellShape(i); + out << shape << '\n'; + } +} + +void WriteVertexCells(std::ostream& out, const vtkm::cont::DataSet& dataSet) +{ + vtkm::Id nCells = dataSet.GetCoordinateSystem(0).GetNumberOfPoints(); + + out << "CELLS " << nCells << " " << nCells * 2 << '\n'; + for (int i = 0; i < nCells; i++) + { + out << "1 " << i << '\n'; + } + out << "CELL_TYPES " << nCells << '\n'; + for (int i = 0; i < nCells; i++) + { + out << vtkm::CELL_SHAPE_VERTEX << '\n'; + } +} + +void WritePointFields(std::ostream& out, const vtkm::cont::DataSet& dataSet) +{ + bool wrote_header = false; + for (vtkm::Id f = 0; f < dataSet.GetNumberOfFields(); f++) + { + const vtkm::cont::Field field = dataSet.GetField(f); + + if (field.GetAssociation() != vtkm::cont::Field::Association::POINTS) + { + continue; + } + + vtkm::Id npoints = field.GetNumberOfValues(); + int ncomps = field.GetData().GetNumberOfComponents(); + if (ncomps > 4) + { + continue; + } + + if (!wrote_header) + { + out << "POINT_DATA " << npoints << '\n'; + wrote_header = true; + } + + std::string typeName; + vtkm::cont::CastAndCall(field.GetData().ResetTypes(vtkm::TypeListAll{}), + GetDataTypeName(typeName)); + std::string name = field.GetName(); + for (auto& c : name) + { + if (std::isspace(c)) + { + c = '_'; + } + } + out << "SCALARS " << name << " " << typeName << " " << ncomps << '\n'; + out << "LOOKUP_TABLE default" << '\n'; + + vtkm::cont::CastAndCall(field.GetData().ResetTypes(vtkm::TypeListAll{}), + OutputFieldFunctor(out)); + } +} + +void WriteCellFields(std::ostream& out, const vtkm::cont::DataSet& dataSet) +{ + bool wrote_header = false; + for (vtkm::Id f = 0; f < dataSet.GetNumberOfFields(); f++) + { + const vtkm::cont::Field field = dataSet.GetField(f); + if (!field.IsFieldCell()) + { + continue; + } + + + vtkm::Id ncells = field.GetNumberOfValues(); + int ncomps = field.GetData().GetNumberOfComponents(); + if (ncomps > 4) + continue; + + if (!wrote_header) + { + out << "CELL_DATA " << ncells << '\n'; + wrote_header = true; + } + + std::string typeName; + vtkm::cont::CastAndCall(field.GetData().ResetTypes(vtkm::TypeListAll{}), + GetDataTypeName(typeName)); + + std::string name = field.GetName(); + for (auto& c : name) + { + if (std::isspace(c)) + { + c = '_'; + } + } + + out << "SCALARS " << name << " " << typeName << " " << ncomps << '\n'; + out << "LOOKUP_TABLE default" << '\n'; + + vtkm::cont::CastAndCall(field.GetData().ResetTypes(vtkm::TypeListAll{}), + OutputFieldFunctor(out)); + } +} + +void WriteDataSetAsPoints(std::ostream& out, const vtkm::cont::DataSet& dataSet) +{ + out << "DATASET UNSTRUCTURED_GRID" << '\n'; + WritePoints(out, dataSet); + WriteVertexCells(out, dataSet); +} + +template +void WriteDataSetAsUnstructured(std::ostream& out, + const vtkm::cont::DataSet& dataSet, + const CellSetType& cellSet) +{ + out << "DATASET UNSTRUCTURED_GRID" << '\n'; + WritePoints(out, dataSet); + WriteExplicitCells(out, cellSet); +} + +template +void WriteDataSetAsStructured(std::ostream& out, + const vtkm::cont::DataSet& dataSet, + const vtkm::cont::CellSetStructured& cellSet) +{ + ///\todo: support uniform/rectilinear + out << "DATASET STRUCTURED_GRID" << '\n'; + + auto pointDimensions = cellSet.GetPointDimensions(); + using VTraits = vtkm::VecTraits; + + out << "DIMENSIONS "; + out << VTraits::GetComponent(pointDimensions, 0) << " "; + out << (DIM > 1 ? VTraits::GetComponent(pointDimensions, 1) : 1) << " "; + out << (DIM > 2 ? VTraits::GetComponent(pointDimensions, 2) : 1) << " "; + + WritePoints(out, dataSet); +} + +void Write(std::ostream& out, const vtkm::cont::DataSet& dataSet, bool just_points = false) +{ + // The Paraview parser cannot handle scientific notation: + out << std::fixed; + out << "# vtk DataFile Version 3.0" << '\n'; + out << "vtk output" << '\n'; + out << "ASCII" << '\n'; + + if (just_points) + { + WriteDataSetAsPoints(out, dataSet); + WritePointFields(out, dataSet); + } + else + { + vtkm::cont::DynamicCellSet cellSet = dataSet.GetCellSet(); + if (cellSet.IsType>()) + { + WriteDataSetAsUnstructured(out, dataSet, cellSet.Cast>()); + } + else if (cellSet.IsType>()) + { + WriteDataSetAsStructured(out, dataSet, cellSet.Cast>()); + } + else if (cellSet.IsType>()) + { + WriteDataSetAsStructured(out, dataSet, cellSet.Cast>()); + } + else if (cellSet.IsType>()) + { + WriteDataSetAsStructured(out, dataSet, cellSet.Cast>()); + } + else if (cellSet.IsType>()) + { + // these function just like explicit cell sets + WriteDataSetAsUnstructured(out, dataSet, cellSet.Cast>()); + } + else + { + throw vtkm::cont::ErrorBadType("Could not determine type to write out."); + } + + WritePointFields(out, dataSet); + WriteCellFields(out, dataSet); + } +} + +} // anonymous namespace + +namespace vtkm +{ +namespace io +{ + +VTKDataSetWriter::VTKDataSetWriter(const char* fileName) + : FileName(fileName) +{ +} + +VTKDataSetWriter::VTKDataSetWriter(const std::string& fileName) + : FileName(fileName) +{ +} + +void VTKDataSetWriter::WriteDataSet(const vtkm::cont::DataSet& dataSet, bool just_points) const +{ + if (dataSet.GetNumberOfCoordinateSystems() < 1) + { + throw vtkm::cont::ErrorBadValue( + "DataSet has no coordinate system, which is not supported by VTK file format."); + } + try + { + std::ofstream fileStream(this->FileName.c_str(), std::fstream::trunc); + Write(fileStream, dataSet, just_points); + fileStream.close(); + } + catch (std::ofstream::failure& error) + { + throw vtkm::io::ErrorIO(error.what()); + } +} +} +} // namespace vtkm::io diff --git a/vtkm/io/VTKDataSetWriter.h b/vtkm/io/VTKDataSetWriter.h index 946ac5c3d..e666f0b7d 100644 --- a/vtkm/io/VTKDataSetWriter.h +++ b/vtkm/io/VTKDataSetWriter.h @@ -10,400 +10,22 @@ #ifndef vtk_m_io_DataSetWriter_h #define vtk_m_io_DataSetWriter_h -#include -#include - -#include -#include -#include #include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include +#include namespace vtkm { namespace io { -namespace detail -{ -struct OutputPointsFunctor -{ -private: - std::ostream& out; - - template - VTKM_CONT void Output(const PortalType& portal) const - { - for (vtkm::Id index = 0; index < portal.GetNumberOfValues(); index++) - { - const int VTKDims = 3; // VTK files always require 3 dims for points - - using ValueType = typename PortalType::ValueType; - using VecType = typename vtkm::VecTraits; - - const ValueType& value = portal.Get(index); - - vtkm::IdComponent numComponents = VecType::GetNumberOfComponents(value); - for (vtkm::IdComponent c = 0; c < numComponents && c < VTKDims; c++) - { - out << (c == 0 ? "" : " ") << VecType::GetComponent(value, c); - } - for (vtkm::IdComponent c = numComponents; c < VTKDims; c++) - { - out << " 0"; - } - out << '\n'; - } - } - -public: - VTKM_CONT - OutputPointsFunctor(std::ostream& o) - : out(o) - { - } - - template - VTKM_CONT void operator()(const vtkm::cont::ArrayHandle& array) const - { - this->Output(array.ReadPortal()); - } -}; - -struct OutputFieldFunctor -{ -private: - std::ostream& out; - - template - VTKM_CONT void Output(const PortalType& portal) const - { - for (vtkm::Id index = 0; index < portal.GetNumberOfValues(); index++) - { - using ValueType = typename PortalType::ValueType; - using VecType = typename vtkm::VecTraits; - - const ValueType& value = portal.Get(index); - - vtkm::IdComponent numComponents = VecType::GetNumberOfComponents(value); - for (vtkm::IdComponent c = 0; c < numComponents; c++) - { - out << (c == 0 ? "" : " ") << VecType::GetComponent(value, c); - } - out << '\n'; - } - } - -public: - VTKM_CONT - OutputFieldFunctor(std::ostream& o) - : out(o) - { - } - - template - VTKM_CONT void operator()(const vtkm::cont::ArrayHandle& array) const - { - this->Output(array.ReadPortal()); - } -}; - -class GetDataTypeName +struct VTKM_IO_EXPORT VTKDataSetWriter { public: - GetDataTypeName(std::string& name) - : Name(&name) - { - } + VTKM_CONT VTKDataSetWriter(const char* fileName); + VTKM_CONT VTKDataSetWriter(const std::string& fileName); - template - void operator()(const ArrayHandleType&) const - { - using DataType = typename vtkm::VecTraits::ComponentType; - *this->Name = vtkm::io::internal::DataTypeName::Name(); - } - -private: - std::string* Name; -}; - -} // namespace detail - -struct VTKDataSetWriter -{ -private: - static void WritePoints(std::ostream& out, const vtkm::cont::DataSet& dataSet) - { - ///\todo: support other coordinate systems - int cindex = 0; - auto cdata = dataSet.GetCoordinateSystem(cindex).GetData(); - - vtkm::Id npoints = cdata.GetNumberOfValues(); - out << "POINTS " << npoints << " " - << vtkm::io::internal::DataTypeName::Name() << " " << '\n'; - - detail::OutputPointsFunctor{ out }(cdata); - } - - template - static void WriteExplicitCells(std::ostream& out, const CellSetType& cellSet) - { - vtkm::Id nCells = cellSet.GetNumberOfCells(); - - vtkm::Id conn_length = 0; - for (vtkm::Id i = 0; i < nCells; ++i) - { - conn_length += 1 + cellSet.GetNumberOfPointsInCell(i); - } - - out << "CELLS " << nCells << " " << conn_length << '\n'; - - for (vtkm::Id i = 0; i < nCells; ++i) - { - vtkm::cont::ArrayHandle ids; - vtkm::Id nids = cellSet.GetNumberOfPointsInCell(i); - cellSet.GetIndices(i, ids); - out << nids; - auto IdPortal = ids.ReadPortal(); - for (int j = 0; j < nids; ++j) - out << " " << IdPortal.Get(j); - out << '\n'; - } - - out << "CELL_TYPES " << nCells << '\n'; - for (vtkm::Id i = 0; i < nCells; ++i) - { - vtkm::Id shape = cellSet.GetCellShape(i); - out << shape << '\n'; - } - } - - static void WriteVertexCells(std::ostream& out, const vtkm::cont::DataSet& dataSet) - { - vtkm::Id nCells = dataSet.GetCoordinateSystem(0).GetNumberOfPoints(); - - out << "CELLS " << nCells << " " << nCells * 2 << '\n'; - for (int i = 0; i < nCells; i++) - { - out << "1 " << i << '\n'; - } - out << "CELL_TYPES " << nCells << '\n'; - for (int i = 0; i < nCells; i++) - { - out << vtkm::CELL_SHAPE_VERTEX << '\n'; - } - } - - static void WritePointFields(std::ostream& out, const vtkm::cont::DataSet& dataSet) - { - bool wrote_header = false; - for (vtkm::Id f = 0; f < dataSet.GetNumberOfFields(); f++) - { - const vtkm::cont::Field field = dataSet.GetField(f); - - if (field.GetAssociation() != vtkm::cont::Field::Association::POINTS) - { - continue; - } - - vtkm::Id npoints = field.GetNumberOfValues(); - int ncomps = field.GetData().GetNumberOfComponents(); - if (ncomps > 4) - { - continue; - } - - if (!wrote_header) - { - out << "POINT_DATA " << npoints << '\n'; - wrote_header = true; - } - - std::string typeName; - vtkm::cont::CastAndCall(field.GetData().ResetTypes(TypeListAll{}), - detail::GetDataTypeName(typeName)); - std::string name = field.GetName(); - for (auto& c : name) - { - if (std::isspace(c)) - { - c = '_'; - } - } - out << "SCALARS " << name << " " << typeName << " " << ncomps << '\n'; - out << "LOOKUP_TABLE default" << '\n'; - - vtkm::cont::CastAndCall(field.GetData().ResetTypes(TypeListAll{}), - detail::OutputFieldFunctor(out)); - } - } - - static void WriteCellFields(std::ostream& out, const vtkm::cont::DataSet& dataSet) - { - bool wrote_header = false; - for (vtkm::Id f = 0; f < dataSet.GetNumberOfFields(); f++) - { - const vtkm::cont::Field field = dataSet.GetField(f); - if (!field.IsFieldCell()) - { - continue; - } - - - vtkm::Id ncells = field.GetNumberOfValues(); - int ncomps = field.GetData().GetNumberOfComponents(); - if (ncomps > 4) - continue; - - if (!wrote_header) - { - out << "CELL_DATA " << ncells << '\n'; - wrote_header = true; - } - - std::string typeName; - vtkm::cont::CastAndCall(field.GetData().ResetTypes(TypeListAll{}), - detail::GetDataTypeName(typeName)); - - std::string name = field.GetName(); - for (auto& c : name) - { - if (std::isspace(c)) - { - c = '_'; - } - } - - out << "SCALARS " << name << " " << typeName << " " << ncomps << '\n'; - out << "LOOKUP_TABLE default" << '\n'; - - vtkm::cont::CastAndCall(field.GetData().ResetTypes(TypeListAll{}), - detail::OutputFieldFunctor(out)); - } - } - - static void WriteDataSetAsPoints(std::ostream& out, const vtkm::cont::DataSet& dataSet) - { - out << "DATASET UNSTRUCTURED_GRID" << '\n'; - WritePoints(out, dataSet); - WriteVertexCells(out, dataSet); - } - - template - static void WriteDataSetAsUnstructured(std::ostream& out, - const vtkm::cont::DataSet& dataSet, - const CellSetType& cellSet) - { - out << "DATASET UNSTRUCTURED_GRID" << '\n'; - WritePoints(out, dataSet); - WriteExplicitCells(out, cellSet); - } - - template - static void WriteDataSetAsStructured(std::ostream& out, - const vtkm::cont::DataSet& dataSet, - const vtkm::cont::CellSetStructured& cellSet) - { - ///\todo: support uniform/rectilinear - out << "DATASET STRUCTURED_GRID" << '\n'; - - auto pointDimensions = cellSet.GetPointDimensions(); - using VTraits = vtkm::VecTraits; - - out << "DIMENSIONS "; - out << VTraits::GetComponent(pointDimensions, 0) << " "; - out << (DIM > 1 ? VTraits::GetComponent(pointDimensions, 1) : 1) << " "; - out << (DIM > 2 ? VTraits::GetComponent(pointDimensions, 2) : 1) << " "; - - WritePoints(out, dataSet); - } - - static void Write(std::ostream& out, const vtkm::cont::DataSet& dataSet, bool just_points = false) - { - // The Paraview parser cannot handle scientific notation: - out << std::fixed; - out << "# vtk DataFile Version 3.0" << '\n'; - out << "vtk output" << '\n'; - out << "ASCII" << '\n'; - - if (just_points) - { - WriteDataSetAsPoints(out, dataSet); - WritePointFields(out, dataSet); - } - else - { - vtkm::cont::DynamicCellSet cellSet = dataSet.GetCellSet(); - if (cellSet.IsType>()) - { - WriteDataSetAsUnstructured(out, dataSet, cellSet.Cast>()); - } - else if (cellSet.IsType>()) - { - WriteDataSetAsStructured(out, dataSet, cellSet.Cast>()); - } - else if (cellSet.IsType>()) - { - WriteDataSetAsStructured(out, dataSet, cellSet.Cast>()); - } - else if (cellSet.IsType>()) - { - WriteDataSetAsStructured(out, dataSet, cellSet.Cast>()); - } - else if (cellSet.IsType>()) - { - // these function just like explicit cell sets - WriteDataSetAsUnstructured(out, dataSet, cellSet.Cast>()); - } - else - { - throw vtkm::cont::ErrorBadType("Could not determine type to write out."); - } - - WritePointFields(out, dataSet); - WriteCellFields(out, dataSet); - } - } - -public: - VTKM_CONT - explicit VTKDataSetWriter(const std::string& filename) - : FileName(filename) - { - } - - VTKM_CONT - void WriteDataSet(const vtkm::cont::DataSet& dataSet, bool just_points = false) const - { - if (dataSet.GetNumberOfCoordinateSystems() < 1) - { - throw vtkm::cont::ErrorBadValue( - "DataSet has no coordinate system, which is not supported by VTK file format."); - } - try - { - std::ofstream fileStream(this->FileName.c_str(), std::fstream::trunc); - this->Write(fileStream, dataSet, just_points); - fileStream.close(); - } - catch (std::ofstream::failure& error) - { - throw vtkm::io::ErrorIO(error.what()); - } - } + VTKM_CONT void WriteDataSet(const vtkm::cont::DataSet& dataSet, bool just_points = false) const; private: std::string FileName; diff --git a/vtkm/io/internal/VTKDataSetTypes.h b/vtkm/io/internal/VTKDataSetTypes.h index 8210ff197..30e9ece20 100644 --- a/vtkm/io/internal/VTKDataSetTypes.h +++ b/vtkm/io/internal/VTKDataSetTypes.h @@ -11,6 +11,7 @@ #define vtk_m_io_internal_VTKDataSetTypes_h #include +#include #include #include @@ -238,4 +239,7 @@ inline void SelectTypeAndCall(DataType dtype, } } // namespace vtkm::io::internal +VTKM_BASIC_TYPE_VECTOR(vtkm::io::internal::ColorChannel8) +VTKM_BASIC_TYPE_VECTOR(vtkm::io::internal::DummyBitType) + #endif // vtk_m_io_internal_VTKDataSetTypes_h