/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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::gravcone_gji::initialize_tensors()
{
	point3dc v1, v2, v3, nf, ne;
	point3dc faceVec, faceVec_rj;
	point3dc edgeVec_j2, edgeVec_j2_rj;
	point3dc edgeVec_23, edgeVec_23_rj;
	point3dc edgeVec_3j, edgeVec_3j_rj;
	point3dc edgeVec_2j, edgeVec_2j_rj;
	point3dc edgeVec_j3, edgeVec_j3_rj;
	point3dc rjNor;

	if (vert[0] == nullptr || vert[1] == nullptr || 
		vert[2] == nullptr || vert[3] == nullptr)
	{
		std::string err_str = "Null pointer found. From void gctl::grav_triangle::initialize_tensors()";
		throw runtime_error(err_str);
	}

	for (int i = 0; i < 4; i++)
	{
		v1 = *vert[cone_order[1+i*3]] - *vert[cone_order[i*3]];
		v2 = *vert[cone_order[2+i*3]] - *vert[cone_order[i*3]];
		nf = cross(v1, v2).normal();
		nface[i] = nf;
		F[i] = kron(nf, nf);

		for (int j = 0; j < 3; j++)
		{
			edglen[j+i*3] = distance(*vert[cone_order[j+i*3]], *vert[cone_order[(j+1)%3+i*3]]);

			v3 = *vert[cone_order[(j+1)%3+i*3]] - *vert[cone_order[j+i*3]];
			ne = cross(v3, nf).normal();
			nedge[j+i*3] = ne;
			E[j+i*3] = kron(nf, ne);
		}
	}

	for (int e = 0; e < 3; e++)
	{
		// 半径的单位矢量
		rjNor = (*vert[e] - *vert[3]).normal();

		//计算顶面的情况
		faceVec = cross(*vert[(e+1)%3] - *vert[e], *vert[(e+2)%3] - *vert[e]);
		faceVec_rj = cross(rjNor, *vert[(e+1)%3] - *vert[(e+2)%3]);
		nf_rj[e] = nr_dr(faceVec, faceVec_rj);

		edgeVec_j2 = cross(*vert[(e+1)%3] - *vert[e], faceVec);
		edgeVec_j2_rj = cross(*vert[(e+1)%3] - *vert[e], faceVec_rj) - cross(rjNor, faceVec);
		nj2_rj[e] = nr_dr(edgeVec_j2, edgeVec_j2_rj);

		edgeVec_23 = cross(*vert[(e+2)%3] - *vert[(e+1)%3], faceVec);
		edgeVec_23_rj = cross(*vert[(e+2)%3] - *vert[(e+1)%3], faceVec_rj);
		n23_rj[e] = nr_dr(edgeVec_23, edgeVec_23_rj);

		edgeVec_3j = cross(*vert[e] - *vert[(e+2)%3], faceVec);
		edgeVec_3j_rj = cross(*vert[e] - *vert[(e+2)%3], faceVec_rj) + cross(rjNor, faceVec);
		n3j_rj[e] = nr_dr(edgeVec_3j, edgeVec_3j_rj);

		//计算右侧面的情况
		faceVec = cross(*vert[(e+1)%3], *vert[e]);
		faceVec_rj = cross(*vert[(e+1)%3], rjNor);

		edgeVec_2j = cross(*vert[e] - *vert[(e+1)%3], faceVec);
		edgeVec_2j_rj = cross(rjNor, faceVec);
		n2j_rj[e] = nr_dr(edgeVec_2j, edgeVec_2j_rj);

		//计算左侧面的情况
		faceVec = cross(*vert[e], *vert[(e+2)%3]);
		faceVec_rj = cross(rjNor, *vert[(e+2)%3]);

		edgeVec_j3 = cross(*vert[(e+2)%3] - *vert[e], faceVec);
		edgeVec_j3_rj = cross(faceVec, rjNor);
		nj3_rj[e] = nr_dr(edgeVec_j3, edgeVec_j3_rj);
	}

	return;
}
*/

void gctl::callink_gravity_para(array<gravcone_gji> &in_cone, array<gravcone_para_gji> &out_para)
{
	point3dc v1, v2, v3, ne, nf;
	point3dc faceVec, faceVec_rj;
	point3dc edgeVec_j2, edgeVec_j2_rj;
	point3dc edgeVec_23, edgeVec_23_rj;
	point3dc edgeVec_3j, edgeVec_3j_rj;
	point3dc edgeVec_2j, edgeVec_2j_rj;
	point3dc edgeVec_j3, edgeVec_j3_rj;
	point3dc rjNor;

	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].nface[f] = nf;
			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();

				ne = cross(v3, nf).normal();
				out_para[i].nedge[e+f*3] = ne;
				out_para[i].E[e+f*3] = kron(nf, ne);
			}

			for (int e = 0; e < 3; ++e)
			{
				// 半径的单位矢量
				rjNor = (*in_cone[i].vert[e] - *in_cone[i].vert[3]).normal();

				//计算顶面的情况
				faceVec = cross(*in_cone[i].vert[(e+1)%3] - *in_cone[i].vert[e], 
					*in_cone[i].vert[(e+2)%3] - *in_cone[i].vert[e]);
				faceVec_rj = cross(rjNor, *in_cone[i].vert[(e+1)%3] - *in_cone[i].vert[(e+2)%3]);
				out_para[i].nf_rj[e] = nr_dr(faceVec, faceVec_rj);

				edgeVec_j2 = cross(*in_cone[i].vert[(e+1)%3] - *in_cone[i].vert[e], faceVec);
				edgeVec_j2_rj = cross(*in_cone[i].vert[(e+1)%3] - *in_cone[i].vert[e], faceVec_rj)
					 - cross(rjNor, faceVec);
				out_para[i].nj2_rj[e] = nr_dr(edgeVec_j2, edgeVec_j2_rj);

				edgeVec_23 = cross(*in_cone[i].vert[(e+2)%3] - *in_cone[i].vert[(e+1)%3], faceVec);
				edgeVec_23_rj = cross(*in_cone[i].vert[(e+2)%3] - *in_cone[i].vert[(e+1)%3], faceVec_rj);
				out_para[i].n23_rj[e] = nr_dr(edgeVec_23, edgeVec_23_rj);

				edgeVec_3j = cross(*in_cone[i].vert[e] - *in_cone[i].vert[(e+2)%3], faceVec);
				edgeVec_3j_rj = cross(*in_cone[i].vert[e] - *in_cone[i].vert[(e+2)%3], faceVec_rj)
					 + cross(rjNor, faceVec);
				out_para[i].n3j_rj[e] = nr_dr(edgeVec_3j, edgeVec_3j_rj);

				//计算右侧面的情况
				faceVec = cross(*in_cone[i].vert[(e+1)%3], *in_cone[i].vert[e]);
				faceVec_rj = cross(*in_cone[i].vert[(e+1)%3], rjNor);

				edgeVec_2j = cross(*in_cone[i].vert[e] - *in_cone[i].vert[(e+1)%3], faceVec);
				edgeVec_2j_rj = cross(rjNor, faceVec);
				out_para[i].n2j_rj[e] = nr_dr(edgeVec_2j, edgeVec_2j_rj);

				//计算左侧面的情况
				faceVec = cross(*in_cone[i].vert[e], *in_cone[i].vert[(e+2)%3]);
				faceVec_rj = cross(rjNor, *in_cone[i].vert[(e+2)%3]);

				edgeVec_j3 = cross(*in_cone[i].vert[(e+2)%3] - *in_cone[i].vert[e], faceVec);
				edgeVec_j3_rj = cross(faceVec, rjNor);
				out_para[i].nj3_rj[e] = nr_dr(edgeVec_j3, edgeVec_j3_rj);
			}
		}

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

typedef void (*gobser_mGrad_tricone)(gctl::array<double> &out_mGrad, 
	const gctl::array<std::vector<gctl::gravcone_gji*> > &vert_neighList, 
	const gctl::array<double> &obs_diff, const gctl::array<gctl::point3ds> &ops, 
	const gctl::array<double> &rho, const gctl::array<gctl::vertex3dc> *verts_ptr, 
	double ang_limit, gctl::verbose_type_e verbose);

void gobser_mGrad_tricone_vr(gctl::array<double> &out_mGrad, 
	const gctl::array<std::vector<gctl::gravcone_gji*> > &vert_neighList, 
	const gctl::array<double> &obs_diff, const gctl::array<gctl::point3ds> &ops, 
	const gctl::array<double> &rho, const gctl::array<gctl::vertex3dc> *verts_ptr, 
	double ang_limit, gctl::verbose_type_e verbose);
void gobser_mGrad_tricone_vrp(gctl::array<double> &out_mGrad, 
	const gctl::array<std::vector<gctl::gravcone_gji*> > &vert_neighList, 
	const gctl::array<double> &obs_diff, const gctl::array<gctl::point3ds> &ops, 
	const gctl::array<double> &rho, const gctl::array<gctl::vertex3dc> *verts_ptr, 
	double ang_limit, gctl::verbose_type_e verbose);
void gobser_mGrad_tricone_vrt(gctl::array<double> &out_mGrad, 
	const gctl::array<std::vector<gctl::gravcone_gji*> > &vert_neighList, 
	const gctl::array<double> &obs_diff, const gctl::array<gctl::point3ds> &ops, 
	const gctl::array<double> &rho, const gctl::array<gctl::vertex3dc> *verts_ptr, 
	double ang_limit, gctl::verbose_type_e verbose);
void gobser_mGrad_tricone_vrr(gctl::array<double> &out_mGrad, 
	const gctl::array<std::vector<gctl::gravcone_gji*> > &vert_neighList, 
	const gctl::array<double> &obs_diff, const gctl::array<gctl::point3ds> &ops, 
	const gctl::array<double> &rho, const gctl::array<gctl::vertex3dc> *verts_ptr, 
	double ang_limit, gctl::verbose_type_e verbose);

void gctl::gobser_model_gradient(array<double> &out_mGrad, 
	const array<std::vector<gravcone_gji*> > &vert_neighList, 
	const array<double> &obs_diff, const array<point3ds> &ops, 
	const array<double> &rho, const array<vertex3dc> *verts_ptr, 
	double ang_limit, gravitational_field_type_e comp_id, verbose_type_e verbose)
{
	gobser_mGrad_tricone gobser_mGrad;
	switch (comp_id)
	{
	case Vz:
		gobser_mGrad = gobser_mGrad_tricone_vr;
		break;
	case Tzx:
		gobser_mGrad = gobser_mGrad_tricone_vrp;
		break;
	case Tzy:
		gobser_mGrad = gobser_mGrad_tricone_vrt;
		break;
	case Tzz:
		gobser_mGrad = gobser_mGrad_tricone_vrr;
		break;
	default:
		gobser_mGrad = gobser_mGrad_tricone_vr;
		break;
	}
	return gobser_mGrad(out_mGrad, vert_neighList, obs_diff, ops, rho, verts_ptr, ang_limit, verbose);
}

double gobser_mGrad_tricone_vr_sig(const std::vector<gctl::gravcone_gji*> &a_list, 
	int vert_id, const gctl::point3ds &a_op, const gctl::array<double> &rho);
double gobser_mGrad_tricone_vrp_sig(const std::vector<gctl::gravcone_gji*> &a_list, 
	int vert_id, const gctl::point3ds &a_op, const gctl::array<double> &rho);
double gobser_mGrad_tricone_vrt_sig(const std::vector<gctl::gravcone_gji*> &a_list, 
	int vert_id, const gctl::point3ds &a_op, const gctl::array<double> &rho);
double gobser_mGrad_tricone_vrr_sig(const std::vector<gctl::gravcone_gji*> &a_list, 
	int vert_id, const gctl::point3ds &a_op, const gctl::array<double> &rho);


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

void gobser_tricone_gji_pot(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone_gji> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose);
void gobser_tricone_gji_vr(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone_gji> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose);
void gobser_tricone_gji_vrp(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone_gji> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose);
void gobser_tricone_gji_vrt(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone_gji> &ele, 
	const gctl::array<gctl::point3ds> &ops, const gctl::array<double> &rho, gctl::verbose_type_e verbose);
void gobser_tricone_gji_vrr(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone_gji> &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_gji> &ele, const array<point3ds> &ops, 
	const array<double> &rho, gravitational_field_type_e comp_id, verbose_type_e verbose)
{
	gobser_tri_cone_gji tricone_obser;
	switch (comp_id)
	{
	case GravPot:
		tricone_obser = gobser_tricone_gji_pot;
	case Vz:
		tricone_obser = gobser_tricone_gji_vr;
		break;
	case Tzx:
		tricone_obser = gobser_tricone_gji_vrp;
		break;
	case Tzy:
		tricone_obser = gobser_tricone_gji_vrt;
		break;
	case Tzz:
		tricone_obser = gobser_tricone_gji_vrr;
		break;
	default:
		tricone_obser = gobser_tricone_gji_vr;
		break;
	}
	return tricone_obser(out_obs, ele, ops, rho, verbose);
}

double gkernel_tricone_gji_pot_sig(const gctl::gravcone_gji &a_ele, const gctl::point3ds &a_op);
double gkernel_tricone_gji_vr_sig(const gctl::gravcone_gji &a_ele, const gctl::point3ds &a_op);
double gkernel_tricone_gji_vrp_sig(const gctl::gravcone_gji &a_ele, const gctl::point3ds &a_op);
double gkernel_tricone_gji_vrt_sig(const gctl::gravcone_gji &a_ele, const gctl::point3ds &a_op);
double gkernel_tricone_gji_vrr_sig(const gctl::gravcone_gji &a_ele, const gctl::point3ds &a_op);

// 以下是具体的实现

void gobser_mGrad_tricone_vr(gctl::array<double> &out_mGrad, 
	const gctl::array<std::vector<gctl::gravcone_gji*> > &vert_neighList, 
	const gctl::array<double> &obs_diff, const gctl::array<gctl::point3ds> &ops, 
	const gctl::array<double> &rho, const gctl::array<gctl::vertex3dc> *verts_ptr, 
	double ang_limit, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int v_size = vert_neighList.size();
	out_mGrad.resize(v_size, 0.0);

	gctl::progress_bar bar(o_size, "gobser_mGrad_vr");

	if (verts_ptr != nullptr && ang_limit > 0.0)
	{
		for (i = 0; i < o_size; i++)
		{
			if (verbose == gctl::FullMsg) bar.progressed(i);
			else if (verbose == gctl::ShortMsg) bar.progressed(i);

#pragma omp parallel for private (j) shared(i) schedule(guided)
			for (j = 0; j < v_size; j++)
			{
				if (angle(s2c(ops[i]), verts_ptr->at(j)) <= ang_limit*GCTL_Pi/180.0)
					out_mGrad.at(j) += obs_diff[i] * gobser_mGrad_tricone_vr_sig(vert_neighList[j], j, ops[i], rho);
			}
		}
	}
	else
	{
		for (i = 0; i < o_size; i++)
		{
			if (verbose == gctl::FullMsg) bar.progressed(i);
			else if (verbose == gctl::ShortMsg) bar.progressed(i);

#pragma omp parallel for private (j) shared(i) schedule(guided)
			for (j = 0; j < v_size; j++)
			{
				out_mGrad.at(j) += obs_diff[i] * gobser_mGrad_tricone_vr_sig(vert_neighList[j], j, ops[i], rho);
			}
		}
	}
	return;
}

void gobser_mGrad_tricone_vrp(gctl::array<double> &out_mGrad, 
	const gctl::array<std::vector<gctl::gravcone_gji*> > &vert_neighList, 
	const gctl::array<double> &obs_diff, const gctl::array<gctl::point3ds> &ops, 
	const gctl::array<double> &rho, const gctl::array<gctl::vertex3dc> *verts_ptr, 
	double ang_limit, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int v_size = vert_neighList.size();
	out_mGrad.resize(v_size, 0.0);

	gctl::progress_bar bar(o_size, "gobser_mGrad_vrp");

	if (verts_ptr != nullptr && ang_limit > 0.0)
	{
		for (i = 0; i < o_size; i++)
		{
			if (verbose == gctl::FullMsg) bar.progressed(i);
			else if (verbose == gctl::ShortMsg) bar.progressed(i);

#pragma omp parallel for private (j) shared(i) schedule(guided)
			for (j = 0; j < v_size; j++)
			{
				if (angle(s2c(ops[i]), verts_ptr->at(j)) <= ang_limit*GCTL_Pi/180.0)
					out_mGrad.at(j) += obs_diff[i] * gobser_mGrad_tricone_vrp_sig(vert_neighList[j], j, ops[i], rho);
			}
		}
	}
	else
	{
		for (i = 0; i < o_size; i++)
		{
			if (verbose == gctl::FullMsg) bar.progressed(i);
			else if (verbose == gctl::ShortMsg) bar.progressed(i);

#pragma omp parallel for private (j) shared(i) schedule(guided)
			for (j = 0; j < v_size; j++)
			{
				out_mGrad.at(j) += obs_diff[i] * gobser_mGrad_tricone_vrp_sig(vert_neighList[j], j, ops[i], rho);
			}
		}
	}
	return;
}

void gobser_mGrad_tricone_vrt(gctl::array<double> &out_mGrad, 
	const gctl::array<std::vector<gctl::gravcone_gji*> > &vert_neighList, 
	const gctl::array<double> &obs_diff, const gctl::array<gctl::point3ds> &ops, 
	const gctl::array<double> &rho, const gctl::array<gctl::vertex3dc> *verts_ptr, 
	double ang_limit, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int v_size = vert_neighList.size();
	out_mGrad.resize(v_size, 0.0);

	gctl::progress_bar bar(o_size, "gobser_mGrad_vrt");

	if (verts_ptr != nullptr && ang_limit > 0.0)
	{
		for (i = 0; i < o_size; i++)
		{
			if (verbose == gctl::FullMsg) bar.progressed(i);
			else if (verbose == gctl::ShortMsg) bar.progressed(i);

#pragma omp parallel for private (j) shared(i) schedule(guided)
			for (j = 0; j < v_size; j++)
			{
				if (angle(s2c(ops[i]), verts_ptr->at(j)) <= ang_limit*GCTL_Pi/180.0)
					out_mGrad.at(j) += obs_diff[i] * gobser_mGrad_tricone_vrt_sig(vert_neighList[j], j, ops[i], rho);
			}
		}
	}
	else
	{
		for (i = 0; i < o_size; i++)
		{
			if (verbose == gctl::FullMsg) bar.progressed(i);
			else if (verbose == gctl::ShortMsg) bar.progressed(i);

#pragma omp parallel for private (j) shared(i) schedule(guided)
			for (j = 0; j < v_size; j++)
			{
				out_mGrad.at(j) += obs_diff[i] * gobser_mGrad_tricone_vrt_sig(vert_neighList[j], j, ops[i], rho);
			}
		}
	}
	return;
}

void gobser_mGrad_tricone_vrr(gctl::array<double> &out_mGrad, 
	const gctl::array<std::vector<gctl::gravcone_gji*> > &vert_neighList, 
	const gctl::array<double> &obs_diff, const gctl::array<gctl::point3ds> &ops, 
	const gctl::array<double> &rho, const gctl::array<gctl::vertex3dc> *verts_ptr, 
	double ang_limit, gctl::verbose_type_e verbose)
{
	int i, j;
	int o_size = ops.size();
	int v_size = vert_neighList.size();
	out_mGrad.resize(v_size, 0.0);

	gctl::progress_bar bar(o_size, "gobser_mGrad_vrr");

	if (verts_ptr != nullptr && ang_limit > 0.0)
	{
		for (i = 0; i < o_size; i++)
		{
			if (verbose == gctl::FullMsg) bar.progressed(i);
			else if (verbose == gctl::ShortMsg) bar.progressed(i);

#pragma omp parallel for private (j) shared(i) schedule(guided)
			for (j = 0; j < v_size; j++)
			{
				if (angle(s2c(ops[i]), verts_ptr->at(j)) <= ang_limit*GCTL_Pi/180.0)
					out_mGrad.at(j) += obs_diff[i] * gobser_mGrad_tricone_vrr_sig(vert_neighList[j], j, ops[i], rho);
			}
		}
	}
	else
	{
		for (i = 0; i < o_size; i++)
		{
			if (verbose == gctl::FullMsg) bar.progressed(i);
			else if (verbose == gctl::ShortMsg) bar.progressed(i);

#pragma omp parallel for private (j) shared(i) schedule(guided)
			for (j = 0; j < v_size; j++)
			{
				out_mGrad.at(j) += obs_diff[i] * gobser_mGrad_tricone_vrr_sig(vert_neighList[j], j, ops[i], rho);
			}
		}
	}
	return;
}


void gobser_tricone_gji_pot(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone_gji> &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.at(i) += gkernel_tricone_gji_pot_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gobser_tricone_gji_vr(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone_gji> &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.at(i) += gkernel_tricone_gji_vr_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gobser_tricone_gji_vrp(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone_gji> &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.at(i) += gkernel_tricone_gji_vrp_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gobser_tricone_gji_vrt(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone_gji> &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.at(i) += gkernel_tricone_gji_vrt_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}

void gobser_tricone_gji_vrr(gctl::array<double> &out_obs, const gctl::array<gctl::gravcone_gji> &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.at(i) += gkernel_tricone_gji_vrr_sig(ele[j], ops[i]) * rho[j];
		}
	}
	return;
}


double gobser_mGrad_tricone_vr_sig(const std::vector<gctl::gravcone_gji*> &a_list, 
	int vert_id, const gctl::point3ds &a_op, const gctl::array<double> &rho)
{
	int local_id;
	double beta, beta_rj, alpha, alpha_rj;
	double a_rj, e_rj, Le_rj, Le;
	double wf, wf_rj;
	double L_j23;
	double L_ijk[3];
	gctl::point3dc op_c;
	gctl::point3dc r_j23[3];
	gctl::point3dc r_j23_side[3];
	gctl::point3dc r_ijk[3];
	gctl::point3dc R;
	gctl::point3dc face_temp, edge_temp;
	gctl::point3dc re;
	gctl::gravcone_gji* curr_tri;
	gctl::gravcone_para_gji* gp;

	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);

	double point_sum = 0.0;
	for (int j = 0; j < a_list.size(); j++)
	{
		curr_tri = a_list.at(j);
		gp = curr_tri->att;
		// 找到顶点在某个三棱锥中的局部排序
		for (int t = 0; t < 3; t++)
		{
			if (vert_id == curr_tri->vert[t]->id)
			{
				local_id = t;
				break;
			}
		}

		//先处理顶面
		r_j23[0] = *curr_tri->vert[local_id];
		r_j23[1] = *curr_tri->vert[(local_id+1)%3];
		r_j23[2] = *curr_tri->vert[(local_id+2)%3];
		//确定观测点到三个顶点的矢量
		r_ijk[0] = r_j23[0] - op_c; //直接取r_ijk[0]
		r_ijk[1] = r_j23[1] - op_c;
		r_ijk[2] = r_j23[2] - op_c;
		//计算对应的矢量长度
		L_j23 = r_j23[0].module();
		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();
		//计算wf和wf相对于rj的偏导数
		beta = dot(r_ijk[0], cross(r_ijk[1], r_ijk[2]));
		beta_rj = dot(r_j23[0], cross(r_ijk[1], r_ijk[2]))/L_j23;

		alpha = 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]);

		alpha_rj = (L_ijk[1]*L_ijk[2] + dot(r_ijk[1], r_ijk[2]))*dot(r_j23[0], r_ijk[0])/(L_j23*L_ijk[0]) + 
				dot(L_ijk[1]*r_ijk[2] + L_ijk[2]*r_ijk[1], r_j23[0])/L_j23;

		wf = 2*atan2(beta, alpha);
		wf_rj = 2*alpha*(beta_rj-beta*alpha_rj/alpha)/(alpha*alpha+beta*beta);

		//计算面与边的乘积
		face_temp = (wf*dot(gp->nface[0], r_ijk[0])*gp->nf_rj[local_id] + 
			wf*dot(gp->nf_rj[local_id], r_ijk[0])*gp->nface[0] + 
			wf*dot(gp->nface[0], r_j23[0].normal())*gp->nface[0] + 
			wf_rj*dot(gp->nface[0], r_ijk[0])*gp->nface[0]);

		//观测点到右侧边
		re = r_j23[0] - op_c;
		//计算a_rj,e_rj
		a_rj = dot(r_j23[0], r_ijk[0])/(L_ijk[0]*L_j23);
		e_rj = dot(r_j23[0], r_j23[0] - r_j23[1])/(gp->edglen[local_id]*L_j23);

		Le = log((L_ijk[0]+L_ijk[1]+gp->edglen[local_id])/
			(L_ijk[0]+L_ijk[1]-gp->edglen[local_id]));

		Le_rj = (a_rj + e_rj)/(L_ijk[0]+L_ijk[1]+gp->edglen[local_id]) - 
				(a_rj - e_rj)/(L_ijk[0]+L_ijk[1]-gp->edglen[local_id]);

		edge_temp = 
			(Le*dot(gp->nedge[local_id], re)*gp->nf_rj[local_id] + 
			Le*dot(gp->nj2_rj[local_id], re)*gp->nface[0] + 
			Le*dot(gp->nedge[local_id], r_j23[0].normal())*gp->nface[0] + 
			Le_rj*dot(gp->nedge[local_id], re)*gp->nface[0] + 
			Le*dot(gp->n2j_rj[local_id], re)*gp->nface[local_id+1] + 
			Le*dot(gp->nedge[local_id*3+5], r_j23[0].normal())*gp->nface[local_id+1] + 
			Le_rj*dot(gp->nedge[local_id*3+5], re)*gp->nface[local_id+1]);

		//观测点到左侧边
		//计算a_rj,e_rj
		a_rj = dot(r_j23[0], r_ijk[0])/(L_ijk[0]*L_j23);
		e_rj = dot(r_j23[0], r_j23[0] - r_j23[2])/(gp->edglen[(local_id+2)%3]*L_j23);

		Le = log((L_ijk[0]+L_ijk[2]+gp->edglen[(local_id+2)%3])/
			(L_ijk[0]+L_ijk[2]-gp->edglen[(local_id+2)%3]));

		Le_rj = (a_rj + e_rj)/(L_ijk[0]+L_ijk[2]+gp->edglen[(local_id+2)%3]) - 
				(a_rj - e_rj)/(L_ijk[0]+L_ijk[2]-gp->edglen[(local_id+2)%3]);

		edge_temp = edge_temp + 
			(Le*dot(gp->nedge[(local_id+2)%3], re)*gp->nf_rj[local_id] + 
			Le*dot(gp->n3j_rj[local_id], re)*gp->nface[0] + 
			Le*dot(gp->nedge[(local_id+2)%3], r_j23[0].normal())*gp->nface[0] + 
			Le_rj*dot(gp->nedge[(local_id+2)%3], re)*gp->nface[0] + 
			Le*dot(gp->nj3_rj[local_id], re)*gp->nface[(local_id+2)%3+1] + 
			Le*dot(gp->nedge[((local_id+2)%3)*3+5], r_j23[0].normal())*gp->nface[(local_id+2)%3+1] + 
			Le_rj*dot(gp->nedge[((local_id+2)%3)*3+5], re)*gp->nface[(local_id+2)%3+1]);

		//观测点到后侧
		re = 0.5*(r_j23[1] + r_j23[2]) - op_c;

		Le = log((L_ijk[1]+L_ijk[2]+gp->edglen[(local_id+1)%3])/
			(L_ijk[1]+L_ijk[2]-gp->edglen[(local_id+1)%3]));

		edge_temp = edge_temp +
			(Le*dot(gp->nedge[(local_id+1)%3], re)*gp->nf_rj[local_id] + 
			Le*dot(gp->n23_rj[local_id], re)*gp->nface[0]);


		//转换r_j23为顶点右边侧面的情况*******************************************
		r_j23_side[0] = r_j23[0];
		r_j23_side[1] = *curr_tri->vert[3];
		r_j23_side[2] = r_j23[1];
		//确定观测点到三个顶点的矢量
		r_ijk[0] = r_j23_side[0] - op_c; //直接取r_ijk[0]为rf
		r_ijk[1] = r_j23_side[1] - op_c;
		r_ijk[2] = r_j23_side[2] - op_c;
		//计算对应的矢量长度
		L_j23 = r_j23_side[0].module();
		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();
		//计算wf和wf相对于rj的偏导数
		beta = dot(r_ijk[0], cross(r_ijk[1], r_ijk[2]));
		beta_rj = dot(r_j23_side[0], cross(r_ijk[1], r_ijk[2]))/L_j23;

		alpha = 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]);

		alpha_rj = (L_ijk[1]*L_ijk[2] + dot(r_ijk[1], r_ijk[2]))*dot(r_j23_side[0], r_ijk[0])/(L_j23*L_ijk[0]) + 
			dot(L_ijk[1]*r_ijk[2] + L_ijk[2]*r_ijk[1],r_j23_side[0])/L_j23;

		wf = 2*atan2(beta, alpha);
		wf_rj = 2*alpha*(beta_rj-beta*alpha_rj/alpha)/(alpha*alpha+beta*beta);
		//累加face_temp
		face_temp = face_temp + 
			(wf*dot(gp->nface[local_id+1], r_j23_side[0])*gp->nface[local_id+1] + 
			wf_rj*dot(gp->nface[local_id+1], r_ijk[0])*gp->nface[local_id+1]);

		//侧棱
		re = r_j23_side[0] - op_c;
		//计算a_rj,e_rj
		a_rj = dot(r_j23_side[0], r_ijk[0])/(L_ijk[0]*L_j23);
		e_rj = dot(r_j23_side[0], r_j23_side[0] - r_j23_side[1])/(gp->edglen[(local_id+1)*3]*L_j23);;

		Le = log((L_ijk[0]+L_ijk[1]+gp->edglen[(local_id+1)*3])/
			(L_ijk[0]+L_ijk[1]-gp->edglen[(local_id+1)*3]));

		Le_rj = (a_rj + e_rj)/(L_ijk[0]+L_ijk[1]+gp->edglen[(local_id+1)*3]) 
			- (a_rj - e_rj)/(L_ijk[0]+L_ijk[1]-gp->edglen[(local_id+1)*3]);

		edge_temp = edge_temp + 
			(Le*dot(gp->nedge[(local_id+1)*3], r_j23_side[0])*gp->nface[local_id+1] + 
			Le_rj*dot(gp->nedge[(local_id+1)*3], re)*gp->nface[local_id+1] + 
			Le*dot(gp->nedge[3*((local_id+2)%3+1)+1], r_j23_side[0])*gp->nface[(local_id+2)%3+1] + 
			Le_rj*dot(gp->nedge[3*((local_id+2)%3+1)+1], re)*gp->nface[(local_id+2)%3+1]);


		//转换r_j23为顶点左边侧面的情况*******************************************
		r_j23_side[0] = r_j23[0];
		r_j23_side[1] = r_j23[2];
		r_j23_side[2] = *curr_tri->vert[3];
		//确定观测点到三个顶点的矢量
		r_ijk[0] = r_j23_side[0] - op_c; //直接取r_ijk[0]为rf
		r_ijk[1] = r_j23_side[1] - op_c;
		r_ijk[2] = r_j23_side[2] - op_c;
		//计算对应的矢量长度
		L_j23 = r_j23_side[0].module();
		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();
		//计算wf和wf相对于rj的偏导数
		beta = dot(r_ijk[0], cross(r_ijk[1],r_ijk[2]));
		beta_rj = dot(r_j23_side[0], cross(r_ijk[1], r_ijk[2]))/L_j23;

		alpha = 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]);

		alpha_rj = (L_ijk[1]*L_ijk[2] + dot(r_ijk[1],r_ijk[2]))*dot(r_j23_side[0],r_ijk[0])/(L_j23*L_ijk[0]) + 
			dot(L_ijk[1]*r_ijk[2] + L_ijk[2]*r_ijk[1],r_j23_side[0])/L_j23;

		wf = 2*atan2(beta, alpha);
		wf_rj = 2*alpha*(beta_rj-beta*alpha_rj/alpha)/(alpha*alpha+beta*beta);
		//累加face_temp
		face_temp = face_temp + 
			(wf*dot(gp->nface[(local_id+2)%3+1], r_j23_side[0])*gp->nface[(local_id+2)%3+1] + 
			wf_rj*dot(gp->nface[(local_id+2)%3+1], r_ijk[0])*gp->nface[(local_id+2)%3+1]);

		point_sum += 1e+8*GCTL_G0*rho[curr_tri->id]*(dot(edge_temp, R) - dot(face_temp, R));
	}
	return point_sum;
}

double gobser_mGrad_tricone_vrp_sig(const std::vector<gctl::gravcone_gji*> &a_list, 
	int vert_id, const gctl::point3ds &a_op, const gctl::array<double> &rho)
{
	int local_id;
	double beta, beta_rj, alpha, alpha_rj;
	double a_rj, e_rj, Le_rj, Le;
	double wf, wf_rj;
	double L_j23;
	double L_ijk[3];
	gctl::point3dc op_c;
	gctl::point3dc r_j23[3];
	gctl::point3dc r_j23_side[3];
	gctl::point3dc r_ijk[3];
	gctl::point3dc R, R_1st;
	gctl::point3dc face_temp, edge_temp;
	gctl::gravcone_gji* curr_tri;
	gctl::gravcone_para_gji* gp;

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

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

	op_c = s2c(a_op);

	double point_sum = 0.0;
	for (int j = 0; j < a_list.size(); j++)
	{
		curr_tri = a_list.at(j);
		gp = curr_tri->att;
		// 找到顶点在某个三棱锥中的局部排序
		for (int t = 0; t < 3; t++)
		{
			if (vert_id == curr_tri->vert[t]->id)
			{
				local_id = t;
				break;
			}
		}

		//先处理顶面
		r_j23[0] = *curr_tri->vert[local_id];
		r_j23[1] = *curr_tri->vert[(local_id+1)%3];
		r_j23[2] = *curr_tri->vert[(local_id+2)%3];
		//确定观测点到三个顶点的矢量
		r_ijk[0] = r_j23[0] - op_c; //直接取r_ijk[0]
		r_ijk[1] = r_j23[1] - op_c;
		r_ijk[2] = r_j23[2] - op_c;
		//计算对应的矢量长度
		L_j23 = r_j23[0].module();
		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();
		//计算wf和wf相对于rj的偏导数
		beta = dot(r_ijk[0], cross(r_ijk[1],r_ijk[2]));
		beta_rj = dot(r_j23[0], cross(r_ijk[1], r_ijk[2]))/L_j23;

		alpha = 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]);

		alpha_rj = (L_ijk[1]*L_ijk[2] + dot(r_ijk[1],r_ijk[2]))*dot(r_j23[0],r_ijk[0])/(L_j23*L_ijk[0]) + 
			dot(L_ijk[1]*r_ijk[2] + L_ijk[2]*r_ijk[1],r_j23[0])/L_j23;

		wf = 2*atan2(beta, alpha);
		wf_rj = 2*alpha*(beta_rj-beta*alpha_rj/alpha)/(alpha*alpha+beta*beta);

		//计算面与边的乘积
		face_temp = (wf*dot(gp->nface[0], R_1st)*gp->nf_rj[local_id] + 
			wf*dot(gp->nf_rj[local_id], R_1st)*gp->nface[0] + 
			wf_rj*dot(gp->nface[0], R_1st)*gp->nface[0]);

		//观测点到右侧边
		//计算a_rj,e_rj
		a_rj = dot(r_j23[0],r_ijk[0])/(L_ijk[0]*L_j23);
		e_rj = dot(r_j23[0],r_j23[0] - r_j23[1])/(gp->edglen[local_id]*L_j23);

		Le = log((L_ijk[0]+L_ijk[1]+gp->edglen[local_id])/
			(L_ijk[0]+L_ijk[1]-gp->edglen[local_id]));

		Le_rj = (a_rj + e_rj)/(L_ijk[0]+L_ijk[1]+gp->edglen[local_id]) - 
				(a_rj - e_rj)/(L_ijk[0]+L_ijk[1]-gp->edglen[local_id]);

		edge_temp = 
			(Le*dot(gp->nedge[local_id],R_1st)*gp->nf_rj[local_id] + 
			Le*dot(gp->nj2_rj[local_id],R_1st)*gp->nface[0] + 
			Le_rj*dot(gp->nedge[local_id],R_1st)*gp->nface[0] + 
			Le*dot(gp->n2j_rj[local_id],R_1st)*gp->nface[local_id+1] + 
			Le_rj*dot(gp->nedge[local_id*3+5],R_1st)*gp->nface[local_id+1]);

		//观测点到左侧边
		//计算a_rj,e_rj
		a_rj = dot(r_j23[0],r_ijk[0])/(L_ijk[0]*L_j23);
		e_rj = dot(r_j23[0],r_j23[0] - r_j23[2])/(gp->edglen[(local_id+2)%3]*L_j23);

		Le = log((L_ijk[0]+L_ijk[2]+gp->edglen[(local_id+2)%3])/
			(L_ijk[0]+L_ijk[2]-gp->edglen[(local_id+2)%3]));

		Le_rj = (a_rj + e_rj)/(L_ijk[0]+L_ijk[2]+gp->edglen[(local_id+2)%3]) - 
				(a_rj - e_rj)/(L_ijk[0]+L_ijk[2]-gp->edglen[(local_id+2)%3]);

		edge_temp = edge_temp + 
			(Le*dot(gp->nedge[(local_id+2)%3],R_1st)*gp->nf_rj[local_id] + 
			Le*dot(gp->n3j_rj[local_id],R_1st)*gp->nface[0] + 
			Le_rj*dot(gp->nedge[(local_id+2)%3],R_1st)*gp->nface[0] + 
			Le*dot(gp->nj3_rj[local_id],R_1st)*gp->nface[(local_id+2)%3+1] + 
			Le_rj*dot(gp->nedge[((local_id+2)%3)*3+5],R_1st)*gp->nface[(local_id+2)%3+1]);

		//观测点到背侧
		Le = log((L_ijk[1]+L_ijk[2]+gp->edglen[(local_id+1)%3])/
			(L_ijk[1]+L_ijk[2]-gp->edglen[(local_id+1)%3]));

		edge_temp = edge_temp +
			(Le*dot(gp->nedge[(local_id+1)%3],R_1st)*gp->nf_rj[local_id] + 
			Le*dot(gp->n23_rj[local_id],R_1st)*gp->nface[0]);

		//转换r_j23为顶点右边侧面的情况*******************************************
		r_j23_side[0] = r_j23[0];
		r_j23_side[1] = *curr_tri->vert[3];
		r_j23_side[2] = r_j23[1];
		//确定观测点到三个顶点的矢量
		r_ijk[0] = r_j23_side[0] - op_c; //直接取r_ijk[0]为rf
		r_ijk[1] = r_j23_side[1] - op_c;
		r_ijk[2] = r_j23_side[2] - op_c;
		//计算对应的矢量长度
		L_j23 = r_j23_side[0].module();
		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();
		//计算wf和wf相对于rj的偏导数
		beta = dot(r_ijk[0],cross(r_ijk[1],r_ijk[2]));
		beta_rj = dot(r_j23_side[0],cross(r_ijk[1], r_ijk[2]))/L_j23;

		alpha = 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]);

		alpha_rj = (L_ijk[1]*L_ijk[2] + dot(r_ijk[1],r_ijk[2]))*dot(r_j23_side[0],r_ijk[0])/(L_j23*L_ijk[0]) + 
			dot(L_ijk[1]*r_ijk[2] + L_ijk[2]*r_ijk[1],r_j23_side[0])/L_j23;

		wf_rj = 2*alpha*(beta_rj-beta*alpha_rj/alpha)/(alpha*alpha+beta*beta);
		//累加face_temp
		face_temp = face_temp + 
			wf_rj*dot(gp->nface[local_id+1],R_1st)*gp->nface[local_id+1];

		//侧棱
		//计算a_rj,e_rj
		a_rj = dot(r_j23_side[0],r_ijk[0])/(L_ijk[0]*L_j23);
		e_rj = dot(r_j23_side[0],r_j23_side[0] - r_j23_side[1])/(gp->edglen[(local_id+1)*3]*L_j23);

		Le_rj = (a_rj + e_rj)/(L_ijk[0]+L_ijk[1]+gp->edglen[(local_id+1)*3]) - 
			(a_rj - e_rj)/(L_ijk[0]+L_ijk[1]-gp->edglen[(local_id+1)*3]);

		edge_temp = edge_temp + 
			(Le_rj*dot(gp->nedge[(local_id+1)*3],R_1st)*gp->nface[local_id+1] + 
			Le_rj*dot(gp->nedge[3*((local_id+2)%3+1)+1],R_1st)*gp->nface[(local_id+2)%3+1]);

		//转换r_j23为顶点左边侧面的情况*******************************************
		r_j23_side[0] = r_j23[0];
		r_j23_side[1] = r_j23[2];
		r_j23_side[2] = *curr_tri->vert[3];
		//确定观测点到三个顶点的矢量
		r_ijk[0] = r_j23_side[0] - op_c; //直接取r_ijk[0]为rf
		r_ijk[1] = r_j23_side[1] - op_c;
		r_ijk[2] = r_j23_side[2] - op_c;
		//计算对应的矢量长度
		L_j23 = r_j23_side[0].module();
		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();
		//计算wf和wf相对于rj的偏导数
		beta = dot(r_ijk[0],cross(r_ijk[1],r_ijk[2]));
		beta_rj = dot(r_j23_side[0],cross(r_ijk[1], r_ijk[2]))/L_j23;

		alpha = 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]);

		alpha_rj = (L_ijk[1]*L_ijk[2] + dot(r_ijk[1],r_ijk[2]))*dot(r_j23_side[0],r_ijk[0])/(L_j23*L_ijk[0]) + 
			dot(L_ijk[1]*r_ijk[2] + L_ijk[2]*r_ijk[1],r_j23_side[0])/L_j23;

		wf_rj = 2*alpha*(beta_rj-beta*alpha_rj/alpha)/(alpha*alpha+beta*beta);
		//累加face_temp
		face_temp = face_temp + 
			wf_rj*dot(gp->nface[(local_id+2)%3+1],R_1st)*gp->nface[(local_id+2)%3+1];

		point_sum += 1e+8*GCTL_G0*rho[curr_tri->id]*(dot(face_temp, R) - dot(edge_temp, R));
	}
	return point_sum;
}

double gobser_mGrad_tricone_vrt_sig(const std::vector<gctl::gravcone_gji*> &a_list, 
	int vert_id, const gctl::point3ds &a_op, const gctl::array<double> &rho)
{
	int local_id;
	double beta, beta_rj, alpha, alpha_rj;
	double a_rj, e_rj, Le_rj, Le;
	double wf, wf_rj;
	double L_j23;
	double L_ijk[3];
	gctl::point3dc op_c;
	gctl::point3dc r_j23[3];
	gctl::point3dc r_j23_side[3];
	gctl::point3dc r_ijk[3];
	gctl::point3dc R, R_1st;
	gctl::point3dc face_temp, edge_temp;
	gctl::gravcone_gji* curr_tri;
	gctl::gravcone_para_gji* gp;

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

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

	op_c = s2c(a_op);

	double point_sum = 0.0;
	for (int j = 0; j < a_list.size(); j++)
	{
		curr_tri = a_list.at(j);
		gp = curr_tri->att;
		// 找到顶点在某个三棱锥中的局部排序
		for (int t = 0; t < 3; t++)
		{
			if (vert_id == curr_tri->vert[t]->id)
			{
				local_id = t;
				break;
			}
		}

		//先处理顶面
		r_j23[0] = *curr_tri->vert[local_id];
		r_j23[1] = *curr_tri->vert[(local_id+1)%3];
		r_j23[2] = *curr_tri->vert[(local_id+2)%3];
		//确定观测点到三个顶点的矢量
		r_ijk[0] = r_j23[0] - op_c; //直接取r_ijk[0]
		r_ijk[1] = r_j23[1] - op_c;
		r_ijk[2] = r_j23[2] - op_c;
		//计算对应的矢量长度
		L_j23 = r_j23[0].module();
		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();
		//计算wf和wf相对于rj的偏导数
		beta = dot(r_ijk[0], cross(r_ijk[1],r_ijk[2]));
		beta_rj = dot(r_j23[0], cross(r_ijk[1], r_ijk[2]))/L_j23;

		alpha = 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]);

		alpha_rj = (L_ijk[1]*L_ijk[2] + dot(r_ijk[1],r_ijk[2]))*dot(r_j23[0],r_ijk[0])/(L_j23*L_ijk[0]) + 
			dot(L_ijk[1]*r_ijk[2] + L_ijk[2]*r_ijk[1],r_j23[0])/L_j23;

		wf = 2*atan2(beta, alpha);
		wf_rj = 2*alpha*(beta_rj-beta*alpha_rj/alpha)/(alpha*alpha+beta*beta);

		//计算面与边的乘积
		face_temp = (wf*dot(gp->nface[0], R_1st)*gp->nf_rj[local_id] + 
			wf*dot(gp->nf_rj[local_id], R_1st)*gp->nface[0] + 
			wf_rj*dot(gp->nface[0], R_1st)*gp->nface[0]);

		//观测点到右侧边
		//计算a_rj,e_rj
		a_rj = dot(r_j23[0],r_ijk[0])/(L_ijk[0]*L_j23);
		e_rj = dot(r_j23[0],r_j23[0] - r_j23[1])/(gp->edglen[local_id]*L_j23);

		Le = log((L_ijk[0]+L_ijk[1]+gp->edglen[local_id])/
			(L_ijk[0]+L_ijk[1]-gp->edglen[local_id]));

		Le_rj = (a_rj + e_rj)/(L_ijk[0]+L_ijk[1]+gp->edglen[local_id]) - 
				(a_rj - e_rj)/(L_ijk[0]+L_ijk[1]-gp->edglen[local_id]);

		edge_temp = 
			(Le*dot(gp->nedge[local_id],R_1st)*gp->nf_rj[local_id] + 
			Le*dot(gp->nj2_rj[local_id],R_1st)*gp->nface[0] + 
			Le_rj*dot(gp->nedge[local_id],R_1st)*gp->nface[0] + 
			Le*dot(gp->n2j_rj[local_id],R_1st)*gp->nface[local_id+1] + 
			Le_rj*dot(gp->nedge[local_id*3+5],R_1st)*gp->nface[local_id+1]);

		//观测点到左侧边
		//计算a_rj,e_rj
		a_rj = dot(r_j23[0],r_ijk[0])/(L_ijk[0]*L_j23);
		e_rj = dot(r_j23[0],r_j23[0] - r_j23[2])/(gp->edglen[(local_id+2)%3]*L_j23);

		Le = log((L_ijk[0]+L_ijk[2]+gp->edglen[(local_id+2)%3])/
			(L_ijk[0]+L_ijk[2]-gp->edglen[(local_id+2)%3]));

		Le_rj = (a_rj + e_rj)/(L_ijk[0]+L_ijk[2]+gp->edglen[(local_id+2)%3]) - 
				(a_rj - e_rj)/(L_ijk[0]+L_ijk[2]-gp->edglen[(local_id+2)%3]);

		edge_temp = edge_temp + 
			(Le*dot(gp->nedge[(local_id+2)%3],R_1st)*gp->nf_rj[local_id] + 
			Le*dot(gp->n3j_rj[local_id],R_1st)*gp->nface[0] + 
			Le_rj*dot(gp->nedge[(local_id+2)%3],R_1st)*gp->nface[0] + 
			Le*dot(gp->nj3_rj[local_id],R_1st)*gp->nface[(local_id+2)%3+1] + 
			Le_rj*dot(gp->nedge[((local_id+2)%3)*3+5],R_1st)*gp->nface[(local_id+2)%3+1]);

		//观测点到背侧
		Le = log((L_ijk[1]+L_ijk[2]+gp->edglen[(local_id+1)%3])/
			(L_ijk[1]+L_ijk[2]-gp->edglen[(local_id+1)%3]));

		edge_temp = edge_temp +
			(Le*dot(gp->nedge[(local_id+1)%3],R_1st)*gp->nf_rj[local_id] + 
			Le*dot(gp->n23_rj[local_id],R_1st)*gp->nface[0]);

		//转换r_j23为顶点右边侧面的情况
		r_j23_side[0] = r_j23[0];
		r_j23_side[1] = *curr_tri->vert[3];
		r_j23_side[2] = r_j23[1];
		//确定观测点到三个顶点的矢量
		r_ijk[0] = r_j23_side[0] - op_c; //直接取r_ijk[0]为rf
		r_ijk[1] = r_j23_side[1] - op_c;
		r_ijk[2] = r_j23_side[2] - op_c;
		//计算对应的矢量长度
		L_j23 = r_j23_side[0].module();
		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();
		//计算wf和wf相对于rj的偏导数
		beta = dot(r_ijk[0],cross(r_ijk[1],r_ijk[2]));
		beta_rj = dot(r_j23_side[0],cross(r_ijk[1], r_ijk[2]))/L_j23;

		alpha = 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]);

		alpha_rj = (L_ijk[1]*L_ijk[2] + dot(r_ijk[1],r_ijk[2]))*dot(r_j23_side[0],r_ijk[0])/(L_j23*L_ijk[0]) + 
			dot(L_ijk[1]*r_ijk[2] + L_ijk[2]*r_ijk[1],r_j23_side[0])/L_j23;

		wf_rj = 2*alpha*(beta_rj-beta*alpha_rj/alpha)/(alpha*alpha+beta*beta);
		//累加face_temp
		face_temp = face_temp + 
			wf_rj*dot(gp->nface[local_id+1],R_1st)*gp->nface[local_id+1];

		//侧棱
		//计算a_rj,e_rj
		a_rj = dot(r_j23_side[0],r_ijk[0])/(L_ijk[0]*L_j23);
		e_rj = dot(r_j23_side[0],r_j23_side[0] - r_j23_side[1])/(gp->edglen[(local_id+1)*3]*L_j23);

		Le_rj = (a_rj + e_rj)/(L_ijk[0]+L_ijk[1]+gp->edglen[(local_id+1)*3]) - 
			(a_rj - e_rj)/(L_ijk[0]+L_ijk[1]-gp->edglen[(local_id+1)*3]);

		edge_temp = edge_temp + 
			(Le_rj*dot(gp->nedge[(local_id+1)*3],R_1st)*gp->nface[local_id+1] + 
			Le_rj*dot(gp->nedge[3*((local_id+2)%3+1)+1],R_1st)*gp->nface[(local_id+2)%3+1]);

		//转换r_j23为顶点左边侧面的情况
		r_j23_side[0] = r_j23[0];
		r_j23_side[1] = r_j23[2];
		r_j23_side[2] = *curr_tri->vert[3];
		//确定观测点到三个顶点的矢量
		r_ijk[0] = r_j23_side[0] - op_c; //直接取r_ijk[0]为rf
		r_ijk[1] = r_j23_side[1] - op_c;
		r_ijk[2] = r_j23_side[2] - op_c;
		//计算对应的矢量长度
		L_j23 = r_j23_side[0].module();
		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();
		//计算wf和wf相对于rj的偏导数
		beta = dot(r_ijk[0],cross(r_ijk[1],r_ijk[2]));
		beta_rj = dot(r_j23_side[0],cross(r_ijk[1], r_ijk[2]))/L_j23;

		alpha = 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]);

		alpha_rj = (L_ijk[1]*L_ijk[2] + dot(r_ijk[1],r_ijk[2]))*dot(r_j23_side[0],r_ijk[0])/(L_j23*L_ijk[0]) + 
			dot(L_ijk[1]*r_ijk[2] + L_ijk[2]*r_ijk[1],r_j23_side[0])/L_j23;

		wf_rj = 2*alpha*(beta_rj-beta*alpha_rj/alpha)/(alpha*alpha+beta*beta);
		//累加face_temp
		face_temp = face_temp + 
			wf_rj*dot(gp->nface[(local_id+2)%3+1],R_1st)*gp->nface[(local_id+2)%3+1];

		point_sum += 1e+8*GCTL_G0*rho[curr_tri->id]*(dot(face_temp, R) - dot(edge_temp, R));
	}
	return point_sum;
}

double gobser_mGrad_tricone_vrr_sig(const std::vector<gctl::gravcone_gji*> &a_list, 
	int vert_id, const gctl::point3ds &a_op, const gctl::array<double> &rho)
{
	int local_id;
	double beta, beta_rj, alpha, alpha_rj;
	double a_rj, e_rj, Le_rj, Le;
	double wf, wf_rj;
	double L_j23;
	double L_ijk[3];
	gctl::point3dc op_c;
	gctl::point3dc r_j23[3];
	gctl::point3dc r_j23_side[3];
	gctl::point3dc r_ijk[3];
	gctl::point3dc R;
	gctl::point3dc face_temp, edge_temp;
	gctl::gravcone_gji* curr_tri;
	gctl::gravcone_para_gji* gp;

	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);

	double point_sum = 0.0;
	for (int j = 0; j < a_list.size(); j++)
	{
		curr_tri = a_list.at(j);
		gp = curr_tri->att;
		// 找到顶点在某个三棱锥中的局部排序
		for (int t = 0; t < 3; t++)
		{
			if (vert_id == curr_tri->vert[t]->id)
			{
				local_id = t;
				break;
			}
		}

		//先处理顶面
		r_j23[0] = *curr_tri->vert[local_id];
		r_j23[1] = *curr_tri->vert[(local_id+1)%3];
		r_j23[2] = *curr_tri->vert[(local_id+2)%3];
		//确定观测点到三个顶点的矢量
		r_ijk[0] = r_j23[0] - op_c; //直接取r_ijk[0]
		r_ijk[1] = r_j23[1] - op_c;
		r_ijk[2] = r_j23[2] - op_c;
		//计算对应的矢量长度
		L_j23 = r_j23[0].module();
		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();
		//计算wf和wf相对于rj的偏导数
		beta = dot(r_ijk[0], cross(r_ijk[1],r_ijk[2]));
		beta_rj = dot(r_j23[0], cross(r_ijk[1], r_ijk[2]))/L_j23;

		alpha = 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]);

		alpha_rj = (L_ijk[1]*L_ijk[2] + dot(r_ijk[1],r_ijk[2]))*dot(r_j23[0],r_ijk[0])/(L_j23*L_ijk[0]) + 
			dot(L_ijk[1]*r_ijk[2] + L_ijk[2]*r_ijk[1],r_j23[0])/L_j23;

		wf = 2*atan2(beta, alpha);
		wf_rj = 2*alpha*(beta_rj-beta*alpha_rj/alpha)/(alpha*alpha+beta*beta);

		//计算面与边的乘积
		face_temp = (wf*dot(gp->nface[0], R)*gp->nf_rj[local_id] + 
			wf*dot(gp->nf_rj[local_id], R)*gp->nface[0] + 
			wf_rj*dot(gp->nface[0], R)*gp->nface[0]);

		//观测点到右侧边
		//计算a_rj,e_rj
		a_rj = dot(r_j23[0],r_ijk[0])/(L_ijk[0]*L_j23);
		e_rj = dot(r_j23[0],r_j23[0] - r_j23[1])/(gp->edglen[local_id]*L_j23);

		Le = log((L_ijk[0]+L_ijk[1]+gp->edglen[local_id])/
			(L_ijk[0]+L_ijk[1]-gp->edglen[local_id]));

		Le_rj = (a_rj + e_rj)/(L_ijk[0]+L_ijk[1]+gp->edglen[local_id]) - 
				(a_rj - e_rj)/(L_ijk[0]+L_ijk[1]-gp->edglen[local_id]);

		edge_temp = 
			(Le*dot(gp->nedge[local_id],R)*gp->nf_rj[local_id] + 
			Le*dot(gp->nj2_rj[local_id],R)*gp->nface[0] + 
			Le_rj*dot(gp->nedge[local_id],R)*gp->nface[0] + 
			Le*dot(gp->n2j_rj[local_id],R)*gp->nface[local_id+1] + 
			Le_rj*dot(gp->nedge[local_id*3+5],R)*gp->nface[local_id+1]);

		//观测点到左侧边
		//计算a_rj,e_rj
		a_rj = dot(r_j23[0],r_ijk[0])/(L_ijk[0]*L_j23);
		e_rj = dot(r_j23[0],r_j23[0] - r_j23[2])/(gp->edglen[(local_id+2)%3]*L_j23);

		Le = log((L_ijk[0]+L_ijk[2]+gp->edglen[(local_id+2)%3])/
			(L_ijk[0]+L_ijk[2]-gp->edglen[(local_id+2)%3]));

		Le_rj = (a_rj + e_rj)/(L_ijk[0]+L_ijk[2]+gp->edglen[(local_id+2)%3]) - 
				(a_rj - e_rj)/(L_ijk[0]+L_ijk[2]-gp->edglen[(local_id+2)%3]);

		edge_temp = edge_temp + 
			(Le*dot(gp->nedge[(local_id+2)%3],R)*gp->nf_rj[local_id] + 
			Le*dot(gp->n3j_rj[local_id],R)*gp->nface[0] + 
			Le_rj*dot(gp->nedge[(local_id+2)%3],R)*gp->nface[0] + 
			Le*dot(gp->nj3_rj[local_id],R)*gp->nface[(local_id+2)%3+1] + 
			Le_rj*dot(gp->nedge[((local_id+2)%3)*3+5],R)*gp->nface[(local_id+2)%3+1]);

		//观测点到背侧
		Le = log((L_ijk[1]+L_ijk[2]+gp->edglen[(local_id+1)%3])/
			(L_ijk[1]+L_ijk[2]-gp->edglen[(local_id+1)%3]));

		edge_temp = edge_temp +
			(Le*dot(gp->nedge[(local_id+1)%3],R)*gp->nf_rj[local_id] + 
			Le*dot(gp->n23_rj[local_id],R)*gp->nface[0]);

		//转换r_j23为顶点右边侧面的情况
		r_j23_side[0] = r_j23[0];
		r_j23_side[1] = *curr_tri->vert[3];
		r_j23_side[2] = r_j23[1];
		//确定观测点到三个顶点的矢量
		r_ijk[0] = r_j23_side[0] - op_c; //直接取r_ijk[0]为rf
		r_ijk[1] = r_j23_side[1] - op_c;
		r_ijk[2] = r_j23_side[2] - op_c;
		//计算对应的矢量长度
		L_j23 = r_j23_side[0].module();
		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();
		//计算wf和wf相对于rj的偏导数
		beta = dot(r_ijk[0],cross(r_ijk[1],r_ijk[2]));
		beta_rj = dot(r_j23_side[0],cross(r_ijk[1], r_ijk[2]))/L_j23;

		alpha = 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]);

		alpha_rj = (L_ijk[1]*L_ijk[2] + dot(r_ijk[1],r_ijk[2]))*dot(r_j23_side[0],r_ijk[0])/(L_j23*L_ijk[0]) + 
			dot(L_ijk[1]*r_ijk[2] + L_ijk[2]*r_ijk[1],r_j23_side[0])/L_j23;

		wf_rj = 2*alpha*(beta_rj-beta*alpha_rj/alpha)/(alpha*alpha+beta*beta);
		//累加face_temp
		face_temp = face_temp + 
			wf_rj*dot(gp->nface[local_id+1],R)*gp->nface[local_id+1];

		//侧棱
		//计算a_rj,e_rj
		a_rj = dot(r_j23_side[0],r_ijk[0])/(L_ijk[0]*L_j23);
		e_rj = dot(r_j23_side[0],r_j23_side[0] - r_j23_side[1])/(gp->edglen[(local_id+1)*3]*L_j23);

		Le_rj = (a_rj + e_rj)/(L_ijk[0]+L_ijk[1]+gp->edglen[(local_id+1)*3]) - 
			(a_rj - e_rj)/(L_ijk[0]+L_ijk[1]-gp->edglen[(local_id+1)*3]);

		edge_temp = edge_temp + 
			(Le_rj*dot(gp->nedge[(local_id+1)*3],R)*gp->nface[local_id+1] + 
			Le_rj*dot(gp->nedge[3*((local_id+2)%3+1)+1],R)*gp->nface[(local_id+2)%3+1]);

		//转换r_j23为顶点左边侧面的情况
		r_j23_side[0] = r_j23[0];
		r_j23_side[1] = r_j23[2];
		r_j23_side[2] = *curr_tri->vert[3];
		//确定观测点到三个顶点的矢量
		r_ijk[0] = r_j23_side[0] - op_c; //直接取r_ijk[0]为rf
		r_ijk[1] = r_j23_side[1] - op_c;
		r_ijk[2] = r_j23_side[2] - op_c;
		//计算对应的矢量长度
		L_j23 = r_j23_side[0].module();
		L_ijk[0] = r_ijk[0].module();
		L_ijk[1] = r_ijk[1].module();
		L_ijk[2] = r_ijk[2].module();
		//计算wf和wf相对于rj的偏导数
		beta = dot(r_ijk[0],cross(r_ijk[1],r_ijk[2]));
		beta_rj = dot(r_j23_side[0],cross(r_ijk[1], r_ijk[2]))/L_j23;

		alpha = 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]);

		alpha_rj = (L_ijk[1]*L_ijk[2] + dot(r_ijk[1],r_ijk[2]))*dot(r_j23_side[0],r_ijk[0])/(L_j23*L_ijk[0]) + 
			dot(L_ijk[1]*r_ijk[2] + L_ijk[2]*r_ijk[1],r_j23_side[0])/L_j23;

		wf_rj = 2*alpha*(beta_rj-beta*alpha_rj/alpha)/(alpha*alpha+beta*beta);
		//累加face_temp
		face_temp = face_temp + 
			wf_rj*dot(gp->nface[(local_id+2)%3+1],R)*gp->nface[(local_id+2)%3+1];

		point_sum += 1e+8*GCTL_G0*rho[curr_tri->id]*(dot(edge_temp, R) - dot(face_temp, R));
	}
	return point_sum;
}


// 以下是具体的实现

double gkernel_tricone_gji_pot_sig(const gctl::gravcone_gji &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_gji* 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.5e+8*GCTL_G0*(face_sum - edge_sum);
}

double gkernel_tricone_gji_vr_sig(const gctl::gravcone_gji &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_gji* 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.0e+8*GCTL_G0*(face_sum - edge_sum);
}

double gkernel_tricone_gji_vrp_sig(const gctl::gravcone_gji &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_gji* 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.0e+8*GCTL_G0*(face_sum - edge_sum);
}

double gkernel_tricone_gji_vrt_sig(const gctl::gravcone_gji &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_gji* 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.0e+8*GCTL_G0*(face_sum - edge_sum);
}

double gkernel_tricone_gji_vrr_sig(const gctl::gravcone_gji &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_gji* 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.0e+8*GCTL_G0*(face_sum - edge_sum);
}