From 982cceff05dfa1c6a179f80af2493471cf5fba08 Mon Sep 17 00:00:00 2001
From: Thomas Fischer <thomas.fischer@ufz.de>
Date: Thu, 23 Jan 2020 14:23:38 +0100
Subject: [PATCH] [MeL/ME/DuplicateMeshComponents] copyElement: Opt. mapping.

---
 .../MeshEditing/DuplicateMeshComponents.cpp   | 40 +++++++++++++------
 MeshLib/MeshEditing/DuplicateMeshComponents.h |  6 ++-
 2 files changed, 31 insertions(+), 15 deletions(-)

diff --git a/MeshLib/MeshEditing/DuplicateMeshComponents.cpp b/MeshLib/MeshEditing/DuplicateMeshComponents.cpp
index 154969bc145..81f690af799 100644
--- a/MeshLib/MeshEditing/DuplicateMeshComponents.cpp
+++ b/MeshLib/MeshEditing/DuplicateMeshComponents.cpp
@@ -51,35 +51,49 @@ std::vector<MeshLib::Element*> copyElementVector(
 /// mesh.
 template <typename E>
 MeshLib::Element* copyElement(MeshLib::Element const* const element,
-                              const std::vector<MeshLib::Node*>& nodes)
+                              const std::vector<MeshLib::Node*>& nodes,
+                              std::vector<std::size_t> const* const id_map)
 {
-    auto** new_nodes = new MeshLib::Node*[element->getNumberOfNodes()];
-    for (unsigned i = 0; i < element->getNumberOfNodes(); ++i)
+    unsigned const number_of_element_nodes(element->getNumberOfNodes());
+    auto** new_nodes = new MeshLib::Node*[number_of_element_nodes];
+    if (id_map)
     {
-        new_nodes[i] = nodes[element->getNode(i)->getID()];
+        for (unsigned i = 0; i < number_of_element_nodes; ++i)
+        {
+            new_nodes[i] = nodes[(*id_map)[element->getNode(i)->getID()]];
+        }
+    }
+    else
+    {
+        for (unsigned i = 0; i < number_of_element_nodes; ++i)
+        {
+            new_nodes[i] = nodes[element->getNode(i)->getID()];
+        }
     }
     return new E(new_nodes);
 }
 
-MeshLib::Element* copyElement(MeshLib::Element const* const element,
-                              const std::vector<MeshLib::Node*>& nodes)
+MeshLib::Element* copyElement(
+    MeshLib::Element const* const element,
+    const std::vector<MeshLib::Node*>& nodes,
+    std::vector<std::size_t> const* const id_map)
 {
     switch (element->getGeomType())
     {
         case MeshElemType::LINE:
-            return copyElement<MeshLib::Line>(element, nodes);
+            return copyElement<MeshLib::Line>(element, nodes, id_map);
         case MeshElemType::TRIANGLE:
-            return copyElement<MeshLib::Tri>(element, nodes);
+            return copyElement<MeshLib::Tri>(element, nodes, id_map);
         case MeshElemType::QUAD:
-            return copyElement<MeshLib::Quad>(element, nodes);
+            return copyElement<MeshLib::Quad>(element, nodes, id_map);
         case MeshElemType::TETRAHEDRON:
-            return copyElement<MeshLib::Tet>(element, nodes);
+            return copyElement<MeshLib::Tet>(element, nodes, id_map);
         case MeshElemType::HEXAHEDRON:
-            return copyElement<MeshLib::Hex>(element, nodes);
+            return copyElement<MeshLib::Hex>(element, nodes, id_map);
         case MeshElemType::PYRAMID:
-            return copyElement<MeshLib::Pyramid>(element, nodes);
+            return copyElement<MeshLib::Pyramid>(element, nodes, id_map);
         case MeshElemType::PRISM:
-            return copyElement<MeshLib::Prism>(element, nodes);
+            return copyElement<MeshLib::Prism>(element, nodes, id_map);
         default:
         {
     ERR ("Error: Unknown element type.");
diff --git a/MeshLib/MeshEditing/DuplicateMeshComponents.h b/MeshLib/MeshEditing/DuplicateMeshComponents.h
index dc2de0a784e..764cb88839d 100644
--- a/MeshLib/MeshEditing/DuplicateMeshComponents.h
+++ b/MeshLib/MeshEditing/DuplicateMeshComponents.h
@@ -38,8 +38,10 @@ std::vector<MeshLib::Element*> copyElementVector(
 
 /// Copies an element without change, using the nodes vector from the result
 /// mesh.
-MeshLib::Element* copyElement(MeshLib::Element const* const element,
-                              const std::vector<MeshLib::Node*>& nodes);
+MeshLib::Element* copyElement(
+    MeshLib::Element const* const element,
+    const std::vector<MeshLib::Node*>& nodes,
+    std::vector<std::size_t> const* const id_map = nullptr);
 
 /// Clones a vector of elements using the Element::clone() function.
 std::vector<MeshLib::Element*> cloneElements(
-- 
GitLab