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

void gctl::triangle_mesh::init(std::string in_name, std::string in_info, const array<vertex2dc> &in_nodes, 
	const array<triangle2d> &in_triangles)
{
	if (initialized)
	{
		throw std::runtime_error("[gctl::triangle_mesh] The mesh is already initialized.");
	}

	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::triangle_mesh::load_binary(std::string filename)
{
	if (initialized)
	{
		throw std::runtime_error("[gctl::triangle_mesh] The mesh is already initialized.");
	}

	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);
		}
	}
	initialized = true;

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

	infile.close();
	return;
}

void gctl::triangle_mesh::save_binary(std::string filename)
{
	if (!initialized)
	{
		throw std::runtime_error("[gctl::triangle_mesh] The mesh is not initialized.");
	}

	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::triangle_mesh::triangle_mesh() : base_mesh::base_mesh(){}

gctl::triangle_mesh::triangle_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::triangle_mesh::~triangle_mesh(){}

const gctl::array<gctl::vertex2dc> &gctl::triangle_mesh::get_nodes() const
{
	if (!initialized)
	{
		throw std::runtime_error("[gctl::triangle_mesh] The mesh is not initialized.");
	}

	return nodes;
}

const gctl::array<gctl::triangle2d> &gctl::triangle_mesh::get_elements() const
{
	if (!initialized)
	{
		throw std::runtime_error("[gctl::triangle_mesh] The mesh is not initialized.");
	}

	return elements;
}

void gctl::triangle_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::triangle_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::triangle_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::triangle_mesh::save_gmsh(std::string filename, mesh_data_type_e d_type, output_type_e out_mode, index_packed_e packed)
{
	base_mesh::save_gmsh(filename, d_type, out_mode, packed);
	return;
}

void gctl::triangle_mesh::save_gmsh(std::string filename, std::string datname, output_type_e out_mode, index_packed_e packed)
{
	base_mesh::save_gmsh(filename, datname, out_mode, packed);
	return;
}
