OGS
swapNodeCoordinateAxes.cpp
Go to the documentation of this file.
1
10#include <tclap/CmdLine.h>
11
12#include <array>
13#include <memory>
14#include <string>
15
16#include "BaseLib/Logging.h"
17#include "BaseLib/MPI.h"
18#include "InfoLib/GitInfo.h"
21#include "MeshLib/Mesh.h"
22#include "MeshLib/Node.h"
23
24static void swapNodeCoordinateAxes(MeshLib::Mesh const& mesh,
25 std::array<int, 3> const& new_axes_indices)
26{
27 double new_coords[3] = {};
28 for (MeshLib::Node* node : mesh.getNodes())
29 {
30 for (int i = 0; i < 3; i++)
31 {
32 new_coords[i] = (*node)[new_axes_indices[i]];
33 }
34 for (int i = 0; i < 3; i++)
35 {
36 (*node)[i] = new_coords[i];
37 }
38 }
39}
40
41static bool parseNewOrder(std::string const& str_order,
42 std::array<int, 3>& new_axes_indices)
43{
44 if (str_order.length() != 3)
45 {
46 ERR("Invalid argument for the new order. The argument should contain "
47 "three characters.");
48 return false;
49 }
50
51 new_axes_indices.fill(-1);
52
53 for (int i = 0; i < 3; i++)
54 {
55 if (str_order[i] == 'x')
56 {
57 new_axes_indices[i] = 0;
58 }
59 else if (str_order[i] == 'y')
60 {
61 new_axes_indices[i] = 1;
62 }
63 else if (str_order[i] == 'z')
64 {
65 new_axes_indices[i] = 2;
66 }
67 else
68 {
69 ERR("Invalid argument for the new order. The given argument "
70 "contains a character other than 'x', 'y', 'z'.");
71 return false;
72 }
73 }
74
75 bool isAxisSet[3] = {false};
76 for (int new_axes_indice : new_axes_indices)
77 {
78 if (isAxisSet[new_axes_indice])
79 {
80 ERR("Invalid argument for the new order. The argument contains "
81 "some character used more than once.");
82 return false;
83 }
84 isAxisSet[new_axes_indice] = true;
85 }
86
87 return true;
88}
89
90int main(int argc, char* argv[])
91{
92 TCLAP::CmdLine cmd(
93 "Swap node coordinate values.\n\n"
94 "OpenGeoSys-6 software, version " +
96 ".\n"
97 "Copyright (c) 2012-2025, OpenGeoSys Community "
98 "(http://www.opengeosys.org)",
100 TCLAP::ValueArg<std::string> input_arg("i", "input-mesh-file",
101 "Input (.vtu) mesh file", true, "",
102 "INPUT_FILE");
103 cmd.add(input_arg);
104 TCLAP::ValueArg<std::string> output_arg("o", "output-mesh-file",
105 "Output (.vtu) mesh file", true, "",
106 "OUTPUT_FILE");
107 cmd.add(output_arg);
108 TCLAP::ValueArg<std::string> new_order_arg(
109 "n", "new-order",
110 "the new order of swapped coordinate values (e.g. 'xzy' for converting "
111 "XYZ values to XZY values)",
112 true, "", "NEW_ORDER");
113 cmd.add(new_order_arg);
114 cmd.parse(argc, argv);
115
116 BaseLib::MPI::Setup mpi_setup(argc, argv);
117
118 const std::string str_order = new_order_arg.getValue();
119 std::array<int, 3> new_order = {{}};
120 if (!parseNewOrder(str_order, new_order))
121 {
122 return EXIT_FAILURE;
123 }
124
125 std::unique_ptr<MeshLib::Mesh> mesh(
126 MeshLib::IO::readMeshFromFile(input_arg.getValue()));
127 if (!mesh)
128 {
129 return EXIT_FAILURE;
130 }
131
132 if (mesh->getDimension() == 3)
133 {
134 WARN(
135 "Swapping coordinate values of 3D elements can result in incorrect "
136 "node-ordering.");
137 }
138
139 INFO("Exchange node coordinates from xyz to {:s}",
140 new_order_arg.getValue().data());
141 swapNodeCoordinateAxes(*mesh, new_order);
142
143 INFO("Save the new mesh into a file");
144 MeshLib::IO::writeMeshToFile(*mesh, output_arg.getValue());
145
146 return EXIT_SUCCESS;
147}
Git information.
void INFO(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:36
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:48
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:42
Definition of the Mesh class.
Definition of the Node class.
std::vector< Node * > const & getNodes() const
Get the nodes-vector for the mesh.
Definition Mesh.h:108
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name, bool const compute_element_neighbors)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, std::set< std::string > variable_output_names)
Definition of readMeshFromFile function.
int main(int argc, char *argv[])
static bool parseNewOrder(std::string const &str_order, std::array< int, 3 > &new_axes_indices)
static void swapNodeCoordinateAxes(MeshLib::Mesh const &mesh, std::array< int, 3 > const &new_axes_indices)