00001 #include "gnuplot.h"
00002 #include <iostream>
00003 #include <algorithm>
00004 #include <functional>
00005 #include <fstream>
00006 #include "exception.h"
00007 using namespace std;
00008
00009 int GnuPlot::m_count = 0;
00010
00011
00012
00013 GnuPlot::GnuPlot()
00014
00015 {
00016 m_font = "";
00017 char aux[100];
00018 m_dir = "./GnuPlot";
00019 string tmp = m_dir;
00020 tmp+="/TMP_00001";
00021
00022
00023
00024
00025 sprintf(aux,"rm %s/TMP* %s/DAT* ",m_dir.c_str(),m_dir.c_str());
00026 runSystemCommand(aux);
00027
00028
00029 m_MinX = m_MinY = -10;
00030 m_MaxX = m_MinY = 10;
00031 m_file = fopen(tmp.c_str(),"wr");
00032 if (m_file == NULL) {
00033 throw new Exception("GnupPlot: Arquivo temporario %s nao pode ser criado.... Exiting",tmp.c_str());
00034
00035 }
00036 m_List.push_back(tmp);
00037 m_bPlot = false;
00038 m_bPlot3D = false;
00039 m_bLeave = true;
00040
00041 }
00042
00043
00044 GnuPlot::~GnuPlot()
00045 {
00046
00047 }
00048
00049 void GnuPlot::plot(string expr, string name,string comp) {
00050 if (m_bPlot3D)
00051 {
00052 cerr << "Graficos 2D e 3D na mesma cena nao pode ser feito\n";
00053 this->finalize();
00054 }
00055
00056
00057 if (!m_bPlot){
00058 fprintf(m_file,"plot %s title \"%s\" %s",expr.c_str(),name.c_str(),comp.c_str());
00059 m_bPlot = true;
00060 }
00061 else{
00062 fprintf(m_file,", %s title \"%s\" %s",expr.c_str(),name.c_str(),comp.c_str());
00063 }
00064 }
00065
00066
00070 void GnuPlot::splot(std::string expr,std::string name,std::string comp)
00071 {
00072 if (m_bPlot)
00073 {
00074 cerr << "splot:: Graficos 2D e 3D na mesma cena nao pode ser feito\n";
00075 this->finalize();
00076 }
00077 if (!m_bPlot3D){
00078 fprintf(m_file,"splot %s title \"%s\" %s",expr.c_str(),name.c_str(),comp.c_str());
00079 m_bPlot3D = true;
00080 }
00081 else{
00082 fprintf(m_file,", %s title \"%s\" %s",expr.c_str(),name.c_str(),comp.c_str());
00083 }
00084 }
00085
00086
00087
00088 void GnuPlot::setInterval(double minX,double maxX,double minY,double maxY) {
00089 if (m_bPlot||m_bPlot3D)
00090 {
00091 cerr << "Erro, funcao ja definida, nao se pode modificar o intervalo.\n";
00092 this->finalize();
00093 }
00094 else {
00095
00096 m_MinX = minX;
00097 m_MaxX = maxX;
00098 m_MaxY = maxY;
00099 m_MinY = minY;
00100 fprintf(m_file,"set yrange [%lf:%lf];\n",m_MinY,m_MaxY);
00101 fprintf(m_file,"set xrange [%lf:%lf];\n",m_MinX,m_MaxX);
00102 }
00103 };
00104
00105
00106
00119 std::string GnuPlot::plotPoints(double X[],int lines,int cols,string name,string comp,string with,int bRowOrder,int bTrans,int blockSize)
00120 {
00121 if (lines ==0 || cols ==0)
00122 return "";
00123
00124 int i,j,iEnd,jEnd,iInc,jInc;
00125 int blCount=0;
00126 string strName = getDataFileName();
00127 FILE* fDat = fopen(strName.c_str(),"wr");
00128 m_List.push_back(strName);
00129
00130 if (m_bPlot3D)
00131 {
00132 cerr << "Graficos 2D e 3D na mesma cena nao pode ser feito\n";
00133 this->finalize();
00134 }
00135
00136 if (!bRowOrder) {
00137 if (bTrans)
00138 {
00139 iEnd = lines*cols;
00140 iInc = lines;
00141 jEnd = lines;
00142 jInc = 1;
00143 }
00144 else
00145 {
00146 iEnd = lines;
00147 iInc = 1;
00148 jEnd = lines*cols;
00149 jInc = lines;
00150 }
00151 }
00152 else {
00153 if (bTrans)
00154 {
00155 iEnd = cols;
00156 iInc = 1;
00157 jEnd = cols*lines;
00158 jInc = cols;
00159 }
00160 else
00161 {
00162 iEnd = lines*cols;
00163 iInc = cols;
00164 jEnd = cols;
00165 jInc = 1;
00166 }
00167 }
00168 for (i=0;i<iEnd;i+=iInc)
00169 {
00170 for (j=0;j<jEnd;j+=jInc)
00171 {
00172 fprintf(fDat,"%lf \t",X[i+j]);
00173 }
00174
00175 fprintf(fDat,"\n");
00176 if (++blCount == blockSize)
00177 {
00178 fprintf(fDat,"\n");
00179 blCount=0;
00180 }
00181 }
00182
00183 fclose(fDat);
00184
00185 if (!m_bPlot) {
00186 if (!with.empty())
00187 fprintf(m_file,"plot \"%s\" %s title \"%s\" %s",strName.c_str(),comp.c_str(),name.c_str(),with.c_str());
00188 else
00189 fprintf(m_file,"plot \"%s\" %s title \"%s\"",strName.c_str(),comp.c_str(),name.c_str());
00190 m_bPlot = true;
00191 }
00192 else {
00193 if (with.empty())
00194 fprintf(m_file,", \"%s\" %s title \"%s\"",strName.c_str(),comp.c_str(),name.c_str());
00195 else
00196 fprintf(m_file,", \"%s\" %s title \"%s\" %s",strName.c_str(),comp.c_str(),name.c_str(),with.c_str());
00197 }
00198 return m_List.back();
00199 };
00200
00201
00217 std::string GnuPlot::plotPoints3d(double X[],int lines,int cols,std::string name,std::string comp,std::string with,int bRowOrder,int bTrans,int blockSize)
00218 {
00219 int i,j,iEnd,jEnd,iInc,jInc;
00220 int blCount=0;
00221 string strName = getDataFileName();
00222 FILE* fDat = fopen(strName.c_str(),"wr");
00223 m_List.push_back(strName);
00224
00225 if (!bRowOrder) {
00226 if (bTrans)
00227 {
00228 iEnd = lines*cols;
00229 iInc = lines;
00230 jEnd = lines;
00231 jInc = 1;
00232 }
00233 else
00234 {
00235 iEnd = lines;
00236 iInc = 1;
00237 jEnd = lines*cols;
00238 jInc = lines;
00239 }
00240 }
00241 else {
00242 if (bTrans)
00243 {
00244 iEnd = cols;
00245 iInc = 1;
00246 jEnd = cols*lines;
00247 jInc = cols;
00248 }
00249 else
00250 {
00251 iEnd = lines*cols;
00252 iInc = cols;
00253 jEnd = cols;
00254 jInc = 1;
00255 }
00256 }
00257 for (i=0;i<iEnd;i+=iInc)
00258 {
00259 for (j=0;j<jEnd;j+=jInc)
00260 {
00261 fprintf(fDat,"%lf \t",X[i+j]);
00262 }
00263
00264 fprintf(fDat,"\n");
00265 if (++blCount == blockSize)
00266 {
00267 fprintf(fDat,"\n\n");
00268 blCount=0;
00269 }
00270 }
00271
00272 fclose(fDat);
00273 if (m_bPlot)
00274 {
00275 std::cerr << "Nao se pode misturar graficos 2D com graficos 3D\n";
00276 this->finalize();
00277 }
00278
00279 if (!m_bPlot3D) {
00280 if (!with.empty())
00281 fprintf(m_file,"splot \"%s\" %s title \"%s\" with %s",strName.c_str(),comp.c_str(),name.c_str(),with.c_str());
00282 else
00283 fprintf(m_file,"splot \"%s\" %s title \"%s\"",strName.c_str(),comp.c_str(),name.c_str());
00284 m_bPlot3D = true;
00285 }
00286 else {
00287 if (with.empty())
00288 fprintf(m_file,", \"%s\" %s title \"%s\"",strName.c_str(),comp.c_str(),name.c_str());
00289 else
00290 fprintf(m_file,", \"%s\" %s title \"%s\" with %s",strName.c_str(),comp.c_str(),name.c_str(),with.c_str());
00291 }
00292 return m_List.back();
00293
00294
00295 }
00296
00297
00306 void GnuPlot::plotPoints(std::string strFileName,std::string name,std::string comp)
00307 {
00308 if (m_bPlot3D)
00309 {
00310 cerr << "Graficos 2D e 3D na mesma cena nao pode ser feito\n";
00311 this->finalize();
00312 }
00313
00314 if (!m_bPlot) {
00315 fprintf(m_file,"plot \"%s\" title \"%s\" %s ",strFileName.c_str(),name.c_str(),comp.c_str());
00316 m_bPlot = true;
00317 }
00318 else {
00319 fprintf(m_file,", \"%s\" title \"%s\" %s",strFileName.c_str(),name.c_str(),comp.c_str());
00320 }
00321 return;
00322 }
00323
00324
00336 void GnuPlot::plotPoints(std::string strFileName,std::string name,int index1,int index2,int col1,int col2,std::string comp)
00337 {
00338 if (m_bPlot3D)
00339 {
00340 cerr << "Graficos 2D e 3D na mesma cena nao pode ser feito\n";
00341 this->finalize();
00342 }
00343
00344
00345 if (!m_bPlot) {
00346 fprintf(m_file,"plot \"%s\" index %d:%d using %d:%d %s title \"%s\" ",strFileName.c_str(),index1,index2,col1,col2,comp.c_str(),name.c_str());
00347 m_bPlot = true;
00348 }
00349 else {
00350 fprintf(m_file,", \"%s\" index %d:%d using %d:%d %s title \"%s\" ",strFileName.c_str(),index1,index2,col1,col2,comp.c_str(),name.c_str());
00351 }
00352 return;
00353 }
00354
00355
00356
00357
00358
00367 void GnuPlot::plotPoints3d(std::string strFileName,std::string name,std::string comp)
00368 {
00369 if (m_bPlot)
00370 {
00371 cerr << "Graficos 2D e 3D na mesma cena nao pode ser feito\n";
00372 this->finalize();
00373 }
00374
00375 if (!m_bPlot3D) {
00376 fprintf(m_file,"splot \"%s\" %s title \"%s\" ",strFileName.c_str(),comp.c_str(),name.c_str());
00377 m_bPlot3D = true;
00378 }
00379 else {
00380 fprintf(m_file,", \"%s\" %s title \"%s\"",strFileName.c_str(),comp.c_str(),name.c_str());
00381 }
00382 return;
00383 }
00384
00385
00386
00387
00388 void GnuPlot::pause(double dSec,std::string str_comment)
00389 {
00390 if (m_bPlot || m_bPlot3D) {
00391 cerr << "(GnuPlot::pause) Feche o grafico primeiro com a funcao NextScreen";
00392 this->finalize();
00393 }
00394 fprintf(m_file,"pause %g \"%s\";\n",dSec,str_comment.c_str());
00395 };
00396
00397 class RemoveFiles : public std::unary_function<string,void>
00398 {
00399 public:
00400 void operator()(std::string str) {remove(str.c_str());}
00401
00402
00403 };
00404 void GnuPlot::Draw()
00405 {
00406 int result;
00407 m_bPlot = false;
00408
00409 fclose(m_file);
00410
00411 string str("gnuplot -background white ");
00412 str+=m_arg;
00413 str+= m_List.front();
00414 result=system(str.c_str());
00415
00416 if (result!=0){
00417 cout << "Erro no Script.\nImprimindo:\n";
00418 string str("cat < ");
00419 str+= m_List.front();
00420 result=system(str.c_str());
00421 }
00422 else
00423 {
00424 cout << "GnuPlot Sucessful :-) " << endl;
00425 }
00426 if (!m_bLeave)
00427 runSystemCommand("rm TMP_* DAT_* ");
00428
00429 };
00430
00431
00432
00433
00434 void GnuPlot::yRange(double a,double b)
00435 {
00436 if (m_bPlot||m_bPlot3D) {
00437 cerr << "(GnuPlot::yRange) Feche o grafico primeiro com a funcao NextScreen";
00438 this->finalize();
00439 }
00440 fprintf(m_file,"set yrange [%lf:%lf];\n",a,b);
00441 }
00442 void GnuPlot::xRange(double a,double b)
00443 {
00444 if (m_bPlot||m_bPlot3D) {
00445 cerr << "(GnuPlot::yRange) Feche o grafico primeiro com a funcao NextScreen";
00446 this->finalize();
00447 }
00448 fprintf(m_file,"set xrange [%lf:%lf];\n",a,b);
00449 }
00450
00451 void GnuPlot::define(std::string var,std::string expr){
00452
00453 if (m_bPlot || m_bPlot3D) {
00454 cerr << "(GnuPlot::define) Feche o grafico primeiro com a funcao NextScreen";
00455 this->finalize();
00456 }
00457
00458 fprintf(m_file,"%s=%s;\n",var.c_str(),expr.c_str());
00459 };
00460
00461
00462
00463
00464
00465 void GnuPlot::define(std::string var,double num){
00466 if (m_bPlot || m_bPlot3D) {
00467 cerr << "(GnuPlot::define) Feche o grafico primeiro com a funcao NextScreen";
00468 this->finalize();
00469 }
00470 fprintf(m_file,"%s=%lf;\n",var.c_str(),num);
00471 };
00472
00473
00474
00481 void GnuPlot::set(std::string var,std::string value)
00482 {
00483 if (m_bPlot || m_bPlot3D) {
00484 cerr << "(GnuPlot::set) Feche o grafico primeiro com a funcao NextScreen";
00485 this->finalize();
00486 }
00487
00488 fprintf(m_file,"set %s %s;\n",var.c_str(),value.c_str());
00489 };
00490 void GnuPlot::set(std::string var,int value)
00491 {
00492 if (m_bPlot || m_bPlot3D) {
00493 cerr << "(GnuPlot::set) Feche o grafico primeiro com a funcao NextScreen";
00494 this->finalize();
00495 }
00496 fprintf(m_file,"set %s %d;\n",var.c_str(),value);
00497 }
00498 void GnuPlot::unset(std::string var)
00499 {
00500 if (m_bPlot || m_bPlot3D) {
00501 cerr << "(GnuPlot::set) Feche o grafico primeiro com a funcao NextScreen";
00502 this->finalize();
00503 }
00504 fprintf(m_file,"unset %s;\n",var.c_str());
00505
00506 }
00507
00508
00509 void GnuPlot::setOutput(std::string strOutput)
00510 {
00511 if (m_bPlot || m_bPlot3D) {
00512 cerr << "(GnuPlot::setOutput) Feche o grafico primeiro com a funcao NextScreen";
00513 this->finalize();
00514 }
00515 fprintf(m_file,"set output \"%s\";\n",strOutput.c_str());
00516 };
00517
00518
00519
00524 void GnuPlot::setOutputEpsMono(std::string strOutput)
00525 {
00526 if (m_bPlot || m_bPlot3D) {
00527 cerr << "(GnuPlot::setOutputEps) Feche o grafico primeiro com a funcao NextScreen";
00528 this->finalize();
00529 }
00530 fprintf(m_file,"set terminal postscript eps monochrome;\n set output \"%s\";\n",strOutput.c_str());
00531
00532 }
00533
00534 void GnuPlot::NextScene(){
00535 if (m_bPlot || m_bPlot3D)
00536 {
00537 m_bPlot = false;
00538 m_bPlot3D = false;
00539 fprintf(m_file,";\n\n");
00540 }
00541
00542 else
00543 cerr <<"(GnuPlot::NextScene) empty graph!!";
00544 }
00545
00552 void GnuPlot::NextPlot()
00553 {
00554 if (m_bPlot || m_bPlot3D)
00555 {
00556 m_bPlot = false;
00557 m_bPlot3D = false;
00558 fprintf(m_file,";\n\n");
00559 }
00560 else
00561 cerr <<"(GnuPlot::NextScene) empty graph!!";
00562 }
00563
00564
00565
00566
00567 void GnuPlot::setTitle(std::string strTitle){
00568 if (m_bPlot || m_bPlot3D)
00569 {
00570 cerr << "(GnuPlot::setTitle) Feche o grafico primeiro com a funcao NextScreen";
00571 this->finalize();
00572 }
00573 if (m_font.empty())
00574 {
00575 fprintf(m_file,"set title \"%s\";\n",strTitle.c_str());
00576 }
00577 else
00578 {
00579 fprintf(m_file,"set title \"%s\" font \"%s\";\n",strTitle.c_str(),m_font.c_str());
00580 }
00581 }
00582
00583
00584
00585 void GnuPlot::setArg(std::string strArg)
00586 {
00587 m_arg = strArg;
00588 }
00589
00590
00591 void GnuPlot::setIntervalX(double minX,double maxX)
00592 {
00593 if (m_bPlot || m_bPlot3D)
00594 {
00595 cerr << "Erro, funcao ja definida, nao se pode modificar o intervalo.\n";
00596 this->finalize();
00597
00598 }
00599 else {
00600
00601 m_MinX = minX;
00602 m_MaxX = maxX;
00603 fprintf(m_file,"set xrange [%lf:%lf];\n",m_MinX,m_MaxX);
00604 }
00605
00606 }
00607
00608
00609
00616 void GnuPlot::finalize()
00617 {
00618 std::for_each(m_List.begin(),m_List.end(),RemoveFiles());
00619 exit(1);
00620 }
00621
00622
00623 void GnuPlot::leaveFiles(int bLeave)
00624 {
00625 m_bLeave = bLeave;
00626 }
00627
00628
00636 void GnuPlot::zRange(double a,double b)
00637 {
00638 if (m_bPlot||m_bPlot3D) {
00639 cerr << "(GnuPlot::zRange) Feche o grafico primeiro com a funcao NextScreen";
00640 this->finalize();
00641 }
00642 fprintf(m_file,"set zrange [%lf:%lf];\n",a,b);
00643 }
00644
00645
00652 string GnuPlot::getDataFileName()
00653 {
00654 char strFileName[30];
00655 sprintf(strFileName,"/DAT_DOF_%05d",m_count++);
00656 if (m_count > 200)
00657 {
00658 printf("GnuPlot:: Too many files, aborting");
00659 abort();
00660 }
00661 return m_dir + strFileName;
00662 }
00663
00664
00671 void GnuPlot::setFont(string font)
00672 {
00673 m_font = font;
00674 }
00675
00676 std::string GnuPlot::newDataFile(std::ofstream &fout)
00677 {
00678 std::string str = getDataFileName();
00679 m_List.push_back(str);
00680 fout.open(str.c_str());
00681 return str;
00682 }
00683
00684
00685
00686
00687
00688 void GnuPlot::runSystemCommand(const char *aux)
00689 {
00690 if (system(aux))
00691 printf("Problema ao rodar o comando %s\n",aux);
00692
00693 }