73 static const char rcsid[] =
"$Id: gnugraph.cpp,v 1.44 2006/05/19 17:49:58 gourdeau Exp $";
103 enLineType(enLineType_)
106 if (vdX.size() != vdY.size())
108 cerr <<
"GNUcurve::GNUcurve number of x and y elements does not match." << endl;
115 cout <<
"Curve label: " << clabel << endl;
116 cout <<
"Curve type: " <<
curvetype[enLineType] << endl;
117 cout <<
"Curve data points: \n";
119 unsigned int vSize = vdX.size();
120 for(
unsigned int i = 0; i < vSize; ++i)
122 cout << vdX[i] <<
" " << vdY[i] << endl;
138 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__) 139 char filename[L_tmpnam];
141 char filename[] =
"tmpfileXXXXXX";
147 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__) 150 int fd = mkstemp(filename);
153 std::cerr <<
"mkstemp(" << filename <<
"failed: " << std::strerror(errno) << std::endl;
158 while((wibsl = strchr(filename,bsl)) != 0) {
163 ofstream fileout(filename);
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";
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()){
182 filedata =
new char[strlen(filename)+3];
186 cerr <<
"Plot2d::gnuplot:: new ran out of memory" << endl;
188 strcpy(filedata,filename);
189 strcat(filedata,
".");
190 strl = strlen(filedata);
192 for(i = 0; i < vCurves.size(); i++)
194 sprintf(&filedata[strl],
"%d",i);
195 ofstream fileout(filedata);
200 unsigned int vSize = vCurves[i]->vdX.size();
201 for (
unsigned int j = 0; j < vSize; ++j)
203 fileout << vCurves[i]->vdX[j] <<
" " << vCurves[i]->vdY[j] << endl;
217 #if defined(__WIN32__) || defined(_WIN32) || defined(__NT__) || defined(__CYGWIN__) || defined(__MINGW32__) 222 if (WinExec(
GNUPLOT, SW_SHOWNORMAL) <= 32) {
224 cout <<
"Cannot find the gnuplot application\n";
225 cout <<
"Press Enter to continue" << endl;
228 for(i = 0; i < vCurves.size(); i++) {
229 sprintf(&filedata[strl],
"%d",i);
236 hwndm = FindWindow((LPSTR) 0, (LPSTR)
"gnuplot");
238 hwnd= GetWindow(hwndm, GW_CHILD);
239 if(hwnd == 0) cout <<
"OUPS!!!\n";
240 sprintf(c,
"load \"%s\" \n",filename);
244 SendMessage(hwnd,WM_CHAR,*d,1L);
247 cout <<
"Press Enter to continue..." << endl;
253 fprintf(command,
"load \"%s\"\n",filename); fflush(command);
254 fprintf(stderr,
"Press Enter to continue...\n"); fflush(stderr);
261 for(i = 0; i < vCurves.size(); i++) {
262 sprintf(&filedata[strl],
"%d",i);
275 for(
int j = 1; j <= data.
Nrows(); j++)
277 x.push_back(data(j,1));
278 y.push_back(data(j,2));
286 catch (bad_alloc & e)
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";
323 for (
unsigned int i = 0; i < vCurves.size(); ++i)
325 cout <<
"\nCurve #" << i <<
"\n";
336 cerr <<
"Plot3d::gnuplot: wrong size of xyz data matrix." << endl;
341 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__) 342 char filename[L_tmpnam];
344 char filename[] =
"tmpfileXXXXXX";
350 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__) 353 int fd = mkstemp(filename);
356 std::cerr <<
"mkstemp(" << filename <<
"failed: " << std::strerror(errno) << std::endl;
361 while((wibsl = strchr(filename,bsl)) != 0) {
365 ofstream fileout(filename);
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" <<
" ";
378 filedata =
new char[strlen(filename)+3];
382 cerr <<
"Plot3d::gnuplot:: new ran out of memory" << endl;
384 strcpy(filedata,filename);
385 strcat(filedata,
".");
386 strl = strlen(filedata);
388 sprintf(&filedata[strl],
"%d",0);
389 ofstream fileout(filedata);
391 for(
int j = 0; j < 3*xyz.
Nrows(); j+=3)
394 fileout <<
" " << endl;
397 #if defined(__WIN32__) || defined(_WIN32) || defined(__NT__) || defined(__CYGWIN__) || defined(__MINGW32__) 402 if (WinExec(
GNUPLOT, SW_SHOWNORMAL) <= 32) {
404 cout <<
"Cannot find the gnuplot application\n";
405 cout <<
"Press Enter to continue" << endl;
413 hwndm = FindWindow((LPSTR) 0, (LPSTR)
"gnuplot");
415 hwnd= GetWindow(hwndm, GW_CHILD);
416 if(hwnd == 0) cout <<
"OUPS!!!\n";
417 sprintf(c,
"load \"%s\" \n",filename);
420 char ccygnus[] =
"cd \"c:\"\n";
425 SendMessage(hwnd,WM_CHAR,*d,1L);
431 SendMessage(hwnd,WM_CHAR,*d,1L);
434 cout <<
"Press Enter to continue..." << endl;
439 fprintf(command,
"load \"%s\"\n",filename); fflush(command);
440 fprintf(stderr,
"Press Enter to continue...\n"); fflush(stderr);
446 sprintf(&filedata[strl],
"%d",0);
481 filename = filename_;
483 nb_iterations_write = 0;
484 nb_iterations_read = 0;
492 vector<string> title;
494 for(
unsigned int i = 1; i <= data.size(); i++)
498 title.push_back(tmp);
531 const char *ptr_filename = filename.c_str();
534 if(!nb_iterations_write)
537 if(stat(ptr_filename, &buf) )
539 ofstream outvecfile(ptr_filename);
542 outvecfile <<
"nd_iterations " << nb_iterations_write
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() <<
" " 549 outvecfile <<
"---------------------------------\n";
553 cerr <<
"IO_matrix_file::write: can not open file " << filename.c_str() << endl;
559 ifstream invecfile(ptr_filename, ios::in);
561 invecfile >> nb_iterations_write;
565 ofstream outvecfile(ptr_filename, ios::in | ios::out);
568 outvecfile.seekp(strlen(
"nb_iterations "));
569 outvecfile << ++nb_iterations_write << endl;
570 outvecfile.seekp(0, std::ios::end);
571 for(
unsigned int i = 0; i < data.size(); i++)
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) <<
" ";
584 cerr <<
"IO_matrix_file::write: can not open file " << filename.c_str() << endl;
590 cerr <<
"IO_matrix_file::write: vector data is empty" << endl;
601 vector<string> data_title;
603 for(
unsigned int i = 1; i <= data.size(); i++)
607 data_title.push_back(tmp);
624 static const char *ptr_filename = filename.c_str();
625 ifstream invecfile(ptr_filename, ios::in);
632 int nbcol = 0, nbrow = 0;
633 invecfile >> temp >> nb_iterations_read;
634 invecfile >> temp >> nb_element;
638 for(
int i = 1; i <= nb_element; i++)
640 data.push_back(mat_tmp);
641 data_title.push_back(temp);
643 for(
int j = 0; j < nb_element; j++)
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);
653 position_read = invecfile.tellg();
656 if(position_read > 0)
658 invecfile.seekg(position_read);
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);
664 position_read = invecfile.tellg();
669 cerr <<
"IO_matrix_file::read, can not open file" << filename.c_str() << endl;
687 static const char *ptr_filename = filename.c_str();
688 ifstream invecfile(ptr_filename, ios::in);
693 int nbcol = 0, nbrow = 0;
694 invecfile >> temp >> nb_iterations_read;
695 invecfile >> temp >> nb_element;
700 for(
int i = 1; i <= nb_element; i++)
702 data.push_back(mat_tmp);
703 data_title.push_back(
" ");
706 for(
int j = 0; j < nb_element; j++)
708 invecfile >> temp >> nbrow;
709 invecfile >> temp >> nbcol;
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);
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);
727 cerr <<
"IO_matrix_file::read_all, can not open file " << filename.c_str() << endl;
748 cerr <<
"Plot_file::Plot_file: problem in reading file " << filename.c_str() <<
"." << endl;
754 const short y,
const short x_start,
const short y_start,
762 cerr <<
"Plot_file::graph: number of rows of xdata and ydata does not match" << endl;
771 for(
int i = y_start; i <= y_end; i++)
774 istr << label << i-y_start+1;
786 cerr <<
"Plot_file::graph: data file buffer is empty." << endl;
793 short set_plot2d(
const char *title_graph,
const char *x_axis_title,
const char *y_axis_title,
795 int start_y,
int end_y)
802 legend =
new char[strlen(label)+1];
806 cerr <<
"set_plot2d:: new ran out of memory" << endl;
812 cerr <<
"set_plot2d:: number of rows of xdata and ydata does not match" << endl;
820 for(
int i = start_y; i <= end_y; i++)
822 snprintf(legend,
sizeof(legend),
"%s%d", label, i-start_y+1);
834 short set_plot2d(
const char *title_graph,
const char *x_axis_title,
const char *y_axis_title,
836 const Matrix &ydata,
const vector<int> & data_select)
846 cerr <<
"set_plot2d:: number of rows of xdata and ydata does not match" << endl;
849 if(data_select.size() != label.size())
851 cerr <<
"set_plot2d:: number of labels does not match" << endl;
855 for(
unsigned int i = 0; i < data_select.size(); i++)
857 1,ydata.
Ncols())).t(), label[i], enLineType);
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)
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)
void dump(void)
Method to dump the content of Plot2d to stdout.
static const char rcsid[]
RCS/CVS version.
short read(std::vector< Matrix > &data)
#define LABELS_NBR_NO_MATCH
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)
void setxlabel(const std::string &t)
Set the x axis name.
void setylabel(const std::string &t)
Set the y axis name.
Plot_file(const std::string &filename)
Constructor.
void addcommand(const std::string &gcom)
Add GNUplot command.
void addcurve(const Matrix &data, const std::string &label="", LineType_en enLineType=DATAPOINTS)
Add a curve on the graphic.
IO_matrix_file(const std::string &filename)
Constructor.
void dump(void)
Method to dump the content of a curve to stdout.
std::vector< std::string > data_title
Data file title.
GNUcurve(void)
Constructor.
std::vector< Matrix > data_from_file
Data file.
#define IO_COULD_NOT_OPEN_FILE
void settitle(const std::string &t)
Set the title.
void gnuplot(const Matrix &xyz)
Creates a GNUplot graphic.
The usual rectangular matrix.
Read and write data at every iterations in a file.
void setzlabel(const std::string &t)
Set the y axis name.
void setxlabel(const std::string &t)
Set the x axis name.
#define PROBLEM_FILE_READING
Header file for graphics definitions.
GetSubMatrix SubMatrix(int fr, int lr, int fc, int lc) const
void settitle(const std::string &t)
Set the title.
short read_all(std::vector< Matrix > &data, std::vector< std::string > &data_title)
Reads all sequences of data.
short write(const std::vector< Matrix > &data)
void setylabel(const std::string &t)
Set the y axis name.
void gnuplot(void)
Creates a GNUplot graphic.
boost::shared_ptr< GNUcurve > PSHR_Curve
#define X_Y_DATA_NO_MATCH
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.