diff --git a/Applications/Utils/MeshEdit/createLayeredMeshFromRasters.cpp b/Applications/Utils/MeshEdit/createLayeredMeshFromRasters.cpp index 117780c09d56061e207416c6250a91798ad33ea5..9cf34ad330b7df7a2e7a6c6f4be6dc8d84679c3e 100644 --- a/Applications/Utils/MeshEdit/createLayeredMeshFromRasters.cpp +++ b/Applications/Utils/MeshEdit/createLayeredMeshFromRasters.cpp @@ -10,7 +10,6 @@ */ #include <algorithm> -#include <fstream> #include <iterator> #include <memory> #include <string> @@ -20,6 +19,7 @@ #include "InfoLib/GitInfo.h" #include "BaseLib/FileTools.h" +#include "BaseLib/IO/readStringListFromFile.h" #include "MeshLib/IO/readMeshFromFile.h" #include "MeshLib/IO/VtkIO/VtuInterface.h" @@ -28,32 +28,6 @@ #include "MeshLib/Mesh.h" #include "MeshLib/MeshGenerators/MeshLayerMapper.h" -int readRasterPaths(std::string const& raster_list_file, std::vector<std::string> &raster_path_vec) -{ - std::ifstream in (raster_list_file.c_str()); - if (in.fail()) - { - ERR("Could not open file {:s}.", raster_list_file); - return -1; - } - std::string line; - while (getline(in, line)) - { - if (line.empty()) - { - continue; - } - raster_path_vec.push_back(line); - } - if (raster_path_vec.size()<2) - { - ERR ("At least two raster files needed to create 3D mesh."); - return -2; - } - std::reverse(raster_path_vec.begin(), raster_path_vec.end()); - return 0; -} - int main (int argc, char* argv[]) { TCLAP::CmdLine cmd( @@ -128,11 +102,14 @@ int main (int argc, char* argv[]) } INFO("done."); - std::vector<std::string> raster_paths; - if (readRasterPaths(raster_path_arg.getValue(), raster_paths) != 0) + std::vector<std::string> raster_paths = + BaseLib::IO::readStringListFromFile(raster_path_arg.getValue()); + if (raster_paths.size()<2) { + ERR ("At least two raster files needed to create 3D mesh."); return EXIT_FAILURE; } + std::reverse(raster_paths.begin(), raster_paths.end()); MeshLib::MeshLayerMapper mapper; if (auto rasters = FileIO::readRasters(raster_paths)) diff --git a/Applications/Utils/MeshGeoTools/VerticalSliceFromLayers.cpp b/Applications/Utils/MeshGeoTools/VerticalSliceFromLayers.cpp index e0875567288eaff0f7e0b77be46247b334851a88..caf0762e9fb297af55eef90269a4b5d659a97b9a 100644 --- a/Applications/Utils/MeshGeoTools/VerticalSliceFromLayers.cpp +++ b/Applications/Utils/MeshGeoTools/VerticalSliceFromLayers.cpp @@ -9,19 +9,18 @@ #include <algorithm> #include <cmath> -#include <fstream> #include <memory> #include <string> #include <vector> // ThirdParty #include <tclap/CmdLine.h> - #include <QCoreApplication> #include "Applications/FileIO/Gmsh/GMSHInterface.h" #include "Applications/FileIO/Gmsh/GmshReader.h" #include "BaseLib/FileTools.h" +#include "BaseLib/IO/readStringListFromFile.h" #include "GeoLib/AABB.h" #include "GeoLib/AnalyticalGeometry.h" #include "GeoLib/GEOObjects.h" @@ -42,24 +41,6 @@ #include "MeshLib/MeshEditing/RemoveMeshComponents.h" #include "MeshLib/Node.h" -/// reads the list of mesh files into a string vector -std::vector<std::string> readLayerFile(std::string const& layer_file) -{ - std::vector<std::string> layer_names; - std::ifstream in(layer_file); - if (!in) - { - ERR("Could not open layer file {:s}.", layer_file); - return layer_names; - } - std::string line; - while (std::getline(in, line)) - { - layer_names.push_back(line); - } - return layer_names; -} - /// creates a vector of sampling points based on the specified resolution std::unique_ptr<std::vector<GeoLib::Point*>> createPoints( MathLib::Point3d const& start, @@ -369,7 +350,8 @@ int main(int argc, char* argv[]) std::size_t const res = std::ceil(length / res_arg.getValue()); double const interval_length = length / res; - std::vector<std::string> const layer_names = readLayerFile(input_name); + std::vector<std::string> const layer_names = + BaseLib::IO::readStringListFromFile(input_name); if (layer_names.size() < 2) { ERR("At least two layers are required to extract a slice."); diff --git a/BaseLib/IO/readStringListFromFile.cpp b/BaseLib/IO/readStringListFromFile.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd39d3ebb1e3c7f88f9c2e1ae82b69b1fbc7c302 --- /dev/null +++ b/BaseLib/IO/readStringListFromFile.cpp @@ -0,0 +1,43 @@ +/** + * \file + * \copyright + * Copyright (c) 2012-2020, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + */ + +#include "BaseLib/IO/readStringListFromFile.h" + +#include <fstream> + +#include "BaseLib/Logging.h" +#include "BaseLib/StringTools.h" + +namespace BaseLib +{ +namespace IO +{ +std::vector<std::string> readStringListFromFile(std::string const& filename) +{ + std::vector<std::string> string_list; + std::ifstream in(filename); + if (!in) + { + ERR("Could not open file {:s}.", filename); + return string_list; + } + std::string line; + while (std::getline(in, line)) + { + trim(line); + if (line.empty()) + { + continue; + } + string_list.push_back(line); + } + return string_list; +} +} // namespace IO +} // namespace BaseLib diff --git a/BaseLib/IO/readStringListFromFile.h b/BaseLib/IO/readStringListFromFile.h new file mode 100644 index 0000000000000000000000000000000000000000..b388708f3ec1be37bf2fa3c545ba36c3f702ce2e --- /dev/null +++ b/BaseLib/IO/readStringListFromFile.h @@ -0,0 +1,22 @@ +/** + * \file + * \copyright + * Copyright (c) 2012-2020, OpenGeoSys Community (http://www.opengeosys.org) + * Distributed under a Modified BSD License. + * See accompanying file LICENSE.txt or + * http://www.opengeosys.org/project/license + */ + +#pragma once + +#include <string> +#include <vector> + +namespace BaseLib +{ +namespace IO +{ +/// Reads a list of strings from a file into a vector +std::vector<std::string> readStringListFromFile(std::string const& filename); +} +} // namespace BaseLib