OGS
MeshNodeSearcher.cpp
Go to the documentation of this file.
1
12#include "MeshNodeSearcher.h"
13
14#include <sstream>
15#include <typeinfo>
16
17#include "BaseLib/Logging.h"
18#include "GeoLib/Point.h"
19#include "GeoLib/Polyline.h"
20#include "GeoLib/Surface.h"
23#include "MeshLib/Mesh.h"
24#include "MeshLib/Node.h"
27#include "MeshNodesOnPoint.h"
28
29namespace MeshGeoToolsLib
30{
31std::vector<std::unique_ptr<MeshNodeSearcher>>
33
35 MeshLib::Mesh const& mesh,
36 std::unique_ptr<MeshGeoToolsLib::SearchLength>&& search_length_algorithm,
37 SearchAllNodes search_all_nodes)
38 : _mesh(mesh),
39 _mesh_grid(_mesh.getNodes().cbegin(), _mesh.getNodes().cend()),
40 _search_length_algorithm(std::move(search_length_algorithm)),
41 _search_all_nodes(search_all_nodes)
42{
43 DBUG("The search length for mesh '{:s}' is {:e}.", _mesh.getName(),
44 _search_length_algorithm->getSearchLength());
45}
46
48{
49 for (auto pointer : _mesh_nodes_on_points)
50 {
51 delete pointer;
52 }
53 for (auto pointer : _mesh_nodes_along_polylines)
54 {
55 delete pointer;
56 }
57 for (auto pointer : _mesh_nodes_along_surfaces)
58 {
59 delete pointer;
60 }
61}
62
63template <typename CacheType, typename GeometryType>
64std::vector<std::size_t> const& getMeshNodeIDs(
65 std::vector<CacheType*>& cached_elements,
66 std::function<GeometryType(CacheType const&)> getCachedItem,
67 GeometryType const& item, MeshLib::Mesh const& mesh,
68 GeoLib::Grid<MeshLib::Node> const& mesh_grid, double const search_length,
69 SearchAllNodes const search_all_nodes)
70{
71 if (auto const it = find_if(cbegin(cached_elements), cend(cached_elements),
72 [&](auto const& element)
73 { return getCachedItem(*element) == item; });
74 it != cend(cached_elements))
75 {
76 return (*it)->getNodeIDs();
77 }
78 // search IDs for geometry object
79 if constexpr (std::is_convertible<GeometryType, GeoLib::Point>::value)
80 {
81 cached_elements.push_back(new CacheType(
82 mesh, mesh_grid, item, search_length, search_all_nodes));
83 }
84 else
85 {
86 cached_elements.push_back(
87 new CacheType(mesh, item, search_length, search_all_nodes));
88 }
89 return cached_elements.back()->getNodeIDs();
90}
91
92std::vector<std::size_t> MeshNodeSearcher::getMeshNodeIDs(
93 GeoLib::GeoObject const& geoObj) const
94{
95 std::vector<std::size_t> vec_nodes;
96 switch (geoObj.getGeoType())
97 {
99 {
100 std::function<GeoLib::Point(MeshNodesOnPoint const&)>
101 get_cached_item_function = &MeshNodesOnPoint::getPoint;
103 _mesh_nodes_on_points, get_cached_item_function,
104 *static_cast<const GeoLib::Point*>(&geoObj), _mesh, _mesh_grid,
105 _search_length_algorithm->getSearchLength(), _search_all_nodes);
106 }
108 {
109 std::function<GeoLib::Polyline(MeshNodesAlongPolyline const&)>
110 get_cached_item_function = &MeshNodesAlongPolyline::getPolyline;
112 _mesh_nodes_along_polylines, get_cached_item_function,
113 *static_cast<const GeoLib::Polyline*>(&geoObj), _mesh,
114 _mesh_grid, _search_length_algorithm->getSearchLength(),
116 }
118 {
119 std::function<GeoLib::Surface(MeshNodesAlongSurface const&)>
120 get_cached_item_function = &MeshNodesAlongSurface::getSurface;
122 _mesh_nodes_along_surfaces, get_cached_item_function,
123 *static_cast<const GeoLib::Surface*>(&geoObj), _mesh,
124 _mesh_grid, _search_length_algorithm->getSearchLength(),
126 }
127 default:
128 break;
129 }
130 return vec_nodes;
131}
132
133std::vector<std::size_t> MeshNodeSearcher::getMeshNodeIDs(
134 std::vector<MathLib::Point3dWithID*> const& points) const
135{
136 double const epsilon_radius = _search_length_algorithm->getSearchLength();
137
138 std::vector<std::size_t> node_ids;
139 node_ids.reserve(points.size());
140
141 for (auto const* const p_ptr : points)
142 {
143 auto const& p = *p_ptr;
144 std::vector<std::size_t> const ids =
145 _mesh_grid.getPointsInEpsilonEnvironment(p, epsilon_radius);
146 if (ids.empty())
147 {
148 OGS_FATAL(
149 "No nodes could be found in the mesh for point {:d} : ({:g}, "
150 "{:g}, {:g}) in {:g} epsilon radius in the mesh '{:s}'",
151 p.getID(), p[0], p[1], p[2], epsilon_radius, _mesh.getName());
152 }
153 if (ids.size() != 1)
154 {
155 std::stringstream ss;
156 auto const& bulk_nodes = _mesh.getNodes();
157 for (auto const id : ids)
158 {
159 ss << "- bulk node: " << (*bulk_nodes[id])[0] << ", "
160 << (*bulk_nodes[id])[1] << ", " << (*bulk_nodes[id])[2]
161 << ", distance: "
162 << std::sqrt(MathLib::sqrDist(*bulk_nodes[id], p)) << "\n";
163 }
164 OGS_FATAL(
165 "Found {:d} nodes in the mesh for point {:d} : ({:g}, {:g}, "
166 "{:g}) in {:g} epsilon radius in the mesh '{:s}'. Expected to "
167 "find exactly one node.\n{:s}",
168 ids.size(), p.getID(), p[0], p[1], p[2], epsilon_radius,
169 _mesh.getName(), ss.str());
170 }
171 node_ids.push_back(ids.front());
172 }
173 return node_ids;
174}
175
177 MeshLib::Mesh const& mesh,
178 std::unique_ptr<MeshGeoToolsLib::SearchLength>&& search_length_algorithm)
179{
180 std::size_t const mesh_id = mesh.getID();
181 if (_mesh_node_searchers.size() < mesh_id + 1)
182 {
183 _mesh_node_searchers.resize(mesh_id + 1);
184 }
185
186 if (_mesh_node_searchers[mesh_id])
187 {
188 auto const& m = *_mesh_node_searchers[mesh_id];
189 // return searcher if search length algorithm and the returned search
190 // length are the same, else recreate the searcher
191 if (typeid(m._search_length_algorithm) ==
192 typeid(search_length_algorithm) &&
193 m._search_length_algorithm->getSearchLength() ==
194 search_length_algorithm->getSearchLength())
195 {
196 return m;
197 }
198 }
199
200 _mesh_node_searchers[mesh_id] =
201 std::make_unique<MeshGeoToolsLib::MeshNodeSearcher>(
202 mesh, std::move(search_length_algorithm), SearchAllNodes::Yes);
203
204 return *_mesh_node_searchers[mesh_id];
205}
206
207} // end namespace MeshGeoToolsLib
Definition of the Element class.
#define OGS_FATAL(...)
Definition Error.h:26
Definition of the Point class.
Interface for heuristic search length strategy.
std::vector< std::size_t > getNodes(GeoLib::Point const &pnt, std::vector< MeshLib::Node * > const &nodes, MeshLib::PropertyVector< int > const &mat_ids, std::pair< int, int > const &mat_limits, std::pair< double, double > const &elevation_limits, MeshLib::Mesh const &mesh)
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
Definition of the Mesh class.
Definition of the Node class.
Definition of the PolyLine class.
std::vector< std::size_t > getPointsInEpsilonEnvironment(P const &pnt, double eps) const
Definition Grid.h:676
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
Definition Polyline.h:40
A Surface is represented by Triangles. It consists of a reference to a vector of (pointers to) points...
Definition Surface.h:33
std::vector< MeshNodesOnPoint * > _mesh_nodes_on_points
std::vector< std::size_t > getMeshNodeIDs(GeoLib::GeoObject const &geoObj) const
GeoLib::Grid< MeshLib::Node > _mesh_grid
static std::vector< std::unique_ptr< MeshNodeSearcher > > _mesh_node_searchers
Mesh node searcher for the meshes indexed by the meshs' ids.
std::unique_ptr< MeshGeoToolsLib::SearchLength > _search_length_algorithm
std::vector< MeshNodesAlongSurface * > _mesh_nodes_along_surfaces
static OGS_NO_DANGLING MeshNodeSearcher const & getMeshNodeSearcher(MeshLib::Mesh const &mesh, std::unique_ptr< MeshGeoToolsLib::SearchLength > &&search_length_algorithm)
std::vector< MeshNodesAlongPolyline * > _mesh_nodes_along_polylines
MeshNodeSearcher(MeshLib::Mesh const &mesh, std::unique_ptr< MeshGeoToolsLib::SearchLength > &&search_length_algorithm, SearchAllNodes search_all_nodes)
GeoLib::Polyline const & getPolyline() const
GeoLib::Surface const & getSurface() const
GeoLib::Point const & getPoint() const
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:106
std::size_t getID() const
Get id of the mesh.
Definition Mesh.h:121
const std::string getName() const
Get name of the mesh.
Definition Mesh.h:103
double sqrDist(MathLib::Point3d const &p0, MathLib::Point3d const &p1)
Definition Point3d.cpp:26
std::vector< std::size_t > const & getMeshNodeIDs(std::vector< CacheType * > &cached_elements, std::function< GeometryType(CacheType const &)> getCachedItem, GeometryType const &item, MeshLib::Mesh const &mesh, GeoLib::Grid< MeshLib::Node > const &mesh_grid, double const search_length, SearchAllNodes const search_all_nodes)
virtual GEOTYPE getGeoType() const =0
return a geometry type