diff --git a/MeshLib/MeshEditing/AddLayerToMesh.cpp b/MeshLib/MeshEditing/AddLayerToMesh.cpp index 07b08c83659737e21670b74ec25dc0ab7eab4504..07736e29863bd70dc86723889388b61154d4da39 100644 --- a/MeshLib/MeshEditing/AddLayerToMesh.cpp +++ b/MeshLib/MeshEditing/AddLayerToMesh.cpp @@ -82,11 +82,10 @@ MeshLib::Mesh* addLayerToMesh(MeshLib::Mesh const& mesh, double thickness, std::unique_ptr<MeshLib::Mesh> sfc_mesh (nullptr); if (mesh.getDimension() == 3) - sfc_mesh = std::unique_ptr<MeshLib::Mesh>( - MeshLib::MeshSurfaceExtraction::getMeshSurface(mesh, dir, angle, true)); + sfc_mesh.reset(MeshLib::MeshSurfaceExtraction::getMeshSurface(mesh, dir, angle, true)); else sfc_mesh = (on_top) ? std::unique_ptr<MeshLib::Mesh>(new MeshLib::Mesh(mesh)) : - std::unique_ptr<MeshLib::Mesh>(MeshLib::flipMeshElements(mesh)); + std::unique_ptr<MeshLib::Mesh>(MeshLib::createFlippedMesh(mesh)); INFO("done."); // *** add new surface nodes diff --git a/MeshLib/MeshEditing/FlipElements.cpp b/MeshLib/MeshEditing/FlipElements.cpp index 6cd042384570ec2d707e12fb9516bb5b2ba5e5f7..d2271971122f43977a47aa4f0f2e2cdff05b71ed 100644 --- a/MeshLib/MeshEditing/FlipElements.cpp +++ b/MeshLib/MeshEditing/FlipElements.cpp @@ -19,35 +19,30 @@ namespace MeshLib { -MeshLib::Element* flipElement(MeshLib::Element const& elem, std::vector<MeshLib::Node*> const& nodes) +std::unique_ptr<MeshLib::Element> createFlippedElement(MeshLib::Element const& elem, std::vector<MeshLib::Node*> const& nodes) { if (elem.getDimension()>2) return nullptr; - std::size_t const n_nodes (elem.getNNodes()); + unsigned const n_nodes (elem.getNNodes()); MeshLib::Node** elem_nodes = new MeshLib::Node*[n_nodes]; for (unsigned i=0; i<n_nodes; ++i) elem_nodes[i] = nodes[elem.getNode(i)->getID()]; - MeshLib::Node* tmp (elem_nodes[0]); - elem_nodes[0] = elem_nodes[1]; - elem_nodes[1] = tmp; - + std::swap(elem_nodes[0], elem_nodes[1]); + if (elem.getGeomType() == MeshElemType::LINE) - return new MeshLib::Line(elem_nodes, elem.getID()); + return std::make_unique<MeshLib::Line>(MeshLib::Line(elem_nodes, elem.getID())); else if (elem.getGeomType() == MeshElemType::TRIANGLE) - return new MeshLib::Tri(elem_nodes, elem.getID()); + return std::make_unique<MeshLib::Tri>(MeshLib::Tri(elem_nodes, elem.getID())); else if (elem.getGeomType() == MeshElemType::QUAD) { - tmp = elem_nodes[2]; - elem_nodes[2] = elem_nodes[3]; - elem_nodes[3] = tmp; - return new MeshLib::Quad(elem_nodes, elem.getID()); + std::swap(elem_nodes[2], elem_nodes[3]); + return std::make_unique<MeshLib::Quad>(MeshLib::Quad(elem_nodes, elem.getID())); } - return nullptr; } -MeshLib::Mesh* flipMeshElements(MeshLib::Mesh const& mesh) +std::unique_ptr<MeshLib::Mesh> createFlippedMesh(MeshLib::Mesh const& mesh) { if (mesh.getDimension() > 2) return nullptr; @@ -59,10 +54,10 @@ MeshLib::Mesh* flipMeshElements(MeshLib::Mesh const& mesh) new_elems.reserve(n_elems); for (std::size_t i=0; i<n_elems; ++i) - new_elems.push_back(flipElement(*elems[i], new_nodes)); + new_elems.push_back(createFlippedElement(*elems[i], new_nodes).release()); MeshLib::Properties new_props (mesh.getProperties()); - return new MeshLib::Mesh("FlippedElementMesh", new_nodes, new_elems, new_props); + return std::make_unique<MeshLib::Mesh>(MeshLib::Mesh("FlippedElementMesh", new_nodes, new_elems, new_props)); } } // end namespace MeshLib diff --git a/MeshLib/MeshEditing/FlipElements.h b/MeshLib/MeshEditing/FlipElements.h index a4305ad13b9fbbba1a7e8bb18f2e41b9710cc5a9..6fa127e8a6e891f1a699301fed8e76d2b15a4b61 100644 --- a/MeshLib/MeshEditing/FlipElements.h +++ b/MeshLib/MeshEditing/FlipElements.h @@ -10,6 +10,7 @@ #ifndef FLIPELEMENTS_H_ #define FLIPELEMENTS_H_ +#include <memory> #include <vector> #include "MeshLib/Elements/Element.h" @@ -19,17 +20,23 @@ class Mesh; class Node; /** - * Reverses the node order of a 1D / 2D element such that the direction - * of a line changes and normals of 2D elements changes its sign. + * Creates a copy of an 1d / 2d element where the node order is reversed, + * such that the direction of a line changes and normals of 2D elements + * changes its sign. + * @param elem original element + * @param nodes node vector used for the copy of the element + * @return a flipped copy of the element */ -MeshLib::Element* flipElement(MeshLib::Element const& elem, std::vector<MeshLib::Node*> const& nodes); +std::unique_ptr<MeshLib::Element> flipElement(MeshLib::Element const& elem, std::vector<MeshLib::Node*> const& nodes); /** - * Reverses the node order of all elements in a 1D / 2D mesh such that - * the direction of lines changes and normals of 2D elements changes - * their sign. + * Creates a copy of a 1d / 2d mesh where the node order of all elements + * is reversed such that the direction of lines changes and normals of + * 2D elements changes their sign. + * @param mesh input mesh + * @return a flipped copy of the input mesh */ -MeshLib::Mesh* flipMeshElements(MeshLib::Mesh const& mesh); +std::unique_ptr<MeshLib::Mesh> createFlippedMesh(MeshLib::Mesh const& mesh); }