Camera_Parameters.js 3.26 KB
const PI = 3.1415926535898;
const EPS = 0.00000001;
//xyz 是指针类型
export var CalcRealCoor = function(point, invK, Objheight) {
	var point_m = {
    x:0,
    y:0
  }
	var _xyz = {x:null,y:null,z:null};
  point_m.x = (point.x-0.5);
	point_m.y = (point.y-0.5);
	_xyz.x = invK[0]*point_m.x+invK[1]*point_m.y+invK[2];
	_xyz.y = invK[3]*point_m.x+invK[4]*point_m.y+invK[5];
	_xyz.z = invK[6]*point_m.x+invK[7]*point_m.y+invK[8];

  _xyz.x = _xyz.x/_xyz.y*Objheight;
	_xyz.z = _xyz.z/_xyz.y*Objheight;
  _xyz.y = Objheight;
  return _xyz
}

export var CalcImgCoor = function(_point, K ,  xyz) {
  var x_coor = K[0]*xyz.x+K[1]*xyz.y+K[2]*xyz.z;
  var	y_coor = K[3]*xyz.x+K[4]*xyz.y+K[5]*xyz.z;
	var z_coor = K[6]*xyz.x+K[7]*xyz.y+K[8]*xyz.z;

	x_coor /= z_coor;
	y_coor /= z_coor;
	x_coor += 0.5;
	y_coor += 0.5;

	_point.x = x_coor;
	_point.y = y_coor;
	return _point
}


export var CalcK = function(cK, fFocusX,  fFocusY,  fTilt,  fRoll,  fPan) {
	var K = cK;
  var theta = fTilt*PI/180,
			gamma = fRoll*PI/180,
			beta = fPan*PI/180,
			sintheta = Math.sin(theta),
			costheta = Math.cos(theta),
			singamma = Math.sin(gamma),
			cosgamma = Math.cos(gamma),
			sinbeta = Math.sin(beta),
			cosbeta = Math.cos(beta),
			f_mat = [0,0,0,0,0,0,0,0,1],
			theta_mat = [1,0,0,0,0,0,0,0,0],
			gamma_mat = [0,0,0,0,0,0,0,0,1],
			beta_mat = [0,0,0,0,1,0,0,0,0];


	f_mat[0]=fFocusX;
	f_mat[4]=fFocusY;
	theta_mat[4]=costheta;
	theta_mat[5]=-sintheta;
	theta_mat[7]=sintheta;
	theta_mat[8]=costheta;
	gamma_mat[0]=cosgamma;
	gamma_mat[1]=-singamma;
	gamma_mat[3]=singamma;
	gamma_mat[4]=cosgamma;
	beta_mat[0]=cosbeta;
	beta_mat[2]=-sinbeta;
	beta_mat[6]=sinbeta;
	beta_mat[8]=cosbeta;

	K = Mat_Multiply(f_mat, theta_mat);
	if ((fRoll > EPS)||(fRoll < -EPS))
	{
	K =	Mat_Multiply(K, gamma_mat);
	}
	if ((fPan > EPS)||(fPan < -EPS))
	{
	K = Mat_Multiply(K, beta_mat);
	}
	return K 
}

export var CalcInvK = function (cinvK, fFocusX, fFocusY, fTilt,  fRoll,  fPan) {
	var	InvK = cinvK,
		 	theta = fTilt*PI/180,
			gamma = fRoll*PI/180,
			beta = fPan*PI/180,
			sintheta = Math.sin(theta),
			costheta = Math.cos(theta),
			singamma = Math.sin(gamma),
			cosgamma = Math.cos(gamma),
			sinbeta = Math.sin(beta),
			cosbeta = Math.cos(beta),

			f_mat = [0,0,0,0,0,0,0,0,1],
			theta_mat = [1,0,0,0,0,0,0,0,0],
			gamma_mat = [0,0,0,0,0,0,0,0,1],
			beta_mat = [0,0,0,0,1,0,0,0,0];

	f_mat[0]= 1/fFocusX;
	f_mat[4]= 1/fFocusY;
	theta_mat[4]=costheta;
	theta_mat[5]=sintheta;
	theta_mat[7]=-sintheta;
	theta_mat[8]=costheta;
	gamma_mat[0]=cosgamma;
	gamma_mat[1]=singamma;
	gamma_mat[3]=-singamma;
	gamma_mat[4]=cosgamma;
	beta_mat[0]=cosbeta;
	beta_mat[2]=sinbeta;
	beta_mat[6]=-sinbeta;
	beta_mat[8]=cosbeta;

	if ((fRoll > EPS)||(fRoll < -EPS))
	{
		InvK = Mat_Multiply(beta_mat, gamma_mat);
		InvK = Mat_Multiply(InvK, theta_mat);
		InvK = Mat_Multiply(InvK, f_mat);
	}
	else if ((fPan > EPS)||(fPan < -EPS))
	{
		InvK = Mat_Multiply(beta_mat, theta_mat);
		InvK = Mat_Multiply(InvK, f_mat);
	}
	else
	{
		InvK	= Mat_Multiply(theta_mat, f_mat);
	}
	return InvK
}

function Mat_Multiply (m1, m2){
  var mm = [], i, j, k,result = [];
	for (var i=0;i<9;i++){
		mm[i] = m1[i];
	}
	for (var i=0;i<3;i++)	{
		for (var j=0;j<3;j++){
			result[i*3+j] = 0;
			for (var k=0;k<3;k++){
				result[i*3+j] += mm[i*3+k]*m2[k*3+j];
			}
		}
	}
	return result;
}