//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/HardParticle/HorizontalCylinder.cpp
//! @brief     Implements class HorizontalCylinder.
//!
//! @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/HorizontalCylinder.h"
#include "Base/Math/Bessel.h"
#include "Base/Math/Functions.h"
#include "Base/Math/Integrator.h"
#include "Base/Util/Assert.h"
#include "Sample/Shape/DoubleEllipse.h"
#include <numbers>

using std::numbers::pi;

HorizontalCylinder::HorizontalCylinder(double radius, double length, double rm_top,
                                       double rm_bottom)
    : HardFormfactor()
    , m_radius(radius)
    , m_length(length)
    , m_rm_top(rm_top)
    , m_rm_bottom(rm_bottom)
{
    HorizontalCylinder::validate();
}

HorizontalCylinder::HorizontalCylinder(double radius, double length)
    : HorizontalCylinder(radius, length, 0, 0)
{
}

complex_t HorizontalCylinder::formfactorAtBottom(C3 q) const
{
    ASSERT(m_validated);
    const double R = m_radius;
    const double L = m_length;
    const double t = m_rm_top;
    const double b = m_rm_bottom;

    const complex_t qL2 = q.x() * L / 2.;
    const complex_t qR = std::sqrt(q.y() * q.y() + q.z() * q.z()) * R;

    const complex_t axial_part = L * Math::sinc(qL2);
    complex_t radial_part;

    if (m_rm_top == 0 && m_rm_bottom == 0)
        radial_part = (2 * pi) * R * R * Math::Bessel::J1c(qR) * exp_I(q.z() * R);

    else
        // integration variable substituted as z = R * sin(s)
        radial_part = 2. * pow(R, 2) * exp_I(q.z() * (R - b))
                      * ComplexIntegrator().integrate(
                          [=](double s) {
                              return pow(cos(s), 2) * Math::sinc(q.y() * R * cos(s))
                                     * exp_I(q.z() * R * sin(s));
                          },
                          asin(-1. + b / R), asin(1. - t / R));

    return radial_part * axial_part;
}

void HorizontalCylinder::validate() const
{
    if (m_radius <= 0)
        throw std::runtime_error("radius <= 0");
    if (m_length <= 0)
        throw std::runtime_error("length <= 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 - m_rm_bottom))
        throw std::runtime_error("parameters violate condition rm_top > H");
    if (m_rm_bottom > (2 * m_radius - m_rm_top))
        throw std::runtime_error("parameters violate condition rm_bottom > H");

    m_shape3D = std::make_unique<DoubleEllipseX>(
        -m_length / 2, m_radius, m_radius, -m_radius + m_rm_bottom, m_radius - m_rm_top,
        m_length / 2, m_radius, m_radius, -m_radius + m_rm_bottom, m_radius - m_rm_top);

    m_validated = true;
}

bool HorizontalCylinder::contains(const R3& position) const
{
    double R = radius();
    double L = length();
    double t = cutFromTop();
    double b = cutFromBottom();

    if (std::abs(position.x()) > L / 2 || std::abs(position.y()) > R || position.z() < 0
        || position.z() > 2 * R - t - b)
        return false;

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

    return false;
}
