gnugraph.cpp
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
23 Professeur Agrege
24 Departement de genie electrique
25 Ecole Polytechnique de Montreal
26 C.P. 6079, Succ. Centre-Ville
27 Montreal, Quebec, H3C 3A7
28 
29 email: richard.gourdeau@polymtl.ca
30 
31 -------------------------------------------------------------------------------
32 Revision_history:
33 
34 2003/02/03: Etienne Lachance
35  -Added functions set_plot2d and classe IO_matrix_file.
36 
37 2003/29/04: Etienne Lachance
38  -Class definitions, functions prototype, ... now in gnugraph.h
39  -Improved class IO_matrix_file.
40  -Class Plot2d, GNUcurve are now using STL string instead of char*.
41  -Added class Plot_graph, to create a graph from a data file.
42  -Replaced all NULL by 0.
43  -Use mkstemp instead of tmpnam in void Plot2d::gnuplot(void).
44 
45 2003/15/08: Etienne Lachance
46  -The member function IO_matrix_file::write writes data in column of each
47  variables, instead of only one. This way it is possible to load a dat file
48  in Matlab.
49 
50 2004/07/01: Etienne Lachance
51  -Added doxygen documentation.
52 
53 2004/07/01: Ethan Tira-Thompson
54  -Added support for newmat's use_namespace #define, using ROBOOP namespace
55 
56 2004/08/10: Etienne Lachance
57  -Added class Plot3d.
58  -Removed using ROBOOP namespace
59 
60 2005/08/06 : Richard Gourdeau
61  -fixed catch(bad_alloc)
62 
63 2005/08/06 : Carmine Lia
64  -added defined(__MINGW32__) for temp files
65 -------------------------------------------------------------------------------
66 */
67 
73 static const char rcsid[] = "$Id: gnugraph.cpp,v 1.44 2006/05/19 17:49:58 gourdeau Exp $";
75 
76 #include "gnugraph.h"
77 
78 using namespace std;
79 
80 
81 const char *curvetype[] =
82  {"lines",
83  "points",
84  "linespoints",
85  "impulses",
86  "dots",
87  "steps",
88  "boxes"};
89 
90 
93 {
94  enLineType = LINES;
95 }
96 
97 
98 GNUcurve::GNUcurve(const std::vector<double> & x, std::vector<double> & y,
99  const string & label, LineType_en enLineType_) :
100  vdX(x),
101  vdY(y),
102  clabel(label),
103  enLineType(enLineType_)
105 {
106  if (vdX.size() != vdY.size())
107  {
108  cerr << "GNUcurve::GNUcurve number of x and y elements does not match." << endl;
109  }
110 }
111 
112 void GNUcurve::dump(void)
114 {
115  cout << "Curve label: " << clabel << endl;
116  cout << "Curve type: " << curvetype[enLineType] << endl;
117  cout << "Curve data points: \n";
118 
119  unsigned int vSize = vdX.size();
120  for(unsigned int i = 0; i < vSize; ++i)
121  {
122  cout << vdX[i] << " " << vdY[i] << endl;
123  }
124  cout << endl;
125 }
126 
127 
130 {
131 }
132 
133 void Plot2d::gnuplot(void)
135 {
136  unsigned int i;
137  int strl;
138 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__)
139  char filename[L_tmpnam];
140 #else
141  char filename[] = "tmpfileXXXXXX";
142 #endif
143  char * filedata=0;
144  char * wibsl;
145  char bsl = '\\';
146 
147 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__)
148  tmpnam(filename); /* generate a temporary file name */
149 #else
150  int fd = mkstemp(filename);
151  if (fd == -1)
152  {
153  std::cerr << "mkstemp(" << filename << "failed: " << std::strerror(errno) << std::endl;
154  return;
155  }
156 #endif
157  /* replacing \ by / */
158  while((wibsl = strchr(filename,bsl)) != 0) {
159  wibsl[0] = '/';
160  }
161 
162  {
163  ofstream fileout(filename); /* write the command file */
164  fileout << gnucommand.c_str();
165  fileout << "set title \"" << title << "\"\n";
166  fileout << "set xlabel \"" << xlabel << "\"\n";
167  fileout << "set ylabel \"" << ylabel << "\"\n";
168  fileout << "plot \\\n";
169 
170  for(i = 0; i < vCurves.size(); i++) {
171  fileout << "\"" << filename << "." << i << "\" ";
172  fileout << "title \"" << vCurves[i]->clabel << "\" ";
173  fileout << "with " << curvetype[vCurves[i]->enLineType] << " ";
174  if( i+1 < vCurves.size()){
175  fileout << ", \\\n";
176  }
177  }
178  fileout << "\n";
179  }
180  try
181  {
182  filedata = new char[strlen(filename)+3];
183  }
184  catch(bad_alloc & e)
185  {
186  cerr << "Plot2d::gnuplot:: new ran out of memory" << endl;
187  }
188  strcpy(filedata,filename);
189  strcat(filedata,".");
190  strl = strlen(filedata);
191 
192  for(i = 0; i < vCurves.size(); i++) /* write the data files */
193  {
194  sprintf(&filedata[strl],"%d",i);
195  ofstream fileout(filedata);
196 
197 //#ifndef _MSC_VER // MSVC++ chokes on the next line !
198  // fileout << curves[i].xy;
199 //#else
200  unsigned int vSize = vCurves[i]->vdX.size();
201  for (unsigned int j = 0; j < vSize; ++j)
202  {
203  fileout << vCurves[i]->vdX[j] << " " << vCurves[i]->vdY[j] << endl;
204  }
205 
206 /*
207  for(int j = 1; j <= curves[i].xy.Nrows(); j++) {
208  for(int k = 1; k <= curves[i].xy.Ncols(); k++) {
209  fileout << curves[i].xy(j,k) << " ";
210  }
211  fileout << "\n";
212  }
213 */
214 //#endif
215  }
216  /* all command and data files are ready for the call to gnuplot */
217 #if defined(__WIN32__) || defined(_WIN32) || defined(__NT__) || defined(__CYGWIN__) || defined(__MINGW32__)
218  /* Windows 95/98/NT/2000 etc */
219  char c[L_tmpnam+15];
220  char *d;
221  HWND hwndm, hwnd;
222  if (WinExec(GNUPLOT, SW_SHOWNORMAL) <= 32) { /* start gnuplot */
223  /* failed */
224  cout << "Cannot find the gnuplot application\n";
225  cout << "Press Enter to continue" << endl;
226  getchar();
227  remove(filename); /* clean up the files and return */
228  for(i = 0; i < vCurves.size(); i++) {
229  sprintf(&filedata[strl],"%d",i);
230  remove(filedata);
231  }
232  delete filedata;
233  return;
234  } else { /* succeed */
235  /* get gnuplot main window handle */
236  hwndm = FindWindow((LPSTR) 0, (LPSTR) "gnuplot");
237  }
238  hwnd= GetWindow(hwndm, GW_CHILD); /* get gnuplot command area handle */
239  if(hwnd == 0) cout << "OUPS!!!\n"; /* something wrong happened */
240  sprintf(c,"load \"%s\" \n",filename); /* load command for the plot */
241 
242  d = c;
243  while(*d != '\0') { /* sending the command through windows messages */
244  SendMessage(hwnd,WM_CHAR,*d,1L);
245  d++;
246  }
247  cout << "Press Enter to continue..." << endl;
248  getchar();
249 #else /* using a pipe under Unix */
250  FILE *command;
251 
252  command = popen(GNUPLOT,"w");
253  fprintf(command,"load \"%s\"\n",filename); fflush(command);
254  fprintf(stderr,"Press Enter to continue...\n"); fflush(stderr);
255  getchar();
256  pclose(command);
257 #endif
258 
259 
260  remove(filename);
261  for(i = 0; i < vCurves.size(); i++) {
262  sprintf(&filedata[strl],"%d",i);
263  remove(filedata);
264  }
265  delete filedata;
266 }
267 
268 
269 void Plot2d::addcurve(const Matrix & data, const string & label, LineType_en enLineType)
271 {
272  vector<double> x;
273  vector<double> y;
274 
275  for(int j = 1; j <= data.Nrows(); j++)
276  {
277  x.push_back(data(j,1));
278  y.push_back(data(j,2));
279  }
280 
281  try
282  {
283  GNUcurve *pCurve = new GNUcurve(x, y, label, enLineType);
284  vCurves.push_back(PSHR_Curve(pCurve));
285  }
286  catch (bad_alloc & e)
287  {
288  }
289 }
290 
291 void Plot2d::addcommand(const string & gcom)
293 {
294  gnucommand += gcom;
295 }
296 
297 void Plot2d::settitle(const string & t)
299 {
300  title = t;
301 }
302 
303 void Plot2d::setxlabel(const string & t)
305 {
306  xlabel = t;
307 }
308 
309 void Plot2d::setylabel(const string & t)
311 {
312  ylabel = t;
313 }
314 
315 void Plot2d::dump(void)
317 {
318  cout << "gnuplot commands:\n" << gnucommand.c_str();
319  cout << "Plot title: " << title.c_str() << "\n";
320  cout << "X label: " << xlabel.c_str() << "\n";
321  cout << "Y label: " << ylabel.c_str() << "\n";
322 
323  for (unsigned int i = 0; i < vCurves.size(); ++i)
324  {
325  cout << "\nCurve #" << i << "\n";
326  vCurves[i]->dump();
327  }
328 }
329 
330 
331 void Plot3d::gnuplot(const Matrix & xyz)
333 {
334  if (xyz.Ncols() != 3*xyz.Nrows())
335  {
336  cerr << "Plot3d::gnuplot: wrong size of xyz data matrix." << endl;
337  return;
338  }
339 
340  int strl;
341 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__)
342  char filename[L_tmpnam];
343 #else
344  char filename[] = "tmpfileXXXXXX";
345 #endif
346  char * filedata=0;
347  char * wibsl;
348  char bsl = '\\';
349 
350 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__)
351  tmpnam(filename); /* generate a temporary file name */
352 #else
353  int fd = mkstemp(filename);
354  if (fd == -1)
355  {
356  std::cerr << "mkstemp(" << filename << "failed: " << std::strerror(errno) << std::endl;
357  return;
358  }
359 #endif
360 
361  while((wibsl = strchr(filename,bsl)) != 0) {
362  wibsl[0] = '/';
363  }
364  {
365  ofstream fileout(filename); // write the command file
366  fileout << "set title \"" << title << "\"\n";
367  fileout << "set xlabel \"" << xlabel << "\"\n";
368  fileout << "set ylabel \"" << ylabel << "\"\n";
369  fileout << "set zlabel \"" << zlabel << "\"\n";
370  fileout << "splot \\\n";
371  fileout << "\"" << filename << "." << 0 << "\" ";
372  fileout << "title \"" << "" << "\" ";
373  fileout << "with " << "linespoints" << " ";
374  }
375 
376  try
377  {
378  filedata = new char[strlen(filename)+3];
379  }
380  catch(bad_alloc & e)
381  {
382  cerr << "Plot3d::gnuplot:: new ran out of memory" << endl;
383  }
384  strcpy(filedata,filename);
385  strcat(filedata,".");
386  strl = strlen(filedata);
387 
388  sprintf(&filedata[strl],"%d",0);
389  ofstream fileout(filedata);
390 
391  for(int j = 0; j < 3*xyz.Nrows(); j+=3)
392  {
393  fileout << xyz.SubMatrix(1, xyz.Nrows(), j+1, j+3);
394  fileout << " " << endl;
395  }
396 
397 #if defined(__WIN32__) || defined(_WIN32) || defined(__NT__) || defined(__CYGWIN__) || defined(__MINGW32__)
398  /* Windows 95/98/NT/2000 etc */
399  char c[L_tmpnam+15];
400  char *d;
401  HWND hwndm, hwnd;
402  if (WinExec(GNUPLOT, SW_SHOWNORMAL) <= 32) { /* start gnuplot */
403  /* failed */
404  cout << "Cannot find the gnuplot application\n";
405  cout << "Press Enter to continue" << endl;
406  getchar();
407  remove(filename); /* clean up the files and return */
408  remove(filedata);
409  delete filedata;
410  return;
411  } else { /* succeed */
412  /* get gnuplot main window handle */
413  hwndm = FindWindow((LPSTR) 0, (LPSTR) "gnuplot");
414  }
415  hwnd= GetWindow(hwndm, GW_CHILD); /* get gnuplot command area handle */
416  if(hwnd == 0) cout << "OUPS!!!\n"; /* something wrong happened */
417  sprintf(c,"load \"%s\" \n",filename); /* load command for the plot */
418 
419 #ifdef __GNUG__ /* Cygnus Gnu C++ for win32*/
420  char ccygnus[] = "cd \"c:\"\n"; /* this string should reflect
421  the drive used to mount /
422  where /tmp is located */
423  d = ccygnus;
424  while(*d != '\0') { /* sending the command through windows messages */
425  SendMessage(hwnd,WM_CHAR,*d,1L);
426  d++;
427  }
428 #endif
429  d = c;
430  while(*d != '\0') { /* sending the command through windows messages */
431  SendMessage(hwnd,WM_CHAR,*d,1L);
432  d++;
433  }
434  cout << "Press Enter to continue..." << endl;
435  getchar();
436 #else /* using a pipe under Unix */
437  FILE *command;
438  command = popen(GNUPLOT,"w");
439  fprintf(command,"load \"%s\"\n",filename); fflush(command);
440  fprintf(stderr,"Press Enter to continue...\n"); fflush(stderr);
441  getchar();
442  pclose(command);
443 #endif
444 
445  remove(filename);
446  sprintf(&filedata[strl],"%d",0);
447  remove(filedata);
448 
449  delete filedata;
450 }
451 
452 void Plot3d::settitle(const string & t)
454 {
455  title = t;
456 }
457 
458 void Plot3d::setxlabel(const string & t)
460 {
461  xlabel = t;
462 }
463 
464 void Plot3d::setylabel(const string & t)
466 {
467  ylabel = t;
468 }
469 
470 void Plot3d::setzlabel(const string & t)
472 {
473  zlabel = t;
474 }
475 
476 // ---------------------------------------------------------------------------------------
477 
478 IO_matrix_file::IO_matrix_file(const string & filename_)
480 {
481  filename = filename_;
482  position_read = 0;
483  nb_iterations_write = 0;
484  nb_iterations_read = 0;
485  nb_element = 0;
486 }
487 
488 
489 short IO_matrix_file::write(const vector<Matrix> & data)
491 {
492  vector<string> title;
493  string tmp;
494  for(unsigned int i = 1; i <= data.size(); i++)
495  {
496  tmp = "data#"; // Provide a default name
497  tmp += i;
498  title.push_back(tmp);
499  }
500 
501  return IO_matrix_file::write(data, title);
502 }
503 
504 
505 short IO_matrix_file::write(const vector<Matrix> & data, const vector<string> & title)
511 {
512  /*
513  If the file "filename" does not exist yet, created it. The first lines of the file
514  contain the following informations (for each line):
515  1) the number of iterations.
516  2) second line is empty.
517  3) the number(n) of matrix/iteration
518  4) number of rows and number of columns of Matrix 1.
519  5) " " i.
520  6) " " n.
521  7)--------------------------------- (end of header file)
522 
523  example of header file;
524  1120
525 
526  2
527  6 1 titre#1
528  6 1 titre#1
529  ---------------------------------
530  */
531  const char *ptr_filename = filename.c_str(); // transform string to *char
532  if(data.size())
533  {
534  if(!nb_iterations_write)
535  {
536  struct stat buf;
537  if(stat(ptr_filename, &buf) ) // File does not exist
538  {
539  ofstream outvecfile(ptr_filename);
540  if(outvecfile)
541  {
542  outvecfile << "nd_iterations " << nb_iterations_write
543  << " " << endl;
544  outvecfile << "nb_vector " << data.size() << endl;
545  for(unsigned int i = 0; i < data.size(); i++)
546  outvecfile << "nb_rows " << data[i].Nrows() << " "
547  << "nb_cols " << data[i].Ncols() << " "
548  << title[i] << endl;
549  outvecfile << "---------------------------------\n";
550  }
551  else
552  {
553  cerr << "IO_matrix_file::write: can not open file " << filename.c_str() << endl;
554  return IO_COULD_NOT_OPEN_FILE;
555  }
556  }
557  else
558  {
559  ifstream invecfile(ptr_filename, ios::in);
560  if(invecfile)
561  invecfile >> nb_iterations_write;
562  }
563  }
564 
565  ofstream outvecfile(ptr_filename, ios::in | ios::out);
566  if(outvecfile)
567  {
568  outvecfile.seekp(strlen("nb_iterations ")); // position at start of fileObject
569  outvecfile << ++nb_iterations_write << endl;
570  outvecfile.seekp(0, std::ios::end); // position at end of fileObject
571  for(unsigned int i = 0; i < data.size(); i++)
572  {
573  for(int j = 1; j <= data[i].Nrows(); j++) {
574  for(int k = 1; k <= data[i].Ncols(); k++) {
575  outvecfile << data[i](j,k) << " ";
576  }
577  }
578  }
579  outvecfile << endl;
580  outvecfile << endl;
581  }
582  else
583  {
584  cerr << "IO_matrix_file::write: can not open file " << filename.c_str() << endl;
585  return IO_COULD_NOT_OPEN_FILE;
586  }
587  }
588  else
589  {
590  cerr << "IO_matrix_file::write: vector data is empty" << endl;
591  return IO_DATA_EMPTY;
592  }
593 
594  return 0;
595 }
596 
597 
598 short IO_matrix_file::read(vector<Matrix> & data)
600 {
601  vector<string> data_title;
602  string tmp;
603  for(unsigned int i = 1; i <= data.size(); i++)
604  {
605  tmp = "data#"; // Provide a default name
606  tmp += i;
607  data_title.push_back(tmp);
608  }
609 
610  return IO_matrix_file::read(data, data_title);
611 }
612 
613 
614 short IO_matrix_file::read(vector<Matrix> & data, vector<string> & data_title)
616 {
617  /*
618  If the file "filename does not exist yet, created it and fill the first line
619  with the number of rows and columns for each element of "data".
620  ex: 6x1;3x1;3x3;
621  This line indidate that data has 3 elements Matrix. The first one has 6 rows and
622  1 columns, the second one has 3 rows and 1 columns ...
623  */
624  static const char *ptr_filename = filename.c_str(); // transform string to *char
625  ifstream invecfile(ptr_filename, ios::in);
626 
627  if(invecfile)
628  {
629  if(!position_read)
630  {
631  string temp;
632  int nbcol = 0, nbrow = 0;
633  invecfile >> temp >> nb_iterations_read;
634  invecfile >> temp >> nb_element;
635  Matrix mat_tmp;
636  data.clear();
637  data_title.clear();
638  for(int i = 1; i <= nb_element; i++)
639  {
640  data.push_back(mat_tmp);
641  data_title.push_back(temp);
642  }
643  for(int j = 0; j < nb_element; j++)
644  {
645  invecfile >> temp >> nbrow;
646  invecfile >> temp >> nbcol;
647  getline(invecfile,data_title[j]);
648  if( (nbrow != data[j].Nrows()) ||
649  (nbcol != data[j].Ncols()) )
650  data[j] = Matrix(nbrow, nbcol);
651  }
652  invecfile >> temp; //------------------------
653  position_read = invecfile.tellg();
654  }
655 
656  if(position_read > 0)
657  {
658  invecfile.seekg(position_read); // position for reading
659  for(unsigned int ii = 0; ii < data.size(); ii++)
660  for(int jj = 1; jj <= data[ii].Nrows(); jj++)
661  for(int kk = 1; kk <= data[ii].Ncols(); kk++)
662  invecfile >> data[ii](jj,kk);
663 
664  position_read = invecfile.tellg(); // position for next reading
665  }
666  }
667  else
668  {
669  cerr << "IO_matrix_file::read, can not open file" << filename.c_str() << endl;
670  return IO_COULD_NOT_OPEN_FILE;
671  }
672  return 0;
673 }
674 
675 
676 short IO_matrix_file::read_all(vector<Matrix> & data, vector<string> & data_title)
686 {
687  static const char *ptr_filename = filename.c_str(); // transform string to *char
688  ifstream invecfile(ptr_filename, ios::in);
689 
690  if(invecfile)
691  {
692  string temp;
693  int nbcol = 0, nbrow = 0;
694  invecfile >> temp >> nb_iterations_read;
695  invecfile >> temp >> nb_element;
696 
697  Matrix mat_tmp;
698  data.clear();
699  data_title.clear();
700  for(int i = 1; i <= nb_element; i++)
701  {
702  data.push_back(mat_tmp);
703  data_title.push_back(" ");
704  }
705 
706  for(int j = 0; j < nb_element; j++)
707  {
708  invecfile >> temp >> nbrow;
709  invecfile >> temp >> nbcol;
710  if(nbcol >1)
711  return IO_MISMATCH_SIZE;
712 
713  getline(invecfile,data_title[j]);
714  if( (nbrow != data[j].Nrows()) ||
715  (nbcol != data[j].Ncols()) )
716  data[j] = Matrix(nbrow, nbcol*nb_iterations_read);
717  }
718  invecfile >> temp; //---------------------------------
719 
720  for(int k = 1; k <= nb_iterations_read; k++)
721  for(unsigned int ii = 0; ii < data.size(); ii++)
722  for(int jj = 1; jj <= data[ii].Nrows(); jj++)
723  invecfile >> data[ii](jj,k);
724  }
725  else
726  {
727  cerr << "IO_matrix_file::read_all, can not open file " << filename.c_str() << endl;
728  return IO_COULD_NOT_OPEN_FILE;
729  }
730  return 0;
731 }
732 
733 // ---------------------------------------------------------------------------------------
734 
741 Plot_file::Plot_file(const string & filename) : IO_matrix_file(filename), Plot2d()
742 {
743  //clear the buffers in case of error while reading the file
745  {
746  data_from_file.clear();
747  data_title.clear();
748  cerr << "Plot_file::Plot_file: problem in reading file " << filename.c_str() << "." << endl;
749  }
750 }
751 
752 
753 short Plot_file::graph(const string & title_graph, const string & label, const short x,
754  const short y, const short x_start, const short y_start,
755  const short y_end)
757 {
758  if(data_from_file.size())
759  {
760  if(data_from_file[x].Ncols() != data_from_file[y].Ncols())
761  {
762  cerr << "Plot_file::graph: number of rows of xdata and ydata does not match" << endl;
763  return X_Y_DATA_NO_MATCH;
764  }
765 
766  settitle(title_graph.c_str());
767  setxlabel(data_title[x]);
768  setylabel(data_title[y]);
769 
770  string legend;
771  for(int i = y_start; i <= y_end; i++)
772  {
773  ostringstream istr;
774  istr << label << i-y_start+1;
775  legend = istr.str();
776 
777  addcurve((data_from_file[x].SubMatrix(x_start,x_start,1,data_from_file[x].Ncols())
778  & data_from_file[y].SubMatrix(i,i,1,data_from_file[y].Ncols())).t(),
779  legend, DATAPOINTS);
780  }
781  gnuplot();
782  return 0;
783  }
784  else
785  {
786  cerr << "Plot_file::graph: data file buffer is empty." << endl;
787  return PROBLEM_FILE_READING;
788  }
789 }
790 
791 // ---------------------------------------------------------------------------------------------
792 
793 short set_plot2d(const char *title_graph, const char *x_axis_title, const char *y_axis_title,
794  const char *label, LineType_en enLineType, const Matrix &xdata, const Matrix &ydata,
795  int start_y, int end_y)
796 {
797 
798  Plot2d plotgraph;
799  char *legend=0;
800  try
801  {
802  legend = new char[strlen(label)+1];
803  }
804  catch(bad_alloc & e)
805  {
806  cerr << "set_plot2d:: new ran out of memory" << endl;
807  return OUT_OF_MEMORY;
808  }
809 
810  if(xdata.Ncols() != ydata.Ncols())
811  {
812  cerr << "set_plot2d:: number of rows of xdata and ydata does not match" << endl;
813  return X_Y_DATA_NO_MATCH;
814  }
815 
816  plotgraph.settitle(title_graph);
817  plotgraph.setxlabel(x_axis_title);
818  plotgraph.setylabel(y_axis_title);
819 
820  for(int i = start_y; i <= end_y; i++)
821  {
822  snprintf(legend, sizeof(legend), "%s%d", label, i-start_y+1);
823 
824 
825  plotgraph.addcurve((xdata & ydata.SubMatrix(i,i,1,ydata.Ncols())).t(), legend, enLineType);
826  }
827  plotgraph.gnuplot();
828 
829  delete [] legend;
830 
831  return 0;
832 }
833 
834 short set_plot2d(const char *title_graph, const char *x_axis_title, const char *y_axis_title,
835  const vector<char *> label, LineType_en enLineType, const Matrix &xdata,
836  const Matrix &ydata, const vector<int> & data_select)
837 {
838  Plot2d plotgraph;
839 
840  plotgraph.settitle(title_graph);
841  plotgraph.setxlabel(x_axis_title);
842  plotgraph.setylabel(y_axis_title);
843 
844  if(xdata.Ncols() != ydata.Ncols())
845  {
846  cerr << "set_plot2d:: number of rows of xdata and ydata does not match" << endl;
847  return X_Y_DATA_NO_MATCH;
848  }
849  if(data_select.size() != label.size())
850  {
851  cerr << "set_plot2d:: number of labels does not match" << endl;
852  return LABELS_NBR_NO_MATCH;
853  }
854 
855  for(unsigned int i = 0; i < data_select.size(); i++)
856  plotgraph.addcurve((xdata & ydata.SubMatrix(data_select[i],data_select[i],
857  1,ydata.Ncols())).t(), label[i], enLineType);
858  plotgraph.gnuplot();
859 
860  return 0;
861 }
862 
863 
864 short set_plot3d(const Matrix & xyz, const string & title_graph, const string & x_axis_title,
865  const string & y_axis_title, const string & z_axis_title)
866 {
867 
868  Plot3d plotgraph;
869 
870  plotgraph.settitle(title_graph);
871  plotgraph.setxlabel(x_axis_title);
872  plotgraph.setylabel(y_axis_title);
873  plotgraph.setzlabel(z_axis_title);
874  plotgraph.gnuplot(xyz);
875 
876  return 0;
877 }
878 
879 
880 
881 
882 
883 
884 
885 
886 
887 
888 
889 
890 
891 
#define GNUPLOT
Definition: gnugraph.h:74
short set_plot2d(const char *title_graph, const char *x_axis_title, const char *y_axis_title, const char *label, LineType_en enLineType, const Matrix &xdata, const Matrix &ydata, int start_y, int end_y)
Definition: gnugraph.cpp:793
void dump(void)
Method to dump the content of Plot2d to stdout.
Definition: gnugraph.cpp:315
#define IO_MISMATCH_SIZE
Definition: gnugraph.h:191
static const char rcsid[]
RCS/CVS version.
Definition: gnugraph.cpp:74
short read(std::vector< Matrix > &data)
#define LABELS_NBR_NO_MATCH
Definition: gnugraph.h:104
short set_plot3d(const Matrix &xyz, const string &title_graph, const string &x_axis_title, const string &y_axis_title, const string &z_axis_title)
Definition: gnugraph.cpp:864
void setxlabel(const std::string &t)
Set the x axis name.
Definition: gnugraph.cpp:458
void setylabel(const std::string &t)
Set the y axis name.
Definition: gnugraph.cpp:309
Plot_file(const std::string &filename)
Constructor.
Definition: gnugraph.cpp:741
void addcommand(const std::string &gcom)
Add GNUplot command.
Definition: gnugraph.cpp:291
3d plot object.
Definition: gnugraph.h:174
#define OUT_OF_MEMORY
Definition: gnugraph.h:102
void addcurve(const Matrix &data, const std::string &label="", LineType_en enLineType=DATAPOINTS)
Add a curve on the graphic.
Definition: gnugraph.cpp:269
IO_matrix_file(const std::string &filename)
Constructor.
Definition: gnugraph.cpp:478
int Nrows() const
Definition: newmat.h:494
void dump(void)
Method to dump the content of a curve to stdout.
Definition: gnugraph.cpp:112
std::vector< std::string > data_title
Data file title.
Definition: gnugraph.h:232
GNUcurve(void)
Constructor.
Definition: gnugraph.cpp:91
FloatVector * d
Object for one curve.
Definition: gnugraph.h:127
std::vector< Matrix > data_from_file
Data file.
Definition: gnugraph.h:231
#define IO_DATA_EMPTY
Definition: gnugraph.h:192
#define IO_COULD_NOT_OPEN_FILE
Definition: gnugraph.h:190
void settitle(const std::string &t)
Set the title.
Definition: gnugraph.cpp:452
void gnuplot(const Matrix &xyz)
Creates a GNUplot graphic.
Definition: gnugraph.cpp:331
The usual rectangular matrix.
Definition: newmat.h:625
Read and write data at every iterations in a file.
Definition: gnugraph.h:201
void setzlabel(const std::string &t)
Set the y axis name.
Definition: gnugraph.cpp:470
int Ncols() const
Definition: newmat.h:495
void setxlabel(const std::string &t)
Set the x axis name.
Definition: gnugraph.cpp:303
2d plot object.
Definition: gnugraph.h:149
#define PROBLEM_FILE_READING
Definition: gnugraph.h:194
Header file for graphics definitions.
GetSubMatrix SubMatrix(int fr, int lr, int fc, int lc) const
Definition: newmat.h:2146
void settitle(const std::string &t)
Set the title.
Definition: gnugraph.cpp:297
LineType_en
Definition: gnugraph.h:108
short read_all(std::vector< Matrix > &data, std::vector< std::string > &data_title)
Reads all sequences of data.
Definition: gnugraph.cpp:676
short write(const std::vector< Matrix > &data)
Plot2d(void)
Constructor.
Definition: gnugraph.cpp:128
void setylabel(const std::string &t)
Set the y axis name.
Definition: gnugraph.cpp:464
void gnuplot(void)
Creates a GNUplot graphic.
Definition: gnugraph.cpp:133
boost::shared_ptr< GNUcurve > PSHR_Curve
Definition: gnugraph.h:141
#define X_Y_DATA_NO_MATCH
Definition: gnugraph.h:103
short graph(const std::string &title_graph, const std::string &label, const short x, const short y, const short x_start, const short y_start, const short y_end)
Creates a graphic.
Definition: gnugraph.cpp:753
const char * curvetype[]
Definition: gnugraph.cpp:81


kni
Author(s): Martin Günther
autogenerated on Fri Jun 7 2019 22:06:44