/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2022  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "gkernel_tricone.h"
#include "cmath"

using namespace gctl::geometry3d;

void gctl::callink_gravity_para(array<gravcone> &in_cone, array<gravcone_para> &out_para)
{
	point3dc v1, v2, v3, nf;

	out_para.resize(in_cone.size());
	for (int i = 0; i < in_cone.size(); ++i)
	{
		if (in_cone[i].vert[0] == nullptr || in_cone[i].vert[1] == nullptr || 
			in_cone[i].vert[2] == nullptr || in_cone[i].vert[3] == nullptr)
		{
			throw runtime_error("Invalid vertex pointer. From callink_gravity_para(...)");
		}

		for (int f = 0; f < 4; ++f)
		{
			v1 = *in_cone[i].fget(f, 1) - *in_cone[i].fget(f, 0);
			v2 = *in_cone[i].fget(f, 2) - *in_cone[i].fget(f, 0);
			nf = cross(v1, v2).normal();
			out_para[i].F[f] = kron(nf, nf);

			for (int e = 0; e < 3; ++e)
			{
				v3 = *in_cone[i].fget(f, (e+1)%3) - *in_cone[i].fget(f, e);
				out_para[i].edglen[e+f*3] = v3.module();
				out_para[i].E[e+f*3] = kron(nf, cross(v3, nf).normal());
			}
		}

		in_cone[i].att = out_para.get(i);
	}
	return;
}

typedef void (*gkernel_tri_cone)(gctl::matrix<double> &out_kernel, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, gctl::verbose_type_e verbose);

void gkernel_tricone_pot(gctl::matrix<double> &out_kernel, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, gctl::verbose_type_e verbose);
void gkernel_tricone_vr(gctl::matrix<double> &out_kernel, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, gctl::verbose_type_e verbose);
void gkernel_tricone_vrp(gctl::matrix<double> &out_kernel, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, gctl::verbose_type_e verbose);
void gkernel_tricone_vrt(gctl::matrix<double> &out_kernel, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, gctl::verbose_type_e verbose);
void gkernel_tricone_vrr(gctl::matrix<double> &out_kernel, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, gctl::verbose_type_e verbose);

void gctl::gkernel(matrix<double> &out_kernel, const array<gravcone> &ele, 
	const array<point3ds> &ops, gravitational_field_type_e comp_id, verbose_type_e verbose)
{
	gkernel_tri_cone tricone_kernel;
	switch (comp_id)
	{
	case GravPot:
		tricone_kernel = gkernel_tricone_pot;
		break;
	case Vz:
		tricone_kernel = gkernel_tricone_vr;
		break;
	case Tzx:
		tricone_kernel = gkernel_tricone_vrp;
		break;
	case Tzy:
		tricone_kernel = gkernel_tricone_vrt;
		break;
	case Tzz:
		tricone_kernel = gkernel_tricone_vrr;
		break;
	default:
		tricone_kernel = gkernel_tricone_vr;
		break;
	}
	return tricone_kernel(out_kernel, ele, ops, verbose);
}


typedef void (*gobser_tri_cone)(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose);

void gobser_tricone_pot(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose);
void gobser_tricone_vr(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose);
void gobser_tricone_vrp(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose);
void gobser_tricone_vrt(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose);
void gobser_tricone_vrr(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose);

void gctl::gobser(array<double> &out_obs, const array<gravcone> &ele, const array<point3ds> &ops, 
	const array<double> &rho, gravitational_field_type_e comp_id, verbose_type_e verbose)
{
	gobser_tri_cone tricone_obser;
	switch (comp_id)
	{
	case GravPot:
		tricone_obser = gobser_tricone_pot;
		break;
	case Vz:
		tricone_obser = gobser_tricone_vr;
		break;
	case Tzx:
		tricone_obser = gobser_tricone_vrp;
		break;
	case Tzy:
		tricone_obser = gobser_tricone_vrt;
		break;
	case Tzz:
		tricone_obser = gobser_tricone_vrr;
		break;
	default:
		tricone_obser = gobser_tricone_vr;
		break;
	}
	return tricone_obser(out_obs, ele, ops, rho, verbose);
}

// 前置声明
gctl::point3dc gkernel_tricone_v_sig(const gctl::gravcone &a_ele, const gctl::point3dc &a_op);

void gctl::gobser(array<point3dc> &out_obs, const array<gravcone> &ele, const array<point3dc> &obsp, 
	const array<double> &rho, verbose_type_e verbose)
{
	int i, j;
	int o_size = obsp.size();
	int e_size = ele.size();
	out_obs.resize(o_size, point3dc(0.0, 0.0, 0.0));

	gctl::progress_bar bar(e_size, "gobser_vecCartesian");
	for (j = 0; j < e_size; j++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(j);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(j);

#pragma omp parallel for private (i) shared(j) schedule(guided)
		for (i = 0; i < o_size; i++)
		{
			out_obs[i] = out_obs[i] + gkernel_tricone_v_sig(ele[j], obsp[i]) * rho[j];
		}
	}
	return ;
}


// 以下是具体的实现
double gkernel_tricone_pot_sig(const gctl::gravcone &a_ele, const gctl::point3ds &a_op);
double gkernel_tricone_vr_sig(const gctl::gravcone &a_ele, const gctl::point3ds &a_op);
double gkernel_tricone_vrp_sig(const gctl::gravcone &a_ele, const gctl::point3ds &a_op);
double gkernel_tricone_vrt_sig(const gctl::gravcone &a_ele, const gctl::point3ds &a_op);
double gkernel_tricone_vrr_sig(const gctl::gravcone &a_ele, const gctl::point3ds &a_op);

void gkernel_tricone_pot(gctl::matrix<double> &out_kernel, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();
	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_pot");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private(j) shared(i) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tricone_pot_sig(ele[j], ops[i]);
		}
	}
	return;
}

void gkernel_tricone_vr(gctl::matrix<double> &out_kernel, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();
	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vr");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private(j) shared(i) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tricone_vr_sig(ele[j], ops[i]);
		}
	}
	return;
}

void gkernel_tricone_vrp(gctl::matrix<double> &out_kernel, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();
	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vrp");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) shared(i) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tricone_vrp_sig(ele[j], ops[i]);
		}
	}
	return;
}

void gkernel_tricone_vrt(gctl::matrix<double> &out_kernel, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();
	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vrt");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) shared(i) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tricone_vrt_sig(ele[j], ops[i]);
		}
	}
	return;
}

void gkernel_tricone_vrr(gctl::matrix<double> &out_kernel, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();
	out_kernel.resize(o_size, e_size);

	gctl::progress_bar bar(o_size, "gkernel_vrr");
	for (i = 0; i < o_size; i++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(i);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(i);

#pragma omp parallel for private (j) shared(i) schedule(guided)
		for (j = 0; j < e_size; j++)
		{
			out_kernel[i][j] = gkernel_tricone_vrr_sig(ele[j], ops[i]);
		}
	}
	return;
}


void gobser_tricone_pot(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();
	out_obs.resize(o_size, 0.0);

	gctl::progress_bar bar(e_size, "gobser_pot");
	for (j = 0; j < e_size; j++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(j);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(j);

#pragma omp parallel for private (i) shared(j) schedule(guided)
		for (i = 0; i < o_size; i++)
		{
			out_obs[i] += gkernel_tricone_pot_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gobser_tricone_vr(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();
	out_obs.resize(o_size, 0.0);

	gctl::progress_bar bar(e_size, "gobser_vr");
	for (j = 0; j < e_size; j++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(j);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(j);

#pragma omp parallel for private (i) shared(j) schedule(guided)
		for (i = 0; i < o_size; i++)
		{
			out_obs[i] += gkernel_tricone_vr_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gobser_tricone_vrp(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();
	out_obs.resize(o_size, 0.0);

	gctl::progress_bar bar(e_size, "gobser_vrp");
	for (j = 0; j < e_size; j++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(j);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(j);

#pragma omp parallel for private (i) shared(j) schedule(guided)
		for (i = 0; i < o_size; i++)
		{
			out_obs[i] += gkernel_tricone_vrp_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gobser_tricone_vrt(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();
	out_obs.resize(o_size, 0.0);

	gctl::progress_bar bar(e_size, "gobser_vrt");
	for (j = 0; j < e_size; j++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(j);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(j);

#pragma omp parallel for private (i) shared(j) schedule(guided)
		for (i = 0; i < o_size; i++)
		{
			out_obs[i] += gkernel_tricone_vrt_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gobser_tricone_vrr(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int e_size = ele.size();
	out_obs.resize(o_size, 0.0);

	gctl::progress_bar bar(e_size, "gobser_vrr");
	for (j = 0; j < e_size; j++)
	{
		if (verbose == gctl::FullMsg) bar.progressed(j);
		else if (verbose == gctl::ShortMsg) bar.progressed_simple(j);

#pragma omp parallel for private (i) shared(j) schedule(guided)
		for (i = 0; i < o_size; i++)
		{
			out_obs[i] += gkernel_tricone_vrr_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}


// 以下是具体的实现

double gkernel_tricone_pot_sig(const gctl::gravcone &a_ele, const gctl::point3ds &a_op)
{
	int f,e;
	double Le,wf;
	double dv1,dv2;
	double face_sum, edge_sum;
	// 直角坐标系下观测点的位置
	gctl::point3dc op_c;
	// 注意face_tmp与edge_tmp并不是直角坐标系下的点 我们只是借用向量操作而已
	gctl::point3dc face_tmp, edge_tmp;
	gctl::point3dc re;
	gctl::point3dc r_ijk[3];
	double L_ijk[3];

	gctl::gravcone_para* gp = a_ele.att;

	op_c = s2c(a_op);

	face_sum = edge_sum = 0.0;
	for (f = 0; f < 4; f++)
	{
		r_ijk[0] = *a_ele.fget(f, 0) - op_c;
		r_ijk[1] = *a_ele.fget(f, 1) - op_c;
		r_ijk[2] = *a_ele.fget(f, 2) - op_c;

		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();

		wf =2*atan2(dot(r_ijk[0],cross(r_ijk[1],r_ijk[2])),
					L_ijk[0]*L_ijk[1]*L_ijk[2] + L_ijk[0]*dot(r_ijk[1],r_ijk[2]) + 
					L_ijk[1]*dot(r_ijk[2],r_ijk[0]) + L_ijk[2]*dot(r_ijk[0],r_ijk[1]));

		face_tmp = gp->F[f] * r_ijk[0];
		face_sum += dot(r_ijk[0],face_tmp)*wf;

		for (e = 0; e < 3; e++)
		{
			dv1 = distance(*a_ele.fget(f, e), op_c);
			dv2 = distance(*a_ele.fget(f, (e+1)%3), op_c);

			re = 0.5*(*a_ele.fget(f, e) + *a_ele.fget(f, (e+1)%3)) - op_c;
			Le = log((dv1+dv2+gp->edglen[e+3*f])/(dv1+dv2-gp->edglen[e+3*f]));

			edge_tmp = gp->E[e+3*f] * re;
			edge_sum += dot(re, edge_tmp)*Le;
		}
	}

	return -0.5*GCTL_G0*(face_sum - edge_sum);
}

gctl::point3dc gkernel_tricone_v_sig(const gctl::gravcone &a_ele, const gctl::point3dc &a_op)
{
	int f,e;
	double Le,wf;
	double dv1,dv2;
	gctl::point3dc re;
	gctl::point3dc r_ijk[3];
	gctl::point3dc face_sum(0.0, 0.0, 0.0);
	gctl::point3dc edge_sum(0.0, 0.0, 0.0);
	double L_ijk[3];

	gctl::gravcone_para* gp = a_ele.att;

	for (f = 0; f < 4; f++)
	{
		r_ijk[0] = *a_ele.fget(f, 0) - a_op;
		r_ijk[1] = *a_ele.fget(f, 1) - a_op;
		r_ijk[2] = *a_ele.fget(f, 2) - a_op;

		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();

		wf =2*atan2(dot(r_ijk[0],cross(r_ijk[1],r_ijk[2])),
					L_ijk[0]*L_ijk[1]*L_ijk[2] + L_ijk[0]*dot(r_ijk[1],r_ijk[2]) + 
					L_ijk[1]*dot(r_ijk[2],r_ijk[0]) + L_ijk[2]*dot(r_ijk[0],r_ijk[1]));

		face_sum = face_sum + wf*r_ijk[0]*gp->F[f];

		for (e = 0; e < 3; e++)
		{
			dv1 = distance(*a_ele.fget(f, e), a_op);
			dv2 = distance(*a_ele.fget(f, (e+1)%3), a_op);

			re = 0.5*(*a_ele.fget(f, e) + *a_ele.fget(f, (e+1)%3)) - a_op;
			Le = log((dv1+dv2+gp->edglen[e+3*f])/(dv1+dv2-gp->edglen[e+3*f]));

			edge_sum = edge_sum + Le*re*gp->E[e+3*f];
		}
	}
	return GCTL_G0*(face_sum - edge_sum);
}

double gkernel_tricone_vr_sig(const gctl::gravcone &a_ele, const gctl::point3ds &a_op)
{
	int f,e;
	double Le,wf;
	double dv1,dv2;
	double face_sum, edge_sum;
	// 直角坐标系下观测点的位置
	gctl::point3dc op_c;
	// 注意face_tmp与edge_tmp并不是直角坐标系下的点 我们只是借用向量操作而已
	gctl::point3dc face_tmp, edge_tmp;
	// 注意这里R并不是一个直角坐标系下的点 我们只是借用向量操作而已
	gctl::point3dc R;
	gctl::point3dc re;
	gctl::point3dc r_ijk[3];
	double L_ijk[3];

	gctl::gravcone_para* gp = a_ele.att;

	R.x = sin((0.5-a_op.lat/180.0)*GCTL_Pi)*cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R.y = sin((0.5-a_op.lat/180.0)*GCTL_Pi)*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R.z = cos((0.5-a_op.lat/180.0)*GCTL_Pi);

	op_c = s2c(a_op);

	face_sum = edge_sum = 0.0;
	for (f = 0; f < 4; f++)
	{
		r_ijk[0] = *a_ele.fget(f, 0) - op_c;
		r_ijk[1] = *a_ele.fget(f, 1) - op_c;
		r_ijk[2] = *a_ele.fget(f, 2) - op_c;

		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();

		wf =2*atan2(dot(r_ijk[0],cross(r_ijk[1],r_ijk[2])),
					L_ijk[0]*L_ijk[1]*L_ijk[2] + L_ijk[0]*dot(r_ijk[1],r_ijk[2]) + 
					L_ijk[1]*dot(r_ijk[2],r_ijk[0]) + L_ijk[2]*dot(r_ijk[0],r_ijk[1]));

		face_tmp = gp->F[f] * r_ijk[0];
		face_sum += dot(R,face_tmp)*wf;

		for (e = 0; e < 3; e++)
		{
			dv1 = distance(*a_ele.fget(f, e), op_c);
			dv2 = distance(*a_ele.fget(f, (e+1)%3), op_c);

			re = 0.5*(*a_ele.fget(f, e) + *a_ele.fget(f, (e+1)%3)) - op_c;
			Le = log((dv1+dv2+gp->edglen[e+3*f])/(dv1+dv2-gp->edglen[e+3*f]));

			edge_tmp = gp->E[e+3*f] * re;
			edge_sum += dot(R,edge_tmp)*Le;
		}
	}
	return -1.0*GCTL_G0*(face_sum - edge_sum);
}

double gkernel_tricone_vrp_sig(const gctl::gravcone &a_ele, const gctl::point3ds &a_op)
{
	int f,e;
	double Le,wf;
	double dv1,dv2;
	double face_sum,edge_sum;
	// 直角坐标系下观测点的位置
	gctl::point3dc op_c;
	// 注意face_tmp与edge_tmp并不是直角坐标系下的点 我们只是借用向量操作而已
	gctl::point3dc face_tmp, edge_tmp;
	// 这里我们需要完整的转换矩阵
	gctl::tensor R;
	gctl::point3dc r_ijk[3];
	double L_ijk[3];

	gctl::gravcone_para* gp = a_ele.att;

	R[0][0] = sin((0.5-a_op.lat/180.0)*GCTL_Pi)*cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[0][1] = sin((0.5-a_op.lat/180.0)*GCTL_Pi)*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[0][2] = cos((0.5-a_op.lat/180.0)*GCTL_Pi);

	R[1][0] = cos((0.5-a_op.lat/180.0)*GCTL_Pi)*cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[1][1] = cos((0.5-a_op.lat/180.0)*GCTL_Pi)*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[1][2] = -1.0*sin((0.5-a_op.lat/180.0)*GCTL_Pi);

	R[2][0] = -1.0*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[2][1] = cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[2][2] = 0.0;

	op_c = s2c(a_op);

	face_sum = edge_sum = 0.0;
	for (f = 0; f < 4; f++)
	{
		r_ijk[0] = *a_ele.fget(f, 0) - op_c;
		r_ijk[1] = *a_ele.fget(f, 1) - op_c;
		r_ijk[2] = *a_ele.fget(f, 2) - op_c;

		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();

		wf =2*atan2(dot(r_ijk[0],cross(r_ijk[1],r_ijk[2])),
					L_ijk[0]*L_ijk[1]*L_ijk[2] + L_ijk[0]*dot(r_ijk[1],r_ijk[2]) + 
					L_ijk[1]*dot(r_ijk[2],r_ijk[0]) + L_ijk[2]*dot(r_ijk[0],r_ijk[1]));

		face_tmp.x = gp->F[f].val[0][0]*R[0][0] + gp->F[f].val[1][0]*R[0][1] + gp->F[f].val[2][0]*R[0][2];
		face_tmp.y = gp->F[f].val[0][1]*R[0][0] + gp->F[f].val[1][1]*R[0][1] + gp->F[f].val[2][1]*R[0][2];
		face_tmp.z = gp->F[f].val[0][2]*R[0][0] + gp->F[f].val[1][2]*R[0][1] + gp->F[f].val[2][2]*R[0][2];

		face_sum += (R[2][0]*face_tmp.x + R[2][1]*face_tmp.y + R[2][2]*face_tmp.z) * wf;

		for (e = 0; e < 3; e++)
		{
			dv1 = distance(*a_ele.fget(f, e), op_c);
			dv2 = distance(*a_ele.fget(f, (e+1)%3), op_c);

			Le = log((dv1+dv2+gp->edglen[e+3*f])/(dv1+dv2-gp->edglen[e+3*f]));

			edge_tmp.x = gp->E[e+3*f].val[0][0]*R[0][0] + gp->E[e+3*f].val[1][0]*R[0][1] + gp->E[e+3*f].val[2][0]*R[0][2];
			edge_tmp.y = gp->E[e+3*f].val[0][1]*R[0][0] + gp->E[e+3*f].val[1][1]*R[0][1] + gp->E[e+3*f].val[2][1]*R[0][2];
			edge_tmp.z = gp->E[e+3*f].val[0][2]*R[0][0] + gp->E[e+3*f].val[1][2]*R[0][1] + gp->E[e+3*f].val[2][2]*R[0][2];

			edge_sum += (R[2][0]*edge_tmp.x + R[2][1]*edge_tmp.y + R[2][2]*edge_tmp.z) * Le;
		}
	}
	return 1.0*GCTL_G0*(face_sum - edge_sum);
}

double gkernel_tricone_vrt_sig(const gctl::gravcone &a_ele, const gctl::point3ds &a_op)
{
	int f,e;
	double Le,wf;
	double dv1,dv2;
	double face_sum,edge_sum;
	// 直角坐标系下观测点的位置
	gctl::point3dc op_c;
	// 注意face_tmp与edge_tmp并不是直角坐标系下的点 我们只是借用向量操作而已
	gctl::point3dc face_tmp, edge_tmp;
	// 这里我们需要完整的转换矩阵
	gctl::tensor R;
	gctl::point3dc r_ijk[3];
	double L_ijk[3];

	gctl::gravcone_para* gp = a_ele.att;

	R[0][0] = sin((0.5-a_op.lat/180.0)*GCTL_Pi)*cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[0][1] = sin((0.5-a_op.lat/180.0)*GCTL_Pi)*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[0][2] = cos((0.5-a_op.lat/180.0)*GCTL_Pi);

	R[1][0] = cos((0.5-a_op.lat/180.0)*GCTL_Pi)*cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[1][1] = cos((0.5-a_op.lat/180.0)*GCTL_Pi)*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[1][2] = -1.0*sin((0.5-a_op.lat/180.0)*GCTL_Pi);

	R[2][0] = -1.0*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[2][1] = cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[2][2] = 0.0;

	op_c = s2c(a_op);

	face_sum = edge_sum = 0.0;
	for (f = 0; f < 4; f++)
	{
		r_ijk[0] = *a_ele.fget(f, 0) - op_c;
		r_ijk[1] = *a_ele.fget(f, 1) - op_c;
		r_ijk[2] = *a_ele.fget(f, 2) - op_c;

		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();

		wf =2*atan2(dot(r_ijk[0],cross(r_ijk[1],r_ijk[2])),
					L_ijk[0]*L_ijk[1]*L_ijk[2] + L_ijk[0]*dot(r_ijk[1],r_ijk[2]) + 
					L_ijk[1]*dot(r_ijk[2],r_ijk[0]) + L_ijk[2]*dot(r_ijk[0],r_ijk[1]));

		face_tmp.x = gp->F[f].val[0][0]*R[0][0] + gp->F[f].val[1][0]*R[0][1] + gp->F[f].val[2][0]*R[0][2];
		face_tmp.y = gp->F[f].val[0][1]*R[0][0] + gp->F[f].val[1][1]*R[0][1] + gp->F[f].val[2][1]*R[0][2];
		face_tmp.z = gp->F[f].val[0][2]*R[0][0] + gp->F[f].val[1][2]*R[0][1] + gp->F[f].val[2][2]*R[0][2];

		face_sum += (R[1][0]*face_tmp.x + R[1][1]*face_tmp.y + R[1][2]*face_tmp.z) * wf;

		for (e = 0; e < 3; e++)
		{
			dv1 = distance(*a_ele.fget(f, e), op_c);
			dv2 = distance(*a_ele.fget(f, (e+1)%3), op_c);

			Le = log((dv1+dv2+gp->edglen[e+3*f])/(dv1+dv2-gp->edglen[e+3*f]));

			edge_tmp.x = gp->E[e+3*f].val[0][0]*R[0][0] + gp->E[e+3*f].val[1][0]*R[0][1] + gp->E[e+3*f].val[2][0]*R[0][2];
			edge_tmp.y = gp->E[e+3*f].val[0][1]*R[0][0] + gp->E[e+3*f].val[1][1]*R[0][1] + gp->E[e+3*f].val[2][1]*R[0][2];
			edge_tmp.z = gp->E[e+3*f].val[0][2]*R[0][0] + gp->E[e+3*f].val[1][2]*R[0][1] + gp->E[e+3*f].val[2][2]*R[0][2];

			edge_sum += (R[1][0]*edge_tmp.x + R[1][1]*edge_tmp.y + R[1][2]*edge_tmp.z) * Le;
		}
	}
	return 1.0*GCTL_G0*(face_sum - edge_sum);
}

double gkernel_tricone_vrr_sig(const gctl::gravcone &a_ele, const gctl::point3ds &a_op)
{
	int f,e;
	double Le,wf;
	double dv1,dv2;
	double face_sum,edge_sum;
	// 直角坐标系下观测点的位置
	gctl::point3dc op_c;
	// 注意face_tmp与edge_tmp并不是直角坐标系下的点 我们只是借用向量操作而已
	gctl::point3dc face_tmp, edge_tmp;
	// 这里我们需要完整的转换矩阵
	gctl::tensor R;
	gctl::point3dc r_ijk[3];
	double L_ijk[3];

	gctl::gravcone_para* gp = a_ele.att;

	R[0][0] = sin((0.5-a_op.lat/180.0)*GCTL_Pi)*cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[0][1] = sin((0.5-a_op.lat/180.0)*GCTL_Pi)*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[0][2] = cos((0.5-a_op.lat/180.0)*GCTL_Pi);

	R[1][0] = cos((0.5-a_op.lat/180.0)*GCTL_Pi)*cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[1][1] = cos((0.5-a_op.lat/180.0)*GCTL_Pi)*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[1][2] = -1.0*sin((0.5-a_op.lat/180.0)*GCTL_Pi);

	R[2][0] = -1.0*sin((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[2][1] = cos((2.0+a_op.lon/180.0)*GCTL_Pi);
	R[2][2] = 0.0;

	op_c = s2c(a_op);

	face_sum = edge_sum = 0.0;
	for (f = 0; f < 4; f++)
	{
		r_ijk[0] = *a_ele.fget(f, 0) - op_c;
		r_ijk[1] = *a_ele.fget(f, 1) - op_c;
		r_ijk[2] = *a_ele.fget(f, 2) - op_c;

		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();

		wf =2*atan2(dot(r_ijk[0],cross(r_ijk[1],r_ijk[2])),
					L_ijk[0]*L_ijk[1]*L_ijk[2] + L_ijk[0]*dot(r_ijk[1],r_ijk[2]) + 
					L_ijk[1]*dot(r_ijk[2],r_ijk[0]) + L_ijk[2]*dot(r_ijk[0],r_ijk[1]));

		face_tmp.x = gp->F[f].val[0][0]*R[0][0] + gp->F[f].val[1][0]*R[0][1] + gp->F[f].val[2][0]*R[0][2];
		face_tmp.y = gp->F[f].val[0][1]*R[0][0] + gp->F[f].val[1][1]*R[0][1] + gp->F[f].val[2][1]*R[0][2];
		face_tmp.z = gp->F[f].val[0][2]*R[0][0] + gp->F[f].val[1][2]*R[0][1] + gp->F[f].val[2][2]*R[0][2];

		face_sum += (R[0][0]*face_tmp.x + R[0][1]*face_tmp.y + R[0][2]*face_tmp.z) * wf;

		for (e = 0; e < 3; e++)
		{
			dv1 = distance(*a_ele.fget(f, e), op_c);
			dv2 = distance(*a_ele.fget(f, (e+1)%3), op_c);

			Le = log((dv1+dv2+gp->edglen[e+3*f])/(dv1+dv2-gp->edglen[e+3*f]));

			edge_tmp.x = gp->E[e+3*f].val[0][0]*R[0][0] + gp->E[e+3*f].val[1][0]*R[0][1] + gp->E[e+3*f].val[2][0]*R[0][2];
			edge_tmp.y = gp->E[e+3*f].val[0][1]*R[0][0] + gp->E[e+3*f].val[1][1]*R[0][1] + gp->E[e+3*f].val[2][1]*R[0][2];
			edge_tmp.z = gp->E[e+3*f].val[0][2]*R[0][0] + gp->E[e+3*f].val[1][2]*R[0][1] + gp->E[e+3*f].val[2][2]*R[0][2];

			edge_sum += (R[0][0]*edge_tmp.x + R[0][1]*edge_tmp.y + R[0][2]*edge_tmp.z) * Le;
		}
	}
	return -1.0*GCTL_G0*(face_sum - edge_sum);
}