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

138 lines
4.9 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/mesh.h"
#include "gctl/utility.h"
#define create_mesh(mesh_ptr, mesh_dimen, mesh_tp) \
if (mesh_dimen == gctl::MESH_2D && mesh_tp == gctl::REGULAR_MESH) \
mesh_ptr = new gctl::regular_mesh_2d; \
else if (mesh_dimen == gctl::MESH_2D && mesh_tp == gctl::LINEAR_MESH) \
mesh_ptr = new gctl::linear_mesh_2d; \
else if (mesh_dimen == gctl::MESH_2D && mesh_tp == gctl::TRI_TET_MESH) \
mesh_ptr = new gctl::triangle_mesh; \
else if (mesh_dimen == gctl::MESH_3D && mesh_tp == gctl::REGULAR_MESH) \
mesh_ptr = new gctl::regular_mesh_3d; \
else if (mesh_dimen == gctl::MESH_3D && mesh_tp == gctl::LINEAR_MESH) \
mesh_ptr = new gctl::linear_mesh_3d; \
else if (mesh_dimen == gctl::MESH_3D && mesh_tp == gctl::TRI_TET_MESH) \
mesh_ptr = new gctl::tetrahedron_mesh; \
else if (mesh_dimen == gctl::MESH_2D && mesh_tp == gctl::REGULAR_GRID) \
mesh_ptr = new gctl::regular_grid; \
else throw "Unknown mesh format.";
int main(int argc, char *argv[]) try
{
const char* pro_name = "2minfo";
const char* pro_info = "1.0 - Inspect a GCTL .2m file and show information. \
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* in_file_str = "Name of a GCTL .2m model file.";
char in_file[1024] = "NULL";
// option format
// char* info, char* format, bool manda
static struct gctl::option_info long_opts_help[] =
{
{in_file_str, "<filename>", true},
{"Show help information.", 0, false},
{0, 0, 0}
};
static struct option long_opts[] =
{
{"input-file", required_argument, NULL, 'i'},
{"help", no_argument, NULL, 'h'},
{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, "hi:", 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 'i':
sscanf(optarg, "%s", in_file);
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(in_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('i', long_opts, long_opts_help);
return 0;
}
gctl::base_mesh *input_mesh = nullptr;
std::ifstream infile;
gctl::open_infile(infile, in_file, ".2m", std::ios::in|std::ios::binary);
// 网格头信息
int mesh_dimension, mesh_type;
infile.read((char*)&mesh_type, sizeof(int));
infile.read((char*)&mesh_dimension, sizeof(int));
infile.close();
create_mesh(input_mesh, mesh_dimension, mesh_type);
input_mesh->load_binary(in_file);
input_mesh->show_info();
if (input_mesh != nullptr) delete input_mesh;
return 0;
}
catch(std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}