From 08cd163bc482acceccd7eff0e817f921d950553d Mon Sep 17 00:00:00 2001
From: Dmitri Naumov <github@naumov.de>
Date: Sun, 16 Jul 2023 16:33:40 +0200
Subject: [PATCH] [MeL/IO] Move class methods into TU

---
 MeshLib/IO/Legacy/MeshIO.cpp | 377 ++++++++++++++++++-----------------
 MeshLib/IO/Legacy/MeshIO.h   |   8 -
 2 files changed, 190 insertions(+), 195 deletions(-)

diff --git a/MeshLib/IO/Legacy/MeshIO.cpp b/MeshLib/IO/Legacy/MeshIO.cpp
index 7a6626ac5a4..5d9438781ee 100644
--- a/MeshLib/IO/Legacy/MeshIO.cpp
+++ b/MeshLib/IO/Legacy/MeshIO.cpp
@@ -27,138 +27,9 @@
 #include "MeshLib/Node.h"
 #include "MeshLib/PropertyVector.h"
 
-namespace MeshLib
-{
-namespace IO
-{
-namespace Legacy
-{
-MeshIO::MeshIO() = default;
-
-MeshLib::Mesh* MeshIO::loadMeshFromFile(const std::string& file_name)
+namespace
 {
-    INFO("Reading OGS legacy mesh ... ");
-
-    std::ifstream in(file_name.c_str(), std::ios::in);
-    if (!in.is_open())
-    {
-        WARN("MeshIO::loadMeshFromFile() - Could not open file {:s}.",
-             file_name);
-        return nullptr;
-    }
-
-    std::string line_string;
-    getline(in, line_string);
-
-    if (line_string.find("#FEM_MSH") != std::string::npos)  // OGS mesh file
-    {
-        std::vector<MeshLib::Node*> nodes;
-        std::vector<MeshLib::Element*> elements;
-        std::vector<std::size_t> materials;
-
-        while (!in.eof())
-        {
-            getline(in, line_string);
-
-            // check keywords
-            if (line_string.find("#STOP") != std::string::npos)
-            {
-                break;
-            }
-            if (line_string.find("$NODES") != std::string::npos)
-            {
-                double x;
-                double y;
-                double z;
-                double double_dummy;
-                unsigned idx;
-                getline(in, line_string);
-                BaseLib::trim(line_string);
-                unsigned nNodes = atoi(line_string.c_str());
-                std::string s;
-                for (unsigned i = 0; i < nNodes; ++i)
-                {
-                    getline(in, line_string);
-                    std::stringstream iss(line_string);
-                    iss >> idx >> x >> y >> z;
-                    auto* node(new MeshLib::Node(x, y, z, idx));
-                    nodes.push_back(node);
-                    iss >> s;
-                    if (s.find("$AREA") != std::string::npos)
-                    {
-                        iss >> double_dummy;
-                    }
-                }
-            }
-            else if (line_string.find("$ELEMENTS") != std::string::npos)
-            {
-                getline(in, line_string);
-                BaseLib::trim(line_string);
-                unsigned nElements = atoi(line_string.c_str());
-                for (unsigned i = 0; i < nElements; ++i)
-                {
-                    getline(in, line_string);
-                    std::stringstream ss(line_string);
-                    materials.push_back(readMaterialID(ss));
-                    MeshLib::Element* elem(readElement(ss, nodes));
-                    if (elem == nullptr)
-                    {
-                        ERR("Reading mesh element {:d} from file '{:s}' "
-                            "failed.",
-                            i, file_name);
-                        // clean up the elements vector
-                        std::for_each(elements.begin(), elements.end(),
-                                      std::default_delete<MeshLib::Element>());
-                        // clean up the nodes vector
-                        std::for_each(nodes.begin(), nodes.end(),
-                                      std::default_delete<MeshLib::Node>());
-                        return nullptr;
-                    }
-                    elements.push_back(elem);
-                }
-            }
-        }
-
-        if (elements.empty())
-        {
-            ERR("MeshIO::loadMeshFromFile() - File did not contain element "
-                "information.");
-            for (auto& node : nodes)
-            {
-                delete node;
-            }
-            return nullptr;
-        }
-
-        MeshLib::Mesh* mesh(new MeshLib::Mesh(
-            BaseLib::extractBaseNameWithoutExtension(file_name), nodes,
-            elements));
-
-        auto* const material_ids =
-            mesh->getProperties().createNewPropertyVector<int>(
-                "MaterialIDs", MeshLib::MeshItemType::Cell, 1);
-        if (!material_ids)
-        {
-            WARN("Could not create PropertyVector for MaterialIDs in Mesh.");
-        }
-        else
-        {
-            material_ids->insert(material_ids->end(), materials.cbegin(),
-                                 materials.cend());
-        }
-        INFO("\t... finished.");
-        INFO("Nr. Nodes: {:d}.", nodes.size());
-        INFO("Nr. Elements: {:d}.", elements.size());
-
-        in.close();
-        return mesh;
-    }
-
-    in.close();
-    return nullptr;
-}
-
-std::size_t MeshIO::readMaterialID(std::istream& in)
+std::size_t readMaterialID(std::istream& in)
 {
     unsigned index;
     unsigned material_id;
@@ -169,8 +40,8 @@ std::size_t MeshIO::readMaterialID(std::istream& in)
     return material_id;
 }
 
-MeshLib::Element* MeshIO::readElement(
-    std::istream& in, const std::vector<MeshLib::Node*>& nodes) const
+MeshLib::Element* readElement(std::istream& in,
+                              const std::vector<MeshLib::Node*>& nodes)
 {
     std::string elem_type_str;
     MeshLib::MeshElemType elem_type(MeshLib::MeshElemType::INVALID);
@@ -319,53 +190,42 @@ MeshLib::Element* MeshIO::readElement(
     return elem;
 }
 
-bool MeshIO::write()
+std::string ElemType2StringOutput(const MeshLib::MeshElemType t)
 {
-    if (!_mesh)
+    if (t == MeshLib::MeshElemType::LINE)
     {
-        WARN("MeshIO::write(): Cannot write: no mesh object specified.");
-        return false;
+        return "line";
     }
-
-    out << "#FEM_MSH\n"
-        << "$PCS_TYPE\n"
-        << "  NO_PCS\n"
-        << "$NODES\n"
-        << "  ";
-    const std::size_t n_nodes(_mesh->getNumberOfNodes());
-    out << n_nodes << "\n";
-    for (std::size_t i(0); i < n_nodes; ++i)
+    if (t == MeshLib::MeshElemType::QUAD)
     {
-        out << i << " " << *(_mesh->getNode(i)) << "\n";
+        return "quad";
     }
-
-    out << "$ELEMENTS\n"
-        << "  ";
-
-    if (!_mesh->getProperties().existsPropertyVector<int>("MaterialIDs"))
+    if (t == MeshLib::MeshElemType::HEXAHEDRON)
     {
-        writeElements(_mesh->getElements(), nullptr, out);
+        return "hex";
     }
-    else
+    if (t == MeshLib::MeshElemType::TRIANGLE)
     {
-        writeElements(
-            _mesh->getElements(),
-            _mesh->getProperties().getPropertyVector<int>("MaterialIDs"), out);
+        return "tri";
     }
-    out << "#STOP\n";
-
-    return true;
-}
-
-void MeshIO::setMesh(const MeshLib::Mesh* mesh)
-{
-    _mesh = mesh;
+    if (t == MeshLib::MeshElemType::TETRAHEDRON)
+    {
+        return "tet";
+    }
+    if (t == MeshLib::MeshElemType::PRISM)
+    {
+        return "pris";
+    }
+    if (t == MeshLib::MeshElemType::PYRAMID)
+    {
+        return "pyra";
+    }
+    return "none";
 }
 
-void MeshIO::writeElements(
-    std::vector<MeshLib::Element*> const& ele_vec,
-    MeshLib::PropertyVector<int> const* const material_ids,
-    std::ostream& out) const
+void writeElements(std::vector<MeshLib::Element*> const& ele_vec,
+                   MeshLib::PropertyVector<int> const* const material_ids,
+                   std::ostream& out)
 {
     const std::size_t ele_vector_size(ele_vec.size());
 
@@ -400,37 +260,180 @@ void MeshIO::writeElements(
     }
 }
 
-std::string MeshIO::ElemType2StringOutput(const MeshLib::MeshElemType t)
+}  // namespace
+
+namespace MeshLib
 {
-    if (t == MeshLib::MeshElemType::LINE)
-    {
-        return "line";
-    }
-    if (t == MeshLib::MeshElemType::QUAD)
+namespace IO
+{
+namespace Legacy
+{
+MeshIO::MeshIO() = default;
+
+MeshLib::Mesh* MeshIO::loadMeshFromFile(const std::string& file_name)
+{
+    INFO("Reading OGS legacy mesh ... ");
+
+    std::ifstream in(file_name.c_str(), std::ios::in);
+    if (!in.is_open())
     {
-        return "quad";
+        WARN("MeshIO::loadMeshFromFile() - Could not open file {:s}.",
+             file_name);
+        return nullptr;
     }
-    if (t == MeshLib::MeshElemType::HEXAHEDRON)
+
+    std::string line_string;
+    getline(in, line_string);
+
+    if (line_string.find("#FEM_MSH") != std::string::npos)  // OGS mesh file
     {
-        return "hex";
+        std::vector<MeshLib::Node*> nodes;
+        std::vector<MeshLib::Element*> elements;
+        std::vector<std::size_t> materials;
+
+        while (!in.eof())
+        {
+            getline(in, line_string);
+
+            // check keywords
+            if (line_string.find("#STOP") != std::string::npos)
+            {
+                break;
+            }
+            if (line_string.find("$NODES") != std::string::npos)
+            {
+                double x;
+                double y;
+                double z;
+                double double_dummy;
+                unsigned idx;
+                getline(in, line_string);
+                BaseLib::trim(line_string);
+                unsigned nNodes = atoi(line_string.c_str());
+                std::string s;
+                for (unsigned i = 0; i < nNodes; ++i)
+                {
+                    getline(in, line_string);
+                    std::stringstream iss(line_string);
+                    iss >> idx >> x >> y >> z;
+                    auto* node(new MeshLib::Node(x, y, z, idx));
+                    nodes.push_back(node);
+                    iss >> s;
+                    if (s.find("$AREA") != std::string::npos)
+                    {
+                        iss >> double_dummy;
+                    }
+                }
+            }
+            else if (line_string.find("$ELEMENTS") != std::string::npos)
+            {
+                getline(in, line_string);
+                BaseLib::trim(line_string);
+                unsigned nElements = atoi(line_string.c_str());
+                for (unsigned i = 0; i < nElements; ++i)
+                {
+                    getline(in, line_string);
+                    std::stringstream ss(line_string);
+                    materials.push_back(readMaterialID(ss));
+                    MeshLib::Element* elem(readElement(ss, nodes));
+                    if (elem == nullptr)
+                    {
+                        ERR("Reading mesh element {:d} from file '{:s}' "
+                            "failed.",
+                            i, file_name);
+                        // clean up the elements vector
+                        std::for_each(elements.begin(), elements.end(),
+                                      std::default_delete<MeshLib::Element>());
+                        // clean up the nodes vector
+                        std::for_each(nodes.begin(), nodes.end(),
+                                      std::default_delete<MeshLib::Node>());
+                        return nullptr;
+                    }
+                    elements.push_back(elem);
+                }
+            }
+        }
+
+        if (elements.empty())
+        {
+            ERR("MeshIO::loadMeshFromFile() - File did not contain element "
+                "information.");
+            for (auto& node : nodes)
+            {
+                delete node;
+            }
+            return nullptr;
+        }
+
+        MeshLib::Mesh* mesh(new MeshLib::Mesh(
+            BaseLib::extractBaseNameWithoutExtension(file_name), nodes,
+            elements));
+
+        auto* const material_ids =
+            mesh->getProperties().createNewPropertyVector<int>(
+                "MaterialIDs", MeshLib::MeshItemType::Cell, 1);
+        if (!material_ids)
+        {
+            WARN("Could not create PropertyVector for MaterialIDs in Mesh.");
+        }
+        else
+        {
+            material_ids->insert(material_ids->end(), materials.cbegin(),
+                                 materials.cend());
+        }
+        INFO("\t... finished.");
+        INFO("Nr. Nodes: {:d}.", nodes.size());
+        INFO("Nr. Elements: {:d}.", elements.size());
+
+        in.close();
+        return mesh;
     }
-    if (t == MeshLib::MeshElemType::TRIANGLE)
+
+    in.close();
+    return nullptr;
+}
+
+bool MeshIO::write()
+{
+    if (!_mesh)
     {
-        return "tri";
+        WARN("MeshIO::write(): Cannot write: no mesh object specified.");
+        return false;
     }
-    if (t == MeshLib::MeshElemType::TETRAHEDRON)
+
+    out << "#FEM_MSH\n"
+        << "$PCS_TYPE\n"
+        << "  NO_PCS\n"
+        << "$NODES\n"
+        << "  ";
+    const std::size_t n_nodes(_mesh->getNumberOfNodes());
+    out << n_nodes << "\n";
+    for (std::size_t i(0); i < n_nodes; ++i)
     {
-        return "tet";
+        out << i << " " << *(_mesh->getNode(i)) << "\n";
     }
-    if (t == MeshLib::MeshElemType::PRISM)
+
+    out << "$ELEMENTS\n"
+        << "  ";
+
+    if (!_mesh->getProperties().existsPropertyVector<int>("MaterialIDs"))
     {
-        return "pris";
+        writeElements(_mesh->getElements(), nullptr, out);
     }
-    if (t == MeshLib::MeshElemType::PYRAMID)
+    else
     {
-        return "pyra";
+        writeElements(
+            _mesh->getElements(),
+            _mesh->getProperties().getPropertyVector<int>("MaterialIDs"), out);
     }
-    return "none";
+    out << "#STOP\n";
+
+    return true;
+}
+
+void MeshIO::setMesh(const MeshLib::Mesh* mesh)
+{
+    _mesh = mesh;
 }
 
 }  // end namespace Legacy
diff --git a/MeshLib/IO/Legacy/MeshIO.h b/MeshLib/IO/Legacy/MeshIO.h
index 0154defad8e..84fa5dec407 100644
--- a/MeshLib/IO/Legacy/MeshIO.h
+++ b/MeshLib/IO/Legacy/MeshIO.h
@@ -52,14 +52,6 @@ protected:
     bool write() override;
 
 private:
-    void writeElements(std::vector<MeshLib::Element*> const& ele_vec,
-                       MeshLib::PropertyVector<int> const* const material_ids,
-                       std::ostream& out) const;
-    static std::size_t readMaterialID(std::istream & in);
-    MeshLib::Element* readElement(
-        std::istream& in, const std::vector<MeshLib::Node*>& nodes) const;
-    static std::string ElemType2StringOutput(const MeshLib::MeshElemType t);
-
     const MeshLib::Mesh* _mesh{nullptr};
 
 };  /* class */
-- 
GitLab