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 #include "gnugraph.h"
00064 #include <fstream>
00065
00066 using namespace std;
00067
00068 #ifdef use_namespace
00069 namespace ROBOOP {
00070 using namespace NEWMAT;
00071 #endif
00072
00073 static const char rcsid[] __UNUSED__ = "$Id: gnugraph.cpp,v 1.6 2007/11/14 02:38:24 ejt Exp $";
00074
00075 char *curvetype[] =
00076 {"lines",
00077 "points",
00078 "linespoints",
00079 "impulses",
00080 "dots",
00081 "steps",
00082 "boxes"};
00083
00084
00085 GNUcurve::GNUcurve(void)
00086
00087 {
00088 ctype = LINES;
00089 clabel = "";
00090 }
00091
00092
00093 GNUcurve::GNUcurve(const Matrix & data, const string & label, int type)
00094
00095 {
00096 xy = data;
00097 if(data.Ncols() > 2)
00098 cerr << "GNUcurve::Warning: Only the first two columns are used in GNUcurve\n";
00099
00100 ctype = type;
00101 clabel = label;
00102 }
00103
00104
00105 GNUcurve::GNUcurve(const GNUcurve & gnuc)
00106
00107 {
00108 xy = gnuc.xy;
00109 ctype = gnuc.ctype;
00110 clabel = gnuc.clabel;
00111 }
00112
00113
00114 GNUcurve & GNUcurve::operator=(const GNUcurve & gnuc)
00115
00116 {
00117 xy = gnuc.xy;
00118 ctype = gnuc.ctype;
00119 clabel = gnuc.clabel;
00120
00121 return *this;
00122 }
00123
00124
00125 void GNUcurve::dump(void)
00126
00127 {
00128 cout << "Curve label: " << clabel.c_str() << "\n";
00129 cout << "Curve type: " << curvetype[ctype] << "\n";
00130 cout << "Curve data points: \n";
00131 cout << xy;
00132 cout << "\n\n";
00133 }
00134
00135
00136 Plot2d::Plot2d(void)
00137
00138 {
00139 ncurves = 0;
00140 try
00141 {
00142 curves = new GNUcurve[NCURVESMAX];
00143 }
00144 catch(bad_alloc ex)
00145 {
00146 cerr << "Plot2d::Plot2d:: new ran out of memory" << endl;
00147 }
00148
00149 }
00150
00151
00152 Plot2d::~Plot2d(void)
00153
00154 {
00155 delete [] curves;
00156 }
00157
00158
00159 void Plot2d::gnuplot(void)
00160
00161 {
00162 int i, strl;
00163 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__)
00164 char filename[L_tmpnam];
00165 #else
00166 char filename[] = "tmpfileXXXXXX";
00167 #endif
00168 char * filedata=0;
00169 char * wibsl;
00170 char bsl = '\\';
00171
00172 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__)
00173 tmpnam(filename);
00174 #else
00175 mkstemp(filename);
00176 #endif
00177
00178 while((wibsl = strchr(filename,bsl)) != 0) {
00179 wibsl[0] = '/';
00180 }
00181
00182 {
00183 ofstream fileout(filename);
00184 fileout << gnucommand.c_str();
00185 fileout << "set title \"" << title << "\"\n";
00186 fileout << "set xlabel \"" << xlabel << "\"\n";
00187 fileout << "set ylabel \"" << ylabel << "\"\n";
00188 fileout << "plot \\\n";
00189 for(i = 0; i < ncurves; i++) {
00190 fileout << "\"" << filename << "." << i << "\" ";
00191 fileout << "title \"" << curves[i].clabel << "\" ";
00192 fileout << "with " << curvetype[curves[i].ctype] << " ";
00193 if( i+1 < ncurves){
00194 fileout << ", \\\n";
00195 }
00196 }
00197 fileout << "\n";
00198 }
00199 try
00200 {
00201 filedata = new char[strlen(filename)+3];
00202 }
00203 catch(bad_alloc ex)
00204 {
00205 cerr << "Plot2d::gnuplot:: new ran out of memory" << endl;
00206 }
00207 strcpy(filedata,filename);
00208 strcat(filedata,".");
00209 strl = strlen(filedata);
00210 for(i = 0; i < ncurves; i++) {
00211 sprintf(&filedata[strl],"%d",i);
00212 ofstream fileout(filedata);
00213 #ifndef _MSC_VER // MSVC++ chokes on the next line !
00214 fileout << curves[i].xy;
00215 #else
00216 for(int j = 1; j <= curves[i].xy.Nrows(); j++) {
00217 for(int k = 1; k <= curves[i].xy.Ncols(); k++) {
00218 fileout << curves[i].xy(j,k) << " ";
00219 }
00220 fileout << "\n";
00221 }
00222 #endif
00223 }
00224
00225 #if defined(__WIN32__) || defined(_WIN32) || defined(__NT__) || defined(__CYGWIN__)
00226
00227 char c[L_tmpnam+15];
00228 char *d;
00229 HWND hwndm, hwnd;
00230 if (WinExec(GNUPLOT, SW_SHOWNORMAL) <= 32) {
00231
00232 cout << "Cannot find the gnuplot application\n";
00233 cout << "Press Enter to continue" << endl;
00234 getchar();
00235 remove(filename);
00236 for(i = 0; i < ncurves; i++) {
00237 sprintf(&filedata[strl],"%d",i);
00238 remove(filedata);
00239 }
00240 delete filedata;
00241 return;
00242 } else {
00243
00244 hwndm = FindWindow((LPSTR) 0, (LPSTR) "gnuplot");
00245 }
00246 hwnd= GetWindow(hwndm, GW_CHILD);
00247 if(hwnd == 0) cout << "OUPS!!!\n";
00248 sprintf(c,"load \"%s\" \n",filename);
00249
00250 #ifdef __GNUG__
00251 char ccygnus[] = "cd \"c:\"\n";
00252
00253
00254 d = ccygnus;
00255 while(*d != '\0') {
00256 SendMessage(hwnd,WM_CHAR,*d,1L);
00257 d++;
00258 }
00259 #endif
00260 d = c;
00261 while(*d != '\0') {
00262 SendMessage(hwnd,WM_CHAR,*d,1L);
00263 d++;
00264 }
00265 cout << "Press Enter to continue..." << endl;
00266 getchar();
00267 #else
00268 FILE *command;
00269
00270 command = popen(GNUPLOT,"w");
00271 fprintf(command,"load \"%s\"\n",filename); fflush(command);
00272 fprintf(stderr,"Press Enter to continue...\n"); fflush(stderr);
00273 getchar();
00274 pclose(command);
00275 #endif
00276 remove(filename);
00277 for(i = 0; i < ncurves; i++) {
00278 sprintf(&filedata[strl],"%d",i);
00279 remove(filedata);
00280 }
00281 delete filedata;
00282 }
00283
00284
00285 void Plot2d::addcurve(const Matrix & data, const string & label, int type)
00286
00287 {
00288 ncurves++;
00289 if(ncurves <= NCURVESMAX) {
00290 curves[ncurves-1] = GNUcurve(data,label,type);
00291 } else {
00292 cout << "Too many curves in your Plot2d (maximum 10)\n";
00293 exit(1);
00294 }
00295 }
00296
00297 void Plot2d::addcommand(const string & gcom)
00298
00299 {
00300 gnucommand += gcom;
00301 }
00302
00303 void Plot2d::settitle(const string & t)
00304
00305 {
00306 title = t;
00307 }
00308
00309 void Plot2d::setxlabel(const string & t)
00310
00311 {
00312 xlabel = t;
00313 }
00314
00315 void Plot2d::setylabel(const string & t)
00316
00317 {
00318 ylabel = t;
00319 }
00320
00321 void Plot2d::dump(void)
00322
00323 {
00324 cout << "gnuplot commands:\n" << gnucommand.c_str();
00325 cout << "Plot title: " << title.c_str() << "\n";
00326 cout << "X label: " << xlabel.c_str() << "\n";
00327 cout << "Y label: " << ylabel.c_str() << "\n";
00328 for (int i = 0; i < ncurves; i++) {
00329 cout << "\nCurve #" << i << "\n";
00330 curves[i].dump();
00331 }
00332 }
00333
00334
00335
00336
00337 IO_matrix_file::IO_matrix_file(const string & filename_)
00338
00339 {
00340 filename = filename_;
00341 position_read = 0;
00342 nb_iterations_write = 0;
00343 nb_iterations_read = 0;
00344 nb_element = 0;
00345 }
00346
00347
00348 short IO_matrix_file::write(const vector<Matrix> & data)
00349
00350 {
00351 vector<string> title;
00352 string tmp;
00353 for(int i = 1; i <= (int)data.size(); i++)
00354 {
00355 tmp = "data#";
00356 tmp += i;
00357 title.push_back(tmp);
00358 }
00359
00360 return IO_matrix_file::write(data, title);
00361 }
00362
00363
00364 short IO_matrix_file::write(const std::vector<Matrix> & data, const std::vector<std::string> & title)
00365
00366
00367
00368
00369
00370 {
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390 const char *ptr_filename = filename.c_str();
00391 if(data.size())
00392 {
00393 if(!nb_iterations_write)
00394 {
00395 struct stat buf;
00396 if(stat(ptr_filename, &buf) )
00397 {
00398 ofstream outvecfile(ptr_filename);
00399 if(outvecfile)
00400 {
00401 outvecfile << "nd_iterations " << nb_iterations_write
00402 << " " << endl;
00403 outvecfile << "nb_vector " << data.size() << endl;
00404 for(int i = 0; i < (int)data.size(); i++)
00405 outvecfile << "nb_rows " << data[i].Nrows() << " "
00406 << "nb_cols " << data[i].Ncols() << " "
00407 << title[i] << endl;
00408 outvecfile << "---------------------------------\n";
00409 }
00410 else
00411 {
00412 cerr << "IO_matrix_file::write: can not open file " << filename.c_str() << endl;
00413 return IO_COULD_NOT_OPEN_FILE;
00414 }
00415 }
00416 else
00417 {
00418 ifstream invecfile(ptr_filename, ios::in);
00419 if(invecfile)
00420 invecfile >> nb_iterations_write;
00421 }
00422 }
00423
00424 ofstream outvecfile(ptr_filename, ios::in | ios::out);
00425 if(outvecfile)
00426 {
00427 outvecfile.seekp(strlen("nb_iterations "));
00428 outvecfile << ++nb_iterations_write << endl;
00429 outvecfile.seekp(0, std::ios::end);
00430 for(int i = 0; i < (int)data.size(); i++)
00431 {
00432 for(int j = 1; j <= data[i].Nrows(); j++) {
00433 for(int k = 1; k <= data[i].Ncols(); k++) {
00434 outvecfile << data[i](j,k) << " ";
00435 }
00436 }
00437 }
00438 outvecfile << endl;
00439 outvecfile << endl;
00440 }
00441 else
00442 {
00443 cerr << "IO_matrix_file::write: can not open file " << filename.c_str() << endl;
00444 return IO_COULD_NOT_OPEN_FILE;
00445 }
00446 }
00447 else
00448 {
00449 cerr << "IO_matrix_file::write: vector data is empty" << endl;
00450 return IO_DATA_EMPTY;
00451 }
00452
00453 return 0;
00454 }
00455
00456
00457 short IO_matrix_file::read(vector<Matrix> & data)
00458
00459 {
00460 vector<string> data_title;
00461 string tmp;
00462 for(int i = 1; i <= (int)data.size(); i++)
00463 {
00464 tmp = "data#";
00465 tmp += i;
00466 data_title.push_back(tmp);
00467 }
00468
00469 return IO_matrix_file::read(data, data_title);
00470 }
00471
00472
00473 short IO_matrix_file::read(std::vector<Matrix> & data, std::vector<std::string> & data_title)
00474
00475 {
00476
00477
00478
00479
00480
00481
00482
00483 static const char *ptr_filename = filename.c_str();
00484 ifstream invecfile(ptr_filename, ios::in);
00485
00486 if(invecfile)
00487 {
00488 if(!position_read)
00489 {
00490 #ifdef __WATCOMC__
00491 char temp[256];
00492 #else
00493 string temp;
00494 #endif
00495 int nbcol = 0, nbrow = 0;
00496 invecfile >> temp >> nb_iterations_read;
00497 invecfile >> temp >> nb_element;
00498 Matrix mat_tmp;
00499 data.clear();
00500 data_title.clear();
00501 for(int i = 1; i <= nb_element; i++)
00502 {
00503 data.push_back(mat_tmp);
00504 data_title.push_back(temp);
00505 }
00506 for(int j = 0; j < nb_element; j++)
00507 {
00508 invecfile >> temp >> nbrow;
00509 invecfile >> temp >> nbcol;
00510 #ifdef __WATCOMC__
00511 invecfile >> temp >> data_title[j];
00512 #else
00513 getline(invecfile,data_title[j]);
00514 #endif
00515 if( (nbrow != data[j].Nrows()) ||
00516 (nbcol != data[j].Ncols()) )
00517 data[j] = Matrix(nbrow, nbcol);
00518 }
00519 invecfile >> temp;
00520 position_read = invecfile.tellg();
00521 }
00522
00523 if(position_read > 0)
00524 {
00525 invecfile.seekg(position_read);
00526 for(int ii = 0; ii < (int)data.size(); ii++)
00527 for(int jj = 1; jj <= data[ii].Nrows(); jj++)
00528 for(int kk = 1; kk <= data[ii].Ncols(); kk++)
00529 invecfile >> data[ii](jj,kk);
00530
00531 position_read = invecfile.tellg();
00532 }
00533 }
00534 else
00535 {
00536 cerr << "IO_matrix_file::read, can not open file" << filename.c_str() << endl;
00537 return IO_COULD_NOT_OPEN_FILE;
00538 }
00539 return 0;
00540 }
00541
00542
00543 short IO_matrix_file::read_all(std::vector<Matrix> & data, std::vector<std::string> & data_title)
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553 {
00554 static const char *ptr_filename = filename.c_str();
00555 ifstream invecfile(ptr_filename, ios::in);
00556
00557 if(invecfile)
00558 {
00559 #ifdef __WATCOMC__
00560 char temp[256];
00561 #else
00562 string temp;
00563 #endif
00564 int nbcol = 0, nbrow = 0;
00565 invecfile >> temp >> nb_iterations_read;
00566 invecfile >> temp >> nb_element;
00567
00568 Matrix mat_tmp;
00569 data.clear();
00570 data_title.clear();
00571 for(int i = 1; i <= nb_element; i++)
00572 {
00573 data.push_back(mat_tmp);
00574 data_title.push_back(" ");
00575 }
00576
00577 for(int j = 0; j < nb_element; j++)
00578 {
00579 invecfile >> temp >> nbrow;
00580 invecfile >> temp >> nbcol;
00581 if(nbcol >1)
00582 return IO_MISMATCH_SIZE;
00583
00584 #ifdef __WATCOMC__
00585 invecfile >> temp >> data_title[j];
00586 #else
00587 getline(invecfile,data_title[j]);
00588 #endif
00589 if( (nbrow != data[j].Nrows()) ||
00590 (nbcol != data[j].Ncols()) )
00591 data[j] = Matrix(nbrow, nbcol*nb_iterations_read);
00592 }
00593 invecfile >> temp;
00594
00595 for(int k = 1; k <= nb_iterations_read; k++)
00596 for(int ii = 0; ii < (int)data.size(); ii++)
00597 for(int jj = 1; jj <= data[ii].Nrows(); jj++)
00598 invecfile >> data[ii](jj,k);
00599 }
00600 else
00601 {
00602 cerr << "IO_matrix_file::read_all, can not open file " << filename.c_str() << endl;
00603 return IO_COULD_NOT_OPEN_FILE;
00604 }
00605 return 0;
00606 }
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616 Plot_file::Plot_file(const string & filename) : IO_matrix_file(filename), Plot2d()
00617 {
00618
00619 if(read_all(data_from_file, data_title))
00620 {
00621 data_from_file.clear();
00622 data_title.clear();
00623 cerr << "Plot_file::Plot_file: problem in reading file " << filename.c_str() << "." << endl;
00624 }
00625 }
00626
00627
00628 short Plot_file::graph(const string & title_graph, const string & label, const short x,
00629 const short y, const short x_start, const short y_start,
00630 const short y_end)
00631
00632 {
00633 if(data_from_file.size())
00634 {
00635 if(data_from_file[x].Ncols() != data_from_file[y].Ncols())
00636 {
00637 cerr << "Plot_file::graph: number of rows of xdata and ydata does not match" << endl;
00638 return X_Y_DATA_NO_MATCH;
00639 }
00640
00641 settitle(title_graph.c_str());
00642 setxlabel(data_title[x]);
00643 setylabel(data_title[y]);
00644
00645 string legend;
00646 for(int i = y_start; i <= y_end; i++)
00647 {
00648 #ifdef __WATCOMC__
00649 ostrstream istr;
00650 {
00651 char temp[256];
00652 sprintf(temp,"%d",i-y_start+1);
00653 istr << temp;
00654 }
00655 #else
00656 ostringstream istr;
00657 istr << label << i-y_start+1;
00658 #endif
00659 legend = istr.str();
00660
00661 addcurve((data_from_file[x].SubMatrix(x_start,x_start,1,data_from_file[x].Ncols())
00662 & data_from_file[y].SubMatrix(i,i,1,data_from_file[y].Ncols())).t(),
00663 legend, POINTS);
00664 }
00665 gnuplot();
00666 return 0;
00667 }
00668 else
00669 {
00670 cerr << "Plot_file::graph: data file buffer is empty." << endl;
00671 return PROBLEM_FILE_READING;
00672 }
00673 }
00674
00675
00676
00677 short set_plot2d(const char *title_graph, const char *x_axis_title, const char *y_axis_title,
00678 const char *label, int type, const Matrix &xdata, const Matrix &ydata,
00679 int start_y, int end_y)
00680 {
00681
00682 Plot2d plotgraph;
00683 char *legend=0;
00684 try
00685 {
00686 legend = new char[strlen(label)+1];
00687 }
00688 catch(bad_alloc ex)
00689 {
00690 cerr << "set_plot2d:: new ran out of memory" << endl;
00691 return OUT_OF_MEMORY;
00692 }
00693
00694 if(xdata.Ncols() != ydata.Ncols())
00695 {
00696 cerr << "set_plot2d:: number of rows of xdata and ydata does not match" << endl;
00697 return X_Y_DATA_NO_MATCH;
00698 }
00699
00700 plotgraph.settitle(title_graph);
00701 plotgraph.setxlabel(x_axis_title);
00702 plotgraph.setylabel(y_axis_title);
00703
00704 for(int i = start_y; i <= end_y; i++)
00705 {
00706 snprintf(legend, sizeof(legend), "%s%d", label, i-start_y+1);
00707 plotgraph.addcurve((xdata & ydata.SubMatrix(i,i,1,ydata.Ncols())).t(), legend, type);
00708 }
00709 plotgraph.gnuplot();
00710
00711 delete [] legend;
00712
00713 return 0;
00714 }
00715
00716 short set_plot2d(const char *title_graph, const char *x_axis_title, const char *y_axis_title,
00717 const vector<char *> label, int type, const Matrix &xdata, const Matrix &ydata,
00718 const vector<int> & data_select)
00719 {
00720 Plot2d plotgraph;
00721
00722 plotgraph.settitle(title_graph);
00723 plotgraph.setxlabel(x_axis_title);
00724 plotgraph.setylabel(y_axis_title);
00725
00726 if(xdata.Ncols() != ydata.Ncols())
00727 {
00728 cerr << "set_plot2d:: number of rows of xdata and ydata does not match" << endl;
00729 return X_Y_DATA_NO_MATCH;
00730 }
00731 if(data_select.size() != label.size())
00732 {
00733 cerr << "set_plot2d:: number of labels does not match" << endl;
00734 return LABELS_NBR_NO_MATCH;
00735 }
00736
00737 for(int i = 0; i < (int)data_select.size(); i++)
00738 plotgraph.addcurve((xdata & ydata.SubMatrix(data_select[i],data_select[i],
00739 1,ydata.Ncols())).t(), label[i], type);
00740 plotgraph.gnuplot();
00741
00742 return 0;
00743 }
00744
00745 #ifdef use_namespace
00746 }
00747 #endif
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760