OGS
GMSHAdaptiveMeshDensity.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <list>
7
8#include "BaseLib/Logging.h"
9#include "GeoLib/Point.h"
10#include "GeoLib/Polygon.h"
11#ifndef NDEBUG
12#include "GeoLib/GEOObjects.h"
13#include "GeoLib/Polyline.h"
14#endif
15#include "GeoLib/QuadTree.h"
16#include "MathLib/MathTools.h"
17
18namespace FileIO
19{
20namespace GMSH
21{
23 double station_density,
24 std::size_t max_pnts_per_leaf)
25 : _pnt_density(pnt_density),
26 _station_density(station_density),
27 _max_pnts_per_leaf(max_pnts_per_leaf)
28{
29}
30
35
37 std::vector<GeoLib::Point const*> const& pnts)
38{
39 // *** QuadTree - determining bounding box
40 DBUG(
41 "GMSHAdaptiveMeshDensity::init(): computing axis aligned bounding box "
42 "(2D) for quadtree.");
43
44 GeoLib::Point min(*pnts[0]);
45 GeoLib::Point max(*pnts[0]);
46 std::size_t n_pnts(pnts.size());
47 for (std::size_t k(1); k < n_pnts; k++)
48 {
49 for (std::size_t j(0); j < 2; j++)
50 {
51 if ((*(pnts[k]))[j] < min[j])
52 {
53 min[j] = (*(pnts[k]))[j];
54 }
55 }
56 for (std::size_t j(0); j < 2; j++)
57 {
58 if ((*(pnts[k]))[j] > max[j])
59 {
60 max[j] = (*(pnts[k]))[j];
61 }
62 }
63 }
64 min[2] = 0.0;
65 max[2] = 0.0;
66 DBUG("GMSHAdaptiveMeshDensity::init(): \tok");
67
68 // *** QuadTree - create object
69 DBUG("GMSHAdaptiveMeshDensity::init(): Creating quadtree.");
72 DBUG("GMSHAdaptiveMeshDensity::init(): \tok.");
73
74 // *** QuadTree - insert points
75 addPoints(pnts);
76}
77
79 std::vector<GeoLib::Point const*> const& pnts)
80{
81 // *** QuadTree - insert points
82 const std::size_t n_pnts(pnts.size());
83 DBUG(
84 "GMSHAdaptiveMeshDensity::addPoints(): Inserting {:d} points into "
85 "quadtree.",
86 n_pnts);
87 for (std::size_t k(0); k < n_pnts; k++)
88 {
89 _quad_tree->addPoint(pnts[k]);
90 }
91 DBUG("GMSHAdaptiveMeshDensity::addPoints(): \tok.");
92 _quad_tree->balance();
93}
94
96 GeoLib::Point const* const pnt) const
97{
100 _quad_tree->getLeaf(*pnt, ll, ur);
101 return _pnt_density * (ur[0] - ll[0]);
102}
103
105 GeoLib::Point const* const pnt) const
106{
107 GeoLib::Point ll;
108 GeoLib::Point ur;
109 _quad_tree->getLeaf(*pnt, ll, ur);
110 return _station_density * (ur[0] - ll[0]);
111}
112
114 std::vector<GeoLib::Point*>& pnts, std::size_t additional_levels) const
115{
116 // get Steiner points
117 std::size_t max_depth(0);
118 _quad_tree->getMaxDepth(max_depth);
119
120 std::list<GeoLib::QuadTree<GeoLib::Point>*> leaf_list;
121 _quad_tree->getLeafs(leaf_list);
122
123 for (std::list<GeoLib::QuadTree<GeoLib::Point>*>::const_iterator it(
124 leaf_list.begin());
125 it != leaf_list.end();
126 ++it)
127 {
128 if ((*it)->getPoints().empty())
129 {
130 // compute point from square
131 GeoLib::Point ll;
132 GeoLib::Point ur;
133 (*it)->getSquarePoints(ll, ur);
134 if ((*it)->getDepth() + additional_levels > max_depth)
135 {
136 additional_levels = max_depth - (*it)->getDepth();
137 }
138 const std::size_t n_pnts_per_quad_dim = static_cast<std::size_t>(1)
139 << additional_levels;
140 const double delta((ur[0] - ll[0]) / (2 * n_pnts_per_quad_dim));
141 for (std::size_t i(0); i < n_pnts_per_quad_dim; i++)
142 {
143 for (std::size_t j(0); j < n_pnts_per_quad_dim; j++)
144 {
145 pnts.push_back(new GeoLib::Point(
146 ll[0] + (2 * i + 1) * delta,
147 ll[1] + (2 * j + 1) * delta, 0.0, pnts.size()));
148 }
149 }
150 }
151 }
152}
153
154#ifndef NDEBUG
156 GeoLib::GEOObjects& geo_objs) const
157{
158 std::list<GeoLib::QuadTree<GeoLib::Point>*> leaf_list;
159 _quad_tree->getLeafs(leaf_list);
160
161 std::string quad_tree_geo("QuadTree");
162 {
163 std::vector<GeoLib::Point*> points{};
164 for (auto const leaf : leaf_list)
165 {
166 // fetch corner points from leaf
167 GeoLib::Point ll;
168 GeoLib::Point ur;
169 leaf->getSquarePoints(ll, ur);
170 std::size_t const pnt_offset(points.size());
171 points.push_back(new GeoLib::Point(ll, pnt_offset));
172 points.push_back(
173 new GeoLib::Point(ur[0], ll[1], 0.0, pnt_offset + 1));
174 points.push_back(new GeoLib::Point(ur, pnt_offset + 2));
175 points.push_back(
176 new GeoLib::Point(ll[0], ur[1], 0.0, pnt_offset + 3));
177 }
178 geo_objs.addPointVec(std::move(points), quad_tree_geo,
180 }
181 auto& points = geo_objs.getPointVecObj(quad_tree_geo)->getVector();
182
183 std::vector<GeoLib::Polyline*> polylines{};
184 for (std::size_t l = 0; l < leaf_list.size(); ++l)
185 {
186 auto* polyline = new GeoLib::Polyline(points);
187 for (std::size_t p = 0; p < 4; ++p)
188 {
189 polyline->addPoint(4 * l + p);
190 }
191 polyline->closePolyline();
192 polylines.push_back(polyline);
193 }
194 geo_objs.addPolylineVec(std::move(polylines), quad_tree_geo,
196
197 return quad_tree_geo;
198}
199#endif
200} // namespace GMSH
201
202} // end namespace FileIO
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
double getMeshDensityAtPoint(GeoLib::Point const *const pnt) const override
double getMeshDensityAtStation(GeoLib::Point const *const) const override
std::string getQuadTreeGeometry(GeoLib::GEOObjects &geo_objs) const
void getSteinerPoints(std::vector< GeoLib::Point * > &pnts, std::size_t additional_levels=0) const
void initialize(std::vector< GeoLib::Point const * > const &pnts) override
GMSHAdaptiveMeshDensity(double pnt_density, double station_density, std::size_t max_pnts_per_leaf)
void addPoints(std::vector< GeoLib::Point const * > const &pnts)
GeoLib::QuadTree< GeoLib::Point > * _quad_tree
Container class for geometric objects.
Definition GEOObjects.h:46
void addPolylineVec(std::vector< Polyline * > &&lines, std::string const &name, PolylineVec::NameIdMap &&ply_names)
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
Class Polyline consists mainly of a reference to a point vector and a vector that stores the indices ...
Definition Polyline.h:29
std::map< std::string, std::size_t > NameIdMap
Definition TemplateVec.h:30
std::vector< T * > const & getVector() const
Definition TemplateVec.h:94