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