#include "sgm_esl.h"

int SGM_ESL::InitModBlocks(char* filename,int region_phy_group,double upper_depth,double lower_depth){
	int temp_int,temp_id,ele_type,attri_num;
	double temp_val;
	_1dArray temp_element;
	string temp_str;
	stringstream temp_ss;

	int2intMap vert_id_map;

	ifstream mshin;
	if (open_infile(mshin,filename)) return -1; //检查并打开模型文件
	while(getline(mshin,temp_str)){
		//读入模型空间顶点集 msh文件版本为2.2
		if (temp_str == "$Nodes"){
			getline(mshin,temp_str);
			temp_ss = str2ss(temp_str);
			temp_ss >> mod_vertice_num_; //第一个数为顶点的个数
			mod_vertice_.resize(mod_vertice_num_); //开辟空间
			for (int i = 0; i < mod_vertice_num_; i++){
				getline(mshin,temp_str);
				temp_ss = str2ss(temp_str);
				temp_ss >> mod_vertice_[i].id >> mod_vertice_[i].x >> mod_vertice_[i].y >> mod_vertice_[i].z;
				//记录顶点索引对应排序位置用于清理块体顶点索引
				vert_id_map[mod_vertice_[i].id] = i;
				//重置顶点索引
				mod_vertice_[i].id = i;
			}
		}
		//读入模型空间单元体
		else if (temp_str == "$Elements"){
			getline(mshin,temp_str);
			temp_ss = str2ss(temp_str);
			temp_ss >> mod_blocks_num_;
			mod_blocks_.resize(mod_blocks_num_);
			for (int i = 0; i < mod_blocks_num_; i++){
				getline(mshin,temp_str);
				temp_ss = str2ss(temp_str);
				temp_ss >> mod_blocks_[i].id >> ele_type;
				if (ele_type != 3){
					cerr << BOLDRED << "error ==> " << RESET << "wrong model element type, quit program!" << endl;
					return 1;
				}
				else{
					//读入模型单元物理组信息用于分组 注意这里attri_num应该永远为1
					temp_ss >> attri_num;
					if (attri_num != 1){
						cerr << BOLDRED << "error ==> " << RESET << "wrong model element attributes, quit program!" << endl;
						return 1;
					}
					else{
						temp_ss >> mod_blocks_[i].phy_group;
						for (int j = 0; j < 4; j++){
							temp_ss >> mod_blocks_[i].vert[j];
						}
					}
				}
			}
		}
		else continue; //不能识别的单元都被忽略了
	}
	mshin.close();

	//清理模型块体顶点索引排序
	for (int i = 0; i < mod_blocks_.size(); i++){
		for (int j = 0; j < 4; j++){
			mod_blocks_[i].vert[j] = vert_id_map[mod_blocks_[i].vert[j]];
		}
	}

	//第二次读入模型文件 初始化模型单元属性
	if (open_infile(mshin,filename)) return -1; //检查并打开模型文件
	while(getline(mshin,temp_str)){
		if (temp_str == "$ElementData"){
			//先读入元素块的名称 添加到数组
			for (int i = 0; i < 2; i++)
				getline(mshin,temp_str);
			mod_elements_names_.push_back(temp_str);
			//跳过元素属性前面的值 最后一次为当前元素块的个数
			for (int i = 0; i < 6; i++)
				getline(mshin,temp_str);
			temp_ss = str2ss(temp_str);
			temp_ss >> temp_int;
			//初始化模型属性
			if (!temp_element.empty()) temp_element.clear();
			temp_element.resize(mod_blocks_num_,0.0);

			for (int i = 0; i < temp_int; i++){
				getline(mshin,temp_str);
				temp_ss = str2ss(temp_str);
				temp_ss >> temp_id >> temp_val; //读入单元体索引与属性值
				temp_element[temp_id] = temp_val; //添加单元体属性到数组对应位置
			}
			mod_elements_.push_back(temp_element);
		}
		else continue; //不能识别的单元都被忽略了
	}
	mshin.close();

	//模型读入完成 初始化模型单元设置
	init_model_.resize(mod_blocks_num_,0.0);
	invert_model_.resize(mod_blocks_num_,0.0);
	for (int i = 0; i < mod_elements_names_.size(); i++){
		if (!strcmp(mod_elements_names_[i].c_str(),init_model_name_)){
			init_model_ = mod_elements_[i];
			clog << "initial model initialized from file." << endl;
		}
		else cerr << BOLDYELLOW << "warning ==> " << RESET << "element data not used: " << mod_elements_names_[i] << endl;
	}

	//计算模型单元尺寸
	for (int i = 0; i < mod_blocks_num_; i++){
		for (int j = 0; j < 4; j++){
			mod_blocks_[i].xmin = MIN(mod_blocks_[i].xmin, mod_vertice_[mod_blocks_[i].vert[j]].x);
			mod_blocks_[i].xmax = MAX(mod_blocks_[i].xmax, mod_vertice_[mod_blocks_[i].vert[j]].x);
			mod_blocks_[i].ymin = MIN(mod_blocks_[i].ymin, mod_vertice_[mod_blocks_[i].vert[j]].y);
			mod_blocks_[i].ymax = MAX(mod_blocks_[i].ymax, mod_vertice_[mod_blocks_[i].vert[j]].y);
		}
		mod_blocks_[i].zmin = upper_depth;
		mod_blocks_[i].zmax = lower_depth;
	}

	//初始化区域与剩余模型单元列表
	regional_model_list_.resize(mod_blocks_num_,0.0);
	object_model_list_.resize(mod_blocks_num_,1.0);
	if (region_phy_group != -1)
	{
		for (int i = 0; i < mod_blocks_num_; i++){
			if (mod_blocks_[i].phy_group == region_phy_group){
				regional_model_list_[i] = 1.0;
				object_model_list_[i] = 0.0;
			}
		}
	}

	//清理数组
	for (int i = 0; i < mod_elements_.size(); i++)
	{
		mod_elements_[i].clear();
		vector <double>().swap(mod_elements_[i]);
	}
	mod_elements_.clear();
	vector < vector<double> >().swap(mod_elements_);
	mod_elements_names_.clear();
	vector <string>().swap(mod_elements_names_);
	temp_element.clear();
	vector <double>().swap(temp_element);
	return 0;
}