OGS
ResetMeshElementProperty.h
Go to the documentation of this file.
1/*
2 * \file
3 * \copyright
4 * Copyright (c) 2012-2024, 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"
26
27namespace MeshGeoToolsLib
28{
29template <typename PT>
31 GeoLib::Polygon const& polygon,
32 std::string const& property_name,
33 PT new_property_value,
34 int restrict_to_material_id,
35 bool const any_of)
36{
38 mesh, property_name, MeshLib::MeshItemType::Cell, 1);
39
40 if (pv->getMeshItemType() != MeshLib::MeshItemType::Cell)
41 {
42 ERR("Values of the PropertyVector are not assigned to cells.");
43 return;
44 }
45
46 auto const outside = markNodesOutSideOfPolygon(mesh.getNodes(), polygon);
47
48 auto is_node_outside = [&outside](auto const* node_ptr)
49 { return outside[node_ptr->getID()]; };
50
51 auto const* material_ids =
52 mesh.getProperties().getPropertyVector<int>("MaterialIDs");
53
54 if (restrict_to_material_id != -1 && !material_ids)
55 {
57 "Restriction of resetting a property in a polygonal region "
58 "requires that a MaterialIDs data array is available in the "
59 "mesh.");
60 }
61
62 auto has_element_required_material_id = [&](int const element_id)
63 {
64 return restrict_to_material_id == -1 ||
65 (*material_ids)[element_id] == restrict_to_material_id;
66 };
67
68 using func =
69 std::function<bool(MeshLib::Node* const*, MeshLib::Node* const*,
70 decltype(is_node_outside))>;
71 auto is_element_outside =
72 any_of ? (func)(std::all_of<MeshLib::Node* const*,
73 decltype(is_node_outside)>)
74 : (func)(std::any_of<MeshLib::Node* const*,
75 decltype(is_node_outside)>);
76
77 for (std::size_t j(0); j < mesh.getElements().size(); ++j)
78 {
79 MeshLib::Element const* const elem(mesh.getElements()[j]);
80 if (is_element_outside(elem->getNodes(),
81 elem->getNodes() + elem->getNumberOfNodes(),
82 is_node_outside))
83 {
84 continue;
85 }
86 if (has_element_required_material_id(elem->getID()))
87 {
88 (*pv)[j] = new_property_value;
89 }
90 }
91}
92
93} // namespace MeshGeoToolsLib
Definition of the Element class.
#define OGS_FATAL(...)
Definition Error.h:26
Definition of the GEOObjects class.
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:45
Definition of the Mesh class.
Definition of the Node class.
Definition of the Polygon class.
virtual unsigned getNumberOfNodes() const =0
virtual Node *const * getNodes() const =0
Get array of element nodes.
std::size_t getID() const
Returns the ID of the element.
Definition Element.h:89
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
PropertyVector< T > const * getPropertyVector(std::string_view 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)
PropertyVector< T > * getOrCreateMeshProperty(Mesh &mesh, std::string const &property_name, MeshItemType const item_type, int const number_of_components)
Definition of readMeshFromFile function.