#include "stt_class.h"

int SttGenerator::InTriangleTopo(QuadTreeNode* node, const ControlTopo& in_topo, double diff_threshold){
	//没有插入的地形 直接返回否
	if (in_topo.vert.empty()){
		return 0;
	}
	else{
		int node_depth;
		double node_resolution;
        double max_topo = -1e+30, min_topo = 1e+30;

		Triangle temp_tri;
		for (int j = 0; j < 3; j++){
			temp_tri.ids[j] = node->tri.ids[j];
		}

		node_depth = node->depth;

		node_resolution = 0;
		for (int i = 0; i < 3; i++){
			node_resolution += acos(DotProduct(array_stt_vert_[temp_tri.ids[i]].posic,array_stt_vert_[temp_tri.ids[(i+1)%3]].posic)
				/(ModuleLength(array_stt_vert_[temp_tri.ids[i]].posic)*ModuleLength(array_stt_vert_[temp_tri.ids[(i+1)%3]].posic)));
		}
		node_resolution = node_resolution*60/Pi;

		if (control_topo_.cnst_type == STD)
		{
			if (in_topo.max_depth >= node_depth && 
				node_resolution >= in_topo.minimal_resolution && 
				TopoSTD(in_topo.topo) >= diff_threshold)
			{
				// 将控制点的组别赋值给当前节点
				node->tri.physic_group = in_topo.physic_group;
				return 1;
			}
			else return 0;
		}
		else if (control_topo_.cnst_type == MAX)
		{
			if (in_topo.max_depth >= node_depth && 
				node_resolution >= in_topo.minimal_resolution && 
				TopoMax(in_topo.topo) >= node_depth)
			{
				// 将控制点的组别赋值给当前节点
				node->tri.physic_group = in_topo.physic_group;
				return 1;
			}
			else return 0;
		}
		else throw std::runtime_error("[stt::in_triangle_topo] Invalid topography constrain type.");
	}
}