Merge pull request 'dev_yi' (#1) from dev_yi into main

Reviewed-on: http://192.168.31.202:8418/zhangyiss/gctl_graphic/pulls/1
This commit is contained in:
张壹 2025-02-09 21:04:45 +08:00
commit e9f228b644
12 changed files with 260 additions and 7 deletions

View File

@ -5,7 +5,7 @@ project(GCTL_GRAPHIC VERSION 1.0)
include(CMakePackageConfigHelpers)
option(GCTL_GRAPHIC_GMT "Use the GMT library" ON)
option(GCTL_GRAPHIC_MATHGL "Use the MathGL2 library" ON)
option(GCTL_GRAPHIC_MATHGL "Use the MathGL2 library" OFF)
message(STATUS "Platform: " ${CMAKE_HOST_SYSTEM_NAME})
message(STATUS "Install prefix: " ${CMAKE_INSTALL_PREFIX})

View File

@ -1,2 +1,2 @@
# gctl_graphic
# gctl_graphic (deprecated)

View File

@ -13,6 +13,8 @@ macro(add_example name switch)
endif()
endmacro()
add_example(gnuplot_ex ON)
if(${GCTL_GRAPHIC_GMT})
add_example(JX_single_ex ON)
endif()

83
example/gnuplot_ex.cpp Normal file
View File

@ -0,0 +1,83 @@
/********************************************************
*
*
*
*
*
*
* 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 "../lib/graphic.h"
int main(int argc, char *argv[])
{
gctl::gnuplot gt;
//one line test
//gt.send("plot [-pi/2:pi] cos(x),-(sin(x) > sin(x+1) ? sin(x) : sin(x+1))");
//buffer test (bessel animation)
/*
gt.to_buffer();
gt.send("set terminal gif animate delay 10 size 600,400");
gt.send("set output 'bessel.gif'");
gt.send("set palette rgb 3,9,9");
gt.send("unset key; unset colorbox; unset border; unset tics");
gt.send("set lmargin at screen 0.03");
gt.send("set bmargin at screen 0");
gt.send("set rmargin at screen 0.97");
gt.send("set tmargin at screen 1");
gt.send("set parametric", true);
gt.send("bessel(x,t) = besj0(x) * cos(2*pi*t)");
gt.send("n = 6 # number of zeros");
gt.send("k = (n*pi-1.0/4*pi)");
gt.send("u_0 = k + 1/(8*k) - 31/(384*k)**3 + 3779/(15360*k)**5");
gt.send("set urange [0:u_0]");
gt.send("set vrange[0:1.5*pi]");
gt.send("set cbrange [-1:1]");
gt.send("set zrange[-1:1]");
gt.send("set isosamples 200,100");
gt.send("set pm3d depthorder");
gt.send("set view 40,200");
std::string cmd;
for (float t = 0.0f; t < 2.0f; t += 0.02f)
{
cmd = "splot u*sin(v),u*cos(v),bessel(u," + std::to_string(t) + ") w pm3d ls 1";
gt.send(cmd);
}
gt.send("set output");
gt.save_buffer("bessel");
gt.send_buffer();
*/
//data test
gt.to_buffer();
gt.send("$Data << EOD");
gt.send("0 0 0");
gt.send("1 1 0.5");
gt.send("2 4 2");
gt.send("3 9 3");
gt.send("EOD");
gt.send("plot $Data using 1:2 with lines title 'y1', '' using 1:3 with lines title 'y2'");
gt.send_buffer();
return 0;
}

View File

@ -31,5 +31,6 @@
#include "graphic/gmt_JX_single.h"
#include "graphic/mathgl_plot.h"
#include "graphic/mathgl_dens.h"
#include "graphic/gnuplot.h"
#endif // _GCTL_GRAPHIC_H

View File

@ -1,2 +1,2 @@
#define GCTL_GRAPHIC_GMT
#define GCTL_GRAPHIC_MATHGL
/* #undef GCTL_GRAPHIC_MATHGL */

View File

@ -29,7 +29,6 @@
#define _GCTL_JX_SINGLE_H
#include "gctl/core/array.h"
#include "gctl/core/exceptions.h"
#include "gctl_graphic_config.h"
#ifdef GCTL_GRAPHIC_GMT

83
lib/graphic/gnuplot.cpp Normal file
View File

@ -0,0 +1,83 @@
/********************************************************
*
*
*
*
*
*
* 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 "gnuplot.h"
gctl::gnuplot::gnuplot(bool persist)
{
_2buffer = false;
pipe = popen(persist ? "gnuplot -persist" : "gnuplot", "w");
if (!pipe) throw std::runtime_error("gnuplot pipe failed to open. Try install gnuplot first.");
}
gctl::gnuplot::~gnuplot()
{
if (pipe) pclose(pipe);
}
void gctl::gnuplot::to_buffer()
{
_2buffer = true;
return;
}
void gctl::gnuplot::send(const std::string& cmd, bool next_block)
{
if (!pipe) return;
if (!_2buffer) fputs((cmd + "\n").c_str(), pipe);
else
{
if (next_block) buffer.push_back(cmd + "\n\n");
else buffer.push_back(cmd + "\n");
}
return;
}
void gctl::gnuplot::send_buffer(size_t repeat)
{
if (!pipe) return;
for (size_t i = 0; i < repeat; i++)
{
for (auto& line : buffer) fputs(line.c_str(), pipe);
fputs("\n", pipe);
}
fflush(pipe);
buffer.clear();
return;
}
void gctl::gnuplot::save_buffer(const std::string& filename)
{
std::ofstream outfile;
open_outfile(outfile, filename, ".gp");
for (auto& line : buffer) outfile << line;
outfile.close();
return;
}

85
lib/graphic/gnuplot.h Normal file
View File

@ -0,0 +1,85 @@
/********************************************************
*
*
*
*
*
*
* 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.
******************************************************/
#ifndef _GCTL_GNUPLOT_H
#define _GCTL_GNUPLOT_H
#include "gctl/core.h"
#include "gctl/utility.h"
namespace gctl
{
class gnuplot
{
public:
/**
* @brief Construct a new gnuplot object
*
* @param persist Gnuplot will hold the window after plot if true.
*/
gnuplot(bool persist = true);
// destructor
virtual ~gnuplot();
/**
* @brief Set this to send all lines to buffer.
*/
void to_buffer();
/**
* @brief Send a line to gnuplot and execute it. If send2buffer is called before, then the line will be stored in the buffer.
*
* @param cmd command line.
* @param next_block If true, an empty line will be added to the buffer after the cmd.
*/
void send(const std::string& cmd, bool next_block = false);
/**
* @brief Send the buffer to gnuplot and execute it at once. Then clear the buffer.
*
* @param repeat How many times to repeat the buffer.
*/
void send_buffer(size_t repeat = 1);
/**
* @brief Save the buffer to a file. This will not clear the buffer.
*
* @param filename File name.
*/
void save_buffer(const std::string& filename);
private:
gnuplot(gnuplot const&) = delete;
void operator=(gnuplot const&) = delete;
FILE* pipe;
std::vector<std::string> buffer;
bool _2buffer;
};
}
#endif // _GCTL_GNUPLOT_H

View File

@ -99,7 +99,7 @@ int gctl::mathgl_dens::Draw(mglGraph *gr)
//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);
gr->Dens(z_d, "UbcyqR");
//if (file != "null")
//{

View File

@ -34,7 +34,7 @@
#include "gctl/core.h"
#include "gctl/maths.h"
#include "gctl/algorithm.h"
#include "gctl/algorithms.h"
#include "mgl2/mgl.h"
#include "mgl2/fltk.h"

View File

@ -34,7 +34,7 @@
#include "gctl/core.h"
#include "gctl/maths.h"
#include "gctl/algorithm.h"
#include "gctl/algorithms.h"
#include "mgl2/mgl.h"
#include "mgl2/fltk.h"