OGS
convertVtkDataArrayToVtkDataArray.cpp
Go to the documentation of this file.
1 
11 #include <tclap/CmdLine.h>
12 
13 #include <algorithm>
14 #include <cmath>
15 #include <memory>
16 #include <numeric>
17 
18 #include "InfoLib/GitInfo.h"
21 #include "MeshLib/Mesh.h"
22 
23 template <typename T1, typename T2>
24 std::pair<bool, std::string> castPropertyVectorToPropertyVector(
25  MeshLib::Properties& properties,
26  std::string const& property_vector_name_in,
27  std::string const& property_vector_name_out)
28 {
29  auto const* const orig_pv = properties.getPropertyVector<T1>(
30  property_vector_name_in, MeshLib::MeshItemType::Cell, 1);
31  if (!orig_pv)
32  {
33  return std::make_pair(false,
34  "Original property vector '" +
35  property_vector_name_in + "' not found.");
36  }
37  auto* new_pv = properties.createNewPropertyVector<T2>(
38  property_vector_name_out, MeshLib::MeshItemType::Cell, 1);
39  if (!new_pv)
40  {
41  return std::make_pair(false,
42  "Could not create new property vector '" +
43  property_vector_name_in + "' not found.");
44  }
45  new_pv->resize(orig_pv->getNumberOfTuples());
46  for (std::size_t i(0); i < new_pv->getNumberOfTuples(); ++i)
47  {
48  (*new_pv)[i] = static_cast<T2>((*orig_pv)[i]);
49  }
50  return std::make_pair(true, "");
51 }
52 
53 int main(int argc, char* argv[])
54 {
55  TCLAP::CmdLine cmd(
56  "Converts a double or floating point cell data array of a vtk "
57  "unstructured grid into a int or double cell data array.\n\n"
58  "OpenGeoSys-6 software, version " +
60  ".\n"
61  "Copyright (c) 2012-2021, OpenGeoSys Community "
62  "(http://www.opengeosys.org)",
64 
65  TCLAP::ValueArg<std::string> new_property_data_type_arg(
66  "t",
67  "new-property-data-type",
68  "the name of the data type as string (int or double)",
69  false,
70  "int",
71  "data type as string");
72  cmd.add(new_property_data_type_arg);
73 
74  TCLAP::ValueArg<std::string> new_property_arg(
75  "n",
76  "new-property-name",
77  "the name of the new cell data array (PropertyVector) the values are "
78  "stored",
79  false,
80  "MaterialIDs",
81  "name of the new cell data array (PropertyVector) as string");
82  cmd.add(new_property_arg);
83 
84  TCLAP::ValueArg<std::string> out_mesh_arg(
85  "o", "out-mesh", "output mesh file name", true, "", "file name");
86  cmd.add(out_mesh_arg);
87 
88  TCLAP::ValueArg<std::string> property_arg(
89  "e",
90  "existing-property-name",
91  "the name of the existing cell data array (PropertyVector)",
92  true,
93  "",
94  "property name as string");
95  cmd.add(property_arg);
96 
97  TCLAP::ValueArg<std::string> mesh_arg(
98  "i", "in-mesh", "input mesh file name", true, "", "file name");
99  cmd.add(mesh_arg);
100 
101  cmd.parse(argc, argv);
102 
103  std::unique_ptr<MeshLib::Mesh> mesh(
104  MeshLib::IO::readMeshFromFile(mesh_arg.getValue()));
105 
106  if (!mesh)
107  {
108  return -1;
109  }
110 
111  bool success = false;
112  std::string err_msg = "Could not find cell data array '" +
113  property_arg.getValue() + "' in the mesh '" +
114  mesh_arg.getValue() + "'";
115 
116  if (new_property_data_type_arg.getValue() == "int")
117  {
118  if (mesh->getProperties().existsPropertyVector<double>(
119  property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
120  {
121  std::tie(success, err_msg) =
122  castPropertyVectorToPropertyVector<double, int>(
123  mesh->getProperties(),
124  property_arg.getValue(),
125  new_property_arg.getValue());
126  }
127 
128  if (mesh->getProperties().existsPropertyVector<float>(
129  property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
130  {
131  std::tie(success, err_msg) =
132  castPropertyVectorToPropertyVector<float, int>(
133  mesh->getProperties(),
134  property_arg.getValue(),
135  new_property_arg.getValue());
136  }
137  }
138  if (new_property_data_type_arg.getValue() == "double")
139  {
140  if (mesh->getProperties().existsPropertyVector<float>(
141  property_arg.getValue(), MeshLib::MeshItemType::Cell, 1))
142  {
143  std::tie(success, err_msg) =
144  castPropertyVectorToPropertyVector<float, double>(
145  mesh->getProperties(),
146  property_arg.getValue(),
147  new_property_arg.getValue());
148  }
149  }
150 
151  if (!success)
152  {
153  ERR("{:s}", err_msg);
154  return -1;
155  }
156 
157  MeshLib::IO::writeMeshToFile(*mesh, out_mesh_arg.getValue());
158 
159  return EXIT_SUCCESS;
160 }
Git information.
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42
Definition of the Mesh class.
Property manager on mesh items. Class Properties manages scalar, vector or matrix properties....
Definition: Properties.h:36
PropertyVector< T > const * getPropertyVector(std::string const &name) const
PropertyVector< T > * createNewPropertyVector(std::string const &name, MeshItemType mesh_item_type, std::size_t n_components=1)
int main(int argc, char *argv[])
std::pair< bool, std::string > castPropertyVectorToPropertyVector(MeshLib::Properties &properties, std::string const &property_vector_name_in, std::string const &property_vector_name_out)
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, [[maybe_unused]] std::set< std::string > variable_output_names)
Definition of readMeshFromFile function.