ROBOOP, A Robotics Object Oriented Package in C++
gnugraph.h
Go to the documentation of this file.
1 /*
2 ROBOOP -- A robotics object oriented package in C++
3 Copyright (C) 1996-2004 Richard Gourdeau
4 
5 This library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9 
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 
19 
20 Report problems and direct all questions to:
21 
22 Richard Gourdeau, Professeur
23 Departement de genie electrique
24 Ecole Polytechnique de Montreal
25 C.P. 6079, Succ. Centre-Ville
26 Montreal, Quebec, H3C 3A7
27 
28 email: richard.gourdeau@polymtl.ca
29 -------------------------------------------------------------------------------
30 Revision_history:
31 
32 2004/07/01: Etienne Lachance
33  -Added doxygen documentation.
34 
35 2004/07/01: Ethan Tira-Thompson
36  -Added support for newmat's use_namespace #define, using ROBOOP namespace
37 
38 2004/08/10: Etienne Lachance
39  -Added class Plot3d.
40  -Removed using ROBOOP namespace
41 -------------------------------------------------------------------------------
42 */
43 
44 #ifndef GNUGRAPH_H
45 #define GNUGRAPH_H
46 
47 
53 #ifdef _MSC_VER // Microsoft
54 #pragma warning (disable:4786) // Disable decorated name truncation warnings
55 #pragma warning (disable:4503) // Disable decorated name truncation warnings
56 #endif
57 
58 #if defined(__WIN32__) || defined(_WIN32) || defined(__NT__) || defined(__CYGWIN__) /* Windows 95/NT */
59 
60 #define GNUPLOT "gnuplot.exe"
61 #define STRICT
62 #include <windows.h>
63 
64 #ifdef _MSC_VER
65 #define snprintf _snprintf
66 #endif
67 
68 #else // Unix
69 
70 #define GNUPLOT "gnuplot"
71 #include <sys/types.h>
72 #include <unistd.h>
73 #endif
74 
75 #include <stdio.h>
76 #include <stdexcept>
77 
78 #include <boost/shared_ptr.hpp>
79 
80 #define WANT_STRING /* include.h will get string fns */
81 #define WANT_STREAM /* include.h will get stream fns */
82 #define WANT_FSTREAM /* include.h will get fstream fns */
83 #define WANT_MATH /* include.h will get math fns */
84  /* newmatap.h will get include.h */
85 #include "newmatap.h" /* need matrix applications */
86 #include "newmatio.h" /* need matrix output routines */
87 
88 #ifdef use_namespace
89 using namespace NEWMAT;
90 #endif
91 
92 #include <sys/stat.h>
93 
94 #include <sstream>
95 #include <vector>
96 
97 
98 #define OUT_OF_MEMORY -1
99 #define X_Y_DATA_NO_MATCH -2
100 #define LABELS_NBR_NO_MATCH -3
101 
102 
103 
104 typedef enum {
105  LINES,
106  DATAPOINTS,
107  LINESPOINTS,
108  IMPULSES,
109  DOTS,
110  STEPS,
111  BOXES
112 } LineType_en;
113 
114 
115 #define NCURVESMAX 10 // maximum number of curves in the same Plot2d
116 
117 class Plot2d;
118 
123 class GNUcurve {
124 
125  public:
126  GNUcurve(const std::vector<double> & x, std::vector<double> & y,
127  const std::string & label = "", LineType_en enLineType = LINES);
128  GNUcurve(void);
129  void dump(void);
130 
131  std::vector<double> vdX;
132  std::vector<double> vdY;
133  std::string clabel;
134  LineType_en enLineType;
135 };
136 
137 typedef boost::shared_ptr<GNUcurve> PSHR_Curve;
138 typedef std::vector<PSHR_Curve> VectorCurves;
139 
140 
145 class Plot2d {
146 public:
147  Plot2d(void);
148  void dump(void);
149  void settitle(const std::string & t);
150  void setxlabel(const std::string & t);
151  void setylabel(const std::string & t);
152  void addcurve(const Matrix & data, const std::string & label = "",
153  LineType_en enLineType = DATAPOINTS);
154  void gnuplot(void);
155  void addcommand(const std::string & gcom);
156 
157 private:
158  std::string title;
159  std::string xlabel;
160  std::string ylabel;
161  std::string gnucommand;
162 
163  VectorCurves vCurves;
164 };
165 
170 class Plot3d
171 {
172  std::string
173  title,
174  xlabel,
175  ylabel,
176  zlabel;
177 public:
178  Plot3d(){}
179  void settitle(const std::string & t);
180  void setxlabel(const std::string & t);
181  void setylabel(const std::string & t);
182  void setzlabel(const std::string & t);
183  void gnuplot(const Matrix & xyz);
184 };
185 
186 #define IO_COULD_NOT_OPEN_FILE -1
187 #define IO_MISMATCH_SIZE -2
188 #define IO_DATA_EMPTY -3
189 #define IO_MISMATCH_ELEMENT_NBR -4
190 #define PROBLEM_FILE_READING -5
191 
192 
198 public:
199  IO_matrix_file(const std::string & filename);
200  short write(const std::vector<Matrix> & data);
201  short write(const std::vector<Matrix> & data, const std::vector<std::string> & title);
202  short read(std::vector<Matrix> & data);
203  short read(std::vector<Matrix> & data, std::vector<std::string> & title);
204  short read_all(std::vector<Matrix> & data, std::vector<std::string> & data_title);
205 private:
206  int
208  nb_iterations_write,
209  nb_iterations_read,
210  nb_element;
211  std::string filename;
212 };
213 
214 
220 {
221 public:
222  Plot_file(const std::string & filename);
223  short graph(const std::string & title_graph, const std::string & label, const short x,
224  const short y, const short x_start, const short y_start,
225  const short y_end);
226 private:
227  std::vector<Matrix> data_from_file;
228  std::vector<std::string> data_title;
229 };
230 
231 
232 
233 short set_plot2d(const char *title_graph, const char *x_axis_title, const char *y_axis_title,
234  const char *label, LineType_en enLineType, const Matrix &xdata, const Matrix &ydata,
235  int start_y, int end_y);
236 
237 short set_plot2d(const char *title_graph, const char *x_axis_title, const char *y_axis_title,
238  const vector<char *> label, LineType_en enLineType, const Matrix &xdata,
239  const Matrix &ydata, const vector<int> & data_select);
240 
241 short set_plot3d(const Matrix & xyz, const std::string & title_graph, const std::string & x_axis_title,
242  const std::string & y_axis_title, const std::string & z_axis_title);
243 
244 
245 #endif
246