gnugraph.cpp
Go to the documentation of this file.
00001 /*
00002 ROBOOP -- A robotics object oriented package in C++
00003 Copyright (C) 1996-2004  Richard Gourdeau
00004 
00005 This library is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU Lesser General Public License as
00007 published by the Free Software Foundation; either version 2.1 of the
00008 License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 
00020 Report problems and direct all questions to:
00021 
00022 Richard Gourdeau
00023 Professeur Agrege
00024 Departement de genie electrique
00025 Ecole Polytechnique de Montreal
00026 C.P. 6079, Succ. Centre-Ville
00027 Montreal, Quebec, H3C 3A7
00028 
00029 email: richard.gourdeau@polymtl.ca
00030 
00031 -------------------------------------------------------------------------------
00032 Revision_history:
00033 
00034 2003/02/03: Etienne Lachance
00035    -Added functions set_plot2d and classe IO_matrix_file.
00036 
00037 2003/29/04: Etienne Lachance
00038    -Class definitions, functions prototype, ... now in gnugraph.h
00039    -Improved class IO_matrix_file.
00040    -Class Plot2d, GNUcurve are now using STL string instead of char*.
00041    -Added class Plot_graph, to create a graph from a data file.
00042    -Replaced all NULL by 0.
00043    -Use mkstemp instead of tmpnam in void Plot2d::gnuplot(void).
00044 
00045 2003/15/08: Etienne Lachance
00046    -The member function IO_matrix_file::write writes data in column of each 
00047     variables, instead of only one. This way it is possible to load a dat file
00048     in Matlab.
00049 
00050 2004/07/01: Etienne Lachance
00051    -Added doxygen documentation.
00052 
00053 2004/07/01: Ethan Tira-Thompson
00054     -Added support for newmat's use_namespace #define, using ROBOOP namespace
00055 
00056 2004/08/10: Etienne Lachance
00057     -Added class Plot3d.
00058     -Removed using ROBOOP namespace
00059 
00060 2005/08/06 : Richard Gourdeau
00061     -fixed catch(bad_alloc)
00062 
00063 2005/08/06 : Carmine Lia
00064     -added defined(__MINGW32__) for temp files
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 const 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); /* generate a temporary file name */
00149 #else
00150    int fd = mkstemp(filename);
00151    if (fd == -1)
00152    {
00153      std::cerr << "mkstemp(" << filename << "failed: " << std::strerror(errno) << std::endl;
00154      return;
00155    }
00156 #endif
00157    /* replacing \ by / */
00158    while((wibsl = strchr(filename,bsl)) != 0) {
00159       wibsl[0] = '/';
00160    }
00161 
00162    {
00163       ofstream fileout(filename); /* write the command file */
00164       fileout << gnucommand.c_str();
00165       fileout << "set title \"" << title << "\"\n";
00166       fileout << "set xlabel \"" << xlabel << "\"\n";
00167       fileout << "set ylabel \"" << ylabel << "\"\n";
00168       fileout << "plot \\\n";
00169 
00170       for(i = 0; i < vCurves.size(); i++) {
00171          fileout << "\"" << filename << "." << i << "\" ";
00172          fileout << "title \"" << vCurves[i]->clabel << "\" ";
00173          fileout << "with " << curvetype[vCurves[i]->enLineType] << " ";
00174          if( i+1 < vCurves.size()){
00175             fileout << ", \\\n";
00176          }
00177       }
00178       fileout << "\n";
00179    }
00180    try
00181    {
00182       filedata = new char[strlen(filename)+3];
00183    }
00184    catch(bad_alloc & e)
00185    {
00186       cerr << "Plot2d::gnuplot:: new ran out of memory" << endl;
00187    }
00188    strcpy(filedata,filename);
00189    strcat(filedata,".");
00190    strl = strlen(filedata);
00191 
00192    for(i = 0; i < vCurves.size(); i++)  /* write the data files */
00193    {
00194       sprintf(&filedata[strl],"%d",i);
00195       ofstream fileout(filedata);
00196 
00197 //#ifndef _MSC_VER // MSVC++ chokes on the next line !
00198        // fileout << curves[i].xy;
00199 //#else
00200       unsigned int vSize = vCurves[i]->vdX.size();
00201       for (unsigned int j = 0; j < vSize; ++j)
00202       {
00203          fileout << vCurves[i]->vdX[j] << " " << vCurves[i]->vdY[j] << endl;
00204       }
00205 
00206 /*
00207         for(int j = 1; j <= curves[i].xy.Nrows(); j++) {
00208             for(int k = 1; k <= curves[i].xy.Ncols(); k++) {
00209                 fileout << curves[i].xy(j,k) << " ";
00210             }
00211             fileout << "\n";
00212         }
00213 */
00214 //#endif
00215    }
00216    /* all command and data files are ready for the call to gnuplot */
00217 #if defined(__WIN32__) || defined(_WIN32) || defined(__NT__) || defined(__CYGWIN__) || defined(__MINGW32__)
00218    /* Windows 95/98/NT/2000 etc */
00219    char c[L_tmpnam+15];
00220    char *d;
00221    HWND hwndm, hwnd;
00222    if (WinExec(GNUPLOT, SW_SHOWNORMAL) <= 32) { /* start gnuplot */
00223       /* failed */
00224       cout << "Cannot find the gnuplot application\n";
00225       cout << "Press Enter to continue" << endl;
00226       getchar();
00227       remove(filename); /* clean up the files and return */
00228       for(i = 0; i < vCurves.size(); i++) {
00229          sprintf(&filedata[strl],"%d",i);
00230          remove(filedata);
00231       }
00232       delete filedata;
00233       return;
00234    } else { /* succeed */
00235       /* get gnuplot main window handle */
00236       hwndm = FindWindow((LPSTR) 0, (LPSTR) "gnuplot");
00237    }
00238    hwnd= GetWindow(hwndm, GW_CHILD); /* get gnuplot command area handle */
00239    if(hwnd == 0) cout << "OUPS!!!\n"; /* something wrong happened */
00240    sprintf(c,"load \"%s\" \n",filename); /* load command for the plot */
00241 
00242    d = c;
00243    while(*d != '\0') { /* sending the command through windows messages */
00244       SendMessage(hwnd,WM_CHAR,*d,1L);
00245       d++;
00246    }
00247    cout << "Press Enter to continue..." << endl;
00248    getchar();
00249 #else      /*  using a pipe under Unix */
00250    FILE *command;
00251 
00252    command = popen(GNUPLOT,"w");
00253    fprintf(command,"load \"%s\"\n",filename); fflush(command);
00254    fprintf(stderr,"Press Enter to continue...\n"); fflush(stderr);
00255    getchar();
00256    pclose(command);
00257 #endif
00258 
00259 
00260   remove(filename);
00261   for(i = 0; i < vCurves.size(); i++) {
00262     sprintf(&filedata[strl],"%d",i);
00263     remove(filedata);
00264   }
00265   delete filedata;
00266 }
00267 
00268 
00269 void Plot2d::addcurve(const Matrix & data, const string & label, LineType_en enLineType)
00271 {
00272     vector<double> x;
00273     vector<double> y;
00274 
00275     for(int j = 1; j <= data.Nrows(); j++) 
00276     {
00277         x.push_back(data(j,1));
00278         y.push_back(data(j,2));
00279     }
00280 
00281     try
00282     {
00283         GNUcurve *pCurve = new GNUcurve(x, y, label, enLineType);
00284         vCurves.push_back(PSHR_Curve(pCurve));
00285     }
00286     catch (bad_alloc & e)
00287     {
00288     }
00289 }
00290 
00291 void Plot2d::addcommand(const string & gcom)
00293 {
00294    gnucommand += gcom;
00295 }
00296 
00297 void Plot2d::settitle(const string & t)
00299 {
00300    title = t;
00301 }
00302 
00303 void Plot2d::setxlabel(const string & t)
00305 {
00306    xlabel = t;
00307 }
00308 
00309 void Plot2d::setylabel(const string & t)
00311 {
00312    ylabel = t;
00313 }
00314 
00315 void Plot2d::dump(void)
00317 {
00318    cout << "gnuplot commands:\n" << gnucommand.c_str();
00319    cout << "Plot title: " << title.c_str() << "\n";
00320    cout << "X label:    " << xlabel.c_str() << "\n";
00321    cout << "Y label:    " << ylabel.c_str() << "\n";
00322 
00323    for (unsigned int i = 0; i < vCurves.size(); ++i) 
00324     {
00325       cout << "\nCurve #" << i << "\n";
00326       vCurves[i]->dump();
00327    }
00328 }
00329 
00330 
00331 void Plot3d::gnuplot(const Matrix & xyz)
00333 {
00334     if (xyz.Ncols() != 3*xyz.Nrows())
00335     {
00336         cerr << "Plot3d::gnuplot: wrong size of xyz data matrix." << endl;
00337         return;
00338     }
00339 
00340    int strl;
00341 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__)
00342    char filename[L_tmpnam];
00343 #else
00344    char filename[] = "tmpfileXXXXXX";
00345 #endif
00346    char * filedata=0;
00347    char * wibsl;
00348    char bsl = '\\';
00349 
00350 #if defined(__BCPLUSPLUS__) || defined(_MSC_VER) || defined(__WATCOMC__) || defined(__MINGW32__)
00351    tmpnam(filename); /* generate a temporary file name */
00352 #else
00353    int fd = mkstemp(filename);
00354    if (fd == -1)
00355    {
00356      std::cerr << "mkstemp(" << filename << "failed: " << std::strerror(errno) << std::endl;
00357      return;
00358    }
00359 #endif
00360 
00361    while((wibsl = strchr(filename,bsl)) != 0) {
00362       wibsl[0] = '/';
00363    }
00364    {
00365        ofstream fileout(filename); // write the command file 
00366        fileout << "set title \"" << title << "\"\n";
00367        fileout << "set xlabel \"" << xlabel << "\"\n";
00368        fileout << "set ylabel \"" << ylabel << "\"\n";
00369        fileout << "set zlabel \"" << zlabel << "\"\n";
00370        fileout << "splot \\\n";   
00371        fileout << "\"" << filename << "." << 0 << "\" ";
00372        fileout << "title \"" << "" << "\" ";
00373        fileout << "with " << "linespoints" << " ";
00374    }
00375    
00376    try
00377    {
00378       filedata = new char[strlen(filename)+3];
00379    }
00380    catch(bad_alloc & e)
00381    {
00382       cerr << "Plot3d::gnuplot:: new ran out of memory" << endl;
00383    }
00384    strcpy(filedata,filename);
00385    strcat(filedata,".");
00386    strl = strlen(filedata);
00387 
00388    sprintf(&filedata[strl],"%d",0);
00389    ofstream fileout(filedata);
00390 
00391    for(int j = 0; j < 3*xyz.Nrows(); j+=3)
00392    {
00393        fileout << xyz.SubMatrix(1, xyz.Nrows(), j+1, j+3);
00394        fileout << " " << endl;
00395    }
00396 
00397 #if defined(__WIN32__) || defined(_WIN32) || defined(__NT__) || defined(__CYGWIN__) || defined(__MINGW32__)
00398    /* Windows 95/98/NT/2000 etc */
00399    char c[L_tmpnam+15];
00400    char *d;
00401    HWND hwndm, hwnd;
00402    if (WinExec(GNUPLOT, SW_SHOWNORMAL) <= 32) { /* start gnuplot */
00403       /* failed */
00404       cout << "Cannot find the gnuplot application\n";
00405       cout << "Press Enter to continue" << endl;
00406       getchar();
00407       remove(filename); /* clean up the files and return */
00408       remove(filedata);
00409       delete filedata;
00410       return;
00411    } else { /* succeed */
00412       /* get gnuplot main window handle */
00413       hwndm = FindWindow((LPSTR) 0, (LPSTR) "gnuplot");
00414    }
00415    hwnd= GetWindow(hwndm, GW_CHILD); /* get gnuplot command area handle */
00416    if(hwnd == 0) cout << "OUPS!!!\n"; /* something wrong happened */
00417    sprintf(c,"load \"%s\" \n",filename); /* load command for the plot */
00418 
00419 #ifdef __GNUG__        /* Cygnus Gnu C++ for win32*/
00420    char ccygnus[] = "cd \"c:\"\n"; /* this string should reflect
00421                                       the drive used to mount / 
00422                                       where /tmp is located */
00423    d = ccygnus;
00424    while(*d != '\0') { /* sending the command through windows messages */
00425       SendMessage(hwnd,WM_CHAR,*d,1L);
00426       d++;
00427    }
00428 #endif
00429    d = c;
00430    while(*d != '\0') { /* sending the command through windows messages */
00431       SendMessage(hwnd,WM_CHAR,*d,1L);
00432       d++;
00433    }
00434    cout << "Press Enter to continue..." << endl;
00435    getchar();
00436 #else      /*  using a pipe under Unix */
00437    FILE *command;
00438    command = popen(GNUPLOT,"w");
00439    fprintf(command,"load \"%s\"\n",filename); fflush(command);
00440    fprintf(stderr,"Press Enter to continue...\n"); fflush(stderr);
00441    getchar();
00442    pclose(command);
00443 #endif
00444 
00445    remove(filename);
00446    sprintf(&filedata[strl],"%d",0);
00447    remove(filedata);
00448 
00449    delete filedata;
00450 }
00451 
00452 void Plot3d::settitle(const string & t)
00454 {
00455    title = t;
00456 }
00457 
00458 void Plot3d::setxlabel(const string & t)
00460 {
00461    xlabel = t;
00462 }
00463 
00464 void Plot3d::setylabel(const string & t)
00466 {
00467    ylabel = t;
00468 }
00469 
00470 void Plot3d::setzlabel(const string & t)
00472 {
00473    zlabel = t;
00474 }
00475 
00476 // ---------------------------------------------------------------------------------------
00477 
00478 IO_matrix_file::IO_matrix_file(const string & filename_)
00480 {
00481    filename = filename_;
00482    position_read = 0;
00483    nb_iterations_write = 0;
00484    nb_iterations_read = 0;
00485    nb_element = 0;
00486 }
00487 
00488 
00489 short IO_matrix_file::write(const vector<Matrix> & data)
00491 {
00492    vector<string> title;
00493    string tmp;
00494    for(unsigned int i = 1; i <= data.size(); i++)
00495    {
00496       tmp = "data#";  // Provide a default name
00497       tmp += i;
00498       title.push_back(tmp);
00499    }
00500 
00501    return IO_matrix_file::write(data, title);
00502 }
00503 
00504 
00505 short IO_matrix_file::write(const vector<Matrix> & data, const vector<string> & title)
00511 {
00512    /*
00513    If the file "filename" does not exist yet, created it. The first lines of the file
00514    contain the following informations (for each line):
00515       1) the number of iterations.
00516       2) second line is empty.
00517       3) the number(n) of matrix/iteration
00518       4) number of rows and number of columns of Matrix 1.
00519       5)  "                                         "   i.
00520       6)  "                                         "   n.
00521       7)---------------------------------   (end of header file)
00522 
00523    example of header file;
00524    1120
00525        
00526    2
00527    6 1 titre#1
00528    6 1 titre#1
00529    ---------------------------------
00530    */
00531    const char *ptr_filename = filename.c_str(); // transform string to *char
00532    if(data.size())
00533    {
00534       if(!nb_iterations_write)
00535       {
00536          struct stat buf;
00537          if(stat(ptr_filename, &buf) )  // File does not exist
00538          {
00539             ofstream outvecfile(ptr_filename);
00540             if(outvecfile)
00541             {
00542                outvecfile << "nd_iterations " << nb_iterations_write
00543                << "        " << endl;
00544                outvecfile << "nb_vector " << data.size() << endl;
00545                for(unsigned int i = 0; i < data.size(); i++)
00546                   outvecfile << "nb_rows " << data[i].Nrows() << "  "
00547                   << "nb_cols " << data[i].Ncols() <<  "  "
00548                   << title[i] << endl;
00549                outvecfile << "---------------------------------\n";
00550             }
00551             else
00552             {
00553                cerr << "IO_matrix_file::write: can not open file " << filename.c_str() << endl;
00554                return IO_COULD_NOT_OPEN_FILE;
00555             }
00556          }
00557          else
00558          {
00559             ifstream invecfile(ptr_filename, ios::in);
00560             if(invecfile)
00561                invecfile >> nb_iterations_write;
00562          }
00563       }
00564 
00565       ofstream outvecfile(ptr_filename, ios::in | ios::out);
00566       if(outvecfile)
00567       {
00568          outvecfile.seekp(strlen("nb_iterations ")); // position at start of fileObject
00569          outvecfile << ++nb_iterations_write << endl;
00570          outvecfile.seekp(0, std::ios::end); // position at end of fileObject
00571          for(unsigned int i = 0; i < data.size(); i++)
00572          {
00573             for(int j = 1; j <= data[i].Nrows(); j++) {
00574                for(int k = 1; k <= data[i].Ncols(); k++) {
00575                   outvecfile << data[i](j,k) << " ";
00576                }
00577             }
00578          }
00579          outvecfile << endl;
00580          outvecfile << endl;
00581       }
00582       else
00583       {
00584          cerr << "IO_matrix_file::write: can not open file " << filename.c_str() << endl;
00585          return IO_COULD_NOT_OPEN_FILE;
00586       }
00587    }
00588    else
00589    {
00590       cerr << "IO_matrix_file::write: vector data is empty" << endl;
00591       return IO_DATA_EMPTY;
00592    }
00593 
00594    return 0;
00595 }
00596 
00597 
00598 short IO_matrix_file::read(vector<Matrix> & data)
00600 {
00601    vector<string> data_title;
00602    string tmp;
00603    for(unsigned int i = 1; i <= data.size(); i++)
00604    {
00605       tmp = "data#";  // Provide a default name
00606       tmp += i;
00607       data_title.push_back(tmp);
00608    }
00609 
00610    return IO_matrix_file::read(data, data_title);
00611 }
00612 
00613 
00614 short IO_matrix_file::read(vector<Matrix> & data, vector<string> & data_title)
00616 {
00617    /*
00618    If the file "filename does not exist yet, created it and fill the first line
00619    with the number of rows and columns for each element of "data".
00620    ex: 6x1;3x1;3x3;
00621    This line indidate that data has 3 elements Matrix. The first one has 6 rows and
00622    1 columns, the second one has 3 rows and 1 columns ...
00623    */
00624    static const char *ptr_filename = filename.c_str(); // transform string to *char
00625    ifstream invecfile(ptr_filename, ios::in);
00626 
00627    if(invecfile)
00628    {
00629       if(!position_read)
00630       {
00631          string temp;
00632          int nbcol = 0, nbrow = 0;
00633          invecfile >> temp >> nb_iterations_read;
00634          invecfile >> temp >> nb_element;
00635          Matrix mat_tmp;
00636          data.clear();
00637          data_title.clear();
00638          for(int i = 1; i <= nb_element; i++)
00639          {
00640             data.push_back(mat_tmp);
00641             data_title.push_back(temp);
00642          }
00643          for(int j = 0; j < nb_element; j++)
00644          {
00645             invecfile >> temp >> nbrow;
00646             invecfile >> temp >> nbcol;
00647             getline(invecfile,data_title[j]);
00648             if( (nbrow != data[j].Nrows()) ||
00649                   (nbcol != data[j].Ncols()) )
00650                data[j] = Matrix(nbrow, nbcol);
00651          }
00652          invecfile >> temp;  //------------------------
00653          position_read = invecfile.tellg();
00654       }
00655 
00656       if(position_read > 0)
00657       {
00658          invecfile.seekg(position_read); // position for reading
00659          for(unsigned int ii = 0; ii < data.size(); ii++)
00660             for(int jj = 1; jj <= data[ii].Nrows(); jj++)
00661                for(int kk = 1; kk <= data[ii].Ncols(); kk++)
00662                   invecfile >> data[ii](jj,kk);
00663 
00664          position_read = invecfile.tellg(); // position for next reading
00665       }
00666    }
00667    else
00668    {
00669       cerr << "IO_matrix_file::read, can not open file" << filename.c_str() << endl;
00670       return IO_COULD_NOT_OPEN_FILE;
00671    }
00672    return 0;
00673 }
00674 
00675 
00676 short IO_matrix_file::read_all(vector<Matrix> & data, vector<string> & data_title)
00686 {
00687    static const char *ptr_filename = filename.c_str(); // transform string to *char
00688    ifstream invecfile(ptr_filename, ios::in);
00689 
00690    if(invecfile)
00691    {
00692       string temp;
00693       int nbcol = 0, nbrow = 0;
00694       invecfile >> temp >> nb_iterations_read;
00695       invecfile >> temp >> nb_element;
00696 
00697       Matrix mat_tmp;
00698       data.clear();
00699       data_title.clear();
00700       for(int i = 1; i <= nb_element; i++)
00701       {
00702          data.push_back(mat_tmp);
00703          data_title.push_back(" ");
00704       }
00705 
00706       for(int j = 0; j < nb_element; j++)
00707       {
00708          invecfile >> temp >> nbrow;
00709          invecfile >> temp >> nbcol;
00710          if(nbcol >1)
00711             return IO_MISMATCH_SIZE;
00712 
00713          getline(invecfile,data_title[j]);
00714          if( (nbrow != data[j].Nrows()) ||
00715                (nbcol != data[j].Ncols()) )
00716             data[j] = Matrix(nbrow, nbcol*nb_iterations_read);
00717       }
00718       invecfile >> temp;  //---------------------------------
00719 
00720       for(int k = 1; k <= nb_iterations_read; k++)
00721          for(unsigned int ii = 0; ii < data.size(); ii++)
00722             for(int jj = 1; jj <= data[ii].Nrows(); jj++)
00723                invecfile >> data[ii](jj,k);
00724    }
00725    else
00726    {
00727       cerr << "IO_matrix_file::read_all, can not open file " << filename.c_str() << endl;
00728       return IO_COULD_NOT_OPEN_FILE;
00729    }
00730    return 0;
00731 }
00732 
00733 // ---------------------------------------------------------------------------------------
00734 
00741 Plot_file::Plot_file(const string & filename) : IO_matrix_file(filename), Plot2d()
00742 {
00743    //clear the buffers in case of error while reading the file
00744    if(read_all(data_from_file, data_title))
00745    {
00746       data_from_file.clear();
00747       data_title.clear();
00748       cerr << "Plot_file::Plot_file: problem in reading file " << filename.c_str() << "." << endl;
00749    }
00750 }
00751 
00752 
00753 short Plot_file::graph(const string & title_graph, const string & label, const short x,
00754                        const short y, const short x_start, const short y_start,
00755                        const short y_end)
00757 {
00758    if(data_from_file.size())
00759    {
00760       if(data_from_file[x].Ncols() != data_from_file[y].Ncols())
00761       {
00762          cerr << "Plot_file::graph: number of rows of xdata and ydata does not match" << endl;
00763          return X_Y_DATA_NO_MATCH;
00764       }
00765 
00766       settitle(title_graph.c_str());
00767       setxlabel(data_title[x]);
00768       setylabel(data_title[y]);
00769 
00770       string legend;
00771       for(int i = y_start; i <= y_end; i++)
00772       {
00773          ostringstream istr;
00774          istr << label << i-y_start+1;
00775          legend = istr.str();
00776 
00777          addcurve((data_from_file[x].SubMatrix(x_start,x_start,1,data_from_file[x].Ncols())
00778                    & data_from_file[y].SubMatrix(i,i,1,data_from_file[y].Ncols())).t(),
00779                   legend, DATAPOINTS);
00780       }
00781       gnuplot();
00782       return 0;
00783    }
00784    else
00785    {
00786       cerr << "Plot_file::graph: data file buffer is empty." << endl;
00787       return PROBLEM_FILE_READING;
00788    }
00789 }
00790 
00791 // ---------------------------------------------------------------------------------------------
00792 
00793 short set_plot2d(const char *title_graph, const char *x_axis_title, const char *y_axis_title,
00794                  const char *label, LineType_en enLineType, const Matrix &xdata, const Matrix &ydata,
00795                  int start_y, int end_y)
00796 {
00797 
00798    Plot2d plotgraph;
00799    char *legend=0;
00800    try
00801    {
00802       legend = new char[strlen(label)+1];
00803    }
00804    catch(bad_alloc & e)
00805    {
00806       cerr << "set_plot2d:: new ran out of memory" << endl;
00807       return OUT_OF_MEMORY;
00808    }
00809 
00810    if(xdata.Ncols() != ydata.Ncols())
00811    {
00812       cerr << "set_plot2d:: number of rows of xdata and ydata does not match" << endl;
00813       return X_Y_DATA_NO_MATCH;
00814    }
00815 
00816    plotgraph.settitle(title_graph);
00817    plotgraph.setxlabel(x_axis_title);
00818    plotgraph.setylabel(y_axis_title);
00819 
00820    for(int i = start_y; i <= end_y; i++)
00821    {
00822       snprintf(legend, sizeof(legend), "%s%d", label, i-start_y+1);
00823 
00824 
00825       plotgraph.addcurve((xdata & ydata.SubMatrix(i,i,1,ydata.Ncols())).t(), legend, enLineType);
00826    }
00827    plotgraph.gnuplot();
00828 
00829    delete [] legend;
00830 
00831    return 0;
00832 }
00833 
00834 short set_plot2d(const char *title_graph, const char *x_axis_title, const char *y_axis_title,
00835                  const vector<char *> label, LineType_en enLineType, const Matrix &xdata, 
00836                  const Matrix &ydata, const vector<int> & data_select)
00837 {
00838    Plot2d plotgraph;
00839 
00840    plotgraph.settitle(title_graph);
00841    plotgraph.setxlabel(x_axis_title);
00842    plotgraph.setylabel(y_axis_title);
00843 
00844    if(xdata.Ncols() != ydata.Ncols())
00845    {
00846       cerr << "set_plot2d:: number of rows of xdata and ydata does not match" << endl;
00847       return X_Y_DATA_NO_MATCH;
00848    }
00849    if(data_select.size() != label.size())
00850    {
00851       cerr << "set_plot2d:: number of labels does not match" << endl;
00852       return LABELS_NBR_NO_MATCH;
00853    }
00854 
00855    for(unsigned int i = 0; i < data_select.size(); i++)
00856       plotgraph.addcurve((xdata & ydata.SubMatrix(data_select[i],data_select[i],
00857                           1,ydata.Ncols())).t(), label[i], enLineType);
00858    plotgraph.gnuplot();
00859 
00860    return 0;
00861 }
00862 
00863 
00864 short set_plot3d(const Matrix & xyz, const string & title_graph, const string & x_axis_title, 
00865                  const string & y_axis_title, const string & z_axis_title)
00866 {
00867 
00868    Plot3d plotgraph;
00869 
00870    plotgraph.settitle(title_graph);
00871    plotgraph.setxlabel(x_axis_title);
00872    plotgraph.setylabel(y_axis_title);
00873    plotgraph.setzlabel(z_axis_title);
00874    plotgraph.gnuplot(xyz);
00875 
00876    return 0;
00877 }
00878 
00879 
00880 
00881 
00882 
00883 
00884 
00885 
00886 
00887 
00888 
00889 
00890 
00891 


kni
Author(s): Martin Günther
autogenerated on Mon Aug 14 2017 02:44:12