Urbanisme  1.3.0
Projet visant à l'optimisation du placement de routes sur une surface. https://flodavid.github.io/Urbanisme
gnuplot_i.hpp
1 
29 
30 #ifndef _GNUPLOT_PIPES_H_
31 #define _GNUPLOT_PIPES_H_
32 
33 
34 #include <iostream>
35 #include <string>
36 #include <vector>
37 #include <fstream>
38 #include <sstream> // for std::ostringstream
39 #include <stdexcept>
40 #include <cstdio>
41 #include <cstdlib> // for getenv()
42 #include <list> // for std::list
43 
44 
45 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
46 //defined for 32 and 64-bit environments
47  #include <io.h> // for _access(), _mktemp()
48  #define GP_MAX_TMP_FILES 27 // 27 temporary files it's Microsoft restriction
49 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
50 //all UNIX-like OSs (Linux, *BSD, MacOSX, Solaris, ...)
51  #include <unistd.h> // for access(), mkstemp()
52  #define GP_MAX_TMP_FILES 64
53 #else
54  #error unsupported or unknown operating system
55 #endif
56 
57 //declare classes in global namespace
58 
59 
60 class GnuplotException : public std::runtime_error
61 {
62  public:
67  explicit GnuplotException(const std::string &msg) : std::runtime_error(msg){}
68 };
69 
70 
75 class Gnuplot
76 {
77  private:
78 
79  //----------------------------------------------------------------------------------
80  // member data
82  FILE *gnucmd;
84  bool valid;
86  bool two_dim;
88  int nplots;
90  std::string pstyle;
92  std::string smooth;
94  std::vector<std::string> tmpfile_list;
95 
96  //----------------------------------------------------------------------------------
97  // static data
99  static int tmpfile_num;
101  static std::string m_sGNUPlotFileName;
103  static std::string m_sGNUPlotPath;
105  static std::string terminal_std;
106 
107  //----------------------------------------------------------------------------------
108  // member functions (auxiliary functions)
109  // ---------------------------------------------------
111  // ---------------------------------------------------
112  void init();
113  // ---------------------------------------------------
119  // ---------------------------------------------------
120  std::string create_tmpfile(std::ofstream &tmp);
121 
122  //----------------------------------------------------------------------------------
126  // ---------------------------------------------------------------------------------
127  static bool get_program_path();
128 
129  // ---------------------------------------------------------------------------------
136  // ---------------------------------------------------------------------------------
137  bool file_available(const std::string &filename);
138 
139  // ---------------------------------------------------------------------------------
146  // ---------------------------------------------------------------------------------
147  static bool file_exists(const std::string &filename, int mode=0);
148 
149  public:
150 
151  // ----------------------------------------------------------------------------
158  // ----------------------------------------------------------------------------
159  static bool set_GNUPlotPath(const std::string &path);
160 
161 
162  // ----------------------------------------------------------------------------
169  // ----------------------------------------------------------------------------
170  static void set_terminal_std(const std::string &type);
171 
172  //-----------------------------------------------------------------------------
173  // constructors
174  // ----------------------------------------------------------------------------
175 
176 
178  explicit Gnuplot(const std::string &style = "points");
179 
181  Gnuplot(const std::vector<double> &x,
182  const std::string &title = "",
183  const std::string &style = "points",
184  const std::string &labelx = "x",
185  const std::string &labely = "y");
186 
188  Gnuplot(const std::vector<double> &x,
189  const std::vector<double> &y,
190  const std::string &title = "",
191  const std::string &style = "points",
192  const std::string &labelx = "x",
193  const std::string &labely = "y");
194 
196  Gnuplot(const std::vector<double> &x,
197  const std::vector<double> &y,
198  const std::vector<double> &z,
199  const std::string &title = "",
200  const std::string &style = "points",
201  const std::string &labelx = "x",
202  const std::string &labely = "y",
203  const std::string &labelz = "z");
204 
206  ~Gnuplot();
207 
208 
209  //----------------------------------------------------------------------------------
210 
212  Gnuplot& cmd(const std::string &cmdstr);
213  // ---------------------------------------------------------------------------------
220  // ---------------------------------------------------------------------------------
221  inline Gnuplot& operator<<(const std::string &cmdstr){
222  cmd(cmdstr);
223  return(*this);
224  }
225 
226 
227 
228  //----------------------------------------------------------------------------------
229  // show on screen or write to file
230 
232  Gnuplot& showonscreen(); // window output is set by default (win/x11/aqua)
233 
235  Gnuplot& savetops(const std::string &filename = "gnuplot_output");
236 
237 
238  //----------------------------------------------------------------------------------
239  // set and unset
240 
244  Gnuplot& set_style(const std::string &stylestr = "points");
245 
250  Gnuplot& set_smooth(const std::string &stylestr = "csplines");
251 
252  // ----------------------------------------------------------------------
257  // ----------------------------------------------------------------------
258  inline Gnuplot& unset_smooth(){ smooth = ""; return *this;}
259 
260 
262  Gnuplot& set_pointsize(const double pointsize = 1.0);
263 
265  inline Gnuplot& set_grid() {cmd("set grid");return *this;}
267  inline Gnuplot& unset_grid(){cmd("unset grid");return *this;}
268 
269  // -----------------------------------------------
273  // -----------------------------------------------
274  inline Gnuplot& set_multiplot(){cmd("set multiplot") ;return *this;}
275 
276  // -----------------------------------------------
280  // -----------------------------------------------
281  inline Gnuplot& unset_multiplot(){cmd("unset multiplot");return *this;}
282 
283 
284 
286  Gnuplot& set_samples(const int samples = 100);
288  Gnuplot& set_isosamples(const int isolines = 10);
289 
290  // --------------------------------------------------------------------------
294  // --------------------------------------------------------------------------
295  Gnuplot& set_hidden3d(){cmd("set hidden3d");return *this;}
296 
297  // ---------------------------------------------------------------------------
301  // ---------------------------------------------------------------------------
302  inline Gnuplot& unset_hidden3d(){cmd("unset hidden3d"); return *this;}
305  Gnuplot& set_contour(const std::string &position = "base");
306  // --------------------------------------------------------------------------
310  // ------------------------------------------------------------------
311  inline Gnuplot& unset_contour(){cmd("unset contour");return *this;}
312 
313  // ------------------------------------------------------------
317  // ------------------------------------------------------------------
318  inline Gnuplot& set_surface(){cmd("set surface");return *this;}
319 
320  // ----------------------------------------------------------
325  // ------------------------------------------------------------------
326  inline Gnuplot& unset_surface(){cmd("unset surface"); return *this;}
327 
328 
331  Gnuplot& set_legend(const std::string &position = "default");
332 
333  // ------------------------------------------------------------------
338  // ------------------------------------------------------------------
339  inline Gnuplot& unset_legend(){cmd("unset key"); return *this;}
340 
341  // -----------------------------------------------------------------------
347  // -----------------------------------------------------------------------
348  inline Gnuplot& set_title(const std::string &title = "")
349  {
350  std::string cmdstr;
351  cmdstr = "set title \"";
352  cmdstr+=title;
353  cmdstr+="\"";
354  *this<<cmdstr;
355  return *this;
356  }
357 
358  //----------------------------------------------------------------------------------
363  // ---------------------------------------------------------------------------------
364  inline Gnuplot& unset_title(){this->set_title();return *this;}
365 
366 
368  Gnuplot& set_ylabel(const std::string &label = "x");
370  Gnuplot& set_xlabel(const std::string &label = "y");
372  Gnuplot& set_zlabel(const std::string &label = "z");
373 
375  Gnuplot& set_xrange(const double iFrom,
376  const double iTo);
378  Gnuplot& set_yrange(const double iFrom,
379  const double iTo);
381  Gnuplot& set_zrange(const double iFrom,
382  const double iTo);
386  // -----------------------------------------------
387  inline Gnuplot& set_xautoscale(){cmd("set xrange restore");cmd("set autoscale x");return *this;}
388 
389  // -----------------------------------------------
393  // -----------------------------------------------
394  inline Gnuplot& set_yautoscale(){cmd("set yrange restore");cmd("set autoscale y");return *this;}
395 
396  // -----------------------------------------------
400  // -----------------------------------------------
401  inline Gnuplot& set_zautoscale(){cmd("set zrange restore");cmd("set autoscale z");return *this;}
402 
403 
405  Gnuplot& set_xlogscale(const double base = 10);
407  Gnuplot& set_ylogscale(const double base = 10);
409  Gnuplot& set_zlogscale(const double base = 10);
410 
411  // -----------------------------------------------
415  // -----------------------------------------------
416  inline Gnuplot& unset_xlogscale(){cmd("unset logscale x"); return *this;}
417 
418  // -----------------------------------------------
422  // -----------------------------------------------
423  inline Gnuplot& unset_ylogscale(){cmd("unset logscale y"); return *this;}
424 
425  // -----------------------------------------------
429  // -----------------------------------------------
430  inline Gnuplot& unset_zlogscale(){cmd("unset logscale z"); return *this;}
431 
432 
434  Gnuplot& set_cbrange(const double iFrom, const double iTo);
435 
436 
437  //----------------------------------------------------------------------------------
438  // plot
439 
442  Gnuplot& plotfile_x(const std::string &filename,
443  const unsigned int column = 1,
444  const std::string &title = "");
446  template<typename X>
447  Gnuplot& plot_x(const X& x, const std::string &title = "");
448 
449 
452  Gnuplot& plotfile_xy(const std::string &filename,
453  const unsigned int column_x = 1,
454  const unsigned int column_y = 2,
455  const std::string &title = "");
457  template<typename X, typename Y>
458  Gnuplot& plot_xy(const X& x, const Y& y, const std::string &title = "");
459 
460 
463  Gnuplot& plotfile_xy_err(const std::string &filename,
464  const unsigned int column_x = 1,
465  const unsigned int column_y = 2,
466  const unsigned int column_dy = 3,
467  const std::string &title = "");
469  template<typename X, typename Y, typename E>
470  Gnuplot& plot_xy_err(const X &x, const Y &y, const E &dy,
471  const std::string &title = "");
472 
473 
476  Gnuplot& plotfile_xyz(const std::string &filename,
477  const unsigned int column_x = 1,
478  const unsigned int column_y = 2,
479  const unsigned int column_z = 3,
480  const std::string &title = "");
482  template<typename X, typename Y, typename Z>
483  Gnuplot& plot_xyz(const X &x,
484  const Y &y,
485  const Z &z,
486  const std::string &title = "");
487 
488 
489 
491  Gnuplot& plot_slope(const double a,
492  const double b,
493  const std::string &title = "");
494 
495 
506  Gnuplot& plot_equation(const std::string &equation,
507  const std::string &title = "");
508 
511  Gnuplot& plot_equation3d(const std::string &equation,
512  const std::string &title = "");
513 
514 
516  Gnuplot& plot_image(const unsigned char *ucPicBuf,
517  const unsigned int iWidth,
518  const unsigned int iHeight,
519  const std::string &title = "");
520 
521 
522  //----------------------------------------------------------------------------------
526  //----------------------------------------------------------------------------------
527  inline Gnuplot& replot(void){if (nplots > 0) cmd("replot"); return *this;}
528 
530  Gnuplot& reset_plot();
531 
533  Gnuplot& reset_all();
534 
536  void remove_tmpfiles();
537 
538  // -------------------------------------------------------------------
542  // -------------------------------------------------------------------
543  inline bool is_valid(){return(valid);}
544 
545 };
546 
547 //------------------------------------------------------------------------------
548 //
549 // initialize static data
550 //
551 int Gnuplot::tmpfile_num = 0;
552 
553 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
554 std::string Gnuplot::m_sGNUPlotFileName = "gnuplot.exe";
555 std::string Gnuplot::m_sGNUPlotPath = "C:/program files/gnuplot/bin/";
556 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
557 std::string Gnuplot::m_sGNUPlotFileName = "gnuplot";
558 std::string Gnuplot::m_sGNUPlotPath = "/usr/local/bin/";
559 #endif
560 
561 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
562 std::string Gnuplot::terminal_std = "windows";
563 #elif ( defined(unix) || defined(__unix) || defined(__unix__) ) && !defined(__APPLE__)
564 std::string Gnuplot::terminal_std = "x11";
565 #elif defined(__APPLE__)
566 std::string Gnuplot::terminal_std = "aqua";
567 #endif
568 
569 //------------------------------------------------------------------------------
570 //
571 // constructor: set a style during construction
572 //
573 inline Gnuplot::Gnuplot(const std::string &style)
574  :gnucmd(NULL) ,valid(false) ,two_dim(false) ,nplots(0)
575 
576 {
577  init();
578  set_style(style);
579 }
580 
581 //------------------------------------------------------------------------------
582 //
583 // constructor: open a new session, plot a signal (x)
584 //
585 inline Gnuplot::Gnuplot(const std::vector<double> &x,
586  const std::string &title,
587  const std::string &style,
588  const std::string &labelx,
589  const std::string &labely)
590  :gnucmd(NULL) ,valid(false) ,two_dim(false) ,nplots(0)
591 {
592  init();
593 
594  set_style(style);
595  set_xlabel(labelx);
596  set_ylabel(labely);
597 
598  plot_x(x,title);
599 }
600 
601 
602 //------------------------------------------------------------------------------
603 //
604 // constructor: open a new session, plot a signal (x,y)
605 //
606 inline Gnuplot::Gnuplot(const std::vector<double> &x,
607  const std::vector<double> &y,
608  const std::string &title,
609  const std::string &style,
610  const std::string &labelx,
611  const std::string &labely)
612  :gnucmd(NULL) ,valid(false) ,two_dim(false) ,nplots(0)
613 {
614  init();
615 
616  set_style(style);
617  set_xlabel(labelx);
618  set_ylabel(labely);
619 
620  plot_xy(x,y,title);
621 }
622 
623 
624 //------------------------------------------------------------------------------
625 //
626 // constructor: open a new session, plot a signal (x,y,z)
627 //
628 inline Gnuplot::Gnuplot(const std::vector<double> &x,
629  const std::vector<double> &y,
630  const std::vector<double> &z,
631  const std::string &title,
632  const std::string &style,
633  const std::string &labelx,
634  const std::string &labely,
635  const std::string &labelz)
636  :gnucmd(NULL) ,valid(false) ,two_dim(false) ,nplots(0)
637 {
638  init();
639 
640  set_style(style);
641  set_xlabel(labelx);
642  set_ylabel(labely);
643  set_zlabel(labelz);
644 
645  plot_xyz(x,y,z,title);
646 }
647 
648 
649 //------------------------------------------------------------------------------
650 //
652 //
653 template<typename X>
654 Gnuplot& Gnuplot::plot_x(const X& x, const std::string &title)
655 {
656  if (x.size() == 0)
657  {
658  throw GnuplotException("std::vector too small");
659  return *this;
660  }
661 
662  std::ofstream tmp;
663  std::string name = create_tmpfile(tmp);
664  if (name == "")
665  return *this;
666 
667  //
668  // write the data to file
669  //
670  for (unsigned int i = 0; i < x.size(); i++)
671  tmp << x[i] << std::endl;
672 
673  tmp.flush();
674  tmp.close();
675 
676 
677  plotfile_x(name, 1, title);
678 
679  return *this;
680 }
681 
682 
683 //------------------------------------------------------------------------------
684 //
686 //
687 template<typename X, typename Y>
688 Gnuplot& Gnuplot::plot_xy(const X& x, const Y& y, const std::string &title)
689 {
690  if (x.size() == 0 || y.size() == 0)
691  {
692  throw GnuplotException("std::vectors too small");
693  return *this;
694  }
695 
696  if (x.size() != y.size())
697  {
698  throw GnuplotException("Length of the std::vectors differs");
699  return *this;
700  }
701 
702 
703  std::ofstream tmp;
704  std::string name = create_tmpfile(tmp);
705  if (name == "")
706  return *this;
707 
708  //
709  // write the data to file
710  //
711  for (unsigned int i = 0; i < x.size(); i++)
712  tmp << x[i] << " " << y[i] << std::endl;
713 
714  tmp.flush();
715  tmp.close();
716 
717 
718  plotfile_xy(name, 1, 2, title);
719 
720  return *this;
721 }
722 
727 template<typename X, typename Y, typename E>
729  const Y &y,
730  const E &dy,
731  const std::string &title)
732 {
733  if (x.size() == 0 || y.size() == 0 || dy.size() == 0)
734  {
735  throw GnuplotException("std::vectors too small");
736  return *this;
737  }
738 
739  if (x.size() != y.size() || y.size() != dy.size())
740  {
741  throw GnuplotException("Length of the std::vectors differs");
742  return *this;
743  }
744 
745 
746  std::ofstream tmp;
747  std::string name = create_tmpfile(tmp);
748  if (name == "")
749  return *this;
750 
751  //
752  // write the data to file
753  //
754  for (unsigned int i = 0; i < x.size(); i++)
755  tmp << x[i] << " " << y[i] << " " << dy[i] << std::endl;
756 
757  tmp.flush();
758  tmp.close();
759 
760 
761  // Do the actual plot
762  plotfile_xy_err(name, 1, 2, 3, title);
763 
764  return *this;
765 }
766 
767 
768 //------------------------------------------------------------------------------
769 //
770 // Plots a 3d graph from a list of doubles: x y z
771 //
772 template<typename X, typename Y, typename Z>
774  const Y &y,
775  const Z &z,
776  const std::string &title)
777 {
778  if (x.size() == 0 || y.size() == 0 || z.size() == 0)
779  {
780  throw GnuplotException("std::vectors too small");
781  return *this;
782  }
783 
784  if (x.size() != y.size() || x.size() != z.size())
785  {
786  throw GnuplotException("Length of the std::vectors differs");
787  return *this;
788  }
789 
790 
791  std::ofstream tmp;
792  std::string name = create_tmpfile(tmp);
793  if (name == "")
794  return *this;
795 
796  //
797  // write the data to file
798  //
799  for (unsigned int i = 0; i < x.size(); i++)
800  tmp << x[i] << " " << y[i] << " " << z[i] <<std::endl;
801 
802  tmp.flush();
803  tmp.close();
804 
805 
806  plotfile_xyz(name, 1, 2, 3, title);
807 
808  return *this;
809 }
810 
811 
812 //------------------------------------------------------------------------------
813 //
814 // define static member function: set Gnuplot path manual
815 // for windows: path with slash '/' not backslash '\'
816 //
817 bool Gnuplot::set_GNUPlotPath(const std::string &path)
818 {
819 
820  std::string tmp = path + "/" + Gnuplot::m_sGNUPlotFileName;
821 
822 
823 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
824  if ( Gnuplot::file_exists(tmp,0) ) // check existence
825 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
826  if ( Gnuplot::file_exists(tmp,1) ) // check existence and execution permission
827 #endif
828  {
829  Gnuplot::m_sGNUPlotPath = path;
830  return true;
831  }
832  else
833  {
834  Gnuplot::m_sGNUPlotPath.clear();
835  return false;
836  }
837 }
838 
839 
840 //------------------------------------------------------------------------------
841 //
842 // define static member function: set standart terminal, used by showonscreen
843 // defaults: Windows - win, Linux - x11, Mac - aqua
844 //
845 void Gnuplot::set_terminal_std(const std::string &type)
846 {
847 #if defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
848  if (type.find("x11") != std::string::npos && getenv("DISPLAY") == NULL)
849  {
850  throw GnuplotException("Can't find DISPLAY variable");
851  }
852 #endif
853 
854 
855  Gnuplot::terminal_std = type;
856  return;
857 }
858 
859 
860 //------------------------------------------------------------------------------
861 //
862 // A string tokenizer taken from http://www.sunsite.ualberta.ca/Documentation/
863 // /Gnu/libstdc++-2.90.8/html/21_strings/stringtok_std_h.txt
864 //
865 template <typename Container>
866 void stringtok (Container &container,
867  std::string const &in,
868  const char * const delimiters = " \t\n")
869 {
870  const std::string::size_type len = in.length();
871  std::string::size_type i = 0;
872 
873  while ( i < len )
874  {
875  // eat leading whitespace
876  i = in.find_first_not_of (delimiters, i);
877 
878  if (i == std::string::npos)
879  return; // nothing left but white space
880 
881  // find the end of the token
882  std::string::size_type j = in.find_first_of (delimiters, i);
883 
884  // push token
885  if (j == std::string::npos)
886  {
887  container.push_back (in.substr(i));
888  return;
889  }
890  else
891  container.push_back (in.substr(i, j-i));
892 
893  // set up for next loop
894  i = j + 1;
895  }
896 
897  return;
898 }
899 
900 
901 //------------------------------------------------------------------------------
902 //
903 // Destructor: needed to delete temporary files
904 //
906 {
907 // remove_tmpfiles();
908 
909  // A stream opened by popen() should be closed by pclose()
910 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
911  if (_pclose(gnucmd) == -1)
912 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
913  if (pclose(gnucmd) == -1)
914 #endif
915  throw GnuplotException("Problem closing communication to gnuplot");
916 }
917 
918 
919 //------------------------------------------------------------------------------
920 //
921 // Resets a gnuplot session (next plot will erase previous ones)
922 //
924 {
925 // remove_tmpfiles();
926 
927  nplots = 0;
928 
929  return *this;
930 }
931 
932 
933 //------------------------------------------------------------------------------
934 //
935 // resets a gnuplot session and sets all varibles to default
936 //
938 {
939 // remove_tmpfiles();
940 
941  nplots = 0;
942  cmd("reset");
943  cmd("clear");
944  pstyle = "points";
945  smooth = "";
946  showonscreen();
947 
948  return *this;
949 }
950 
951 
952 //------------------------------------------------------------------------------
953 //
954 // Change the plotting style of a gnuplot session
955 //
956 Gnuplot& Gnuplot::set_style(const std::string &stylestr)
957 {
958  if (stylestr.find("lines") == std::string::npos &&
959  stylestr.find("points") == std::string::npos &&
960  stylestr.find("linespoints") == std::string::npos &&
961  stylestr.find("impulses") == std::string::npos &&
962  stylestr.find("dots") == std::string::npos &&
963  stylestr.find("steps") == std::string::npos &&
964  stylestr.find("fsteps") == std::string::npos &&
965  stylestr.find("histeps") == std::string::npos &&
966  stylestr.find("boxes") == std::string::npos && // 1-4 columns of data are required
967  stylestr.find("filledcurves") == std::string::npos &&
968  stylestr.find("histograms") == std::string::npos ) //only for one data column
969 // stylestr.find("labels") == std::string::npos && // 3 columns of data are required
970 // stylestr.find("xerrorbars") == std::string::npos && // 3-4 columns of data are required
971 // stylestr.find("xerrorlines") == std::string::npos && // 3-4 columns of data are required
972 // stylestr.find("errorbars") == std::string::npos && // 3-4 columns of data are required
973 // stylestr.find("errorlines") == std::string::npos && // 3-4 columns of data are required
974 // stylestr.find("yerrorbars") == std::string::npos && // 3-4 columns of data are required
975 // stylestr.find("yerrorlines") == std::string::npos && // 3-4 columns of data are required
976 // stylestr.find("boxerrorbars") == std::string::npos && // 3-5 columns of data are required
977 // stylestr.find("xyerrorbars") == std::string::npos && // 4,6,7 columns of data are required
978 // stylestr.find("xyerrorlines") == std::string::npos && // 4,6,7 columns of data are required
979 // stylestr.find("boxxyerrorbars") == std::string::npos && // 4,6,7 columns of data are required
980 // stylestr.find("financebars") == std::string::npos && // 5 columns of data are required
981 // stylestr.find("candlesticks") == std::string::npos && // 5 columns of data are required
982 // stylestr.find("vectors") == std::string::npos &&
983 // stylestr.find("image") == std::string::npos &&
984 // stylestr.find("rgbimage") == std::string::npos &&
985 // stylestr.find("pm3d") == std::string::npos )
986  {
987  pstyle = std::string("points");
988  }
989  else
990  {
991  pstyle = stylestr;
992  }
993 
994  return *this;
995 }
996 
997 
998 //------------------------------------------------------------------------------
999 //
1000 // smooth: interpolation and approximation of data
1001 //
1002 Gnuplot& Gnuplot::set_smooth(const std::string &stylestr)
1003 {
1004  if (stylestr.find("unique") == std::string::npos &&
1005  stylestr.find("frequency") == std::string::npos &&
1006  stylestr.find("csplines") == std::string::npos &&
1007  stylestr.find("acsplines") == std::string::npos &&
1008  stylestr.find("bezier") == std::string::npos &&
1009  stylestr.find("sbezier") == std::string::npos )
1010  {
1011  smooth = "";
1012  }
1013  else
1014  {
1015  smooth = stylestr;
1016  }
1017 
1018  return *this;
1019 }
1020 
1021 
1022 //------------------------------------------------------------------------------
1023 //
1024 // sets terminal type to windows / x11
1025 //
1027 {
1028  cmd("set output");
1029  cmd("set terminal " + Gnuplot::terminal_std);
1030 
1031  return *this;
1032 }
1033 
1034 //------------------------------------------------------------------------------
1035 //
1036 // saves a gnuplot session to a postscript file
1037 //
1038 Gnuplot& Gnuplot::savetops(const std::string &filename)
1039 {
1040  cmd("set terminal postscript color");
1041 
1042  std::ostringstream cmdstr;
1043  cmdstr << "set output \"" << filename << ".ps\"";
1044  cmd(cmdstr.str());
1045 
1046  return *this;
1047 }
1048 
1049 //------------------------------------------------------------------------------
1050 //
1051 // Switches legend on
1052 //
1053 Gnuplot& Gnuplot::set_legend(const std::string &position)
1054 {
1055  std::ostringstream cmdstr;
1056  cmdstr << "set key " << position;
1057 
1058  cmd(cmdstr.str());
1059 
1060  return *this;
1061 }
1062 
1063 //------------------------------------------------------------------------------
1064 //
1065 // turns on log scaling for the x axis
1066 //
1067 Gnuplot& Gnuplot::set_xlogscale(const double base)
1068 {
1069  std::ostringstream cmdstr;
1070 
1071  cmdstr << "set logscale x " << base;
1072  cmd(cmdstr.str());
1073 
1074  return *this;
1075 }
1076 
1077 //------------------------------------------------------------------------------
1078 //
1079 // turns on log scaling for the y axis
1080 //
1081 Gnuplot& Gnuplot::set_ylogscale(const double base)
1082 {
1083  std::ostringstream cmdstr;
1084 
1085  cmdstr << "set logscale y " << base;
1086  cmd(cmdstr.str());
1087 
1088  return *this;
1089 }
1090 
1091 //------------------------------------------------------------------------------
1092 //
1093 // turns on log scaling for the z axis
1094 //
1095 Gnuplot& Gnuplot::set_zlogscale(const double base)
1096 {
1097  std::ostringstream cmdstr;
1098 
1099  cmdstr << "set logscale z " << base;
1100  cmd(cmdstr.str());
1101 
1102  return *this;
1103 }
1104 
1105 //------------------------------------------------------------------------------
1106 //
1107 // scales the size of the points used in plots
1108 //
1109 Gnuplot& Gnuplot::set_pointsize(const double pointsize)
1110 {
1111  std::ostringstream cmdstr;
1112  cmdstr << "set pointsize " << pointsize;
1113  cmd(cmdstr.str());
1114 
1115  return *this;
1116 }
1117 
1118 //------------------------------------------------------------------------------
1119 //
1120 // set isoline density (grid) for plotting functions as surfaces
1121 //
1122 Gnuplot& Gnuplot::set_samples(const int samples)
1123 {
1124  std::ostringstream cmdstr;
1125  cmdstr << "set samples " << samples;
1126  cmd(cmdstr.str());
1127 
1128  return *this;
1129 }
1130 
1131 
1132 //------------------------------------------------------------------------------
1133 //
1134 // set isoline density (grid) for plotting functions as surfaces
1135 //
1136 Gnuplot& Gnuplot::set_isosamples(const int isolines)
1137 {
1138  std::ostringstream cmdstr;
1139  cmdstr << "set isosamples " << isolines;
1140  cmd(cmdstr.str());
1141 
1142  return *this;
1143 }
1144 
1145 
1146 //------------------------------------------------------------------------------
1147 //
1148 // enables contour drawing for surfaces set contour {base | surface | both}
1149 //
1150 
1151 Gnuplot& Gnuplot::set_contour(const std::string &position)
1152 {
1153  if (position.find("base") == std::string::npos &&
1154  position.find("surface") == std::string::npos &&
1155  position.find("both") == std::string::npos )
1156  {
1157  cmd("set contour base");
1158  }
1159  else
1160  {
1161  cmd("set contour " + position);
1162  }
1163 
1164  return *this;
1165 }
1166 
1167 //------------------------------------------------------------------------------
1168 //
1169 // set labels
1170 //
1171 // set the xlabel
1172 Gnuplot& Gnuplot::set_xlabel(const std::string &label)
1173 {
1174  std::ostringstream cmdstr;
1175 
1176  cmdstr << "set xlabel \"" << label << "\"";
1177  cmd(cmdstr.str());
1178 
1179  return *this;
1180 }
1181 
1182 //------------------------------------------------------------------------------
1183 // set the ylabel
1184 //
1185 Gnuplot& Gnuplot::set_ylabel(const std::string &label)
1186 {
1187  std::ostringstream cmdstr;
1188 
1189  cmdstr << "set ylabel \"" << label << "\"";
1190  cmd(cmdstr.str());
1191 
1192  return *this;
1193 }
1194 
1195 //------------------------------------------------------------------------------
1196 // set the zlabel
1197 //
1198 Gnuplot& Gnuplot::set_zlabel(const std::string &label)
1199 {
1200  std::ostringstream cmdstr;
1201 
1202  cmdstr << "set zlabel \"" << label << "\"";
1203  cmd(cmdstr.str());
1204 
1205  return *this;
1206 }
1207 
1208 //------------------------------------------------------------------------------
1209 //
1210 // set range
1211 //
1212 // set the xrange
1213 Gnuplot& Gnuplot::set_xrange(const double iFrom,
1214  const double iTo)
1215 {
1216  std::ostringstream cmdstr;
1217 
1218  cmdstr << "set xrange[" << iFrom << ":" << iTo << "]";
1219  cmd(cmdstr.str());
1220 
1221  return *this;
1222 }
1223 
1224 //------------------------------------------------------------------------------
1225 // set the yrange
1226 //
1227 Gnuplot& Gnuplot::set_yrange(const double iFrom,
1228  const double iTo)
1229 {
1230  std::ostringstream cmdstr;
1231 
1232  cmdstr << "set yrange[" << iFrom << ":" << iTo << "]";
1233  cmd(cmdstr.str());
1234 
1235  return *this;
1236 }
1237 
1238 //------------------------------------------------------------------------------
1239 // set the zrange
1240 //
1241 Gnuplot& Gnuplot::set_zrange(const double iFrom,
1242  const double iTo)
1243 {
1244  std::ostringstream cmdstr;
1245 
1246  cmdstr << "set zrange[" << iFrom << ":" << iTo << "]";
1247  cmd(cmdstr.str());
1248 
1249  return *this;
1250 }
1251 
1252 //------------------------------------------------------------------------------
1253 //
1254 // set the palette range
1255 //
1256 Gnuplot& Gnuplot::set_cbrange(const double iFrom,
1257  const double iTo)
1258 {
1259  std::ostringstream cmdstr;
1260 
1261  cmdstr << "set cbrange[" << iFrom << ":" << iTo << "]";
1262  cmd(cmdstr.str());
1263 
1264  return *this;
1265 }
1266 
1267 //------------------------------------------------------------------------------
1268 //
1269 // Plots a linear equation y=ax+b (where you supply the
1270 // slope a and intercept b)
1271 //
1273  const double b,
1274  const std::string &title)
1275 {
1276  std::ostringstream cmdstr;
1277  //
1278  // command to be sent to gnuplot
1279  //
1280  if (nplots > 0 && two_dim == true)
1281  cmdstr << "replot ";
1282  else
1283  cmdstr << "plot ";
1284 
1285  cmdstr << a << " * x + " << b << " title \"";
1286 
1287  if (title == "")
1288  cmdstr << "f(x) = " << a << " * x + " << b;
1289  else
1290  cmdstr << title;
1291 
1292  cmdstr << "\" with " << pstyle;
1293 
1294  //
1295  // Do the actual plot
1296  //
1297  cmd(cmdstr.str());
1298 
1299  return *this;
1300 }
1301 
1302 //------------------------------------------------------------------------------
1303 //
1304 // Plot an equation supplied as a std::string y=f(x) (only f(x) expected)
1305 //
1306 Gnuplot& Gnuplot::plot_equation(const std::string &equation,
1307  const std::string &title)
1308 {
1309  std::ostringstream cmdstr;
1310  //
1311  // command to be sent to gnuplot
1312  //
1313  if (nplots > 0 && two_dim == true)
1314  cmdstr << "replot ";
1315  else
1316  cmdstr << "plot ";
1317 
1318  cmdstr << equation << " title \"";
1319 
1320  if (title == "")
1321  cmdstr << "f(x) = " << equation;
1322  else
1323  cmdstr << title;
1324 
1325  cmdstr << "\" with " << pstyle;
1326 
1327  //
1328  // Do the actual plot
1329  //
1330  cmd(cmdstr.str());
1331 
1332  return *this;
1333 }
1334 
1335 //------------------------------------------------------------------------------
1336 //
1337 // plot an equation supplied as a std::string y=(x)
1338 //
1339 Gnuplot& Gnuplot::plot_equation3d(const std::string &equation,
1340  const std::string &title)
1341 {
1342  std::ostringstream cmdstr;
1343  //
1344  // command to be sent to gnuplot
1345  //
1346  if (nplots > 0 && two_dim == false)
1347  cmdstr << "replot ";
1348  else
1349  cmdstr << "splot ";
1350 
1351  cmdstr << equation << " title \"";
1352 
1353  if (title == "")
1354  cmdstr << "f(x,y) = " << equation;
1355  else
1356  cmdstr << title;
1357 
1358  cmdstr << "\" with " << pstyle;
1359 
1360  //
1361  // Do the actual plot
1362  //
1363  cmd(cmdstr.str());
1364 
1365  return *this;
1366 }
1367 
1368 
1369 //------------------------------------------------------------------------------
1370 //
1371 // Plots a 2d graph from a list of doubles (x) saved in a file
1372 //
1373 Gnuplot& Gnuplot::plotfile_x(const std::string &filename,
1374  const unsigned int column,
1375  const std::string &title)
1376 {
1377  //
1378  // check if file exists
1379  //
1380  file_available(filename);
1381 
1382 
1383  std::ostringstream cmdstr;
1384  //
1385  // command to be sent to gnuplot
1386  //
1387  if (nplots > 0 && two_dim == true)
1388  cmdstr << "replot ";
1389  else
1390  cmdstr << "plot ";
1391 
1392  cmdstr << "\"" << filename << "\" using " << column;
1393 
1394  if (title == "")
1395  cmdstr << " notitle ";
1396  else
1397  cmdstr << " title \"" << title << "\" ";
1398 
1399  if(smooth == "")
1400  cmdstr << "with " << pstyle;
1401  else
1402  cmdstr << "smooth " << smooth;
1403 
1404  //
1405  // Do the actual plot
1406  //
1407  cmd(cmdstr.str()); //nplots++; two_dim = true; already in cmd();
1408 
1409  return *this;
1410 }
1411 
1412 
1413 
1414 //------------------------------------------------------------------------------
1415 //
1416 // Plots a 2d graph from a list of doubles (x y) saved in a file
1417 //
1418 Gnuplot& Gnuplot::plotfile_xy(const std::string &filename,
1419  const unsigned int column_x,
1420  const unsigned int column_y,
1421  const std::string &title)
1422 {
1423  //
1424  // check if file exists
1425  //
1426  file_available(filename);
1427 
1428 
1429  std::ostringstream cmdstr;
1430  //
1431  // command to be sent to gnuplot
1432  //
1433  if (nplots > 0 && two_dim == true)
1434  cmdstr << "replot ";
1435  else
1436  cmdstr << "plot ";
1437 
1438  cmdstr << "\"" << filename << "\" using " << column_x << ":" << column_y;
1439 
1440  if (title == "")
1441  cmdstr << " notitle ";
1442  else
1443  cmdstr << " title \"" << title << "\" ";
1444 
1445  if(smooth == "")
1446  cmdstr << "with " << pstyle;
1447  else
1448  cmdstr << "smooth " << smooth;
1449 
1450  //
1451  // Do the actual plot
1452  //
1453  cmd(cmdstr.str());
1454 
1455  return *this;
1456 }
1457 
1458 
1459 //------------------------------------------------------------------------------
1460 //
1461 // Plots a 2d graph with errorbars from a list of doubles (x y dy) in a file
1462 //
1463 Gnuplot& Gnuplot::plotfile_xy_err(const std::string &filename,
1464  const unsigned int column_x,
1465  const unsigned int column_y,
1466  const unsigned int column_dy,
1467  const std::string &title)
1468 {
1469  //
1470  // check if file exists
1471  //
1472  file_available(filename);
1473 
1474  std::ostringstream cmdstr;
1475  //
1476  // command to be sent to gnuplot
1477  //
1478  if (nplots > 0 && two_dim == true)
1479  cmdstr << "replot ";
1480  else
1481  cmdstr << "plot ";
1482 
1483  cmdstr << "\"" << filename << "\" using "
1484  << column_x << ":" << column_y << ":" << column_dy
1485  << " with errorbars ";
1486 
1487  if (title == "")
1488  cmdstr << " notitle ";
1489  else
1490  cmdstr << " title \"" << title << "\" ";
1491 
1492  //
1493  // Do the actual plot
1494  //
1495  cmd(cmdstr.str());
1496 
1497  return *this;
1498 }
1499 
1500 
1501 //------------------------------------------------------------------------------
1502 //
1503 // Plots a 3d graph from a list of doubles (x y z) saved in a file
1504 //
1505 Gnuplot& Gnuplot::plotfile_xyz(const std::string &filename,
1506  const unsigned int column_x,
1507  const unsigned int column_y,
1508  const unsigned int column_z,
1509  const std::string &title)
1510 {
1511  //
1512  // check if file exists
1513  //
1514  file_available(filename);
1515 
1516  std::ostringstream cmdstr;
1517  //
1518  // command to be sent to gnuplot
1519  //
1520  if (nplots > 0 && two_dim == false)
1521  cmdstr << "replot ";
1522  else
1523  cmdstr << "splot ";
1524 
1525  cmdstr << "\"" << filename << "\" using " << column_x << ":" << column_y
1526  << ":" << column_z;
1527 
1528  if (title == "")
1529  cmdstr << " notitle with " << pstyle;
1530  else
1531  cmdstr << " title \"" << title << "\" with " << pstyle;
1532 
1533  //
1534  // Do the actual plot
1535  //
1536  cmd(cmdstr.str());
1537 
1538  return *this;
1539 }
1540 
1541 
1542 
1543 //------------------------------------------------------------------------------
1544 //
1546 //
1547 Gnuplot& Gnuplot::plot_image(const unsigned char * ucPicBuf,
1548  const unsigned int iWidth,
1549  const unsigned int iHeight,
1550  const std::string &title)
1551 {
1552  std::ofstream tmp;
1553  std::string name = create_tmpfile(tmp);
1554  if (name == "")
1555  return *this;
1556 
1557  //
1558  // write the data to file
1559  //
1560  int iIndex = 0;
1561  for(unsigned iRow = 0; iRow < iHeight; ++iRow)
1562  {
1563  for(unsigned iColumn = 0; iColumn < iWidth; ++iColumn)
1564  {
1565  tmp << iColumn << " " << iRow << " "
1566  << static_cast<float>(ucPicBuf[iIndex++]) << std::endl;
1567  }
1568  }
1569 
1570  tmp.flush();
1571  tmp.close();
1572 
1573 
1574  std::ostringstream cmdstr;
1575  //
1576  // command to be sent to gnuplot
1577  //
1578  if (nplots > 0 && two_dim == true)
1579  cmdstr << "replot ";
1580  else
1581  cmdstr << "plot ";
1582 
1583  if (title == "")
1584  cmdstr << "\"" << name << "\" with image";
1585  else
1586  cmdstr << "\"" << name << "\" title \"" << title << "\" with image";
1587 
1588  //
1589  // Do the actual plot
1590  //
1591  cmd(cmdstr.str());
1592 
1593  return *this;
1594 }
1595 
1596 
1597 
1598 //------------------------------------------------------------------------------
1599 //
1600 // Sends a command to an active gnuplot session
1601 //
1602 Gnuplot& Gnuplot::cmd(const std::string &cmdstr)
1603 {
1604  if( !(valid) )
1605  {
1606  return *this;
1607  }
1608 
1609 
1610  // int fputs ( const char * str, FILE * stream );
1611  // writes the string str to the stream.
1612  // The function begins copying from the address specified (str) until it
1613  // reaches the terminating null character ('\0'). This final
1614  // null-character is not copied to the stream.
1615  fputs( (cmdstr+"\n").c_str(), gnucmd );
1616 
1617  // int fflush ( FILE * stream );
1618  // If the given stream was open for writing and the last i/o operation was
1619  // an output operation, any unwritten data in the output buffer is written
1620  // to the file. If the argument is a null pointer, all open files are
1621  // flushed. The stream remains open after this call.
1622  fflush(gnucmd);
1623 
1624 
1625  if( cmdstr.find("replot") != std::string::npos )
1626  {
1627  return *this;
1628  }
1629  else if( cmdstr.find("splot") != std::string::npos )
1630  {
1631  two_dim = false;
1632  nplots++;
1633  }
1634  else if( cmdstr.find("plot") != std::string::npos )
1635  {
1636  two_dim = true;
1637  nplots++;
1638  }
1639 
1640  return *this;
1641 }
1642 
1643 
1644 
1645 //------------------------------------------------------------------------------
1646 //
1647 // Opens up a gnuplot session, ready to receive commands
1648 //
1649 void Gnuplot::init()
1650 {
1651  // char * getenv ( const char * name ); get value of environment variable
1652  // Retrieves a C string containing the value of the environment variable
1653  // whose name is specified as argument. If the requested variable is not
1654  // part of the environment list, the function returns a NULL pointer.
1655 #if ( defined(unix) || defined(__unix) || defined(__unix__) ) && !defined(__APPLE__)
1656  if (getenv("DISPLAY") == NULL)
1657  {
1658  valid = false;
1659  throw GnuplotException("Can't find DISPLAY variable");
1660  }
1661 #endif
1662 
1663 
1664  // if gnuplot not available
1665  if (!Gnuplot::get_program_path())
1666  {
1667  valid = false;
1668  throw GnuplotException("Can't find gnuplot");
1669  }
1670 
1671 
1672  //
1673  // open pipe
1674  //
1675  std::string tmp = Gnuplot::m_sGNUPlotPath + "/" +
1676  Gnuplot::m_sGNUPlotFileName;
1677 
1678  // FILE *popen(const char *command, const char *mode);
1679  // The popen() function shall execute the command specified by the string
1680  // command, create a pipe between the calling program and the executed
1681  // command, and return a pointer to a stream that can be used to either read
1682  // from or write to the pipe.
1683 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
1684  gnucmd = _popen(tmp.c_str(),"w");
1685 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
1686  gnucmd = popen(tmp.c_str(),"w");
1687 #endif
1688 
1689  // popen() shall return a pointer to an open stream that can be used to read
1690  // or write to the pipe. Otherwise, it shall return a null pointer and may
1691  // set errno to indicate the error.
1692  if (!gnucmd)
1693  {
1694  valid = false;
1695  throw GnuplotException("Couldn't open connection to gnuplot");
1696  }
1697 
1698  nplots = 0;
1699  valid = true;
1700  smooth = "";
1701 
1702  //set terminal type
1703  showonscreen();
1704 
1705  return;
1706 }
1707 
1708 
1709 //------------------------------------------------------------------------------
1710 //
1711 // Find out if a command lives in m_sGNUPlotPath or in PATH
1712 //
1713 bool Gnuplot::get_program_path()
1714 {
1715  //
1716  // first look in m_sGNUPlotPath for Gnuplot
1717  //
1718  std::string tmp = Gnuplot::m_sGNUPlotPath + "/" +
1719  Gnuplot::m_sGNUPlotFileName;
1720 
1721 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
1722  if ( Gnuplot::file_exists(tmp,0) ) // check existence
1723 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
1724  if ( Gnuplot::file_exists(tmp,1) ) // check existence and execution permission
1725 #endif
1726  {
1727  return true;
1728  }
1729 
1730 
1731  //
1732  // second look in PATH for Gnuplot
1733  //
1734  char *path;
1735  // Retrieves a C string containing the value of environment variable PATH
1736  path = getenv("PATH");
1737 
1738 
1739  if (path == NULL)
1740  {
1741  throw GnuplotException("Path is not set");
1742  return false;
1743  }
1744  else
1745  {
1746  std::list<std::string> ls;
1747 
1748  //split path (one long string) into list ls of strings
1749 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
1750  stringtok(ls,path,";");
1751 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
1752  stringtok(ls,path,":");
1753 #endif
1754 
1755  // scan list for Gnuplot program files
1756  for (std::list<std::string>::const_iterator i = ls.begin();
1757  i != ls.end(); ++i)
1758  {
1759  tmp = (*i) + "/" + Gnuplot::m_sGNUPlotFileName;
1760 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
1761  if ( Gnuplot::file_exists(tmp,0) ) // check existence
1762 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
1763  if ( Gnuplot::file_exists(tmp,1) ) // check existence and execution permission
1764 #endif
1765  {
1766  Gnuplot::m_sGNUPlotPath = *i; // set m_sGNUPlotPath
1767  return true;
1768  }
1769  }
1770 
1771  tmp = "Can't find gnuplot neither in PATH nor in \"" +
1772  Gnuplot::m_sGNUPlotPath + "\"";
1773  throw GnuplotException(tmp);
1774 
1775  Gnuplot::m_sGNUPlotPath = "";
1776  return false;
1777  }
1778 }
1779 
1780 
1781 
1782 //------------------------------------------------------------------------------
1783 //
1784 // check if file exists
1785 //
1786 bool Gnuplot::file_exists(const std::string &filename, int mode)
1787 {
1788  if ( mode < 0 || mode > 7)
1789  {
1790  throw std::runtime_error("In function \"Gnuplot::file_exists\": mode\
1791  has to be an integer between 0 and 7");
1792  return false;
1793  }
1794 
1795  // int _access(const char *path, int mode);
1796  // returns 0 if the file has the given mode,
1797  // it returns -1 if the named file does not exist or is not accessible in
1798  // the given mode
1799  // mode = 0 (F_OK) (default): checks file for existence only
1800  // mode = 1 (X_OK): execution permission
1801  // mode = 2 (W_OK): write permission
1802  // mode = 4 (R_OK): read permission
1803  // mode = 6 : read and write permission
1804  // mode = 7 : read, write and execution permission
1805 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
1806  if (_access(filename.c_str(), mode) == 0)
1807 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
1808  if (access(filename.c_str(), mode) == 0)
1809 #endif
1810  {
1811  return true;
1812  }
1813  else
1814  {
1815  return false;
1816  }
1817 
1818 }
1819 
1820 bool Gnuplot::file_available(const std::string &filename){
1821  std::ostringstream except;
1822  if( Gnuplot::file_exists(filename,0) ) // check existence
1823  {
1824  if( !(Gnuplot::file_exists(filename,4)) ){// check read permission
1825  except << "No read permission for File \"" << filename << "\"";
1826  throw GnuplotException( except.str() );
1827  return false;
1828  }
1829  }
1830  else{
1831  except << "File \"" << filename << "\" does not exist";
1832  throw GnuplotException( except.str() );
1833  return false;
1834  }
1835 
1836  return true;
1837 }
1838 
1839 
1840 
1841 //------------------------------------------------------------------------------
1842 //
1843 // Opens a temporary file
1844 //
1845 std::string Gnuplot::create_tmpfile(std::ofstream &tmp)
1846 {
1847 
1848 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
1849  char name[] = "gnuplotiXXXXXX"; //tmp file in working directory
1850 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
1851  char name[] = "/tmp/gnuplotiXXXXXX"; // tmp file in /tmp
1852 #endif
1853 
1854  //
1855  // check if maximum number of temporary files reached
1856  //
1857  if (Gnuplot::tmpfile_num == GP_MAX_TMP_FILES - 1)
1858  {
1859  std::ostringstream except;
1860  except << "Maximum number of temporary files reached ("
1861  << GP_MAX_TMP_FILES << "): cannot open more files" << std::endl;
1862 
1863  throw GnuplotException( except.str() );
1864  return "";
1865  }
1866 
1867  // int mkstemp(char *name);
1868  // shall replace the contents of the string pointed to by "name" by a unique
1869  // filename, and return a file descriptor for the file open for reading and
1870  // writing. Otherwise, -1 shall be returned if no suitable file could be
1871  // created. The string in template should look like a filename with six
1872  // trailing 'X' s; mkstemp() replaces each 'X' with a character from the
1873  // portable filename character set. The characters are chosen such that the
1874  // resulting name does not duplicate the name of an existing file at the
1875  // time of a call to mkstemp()
1876 
1877 
1878  //
1879  // open temporary files for output
1880  //
1881 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
1882  if (_mktemp(name) == NULL)
1883 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
1884  if (mkstemp(name) == -1)
1885 #endif
1886  {
1887  std::ostringstream except;
1888  except << "Cannot create temporary file \"" << name << "\"";
1889  throw GnuplotException(except.str());
1890  return "";
1891  }
1892 
1893  tmp.open(name);
1894  if (tmp.bad())
1895  {
1896  std::ostringstream except;
1897  except << "Cannot create temporary file \"" << name << "\"";
1898  throw GnuplotException(except.str());
1899  return "";
1900  }
1901 
1902  //
1903  // Save the temporary filename
1904  //
1905  tmpfile_list.push_back(name);
1906  Gnuplot::tmpfile_num++;
1907 
1908  return name;
1909 }
1910 
1912  if ((tmpfile_list).size() > 0)
1913  {
1914  for (unsigned int i = 0; i < tmpfile_list.size(); i++)
1915  remove( tmpfile_list[i].c_str() );
1916 
1917  Gnuplot::tmpfile_num -= tmpfile_list.size();
1918  }
1919 }
1920 #endif
Gnuplot & reset_all()
resets a gnuplot session and sets all variables to default
Definition: gnuplot_i.hpp:937
Gnuplot & plot_xyz(const X &x, const Y &y, const Z &z, const std::string &title="")
from std::vector
Definition: gnuplot_i.hpp:773
Gnuplot & set_style(const std::string &stylestr="points")
Definition: gnuplot_i.hpp:956
Gnuplot & operator<<(const std::string &cmdstr)
Sends a command to an active gnuplot session, identical to cmd() send a command to gnuplot using the ...
Definition: gnuplot_i.hpp:221
Gnuplot & set_yrange(const double iFrom, const double iTo)
set y-axis - ranges
Definition: gnuplot_i.hpp:1227
Gnuplot & set_isosamples(const int isolines=10)
set isoline density (grid) for plotting functions as surfaces (for 3d plots)
Definition: gnuplot_i.hpp:1136
Gnuplot & set_title(const std::string &title="")
sets and clears the title of a gnuplot session
Definition: gnuplot_i.hpp:348
A C++ interface to gnuplot.
Definition: gnuplot_i.hpp:60
Gnuplot & set_pointsize(const double pointsize=1.0)
scales the size of the points used in plots
Definition: gnuplot_i.hpp:1109
Gnuplot & unset_hidden3d()
Definition: gnuplot_i.hpp:302
Gnuplot & showonscreen()
sets terminal type to terminal_std
Definition: gnuplot_i.hpp:1026
The Gnuplot class, can receive parameters, then transmit them to Gnuplot via pipes Pipes are closed w...
Definition: gnuplot_i.hpp:75
Gnuplot & set_ylabel(const std::string &label="x")
set x axis label
Definition: gnuplot_i.hpp:1185
Gnuplot & plotfile_xy(const std::string &filename, const unsigned int column_x=1, const unsigned int column_y=2, const std::string &title="")
Definition: gnuplot_i.hpp:1418
Gnuplot & plotfile_x(const std::string &filename, const unsigned int column=1, const std::string &title="")
Definition: gnuplot_i.hpp:1373
Gnuplot & unset_xlogscale()
Definition: gnuplot_i.hpp:416
Gnuplot & set_xlabel(const std::string &label="y")
set y axis label
Definition: gnuplot_i.hpp:1172
Gnuplot & set_xautoscale()
Definition: gnuplot_i.hpp:387
Gnuplot & set_legend(const std::string &position="default")
Definition: gnuplot_i.hpp:1053
Gnuplot & set_zautoscale()
Definition: gnuplot_i.hpp:401
Gnuplot & set_xrange(const double iFrom, const double iTo)
set axis - ranges
Definition: gnuplot_i.hpp:1213
Gnuplot & set_smooth(const std::string &stylestr="csplines")
Definition: gnuplot_i.hpp:1002
Gnuplot & set_cbrange(const double iFrom, const double iTo)
set palette range (autoscale by default)
Definition: gnuplot_i.hpp:1256
Gnuplot & set_zrange(const double iFrom, const double iTo)
set z-axis - ranges
Definition: gnuplot_i.hpp:1241
Gnuplot & unset_smooth()
unset smooth attention: smooth is not set by default
Definition: gnuplot_i.hpp:258
Gnuplot & cmd(const std::string &cmdstr)
send a command to gnuplot
Definition: gnuplot_i.hpp:1602
Gnuplot & plot_xy(const X &x, const Y &y, const std::string &title="")
from data
Definition: gnuplot_i.hpp:688
Gnuplot & set_zlabel(const std::string &label="z")
set z axis label
Definition: gnuplot_i.hpp:1198
Gnuplot & plot_equation(const std::string &equation, const std::string &title="")
Definition: gnuplot_i.hpp:1306
Gnuplot & unset_legend()
Switches legend off attention:legend is set by default.
Definition: gnuplot_i.hpp:339
Gnuplot & set_yautoscale()
Definition: gnuplot_i.hpp:394
Gnuplot & unset_multiplot()
Definition: gnuplot_i.hpp:281
Gnuplot & savetops(const std::string &filename="gnuplot_output")
saves a gnuplot session to a postscript file, filename without extension
Definition: gnuplot_i.hpp:1038
static void set_terminal_std(const std::string &type)
Definition: gnuplot_i.hpp:845
Gnuplot & plot_slope(const double a, const double b, const std::string &title="")
plot an equation of the form: y = ax + b, you supply a and b
Definition: gnuplot_i.hpp:1272
Gnuplot & set_samples(const int samples=100)
set sampling rate of functions, or for interpolating data
Definition: gnuplot_i.hpp:1122
GnuplotException(const std::string &msg)
GnuplotException Class constructor.
Definition: gnuplot_i.hpp:67
Gnuplot & unset_surface()
Definition: gnuplot_i.hpp:326
Gnuplot & plot_equation3d(const std::string &equation, const std::string &title="")
Definition: gnuplot_i.hpp:1339
Gnuplot & reset_plot()
resets a gnuplot session (next plot will erase previous ones)
Definition: gnuplot_i.hpp:923
Gnuplot & unset_zlogscale()
Definition: gnuplot_i.hpp:430
Gnuplot & plot_x(const X &x, const std::string &title="")
from std::vector
Definition: gnuplot_i.hpp:654
Gnuplot & set_ylogscale(const double base=10)
turns on/off log scaling for the specified yaxis (logscale is not set by default) ...
Definition: gnuplot_i.hpp:1081
Gnuplot & set_hidden3d()
Definition: gnuplot_i.hpp:295
Gnuplot & set_grid()
turns grid on/off
Definition: gnuplot_i.hpp:265
Gnuplot & plot_xy_err(const X &x, const Y &y, const E &dy, const std::string &title="")
from data
Definition: gnuplot_i.hpp:728
Gnuplot & plotfile_xyz(const std::string &filename, const unsigned int column_x=1, const unsigned int column_y=2, const unsigned int column_z=3, const std::string &title="")
Definition: gnuplot_i.hpp:1505
Gnuplot & unset_ylogscale()
Definition: gnuplot_i.hpp:423
Gnuplot & replot(void)
replot repeats the last plot or splot command. this can be useful for viewing a plot with different s...
Definition: gnuplot_i.hpp:527
Gnuplot & unset_title()
Clears the title of a gnuplot session The title is not set by default.
Definition: gnuplot_i.hpp:364
Gnuplot & set_multiplot()
Definition: gnuplot_i.hpp:274
Gnuplot & plotfile_xy_err(const std::string &filename, const unsigned int column_x=1, const unsigned int column_y=2, const unsigned int column_dy=3, const std::string &title="")
Definition: gnuplot_i.hpp:1463
~Gnuplot()
destructor: needed to delete temporary files
Definition: gnuplot_i.hpp:905
bool is_valid()
Is the gnuplot session valid ??
Definition: gnuplot_i.hpp:543
void remove_tmpfiles()
deletes temporary files
Definition: gnuplot_i.hpp:1911
Gnuplot & set_surface()
Definition: gnuplot_i.hpp:318
Gnuplot & set_contour(const std::string &position="base")
Definition: gnuplot_i.hpp:1151
Gnuplot & set_xlogscale(const double base=10)
turns on/off log scaling for the specified xaxis (logscale is not set by default) ...
Definition: gnuplot_i.hpp:1067
Gnuplot(const std::string &style="points")
set a style during construction
Definition: gnuplot_i.hpp:573
static bool set_GNUPlotPath(const std::string &path)
optional function: set Gnuplot path manual attention: for windows: path with slash &#39;/&#39; not backslash ...
Definition: gnuplot_i.hpp:817
Gnuplot & plot_image(const unsigned char *ucPicBuf, const unsigned int iWidth, const unsigned int iHeight, const std::string &title="")
plot image
Definition: gnuplot_i.hpp:1547
Gnuplot & unset_grid()
grid is not set by default
Definition: gnuplot_i.hpp:267
Gnuplot & unset_contour()
Definition: gnuplot_i.hpp:311
Gnuplot & set_zlogscale(const double base=10)
turns on/off log scaling for the specified zaxis (logscale is not set by default) ...
Definition: gnuplot_i.hpp:1095