//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/HardParticle/EllipsoidalSegment.cpp
//! @brief     Implements class EllipsoidalSegment.
//!
//! @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/EllipsoidalSegment.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;

EllipsoidalSegment::EllipsoidalSegment(double radius_x, double radius_y, double radius_z,
                                       double rm_top, double rm_bottom)
    : HardFormfactor()
    , m_radius_x(radius_x)
    , m_radius_y(radius_y)
    , m_radius_z(radius_z)
    , m_rm_top(rm_top)
    , m_rm_bottom(rm_bottom)
{
    EllipsoidalSegment::validate();
}

complex_t EllipsoidalSegment::formfactorAtBottom(C3 q) const
{
    ASSERT(m_validated);
    const double Rx = m_radius_x;
    const double Ry = m_radius_y;
    const double Rz = m_radius_z;
    const double t = m_rm_top;
    const double b = m_rm_bottom;
    const complex_t G = std::sqrt((q.x() * Rx) * (q.x() * Rx) + (q.y() * Ry) * (q.y() * Ry));
    const complex_t HQ = q.z() * Rz;

    if (std::abs(q.mag()) <= std::numeric_limits<double>::epsilon())
        return pi / 3 * Rx * Ry / Rz / Rz
               * (4 * Rz * Rz * Rz - 3 * Rz * (t * t + b * b) + (t * t * t + b * b * b));

    return (2 * pi) * Rx * Ry * Rz * exp_I(q.z() * (Rz - b))
           * ComplexIntegrator().integrate(
               [=](double z) {
                   const double cz = 1 - z * z;
                   return cz * Math::Bessel::J1c(G * std::sqrt(cz)) * exp_I(HQ * z);
               },
               -1 + b / Rz, 1. - t / Rz);
}

void EllipsoidalSegment::validate() const
{
    if (m_radius_x <= 0)
        throw std::runtime_error("radius_x <= 0");
    if (m_radius_y <= 0)
        throw std::runtime_error("radius_y <= 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_x, m_radius_y, m_radius_z,
                                                        untruncated_height, m_rm_top);
    m_validated = true;
}

double EllipsoidalSegment::radialExtension() const
{
    ASSERT(m_validated);
    return (m_radius_x + m_radius_y) / 2.0;
}

bool EllipsoidalSegment::contains(const R3& position) const
{
    double Rx = radiusX(); // semi-axis length along x
    double Ry = radiusY(); // semi-axis length along y
    double Rz = radiusZ(); // semi-axis length along z
    const double t = cutFromTop();
    const double b = cutFromBottom();

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

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

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

    return false;
}
