/***************************************************************************
  qgsmatrix4x4.h
  --------------------------------------
  Date                 : July 2023
  Copyright            : (C) 2023 by Martin Dobias
  Email                : wonder dot sk at gmail dot com
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef QGSMATRIX4X4_H
#define QGSMATRIX4X4_H

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgsvector3d.h"

#include <QString>

using namespace Qt::StringLiterals;

/**
 * \ingroup core
 * \brief A simple 4x4 matrix implementation useful for transformation in 3D space.
 *
 * It is similar to QMatrix4x4, but working with double precision values.
 * Most of the time, doing transform using QMatrix4x4 is fine, however QgsMatrix4x4
 * is useful in situations where single precision floats are not enough.
 * For example, when using transform matrix where translation component has
 * values in order of millions.
 *
 * \warning Non-stable API, exposed to Python for unit testing only.
 *
 * \since QGIS 3.34
 */
class CORE_EXPORT QgsMatrix4x4
{
  public:
    //! Initializes identity matrix
    QgsMatrix4x4() { setToIdentity(); }
    //! Initializes matrix by setting all values in row-major order
    // clang-format off
    QgsMatrix4x4(
      double m11, double m12, double m13, double m14,
      double m21, double m22, double m23, double m24,
      double m31, double m32, double m33, double m34,
      double m41, double m42, double m43, double m44
    );
    // clang-format on

    bool operator==( const QgsMatrix4x4 &other ) const { return fuzzyEqual( other, 4 * std::numeric_limits<double>::epsilon() ); }

    bool operator!=( const QgsMatrix4x4 &other ) const { return !( *this == other ); }


    /**
     * Performs fuzzy comparison between this matrix and \a other using an \a epsilon.
     *
     * \since QGIS 4.2
     */
    bool fuzzyEqual( const QgsMatrix4x4 &other, double epsilon = 1e-8 ) const
    SIP_HOLDGIL;

    //! Returns pointer to the matrix data (stored in column-major order)
    const double *constData() const SIP_SKIP { return *m; }
    //! Returns pointer to the matrix data (stored in column-major order)
    double *data() SIP_SKIP { return *m; }
    // clang-format off
    //! Returns matrix data (in column-major order)
    QList< double > dataList() const SIP_PYNAME( data ) SIP_HOLDGIL;
    // clang-format on

    /**
     * Multiplies this matrix by another that translates coordinates by the components of a \a vector.
     */
    void translate( const QgsVector3D &vector );

    /**
     * Matrix-vector multiplication (vector is converted to homogeneous coordinates [X,Y,Z,1] and back)
     *
     * \see mapVector()
     *
     * \note This function is not the same as mapVector().
*             For points, always use map(). mapVector() is suitable for vectors (directions) only.
     */
    QgsVector3D map( const QgsVector3D &vector ) const SIP_HOLDGIL
    {
      return *this * vector;
    }

    /**
     * Maps vector by multiplying the top 3x3 portion of this matrix by \a vector.
     * The translation and projection components of this matrix are ignored.
     * The matrix is applied pre-vector.
     *
     * \param vector input vector to transform
     *
     * \see map()
     *
     * \since QGIS 4.2
     */
    QgsVector3D mapVector( const QgsVector3D &vector ) const SIP_HOLDGIL;


    //! Returns whether this matrix is an identity matrix
    bool isIdentity() const SIP_HOLDGIL;
    //! Sets matrix to be identity matrix
    void setToIdentity() SIP_HOLDGIL;

    /**
     * Multiplies this matrix by another that scales coordinates by the components of a \a vector.
     *
     * \since QGIS 4.2
     */
    void scale( const QgsVector3D &vector );

    /**
     * Multiples this matrix by another that rotates coordinates through
     * \a angle degrees about the vector (\a x, \a y, \a z).
     *
     * \since QGIS 4.2
     */
    void rotate( double angle, double x, double y, double z );

    /**
     * Multiples this matrix by another that rotates coordinates through
     * \a angle degrees about \a vector.
     *
     * \since QGIS 4.2
     */
    void rotate( double angle, const QgsVector3D &vector );

    /**
     * Returns the determinant of this matrix.
     *
     * \since QGIS 4.2
     */
    double determinant() const;

#ifdef SIP_RUN
// clang-format off
    SIP_PYOBJECT __repr__();
    % MethodCode
    QString str = u"<QgsMatrix4x4(%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12, %13, %14, %15, %16)>"_s
                  .arg( sipCpp->data()[0] )
                  .arg( sipCpp->data()[4] )
                  .arg( sipCpp->data()[8] )
                  .arg( sipCpp->data()[12] )
                  .arg( sipCpp->data()[1] )
                  .arg( sipCpp->data()[5] )
                  .arg( sipCpp->data()[9] )
                  .arg( sipCpp->data()[13] )
                  .arg( sipCpp->data()[2] )
                  .arg( sipCpp->data()[6] )
                  .arg( sipCpp->data()[10] )
                  .arg( sipCpp->data()[14] )
                  .arg( sipCpp->data()[3] )
                  .arg( sipCpp->data()[7] )
                  .arg( sipCpp->data()[11] )
                  .arg( sipCpp->data()[15] );
    sipRes = PyUnicode_FromString( str.toUtf8().constData() );
    % End
// clang-format on
#endif

#ifndef SIP_RUN
    friend CORE_EXPORT QgsMatrix4x4 operator*( const QgsMatrix4x4 &m1, const QgsMatrix4x4 &m2 );
    friend CORE_EXPORT QgsVector3D operator*( const QgsMatrix4x4 &matrix, const QgsVector3D &vector );
#endif

  private:
    // Matrix data - in column-major order
    double m[4][4];

    //! Construct without initializing identity matrix.
    explicit QgsMatrix4x4( int ) { }   // cppcheck-suppress uninitMemberVarPrivate
};

//! Matrix-vector multiplication (vector is converted to homogeneous coordinates [X,Y,Z,1] and back)
CORE_EXPORT QgsVector3D operator*( const QgsMatrix4x4 &matrix, const QgsVector3D &vector );
//! Matrix-matrix multiplication (useful to concatenate transforms)
CORE_EXPORT QgsMatrix4x4 operator*( const QgsMatrix4x4 &m1, const QgsMatrix4x4 &m2 );


#endif
