19{
20 TCLAP::CmdLine cmd(
21 "Reads a 3D unstructured mesh and samples it onto a structured grid of "
22 "the same extent. Cell properties are mapped onto the grid (sampled at "
23 "the centre-points of each cube), node properties are ignored. Note, "
24 "that a large cube size may result in an undersampling of the original "
25 "mesh structure.\nCube sizes are defines by x/y/z-parameters. For "
26 "equilateral cubes, only the x-parameter needs to be set.\n\n"
27 "OpenGeoSys-6 software, version " +
29 ".\n"
30 "Copyright (c) 2012-2026, OpenGeoSys Community "
31 "(http://www.opengeosys.org)",
33
34 TCLAP::ValueArg<double> z_arg("z", "cellsize-z",
35 "edge length of cubes in z-direction "
36 "(depth), (min = 0)",
37 false, 1000, "CELLSIZE_Z");
38 cmd.add(z_arg);
39
40 TCLAP::ValueArg<double> y_arg("y", "cellsize-y",
41 "edge length of cubes in y-direction "
42 "(latitude), (min = 0)",
43 false, 1000, "CELLSIZE_Y");
44 cmd.add(y_arg);
45
46 TCLAP::ValueArg<double> x_arg(
47 "x", "cellsize-x",
48 "edge length of cubes in x-direction (longitude) or all directions, if "
49 "y and z are not set, (min = 0)",
50 true, 1000, "CELLSIZE_X");
51 cmd.add(x_arg);
52
53 TCLAP::ValueArg<std::string> output_arg(
54 "o", "output", "Output (.vtu). The output grid file", true, "",
55 "OUTPUT_FILE");
56 cmd.add(output_arg);
57
58 TCLAP::ValueArg<std::string> input_arg(
59 "i", "input", "Input (.vtu | .msh). The 3D input mesh file", true, "",
60 "INPUT_FILE");
61 cmd.add(input_arg);
63 cmd.add(log_level_arg);
64 cmd.parse(argc, argv);
65
68
69 if ((y_arg.isSet() && !z_arg.isSet()) ||
70 ((!y_arg.isSet() && z_arg.isSet())))
71 {
72 ERR(
"For equilateral cubes, only x needs to be set. For unequal "
73 "cuboids, all three edge lengths (x/y/z) need to be specified.");
74 return -1;
75 }
77
78 double const x_size = x_arg.getValue();
79 double const y_size = (y_arg.isSet()) ? y_arg.getValue() : x_arg.getValue();
80 double const z_size = (z_arg.isSet()) ? z_arg.getValue() : x_arg.getValue();
81
82 if (x_size <= 0 || y_size <= 0 || z_size <= 0)
83 {
84 ERR(
"A cellsize ({},{},{}) is not allowed to be <= 0", x_size, y_size,
85 z_size);
86 return -1;
87 }
88
89 std::array<double, 3> const cellsize = {x_size, y_size, z_size};
90
91 vtkSmartPointer<vtkUnstructuredGrid> mesh =
93 input_arg.getValue());
94 if (mesh == nullptr)
95 {
96 return EXIT_FAILURE;
97 }
98
99 double* const bounds = mesh->GetBounds();
101 std::array<double, 3>{bounds[0], bounds[2], bounds[4]});
103 std::array<double, 3>{bounds[1], bounds[3], bounds[5]});
104 std::array<double, 3> ranges = {max[0] - min[0], max[1] - min[1],
105 max[2] - min[2]};
106 if (ranges[0] < 0 || ranges[1] < 0 || ranges[2] < 0)
107 {
108 ERR(
"The range ({},{},{}) is not allowed to be < 0", ranges[0],
109 ranges[1], ranges[2]);
110 return -1;
111 }
112 std::array<std::size_t, 3> const dims =
114 std::unique_ptr<MeshLib::Mesh> grid(
116 dims[0], dims[1], dims[2], cellsize[0], cellsize[1], cellsize[2],
117 min, "grid"));
118
119 std::vector<int> const tmp_ids =
121 auto* const cell_ids = grid->getProperties().createNewPropertyVector<int>(
123 assert(cell_ids);
124 cell_ids->assign(tmp_ids);
125
127 {
128 return EXIT_FAILURE;
129 }
130
132
134 {
135 return EXIT_FAILURE;
136 }
137
138 return EXIT_SUCCESS;
139}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
static vtkSmartPointer< vtkUnstructuredGrid > readVtuFileToVtkUnstructuredGrid(std::string const &file_name)
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
GITINFOLIB_EXPORT const std::string ogs_version
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > variable_output_names)