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