gctl_mesh/lib/mesh/tri2d_mesh.cpp
2025-01-14 08:56:58 +08:00

216 lines
6.2 KiB
C++

/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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.
******************************************************/
#include "tri2d_mesh.h"
void gctl::triangle2d_mesh::init(std::string in_name, std::string in_info, const array<vertex2dc> &in_nodes,
const array<triangle2d> &in_triangles)
{
check_initiated(true); // 检查是否已经初始化
base_mesh::init(TRI_TET_MESH, MESH_2D, in_name, in_info);
node_num_ = in_nodes.size();
ele_num_ = in_triangles.size();
nodes.resize(node_num_);
for (int i = 0; i < node_num_; i++)
{
nodes[i] = in_nodes[i];
}
elements.resize(ele_num_);
for (int i = 0; i < ele_num_; i++)
{
elements[i].id = i;
for (int j = 0; j < 3; j++)
{
elements[i].vert[j] = nodes.get(in_triangles[i].vert[j]->id);
}
}
initialized_ = true;
return;
}
void gctl::triangle2d_mesh::show_mesh_dimension(std::ostream &os) const
{
os << "node num: " << node_num_ << std::endl;
os << "elem num: " << ele_num_ << std::endl;
return;
}
void gctl::triangle2d_mesh::load_binary(std::string filename)
{
check_initiated(true); // 检查是否已经初始化
std::ifstream infile;
gctl::open_infile(infile, filename, ".2m",
std::ios::in|std::ios::binary);
// 读入网格头信息
load_headinfo(infile, TRI_TET_MESH, MESH_2D);
// 读入网格信息
infile.read((char*)&node_num_, sizeof(int));
infile.read((char*)&ele_num_, sizeof(int));
nodes.resize(node_num_);
elements.resize(ele_num_);
for (int i = 0; i < node_num_; i++)
{
infile.read((char*)nodes.get(i), sizeof(gctl::vertex2dc));
}
int in_index;
for (int i = 0; i < ele_num_; i++)
{
elements[i].id = i;
for (int j = 0; j < 3; j++)
{
infile.read((char*)&in_index, sizeof(int));
elements[i].vert[j] = nodes.get(in_index);
}
}
// 读入模型数据单元
load_datablock(infile);
infile.close();
initialized_ = true;
return;
}
void gctl::triangle2d_mesh::save_binary(std::string filename)
{
check_initiated(); // 检查是否已经初始化
std::ofstream outfile;
gctl::open_outfile(outfile, filename, ".2m",
std::ios::out|std::ios::binary);
// 首先输出网格的头信息
save_headinfo(outfile);
// 输出网格信息
outfile.write((char*)&node_num_, sizeof(int));
outfile.write((char*)&ele_num_, sizeof(int));
for (int i = 0; i < node_num_; i++)
{
outfile.write((char*)nodes.get(i), sizeof(gctl::vertex2dc));
}
int in_index;
for (int i = 0; i < ele_num_; i++)
{
for (int j = 0; j < 3; j++)
{
in_index = elements[i].vert[j]->id;
outfile.write((char*)&in_index, sizeof(int));
}
}
// 输出的模型数据单元
save_datablock(outfile);
outfile.close();
return;
}
gctl::triangle2d_mesh::triangle2d_mesh() : base_mesh::base_mesh(){}
gctl::triangle2d_mesh::triangle2d_mesh(std::string in_name, std::string in_info, const array<vertex2dc> &in_nodes,
const array<triangle2d> &in_triangles)
{
init(in_name, in_info, in_nodes, in_triangles);
}
gctl::triangle2d_mesh::~triangle2d_mesh(){}
const gctl::array<gctl::vertex2dc> &gctl::triangle2d_mesh::get_nodes() const
{
check_initiated(); // 检查是否已经初始化
return nodes;
}
const gctl::array<gctl::triangle2d> &gctl::triangle2d_mesh::get_elements() const
{
check_initiated(); // 检查是否已经初始化
return elements;
}
void gctl::triangle2d_mesh::load_triangle(std::string filename, index_packed_e packed)
{
gctl::read_Triangle_node(filename, nodes, packed);
gctl::read_Triangle_element(filename, elements, nodes, packed);
// 设置名称与信息等
node_num_ = nodes.size();
ele_num_ = elements.size();
base_mesh::init(TRI_TET_MESH, MESH_2D, filename, "Imported from a .node and .ele file.");
initialized_ = true;
return;
}
void gctl::triangle2d_mesh::load_gmsh(std::string filename, index_packed_e packed)
{
std::ifstream infile;
gctl::open_infile(infile, filename, ".msh");
gctl::read_gmsh_node(infile, nodes, packed);
gctl::read_gmsh_element(infile, elements, nodes, packed);
infile.close();
// 设置名称与信息等
node_num_ = nodes.size();
ele_num_ = elements.size();
base_mesh::init(TRI_TET_MESH, MESH_2D, filename, "Imported from a .msh file.");
initialized_ = true;
return;
}
void gctl::triangle2d_mesh::save_gmsh(std::string filename, index_packed_e packed)
{
std::ofstream outfile;
gctl::open_outfile(outfile, filename, ".msh");
gctl::save2gmsh(outfile, elements, nodes, packed);
outfile.close();
return;
}
void gctl::triangle2d_mesh::save_gmsh(std::string filename, output_type_e out_mode, index_packed_e packed)
{
base_mesh::save_gmsh_withdata(filename, out_mode, packed);
return;
}
void gctl::triangle2d_mesh::save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed)
{
base_mesh::save_gmsh_withdata(filename, datname, out_mode, packed);
return;
}