OGS
convertMeshToGeo.cpp
Go to the documentation of this file.
1
15#include "convertMeshToGeo.h"
16
17#include "BaseLib/Logging.h"
18#include "GeoLib/GEOObjects.h"
19#include "GeoLib/Surface.h"
20#include "GeoLib/Triangle.h"
23#include "MeshLib/Mesh.h"
26
27namespace
28{
32 double const eps,
33 GeoLib::GEOObjects& geo_objects)
34{
35 std::vector<GeoLib::Point*> points;
36 points.reserve(mesh.getNumberOfNodes());
37
38 std::transform(begin(mesh.getNodes()), end(mesh.getNodes()),
39 std::back_inserter(points),
40 [](MeshLib::Node const* node_ptr)
41 { return new GeoLib::Point(*node_ptr, node_ptr->getID()); });
42
43 auto geoobject_name = mesh.getName(); // Possibly modified when adding
44 // points to the geo objects.
45 geo_objects.addPointVec(std::move(points), geoobject_name, {}, eps);
46 return geoobject_name;
47}
48
50 std::vector<std::size_t> const& id_map,
51 GeoLib::Surface& surface)
52{
54 {
55 surface.addTriangle(id_map[getNodeIndex(e, 0)],
56 id_map[getNodeIndex(e, 1)],
57 id_map[getNodeIndex(e, 2)]);
58 return;
59 }
61 {
62 surface.addTriangle(id_map[getNodeIndex(e, 0)],
63 id_map[getNodeIndex(e, 1)],
64 id_map[getNodeIndex(e, 2)]);
65 surface.addTriangle(id_map[getNodeIndex(e, 0)],
66 id_map[getNodeIndex(e, 2)],
67 id_map[getNodeIndex(e, 3)]);
68 return;
69 }
70 // all other element types are ignored (i.e. lines)
71};
72
73} // namespace
74
75namespace MeshToolsLib
76{
78 GeoLib::GEOObjects& geo_objects,
79 double const eps)
80{
81 if (mesh.getDimension() != 2)
82 {
83 ERR("Mesh to geometry conversion is only working for 2D meshes.");
84 return false;
85 }
86
87 // Special handling of the bounds in case there are no materialIDs present.
88 auto get_material_ids_and_bounds = [&]()
89 -> std::tuple<MeshLib::PropertyVector<int> const*, std::pair<int, int>>
90 {
91 auto const materialIds = materialIDs(mesh);
92 if (!materialIds)
93 {
94 return std::make_tuple(nullptr, std::make_pair(0, 0));
95 }
96
97 auto const bounds =
99 if (!bounds)
100 {
101 OGS_FATAL(
102 "Could not get minimum/maximum ranges values for the "
103 "MaterialIDs property in the mesh '{:s}'.",
104 mesh.getName());
105 }
106 return std::make_tuple(materialIds, *bounds);
107 };
108
109 auto const [materialIds, bounds] = get_material_ids_and_bounds();
110 // elements to surface triangles conversion
111 const unsigned nMatGroups(bounds.second - bounds.first + 1);
112 std::vector<GeoLib::Surface*> sfcs;
113 sfcs.reserve(nMatGroups);
114 std::string const geoobject_name =
115 convertMeshNodesToGeoPoints(mesh, eps, geo_objects);
116 auto const& points = *geo_objects.getPointVec(geoobject_name);
117 for (unsigned i = 0; i < nMatGroups; ++i)
118 {
119 sfcs.push_back(new GeoLib::Surface(points));
120 }
121
122 const std::vector<std::size_t>& id_map(
123 geo_objects.getPointVecObj(geoobject_name)->getIDMap());
124 const std::vector<MeshLib::Element*>& elements = mesh.getElements();
125 const std::size_t nElems(mesh.getNumberOfElements());
126
127 for (unsigned i = 0; i < nElems; ++i)
128 {
129 auto surfaceId = !materialIds ? 0 : ((*materialIds)[i] - bounds.first);
130 addElementToSurface(*elements[i], id_map, *sfcs[surfaceId]);
131 }
132
133 std::for_each(sfcs.begin(), sfcs.end(),
134 [](GeoLib::Surface*& sfc)
135 {
136 if (sfc->getNumberOfTriangles() == 0)
137 {
138 delete sfc;
139 sfc = nullptr;
140 }
141 });
142 auto sfcs_end = std::remove(sfcs.begin(), sfcs.end(), nullptr);
143 sfcs.erase(sfcs_end, sfcs.end());
144
145 geo_objects.addSurfaceVec(std::move(sfcs), geoobject_name,
147 return true;
148}
149
151 const std::string& mesh_name,
152 double eps)
153{
154 // convert to a mesh including duplicated nodes
155 std::vector<MeshLib::Node*> nodes;
156 std::vector<MeshLib::Element*> elements;
157 std::size_t nodeId = 0;
158 for (std::size_t i = 0; i < sfc.getNumberOfTriangles(); i++)
159 {
160 auto* tri = sfc[i];
161 auto** tri_nodes = new MeshLib::Node*[3];
162 for (unsigned j = 0; j < 3; j++)
163 {
164 tri_nodes[j] =
165 new MeshLib::Node(tri->getPoint(j)->data(), nodeId++);
166 }
167 elements.push_back(new MeshLib::Tri(tri_nodes, i));
168 for (unsigned j = 0; j < 3; j++)
169 {
170 nodes.push_back(tri_nodes[j]);
171 }
172 }
173 MeshLib::Mesh mesh_with_duplicated_nodes(
174 mesh_name, nodes, elements, true /* compute_element_neighbors */);
175
176 // remove duplicated nodes
177 MeshToolsLib::MeshRevision rev(mesh_with_duplicated_nodes);
178 return rev.simplifyMesh(mesh_with_duplicated_nodes.getName(), eps);
179}
180
181} // namespace MeshToolsLib
#define OGS_FATAL(...)
Definition Error.h:26
Definition of the GEOObjects class.
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the MeshInformation class.
Definition of the MeshRevision class.
Definition of the Mesh class.
Definition of the Quad class.
Definition of the Tri class.
Container class for geometric objects.
Definition GEOObjects.h:57
const std::vector< Point * > * getPointVec(const std::string &name) const
void addPointVec(std::vector< Point * > &&points, std::string &name, PointVec::NameIdMap &&pnt_id_name_map, double const eps=std::sqrt(std::numeric_limits< double >::epsilon()))
const PointVec * getPointVecObj(const std::string &name) const
const std::vector< std::size_t > & getIDMap() const
Definition PointVec.h:94
A Surface is represented by Triangles. It consists of a reference to a vector of (pointers to) points...
Definition Surface.h:33
std::size_t getNumberOfTriangles() const
Definition Surface.cpp:82
void addTriangle(std::size_t pnt_a, std::size_t pnt_b, std::size_t pnt_c)
Definition Surface.cpp:49
std::map< std::string, std::size_t > NameIdMap
Definition TemplateVec.h:41
virtual MeshElemType getGeomType() const =0
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:106
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:109
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition Mesh.h:88
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:103
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition Mesh.h:100
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:97
static std::optional< std::pair< T, T > > const getValueBounds(MeshLib::PropertyVector< T > const &property)
MeshLib::Mesh * simplifyMesh(const std::string &new_mesh_name, double eps, unsigned min_elem_dim=1) const
Definition of mesh to geometry conversion.
bool convertMeshToGeo(const MeshLib::Mesh &mesh, GeoLib::GEOObjects &geo_objects, double const eps)
MeshLib::Mesh * convertSurfaceToMesh(const GeoLib::Surface &sfc, const std::string &mesh_name, double eps)
void addElementToSurface(MeshLib::Element const &e, std::vector< std::size_t > const &id_map, GeoLib::Surface &surface)
std::string convertMeshNodesToGeoPoints(MeshLib::Mesh const &mesh, double const eps, GeoLib::GEOObjects &geo_objects)