//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/HardParticle/SpheroidalSegment.cpp
//! @brief     Implements class SpheroidalSegment.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "Sample/HardParticle/SpheroidalSegment.h"
#include "Base/Math/Bessel.h"
#include "Base/Math/Integrator.h"
#include "Base/Util/Assert.h"
#include "Sample/Shape/TruncatedEllipsoidNet.h"
#include <limits>
#include <numbers>

using std::numbers::pi;

SpheroidalSegment::SpheroidalSegment(double radius_xy, double radius_z, double rm_top,
                                     double rm_bottom)
    : HardFormfactor()
    , m_radius_xy(radius_xy)
    , m_radius_z(radius_z)
    , m_rm_top(rm_top)
    , m_rm_bottom(rm_bottom)
{
    SpheroidalSegment::validate();
}

complex_t SpheroidalSegment::formfactorAtBottom(C3 q) const
{
    ASSERT(m_validated);
    const double H = 2 * m_radius_z - m_rm_bottom;
    const double R = m_radius_xy;
    const double fp = m_radius_z / m_radius_xy;
    const double dh = m_rm_top;
    const double R2 = R * R;
    const complex_t Qp = std::sqrt(q.x() * q.x() + q.y() * q.y());
    const complex_t Qz = q.z() * fp;

    if (std::abs(q.mag()) <= std::numeric_limits<double>::epsilon())
        return pi / 3. / fp * (H * H * (3. * R - H / fp) - dh * dh * (3. * R - dh / fp));

    return (2 * pi) * exp_I(q.z() * (H - fp * R)) * fp
           * ComplexIntegrator().integrate(
               [=](double Z) {
                   const double R2Z2 = R2 - Z * Z;
                   return R2Z2 * Math::Bessel::J1c(Qp * std::sqrt(R2Z2)) * exp_I(Qz * Z);
               },
               R - H / fp, R - dh / fp);
}

void SpheroidalSegment::validate() const
{
    if (m_radius_xy <= 0)
        throw std::runtime_error("radius_xy <= 0");
    if (m_radius_z <= 0)
        throw std::runtime_error("radius_z <= 0");
    if (m_rm_top < 0)
        throw std::runtime_error("rm_top < 0");
    if (m_rm_bottom < 0)
        throw std::runtime_error("rm_bottom < 0");
    if (m_rm_top > (2 * m_radius_z - m_rm_bottom))
        throw std::runtime_error("parameters violate condition rm_top > H");
    if (m_rm_bottom > (2 * m_radius_z - m_rm_top))
        throw std::runtime_error("parameters violate condition rm_bottom > H");

    double untruncated_height = 2 * m_radius_z - m_rm_bottom;
    m_shape3D = std::make_unique<TruncatedEllipsoidNet>(m_radius_xy, m_radius_xy, m_radius_z,
                                                        untruncated_height, m_rm_top);
    m_validated = true;
}

bool SpheroidalSegment::contains(const R3& position) const
{
    double Rxy = radiusXY(); // semi-axis length along x & y
    double Rz = radiusZ();   // semi-axis length along z
    const double t = cutFromTop();
    const double b = cutFromBottom();

    if (std::abs(position.x()) > Rxy || std::abs(position.y()) > Rxy)
        return false;

    if (position.z() < 0 || position.z() > 2 * Rz - t - b)
        return false;

    if (std::pow(position.x() / Rxy, 2) + std::pow(position.y() / Rxy, 2)
            + std::pow((position.z() - (Rz - b)) / Rz, 2)
        <= 1)
        return true;

    return false;
}
