gctl/lib/io/mesh_io.h

526 lines
20 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
{
2025-01-21 10:33:52 +08:00
PhysicalTag, // 元素的物理分组标签
GeometryTag, // 元素的几何分组标签
PartitionTag, // 元素的剖分分组标签
NodeTag, // 顶点的标签(仅用于输出顶点标签数据)
2024-09-10 15:45:07 +08:00
};
/**
* @brief
*
*/
2025-01-21 17:21:19 +08:00
struct meshio_element
2024-09-10 15:45:07 +08:00
{
2025-01-21 10:33:52 +08:00
bool enabled; // 单元体是否有效
int id; // 单元体编号
element_type_enum type; // 单元体类型
array<vertex3dc*> vert_ptrs; // 顶点指针数组
2025-01-21 17:21:19 +08:00
array<meshio_element*> neigh_ptrs; // 相邻单元体指针数组
2024-09-10 15:45:07 +08:00
2025-01-21 17:21:19 +08:00
meshio_element();
2024-09-10 15:45:07 +08:00
};
/**
* @brief
*
*/
2025-01-21 17:21:19 +08:00
struct meshio_data
2024-09-10 15:45:07 +08:00
{
2025-01-21 10:33:52 +08:00
bool enabled; // 数据体是否有效
mesh_data_type_e d_type; // 数据类型
array<std::string> str_tag; // 字符串类型的标签(默认为一个,即为数据的名称)
array<double> real_tag; // 实数类型的标签默认为一个等于0.0
array<int> int_tag; // 整数类型的标签(最少三个,最后一个为数据的长度)
2025-01-23 10:07:17 +08:00
array<void*> tar_ptrs; // 数据连接的对象指针数组 具体类型为vertex3dc*或meshio_element*
array<double> val; // 数据值(目前仅支持标量数据,后续再添加对矢量数据的支持)
2024-09-10 15:45:07 +08:00
2025-01-21 17:21:19 +08:00
meshio_data();
2024-09-10 15:45:07 +08:00
/**
* @brief
*
*/
void clear();
/**
* @brief
*
*/
bool pass_check();
};
/**
* @brief
*
*/
2025-01-21 17:21:19 +08:00
struct meshio_element_group
2024-09-10 15:45:07 +08:00
{
2025-01-21 10:33:52 +08:00
bool enabled; // 组是否有效
element_type_enum type; // 组内单元体类型
int phys_group; // 物理分组标签
int geom_group; // 几何分组标签
int part_group; // 剖分分组标签
std::string name; // 组名
2025-01-21 17:21:19 +08:00
std::vector<meshio_element*> elem_ptrs; // 组内单元体指针数组
2024-09-10 15:45:07 +08:00
2025-01-21 17:21:19 +08:00
meshio_element_group();
2024-09-10 15:45:07 +08:00
/**
* @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();
2025-01-21 17:21:19 +08:00
/**
* @brief
*
* @param anchor_type PhysicalTagGeometryTag或者PartitionTag
* @param anchor_name
* @return
*/
int get_tag(element_tag_enum anchor_type, std::string anchor_name);
2024-09-10 15:45:07 +08:00
/**
2025-01-21 10:33:52 +08:00
* @brief
2024-09-10 15:45:07 +08:00
*
* @return
*/
const array<vertex3dc> &get_nodes();
/**
2025-01-21 10:33:52 +08:00
* @brief
2024-09-10 15:45:07 +08:00
*
* @return
*/
2025-01-21 17:21:19 +08:00
const array<meshio_element> &get_elems();
2024-09-10 15:45:07 +08:00
/**
2025-01-21 17:21:19 +08:00
* @brief
2024-09-10 15:45:07 +08:00
*
* @param e_type NotSet
* @return
*/
size_t element_size(element_type_enum e_type = NotSet);
/**
2025-01-21 17:21:19 +08:00
* @brief
2024-09-10 15:45:07 +08:00
*
* @param tag_type
* @param tag
* @return
*/
size_t element_size(element_tag_enum tag_type, int tag);
/**
2025-01-21 17:21:19 +08:00
* @brief
2024-09-10 15:45:07 +08:00
*
* @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);
2025-01-21 17:21:19 +08:00
/**
* @brief gmsh格式分组表
*
* @param g_groups gmsh格式表
*/
void get_gmsh_physical_groups(std::vector<gmsh_physical_group> &g_groups);
2024-09-10 15:45:07 +08:00
/**
2025-01-21 10:33:52 +08:00
* @brief name的数据
2024-09-10 15:45:07 +08:00
*
2025-01-21 10:33:52 +08:00
* @param name
2025-01-21 17:21:19 +08:00
* @param type
2025-01-21 10:33:52 +08:00
*
* @return -1
*/
2025-01-21 17:21:19 +08:00
int if_saved_data(std::string name, mesh_data_type_e type);
/**
* @brief
*
* @param name
* @param type
* @return
*/
meshio_data &get_data(std::string name, mesh_data_type_e type);
/**
* @brief
*
* @param name
* @param type
* @return
*/
meshio_data *get_data_ptr(std::string name, mesh_data_type_e type);
2025-01-21 10:33:52 +08:00
/**
* @brief
*
* @note
*
* @param data
* @param name
*/
2025-01-21 17:21:19 +08:00
void add_node_data(std::string name, const array<double> &data);
2025-01-21 10:33:52 +08:00
/**
* @brief
*
* @note
*
* @param data
* @param boolen
2024-09-10 15:45:07 +08:00
* @param name
*/
2025-01-21 17:21:19 +08:00
void add_node_data(std::string name, const array<double> &data, const array<bool> &boolen);
2024-09-10 15:45:07 +08:00
/**
2025-01-21 10:33:52 +08:00
* @brief
*
* @note
2024-09-10 15:45:07 +08:00
*
* @param data
* @param name
* @param e_type NotSet
*/
2025-01-21 17:21:19 +08:00
void add_element_data(std::string name, const array<double> &data, element_type_enum e_type = NotSet);
2024-09-10 15:45:07 +08:00
/**
* @brief
*
2025-01-21 10:33:52 +08:00
* @note
*
2024-09-10 15:45:07 +08:00
* @param data
* @param name
* @param tag_type
* @param tag
*/
2025-01-21 17:21:19 +08:00
void add_element_data(std::string name, const array<double> &data, element_tag_enum tag_type, int tag);
2024-09-10 15:45:07 +08:00
/**
* @brief
*
2025-01-21 10:33:52 +08:00
* @note
*
2024-09-10 15:45:07 +08:00
* @param data
* @param name
* @param phys_name
*/
2025-01-21 17:21:19 +08:00
void add_element_data(std::string name, std::string phys_name, const array<double> &data);
/**
* @brief
*
* @note
*
* @param phys_val
* @param name
* @param phys_name
*/
void add_element_data(std::string name, std::string phys_name, double phys_val);
2024-09-10 15:45:07 +08:00
/**
* @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);
2025-01-21 17:21:19 +08:00
protected:
2025-01-21 10:33:52 +08:00
bool initialized_; // 类型是否已经初始化完成
// 有效的顶点、单元体和单元体组的数量
2024-09-10 15:45:07 +08:00
size_t valid_node_size_, valid_elem_size_, valid_group_size_;
2025-01-21 10:33:52 +08:00
array<vertex3dc> nodes_; // 网格顶点 当顶点索引为无效值时将不会被输出
2025-01-21 17:21:19 +08:00
array<meshio_element> elems_; // 网格元素
std::vector<meshio_data> datas_; // 网格数据
std::vector<meshio_element_group> groups_; // 网格单元体组
2025-01-21 10:33:52 +08:00
array<int> nodes_tag_; // 顶点标签
2024-09-10 15:45:07 +08:00
2025-01-21 10:33:52 +08:00
element_type_enum elem_gmshcode2type_[94]; // gmsh的单元体类型数组 数组索引为gmsh的单元体类型码值
element_type_enum elem_vtkcode2type_[14]; // vtk的单元体类型数组 数组索引为vtk的单元体类型码值
std::map<element_type_enum, int> elem_type2gmshcode_; // 单元体类型到gmsh类型码值的映射
std::map<element_type_enum, int> elem_type2vtkcode_; // 单元体类型到vtk类型码值的映射
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); // 获取单元体gmsh类型码值
int elem_vtk_code(element_type_enum e_type); // 获取单元体vtk类型码值
int elem_size(element_type_enum e_type); // 获取单元体顶点数量
element_type_enum elem_gmsh_type(int code); // 获取对应gmsh类型码的单元体类型
element_type_enum elem_vtk_type(int code); // 获取对应vtk类型码的单元体类型
void update_indexing(); // 更新索引(对网格的元素进行操作后需调用)
void sort_groups(); // 对单元体组进行梳理(对网格的元素进行操作后需调用)
2024-09-10 15:45:07 +08:00
};
}
#endif // _GCTL_MESH_IO_H