Gnuplot.hh 4.63 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
////////////////////////////////////////////
//
// A C++ interface to gnuplot. 
//
// This is a direct translation from the C interface
// written by N. Devillard (which is available from
// http://ndevilla.free.fr/gnuplot/).
//
// As in the C interface this uses pipes and so wont
// run on a system that doesn't have POSIX pipe 
// support
//
// Rajarshi Guha
// <rajarshi@presidency.com>
//
// 07/03/03
//
////////////////////////////////////////////
//
// A little correction for Win32 compatibility
// and MS VC 6.0 done by V.Chyzhdzenka 
//
// Notes:
// 1. Added private method Gnuplot::init().
// 2. Temporary file is created in th current
//    folder but not in /tmp.
// 3. Added #indef WIN32 e.t.c. where is needed.
// 4. Added private member m_sGNUPlotFileName is
//    a name of executed GNUPlot file.
//
// Viktor Chyzhdzenka
// e-mail: chyzhdzenka@mail.ru
//
// 20/05/03
//
////////////////////////////////////////////

#ifndef _GNUPLOT_HH
#define _GNUPLOT_HH

#include <OpenMesh/Core/System/config.hh>
// #ifndef WIN32
// #  include <unistd.h>
// #else
// #  pragma warning (disable : 4786) // Disable 4786 warning for MS VC 6.0
// #endif
#if defined(OM_CC_MIPS)
#  include <stdio.h>
#else
#  include <cstdio>
#endif
#include <string>
#include <vector>
#include <stdexcept>

// ----------------------------------------------------------------------------

#ifdef WIN32
#  define GP_MAX_TMP_FILES    27 //27 temporary files it's Microsoft restriction
#else
#  define GP_MAX_TMP_FILES    64
#  define GP_TMP_NAME_SIZE    512
#  define GP_TITLE_SIZE       80
#endif
#define GP_CMD_SIZE         1024

// ----------------------------------------------------------------------------

using namespace std;

// ----------------------------------------------------------------------------

/// Exception thrown by class Gnuplot
class GnuplotException : public runtime_error
{
public:
77
  explicit GnuplotException(const string &msg) : runtime_error(msg){}
Jan Möbius's avatar
Jan Möbius committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
};

// ----------------------------------------------------------------------------

/** Utility class interfacing with Gnuplot.
 * 
 *  \note The plot will be visible as long as the object is not destructed.
 *
 *  \author Rajarshi Guha (C++ API based on the C API by Nicolas Devillard)
 *
 *  \see <a
 *  href="http://ndevilla.free.fr/gnuplot/">http://ndevilla.free.fr/gnuplot/</a>
 *  more information.
 */
class Gnuplot
{
private:

  FILE            *gnucmd;
  string           pstyle;
  vector<string>   to_delete;
  int              nplots;
Jan Möbius's avatar
Jan Möbius committed
100
  bool             get_program_path(const string& );
Jan Möbius's avatar
Jan Möbius committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  bool             valid;

  // Name of executed GNUPlot file
  static string    gnuplot_executable_;

  void init();

public:

  /// \name Constructors
  //@{
  /// Default constructor.
  Gnuplot();
  
  /// Set a style during construction.
116
  explicit Gnuplot(const string & _style);
Jan Möbius's avatar
Jan Möbius committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

  /// Constructor calling plot_xy().
  Gnuplot(const string & _title,
          const string & _style,
          const string & _xlabel,
          const string & _ylabel,
          vector<double> _x, vector<double> _y);
  
  /// Constructor calling plot_x().
  Gnuplot(const string &_title,
          const string &_style,
          const string &_xlabel,
          const string &_ylabel,
          vector<double> _x);
  //@}

  ~Gnuplot();
  
  /// Send a command to gnuplot (low-level function use by all plot functions.)
  void cmd(const char *_cmd, ...);
  
  /// \name Gnuplot settings
  //@{
  void set_style(const string & _style);   ///< set line style
  void set_ylabel(const string & _ylabel); ///< set x axis label
  void set_xlabel(const string & _xlabel); ///< set x axis label
  //@}

  /// \name plot functions
  //@{

  /// Plot a single vector
  void plot_x(vector<double> _x, const string &_title);
  
  /// Plot x,y pairs
  void plot_xy(vector<double> _x, vector<double> _y, const string  &_title);
  
  /// Plot an equation of the form: y = ax + b
  /// You supply a and b
  void plot_slope(
                  double _a,
                  double _b,
                  const string & _title
                  );
  
  /// Plot an equation supplied as a string
  void plot_equation(
                     const string & _equation,
                     const string & _title
                     );
  
  /// If multiple plots are present it will clear the plot area
  void reset_plot(void);

  //@}

  /// Is \c Self valid?
  bool is_valid(void) const { return valid; }

  /// Is \c Self active, i.e. does it have an active plot?
  bool is_active(void) const { return this->nplots > 0; }
};


// ----------------------------------------------------------------------------
#endif // _GNUPLOT_HH
// ============================================================================