/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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.
 ******************************************************/

#ifndef _GCTL_MAG_KERNEL_TRICONE_REN2017_H
#define _GCTL_MAG_KERNEL_TRICONE_REN2017_H

#include "gm_data.h"
#include "gctl/poly/tri_cone.h"
#include "gctl/math/geometry3d.h"
#include "gctl/utility/progress_bar.h"

namespace gctl
{
  struct magcone_para_ren17
	{
        double mag_amp[4]; // 四面体四个面的磁化强度
		point3dc nf[4]; // 四面体面外法线矢量
		point3dc ne[12]; // 四面体边外法线矢量
		point3dc te[12]; // 四面体边切线矢量
	};

	typedef type_tricone<magcone_para_ren17> magcone_ren17; ///< 带magcone_para_ren17属性的三角锥结构体

  /**
   * @brief Set the tolerance for calculating the magnetic parameters of tricone elements.
   *
   * @param tol Tolerance
   */
  void set_magcone_ren17_tolerance(double tol);

	/**
   * @brief Calculate the magnetic parameters of given tricone elements.
   * 
   * @param in_tet Input and output elements
   * @param out_para Output parameters
   * @param mag_B magnetization vecrtors
   */
  void callink_magnetic_para(array<magcone_ren17> &in_cone, array<magcone_para_ren17> &out_para, const array<point3dc> &mag_B);

    /**
	 * @brief Calculate the magnetic parameters of given tricone elements wrt. the spherical coordinates.
	 * 
	 * @note  The value of magnetic susceptibility is taken as one here. This is usefull for calculating 
	 * kernel matrix of the magnetic anomalies.
	 * 
	 * @param in_tet Input elements
	 * @param out_para Output parameters
	 * @param inclina_deg inclination angle of the magnetization vector wrt. the local Cartesian coordinates at every tricone elements
	 * @param declina_deg declination angle of the magnetization vector wrt. the local Cartesian coordinates at every tricone elements
	 * @param mag_vec Output magnetization vectors (This is useful for data visualization)
	 * @param field_tense  Tense of the Earth's magnetic field
	 */
	void callink_magnetic_para_earth_sph(array<magcone_ren17> &in_tet, array<magcone_para_ren17> &out_para, 
		double inclina_deg, double declina_deg, array<point3dc> *mag_vec = nullptr, double field_tense = GCTL_T0);

    /**
     * @brief Calculate the magnetic field vector at a given observation point.
     *
     * @param a_ele Input element
     * @param a_op Input observation point
     * @param R_ptr Output rotation matrix
     * @return point3dc Magnetic field vector
     */
    point3dc magkernel_single(const magcone_ren17 &a_ele, const point3ds &a_op, tensor *R_ptr = nullptr);

    /**
     * @brief Calculate the magnetic field vector at a given observation point.
     *
     * @param a_ele Input element
     * @param a_op Input observation point
     * @param R_ptr Output rotation matrix
     * @return point3dc Magnetic field vector
     */
    tensor magkernel_single_tensor(const magcone_ren17 &a_ele, const point3ds &a_op, tensor *R_ptr = nullptr);

    /**
     * @brief Calculate the magnetic field vector at a given observation point.
     *
     * @param a_ele Input element
     * @param a_op Input observation point
     * @param R_ptr Output rotation matrix
     * @return point3dc Magnetic field vector
     */
	void magkernel(matrix<double> &kernel, const array<magcone_ren17> &top_ele, const array<magcone_ren17> &btm_ele, 
		const array<point3ds> &obsp, magnetic_field_type_e comp_type = Bz, verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate the magnetic field vector at a given observation point.
     *
     * @param a_ele Input element
     * @param a_op Input observation point
     * @param R_ptr Output rotation matrix
     * @return point3dc Magnetic field vector
     */
	void magkernel(spmat<double> &kernel, const array<magcone_ren17> &top_ele, const array<magcone_ren17> &btm_ele, 
		const array<point3ds> &obsp, double cut_angle, magnetic_field_type_e comp_type = Bz, verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate the magnetic field vector at a given observation point.
     *
     * @param a_ele Input element
     * @param a_op Input observation point
     * @param R_ptr Output rotation matrix
     * @return point3dc Magnetic field vector
     */
    void magobser(array<point3dc> &out_obs, const array<magcone_ren17> &top_ele, const array<magcone_ren17> &btm_ele, 
		  const array<point3ds> &obsp, const array<double> &sus, verbose_type_e verbose = FullMsg);

    /**
     * @brief Calculate the magnetic field vector at a given observation point.
     *
     * @param a_ele Input element
     * @param a_op Input observation point
     * @param R_ptr Output rotation matrix
     * @return point3dc Magnetic field vector
     */
    void magobser(array<tensor> &out_obs, const array<magcone_ren17> &top_ele, const array<magcone_ren17> &btm_ele, 
		  const array<point3ds> &obsp, const array<double> &sus, verbose_type_e verbose = FullMsg);
};

#endif // _GCTL_MAG_KERNEL_TRICONE_REN2017_H