OGS
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
ResetMeshElementProperty.h
Go to the documentation of this file.
1/*
2 * \file
3 * \copyright
4 * Copyright (c) 2012-2025, 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 <range/v3/algorithm/all_of.hpp>
16#include <range/v3/algorithm/any_of.hpp>
17#include <range/v3/view/transform.hpp>
18#include <vector>
19
20#include "GeoLib/GEOObjects.h"
21#include "GeoLib/Polygon.h"
26#include "MeshLib/Mesh.h"
27#include "MeshLib/Node.h"
29
30namespace MeshGeoToolsLib
31{
32template <typename PT>
34 GeoLib::Polygon const& polygon,
35 std::string const& property_name,
36 PT new_property_value,
37 int restrict_to_material_id,
38 bool const any_of)
39{
41 mesh, property_name, MeshLib::MeshItemType::Cell, 1);
42
43 if (pv->getMeshItemType() != MeshLib::MeshItemType::Cell)
44 {
45 ERR("Values of the PropertyVector are not assigned to cells.");
46 return;
47 }
48
49 auto const* material_ids =
50 mesh.getProperties().getPropertyVector<int>("MaterialIDs");
51
52 if (restrict_to_material_id != -1 && !material_ids)
53 {
55 "Restriction of resetting a property in a polygonal region "
56 "requires that a MaterialIDs data array is available in the "
57 "mesh.");
58 }
59
60 auto has_element_required_material_id = [&](int const element_id)
61 {
62 return restrict_to_material_id == -1 ||
63 (*material_ids)[element_id] == restrict_to_material_id;
64 };
65
66 auto const outside = markNodesOutSideOfPolygon(mesh.getNodes(), polygon);
67
68 auto is_node_outside = [&outside](std::size_t const node_id)
69 { return outside[node_id]; };
70
71 auto is_element_outside = [&](MeshLib::Element const& e)
72 {
73 auto ids = e.nodes() | MeshLib::views::ids;
74 return any_of ? ranges::all_of(ids, is_node_outside)
75 : ranges::any_of(ids, is_node_outside);
76 };
77
78 auto is_valid_element = [&](MeshLib::Element const& e)
79 {
80 return !is_element_outside(e) &&
81 has_element_required_material_id(e.getID());
82 };
83
84 auto compute_value = [&](MeshLib::Element const* const e) -> PT
85 { return is_valid_element(*e) ? new_property_value : (*pv)[e->getID()]; };
86
87 auto values = mesh.getElements() | ranges::views::transform(compute_value);
88
89 pv->assign(values.begin(), values.end());
90}
91
92} // 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.
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:108
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:111
Properties & getProperties()
Definition Mesh.h:136
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)
constexpr ranges::views::view_closure ids
For an element of a range view return its id.
Definition Mesh.h:227
PropertyVector< T > * getOrCreateMeshProperty(Mesh &mesh, std::string const &property_name, MeshItemType const item_type, int const number_of_components)
Definition of readMeshFromFile function.