OGS
FlipElements.cpp
Go to the documentation of this file.
1 
11 #include "FlipElements.h"
12 
15 #include "MeshLib/Elements/Line.h"
16 #include "MeshLib/Elements/Quad.h"
17 #include "MeshLib/Elements/Tri.h"
18 #include "MeshLib/Node.h"
19 
20 namespace MeshLib
21 {
22 std::unique_ptr<Element> createFlippedElement(Element const& elem,
23  std::vector<Node*> const& nodes)
24 {
25  if (elem.getDimension() > 2)
26  {
27  return nullptr;
28  }
29 
30  unsigned const n_nodes(elem.getNumberOfNodes());
31  auto elem_nodes = std::make_unique<Node*[]>(n_nodes);
32  for (unsigned i = 0; i < n_nodes; ++i)
33  {
34  elem_nodes[i] = nodes[elem.getNode(i)->getID()];
35  }
36  std::swap(elem_nodes[0], elem_nodes[1]);
37 
38  if (elem.getGeomType() == MeshElemType::LINE)
39  {
40  return std::make_unique<Line>(elem_nodes.release(), elem.getID());
41  }
42  if (elem.getGeomType() == MeshElemType::TRIANGLE)
43  {
44  return std::make_unique<Tri>(elem_nodes.release(), elem.getID());
45  }
46  if (elem.getGeomType() == MeshElemType::QUAD)
47  {
48  std::swap(elem_nodes[2], elem_nodes[3]);
49  return std::make_unique<Quad>(elem_nodes.release(), elem.getID());
50  }
51  return nullptr;
52 }
53 
54 std::unique_ptr<Mesh> createFlippedMesh(Mesh const& mesh)
55 {
56  if (mesh.getDimension() > 2)
57  {
58  return nullptr;
59  }
60 
61  std::vector<Node*> new_nodes(copyNodeVector(mesh.getNodes()));
62  std::vector<Element*> const& elems(mesh.getElements());
63  std::vector<Element*> new_elems;
64  std::size_t n_elems(mesh.getNumberOfElements());
65  new_elems.reserve(n_elems);
66 
67  for (std::size_t i = 0; i < n_elems; ++i)
68  {
69  new_elems.push_back(
70  createFlippedElement(*elems[i], new_nodes).release());
71  }
72 
73  return std::make_unique<Mesh>("FlippedElementMesh", new_nodes, new_elems,
74  mesh.getProperties());
75 }
76 
77 } // end namespace MeshLib
Definition of Duplicate functions.
Definition of the Element class.
Definition of the Line class.
Definition of the Node class.
Definition of the Quad class.
Definition of the Tri class.
std::size_t getID() const
Definition: Point3dWithID.h:62
virtual MeshElemType getGeomType() const =0
virtual const Node * getNode(unsigned idx) const =0
virtual unsigned getNumberOfNodes() const =0
virtual constexpr unsigned getDimension() const =0
Get dimension of the mesh element.
virtual std::size_t getID() const final
Returns the ID of the element.
Definition: Element.h:82
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition: Mesh.h:95
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition: Mesh.h:71
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:98
Properties & getProperties()
Definition: Mesh.h:123
std::size_t getNumberOfElements() const
Get the number of elements.
Definition: Mesh.h:86
std::vector< Node * > copyNodeVector(const std::vector< Node * > &nodes)
Creates a deep copy of a Node vector.
std::unique_ptr< Element > createFlippedElement(Element const &elem, std::vector< Node * > const &nodes)
std::unique_ptr< Mesh > createFlippedMesh(Mesh const &mesh)