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