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

#ifndef BORNAGAIN_SAMPLE_SAMPLE_ROUGHNESSMAP_H
#define BORNAGAIN_SAMPLE_SAMPLE_ROUGHNESSMAP_H

#include "Base/Math/FourierTransform.h"
#include <random>

class Sample;

#ifdef BORNAGAIN_PYTHON
#include "Base/Py/ArrayWrapper.h" // Arrayf64Wrapper
#endif                            // BORNAGAIN_PYTHON

//! Generation of random rough surface with given both spectrum and height statistics.
//!
//! Accepts not just the roughness model of the chosen interface, but the whole underlying part of
//! sample because of roughness replication.
//!
//! Algorithm by Pérez-Ràfols & Almqvist, Tribology International 131, 591-604 (2019)
//! https://doi.org/10.1016/j.triboint.2018.11.020

class RoughnessMap {
public:
#ifndef SWIG
    //! Generates coupled interface maps from auto- and adjacent cross-spectra.
    //!
    //! The lowest rough interface is generated by the original single-interface IAAFT algorithm.
    //! Each next interface is synthesized from the nearest generated rough interface below plus an
    //! orthogonal independent residual chosen to satisfy the adjacent cross-spectrum.
    //!
    //! The whole rough stack is generated even if only selected interfaces are later exported.
    //! Therefore a stack-generated upper interface is generally not identical to an independent
    //! RoughnessMap(...).generateMap() result with the same seed. The height distribution is
    //! IAAFT-adjusted only for the lowest rough interface; upper interfaces preserve the target
    //! auto- and adjacent cross-spectra, with self-conjugate DC/Nyquist bins as degenerate cases.
    static std::vector<double2d_t> generateInterfaceMaps(size_t x_points, size_t y_points,
                                                         double Lx, double Ly, const Sample& sample,
                                                         unsigned seed = 0);
#endif // SWIG
#ifdef BORNAGAIN_PYTHON
    //! Generates selected rough interface maps and returns them in the requested order.
    static Arrayf64Wrapper generateInterfaceMaps(size_t x_points, size_t y_points, double Lx,
                                                 double Ly, const Sample& sample,
                                                 const std::vector<int>& interface_indices,
                                                 unsigned seed = 0);
#endif // BORNAGAIN_PYTHON

    RoughnessMap(size_t x_points, size_t y_points, double Lx, double Ly, const Sample& sample,
                 size_t i_layer, unsigned seed = 0);
    RoughnessMap() = delete;

    //! Generates one rough interface with the requested auto-spectrum and height statistics.
    double2d_t generateMap();

#ifdef BORNAGAIN_PYTHON
    //! Returns data to construct a Python Numpy array.
    // NOTE: The method is overriden in the SWIG interface files
    // to return a numpy array instead of an Arrayf64Wrapper instance.
    Arrayf64Wrapper generate();
#endif // BORNAGAIN_PYTHON

private:
#ifndef SWIG
    static constexpr double MIN_RMS = 1e-10;
#endif // SWIG

    //! Creates an uncorrelated random map with the layer transient height statistics.
    double2d_t mapFromHeights() const;

    //! Creates a random map with the layer auto-spectrum and random Fourier phases.
    double2d_t mapFromSpectrum() const;

    //! Replaces Fourier amplitudes by those of s_map while preserving phases from h_map.
    double2d_t applySpectrumToHeights(const double2d_t& h_map, const double2d_t& s_map) const;

    //! Replaces values by the order statistics of h_map while preserving rank order from s_map.
    static double2d_t applyHeightsToSpectrum(const double2d_t& h_map, const double2d_t& s_map);

    int m_x_points{0};
    int m_y_points{0};
    double m_lx; //!< sample x size in nm
    double m_ly; //!< sample y size in nm

    const Sample& m_sample;

    size_t m_i_layer;

    mutable FourierTransform m_ft;
    mutable std::mt19937 m_gen;
};

#endif // BORNAGAIN_SAMPLE_SAMPLE_ROUGHNESSMAP_H
