gctl_toolkits/triangle2gmsh/triangle2gmsh.cpp
2024-09-10 20:25:18 +08:00

185 lines
6.7 KiB
C++

/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
// add gctl head files
#include "gctl/core.h"
#include "gctl/io.h"
#include "gctl/utility.h"
int main(int argc, char *argv[]) try
{
const char* pro_name = "triangle2gmsh";
const char* pro_info = "2.0 - Convert Triangle's files to Gmsh's .msh file. This program is a toolkit of the GCTL package. The GCTL comes with ABSOLUTE NO WARRANTY. \
Please see instructions or contact the author for more information.";
const char* triangle_file_str = "Prefix of the Triangle's mesh file. To run the program correctly, one must at least have both .node and .ele files. \
NOTE: name extensions are not needed.";
const char* gmsh_file_str = "Output name of the Gmsh's .msh file. The input name will be used if -g option is not set. \
NOTE: name extensions are not needed.";
char triangle_file[1024] = "NULL";
char gmsh_file[1024] = "NULL";
bool extract_bound = false;
bool extract_eleattri = false;
bool second_order = false;
gctl::index_packed_e file_packed = gctl::Packed;
gctl::index_packed_e output_packed = gctl::NotPacked;
// option format
// char* info, char* format, bool manda
static struct gctl::option_info long_opts_help[] =
{
{triangle_file_str, "<filename>", true},
{gmsh_file_str, "<filename>", false},
{"The starting index of the input nodes and elements are one.", 0, false},
{"Set the starting index of the output nodes and elements to zero (This may cause display error of Gmsh).", 0, false},
{"Extract node markers and save as node data.", 0, false},
{"Extract element attributes and save as element data.", 0, false},
{"Work with 2nd order elements.", 0, false},
{0, 0, 0}
};
static struct option long_opts[] =
{
{"triangle-file", required_argument, NULL, 't'},
{"gmsh-file", required_argument, NULL, 'g'},
{"not-zero", no_argument, NULL, 'n'},
{"zero-start", no_argument, NULL, 'z'},
{"node-mark", no_argument, NULL, 'b'},
{"elem-attribute", no_argument, NULL, 'e'},
{"second-order", no_argument, NULL, 's'},
{0, 0, 0, 0}
};
int curr;
while (1)
{
int opt_index = 0;
if (argc == 1)
{
gctl::display_logo(std::clog);
gctl::getopt_long_help(long_opts, long_opts_help, pro_name, pro_info);
return 0;
}
curr = getopt_long(argc, argv, "hnzbest:g:", long_opts, &opt_index);
if (curr == -1) break;
switch (curr)
{
case 'h': //显示帮助信息
gctl::display_logo(std::clog);
gctl::getopt_long_help(long_opts, long_opts_help, pro_name, pro_info);
return 0;
case 't':
sscanf(optarg, "%s", triangle_file);
break;
case 'g':
sscanf(optarg, "%s", gmsh_file);
break;
case 'n':
file_packed = gctl::NotPacked;
break;
case 'z':
output_packed = gctl::Packed;
break;
case 'b':
extract_bound = true;
break;
case 'e':
extract_eleattri = true;
break;
case 's':
second_order = true;
break;
case '?':
gctl::getopt_long_help(long_opts, long_opts_help, pro_name, " - Unknown options. Please see the help information below.");
break;
default:
abort();
}
}
// check for mandatory option(s)
if (!strcmp(triangle_file, "NULL"))
{
GCTL_ShowWhatError("Missing arguments for mandatory option(s).", GCTL_ERROR_ERROR,
0, "See tips below. Or use -h option to see the full instruction.", 0);
gctl::getopt_long_option_info('t', long_opts, long_opts_help);
return 0;
}
// declare arrays here
gctl::array<gctl::vertex2dc> tetgen_node;
gctl::array<gctl::triangle2d> triangle_ele;
gctl::array<gctl::triangle2d2o> triangle2o_ele;
gctl::array<int> bound_mark;
gctl::matrix<double> elem_attris;
if (extract_bound) gctl::read_Triangle_node(triangle_file, tetgen_node, file_packed, &bound_mark);
else gctl::read_Triangle_node(triangle_file, tetgen_node, file_packed);
if (second_order)
{
if (extract_eleattri) gctl::read_Triangle_element(triangle_file, triangle2o_ele, tetgen_node, file_packed, &elem_attris);
else gctl::read_Triangle_element(triangle_file, triangle2o_ele, tetgen_node, file_packed);
}
else
{
if (extract_eleattri) gctl::read_Triangle_element(triangle_file, triangle_ele, tetgen_node, file_packed, &elem_attris);
else gctl::read_Triangle_element(triangle_file, triangle_ele, tetgen_node, file_packed);
}
std::ofstream outfile;
if (strcmp(gmsh_file, "NULL"))
gctl::open_outfile(outfile, gmsh_file, ".msh");
else gctl::open_outfile(outfile, triangle_file, ".msh");
if (second_order) gctl::save2gmsh(outfile, triangle2o_ele, tetgen_node, output_packed);
else gctl::save2gmsh(outfile, triangle_ele, tetgen_node, output_packed);
if (extract_bound) gctl::save_gmsh_data(outfile, "Node Marker", bound_mark, gctl::NodeData, output_packed);
if (extract_eleattri)
{
gctl::array<double> tmp_attr(triangle_ele.size());
for (int j = 0; j < elem_attris.col_size(); j++)
{
for (int i = 0; i < elem_attris.row_size(); i++)
{
tmp_attr[i] = elem_attris[i][j];
}
gctl::save_gmsh_data(outfile, "Element Attribute "+std::to_string(j), tmp_attr, gctl::ElemData, output_packed);
}
}
outfile.close();
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}