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