OGS
RasterDataToMesh.cpp
Go to the documentation of this file.
1 
11 #include "RasterDataToMesh.h"
12 
13 #include "BaseLib/StringTools.h"
15 #include "MeshLib/Node.h"
16 
17 namespace MeshLib
18 {
19 namespace RasterDataToMesh
20 {
21 static bool checkMesh(MeshLib::Mesh const& mesh)
22 {
23  if (mesh.getDimension() > 2)
24  {
25  ERR("This functionality is currently only available for 2D meshes.");
26  return false;
27  }
28  return true;
29 }
30 
31 static double evaluatePixel(double const value, double const no_data,
32  double const replacement)
33 {
34  if (std::abs(value - no_data) < std::numeric_limits<double>::epsilon())
35  {
36  return replacement;
37  }
38  return value;
39 }
40 
41 bool projectToNodes(MeshLib::Mesh& mesh, GeoLib::Raster const& raster,
42  double const default_replacement,
43  std::string const& array_name)
44 {
45  if (!checkMesh(mesh))
46  {
47  return false;
48  }
49 
50  auto& nodes = mesh.getNodes();
51  auto& props = mesh.getProperties();
52  std::string const name =
53  BaseLib::getUniqueName(props.getPropertyVectorNames(), array_name);
54  auto vec = props.createNewPropertyVector<double>(
56  double const no_data = raster.getHeader().no_data;
57  std::transform(nodes.cbegin(), nodes.cend(), std::back_inserter(*vec),
58  [&](auto const node)
59  {
60  return evaluatePixel(raster.getValueAtPoint(*node),
61  no_data, default_replacement);
62  });
63  return true;
64 }
65 
67  double const default_replacement,
68  std::string const& array_name)
69 {
70  if (!checkMesh(mesh))
71  {
72  return false;
73  }
74 
75  auto& elems = mesh.getElements();
76  auto& props = mesh.getProperties();
77  std::string const name =
78  BaseLib::getUniqueName(props.getPropertyVectorNames(), array_name);
79  auto vec = props.createNewPropertyVector<double>(
81  double const no_data = raster.getHeader().no_data;
82  std::transform(elems.cbegin(), elems.cend(), std::back_inserter(*vec),
83  [&](auto const elem)
84  {
85  auto node = getCenterOfGravity(*elem);
86  return evaluatePixel(raster.getValueAtPoint(node),
87  no_data, default_replacement);
88  });
89  return true;
90 }
91 
92 } // end namespace RasterDataToMesh
93 } // end namespace MeshLib
Definition of the Element class.
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
Definition of the Node class.
Definition of string helper functions.
Class Raster is used for managing raster data.
Definition: Raster.h:42
RasterHeader const & getHeader() const
Returns the complete header information.
Definition: Raster.h:70
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition: Mesh.h:95
unsigned getDimension() const
Returns the dimension of the mesh (determined by the maximum dimension over all elements).
Definition: Mesh.h:71
std::vector< Element * > const & getElements() const
Get the element-vector for the mesh.
Definition: Mesh.h:98
Properties & getProperties()
Definition: Mesh.h:123
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.
static bool checkMesh(MeshLib::Mesh const &mesh)
bool projectToElements(MeshLib::Mesh &mesh, GeoLib::Raster const &raster, double const default_replacement, std::string const &array_name)
static double evaluatePixel(double const value, double const no_data, double const replacement)
bool projectToNodes(MeshLib::Mesh &mesh, GeoLib::Raster const &raster, double const default_replacement, std::string const &array_name)