/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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 "tet_mesh.h"

void gctl::tetrahedron_mesh::init(std::string in_name, std::string in_info, const array<vertex3dc> &in_nodes, 
	const array<tetrahedron> &in_tets)
{
	check_initiated(true); // 检查网格是否已经初始化
	base_mesh::init(TRI_TET_MESH, MESH_3D, in_name, in_info);

	node_num_ = in_nodes.size();
	ele_num_  = in_tets.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 < 4; j++)
		{
			elements[i].vert[j] = nodes.get(in_tets[i].vert[j]->id);
		}
		elements[i].deter_vert_order();
	}

	initialized_ = true;
	return;
}

void gctl::tetrahedron_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::tetrahedron_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::vertex3dc));
	}

	int in_index;
	for (int i = 0; i < ele_num_; i++)
	{
		elements[i].id = i;
		for (int j = 0; j < 4; j++)
		{
			infile.read((char*)&in_index, sizeof(int));
			elements[i].vert[j] = nodes.get(in_index);
		}
		elements[i].deter_vert_order();
	}

	// 读入模型数据单元
	load_datablock(infile);

	infile.close();
	initialized_ = true;
	return;
}

void gctl::tetrahedron_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::vertex3dc));
	}

	int in_index;
	for (int i = 0; i < ele_num_; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			in_index = elements[i].vert[j]->id;
			outfile.write((char*)&in_index, sizeof(int));
		}
	}

	// 输出的模型数据单元
	save_datablock(outfile);

	outfile.close();
	return;
}

gctl::tetrahedron_mesh::tetrahedron_mesh() : base_mesh::base_mesh(){}

gctl::tetrahedron_mesh::tetrahedron_mesh(std::string in_name, std::string in_info, 
	const array<vertex3dc> &in_nodes, const array<tetrahedron> &in_tets)
{
	init(in_name, in_info, in_nodes, in_tets);
}

gctl::tetrahedron_mesh::~tetrahedron_mesh(){}

const gctl::array<gctl::vertex3dc> &gctl::tetrahedron_mesh::get_nodes() const
{
	check_initiated(); // 检查网格是否已经初始化
	return nodes;
}

const gctl::array<gctl::tetrahedron> &gctl::tetrahedron_mesh::get_elements() const
{
	check_initiated(); // 检查网格是否已经初始化
	return elements;
}

void gctl::tetrahedron_mesh::load_tetgen(std::string filename, index_packed_e packed)
{
	gctl::read_Tetgen_node(filename, nodes, packed);
	gctl::read_Tetgen_element(filename, elements, nodes, packed);

	// 设置名称与信息等
	node_num_ = nodes.size();
	ele_num_  = elements.size();
	base_mesh::init(TRI_TET_MESH, MESH_3D, filename, "Imported from a .node and .ele file.");
	initialized_ = true;
	return;
}

void gctl::tetrahedron_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_3D, filename, "Imported from a .msh file.");
	initialized_ = true;
	return;
}

void gctl::tetrahedron_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;
}