OGS 6.1.0-1721-g6382411ad
MeshLayerMapper.cpp
Go to the documentation of this file.
1 
15 #include "MeshLayerMapper.h"
16 
17 #include <algorithm>
18 
19 #include <logog/include/logog.hpp>
20 
21 #include "GeoLib/Raster.h"
22 
23 #include "MathLib/MathTools.h"
24 
25 #include "MeshLib/Elements/Tet.h"
26 #include "MeshLib/Elements/Hex.h"
28 #include "MeshLib/Elements/Prism.h"
30 #include "MeshLib/Properties.h"
31 
32 namespace MeshLib
33 {
34 
35 MeshLib::Mesh* MeshLayerMapper::createStaticLayers(MeshLib::Mesh const& mesh, std::vector<float> const& layer_thickness_vector, std::string const& mesh_name)
36 {
37  std::vector<float> thickness;
38  for (std::size_t i=0; i<layer_thickness_vector.size(); ++i)
39  if (layer_thickness_vector[i] > std::numeric_limits<float>::epsilon())
40  thickness.push_back(layer_thickness_vector[i]);
41  else
42  WARN ("Ignoring layer %d with thickness %f.", i, layer_thickness_vector[i]);
43 
44  const std::size_t nLayers(thickness.size());
45  if (nLayers < 1 || mesh.getDimension() != 2)
46  {
47  ERR("MeshLayerMapper::createStaticLayers(): A 2D mesh with nLayers > 0 is required as input.");
48  return nullptr;
49  }
50 
51  const std::size_t nNodes = mesh.getNumberOfNodes();
52  // count number of 2d elements in the original mesh
53  const std::size_t nElems (std::count_if(mesh.getElements().begin(), mesh.getElements().end(),
54  [](MeshLib::Element const* elem) { return (elem->getDimension() == 2);}));
55 
56  const std::size_t nOrgElems (mesh.getNumberOfElements());
57  const std::vector<MeshLib::Node*> &nodes = mesh.getNodes();
58  const std::vector<MeshLib::Element*> &elems = mesh.getElements();
59  std::vector<MeshLib::Node*> new_nodes(nNodes + (nLayers * nNodes));
60  std::vector<MeshLib::Element*> new_elems;
61  new_elems.reserve(nElems * nLayers);
62  MeshLib::Properties properties;
63  auto* const materials = properties.createNewPropertyVector<int>(
64  "MaterialIDs", MeshLib::MeshItemType::Cell);
65  if (!materials)
66  {
67  ERR("Could not create PropertyVector object 'MaterialIDs'.");
68  return nullptr;
69  }
70 
71  materials->reserve(nElems * nLayers);
72  double z_offset (0.0);
73 
74  for (unsigned layer_id = 0; layer_id <= nLayers; ++layer_id)
75  {
76  // add nodes for new layer
77  unsigned node_offset (nNodes * layer_id);
78  if (layer_id > 0) z_offset += thickness[layer_id-1];
79 
80  std::transform(nodes.cbegin(), nodes.cend(), new_nodes.begin() + node_offset,
81  [&z_offset](MeshLib::Node* node){ return new MeshLib::Node((*node)[0], (*node)[1], (*node)[2]-z_offset); });
82 
83  // starting with 2nd layer create prism or hex elements connecting the last layer with the current one
84  if (layer_id == 0)
85  continue;
86 
87  node_offset -= nNodes;
88  const unsigned mat_id (nLayers - layer_id);
89 
90  for (unsigned i = 0; i < nOrgElems; ++i)
91  {
92  const MeshLib::Element* sfc_elem( elems[i] );
93  if (sfc_elem->getDimension() < 2) // ignore line-elements
94  continue;
95 
96  const unsigned nElemNodes(sfc_elem->getNumberOfBaseNodes());
97  auto** e_nodes = new MeshLib::Node*[2 * nElemNodes];
98 
99  for (unsigned j=0; j<nElemNodes; ++j)
100  {
101  const unsigned node_id = sfc_elem->getNode(j)->getID() + node_offset;
102  e_nodes[j] = new_nodes[node_id+nNodes];
103  e_nodes[j+nElemNodes] = new_nodes[node_id];
104  }
105  if (sfc_elem->getGeomType() == MeshLib::MeshElemType::TRIANGLE)
106  {
107  // extrude triangles to prism
108  new_elems.push_back(new MeshLib::Prism(e_nodes));
109  }
110  else if (sfc_elem->getGeomType() == MeshLib::MeshElemType::QUAD)
111  {
112  // extrude quads to hexes
113  new_elems.push_back(new MeshLib::Hex(e_nodes));
114  }
115  else
116  {
117  OGS_FATAL("MeshLayerMapper: Unknown element type to extrude.");
118  }
119  materials->push_back(mat_id);
120  }
121  }
122  return new MeshLib::Mesh(mesh_name, new_nodes, new_elems, properties);
123 }
124 
126  MeshLib::Mesh const& mesh,
127  std::vector<GeoLib::Raster const*> const& rasters,
128  double minimum_thickness,
129  double noDataReplacementValue)
130 {
131  const std::size_t nLayers(rasters.size());
132  if (nLayers < 2 || mesh.getDimension() != 2)
133  {
134  ERR("MeshLayerMapper::createRasterLayers(): A 2D mesh and at least two rasters required as input.");
135  return false;
136  }
137 
138  auto top = std::make_unique<MeshLib::Mesh>(mesh);
139  if (!layerMapping(*top, *rasters.back(), noDataReplacementValue))
140  return false;
141 
142  auto bottom = std::make_unique<MeshLib::Mesh>(mesh);
143  if (!layerMapping(*bottom, *rasters[0], 0))
144  {
145  return false;
146  }
147 
148  this->_minimum_thickness = minimum_thickness;
149  std::size_t const nNodes = mesh.getNumberOfNodes();
150  _nodes.reserve(nLayers * nNodes);
151 
152  // number of triangles in the original mesh
153  std::size_t const nElems (std::count_if(mesh.getElements().begin(), mesh.getElements().end(),
154  [](MeshLib::Element const* elem)
155  { return (elem->getGeomType() == MeshLib::MeshElemType::TRIANGLE);}));
156  _elements.reserve(nElems * (nLayers-1));
157  _materials.reserve(nElems * (nLayers-1));
158 
159  // add bottom layer
160  std::vector<MeshLib::Node*> const& nodes = bottom->getNodes();
161  for (MeshLib::Node* node : nodes)
162  _nodes.push_back(new MeshLib::Node(*node));
163 
164  // add the other layers
165  for (std::size_t i=0; i<nLayers-1; ++i)
166  addLayerToMesh(*top, i, *rasters[i+1]);
167 
168  return true;
169 }
170 
171 void MeshLayerMapper::addLayerToMesh(const MeshLib::Mesh &dem_mesh, unsigned layer_id, GeoLib::Raster const& raster)
172 {
173  const unsigned pyramid_base[3][4] =
174  {
175  {1, 3, 4, 2}, // Point 4 missing
176  {2, 4, 3, 0}, // Point 5 missing
177  {0, 3, 4, 1}, // Point 6 missing
178  };
179 
180  std::size_t const nNodes = dem_mesh.getNumberOfNodes();
181  std::vector<MeshLib::Node*> const& nodes = dem_mesh.getNodes();
182  int const last_layer_node_offset = layer_id * nNodes;
183 
184  // add nodes for new layer
185  for (std::size_t i=0; i<nNodes; ++i)
186  _nodes.push_back(getNewLayerNode(*nodes[i], *_nodes[last_layer_node_offset + i], raster, _nodes.size()));
187 
188  std::vector<MeshLib::Element*> const& elems = dem_mesh.getElements();
189  std::size_t const nElems (dem_mesh.getNumberOfElements());
190 
191  for (std::size_t i=0; i<nElems; ++i)
192  {
193  MeshLib::Element* elem (elems[i]);
195  continue;
196  unsigned node_counter(3), missing_idx(0);
197  std::array<MeshLib::Node*, 6> new_elem_nodes;
198  for (unsigned j=0; j<3; ++j)
199  {
200  new_elem_nodes[j] = _nodes[_nodes[last_layer_node_offset + elem->getNodeIndex(j)]->getID()];
201  new_elem_nodes[node_counter] = (_nodes[last_layer_node_offset + elem->getNodeIndex(j) + nNodes]);
202  if (new_elem_nodes[j]->getID() != new_elem_nodes[node_counter]->getID())
203  node_counter++;
204  else
205  missing_idx = j;
206  }
207 
208  switch (node_counter)
209  {
210  case 6:
211  _elements.push_back(new MeshLib::Prism(new_elem_nodes));
212  _materials.push_back(layer_id);
213  break;
214  case 5:
215  std::array<MeshLib::Node*, 5> pyramid_nodes;
216  pyramid_nodes[0] = new_elem_nodes[pyramid_base[missing_idx][0]];
217  pyramid_nodes[1] = new_elem_nodes[pyramid_base[missing_idx][1]];
218  pyramid_nodes[2] = new_elem_nodes[pyramid_base[missing_idx][2]];
219  pyramid_nodes[3] = new_elem_nodes[pyramid_base[missing_idx][3]];
220  pyramid_nodes[4] = new_elem_nodes[missing_idx];
221  _elements.push_back(new MeshLib::Pyramid(pyramid_nodes));
222  _materials.push_back(layer_id);
223  break;
224  case 4:
225  std::array<MeshLib::Node*, 4> tet_nodes;
226  std::copy(new_elem_nodes.begin(), new_elem_nodes.begin() + node_counter, tet_nodes.begin());
227  _elements.push_back(new MeshLib::Tet(tet_nodes));
228  _materials.push_back(layer_id);
229  break;
230  default:
231  continue;
232  }
233  }
234 }
235 
236 bool MeshLayerMapper::layerMapping(MeshLib::Mesh &new_mesh, GeoLib::Raster const& raster, double noDataReplacementValue = 0.0)
237 {
238  if (new_mesh.getDimension() != 2)
239  {
240  ERR("MshLayerMapper::layerMapping() - requires 2D mesh");
241  return false;
242  }
243 
244  GeoLib::RasterHeader const& header (raster.getHeader());
245  const double x0(header.origin[0]);
246  const double y0(header.origin[1]);
247  const double delta(header.cell_size);
248 
249  const std::pair<double, double> xDim(x0, x0 + header.n_cols * delta); // extension in x-dimension
250  const std::pair<double, double> yDim(y0, y0 + header.n_rows * delta); // extension in y-dimension
251 
252  const std::size_t nNodes (new_mesh.getNumberOfNodes());
253  const std::vector<MeshLib::Node*> &nodes = new_mesh.getNodes();
254  for (unsigned i = 0; i < nNodes; ++i)
255  {
256  if (!raster.isPntOnRaster(*nodes[i]))
257  {
258  // use either default value or elevation from layer above
259  nodes[i]->updateCoordinates((*nodes[i])[0], (*nodes[i])[1], noDataReplacementValue);
260  continue;
261  }
262 
263  double elevation (raster.interpolateValueAtPoint(*nodes[i]));
264  if (std::abs(elevation - header.no_data) < std::numeric_limits<double>::epsilon())
265  elevation = noDataReplacementValue;
266  nodes[i]->updateCoordinates((*nodes[i])[0], (*nodes[i])[1], elevation);
267  }
268 
269  return true;
270 }
271 
273 {
274  if (mesh.getDimension() != 2)
275  {
276  ERR("MshLayerMapper::mapToStaticValue() - requires 2D mesh");
277  return false;
278  }
279 
280  std::vector<MeshLib::Node*> const& nodes (mesh.getNodes());
281  for (MeshLib::Node* node : nodes)
282  node->updateCoordinates((*node)[0], (*node)[1], value);
283  return true;
284 }
285 
286 } // end namespace MeshLib
virtual unsigned getNumberOfBaseNodes() const =0
std::size_t getNumberOfNodes() const
Get the number of nodes.
Definition: Mesh.h:99
RasterHeader const & getHeader() const
Returns the complete header information.
Definition: Raster.h:69
static bool mapToStaticValue(MeshLib::Mesh &mesh, double value)
Maps the elevation of all mesh nodes to the specified static value.
Class Raster is used for managing raster data.
Definition: Raster.h:42
Contains the relevant information when storing a geoscientific raster data.
Definition: Raster.h:24
MeshLib::Node * getNewLayerNode(MeshLib::Node const &dem_node, MeshLib::Node const &last_layer_node, GeoLib::Raster const &raster, std::size_t new_node_id) const
PropertyVector< T > * createNewPropertyVector(std::string const &name, MeshItemType mesh_item_type, std::size_t n_components=1)
Definition: Properties.h:15
Definition of the Tet class.
bool createRasterLayers(MeshLib::Mesh const &mesh, std::vector< GeoLib::Raster const *> const &rasters, double minimum_thickness, double noDataReplacementValue=0.0) override
void addLayerToMesh(const MeshLib::Mesh &mesh_layer, unsigned layer_id, GeoLib::Raster const &raster) override
Adds another layer to a subsurface mesh.
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition: Mesh.h:105
double interpolateValueAtPoint(const MathLib::Point3d &pnt) const
Interpolates the elevation of the given point based on the 8-neighbourhood of the raster cell it is l...
Definition: Raster.cpp:118
virtual unsigned getDimension() const =0
Get dimension of the mesh element.
Definition of the GeoLib::Raster class.
std::vector< MeshLib::Element * > _elements
static bool layerMapping(MeshLib::Mesh &mesh, const GeoLib::Raster &raster, double noDataReplacementValue)
std::size_t getID() const
Definition: Point3dWithID.h:63
bool isPntOnRaster(MathLib::Point3d const &node) const
Checks if the given point is located within the (x,y)-extension of the raster.
Definition: Raster.cpp:177
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties...
Definition: Properties.h:37
Definition of the MeshSurfaceExtraction class.
std::vector< MeshLib::Node * > _nodes
Definition of the Pyramid class.
Definition of the MeshLayerMapper class.
std::size_t getNodeIndex(unsigned i) const
Definition: Element.cpp:164
Interface for heuristic search length strategy.
Definition: ProjectData.h:28
static MeshLib::Mesh * createStaticLayers(MeshLib::Mesh const &mesh, std::vector< float > const &layer_thickness_vector, std::string const &mesh_name="SubsurfaceMesh")
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:108
Definition of the class Properties that implements a container of properties.
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition: Mesh.h:81
std::size_t getNumberOfElements() const
Get the number of elements.
Definition: Mesh.h:96
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
std::vector< int > _materials
virtual MeshElemType getGeomType() const =0
void copy(MatrixOrVector const &x, MatrixOrVector &y)
Copies x to y.
Definition: LinAlg.h:36
Definition of the Prism class.
Definition of the Hex class.
const Node * getNode(unsigned i) const
Definition: Element.cpp:144