OGS
swapNodeCoordinateAxes.cpp File Reference

Detailed Description

Definition in file swapNodeCoordinateAxes.cpp.

#include <tclap/CmdLine.h>
#include <array>
#include <memory>
#include <string>
#include "BaseLib/Logging.h"
#include "InfoLib/GitInfo.h"
#include "MeshLib/IO/readMeshFromFile.h"
#include "MeshLib/IO/writeMeshToFile.h"
#include "MeshLib/Mesh.h"
#include "MeshLib/Node.h"
Include dependency graph for swapNodeCoordinateAxes.cpp:

Go to the source code of this file.

Functions

static void swapNodeCoordinateAxes (MeshLib::Mesh const &mesh, std::array< int, 3 > const &new_axes_indices)
 
static bool parseNewOrder (std::string const &str_order, std::array< int, 3 > &new_axes_indices)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 89 of file swapNodeCoordinateAxes.cpp.

90 {
91  TCLAP::CmdLine cmd(
92  "Swap node coordinate values.\n\n"
93  "OpenGeoSys-6 software, version " +
95  ".\n"
96  "Copyright (c) 2012-2021, OpenGeoSys Community "
97  "(http://www.opengeosys.org)",
99  TCLAP::ValueArg<std::string> input_arg(
100  "i", "input-mesh-file", "input mesh file", true, "", "string");
101  cmd.add(input_arg);
102  TCLAP::ValueArg<std::string> output_arg(
103  "o", "output-mesh-file", "output mesh file", true, "", "string");
104  cmd.add(output_arg);
105  TCLAP::ValueArg<std::string> new_order_arg(
106  "n", "new-order",
107  "the new order of swapped coordinate values (e.g. 'xzy' for converting "
108  "XYZ values to XZY values)",
109  true, "", "string");
110  cmd.add(new_order_arg);
111  cmd.parse(argc, argv);
112 
113  const std::string str_order = new_order_arg.getValue();
114  std::array<int, 3> new_order = {{}};
115  if (!parseNewOrder(str_order, new_order))
116  {
117  return EXIT_FAILURE;
118  }
119 
120  std::unique_ptr<MeshLib::Mesh> mesh(
121  MeshLib::IO::readMeshFromFile(input_arg.getValue()));
122  if (!mesh)
123  {
124  return EXIT_FAILURE;
125  }
126 
127  if (mesh->getDimension() == 3)
128  {
129  WARN(
130  "Swapping coordinate values of 3D elements can result in incorrect "
131  "node-ordering.");
132  }
133 
134  INFO("Exchange node coordinates from xyz to {:s}",
135  new_order_arg.getValue().data());
136  swapNodeCoordinateAxes(*mesh, new_order);
137 
138  INFO("Save the new mesh into a file");
139  MeshLib::IO::writeMeshToFile(*mesh, output_arg.getValue());
140 
141  return EXIT_SUCCESS;
142 }
void INFO(char const *fmt, Args const &... args)
Definition: Logging.h:32
void WARN(char const *fmt, Args const &... args)
Definition: Logging.h:37
GITINFOLIB_EXPORT const std::string ogs_version
MeshLib::Mesh * readMeshFromFile(const std::string &file_name)
int writeMeshToFile(const MeshLib::Mesh &mesh, std::filesystem::path const &file_path, [[maybe_unused]] std::set< std::string > variable_output_names)
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)

References INFO(), GitInfoLib::GitInfo::ogs_version, parseNewOrder(), MeshLib::IO::readMeshFromFile(), swapNodeCoordinateAxes(), WARN(), and MeshLib::IO::writeMeshToFile().

◆ parseNewOrder()

static bool parseNewOrder ( std::string const &  str_order,
std::array< int, 3 > &  new_axes_indices 
)
static

Definition at line 40 of file swapNodeCoordinateAxes.cpp.

42 {
43  if (str_order.length() != 3)
44  {
45  ERR("Invalid argument for the new order. The argument should contain "
46  "three characters.");
47  return false;
48  }
49 
50  new_axes_indices.fill(-1);
51 
52  for (int i = 0; i < 3; i++)
53  {
54  if (str_order[i] == 'x')
55  {
56  new_axes_indices[i] = 0;
57  }
58  else if (str_order[i] == 'y')
59  {
60  new_axes_indices[i] = 1;
61  }
62  else if (str_order[i] == 'z')
63  {
64  new_axes_indices[i] = 2;
65  }
66  else
67  {
68  ERR("Invalid argument for the new order. The given argument "
69  "contains a character other than 'x', 'y', 'z'.");
70  return false;
71  }
72  }
73 
74  bool isAxisSet[3] = {false};
75  for (int new_axes_indice : new_axes_indices)
76  {
77  if (isAxisSet[new_axes_indice])
78  {
79  ERR("Invalid argument for the new order. The argument contains "
80  "some character used more than once.");
81  return false;
82  }
83  isAxisSet[new_axes_indice] = true;
84  }
85 
86  return true;
87 }
void ERR(char const *fmt, Args const &... args)
Definition: Logging.h:42

References ERR().

Referenced by main().

◆ swapNodeCoordinateAxes()

static void swapNodeCoordinateAxes ( MeshLib::Mesh const &  mesh,
std::array< int, 3 > const &  new_axes_indices 
)
static

Definition at line 23 of file swapNodeCoordinateAxes.cpp.

25 {
26  double new_coords[3] = {};
27  for (MeshLib::Node* node : mesh.getNodes())
28  {
29  for (int i = 0; i < 3; i++)
30  {
31  new_coords[i] = (*node)[new_axes_indices[i]];
32  }
33  for (int i = 0; i < 3; i++)
34  {
35  (*node)[i] = new_coords[i];
36  }
37  }
38 }

References MeshLib::Mesh::getNodes().

Referenced by main().