OGS
ResetMeshElementProperty.h
Go to the documentation of this file.
1 /*
2  * \file
3  * \copyright
4  * Copyright (c) 2012-2021, OpenGeoSys Community (http://www.opengeosys.org)
5  * Distributed under a Modified BSD License.
6  * See accompanying file LICENSE.txt or
7  * http://www.opengeosys.org/project/license
8  *
9  */
10 
11 #pragma once
12 
13 #include <algorithm>
14 #include <cstdlib>
15 #include <vector>
16 
17 #include "GeoLib/GEOObjects.h"
18 #include "GeoLib/Polygon.h"
23 #include "MeshLib/Mesh.h"
24 #include "MeshLib/Node.h"
25 
26 namespace MeshGeoToolsLib
27 {
28 template <typename PT>
30  GeoLib::Polygon const& polygon,
31  std::string const& property_name,
32  PT new_property_value,
33  int restrict_to_material_id,
34  bool const any_of)
35 {
36  auto* const pv = MeshLib::getOrCreateMeshProperty<PT>(
37  mesh, property_name, MeshLib::MeshItemType::Cell, 1);
38 
39  if (pv->getMeshItemType() != MeshLib::MeshItemType::Cell)
40  {
41  ERR("Values of the PropertyVector are not assigned to cells.");
42  return;
43  }
44 
45  auto const outside = markNodesOutSideOfPolygon(mesh.getNodes(), polygon);
46 
47  auto is_node_outside = [&outside](auto const* node_ptr)
48  { return outside[node_ptr->getID()]; };
49 
50  auto const* material_ids =
51  mesh.getProperties().getPropertyVector<int>("MaterialIDs");
52 
53  if (restrict_to_material_id != -1 && !material_ids)
54  {
55  OGS_FATAL(
56  "Restriction of resetting a property in a polygonal region "
57  "requires that a MaterialIDs data array is available in the "
58  "mesh.");
59  }
60 
61  auto has_element_required_material_id = [&](int const element_id)
62  {
63  return restrict_to_material_id == -1 ||
64  (*material_ids)[element_id] == restrict_to_material_id;
65  };
66 
67  using func =
68  std::function<bool(MeshLib::Node* const*, MeshLib::Node* const*,
69  decltype(is_node_outside))>;
70  auto is_element_outside =
71  any_of ? (func)(std::all_of<MeshLib::Node* const*,
72  decltype(is_node_outside)>)
73  : (func)(std::any_of<MeshLib::Node* const*,
74  decltype(is_node_outside)>);
75 
76  for (std::size_t j(0); j < mesh.getElements().size(); ++j)
77  {
78  MeshLib::Element const* const elem(mesh.getElements()[j]);
79  if (is_element_outside(elem->getNodes(),
80  elem->getNodes() + elem->getNumberOfNodes(),
81  is_node_outside))
82  {
83  continue;
84  }
85  if (has_element_required_material_id(elem->getID()))
86  {
87  (*pv)[j] = new_property_value;
88  }
89  }
90 }
91 
92 } // namespace MeshGeoToolsLib
Definition of the Element class.
#define OGS_FATAL(...)
Definition: Error.h:26
Definition of the GEOObjects class.
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
Definition of the Mesh class.
Definition of the Node class.
Definition of the Polygon class.
virtual Node *const * getNodes() const =0
Get array of element nodes.
virtual unsigned getNumberOfNodes() const =0
virtual std::size_t getID() const final
Returns the ID of the element.
Definition: Element.h:82
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition: Mesh.h:95
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:98
Properties & getProperties()
Definition: Mesh.h:123
PropertyVector< T > const * getPropertyVector(std::string const &name) const
std::vector< bool > markNodesOutSideOfPolygon(std::vector< MeshLib::Node * > const &nodes, GeoLib::Polygon const &polygon)
void resetMeshElementProperty(MeshLib::Mesh &mesh, GeoLib::Polygon const &polygon, std::string const &property_name, PT new_property_value, int restrict_to_material_id, bool const any_of)
Definition of readMeshFromFile function.