From 737b39fee3422e57269d2ed344bd87d7a7ce9ab3 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Thu, 16 Jan 2025 10:21:28 +0800 Subject: [PATCH 1/6] Update mathgl_dens.cpp --- lib/graphic/mathgl_dens.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/graphic/mathgl_dens.cpp b/lib/graphic/mathgl_dens.cpp index b12a295..13055d9 100644 --- a/lib/graphic/mathgl_dens.cpp +++ b/lib/graphic/mathgl_dens.cpp @@ -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") //{ From 22f8a6aaa2449e04aefdaa1e89ba3b40d6b88d89 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Thu, 23 Jan 2025 11:29:42 +0800 Subject: [PATCH 2/6] tmp --- example/CMakeLists.txt | 2 + example/gnuplot_ex.cpp | 70 +++++++++++++++++++++++++++++++++ lib/graphic.h | 1 + lib/graphic/gnuplot.cpp | 83 ++++++++++++++++++++++++++++++++++++++++ lib/graphic/gnuplot.h | 85 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 241 insertions(+) create mode 100644 example/gnuplot_ex.cpp create mode 100644 lib/graphic/gnuplot.cpp create mode 100644 lib/graphic/gnuplot.h diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index e20a0ef..e7551da 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -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() diff --git a/example/gnuplot_ex.cpp b/example/gnuplot_ex.cpp new file mode 100644 index 0000000..d689705 --- /dev/null +++ b/example/gnuplot_ex.cpp @@ -0,0 +1,70 @@ +/******************************************************** + * ██████╗ ██████╗████████╗██╗ + * ██╔════╝ ██╔════╝╚══██╔══╝██║ + * ██║ ███╗██║ ██║ ██║ + * ██║ ██║██║ ██║ ██║ + * ╚██████╔╝╚██████╗ ██║ ███████╗ + * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ + * 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 . + * + * 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(); + return 0; +} diff --git a/lib/graphic.h b/lib/graphic.h index eb26671..958a818 100644 --- a/lib/graphic.h +++ b/lib/graphic.h @@ -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 \ No newline at end of file diff --git a/lib/graphic/gnuplot.cpp b/lib/graphic/gnuplot.cpp new file mode 100644 index 0000000..c50106d --- /dev/null +++ b/lib/graphic/gnuplot.cpp @@ -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 . + * + * 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; +} \ No newline at end of file diff --git a/lib/graphic/gnuplot.h b/lib/graphic/gnuplot.h new file mode 100644 index 0000000..4cde491 --- /dev/null +++ b/lib/graphic/gnuplot.h @@ -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 . + * + * 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 buffer; + bool _2buffer; + }; +} + +#endif // _GCTL_GNUPLOT_H \ No newline at end of file From 2c8bedd847c94ebd501a95cd6a238b0c24396f4b Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Thu, 23 Jan 2025 12:14:15 +0800 Subject: [PATCH 3/6] tmp --- example/gnuplot_ex.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/example/gnuplot_ex.cpp b/example/gnuplot_ex.cpp index d689705..4f19df6 100644 --- a/example/gnuplot_ex.cpp +++ b/example/gnuplot_ex.cpp @@ -34,6 +34,7 @@ int main(int argc, char *argv[]) //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'"); @@ -66,5 +67,17 @@ int main(int argc, char *argv[]) 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; } From a6f89f092282e1f82227f9985f10181bd5451ef6 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Thu, 6 Feb 2025 21:34:14 +0800 Subject: [PATCH 4/6] tmp --- lib/graphic/gmt_JX_single.h | 1 - lib/graphic/mathgl_dens.h | 2 +- lib/graphic/mathgl_plot.h | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/graphic/gmt_JX_single.h b/lib/graphic/gmt_JX_single.h index 8b19acb..2386ff5 100644 --- a/lib/graphic/gmt_JX_single.h +++ b/lib/graphic/gmt_JX_single.h @@ -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 diff --git a/lib/graphic/mathgl_dens.h b/lib/graphic/mathgl_dens.h index 70c13b7..53f41e7 100644 --- a/lib/graphic/mathgl_dens.h +++ b/lib/graphic/mathgl_dens.h @@ -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" diff --git a/lib/graphic/mathgl_plot.h b/lib/graphic/mathgl_plot.h index 11adfff..9aac48b 100644 --- a/lib/graphic/mathgl_plot.h +++ b/lib/graphic/mathgl_plot.h @@ -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" From 3223f001a84e4b45413d184bf016ae27e888ee5a Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Sat, 8 Feb 2025 20:51:05 +0800 Subject: [PATCH 5/6] tmp --- CMakeLists.txt | 2 +- lib/graphic/gctl_graphic_config.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f61dc1..4ec5dea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/lib/graphic/gctl_graphic_config.h b/lib/graphic/gctl_graphic_config.h index 9042f93..2bad89f 100644 --- a/lib/graphic/gctl_graphic_config.h +++ b/lib/graphic/gctl_graphic_config.h @@ -1,2 +1,2 @@ #define GCTL_GRAPHIC_GMT -#define GCTL_GRAPHIC_MATHGL +/* #undef GCTL_GRAPHIC_MATHGL */ From 8bfc325ea65537425d7fdececebc64023a8087a4 Mon Sep 17 00:00:00 2001 From: Yi Zhang Date: Sun, 9 Feb 2025 21:01:39 +0800 Subject: [PATCH 6/6] tmp --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 59fa707..fe541de 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# gctl_graphic +# gctl_graphic (deprecated)