OGS
VoxelGridFromLayeredMeshes.cpp
Go to the documentation of this file.
1
11
12#include "MeshLib/Mesh.h"
17
18static std::string mat_name = "MaterialIDs";
19
20// returns the AABB of all mesh nodes of layers read so far
21void adjustExtent(std::pair<MathLib::Point3d, MathLib::Point3d>& extent,
22 MeshLib::Mesh const& mesh)
23{
24 auto const& nodes = mesh.getNodes();
25 GeoLib::AABB aabb(nodes.cbegin(), nodes.cend());
26 for (std::size_t i = 0; i < 3; ++i)
27 {
28 extent.first[i] = std::min(extent.first[i], aabb.getMinPoint()[i]);
29 extent.second[i] = std::max(extent.second[i], aabb.getMaxPoint()[i]);
30 }
31}
32
33// creates a voxel grid of the AABB of all layers
34std::unique_ptr<MeshLib::Mesh> generateInitialMesh(
35 std::pair<MathLib::Point3d, MathLib::Point3d>& extent,
36 std::array<double, 3> const& res)
37{
38 INFO("Creating initial mesh...");
39 std::array<double, 3> mesh_range{{extent.second[0] - extent.first[0],
40 extent.second[1] - extent.first[1],
41 extent.second[2] - extent.first[2]}};
42 std::array<std::size_t, 3> const n_cells{
43 {static_cast<std::size_t>(std::ceil(mesh_range[0] / res[0])),
44 static_cast<std::size_t>(std::ceil(mesh_range[1] / res[1])),
45 static_cast<std::size_t>(std::ceil(mesh_range[2] / res[2]))}};
46 for (std::size_t i = 0; i < 3; ++i)
47 {
48 double const ext_range = n_cells[i] * res[i];
49 double const offset = (ext_range - mesh_range[i]) / 2.0;
50 mesh_range[i] = ext_range;
51 extent.first[i] -= offset;
52 extent.second[i] += offset;
53 }
54 std::unique_ptr<MeshLib::Mesh> mesh(
56 mesh_range[0], mesh_range[1], mesh_range[2], n_cells[0], n_cells[1],
57 n_cells[2], extent.first));
58 auto mat_id = mesh->getProperties().createNewPropertyVector<int>(
60 if (!mat_id)
61 {
62 return nullptr;
63 }
64 mat_id->insert(mat_id->end(), mesh->getNumberOfElements(), -1);
65 return mesh;
66}
67
68// returns the element the given node is projected on (or nullptr otherwise)
70 MeshLib::MeshElementGrid const& grid,
71 MathLib::Point3d const& node,
72 double const max_edge)
73{
74 constexpr double max_val = std::numeric_limits<double>::max();
75 MathLib::Point3d const min_vol{
76 {node[0] - max_edge, node[1] - max_edge, -max_val}};
77 MathLib::Point3d const max_vol{
78 {node[0] + max_edge, node[1] + max_edge, max_val}};
79 auto const& intersection_candidates =
80 grid.getElementsInVolume(min_vol, max_vol);
82 intersection_candidates, node);
83}
84
85// casts vote if the given nodes belongs to lower layer, upper layer or no layer
86// at all
87void voteMatId(MathLib::Point3d const& node,
88 MeshLib::MeshElementGrid const& grid,
89 double const max_edge,
90 std::size_t& nullptr_cnt,
91 std::size_t& upper_layer_cnt,
92 std::size_t& lower_layer_cnt)
93{
94 auto const& proj_elem = getProjectedElement(grid, node, max_edge);
95 if (proj_elem == nullptr)
96 {
97 nullptr_cnt++;
98 return;
99 }
100 if (node[2] >
102 {
103 upper_layer_cnt++;
104 return;
105 }
106 lower_layer_cnt++;
107}
108
109// sets material IDs for all elements depending on the layers they are located
110// between
112 std::vector<MeshLib::Mesh const*> const& layers,
113 bool const dilate)
114{
115 INFO("Setting material properties...");
116 std::size_t const n_layers = layers.size();
117 auto const& elems = mesh.getElements();
118 std::size_t const n_elems = mesh.getNumberOfElements();
119 auto mat_ids = mesh.getProperties().getPropertyVector<int>(mat_name);
120 std::vector<bool> is_set(n_elems, false);
121 for (int i = n_layers - 1; i >= 0; --i)
122 {
123 INFO("-> Layer {:d}", n_layers - i - 1);
124 MeshLib::MeshElementGrid const grid(*layers[i]);
125 auto const edgeLengths = minMaxEdgeLength(layers[i]->getElements());
126 double const max_edge = edgeLengths.second;
127 for (std::size_t j = 0; j < n_elems; ++j)
128 {
129 if (is_set[j])
130 {
131 continue;
132 }
133
134 std::size_t nullptr_cnt(0);
135 std::size_t upper_layer_cnt(0);
136 std::size_t lower_layer_cnt(0);
137
138 auto const& node = MeshLib::getCenterOfGravity(*elems[j]);
139 voteMatId(node, grid, max_edge, nullptr_cnt, upper_layer_cnt,
140 lower_layer_cnt);
141 if (nullptr_cnt)
142 {
143 // if no element was found at centre point, vote via corners
144 for (std::size_t k = 0; k < 8; ++k)
145 {
146 MeshLib::Node const& n = *elems[j]->getNode(k);
147 voteMatId(n, grid, max_edge, nullptr_cnt, upper_layer_cnt,
148 lower_layer_cnt);
149 }
150
151 // If the "dilate"-param is set, a mat ID will be assigned if at
152 // least one node was voting for a specific layer. Without the
153 // "dilate"-param, an absolute majority is needed. In case of a
154 // tie, the lower layer will be favoured.
155 if ((upper_layer_cnt == 0 && lower_layer_cnt == 0) ||
156 (!dilate && nullptr_cnt >= upper_layer_cnt &&
157 nullptr_cnt >= lower_layer_cnt))
158 {
159 continue;
160 }
161 if (upper_layer_cnt > lower_layer_cnt)
162 {
163 (*mat_ids)[j] = n_layers - i - 1;
164 }
165 else
166 {
167 is_set[j] = true;
168 }
169 continue;
170 }
171 if (upper_layer_cnt)
172 {
173 (*mat_ids)[j] = n_layers - i - 1;
174 }
175 else
176 {
177 is_set[j] = true;
178 }
179 }
180 }
181 // set all elements above uppermost layer back to -1 so they are
182 // subsequently cut
183 std::replace(mat_ids->begin(), mat_ids->end(),
184 static_cast<int>(n_layers - 1), -1);
185}
186
187// Removes all elements from mesh that have not been marked as being located
188// between two layers. If all elements remain unmarked, a nullptr is returned.
189std::vector<std::size_t> markSpecificElements(MeshLib::Mesh const& mesh,
190 int const mat_id)
191{
192 std::vector<std::size_t> marked_elems;
193 auto const mat_ids = *MeshLib::materialIDs(mesh);
194 std::size_t const n_elems = mat_ids.size();
195 for (std::size_t i = 0; i < n_elems; ++i)
196 {
197 if (mat_ids[i] == mat_id)
198 {
199 marked_elems.push_back(i);
200 }
201 }
202 return marked_elems;
203}
204
205// Creates a VoxelGrid after extending the AABB for each layer.
206std::unique_ptr<MeshLib::Mesh> MeshToolsLib::MeshGenerators::
208 std::pair<MathLib::Point3d, MathLib::Point3d>& extent,
209 std::vector<MeshLib::Mesh const*> const& layers,
210 std::array<double, 3> const cellsize,
211 bool const dilate)
212{
213 for (auto const& layer : layers)
214 {
215 adjustExtent(extent, *layer);
216 }
217
218 std::unique_ptr<MeshLib::Mesh> mesh(generateInitialMesh(extent, cellsize));
219 if (mesh == nullptr)
220 {
221 return nullptr;
222 }
223 setMaterialIDs(*mesh, layers, dilate);
224 auto const marked_elements = markSpecificElements(*mesh, -1);
225 if (marked_elements.size() == mesh->getNumberOfElements())
226 {
227 return nullptr;
228 }
229 std::unique_ptr<MeshLib::Mesh> new_mesh(
230 MeshToolsLib::removeElements(*mesh, marked_elements, "mesh"));
231 return new_mesh;
232}
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:35
Definition of the Mesh class.
static std::string mat_name
std::vector< std::size_t > markSpecificElements(MeshLib::Mesh const &mesh, int const mat_id)
void voteMatId(MathLib::Point3d const &node, MeshLib::MeshElementGrid const &grid, double const max_edge, std::size_t &nullptr_cnt, std::size_t &upper_layer_cnt, std::size_t &lower_layer_cnt)
MeshLib::Element const * getProjectedElement(MeshLib::MeshElementGrid const &grid, MathLib::Point3d const &node, double const max_edge)
void setMaterialIDs(MeshLib::Mesh &mesh, std::vector< MeshLib::Mesh const * > const &layers, bool const dilate)
std::unique_ptr< MeshLib::Mesh > generateInitialMesh(std::pair< MathLib::Point3d, MathLib::Point3d > &extent, std::array< double, 3 > const &res)
void adjustExtent(std::pair< MathLib::Point3d, MathLib::Point3d > &extent, MeshLib::Mesh const &mesh)
Class AABB is an axis aligned bounding box around a given set of geometric points of (template) type ...
Definition AABB.h:56
Eigen::Vector3d const & getMaxPoint() const
Definition AABB.h:187
Eigen::Vector3d const & getMinPoint() const
Definition AABB.h:180
std::vector< MeshLib::Element const * > getElementsInVolume(POINT const &min, POINT const &max) const
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
Properties & getProperties()
Definition Mesh.h:134
std::size_t getNumberOfElements() const
Get the number of elements.
Definition Mesh.h:97
PropertyVector< T > const * getPropertyVector(std::string_view name) const
PropertyVector< int > const * materialIDs(Mesh const &mesh)
Definition Mesh.cpp:268
MathLib::Point3d getCenterOfGravity(Element const &element)
Calculates the center of gravity for the mesh element.
Definition Element.cpp:124
MeshLib::Mesh * generateRegularHexMesh(const BaseLib::ISubdivision &div_x, const BaseLib::ISubdivision &div_y, const BaseLib::ISubdivision &div_z, MathLib::Point3d const &origin=MathLib::ORIGIN, std::string const &mesh_name="mesh")
std::unique_ptr< MeshLib::Mesh > createVoxelFromLayeredMesh(std::pair< MathLib::Point3d, MathLib::Point3d > &extent, std::vector< MeshLib::Mesh const * > const &layers, std::array< double, 3 > const cellsize, bool const dilate)
double getElevation(MeshLib::Element const &element, MathLib::Point3d const &node)
MeshLib::Element const * getProjectedElement(std::vector< const MeshLib::Element * > const &elements, MathLib::Point3d const &node)
MeshLib::Mesh * removeElements(const MeshLib::Mesh &mesh, const std::vector< std::size_t > &removed_element_ids, const std::string &new_mesh_name)