From e1462ac7f0cb23ad25f8d4496b3f4f0c869ba7c5 Mon Sep 17 00:00:00 2001 From: rinkk <karsten.rink@ufz.de> Date: Wed, 10 Jul 2019 12:23:03 +0200 Subject: [PATCH] changed mesh vector to outside variable --- Applications/DataExplorer/mainwindow.cpp | 6 +- .../FileIO/GocadIO/GocadTSurfaceReader.cpp | 121 ++++-------------- .../FileIO/GocadIO/GocadTSurfaceReader.h | 29 +---- .../FileConverter/GocadTSurfaceReader.cpp | 31 ++++- 4 files changed, 55 insertions(+), 132 deletions(-) diff --git a/Applications/DataExplorer/mainwindow.cpp b/Applications/DataExplorer/mainwindow.cpp index 7bacbe337c7..36728d5ae51 100644 --- a/Applications/DataExplorer/mainwindow.cpp +++ b/Applications/DataExplorer/mainwindow.cpp @@ -615,10 +615,10 @@ void MainWindow::loadFile(ImportFileType::type t, const QString &fileName) else if (t == ImportFileType::GOCAD_TSURF) { std::string file_name(fileName.toStdString()); - FileIO::Gocad::GocadTSurfaceReader gcts (file_name); - if (gcts.readFile()) + FileIO::Gocad::GocadTSurfaceReader gcts; + std::vector<MeshLib::Mesh*> meshes; + if (gcts.readFile(file_name, meshes)) { - std::vector<MeshLib::Mesh*> meshes = gcts.getData(); for (MeshLib::Mesh* m : meshes) { std::unique_ptr<MeshLib::Mesh> mesh(m); diff --git a/Applications/FileIO/GocadIO/GocadTSurfaceReader.cpp b/Applications/FileIO/GocadIO/GocadTSurfaceReader.cpp index 2dd25a9a9b3..1e86220921a 100644 --- a/Applications/FileIO/GocadIO/GocadTSurfaceReader.cpp +++ b/Applications/FileIO/GocadIO/GocadTSurfaceReader.cpp @@ -17,7 +17,6 @@ #include "BaseLib/FileTools.h" #include "BaseLib/StringTools.h" #include "MeshLib/Elements/Tri.h" -#include "MeshLib/IO/VtkIO/VtuInterface.h" #include "MeshLib/Mesh.h" #include "MeshLib/Node.h" @@ -28,38 +27,41 @@ namespace Gocad const std::string mat_id_name = "MaterialIDs"; const std::string eof_error = "Error: Unexpected end of file."; -GocadTSurfaceReader::GocadTSurfaceReader(std::string const& filename) - : _file_name(filename) +GocadTSurfaceReader::GocadTSurfaceReader() { } -bool GocadTSurfaceReader::readFile() +bool GocadTSurfaceReader::readFile( + std::string const& file_name, std::vector<MeshLib::Mesh*>& meshes) { - std::ifstream in(_file_name.c_str()); + std::ifstream in(file_name.c_str()); if (!in.is_open()) { ERR("GocadTSurfaceReader::readFile(): Could not open file %s.", - _file_name.c_str()); + file_name.c_str()); return false; } while (TSurfaceFound(in)) { - if (!readMesh(in)) + std::string mesh_name = BaseLib::dropFileExtension(file_name) + + std::to_string(meshes.size() + 1); + MeshLib::Mesh* mesh = readMesh(in, mesh_name); + if (mesh == nullptr) { ERR("File parsing aborted...") return false; } + meshes.push_back(mesh); } return true; } -bool GocadTSurfaceReader::readMesh(std::ifstream& in) +MeshLib::Mesh* GocadTSurfaceReader::readMesh(std::ifstream& in, + std::string& mesh_name) { - std::string const mesh_cnt = "-" + std::to_string(_mesh_vec.size() + 1); - std::string mesh_name = BaseLib::dropFileExtension(_file_name) + mesh_cnt; if (!parseHeader(in, mesh_name)) - return false; + return nullptr; MeshLib::Properties mesh_prop; mesh_prop.createNewPropertyVector<int>(mat_id_name, @@ -76,7 +78,7 @@ bool GocadTSurfaceReader::readMesh(std::ifstream& in) if (!coordinate_system.parse(in)) { ERR("Error parsing coordinate system."); - return false; + return nullptr; } } else if (str[0] == "GEOLOGICAL_FEATURE" || @@ -90,7 +92,7 @@ bool GocadTSurfaceReader::readMesh(std::ifstream& in) if (!parsePropertyClass(in)) { ERR("Error parsing PROPERTY_CLASS_HEADER."); - return false; + return nullptr; } } else if (str[0] == "PROPERTIES") @@ -98,7 +100,7 @@ bool GocadTSurfaceReader::readMesh(std::ifstream& in) if (!parseProperties(in, str, mesh_prop)) { ERR("Error parsing PROPERTIES"); - return false; + return nullptr; } } else if (str[0] == "TFACE") @@ -111,11 +113,10 @@ bool GocadTSurfaceReader::readMesh(std::ifstream& in) { ERR("Error parsing Surface %s.", mesh_name.c_str()); clearData(nodes, elems); - return false; + return nullptr; } - _mesh_vec.push_back( - new MeshLib::Mesh(mesh_name, nodes, elems, mesh_prop)); - return true; + + return new MeshLib::Mesh(mesh_name, nodes, elems, mesh_prop); } else { @@ -127,87 +128,6 @@ bool GocadTSurfaceReader::readMesh(std::ifstream& in) return false; } -MeshLib::Mesh* GocadTSurfaceReader::getData(std::size_t const idx) const -{ - if (_mesh_vec.empty()) - { - ERR("Error: No mesh data available."); - return nullptr; - } - if (idx < _mesh_vec.size()) - return _mesh_vec[idx]; - ERR("Error: Mesh index (%d) out of range (0, %d).", idx, _mesh_vec.size()-1); - return nullptr; -} - -std::vector<MeshLib::Mesh*> GocadTSurfaceReader::getData() const -{ - if (_mesh_vec.empty()) - { - ERR("Error: No mesh data available."); - } - return _mesh_vec; -} - -std::string GocadTSurfaceReader::getMeshName(std::size_t idx) const -{ - if (_mesh_vec.empty()) - { - ERR("Error: No mesh data available."); - return std::string(); - } - if (idx < _mesh_vec.size()) - return _mesh_vec[idx]->getName(); - ERR("Error: Mesh index (%d) out of range (0, %d).", idx, _mesh_vec.size()-1); - return std::string(); -} - -void GocadTSurfaceReader::writeData(std::string const& file_name, - std::size_t const idx, - bool write_binary) const -{ - if (_mesh_vec.empty()) - { - ERR("Error: No mesh data available."); - return; - } - if (idx < _mesh_vec.size()) - { - INFO("Writing mesh \"%s\"", file_name.c_str()); - int data_mode = (write_binary) ? 2 : 0; - bool compressed = (write_binary) ? true : false; - MeshLib::IO::VtuInterface vtu(_mesh_vec[idx], data_mode, compressed); - vtu.writeToFile(file_name); - return; - } - ERR("Error: Mesh index (%d) out of range (0, %d).", idx, _mesh_vec.size()-1); - return; -} - -std::string getDelim(std::string const& str) -{ - std::size_t const bslash = str.find_first_of('\\'); - char const delim = (bslash == str.npos) ? '/' : '\\'; - return (str.back() == delim) ? "" : std::string(1, delim); -} - -void GocadTSurfaceReader::writeData(std::string const& dir, - bool write_binary) const -{ - if (_mesh_vec.empty()) - { - ERR("Error: No mesh data available."); - return; - } - std::size_t const n_meshes(_mesh_vec.size()); - - std::string const delim = getDelim(dir); - for (std::size_t i = 0; i < n_meshes; ++i) - { - writeData(dir + delim + _mesh_vec[i]->getName() + ".vtu", i, write_binary); - } -} - bool GocadTSurfaceReader::TSurfaceFound(std::ifstream& in) const { std::string line(""); @@ -247,7 +167,10 @@ bool GocadTSurfaceReader::parseHeader(std::ifstream& in, std::string& mesh_name) while (std::getline(in, line)) { if (line.substr(0, 5) == "name:") + { mesh_name = line.substr(5, line.length() - 5); + BaseLib::trim(mesh_name, ' '); + } else if (line.substr(0, 1) == "}") return true; // ignore all other header parameters diff --git a/Applications/FileIO/GocadIO/GocadTSurfaceReader.h b/Applications/FileIO/GocadIO/GocadTSurfaceReader.h index 47c7f67473b..4ecec7aeb6b 100644 --- a/Applications/FileIO/GocadIO/GocadTSurfaceReader.h +++ b/Applications/FileIO/GocadIO/GocadTSurfaceReader.h @@ -33,39 +33,19 @@ public: * Constructor takes as argument the Gocad .sg text file. * @param fname file name */ - explicit GocadTSurfaceReader(std::string const& fname); + explicit GocadTSurfaceReader(); - GocadTSurfaceReader() = delete; GocadTSurfaceReader(GocadTSurfaceReader&& src) = delete; GocadTSurfaceReader(GocadTSurfaceReader const& src) = delete; GocadTSurfaceReader& operator=(GocadTSurfaceReader&& rhs) = delete; GocadTSurfaceReader& operator=(GocadTSurfaceReader const& rhs) = delete; /// Reads the specified file and writes data into internal mesh vector - bool readFile(); - - /// Returns the specified mesh from the internal mesh vector - MeshLib::Mesh* getData(std::size_t const idx) const; - - /// Returns the complete mesh vector - std::vector<MeshLib::Mesh*> getData() const; - - /// Returns the name of the specified mesh - std::string getMeshName(std::size_t idx) const; - - /// Returns the number of meshes in the mesh vector - std::size_t getNumberOfMeshes() const { return _mesh_vec.size(); }; - - /// Writes one mesh to the specified file - void writeData(std::string const& file_name, std::size_t const idx, - bool write_binary) const; - - /// Writes all meshes into the specified directory - void writeData(std::string const& dir, bool write_binary = false) const; + bool readFile(std::string const& file_name, std::vector<MeshLib::Mesh*>& meshes); private: /// Reads one mesh contained in the file (there may be more than one!) - bool readMesh(std::ifstream& in); + MeshLib::Mesh* readMesh(std::ifstream& in, std::string& mesh_name); /// Checks if the current line is a comment bool isCommentLine(std::string const& str) const; @@ -117,9 +97,6 @@ private: PVRTX }; - std::vector<MeshLib::Mesh*> _mesh_vec; - std::string _file_name; - }; // end class GocadTSurfaceReader } // end namespace Gocad diff --git a/Applications/Utils/FileConverter/GocadTSurfaceReader.cpp b/Applications/Utils/FileConverter/GocadTSurfaceReader.cpp index 0ad2c30457a..f4a6fb3ec68 100644 --- a/Applications/Utils/FileConverter/GocadTSurfaceReader.cpp +++ b/Applications/Utils/FileConverter/GocadTSurfaceReader.cpp @@ -10,9 +10,18 @@ #include <tclap/CmdLine.h> #include "BaseLib/BuildInfo.h" +#include "MeshLib/Mesh.h" +#include "MeshLib/IO/VtkIO/VtuInterface.h" #include "Applications/ApplicationsLib/LogogSetup.h" #include "Applications/FileIO/GocadIO/GocadTSurfaceReader.h" +std::string getDelim(std::string const& str) +{ + std::size_t const bslash = str.find_first_of('\\'); + char const delim = (bslash == str.npos) ? '/' : '\\'; + return (str.back() == delim) ? "" : std::string(1, delim); +} + int main(int argc, char* argv[]) { ApplicationsLib::LogogSetup logog_setup; @@ -44,9 +53,23 @@ int main(int argc, char* argv[]) cmd.parse(argc, argv); - FileIO::Gocad::GocadTSurfaceReader gcts(input_arg.getValue()); - gcts.readFile(); - gcts.writeData(output_arg.getValue(), write_binary_arg.getValue()); - + std::string const file_name (input_arg.getValue()); + FileIO::Gocad::GocadTSurfaceReader gcts; + std::vector<MeshLib::Mesh*> meshes; + if (!gcts.readFile(file_name, meshes)) + { + ERR("Error reading file."); + } + std::string const dir = output_arg.getValue(); + bool const write_binary = write_binary_arg.getValue(); + std::string const delim = getDelim(dir); + for (MeshLib::Mesh* mesh : meshes) + { + INFO("Writing mesh \"%s\"", mesh->getName().c_str()); + int data_mode = (write_binary) ? 2 : 0; + bool compressed = (write_binary) ? true : false; + MeshLib::IO::VtuInterface vtu(mesh, data_mode, compressed); + vtu.writeToFile(dir + delim + mesh->getName() + ".vtu"); + } return 0; } -- GitLab