gctl/lib/io/mesh_io.h

452 lines
15 KiB
C
Raw Normal View History

2024-09-10 15:45:07 +08:00
/********************************************************
*
*
*
*
*
*
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2023 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.
******************************************************/
#ifndef _GCTL_MESH_IO_H
#define _GCTL_MESH_IO_H
// library's head files
#include "../core.h"
#include "../geometry.h"
#include "../utility.h"
#include "triangle_io.h"
#include "tetgen_io.h"
#include "gmsh_io.h"
#include "map"
namespace gctl
{
/**
* @brief
*/
#define DEFAULT_INVALID_TAG -9999
/**
* @brief
*/
enum element_type_enum
{
NotSet,
_2NodeLine,
_3NodeTriangle,
_4NodeQuadrangle,
_4NodeTetrahedron,
_8NodeHexahedron,
_6NodePrism,
_5NodePyramid,
_3NodeSecondOrderLine,
_6NdoeSecondOrderLine,
_9NodeSecondOrderQuadrangle,
_10NodeSecondOrderTetrahedron,
_27NodeSecondOrderHexahedron,
_18NodeSecondOrderPrism,
_14NodeSecondOrderPyramid,
_1NodePoint,
_8NodeSecondOrderQuadrangle,
_20NdoeSecondOrderHexahedron,
_15NodeSecondOrderPrism,
_13NodeSecondOrderPyramid,
_9NodeThirdOrderIncompleteTriangle,
_10NdoeThirdOrderTriangle,
_12NodeFourthOrderIncompleteTriangle,
_15NodeFourthOrderTriangle,
_15NodeFifthOrderCompleteTriangle,
_21NodeFifthOrderCompleteTriangle,
_4NodeThirdOrderEdge,
_5NodeFourthOrderEdge,
_6NodeFifthOrderEdge,
_20NodeThirdOrderTetrahedron,
_35NodeFourthOrderTetrahedron,
_56NodeFifithOrderTetrahedron,
_64NodeThirdOrderHexahedron,
_125NodeFourthOrderHexahedron,
};
/**
* @brief
*
*/
enum element_tag_enum
{
PhysicalTag,
GeometryTag,
PartitionTag,
NodeTag,
};
/**
* @brief
*
*/
struct mesh_element
{
bool enabled;
int id;
element_type_enum type;
array<vertex3dc*> vert_ptrs;
array<mesh_element*> neigh_ptrs;
mesh_element();
};
/**
* @brief
*
*/
struct mesh_data
{
bool enabled;
mesh_data_type_e d_type;
array<std::string> str_tag;
array<double> real_tag;
array<int> int_tag;
array<vertex3dc*> vert_ptrs; // 两者只能存在一个
array<mesh_element*> elem_ptrs; // 两者只能存在一个
array<double> val;
mesh_data();
/**
* @brief
*
*/
void clear();
/**
* @brief
*
*/
bool pass_check();
};
/**
* @brief
*
*/
struct mesh_element_group
{
bool enabled;
element_type_enum type;
int phys_group;
int geom_group;
int part_group;
std::string name;
std::vector<mesh_element*> elem_ptrs;
mesh_element_group();
/**
* @brief
*
*/
void enable_elements();
/**
* @brief
*
*/
void disable_elements();
};
/**
* @brief
*
*/
class mesh_io
{
public:
mesh_io();
virtual ~mesh_io();
/**
* @brief
*
*/
void reset();
/**
* @brief
*
* @param ss clog
*/
void info(std::ostream &ss = std::clog);
/**
* @brief
*
* @param swt 使Enable或Disable
* @param dataname null
*/
void edit_data(switch_type_e swt, std::string dataname = "null");
/**
* @brief
*
* @param swt 使Enable或Disable
* @param e_type NotSet
*/
void edit_group(switch_type_e swt, element_type_enum e_type = NotSet);
/**
* @brief
*
* @param swt 使Enable或Disable
* @param grp_name
*/
void edit_group(switch_type_e swt, std::string grp_name);
/**
* @brief
*
* @param swt 使Enable或Disable
* @param tag_type PhysicalTagGeometryTag或者PartitionTag
* @param tag
*/
void edit_group(switch_type_e swt, element_tag_enum tag_type, int tag);
/**
* @brief
*
* @param anchor_type PhysicalTagGeometryTag或者PartitionTag
* @param anchor_group
* @param new_name
*/
void edit_group(element_tag_enum anchor_type, int anchor_group, std::string new_name);
/**
* @brief
*
* @param anchor_type PhysicalTagGeometryTag或者PartitionTag
* @param anchor_group
* @param tar_type PhysicalTagGeometryTag或者PartitionTag
* @param tar_group
*/
void edit_group(element_tag_enum anchor_type, int anchor_group, element_tag_enum tar_type, int tar_group);
/**
* @brief
*
* @return
*/
const array<int> &get_node_tag();
/**
* @brief
*
* @return
*/
const array<vertex3dc> &get_nodes();
/**
* @brief
*
* @return
*/
const array<mesh_element> &get_elems();
/**
* @brief
*
* @param e_type NotSet
* @return
*/
size_t element_size(element_type_enum e_type = NotSet);
/**
* @brief
*
* @param tag_type
* @param tag
* @return
*/
size_t element_size(element_tag_enum tag_type, int tag);
/**
* @brief
*
* @param phys_name
* @return
*/
size_t element_size(std::string phys_name);
/**
* @brief
*
* @param tag_type
*/
void convert_tags_to_data(element_tag_enum tag_type);
/**
* @brief
*
* @param tris
* @param phys_name
*/
void export_elements_to(array<triangle> &tris, std::string phys_name = "All");
/**
* @brief
*
* @param tris
* @param tag_type
* @param tag
*/
void export_elements_to(array<triangle> &tris, element_tag_enum tag_type, int tag);
/**
* @brief
*
* @param tets
* @param phys_name
*/
void export_elements_to(array<tetrahedron> &tets, std::string phys_name = "All");
/**
* @brief
*
* @param tris
* @param tag_type
* @param tag
*/
void export_elements_to(array<tetrahedron> &tets, element_tag_enum tag_type, int tag);
/**
* @brief
*
* @param data
* @param name
*/
void create_node_data(const array<double> &data, std::string name);
/**
* @brief
*
* @param data
* @param name
* @param e_type NotSet
*/
void create_element_data(const array<double> &data, std::string name, element_type_enum e_type = NotSet);
/**
* @brief
*
* @param data
* @param name
* @param tag_type
* @param tag
*/
void create_element_data(const array<double> &data, std::string name, element_tag_enum tag_type, int tag);
/**
* @brief
*
* @param data
* @param name
* @param phys_name
*/
void create_element_data(const array<double> &data, std::string name, std::string phys_name);
/**
* @brief triangle软件输出的网格剖分文件
*
* @param filename .node和.ele文件必须在同一路径下.neigh文件不是必须的
* @param is_packed 0
*/
void read_triangle_ascii(std::string filename, index_packed_e is_packed = Packed);
/**
* @brief tetgen软件输出的网格剖分文件
*
* @param filename .node和.ele文件必须在同一路径下.neigh文件不是必须的
* @param is_packed 0
*/
void read_tetgen_ascii(std::string filename, index_packed_e is_packed = Packed);
/**
* @brief Gmsh软件输出的网格剖分文件v2.2ASCII文件
*
* @param filename
* @param is_packed 0
*/
void read_gmsh_v2_ascii(std::string filename, index_packed_e is_packed = NotPacked);
/**
* @brief Gmsh软件格式的网格剖分文件v2.2ASCII文件
*
* @param filename
* @param is_packed 0
*/
void save_gmsh_v2_ascii(std::string filename, index_packed_e is_packed = NotPacked);
/**
* @brief Paraview软件格式的网格剖分文件
*
* @param filename
*/
void save_vtk_legacy_ascii(std::string filename);
/**
* @brief
*
* @param filename
* @param dataname
* @param out_coor Cartesian或Spherical
* @param refr out_coor为Cartesian时无效
* @param refR out_coor为Cartesian时无效
*/
void save_data_to_xyz(std::string filename, std::string dataname = "null", coordinate_system_e out_coor = Cartesian, double refr = GCTL_Earth_Radius, double refR = GCTL_Earth_Radius);
private:
bool initialized_;
size_t valid_node_size_, valid_elem_size_, valid_group_size_;
array<vertex3dc> nodes_;
array<mesh_element> elems_;
std::vector<mesh_data> datas_;
std::vector<mesh_element_group> groups_;
array<int> nodes_tag_;
element_type_enum elem_gmshcode2type_[94];
element_type_enum elem_vtkcode2type_[14];
std::map<element_type_enum, int> elem_type2gmshcode_;
std::map<element_type_enum, int> elem_type2vtkcode_;
std::map<element_type_enum, int> elem_type2size_;
std::map<element_type_enum, std::string> elem_type2name_;
std::string elem_name(element_type_enum e_type);
int elem_gmsh_code(element_type_enum e_type);
int elem_vtk_code(element_type_enum e_type);
int elem_size(element_type_enum e_type);
element_type_enum elem_gmsh_type(int code);
element_type_enum elem_vtk_type(int code);
void update_indexing();
void sort_groups();
};
}
#endif // _GCTL_MESH_IO_H