#include "sgm_esl.h"
#include "progressBar.h"

void SGM_ESL::CalWvWz(){
	double weight_angle;
	double max_vol = BDL_MIN, max_depth = BDL_MIN;
	cpoint blo_center,obs_center;

	WvTWv.resize(mod_blocks_num_,0.0); WzTWz.resize(mod_blocks_num_,0.0);
	PartB.resize(mod_blocks_num_,0.0);
	//计算体积加权矩阵
	ProgressBar *bar = new ProgressBar(mod_blocks_num_,"Calculating Wv");
	for (int i = 0; i < mod_blocks_num_; i++){
		bar->Progressed(i);
		WvTWv[i] = (mod_blocks_[i].xmax - mod_blocks_[i].xmin) * (mod_blocks_[i].ymax - mod_blocks_[i].ymin) * (mod_blocks_[i].zmax - mod_blocks_[i].zmin);
		max_vol = MAX(max_vol,WvTWv[i]);
	}

	//计算深度加权矩阵
	ProgressBar *bar2 = new ProgressBar(mod_blocks_num_,"Calculating Wz");
	for (int i = 0; i < mod_blocks_num_; i++){
		bar2->Progressed(i);
		blo_center.x = 0.5*(mod_blocks_[i].xmin + mod_blocks_[i].xmax);
		blo_center.y = 0.5*(mod_blocks_[i].ymin + mod_blocks_[i].ymax);
		blo_center.z = 0.5*(mod_blocks_[i].zmin + mod_blocks_[i].zmax);
		for (int j = 0; j < obs_points_num_; j++){
			weight_angle = (blo_center.z + obs_points_[j].z)/modCpoint(blo_center - obs_points_[j]);
			WzTWz[i] += weight_angle * pow(1.0/pow(modCpoint(obs_points_[j]-blo_center)+ZERO,3),2);
		}
		max_depth = MAX(max_depth,WzTWz[i]);
	}

	for (int i = 0; i < mod_blocks_.size(); i++){
		WvTWv[i] = pow(WvTWv[i]/max_vol,alpha_);
		WzTWz[i] = pow(WzTWz[i]/max_depth,beta_);
	}
	return;
}