diff --git a/docs/changelog/vtk-io-in-library.md b/docs/changelog/vtk-io-in-library.md new file mode 100644 index 000000000..d6616813e --- /dev/null +++ b/docs/changelog/vtk-io-in-library.md @@ -0,0 +1,12 @@ +# Move VTK file readers and writers into vtkm_io + +The legacy VTK file reader and writer were created back when VTK-m was a +header-only library. Things have changed and we now compile quite a bit of +code into libraries. At this point, there is no reason why the VTK file +reader/writer should be any different. + +Thus, `VTKDataSetReader`, `VTKDataSetWriter`, and several supporting +classes are now compiled into the `vtk_io` library. + +As a side effect, code using VTK-m will need to link to `vtk_io` if they +are using any readers or writers. diff --git a/vtkm/io/CMakeLists.txt b/vtkm/io/CMakeLists.txt index 32d9d9ae5..a8d63aea4 100644 --- a/vtkm/io/CMakeLists.txt +++ b/vtkm/io/CMakeLists.txt @@ -35,7 +35,13 @@ set(template_sources set(sources DecodePNG.cxx EncodePNG.cxx + VTKDataSetReader.cxx VTKDataSetReaderBase.cxx + VTKPolyDataReader.cxx + VTKRectilinearGridReader.cxx + VTKStructuredGridReader.cxx + VTKStructuredPointsReader.cxx + VTKUnstructuredGridReader.cxx ) vtkm_declare_headers( diff --git a/vtkm/io/VTKDataSetReader.cxx b/vtkm/io/VTKDataSetReader.cxx new file mode 100644 index 000000000..a1fdbbdb9 --- /dev/null +++ b/vtkm/io/VTKDataSetReader.cxx @@ -0,0 +1,92 @@ +//============================================================================ +// 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 + +namespace vtkm +{ +namespace io +{ + +VTKDataSetReader::VTKDataSetReader(const char* fileName) + : VTKDataSetReaderBase(fileName) +{ +} + +VTKDataSetReader::VTKDataSetReader(const std::string& fileName) + : VTKDataSetReaderBase(fileName) +{ +} + +VTKDataSetReader::~VTKDataSetReader() +{ +} + +void VTKDataSetReader::PrintSummary(std::ostream& out) const +{ + if (this->Reader) + { + this->Reader->PrintSummary(out); + } + else + { + VTKDataSetReaderBase::PrintSummary(out); + } +} + +void VTKDataSetReader::CloseFile() +{ + if (this->Reader) + { + this->Reader->CloseFile(); + } + else + { + VTKDataSetReaderBase::CloseFile(); + } +} + +void VTKDataSetReader::Read() +{ + switch (this->DataFile->Structure) + { + case vtkm::io::internal::DATASET_STRUCTURED_POINTS: + this->Reader.reset(new VTKStructuredPointsReader("")); + break; + case vtkm::io::internal::DATASET_STRUCTURED_GRID: + this->Reader.reset(new VTKStructuredGridReader("")); + break; + case vtkm::io::internal::DATASET_RECTILINEAR_GRID: + this->Reader.reset(new VTKRectilinearGridReader("")); + break; + case vtkm::io::internal::DATASET_POLYDATA: + this->Reader.reset(new VTKPolyDataReader("")); + break; + case vtkm::io::internal::DATASET_UNSTRUCTURED_GRID: + this->Reader.reset(new VTKUnstructuredGridReader("")); + break; + default: + throw vtkm::io::ErrorIO("Unsupported DataSet type."); + } + + this->TransferDataFile(*this->Reader.get()); + this->Reader->Read(); + this->DataSet = this->Reader->GetDataSet(); +} +} +} // namespace vtkm::io diff --git a/vtkm/io/VTKDataSetReader.h b/vtkm/io/VTKDataSetReader.h index 166c9466d..49e4b4537 100644 --- a/vtkm/io/VTKDataSetReader.h +++ b/vtkm/io/VTKDataSetReader.h @@ -11,86 +11,27 @@ #define vtk_m_io_VTKDataSetReader_h #include -#include -#include -#include -#include -#include - -#include namespace vtkm { namespace io { -VTKM_SILENCE_WEAK_VTABLE_WARNING_START - -class VTKDataSetReader : public VTKDataSetReaderBase +class VTKM_IO_EXPORT VTKDataSetReader : public VTKDataSetReaderBase { public: - explicit VTKDataSetReader(const char* fileName) - : VTKDataSetReaderBase(fileName) - { - } + VTKM_CONT VTKDataSetReader(const char* fileName); + VTKM_CONT VTKDataSetReader(const std::string& fileName); + VTKM_CONT ~VTKDataSetReader() override; - explicit VTKDataSetReader(const std::string& fileName) - : VTKDataSetReaderBase(fileName) - { - } + VTKDataSetReader(const VTKDataSetReader&) = delete; + void operator=(const VTKDataSetReader&) = delete; - virtual void PrintSummary(std::ostream& out) const - { - if (this->Reader) - { - this->Reader->PrintSummary(out); - } - else - { - VTKDataSetReaderBase::PrintSummary(out); - } - } + VTKM_CONT void PrintSummary(std::ostream& out) const override; private: - virtual void CloseFile() - { - if (this->Reader) - { - this->Reader->CloseFile(); - } - else - { - VTKDataSetReaderBase::CloseFile(); - } - } - - virtual void Read() - { - switch (this->DataFile->Structure) - { - case vtkm::io::internal::DATASET_STRUCTURED_POINTS: - this->Reader.reset(new VTKStructuredPointsReader("")); - break; - case vtkm::io::internal::DATASET_STRUCTURED_GRID: - this->Reader.reset(new VTKStructuredGridReader("")); - break; - case vtkm::io::internal::DATASET_RECTILINEAR_GRID: - this->Reader.reset(new VTKRectilinearGridReader("")); - break; - case vtkm::io::internal::DATASET_POLYDATA: - this->Reader.reset(new VTKPolyDataReader("")); - break; - case vtkm::io::internal::DATASET_UNSTRUCTURED_GRID: - this->Reader.reset(new VTKUnstructuredGridReader("")); - break; - default: - throw vtkm::io::ErrorIO("Unsupported DataSet type."); - } - - this->TransferDataFile(*this->Reader.get()); - this->Reader->Read(); - this->DataSet = this->Reader->GetDataSet(); - } + VTKM_CONT void CloseFile() override; + VTKM_CONT void Read() override; std::unique_ptr Reader; }; diff --git a/vtkm/io/VTKPolyDataReader.cxx b/vtkm/io/VTKPolyDataReader.cxx new file mode 100644 index 000000000..e134d6160 --- /dev/null +++ b/vtkm/io/VTKPolyDataReader.cxx @@ -0,0 +1,152 @@ +//============================================================================ +// 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 + +namespace +{ + +template +inline vtkm::cont::ArrayHandle ConcatinateArrayHandles( + const std::vector>& arrays) +{ + vtkm::Id size = 0; + for (std::size_t i = 0; i < arrays.size(); ++i) + { + size += arrays[i].GetNumberOfValues(); + } + + vtkm::cont::ArrayHandle out; + out.Allocate(size); + + auto outp = vtkm::cont::ArrayPortalToIteratorBegin(out.WritePortal()); + for (std::size_t i = 0; i < arrays.size(); ++i) + { + std::copy(vtkm::cont::ArrayPortalToIteratorBegin(arrays[i].ReadPortal()), + vtkm::cont::ArrayPortalToIteratorEnd(arrays[i].ReadPortal()), + outp); + using DifferenceType = typename std::iterator_traits::difference_type; + std::advance(outp, static_cast(arrays[i].GetNumberOfValues())); + } + + return out; +} +} + +namespace vtkm +{ +namespace io +{ + +VTKPolyDataReader::VTKPolyDataReader(const char* fileName) + : VTKDataSetReaderBase(fileName) +{ +} + +VTKPolyDataReader::VTKPolyDataReader(const std::string& fileName) + : VTKDataSetReaderBase(fileName) +{ +} + +void VTKPolyDataReader::Read() +{ + if (this->DataFile->Structure != vtkm::io::internal::DATASET_POLYDATA) + { + throw vtkm::io::ErrorIO("Incorrect DataSet type"); + } + + //We need to be able to handle VisIt files which dump Field data + //at the top of a VTK file + std::string tag; + this->DataFile->Stream >> tag; + if (tag == "FIELD") + { + this->ReadGlobalFields(); + this->DataFile->Stream >> tag; + } + + // Read the points + internal::parseAssert(tag == "POINTS"); + this->ReadPoints(); + + vtkm::Id numPoints = this->DataSet.GetNumberOfPoints(); + + // Read the cellset + std::vector> connectivityArrays; + std::vector> numIndicesArrays; + std::vector shapesBuffer; + while (!this->DataFile->Stream.eof()) + { + vtkm::UInt8 shape = vtkm::CELL_SHAPE_EMPTY; + this->DataFile->Stream >> tag; + if (tag == "VERTICES") + { + shape = vtkm::io::internal::CELL_SHAPE_POLY_VERTEX; + } + else if (tag == "LINES") + { + shape = vtkm::io::internal::CELL_SHAPE_POLY_LINE; + } + else if (tag == "POLYGONS") + { + shape = vtkm::CELL_SHAPE_POLYGON; + } + else if (tag == "TRIANGLE_STRIPS") + { + shape = vtkm::io::internal::CELL_SHAPE_TRIANGLE_STRIP; + } + else + { + this->DataFile->Stream.seekg(-static_cast(tag.length()), std::ios_base::cur); + break; + } + + vtkm::cont::ArrayHandle cellConnectivity; + vtkm::cont::ArrayHandle cellNumIndices; + this->ReadCells(cellConnectivity, cellNumIndices); + + connectivityArrays.push_back(cellConnectivity); + numIndicesArrays.push_back(cellNumIndices); + shapesBuffer.insert( + shapesBuffer.end(), static_cast(cellNumIndices.GetNumberOfValues()), shape); + } + + vtkm::cont::ArrayHandle connectivity = ConcatinateArrayHandles(connectivityArrays); + vtkm::cont::ArrayHandle numIndices = ConcatinateArrayHandles(numIndicesArrays); + vtkm::cont::ArrayHandle shapes; + shapes.Allocate(static_cast(shapesBuffer.size())); + std::copy(shapesBuffer.begin(), + shapesBuffer.end(), + vtkm::cont::ArrayPortalToIteratorBegin(shapes.WritePortal())); + + vtkm::cont::ArrayHandle permutation; + vtkm::io::internal::FixupCellSet(connectivity, numIndices, shapes, permutation); + this->SetCellsPermutation(permutation); + + if (vtkm::io::internal::IsSingleShape(shapes)) + { + vtkm::cont::CellSetSingleType<> cellSet; + cellSet.Fill( + numPoints, shapes.ReadPortal().Get(0), numIndices.ReadPortal().Get(0), connectivity); + this->DataSet.SetCellSet(cellSet); + } + else + { + auto offsets = vtkm::cont::ConvertNumIndicesToOffsets(numIndices); + vtkm::cont::CellSetExplicit<> cellSet; + cellSet.Fill(numPoints, shapes, connectivity, offsets); + this->DataSet.SetCellSet(cellSet); + } + + // Read points and cell attributes + this->ReadAttributes(); +} +} +} // namespace vtkm::io diff --git a/vtkm/io/VTKPolyDataReader.h b/vtkm/io/VTKPolyDataReader.h index dcc0623f7..449c1747f 100644 --- a/vtkm/io/VTKPolyDataReader.h +++ b/vtkm/io/VTKPolyDataReader.h @@ -22,147 +22,15 @@ namespace vtkm namespace io { -namespace internal -{ - -template -inline vtkm::cont::ArrayHandle ConcatinateArrayHandles( - const std::vector>& arrays) -{ - vtkm::Id size = 0; - for (std::size_t i = 0; i < arrays.size(); ++i) - { - size += arrays[i].GetNumberOfValues(); - } - - vtkm::cont::ArrayHandle out; - out.Allocate(size); - - auto outp = vtkm::cont::ArrayPortalToIteratorBegin(out.WritePortal()); - for (std::size_t i = 0; i < arrays.size(); ++i) - { - std::copy(vtkm::cont::ArrayPortalToIteratorBegin(arrays[i].ReadPortal()), - vtkm::cont::ArrayPortalToIteratorEnd(arrays[i].ReadPortal()), - outp); - using DifferenceType = typename std::iterator_traits::difference_type; - std::advance(outp, static_cast(arrays[i].GetNumberOfValues())); - } - - return out; -} - -} // namespace internal - -VTKM_SILENCE_WEAK_VTABLE_WARNING_START - -class VTKPolyDataReader : public VTKDataSetReaderBase +class VTKM_IO_EXPORT VTKPolyDataReader : public VTKDataSetReaderBase { public: - explicit VTKPolyDataReader(const char* fileName) - : VTKDataSetReaderBase(fileName) - { - } + explicit VTKM_CONT VTKPolyDataReader(const char* fileName); + explicit VTKM_CONT VTKPolyDataReader(const std::string& fileName); private: - virtual void Read() - { - if (this->DataFile->Structure != vtkm::io::internal::DATASET_POLYDATA) - { - throw vtkm::io::ErrorIO("Incorrect DataSet type"); - } - - //We need to be able to handle VisIt files which dump Field data - //at the top of a VTK file - std::string tag; - this->DataFile->Stream >> tag; - if (tag == "FIELD") - { - this->ReadGlobalFields(); - this->DataFile->Stream >> tag; - } - - // Read the points - internal::parseAssert(tag == "POINTS"); - this->ReadPoints(); - - vtkm::Id numPoints = this->DataSet.GetNumberOfPoints(); - - // Read the cellset - std::vector> connectivityArrays; - std::vector> numIndicesArrays; - std::vector shapesBuffer; - while (!this->DataFile->Stream.eof()) - { - vtkm::UInt8 shape = vtkm::CELL_SHAPE_EMPTY; - this->DataFile->Stream >> tag; - if (tag == "VERTICES") - { - shape = vtkm::io::internal::CELL_SHAPE_POLY_VERTEX; - } - else if (tag == "LINES") - { - shape = vtkm::io::internal::CELL_SHAPE_POLY_LINE; - } - else if (tag == "POLYGONS") - { - shape = vtkm::CELL_SHAPE_POLYGON; - } - else if (tag == "TRIANGLE_STRIPS") - { - shape = vtkm::io::internal::CELL_SHAPE_TRIANGLE_STRIP; - } - else - { - this->DataFile->Stream.seekg(-static_cast(tag.length()), - std::ios_base::cur); - break; - } - - vtkm::cont::ArrayHandle cellConnectivity; - vtkm::cont::ArrayHandle cellNumIndices; - this->ReadCells(cellConnectivity, cellNumIndices); - - connectivityArrays.push_back(cellConnectivity); - numIndicesArrays.push_back(cellNumIndices); - shapesBuffer.insert( - shapesBuffer.end(), static_cast(cellNumIndices.GetNumberOfValues()), shape); - } - - vtkm::cont::ArrayHandle connectivity = - internal::ConcatinateArrayHandles(connectivityArrays); - vtkm::cont::ArrayHandle numIndices = - internal::ConcatinateArrayHandles(numIndicesArrays); - vtkm::cont::ArrayHandle shapes; - shapes.Allocate(static_cast(shapesBuffer.size())); - std::copy(shapesBuffer.begin(), - shapesBuffer.end(), - vtkm::cont::ArrayPortalToIteratorBegin(shapes.WritePortal())); - - vtkm::cont::ArrayHandle permutation; - vtkm::io::internal::FixupCellSet(connectivity, numIndices, shapes, permutation); - this->SetCellsPermutation(permutation); - - if (vtkm::io::internal::IsSingleShape(shapes)) - { - vtkm::cont::CellSetSingleType<> cellSet; - cellSet.Fill( - numPoints, shapes.ReadPortal().Get(0), numIndices.ReadPortal().Get(0), connectivity); - this->DataSet.SetCellSet(cellSet); - } - else - { - auto offsets = vtkm::cont::ConvertNumIndicesToOffsets(numIndices); - vtkm::cont::CellSetExplicit<> cellSet; - cellSet.Fill(numPoints, shapes, connectivity, offsets); - this->DataSet.SetCellSet(cellSet); - } - - // Read points and cell attributes - this->ReadAttributes(); - } + void Read() override; }; - -VTKM_SILENCE_WEAK_VTABLE_WARNING_END } } // namespace vtkm::io diff --git a/vtkm/io/VTKRectilinearGridReader.cxx b/vtkm/io/VTKRectilinearGridReader.cxx new file mode 100644 index 000000000..9dcf3a5ca --- /dev/null +++ b/vtkm/io/VTKRectilinearGridReader.cxx @@ -0,0 +1,95 @@ +//============================================================================ +// 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 + +namespace vtkm +{ +namespace io +{ + +VTKRectilinearGridReader::VTKRectilinearGridReader(const char* fileName) + : VTKDataSetReaderBase(fileName) +{ +} + +VTKRectilinearGridReader::VTKRectilinearGridReader(const std::string& fileName) + : VTKDataSetReaderBase(fileName) +{ +} + +void VTKRectilinearGridReader::Read() +{ + if (this->DataFile->Structure != vtkm::io::internal::DATASET_RECTILINEAR_GRID) + throw vtkm::io::ErrorIO("Incorrect DataSet type"); + + //We need to be able to handle VisIt files which dump Field data + //at the top of a VTK file + std::string tag; + this->DataFile->Stream >> tag; + if (tag == "FIELD") + { + this->ReadGlobalFields(); + this->DataFile->Stream >> tag; + } + + // Read structured grid specific meta-data + internal::parseAssert(tag == "DIMENSIONS"); + vtkm::Id3 dim; + this->DataFile->Stream >> dim[0] >> dim[1] >> dim[2] >> std::ws; + + //Read the points. + std::string dataType; + std::size_t numPoints[3]; + vtkm::cont::VariantArrayHandle X, Y, Z; + + // Always read coordinates as vtkm::FloatDefault + std::string readDataType = vtkm::io::internal::DataTypeName::Name(); + + this->DataFile->Stream >> tag >> numPoints[0] >> dataType >> std::ws; + if (tag != "X_COORDINATES") + throw vtkm::io::ErrorIO("X_COORDINATES tag not found"); + X = this->DoReadArrayVariant(vtkm::cont::Field::Association::ANY, readDataType, numPoints[0], 1); + + this->DataFile->Stream >> tag >> numPoints[1] >> dataType >> std::ws; + if (tag != "Y_COORDINATES") + throw vtkm::io::ErrorIO("Y_COORDINATES tag not found"); + Y = this->DoReadArrayVariant(vtkm::cont::Field::Association::ANY, readDataType, numPoints[1], 1); + + this->DataFile->Stream >> tag >> numPoints[2] >> dataType >> std::ws; + if (tag != "Z_COORDINATES") + throw vtkm::io::ErrorIO("Z_COORDINATES tag not found"); + Z = this->DoReadArrayVariant(vtkm::cont::Field::Association::ANY, readDataType, numPoints[2], 1); + + if (dim != vtkm::Id3(static_cast(numPoints[0]), + static_cast(numPoints[1]), + static_cast(numPoints[2]))) + throw vtkm::io::ErrorIO("DIMENSIONS not equal to number of points"); + + vtkm::cont::ArrayHandleCartesianProduct, + vtkm::cont::ArrayHandle, + vtkm::cont::ArrayHandle> + coords; + + vtkm::cont::ArrayHandle Xc, Yc, Zc; + X.CopyTo(Xc); + Y.CopyTo(Yc); + Z.CopyTo(Zc); + coords = vtkm::cont::make_ArrayHandleCartesianProduct(Xc, Yc, Zc); + vtkm::cont::CoordinateSystem coordSys("coordinates", coords); + this->DataSet.AddCoordinateSystem(coordSys); + + this->DataSet.SetCellSet(internal::CreateCellSetStructured(dim)); + + // Read points and cell attributes + this->ReadAttributes(); +} +} +} // namespace vtkm::io diff --git a/vtkm/io/VTKRectilinearGridReader.h b/vtkm/io/VTKRectilinearGridReader.h index 92a5c6e05..37c681c28 100644 --- a/vtkm/io/VTKRectilinearGridReader.h +++ b/vtkm/io/VTKRectilinearGridReader.h @@ -17,89 +17,15 @@ namespace vtkm namespace io { -VTKM_SILENCE_WEAK_VTABLE_WARNING_START - -class VTKRectilinearGridReader : public VTKDataSetReaderBase +class VTKM_IO_EXPORT VTKRectilinearGridReader : public VTKDataSetReaderBase { public: - explicit VTKRectilinearGridReader(const char* fileName) - : VTKDataSetReaderBase(fileName) - { - } + explicit VTKM_CONT VTKRectilinearGridReader(const char* fileName); + explicit VTKM_CONT VTKRectilinearGridReader(const std::string& fileName); private: - virtual void Read() - { - if (this->DataFile->Structure != vtkm::io::internal::DATASET_RECTILINEAR_GRID) - throw vtkm::io::ErrorIO("Incorrect DataSet type"); - - //We need to be able to handle VisIt files which dump Field data - //at the top of a VTK file - std::string tag; - this->DataFile->Stream >> tag; - if (tag == "FIELD") - { - this->ReadGlobalFields(); - this->DataFile->Stream >> tag; - } - - // Read structured grid specific meta-data - internal::parseAssert(tag == "DIMENSIONS"); - vtkm::Id3 dim; - this->DataFile->Stream >> dim[0] >> dim[1] >> dim[2] >> std::ws; - - //Read the points. - std::string dataType; - std::size_t numPoints[3]; - vtkm::cont::VariantArrayHandle X, Y, Z; - - // Always read coordinates as vtkm::FloatDefault - std::string readDataType = vtkm::io::internal::DataTypeName::Name(); - - this->DataFile->Stream >> tag >> numPoints[0] >> dataType >> std::ws; - if (tag != "X_COORDINATES") - throw vtkm::io::ErrorIO("X_COORDINATES tag not found"); - X = - this->DoReadArrayVariant(vtkm::cont::Field::Association::ANY, readDataType, numPoints[0], 1); - - this->DataFile->Stream >> tag >> numPoints[1] >> dataType >> std::ws; - if (tag != "Y_COORDINATES") - throw vtkm::io::ErrorIO("Y_COORDINATES tag not found"); - Y = - this->DoReadArrayVariant(vtkm::cont::Field::Association::ANY, readDataType, numPoints[1], 1); - - this->DataFile->Stream >> tag >> numPoints[2] >> dataType >> std::ws; - if (tag != "Z_COORDINATES") - throw vtkm::io::ErrorIO("Z_COORDINATES tag not found"); - Z = - this->DoReadArrayVariant(vtkm::cont::Field::Association::ANY, readDataType, numPoints[2], 1); - - if (dim != vtkm::Id3(static_cast(numPoints[0]), - static_cast(numPoints[1]), - static_cast(numPoints[2]))) - throw vtkm::io::ErrorIO("DIMENSIONS not equal to number of points"); - - vtkm::cont::ArrayHandleCartesianProduct, - vtkm::cont::ArrayHandle, - vtkm::cont::ArrayHandle> - coords; - - vtkm::cont::ArrayHandle Xc, Yc, Zc; - X.CopyTo(Xc); - Y.CopyTo(Yc); - Z.CopyTo(Zc); - coords = vtkm::cont::make_ArrayHandleCartesianProduct(Xc, Yc, Zc); - vtkm::cont::CoordinateSystem coordSys("coordinates", coords); - this->DataSet.AddCoordinateSystem(coordSys); - - this->DataSet.SetCellSet(internal::CreateCellSetStructured(dim)); - - // Read points and cell attributes - this->ReadAttributes(); - } + VTKM_CONT void Read() override; }; - -VTKM_SILENCE_WEAK_VTABLE_WARNING_END } } // namespace vtkm::io diff --git a/vtkm/io/VTKStructuredGridReader.cxx b/vtkm/io/VTKStructuredGridReader.cxx new file mode 100644 index 000000000..76b7d2101 --- /dev/null +++ b/vtkm/io/VTKStructuredGridReader.cxx @@ -0,0 +1,62 @@ +//============================================================================ +// 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 + +namespace vtkm +{ +namespace io +{ + +VTKStructuredGridReader::VTKStructuredGridReader(const char* fileName) + : VTKDataSetReaderBase(fileName) +{ +} + +VTKStructuredGridReader::VTKStructuredGridReader(const std::string& fileName) + : VTKDataSetReaderBase(fileName) +{ +} + +void VTKStructuredGridReader::Read() +{ + if (this->DataFile->Structure != vtkm::io::internal::DATASET_STRUCTURED_GRID) + { + throw vtkm::io::ErrorIO("Incorrect DataSet type"); + } + + std::string tag; + + //We need to be able to handle VisIt files which dump Field data + //at the top of a VTK file + this->DataFile->Stream >> tag; + if (tag == "FIELD") + { + this->ReadGlobalFields(); + this->DataFile->Stream >> tag; + } + + // Read structured grid specific meta-data + internal::parseAssert(tag == "DIMENSIONS"); + vtkm::Id3 dim; + this->DataFile->Stream >> dim[0] >> dim[1] >> dim[2] >> std::ws; + + this->DataSet.SetCellSet(internal::CreateCellSetStructured(dim)); + + // Read the points + this->DataFile->Stream >> tag; + internal::parseAssert(tag == "POINTS"); + this->ReadPoints(); + + // Read points and cell attributes + this->ReadAttributes(); +} +} +} // namespace vtkm::io diff --git a/vtkm/io/VTKStructuredGridReader.h b/vtkm/io/VTKStructuredGridReader.h index b37e98301..745cef2b7 100644 --- a/vtkm/io/VTKStructuredGridReader.h +++ b/vtkm/io/VTKStructuredGridReader.h @@ -17,53 +17,15 @@ namespace vtkm namespace io { -VTKM_SILENCE_WEAK_VTABLE_WARNING_START - -class VTKStructuredGridReader : public VTKDataSetReaderBase +class VTKM_IO_EXPORT VTKStructuredGridReader : public VTKDataSetReaderBase { public: - explicit VTKStructuredGridReader(const char* fileName) - : VTKDataSetReaderBase(fileName) - { - } + explicit VTKM_CONT VTKStructuredGridReader(const char* fileName); + explicit VTKM_CONT VTKStructuredGridReader(const std::string& fileName); private: - virtual void Read() - { - if (this->DataFile->Structure != vtkm::io::internal::DATASET_STRUCTURED_GRID) - { - throw vtkm::io::ErrorIO("Incorrect DataSet type"); - } - - std::string tag; - - //We need to be able to handle VisIt files which dump Field data - //at the top of a VTK file - this->DataFile->Stream >> tag; - if (tag == "FIELD") - { - this->ReadGlobalFields(); - this->DataFile->Stream >> tag; - } - - // Read structured grid specific meta-data - internal::parseAssert(tag == "DIMENSIONS"); - vtkm::Id3 dim; - this->DataFile->Stream >> dim[0] >> dim[1] >> dim[2] >> std::ws; - - this->DataSet.SetCellSet(internal::CreateCellSetStructured(dim)); - - // Read the points - this->DataFile->Stream >> tag; - internal::parseAssert(tag == "POINTS"); - this->ReadPoints(); - - // Read points and cell attributes - this->ReadAttributes(); - } + VTKM_CONT void Read() override; }; - -VTKM_SILENCE_WEAK_VTABLE_WARNING_END } } // namespace vtkm::io diff --git a/vtkm/io/VTKStructuredPointsReader.cxx b/vtkm/io/VTKStructuredPointsReader.cxx new file mode 100644 index 000000000..1293da374 --- /dev/null +++ b/vtkm/io/VTKStructuredPointsReader.cxx @@ -0,0 +1,79 @@ +//============================================================================ +// 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 + +namespace vtkm +{ +namespace io +{ + +VTKStructuredPointsReader::VTKStructuredPointsReader(const char* fileName) + : VTKDataSetReaderBase(fileName) +{ +} + +VTKStructuredPointsReader::VTKStructuredPointsReader(const std::string& fileName) + : VTKDataSetReaderBase(fileName) +{ +} + +void VTKStructuredPointsReader::Read() +{ + if (this->DataFile->Structure != vtkm::io::internal::DATASET_STRUCTURED_POINTS) + { + throw vtkm::io::ErrorIO("Incorrect DataSet type"); + } + + std::string tag; + + // Read structured points specific meta-data + vtkm::Id3 dim; + vtkm::Vec3f_32 origin, spacing; + + //Two ways the file can describe the dimensions. The proper way is by + //using the DIMENSIONS keyword, but VisIt written VTK files spicify data + //bounds instead, as a FIELD + std::vector visitBounds; + this->DataFile->Stream >> tag; + if (tag == "FIELD") + { + this->ReadGlobalFields(&visitBounds); + this->DataFile->Stream >> tag; + } + if (visitBounds.empty()) + { + internal::parseAssert(tag == "DIMENSIONS"); + this->DataFile->Stream >> dim[0] >> dim[1] >> dim[2] >> std::ws; + this->DataFile->Stream >> tag; + } + + internal::parseAssert(tag == "SPACING"); + this->DataFile->Stream >> spacing[0] >> spacing[1] >> spacing[2] >> std::ws; + if (!visitBounds.empty()) + { + //now with spacing and physical bounds we can back compute the dimensions + dim[0] = static_cast((visitBounds[1] - visitBounds[0]) / spacing[0]); + dim[1] = static_cast((visitBounds[3] - visitBounds[2]) / spacing[1]); + dim[2] = static_cast((visitBounds[5] - visitBounds[4]) / spacing[2]); + } + + this->DataFile->Stream >> tag >> origin[0] >> origin[1] >> origin[2] >> std::ws; + internal::parseAssert(tag == "ORIGIN"); + + this->DataSet.SetCellSet(internal::CreateCellSetStructured(dim)); + this->DataSet.AddCoordinateSystem( + vtkm::cont::CoordinateSystem("coordinates", dim, origin, spacing)); + + // Read points and cell attributes + this->ReadAttributes(); +} +} +} // namespace vtkm::io diff --git a/vtkm/io/VTKStructuredPointsReader.h b/vtkm/io/VTKStructuredPointsReader.h index 17d786380..2e998f96f 100644 --- a/vtkm/io/VTKStructuredPointsReader.h +++ b/vtkm/io/VTKStructuredPointsReader.h @@ -17,70 +17,15 @@ namespace vtkm namespace io { -VTKM_SILENCE_WEAK_VTABLE_WARNING_START - -class VTKStructuredPointsReader : public VTKDataSetReaderBase +class VTKM_IO_EXPORT VTKStructuredPointsReader : public VTKDataSetReaderBase { public: - explicit VTKStructuredPointsReader(const char* fileName) - : VTKDataSetReaderBase(fileName) - { - } + explicit VTKM_CONT VTKStructuredPointsReader(const char* fileName); + explicit VTKM_CONT VTKStructuredPointsReader(const std::string& fileName); private: - virtual void Read() - { - if (this->DataFile->Structure != vtkm::io::internal::DATASET_STRUCTURED_POINTS) - { - throw vtkm::io::ErrorIO("Incorrect DataSet type"); - } - - std::string tag; - - // Read structured points specific meta-data - vtkm::Id3 dim; - vtkm::Vec3f_32 origin, spacing; - - //Two ways the file can describe the dimensions. The proper way is by - //using the DIMENSIONS keyword, but VisIt written VTK files spicify data - //bounds instead, as a FIELD - std::vector visitBounds; - this->DataFile->Stream >> tag; - if (tag == "FIELD") - { - this->ReadGlobalFields(&visitBounds); - this->DataFile->Stream >> tag; - } - if (visitBounds.empty()) - { - internal::parseAssert(tag == "DIMENSIONS"); - this->DataFile->Stream >> dim[0] >> dim[1] >> dim[2] >> std::ws; - this->DataFile->Stream >> tag; - } - - internal::parseAssert(tag == "SPACING"); - this->DataFile->Stream >> spacing[0] >> spacing[1] >> spacing[2] >> std::ws; - if (!visitBounds.empty()) - { - //now with spacing and physical bounds we can back compute the dimensions - dim[0] = static_cast((visitBounds[1] - visitBounds[0]) / spacing[0]); - dim[1] = static_cast((visitBounds[3] - visitBounds[2]) / spacing[1]); - dim[2] = static_cast((visitBounds[5] - visitBounds[4]) / spacing[2]); - } - - this->DataFile->Stream >> tag >> origin[0] >> origin[1] >> origin[2] >> std::ws; - internal::parseAssert(tag == "ORIGIN"); - - this->DataSet.SetCellSet(internal::CreateCellSetStructured(dim)); - this->DataSet.AddCoordinateSystem( - vtkm::cont::CoordinateSystem("coordinates", dim, origin, spacing)); - - // Read points and cell attributes - this->ReadAttributes(); - } + VTKM_CONT void Read() override; }; - -VTKM_SILENCE_WEAK_VTABLE_WARNING_END } } // namespace vtkm::io diff --git a/vtkm/io/VTKUnstructuredGridReader.cxx b/vtkm/io/VTKUnstructuredGridReader.cxx new file mode 100644 index 000000000..0fb69cacb --- /dev/null +++ b/vtkm/io/VTKUnstructuredGridReader.cxx @@ -0,0 +1,88 @@ +//============================================================================ +// 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 + +namespace vtkm +{ +namespace io +{ + +VTKUnstructuredGridReader::VTKUnstructuredGridReader(const char* fileName) + : VTKDataSetReaderBase(fileName) +{ +} + +VTKUnstructuredGridReader::VTKUnstructuredGridReader(const std::string& fileName) + : VTKDataSetReaderBase(fileName) +{ +} + +void VTKUnstructuredGridReader::Read() +{ + if (this->DataFile->Structure != vtkm::io::internal::DATASET_UNSTRUCTURED_GRID) + { + throw vtkm::io::ErrorIO("Incorrect DataSet type"); + } + + //We need to be able to handle VisIt files which dump Field data + //at the top of a VTK file + std::string tag; + this->DataFile->Stream >> tag; + if (tag == "FIELD") + { + this->ReadGlobalFields(); + this->DataFile->Stream >> tag; + } + + // Read the points + internal::parseAssert(tag == "POINTS"); + this->ReadPoints(); + + vtkm::Id numPoints = this->DataSet.GetNumberOfPoints(); + + // Read the cellset + vtkm::cont::ArrayHandle connectivity; + vtkm::cont::ArrayHandle numIndices; + vtkm::cont::ArrayHandle shapes; + + this->DataFile->Stream >> tag; + internal::parseAssert(tag == "CELLS"); + + this->ReadCells(connectivity, numIndices); + this->ReadShapes(shapes); + + vtkm::cont::ArrayHandle permutation; + vtkm::io::internal::FixupCellSet(connectivity, numIndices, shapes, permutation); + this->SetCellsPermutation(permutation); + + //DRP + if (false) //vtkm::io::internal::IsSingleShape(shapes)) + { + vtkm::cont::CellSetSingleType<> cellSet; + cellSet.Fill( + numPoints, shapes.ReadPortal().Get(0), numIndices.ReadPortal().Get(0), connectivity); + this->DataSet.SetCellSet(cellSet); + } + else + { + auto offsets = vtkm::cont::ConvertNumIndicesToOffsets(numIndices); + vtkm::cont::CellSetExplicit<> cellSet; + cellSet.Fill(numPoints, shapes, connectivity, offsets); + this->DataSet.SetCellSet(cellSet); + } + + // Read points and cell attributes + this->ReadAttributes(); +} +} +} diff --git a/vtkm/io/VTKUnstructuredGridReader.h b/vtkm/io/VTKUnstructuredGridReader.h index bee4e06f8..4d60e1d39 100644 --- a/vtkm/io/VTKUnstructuredGridReader.h +++ b/vtkm/io/VTKUnstructuredGridReader.h @@ -17,77 +17,15 @@ namespace vtkm namespace io { -VTKM_SILENCE_WEAK_VTABLE_WARNING_START - -class VTKUnstructuredGridReader : public VTKDataSetReaderBase +class VTKM_IO_EXPORT VTKUnstructuredGridReader : public VTKDataSetReaderBase { public: - explicit VTKUnstructuredGridReader(const char* fileName) - : VTKDataSetReaderBase(fileName) - { - } + explicit VTKM_CONT VTKUnstructuredGridReader(const char* fileName); + explicit VTKM_CONT VTKUnstructuredGridReader(const std::string& fileName); private: - virtual void Read() - { - if (this->DataFile->Structure != vtkm::io::internal::DATASET_UNSTRUCTURED_GRID) - { - throw vtkm::io::ErrorIO("Incorrect DataSet type"); - } - - //We need to be able to handle VisIt files which dump Field data - //at the top of a VTK file - std::string tag; - this->DataFile->Stream >> tag; - if (tag == "FIELD") - { - this->ReadGlobalFields(); - this->DataFile->Stream >> tag; - } - - // Read the points - internal::parseAssert(tag == "POINTS"); - this->ReadPoints(); - - vtkm::Id numPoints = this->DataSet.GetNumberOfPoints(); - - // Read the cellset - vtkm::cont::ArrayHandle connectivity; - vtkm::cont::ArrayHandle numIndices; - vtkm::cont::ArrayHandle shapes; - - this->DataFile->Stream >> tag; - internal::parseAssert(tag == "CELLS"); - - this->ReadCells(connectivity, numIndices); - this->ReadShapes(shapes); - - vtkm::cont::ArrayHandle permutation; - vtkm::io::internal::FixupCellSet(connectivity, numIndices, shapes, permutation); - this->SetCellsPermutation(permutation); - - //DRP - if (false) //vtkm::io::internal::IsSingleShape(shapes)) - { - vtkm::cont::CellSetSingleType<> cellSet; - cellSet.Fill( - numPoints, shapes.ReadPortal().Get(0), numIndices.ReadPortal().Get(0), connectivity); - this->DataSet.SetCellSet(cellSet); - } - else - { - auto offsets = vtkm::cont::ConvertNumIndicesToOffsets(numIndices); - vtkm::cont::CellSetExplicit<> cellSet; - cellSet.Fill(numPoints, shapes, connectivity, offsets); - this->DataSet.SetCellSet(cellSet); - } - - // Read points and cell attributes - this->ReadAttributes(); - } + VTKM_CONT void Read() override; }; - -VTKM_SILENCE_WEAK_VTABLE_WARNING_END } } // namespace vtkm::io