OGS
RasterDataToMesh.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "RasterDataToMesh.h"
5
8#include "MeshLib/Node.h"
9
10namespace MeshToolsLib
11{
13{
14static bool checkMesh(MeshLib::Mesh const& mesh)
15{
16 if (mesh.getDimension() > 2)
17 {
18 ERR("This functionality is currently only available for 2D meshes.");
19 return false;
20 }
21 return true;
22}
23
24static double evaluatePixel(double const value, double const no_data,
25 double const replacement)
26{
27 if (std::abs(value - no_data) < std::numeric_limits<double>::epsilon())
28 {
29 return replacement;
30 }
31 return value;
32}
33
35 double const default_replacement,
36 std::string const& array_name)
37{
38 if (!checkMesh(mesh))
39 {
40 return false;
41 }
42
43 auto& nodes = mesh.getNodes();
44 auto& props = mesh.getProperties();
45 std::string const name =
46 BaseLib::getUniqueName(props.getPropertyVectorNames(), array_name);
47 auto vec = props.createNewPropertyVector<double>(
49 double const no_data = raster.getHeader().no_data;
50 std::transform(nodes.cbegin(), nodes.cend(), std::back_inserter(*vec),
51 [&](auto const node)
52 {
53 return evaluatePixel(raster.getValueAtPoint(*node),
54 no_data, default_replacement);
55 });
56 return true;
57}
58
60 double const default_replacement,
61 std::string const& array_name)
62{
63 if (!checkMesh(mesh))
64 {
65 return false;
66 }
67
68 auto& elems = mesh.getElements();
69 auto& props = mesh.getProperties();
70 std::string const name =
71 BaseLib::getUniqueName(props.getPropertyVectorNames(), array_name);
72 auto vec = props.createNewPropertyVector<double>(
74 double const no_data = raster.getHeader().no_data;
75 std::transform(elems.cbegin(), elems.cend(), std::back_inserter(*vec),
76 [&](auto const elem)
77 {
78 auto node = getCenterOfGravity(*elem);
79 return evaluatePixel(raster.getValueAtPoint(node),
80 no_data, default_replacement);
81 });
82 return true;
83}
84
85} // end namespace RasterDataToMesh
86} // namespace MeshToolsLib
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
Class Raster is used for managing raster data.
Definition Raster.h:39
RasterHeader const & getHeader() const
Returns the complete header information.
Definition Raster.h:75
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:97
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition Mesh.h:100
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition Mesh.h:79
Properties & getProperties()
Definition Mesh.h:125
std::string getUniqueName(std::vector< std::string > const &existing_names, std::string const &input_name)
Append '-' and a number such that the name is unique.
Adding pixel values from a raster onto nodes or cells of a mesh.
static double evaluatePixel(double const value, double const no_data, double const replacement)
static bool checkMesh(MeshLib::Mesh const &mesh)
bool projectToNodes(MeshLib::Mesh &mesh, GeoLib::Raster const &raster, double const default_replacement, std::string const &array_name)
bool projectToElements(MeshLib::Mesh &mesh, GeoLib::Raster const &raster, double const default_replacement, std::string const &array_name)