24int main(
int argc,
char* argv[])
27 "Reads a 3D unstructured mesh and samples it onto a structured grid of "
28 "the same extent. Cell properties are mapped onto the grid (sampled at "
29 "the centre-points of each cube), node properties are ignored. Note, "
30 "that a large cube size may result in an undersampling of the original "
31 "mesh structure.\nCube sizes are defines by x/y/z-parameters. For "
32 "equilateral cubes, only the x-parameter needs to be set.\n\n"
33 "OpenGeoSys-6 software, version " +
36 "Copyright (c) 2012-2024, OpenGeoSys Community "
37 "(http://www.opengeosys.org)",
40 TCLAP::ValueArg<double> z_arg(
"z",
"cellsize-z",
41 "edge length of cubes in z-direction (depth)",
42 false, 1000,
"floating point number");
45 TCLAP::ValueArg<double> y_arg(
46 "y",
"cellsize-y",
"edge length of cubes in y-direction (latitude)",
47 false, 1000,
"floating point number");
50 TCLAP::ValueArg<double> x_arg(
52 "edge length of cubes in x-direction (longitude) or all directions, if "
53 "y and z are not set",
54 true, 1000,
"floating point number");
57 TCLAP::ValueArg<std::string> output_arg(
58 "o",
"output",
"the output grid (*.vtu)",
true,
"",
"output.vtu");
61 TCLAP::ValueArg<std::string> input_arg(
"i",
"input",
62 "the 3D input mesh (*.vtu, *.msh)",
63 true,
"",
"input.vtu");
65 cmd.parse(argc, argv);
68 MPI_Init(&argc, &argv);
71 if ((y_arg.isSet() && !z_arg.isSet()) ||
72 ((!y_arg.isSet() && z_arg.isSet())))
74 ERR(
"For equilateral cubes, only x needs to be set. For unequal "
75 "cuboids, all three edge lengths (x/y/z) need to be specified.");
83 double const x_size = x_arg.getValue();
84 double const y_size = (y_arg.isSet()) ? y_arg.getValue() : x_arg.getValue();
85 double const z_size = (z_arg.isSet()) ? z_arg.getValue() : x_arg.getValue();
87 if (x_size <= 0 || y_size <= 0 || z_size <= 0)
89 ERR(
"A cellsize ({},{},{}) is not allowed to be <= 0", x_size, y_size,
97 std::array<double, 3>
const cellsize = {x_size, y_size, z_size};
99 vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
100 vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
101 reader->SetFileName(input_arg.getValue().c_str());
103 vtkSmartPointer<vtkUnstructuredGrid> mesh = reader->GetOutput();
105 double*
const bounds = mesh->GetBounds();
107 std::array<double, 3>{bounds[0], bounds[2], bounds[4]});
109 std::array<double, 3>{bounds[1], bounds[3], bounds[5]});
110 std::array<double, 3> ranges = {max[0] - min[0], max[1] - min[1],
112 if (ranges[0] < 0 || ranges[1] < 0 || ranges[2] < 0)
114 ERR(
"The range ({},{},{}) is not allowed to be < 0", ranges[0],
115 ranges[1], ranges[2]);
121 std::array<std::size_t, 3>
const dims =
122 VoxelGridFromMesh::getNumberOfVoxelPerDimension(ranges, cellsize);
123 std::unique_ptr<MeshLib::Mesh> grid(
125 dims[0], dims[1], dims[2], cellsize[0], cellsize[1], cellsize[2],
128 std::vector<int>
const tmp_ids =
129 VoxelGridFromMesh::assignCellIds(mesh, min, dims, cellsize);
130 std::vector<int>& cell_ids =
131 *grid->getProperties().createNewPropertyVector<
int>(
133 std::copy(tmp_ids.cbegin(), tmp_ids.cend(), std::back_inserter(cell_ids));
135 if (!VoxelGridFromMesh::removeUnusedGridCells(mesh, grid))
143 VoxelGridFromMesh::mapMeshArraysOntoGrid(mesh, grid);