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