#include "head_func.h"
/*************************矢量函数********************************/
//cpoint减法
cpoint operator -(cpoint a, cpoint b){
	cpoint m;
	m.x=a.x-b.x;
	m.y=a.y-b.y;
	m.z=a.z-b.z;
	return m;
}
//cpoint乘标量 注意标量在前
cpoint operator *(double sign,cpoint b){
	cpoint m;
	m.x=sign*b.x;
	m.y=sign*b.y;
	m.z=sign*b.z;
	return m;
}
//cpoint模长
double modCpoint(cpoint v){
	return sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
}
//cpoint两点向量夹角
double angCpoint(cpoint a,cpoint b)
{
	double arg = SetToBox(-1.0,1.0,(a.x*b.x+a.y*b.y+a.z*b.z)/(sqrt(a.x*a.x+a.y*a.y+a.z*a.z)*sqrt(b.x*b.x+b.y*b.y+b.z*b.z)));
	return acos(arg)*180.0/Pi;
}
/*************************全局函数********************************/
double arctg(double v){
	double ang;
	if(v>=0) ang=atan(v);
	else if(v<0) ang=atan(v)+Pi;
	return ang;
}
//将string转换为stringstream
stringstream str2ss(string s){
	stringstream sstr;
	sstr.str(""); sstr.clear(); sstr.str(s);
	return sstr;
}
//测试打开输入文件 如果成功则返回0并输出信息 否则返回1
int open_infile(ifstream &infile, const char* filename){
	infile.open(filename);
	if (!infile){
		cerr << BOLDRED << "error ==> " << RESET << "file not found: " << filename << endl;
		return -1;
	}
	return 0;
}
//测试打开输出文件 如果成功则返回0并输出信息 否则返回1
int open_outfile(ofstream &outfile, const char* filename){
	outfile.open(filename);
	if (!outfile){
		cerr << BOLDRED << "error ==> " << RESET << "fail to create the file: " << filename << endl;
		return -1;
	}
	return 0;
}

/*
y
|
|
v3-------v4
|         |
|         |
|         |
v1-------v2-------> x
*/
double cube_interpolate(double x1, double x2, double y1, double y2,
						double x, double y, double v1, double v2, double v3, double v4)
{
	double A = (x - x1) / (x2 - x1);
	double B = (y - y1) / (y2 - y1);
	return A * B * (v4 - v3 - v2 + v1) + B * (v3 - v1) + A * (v2 - v1) + v1;
}