//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/HardParticle/ProfileRipple.cpp
//! @brief     Implements interface SawtoothRipple.
//!
//! @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/ProfileRipple.h"
#include "Sample/HardParticle/Ripples.h"
#include "Sample/Shape/BoxNet.h"
#include "Sample/Shape/RippleCosineNet.h"
#include "Sample/Shape/RippleSawtoothNet.h"

//  ************************************************************************************************
//  interface ProfileRipple
//  ************************************************************************************************

ProfileRipple::ProfileRipple(double length, double width, double height)
    : HardFormfactor()
    , m_length(length)
    , m_width(width)
    , m_height(height)
{
}

double ProfileRipple::radialExtension() const
{
    return (m_width + m_length) / 4.0;
}

complex_t ProfileRipple::formfactorAtBottom(C3 q) const
{
    return factor_x(q.x()) * factor_yz(q.y(), q.z());
}

void ProfileRipple::validate() const
{
    if (m_length <= 0)
        throw std::runtime_error("length <= 0");
    if (m_width <= 0)
        throw std::runtime_error("width <= 0");
    if (m_height <= 0)
        throw std::runtime_error("height <= 0");
    m_validated = true;
}

//  ************************************************************************************************
//  interface ProfileRectangularRipple
//  ************************************************************************************************

ProfileRectangularRipple::ProfileRectangularRipple(double length, double width, double height)
    : ProfileRipple(length, width, height)
{
    m_shape3D = std::make_unique<BoxNet>(m_length, m_width, m_height);
}

//! Complex form factor.
complex_t ProfileRectangularRipple::factor_yz(complex_t qy, complex_t qz) const
{
    return ripples::profile_yz_bar(qy, qz, m_width, m_height);
}

//  ************************************************************************************************
//  interface CosineRipple
//  ************************************************************************************************

CosineRipple::CosineRipple(double length, double width, double height)
    : ProfileRipple(length, width, height)
{
    m_shape3D = std::make_unique<RippleCosineNet>(m_length, m_width, m_height);
}

//! Complex form factor.
complex_t CosineRipple::factor_yz(complex_t qy, complex_t qz) const
{
    return ripples::profile_yz_cosine(qy, qz, m_width, m_height);
}

//  ************************************************************************************************
//  interface SawtoothRipple
//  ************************************************************************************************

SawtoothRipple::SawtoothRipple(double length, double width, double height, double asymmetry)
    : ProfileRipple(length, width, height)
    , m_asymmetry(asymmetry)
{
    m_shape3D = std::make_unique<RippleSawtoothNet>(m_length, m_width, m_height, m_asymmetry);
}

//! Complex form factor.
complex_t SawtoothRipple::factor_yz(complex_t qy, complex_t qz) const
{
    return ripples::profile_yz_triangular(qy, qz, m_width, m_height, m_asymmetry);
}
