/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2022  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "mathgl_dens.h"

#ifdef GCTL_GRAPHIC_MATHGL

gctl::mathgl_dens::mathgl_dens()
{
    xnum_ = ynum_ = 0;
    x_ll_ = "x"; y_ll_ = "y"; z_ll_ = "z";
}

gctl::mathgl_dens::~mathgl_dens(){}

void gctl::mathgl_dens::range(double xmin, double xmax, double ymin, double ymax)
{
    xmin_ = xmin; xmax_ = xmax;
    ymin_ = ymin; ymax_ = ymax;
    return;
}

void gctl::mathgl_dens::demension(int xnum, int ynum)
{
    xnum_ = xnum; ynum_ = ynum;
    return;
}

void gctl::mathgl_dens::xlabel(std::string xname)
{
    x_ll_ = xname;
    return;
}

void gctl::mathgl_dens::ylabel(std::string yname)
{
    y_ll_ = yname;
    return;
}

void gctl::mathgl_dens::add_dens(const array<double> &in_z, std::string z_label)
{
    if (xnum_*ynum_ != in_z.size())
        throw std::runtime_error("[gctl::mathgl_dens] Invalid dens data size.");

    z_ = in_z;
    z_ll_ = z_label;
    return;
}

int gctl::mathgl_dens::Draw(mglGraph *gr)
{
    mglData z_d;
    double zmin = z_[0], zmax = z_[0];

    z_d.Create(xnum_, ynum_);
    for (size_t i = 0; i < ynum_; i++)
    {
        for (size_t j = 0; j < xnum_; j++)
        {
            z_d.a[j + i*xnum_] = z_[j + i*xnum_];
            zmin = GCTL_MIN(zmin, z_[j + i*xnum_]);
            zmax = GCTL_MAX(zmax, z_[j + i*xnum_]);
        }
    }

    gr->SetSize(600, 600);
    gr->SetRanges(xmin_, xmax_, ymin_, ymax_, zmin, zmax);
    //gr->Title(z_ll_.c_str());
    gr->Colorbar("UbcyqR", 1.0, 0.1, 0.5, 0.8);
    //gr->Puts(mglPoint(xmax_ + 2, ymax_ + 1), z_ll_.c_str());
    gr->Box();
    gr->Axis("x");
    gr->Axis("y");
    //gr->Grid("xy", "h:");
    gr->Label('x', x_ll_.c_str(), 0.0);
    gr->Label('y', y_ll_.c_str(), 0.0);
    gr->Dens(z_d, "UbcyqR");

    //if (file != "null")
    //{
    //    std::string tp = file.substr(file.find_last_of('.') + 1);
    //    if (tp == "eps") gr->WriteEPS(file.c_str());
    //    if (tp == "png") gr->WritePNG(file.c_str());
    //    if (tp == "jpg") gr->WriteJPEG(file.c_str());
    //}
    return 0;
}

int gctl::mathgl_dens::plot()
{
    mglFLTK gr(this, "GCTL Viewer");
    return gr.Run();
}

#endif // GCTL_GRAPHIC_MATHGL