//#####################################################################
// Copyright 2020, Craig Schroeder.
// This file is part of PhysBAM whose distribution is governed by the license contained in the accompanying file PHYSBAM_COPYRIGHT.txt.
//#####################################################################
#ifndef __QT_MATH__
#define __QT_MATH__

#include <Core/Matrices/MATRIX.h>
#include <Core/Vectors/VECTOR.h>
#include <QColor>
#include <QMatrix2x2>
#include <QMatrix3x3>
#include <QMatrix4x4>
#include <QString>
#include <QVector2D>
#include <QVector3D>

namespace PhysBAM{

template<class T>
QMatrix4x4 to_qt(const MATRIX<T,4>& m)
{
    QMatrix4x4 r;
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            r(i,j)=m(i,j);
    return r;
}

template<class T>
QMatrix3x3 to_qt(const MATRIX<T,3>& m)
{
    QMatrix3x3 r;
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            r(i,j)=m(i,j);
    return r;
}

template<class T>
QMatrix2x2 to_qt(const MATRIX<T,2>& m)
{
    QMatrix2x2 r;
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
            r(i,j)=m(i,j);
    return r;
}

template<class T>
QMatrix4x4 to_qt_4d(const MATRIX<T,3>& m)
{
    MATRIX<T,4> r;
    r.Set_Submatrix(0,0,m);
    r(3,3)=1;
    return to_qt(r);
}

template<class T>
QVector3D to_qt(const VECTOR<T,3>& v)
{
    QVector3D r;
    for(int i=0;i<3;i++)
        r[i]=v(i);
    return r;
}

template<class T>
QVector2D to_qt(const VECTOR<T,2>& v)
{
    QVector2D r;
    for(int i=0;i<2;i++)
        r[i]=v(i);
    return r;
}

template<class T>
QVector4D to_qt(const VECTOR<T,4>& v)
{
    QVector4D r;
    for(int i=0;i<4;i++)
        r[i]=v(i);
    return r;
}

template<class T>
QColor to_qt_color(const VECTOR<T,3>& v)
{
    return QColor::fromRgbF(v.x,v.y,v.z);
}

template<class T>
void from_qt(VECTOR<T,3>& v,const QColor& c)
{
    v.x=c.redF();
    v.y=c.greenF();
    v.z=c.blueF();
}

inline QString to_qt(const std::string& s)
{
    return QString::fromStdString(s);
}

}
#endif
