//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      Sample/Aggregate/Crystal2D.h
//! @brief     Defines class Crystal2D.
//!
//! @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)
//
//  ************************************************************************************************

#ifndef BORNAGAIN_SAMPLE_AGGREGATE_CRYSTAL2D_H
#define BORNAGAIN_SAMPLE_AGGREGATE_CRYSTAL2D_H

#include "Sample/Aggregate/ICrystal2D.h"
#include "Sample/Aggregate/LongRangeOrdered2D.h"
#include "Sample/Lattice/Lattice2D.h"

class Profile2D;

//! Interference function of an infinite 2D crystal.

class Crystal2D : public LongRangeOrdered2D, public ICrystal2D {
public:
    Crystal2D(const IParticle& particle, const Lattice2D& lattice);
    ~Crystal2D() override;

    void setDecayFunction(const Profile2D& decay);

    std::string className() const override { return "Crystal2D"; }

#ifndef SWIG
    double decayLengthX() const;
    double decayLengthY() const;
    double decayGamma() const;
    Crystal2D* clone() const override;


private:
    double iff_without_dw(const R3& q) const override;
    double interferenceForXi(double xi, double qx, double qy) const override;

    //! Returns interference from a single reciprocal lattice vector
    double interferenceAtOneRecLatticePoint(double qx, double qy) const;

    //! Returns reciprocal coordinates in the coordinate system rotated by the angle gamma
    std::pair<double, double> rotateOrthonormal(double qx, double qy, double gamma) const;

    //! Returns qx,qy coordinates of q - qint, where qint is a reciprocal lattice vector
    //! bounding the reciprocal unit cell to which q belongs
    std::pair<double, double> calculateReciprocalVectorFraction(double qx, double qy,
                                                                double xi) const;
    std::unique_ptr<Profile2D> m_decay;
    ReciprocalBases m_sbase; //!< reciprocal lattice is stored without xi
    int m_na, m_nb;          //!< determines the number of reciprocal lattice points to use
#endif                       // SWIG
};

#endif // BORNAGAIN_SAMPLE_AGGREGATE_CRYSTAL2D_H
