00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00073
00074 static const char rcsid[] = "$Id: gnugraph.cpp,v 1.44 2006/05/19 17:49:58 gourdeau Exp $";
00075
00076 #include "gnugraph.h"
00077
00078 using namespace std;
00079
00080
00081 char *curvetype[] =
00082 {"lines",
00083 "points",
00084 "linespoints",
00085 "impulses",
00086 "dots",
00087 "steps",
00088 "boxes"};
00089
00090
00091 GNUcurve::GNUcurve(void)
00093 {
00094 enLineType = LINES;
00095 }
00096
00097
00098 GNUcurve::GNUcurve(const std::vector<double> & x, std::vector<double> & y,
00099 const string & label, LineType_en enLineType_) :
00100 vdX(x),
00101 vdY(y),
00102 clabel(label),
00103 enLineType(enLineType_)
00105 {
00106 if (vdX.size() != vdY.size())
00107 {
00108 cerr << "GNUcurve::GNUcurve number of x and y elements does not match." << endl;
00109 }
00110 }
00111
00112 void GNUcurve::dump(void)
00114 {
00115 cout << "Curve label: " << clabel << endl;
00116 cout << "Curve type: " << curvetype[enLineType] << endl;
00117 cout << "Curve data points: \n";
00118
00119 unsigned int vSize = vdX.size();
00120 for(unsigned int i = 0; i < vSize; ++i)
00121 {
00122 cout << vdX[i] << " " << vdY[i] << endl;
00123 }
00124 cout << endl;
00125 }
00126
00127
00128 Plot2d::Plot2d(void)
00130 {
00131 }
00132
00133 void Plot2d::gnuplot(void)
00135 {
00136 unsigned int i;
00137 int strl;
00138 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__)
00139 char filename[L_tmpnam];
00140 #else
00141 char filename[] = "tmpfileXXXXXX";
00142 #endif
00143 char * filedata=0;
00144 char * wibsl;
00145 char bsl = '\\';
00146
00147 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__)
00148 tmpnam(filename);
00149 #else
00150 mkstemp(filename);
00151 #endif
00152
00153 while((wibsl = strchr(filename,bsl)) != 0) {
00154 wibsl[0] = '/';
00155 }
00156
00157 {
00158 ofstream fileout(filename);
00159 fileout << gnucommand.c_str();
00160 fileout << "set title \"" << title << "\"\n";
00161 fileout << "set xlabel \"" << xlabel << "\"\n";
00162 fileout << "set ylabel \"" << ylabel << "\"\n";
00163 fileout << "plot \\\n";
00164
00165 for(i = 0; i < vCurves.size(); i++) {
00166 fileout << "\"" << filename << "." << i << "\" ";
00167 fileout << "title \"" << vCurves[i]->clabel << "\" ";
00168 fileout << "with " << curvetype[vCurves[i]->enLineType] << " ";
00169 if( i+1 < vCurves.size()){
00170 fileout << ", \\\n";
00171 }
00172 }
00173 fileout << "\n";
00174 }
00175 try
00176 {
00177 filedata = new char[strlen(filename)+3];
00178 }
00179 catch(bad_alloc & e)
00180 {
00181 cerr << "Plot2d::gnuplot:: new ran out of memory" << endl;
00182 }
00183 strcpy(filedata,filename);
00184 strcat(filedata,".");
00185 strl = strlen(filedata);
00186
00187 for(i = 0; i < vCurves.size(); i++)
00188 {
00189 sprintf(&filedata[strl],"%d",i);
00190 ofstream fileout(filedata);
00191
00192
00193
00194
00195 unsigned int vSize = vCurves[i]->vdX.size();
00196 for (unsigned int j = 0; j < vSize; ++j)
00197 {
00198 fileout << vCurves[i]->vdX[j] << " " << vCurves[i]->vdY[j] << endl;
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 }
00211
00212 #if defined(__WIN32__) || defined(_WIN32) || defined(__NT__) || defined(__CYGWIN__) || defined(__MINGW32__)
00213
00214 char c[L_tmpnam+15];
00215 char *d;
00216 HWND hwndm, hwnd;
00217 if (WinExec(GNUPLOT, SW_SHOWNORMAL) <= 32) {
00218
00219 cout << "Cannot find the gnuplot application\n";
00220 cout << "Press Enter to continue" << endl;
00221 getchar();
00222 remove(filename);
00223 for(i = 0; i < vCurves.size(); i++) {
00224 sprintf(&filedata[strl],"%d",i);
00225 remove(filedata);
00226 }
00227 delete filedata;
00228 return;
00229 } else {
00230
00231 hwndm = FindWindow((LPSTR) 0, (LPSTR) "gnuplot");
00232 }
00233 hwnd= GetWindow(hwndm, GW_CHILD);
00234 if(hwnd == 0) cout << "OUPS!!!\n";
00235 sprintf(c,"load \"%s\" \n",filename);
00236
00237 d = c;
00238 while(*d != '\0') {
00239 SendMessage(hwnd,WM_CHAR,*d,1L);
00240 d++;
00241 }
00242 cout << "Press Enter to continue..." << endl;
00243 getchar();
00244 #else
00245 FILE *command;
00246
00247 command = popen(GNUPLOT,"w");
00248 fprintf(command,"load \"%s\"\n",filename); fflush(command);
00249 fprintf(stderr,"Press Enter to continue...\n"); fflush(stderr);
00250 getchar();
00251 pclose(command);
00252 #endif
00253
00254
00255 remove(filename);
00256 for(i = 0; i < vCurves.size(); i++) {
00257 sprintf(&filedata[strl],"%d",i);
00258 remove(filedata);
00259 }
00260 delete filedata;
00261 }
00262
00263
00264 void Plot2d::addcurve(const Matrix & data, const string & label, LineType_en enLineType)
00266 {
00267 vector<double> x;
00268 vector<double> y;
00269
00270 for(int j = 1; j <= data.Nrows(); j++)
00271 {
00272 x.push_back(data(j,1));
00273 y.push_back(data(j,2));
00274 }
00275
00276 try
00277 {
00278 GNUcurve *pCurve = new GNUcurve(x, y, label, enLineType);
00279 vCurves.push_back(PSHR_Curve(pCurve));
00280 }
00281 catch (bad_alloc & e)
00282 {
00283 }
00284 }
00285
00286 void Plot2d::addcommand(const string & gcom)
00288 {
00289 gnucommand += gcom;
00290 }
00291
00292 void Plot2d::settitle(const string & t)
00294 {
00295 title = t;
00296 }
00297
00298 void Plot2d::setxlabel(const string & t)
00300 {
00301 xlabel = t;
00302 }
00303
00304 void Plot2d::setylabel(const string & t)
00306 {
00307 ylabel = t;
00308 }
00309
00310 void Plot2d::dump(void)
00312 {
00313 cout << "gnuplot commands:\n" << gnucommand.c_str();
00314 cout << "Plot title: " << title.c_str() << "\n";
00315 cout << "X label: " << xlabel.c_str() << "\n";
00316 cout << "Y label: " << ylabel.c_str() << "\n";
00317
00318 for (unsigned int i = 0; i < vCurves.size(); ++i)
00319 {
00320 cout << "\nCurve #" << i << "\n";
00321 vCurves[i]->dump();
00322 }
00323 }
00324
00325
00326 void Plot3d::gnuplot(const Matrix & xyz)
00328 {
00329 if (xyz.Ncols() != 3*xyz.Nrows())
00330 {
00331 cerr << "Plot3d::gnuplot: wrong size of xyz data matrix." << endl;
00332 return;
00333 }
00334
00335 int strl;
00336 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__)
00337 char filename[L_tmpnam];
00338 #else
00339 char filename[] = "tmpfileXXXXXX";
00340 #endif
00341 char * filedata=0;
00342 char * wibsl;
00343 char bsl = '\\';
00344
00345 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__)
00346 tmpnam(filename);
00347 #else
00348 mkstemp(filename);
00349 #endif
00350
00351 while((wibsl = strchr(filename,bsl)) != 0) {
00352 wibsl[0] = '/';
00353 }
00354 {
00355 ofstream fileout(filename);
00356 fileout << "set title \"" << title << "\"\n";
00357 fileout << "set xlabel \"" << xlabel << "\"\n";
00358 fileout << "set ylabel \"" << ylabel << "\"\n";
00359 fileout << "set zlabel \"" << zlabel << "\"\n";
00360 fileout << "splot \\\n";
00361 fileout << "\"" << filename << "." << 0 << "\" ";
00362 fileout << "title \"" << "" << "\" ";
00363 fileout << "with " << "linespoints" << " ";
00364 }
00365
00366 try
00367 {
00368 filedata = new char[strlen(filename)+3];
00369 }
00370 catch(bad_alloc & e)
00371 {
00372 cerr << "Plot3d::gnuplot:: new ran out of memory" << endl;
00373 }
00374 strcpy(filedata,filename);
00375 strcat(filedata,".");
00376 strl = strlen(filedata);
00377
00378 sprintf(&filedata[strl],"%d",0);
00379 ofstream fileout(filedata);
00380
00381 for(int j = 0; j < 3*xyz.Nrows(); j+=3)
00382 {
00383 fileout << xyz.SubMatrix(1, xyz.Nrows(), j+1, j+3);
00384 fileout << " " << endl;
00385 }
00386
00387 #if defined(__WIN32__) || defined(_WIN32) || defined(__NT__) || defined(__CYGWIN__) || defined(__MINGW32__)
00388
00389 char c[L_tmpnam+15];
00390 char *d;
00391 HWND hwndm, hwnd;
00392 if (WinExec(GNUPLOT, SW_SHOWNORMAL) <= 32) {
00393
00394 cout << "Cannot find the gnuplot application\n";
00395 cout << "Press Enter to continue" << endl;
00396 getchar();
00397 remove(filename);
00398 remove(filedata);
00399 delete filedata;
00400 return;
00401 } else {
00402
00403 hwndm = FindWindow((LPSTR) 0, (LPSTR) "gnuplot");
00404 }
00405 hwnd= GetWindow(hwndm, GW_CHILD);
00406 if(hwnd == 0) cout << "OUPS!!!\n";
00407 sprintf(c,"load \"%s\" \n",filename);
00408
00409 #ifdef __GNUG__
00410 char ccygnus[] = "cd \"c:\"\n";
00411
00412
00413 d = ccygnus;
00414 while(*d != '\0') {
00415 SendMessage(hwnd,WM_CHAR,*d,1L);
00416 d++;
00417 }
00418 #endif
00419 d = c;
00420 while(*d != '\0') {
00421 SendMessage(hwnd,WM_CHAR,*d,1L);
00422 d++;
00423 }
00424 cout << "Press Enter to continue..." << endl;
00425 getchar();
00426 #else
00427 FILE *command;
00428 command = popen(GNUPLOT,"w");
00429 fprintf(command,"load \"%s\"\n",filename); fflush(command);
00430 fprintf(stderr,"Press Enter to continue...\n"); fflush(stderr);
00431 getchar();
00432 pclose(command);
00433 #endif
00434
00435 remove(filename);
00436 sprintf(&filedata[strl],"%d",0);
00437 remove(filedata);
00438
00439 delete filedata;
00440 }
00441
00442 void Plot3d::settitle(const string & t)
00444 {
00445 title = t;
00446 }
00447
00448 void Plot3d::setxlabel(const string & t)
00450 {
00451 xlabel = t;
00452 }
00453
00454 void Plot3d::setylabel(const string & t)
00456 {
00457 ylabel = t;
00458 }
00459
00460 void Plot3d::setzlabel(const string & t)
00462 {
00463 zlabel = t;
00464 }
00465
00466
00467
00468 IO_matrix_file::IO_matrix_file(const string & filename_)
00470 {
00471 filename = filename_;
00472 position_read = 0;
00473 nb_iterations_write = 0;
00474 nb_iterations_read = 0;
00475 nb_element = 0;
00476 }
00477
00478
00479 short IO_matrix_file::write(const vector<Matrix> & data)
00481 {
00482 vector<string> title;
00483 string tmp;
00484 for(unsigned int i = 1; i <= data.size(); i++)
00485 {
00486 tmp = "data#";
00487 tmp += i;
00488 title.push_back(tmp);
00489 }
00490
00491 return IO_matrix_file::write(data, title);
00492 }
00493
00494
00495 short IO_matrix_file::write(const vector<Matrix> & data, const vector<string> & title)
00501 {
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521 const char *ptr_filename = filename.c_str();
00522 if(data.size())
00523 {
00524 if(!nb_iterations_write)
00525 {
00526 struct stat buf;
00527 if(stat(ptr_filename, &buf) )
00528 {
00529 ofstream outvecfile(ptr_filename);
00530 if(outvecfile)
00531 {
00532 outvecfile << "nd_iterations " << nb_iterations_write
00533 << " " << endl;
00534 outvecfile << "nb_vector " << data.size() << endl;
00535 for(unsigned int i = 0; i < data.size(); i++)
00536 outvecfile << "nb_rows " << data[i].Nrows() << " "
00537 << "nb_cols " << data[i].Ncols() << " "
00538 << title[i] << endl;
00539 outvecfile << "---------------------------------\n";
00540 }
00541 else
00542 {
00543 cerr << "IO_matrix_file::write: can not open file " << filename.c_str() << endl;
00544 return IO_COULD_NOT_OPEN_FILE;
00545 }
00546 }
00547 else
00548 {
00549 ifstream invecfile(ptr_filename, ios::in);
00550 if(invecfile)
00551 invecfile >> nb_iterations_write;
00552 }
00553 }
00554
00555 ofstream outvecfile(ptr_filename, ios::in | ios::out);
00556 if(outvecfile)
00557 {
00558 outvecfile.seekp(strlen("nb_iterations "));
00559 outvecfile << ++nb_iterations_write << endl;
00560 outvecfile.seekp(0, std::ios::end);
00561 for(unsigned int i = 0; i < data.size(); i++)
00562 {
00563 for(int j = 1; j <= data[i].Nrows(); j++) {
00564 for(int k = 1; k <= data[i].Ncols(); k++) {
00565 outvecfile << data[i](j,k) << " ";
00566 }
00567 }
00568 }
00569 outvecfile << endl;
00570 outvecfile << endl;
00571 }
00572 else
00573 {
00574 cerr << "IO_matrix_file::write: can not open file " << filename.c_str() << endl;
00575 return IO_COULD_NOT_OPEN_FILE;
00576 }
00577 }
00578 else
00579 {
00580 cerr << "IO_matrix_file::write: vector data is empty" << endl;
00581 return IO_DATA_EMPTY;
00582 }
00583
00584 return 0;
00585 }
00586
00587
00588 short IO_matrix_file::read(vector<Matrix> & data)
00590 {
00591 vector<string> data_title;
00592 string tmp;
00593 for(unsigned int i = 1; i <= data.size(); i++)
00594 {
00595 tmp = "data#";
00596 tmp += i;
00597 data_title.push_back(tmp);
00598 }
00599
00600 return IO_matrix_file::read(data, data_title);
00601 }
00602
00603
00604 short IO_matrix_file::read(vector<Matrix> & data, vector<string> & data_title)
00606 {
00607
00608
00609
00610
00611
00612
00613
00614 static const char *ptr_filename = filename.c_str();
00615 ifstream invecfile(ptr_filename, ios::in);
00616
00617 if(invecfile)
00618 {
00619 if(!position_read)
00620 {
00621 string temp;
00622 int nbcol = 0, nbrow = 0;
00623 invecfile >> temp >> nb_iterations_read;
00624 invecfile >> temp >> nb_element;
00625 Matrix mat_tmp;
00626 data.clear();
00627 data_title.clear();
00628 for(int i = 1; i <= nb_element; i++)
00629 {
00630 data.push_back(mat_tmp);
00631 data_title.push_back(temp);
00632 }
00633 for(int j = 0; j < nb_element; j++)
00634 {
00635 invecfile >> temp >> nbrow;
00636 invecfile >> temp >> nbcol;
00637 getline(invecfile,data_title[j]);
00638 if( (nbrow != data[j].Nrows()) ||
00639 (nbcol != data[j].Ncols()) )
00640 data[j] = Matrix(nbrow, nbcol);
00641 }
00642 invecfile >> temp;
00643 position_read = invecfile.tellg();
00644 }
00645
00646 if(position_read > 0)
00647 {
00648 invecfile.seekg(position_read);
00649 for(unsigned int ii = 0; ii < data.size(); ii++)
00650 for(int jj = 1; jj <= data[ii].Nrows(); jj++)
00651 for(int kk = 1; kk <= data[ii].Ncols(); kk++)
00652 invecfile >> data[ii](jj,kk);
00653
00654 position_read = invecfile.tellg();
00655 }
00656 }
00657 else
00658 {
00659 cerr << "IO_matrix_file::read, can not open file" << filename.c_str() << endl;
00660 return IO_COULD_NOT_OPEN_FILE;
00661 }
00662 return 0;
00663 }
00664
00665
00666 short IO_matrix_file::read_all(vector<Matrix> & data, vector<string> & data_title)
00676 {
00677 static const char *ptr_filename = filename.c_str();
00678 ifstream invecfile(ptr_filename, ios::in);
00679
00680 if(invecfile)
00681 {
00682 string temp;
00683 int nbcol = 0, nbrow = 0;
00684 invecfile >> temp >> nb_iterations_read;
00685 invecfile >> temp >> nb_element;
00686
00687 Matrix mat_tmp;
00688 data.clear();
00689 data_title.clear();
00690 for(int i = 1; i <= nb_element; i++)
00691 {
00692 data.push_back(mat_tmp);
00693 data_title.push_back(" ");
00694 }
00695
00696 for(int j = 0; j < nb_element; j++)
00697 {
00698 invecfile >> temp >> nbrow;
00699 invecfile >> temp >> nbcol;
00700 if(nbcol >1)
00701 return IO_MISMATCH_SIZE;
00702
00703 getline(invecfile,data_title[j]);
00704 if( (nbrow != data[j].Nrows()) ||
00705 (nbcol != data[j].Ncols()) )
00706 data[j] = Matrix(nbrow, nbcol*nb_iterations_read);
00707 }
00708 invecfile >> temp;
00709
00710 for(int k = 1; k <= nb_iterations_read; k++)
00711 for(unsigned int ii = 0; ii < data.size(); ii++)
00712 for(int jj = 1; jj <= data[ii].Nrows(); jj++)
00713 invecfile >> data[ii](jj,k);
00714 }
00715 else
00716 {
00717 cerr << "IO_matrix_file::read_all, can not open file " << filename.c_str() << endl;
00718 return IO_COULD_NOT_OPEN_FILE;
00719 }
00720 return 0;
00721 }
00722
00723
00724
00731 Plot_file::Plot_file(const string & filename) : IO_matrix_file(filename), Plot2d()
00732 {
00733
00734 if(read_all(data_from_file, data_title))
00735 {
00736 data_from_file.clear();
00737 data_title.clear();
00738 cerr << "Plot_file::Plot_file: problem in reading file " << filename.c_str() << "." << endl;
00739 }
00740 }
00741
00742
00743 short Plot_file::graph(const string & title_graph, const string & label, const short x,
00744 const short y, const short x_start, const short y_start,
00745 const short y_end)
00747 {
00748 if(data_from_file.size())
00749 {
00750 if(data_from_file[x].Ncols() != data_from_file[y].Ncols())
00751 {
00752 cerr << "Plot_file::graph: number of rows of xdata and ydata does not match" << endl;
00753 return X_Y_DATA_NO_MATCH;
00754 }
00755
00756 settitle(title_graph.c_str());
00757 setxlabel(data_title[x]);
00758 setylabel(data_title[y]);
00759
00760 string legend;
00761 for(int i = y_start; i <= y_end; i++)
00762 {
00763 ostringstream istr;
00764 istr << label << i-y_start+1;
00765 legend = istr.str();
00766
00767 addcurve((data_from_file[x].SubMatrix(x_start,x_start,1,data_from_file[x].Ncols())
00768 & data_from_file[y].SubMatrix(i,i,1,data_from_file[y].Ncols())).t(),
00769 legend, DATAPOINTS);
00770 }
00771 gnuplot();
00772 return 0;
00773 }
00774 else
00775 {
00776 cerr << "Plot_file::graph: data file buffer is empty." << endl;
00777 return PROBLEM_FILE_READING;
00778 }
00779 }
00780
00781
00782
00783 short set_plot2d(const char *title_graph, const char *x_axis_title, const char *y_axis_title,
00784 const char *label, LineType_en enLineType, const Matrix &xdata, const Matrix &ydata,
00785 int start_y, int end_y)
00786 {
00787
00788 Plot2d plotgraph;
00789 char *legend=0;
00790 try
00791 {
00792 legend = new char[strlen(label)+1];
00793 }
00794 catch(bad_alloc & e)
00795 {
00796 cerr << "set_plot2d:: new ran out of memory" << endl;
00797 return OUT_OF_MEMORY;
00798 }
00799
00800 if(xdata.Ncols() != ydata.Ncols())
00801 {
00802 cerr << "set_plot2d:: number of rows of xdata and ydata does not match" << endl;
00803 return X_Y_DATA_NO_MATCH;
00804 }
00805
00806 plotgraph.settitle(title_graph);
00807 plotgraph.setxlabel(x_axis_title);
00808 plotgraph.setylabel(y_axis_title);
00809
00810 for(int i = start_y; i <= end_y; i++)
00811 {
00812 snprintf(legend, sizeof(legend), "%s%d", label, i-start_y+1);
00813
00814
00815 plotgraph.addcurve((xdata & ydata.SubMatrix(i,i,1,ydata.Ncols())).t(), legend, enLineType);
00816 }
00817 plotgraph.gnuplot();
00818
00819 delete [] legend;
00820
00821 return 0;
00822 }
00823
00824 short set_plot2d(const char *title_graph, const char *x_axis_title, const char *y_axis_title,
00825 const vector<char *> label, LineType_en enLineType, const Matrix &xdata,
00826 const Matrix &ydata, const vector<int> & data_select)
00827 {
00828 Plot2d plotgraph;
00829
00830 plotgraph.settitle(title_graph);
00831 plotgraph.setxlabel(x_axis_title);
00832 plotgraph.setylabel(y_axis_title);
00833
00834 if(xdata.Ncols() != ydata.Ncols())
00835 {
00836 cerr << "set_plot2d:: number of rows of xdata and ydata does not match" << endl;
00837 return X_Y_DATA_NO_MATCH;
00838 }
00839 if(data_select.size() != label.size())
00840 {
00841 cerr << "set_plot2d:: number of labels does not match" << endl;
00842 return LABELS_NBR_NO_MATCH;
00843 }
00844
00845 for(unsigned int i = 0; i < data_select.size(); i++)
00846 plotgraph.addcurve((xdata & ydata.SubMatrix(data_select[i],data_select[i],
00847 1,ydata.Ncols())).t(), label[i], enLineType);
00848 plotgraph.gnuplot();
00849
00850 return 0;
00851 }
00852
00853
00854 short set_plot3d(const Matrix & xyz, const string & title_graph, const string & x_axis_title,
00855 const string & y_axis_title, const string & z_axis_title)
00856 {
00857
00858 Plot3d plotgraph;
00859
00860 plotgraph.settitle(title_graph);
00861 plotgraph.setxlabel(x_axis_title);
00862 plotgraph.setylabel(y_axis_title);
00863 plotgraph.setzlabel(z_axis_title);
00864 plotgraph.gnuplot(xyz);
00865
00866 return 0;
00867 }
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881