From f60f437000575a721bec4f0f6745651aaa089fb0 Mon Sep 17 00:00:00 2001 From: Dave Pugmire Date: Mon, 13 Mar 2023 16:13:21 -0400 Subject: [PATCH] Add VisIt file reader. --- data/data/uniform/venn250.visit | 3 + vtkm/io/CMakeLists.txt | 2 + vtkm/io/VTKVisItFileReader.cxx | 99 +++++++++++++++++++ vtkm/io/VTKVisItFileReader.h | 39 ++++++++ vtkm/io/testing/CMakeLists.txt | 1 + .../UnitTestVisItFileDataSetReader.cxx | 60 +++++++++++ 6 files changed, 204 insertions(+) create mode 100644 data/data/uniform/venn250.visit create mode 100644 vtkm/io/VTKVisItFileReader.cxx create mode 100644 vtkm/io/VTKVisItFileReader.h create mode 100644 vtkm/io/testing/UnitTestVisItFileDataSetReader.cxx diff --git a/data/data/uniform/venn250.visit b/data/data/uniform/venn250.visit new file mode 100644 index 000000000..12e244a73 --- /dev/null +++ b/data/data/uniform/venn250.visit @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:62accba30cde47cac6980dbec18c6edc08e48f353da8f0817a810e66631cb89e +size 38 diff --git a/vtkm/io/CMakeLists.txt b/vtkm/io/CMakeLists.txt index 1689de6b5..3aab645a5 100644 --- a/vtkm/io/CMakeLists.txt +++ b/vtkm/io/CMakeLists.txt @@ -30,6 +30,7 @@ set(headers VTKStructuredGridReader.h VTKStructuredPointsReader.h VTKUnstructuredGridReader.h + VTKVisItFileReader.h ) set(template_sources @@ -57,6 +58,7 @@ set(sources VTKStructuredGridReader.cxx VTKStructuredPointsReader.cxx VTKUnstructuredGridReader.cxx + VTKVisItFileReader.cxx ) if (VTKm_ENABLE_HDF5_IO) diff --git a/vtkm/io/VTKVisItFileReader.cxx b/vtkm/io/VTKVisItFileReader.cxx new file mode 100644 index 000000000..98e1b424c --- /dev/null +++ b/vtkm/io/VTKVisItFileReader.cxx @@ -0,0 +1,99 @@ +//============================================================================ +// 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 + +namespace vtkm +{ +namespace io +{ + +VTKVisItFileReader::VTKVisItFileReader(const char* fileName) + : FileName(fileName) +{ +} + +VTKVisItFileReader::VTKVisItFileReader(const std::string& fileName) + : FileName(fileName) +{ +} + +vtkm::cont::PartitionedDataSet VTKVisItFileReader::ReadPartitionedDataSet() +{ + //Get the base path of the input file. + std::string baseDirPath = "."; + baseDirPath = "../vtk-m/data/data/uniform"; + + //Get the base dir name + auto pos = this->FileName.rfind("/"); + if (pos != std::string::npos) + baseDirPath = baseDirPath.substr(0, pos); + + //Open up the file of filenames. + std::ifstream stream(this->FileName); + if (stream.fail()) + throw vtkm::io::ErrorIO("Failed to open file: " + this->FileName); + + int numBlocks = -1; + std::string line; + std::vector fileNames; + while (stream.good()) + { + std::getline(stream, line); + if (line.size() == 0 || line[0] == '#') + continue; + else if (line.find("!NBLOCKS") != std::string::npos) + { + if (numBlocks > 0) + throw vtkm::io::ErrorIO("Invalid file: " + this->FileName + + ". Number of blocks already specified"); + numBlocks = std::atoi(line.substr(8, line.size()).c_str()); + + if (numBlocks <= 0) + throw vtkm::io::ErrorIO("Invalid file: " + this->FileName + + ". Number of blocks must be > 0"); + } + else if (numBlocks > 0) + { + char char_to_remove = ' '; + line.erase(std::remove(line.begin(), line.end(), char_to_remove), line.end()); + if (line.find(".vtk") != std::string::npos) + fileNames.push_back(baseDirPath + "/" + line); + else + std::cerr << "Skipping: " << line << std::endl; + } + else + { + std::cerr << "Skipping line: " << line << std::endl; + } + } + + vtkm::cont::PartitionedDataSet pds; + + //Read all the files. + for (const auto fn : fileNames) + { + std::ifstream s(fn); + if (s.fail()) + throw vtkm::io::ErrorIO("Failed to open file: " + fn); + + vtkm::io::VTKDataSetReader reader(fn); + pds.AppendPartition(reader.ReadDataSet()); + } + + return pds; +} + +} +} //vtkm::io diff --git a/vtkm/io/VTKVisItFileReader.h b/vtkm/io/VTKVisItFileReader.h new file mode 100644 index 000000000..7b2495b89 --- /dev/null +++ b/vtkm/io/VTKVisItFileReader.h @@ -0,0 +1,39 @@ +//============================================================================ +// 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. +//============================================================================ + +#ifndef vtkm_io_VTKVisItFileReader_h +#define vtkm_io_VTKVisItFileReader_h + +#include +#include +#include +#include + +namespace vtkm +{ +namespace io +{ + +class VTKM_IO_EXPORT VTKVisItFileReader +{ +public: + VTKM_CONT VTKVisItFileReader(const char* fileName); + VTKM_CONT VTKVisItFileReader(const std::string& fileName); + + VTKM_CONT vtkm::cont::PartitionedDataSet ReadPartitionedDataSet(); + +private: + std::string FileName; +}; + +} +} //vtkm::io + +#endif //vtkm_io_VTKVisItFileReader_h diff --git a/vtkm/io/testing/CMakeLists.txt b/vtkm/io/testing/CMakeLists.txt index 1ed94d1b4..de5f86107 100644 --- a/vtkm/io/testing/CMakeLists.txt +++ b/vtkm/io/testing/CMakeLists.txt @@ -10,6 +10,7 @@ set(unit_tests UnitTestBOVDataSetReader.cxx + UnitTestVisItFileDataSetReader.cxx UnitTestFileUtils.cxx UnitTestPixelTypes.cxx UnitTestVTKDataSetReader.cxx diff --git a/vtkm/io/testing/UnitTestVisItFileDataSetReader.cxx b/vtkm/io/testing/UnitTestVisItFileDataSetReader.cxx new file mode 100644 index 000000000..56362d095 --- /dev/null +++ b/vtkm/io/testing/UnitTestVisItFileDataSetReader.cxx @@ -0,0 +1,60 @@ +//============================================================================ +// 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 + +namespace +{ + +inline vtkm::cont::PartitionedDataSet readVisItFileDataSet(const std::string& fname) +{ + vtkm::cont::PartitionedDataSet pds; + vtkm::io::VTKVisItFileReader reader(fname); + try + { + pds = reader.ReadPartitionedDataSet(); + } + catch (vtkm::io::ErrorIO& e) + { + std::string message("Error reading "); + message += fname; + message += ", "; + message += e.GetMessage(); + + VTKM_TEST_FAIL(message.c_str()); + } + + return pds; +} + +} // anonymous namespace + +void TestReadingVisItFileDataSet() +{ + std::string visItFile = vtkm::cont::testing::Testing::DataPath("uniform/venn250.visit"); + + auto const& pds = readVisItFileDataSet(visItFile); + VTKM_TEST_ASSERT(pds.GetNumberOfPartitions() == 2, "Incorrect number of partitions"); + + for (const auto& ds : pds) + { + VTKM_TEST_ASSERT(ds.GetNumberOfPoints() == 63001, "Wrong number of points in partition"); + VTKM_TEST_ASSERT(ds.GetNumberOfFields() == 5, "Wrong number of fields in partition"); + } +} + + +int UnitTestVisItFileDataSetReader(int argc, char* argv[]) +{ + return vtkm::cont::testing::Testing::Run(TestReadingVisItFileDataSet, argc, argv); +}