example.cc
Go to the documentation of this file.
00001 // Example for C++ Interface to Gnuplot
00002 
00003 // requirements:
00004 // * gnuplot has to be installed (http://www.gnuplot.info/download.html)
00005 // * for Windows: set Path-Variable for Gnuplot path (e.g. C:/program files/gnuplot/bin)
00006 //             or set Gnuplot path with: Gnuplot::set_GNUPlotPath(const std::string &path);
00007 
00008 
00009 #include <iostream>
00010 #include "gnuplot_i.hpp" //Gnuplot class handles POSIX-Pipe-communikation with Gnuplot
00011 
00012 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
00013  #include <conio.h>   //for getch(), needed in wait_for_key()
00014  #include <windows.h> //for Sleep()
00015  void sleep(int i) { Sleep(i*1000); }
00016 #endif
00017 
00018 
00019 #define SLEEP_LGTH 2  // sleep time in seconds
00020 #define NPOINTS    50 // length of array
00021 
00022 void wait_for_key(); // Programm halts until keypress
00023 
00024 using std::cout;
00025 using std::endl;
00026 
00027 int main(int argc, char* argv[])
00028 {
00029     // if path-variable for gnuplot is not set, do it with:
00030     // Gnuplot::set_GNUPlotPath("C:/program files/gnuplot/bin/");
00031 
00032     // set a special standard terminal for showonscreen (normally not needed),
00033     //   e.g. Mac users who want to use x11 instead of aqua terminal:
00034     // Gnuplot::set_terminal_std("x11");
00035 
00036     cout << "*** example of gnuplot control through C++ ***" << endl << endl;
00037 
00038     //
00039     // Using the GnuplotException class
00040     //
00041     try
00042     {
00043         Gnuplot g1("lines");
00044 
00045         //
00046         // Slopes
00047         //
00048         cout << "*** plotting slopes" << endl;
00049         g1.set_title("Slopes\\nNew Line");
00050 
00051         cout << "y = x" << endl;
00052         g1.plot_slope(1.0,0.0,"y=x");
00053 
00054         cout << "y = 2*x" << endl;
00055         g1.plot_slope(2.0,0.0,"y=2x");
00056 
00057         cout << "y = -x" << endl;
00058         g1.plot_slope(-1.0,0.0,"y=-x");
00059         g1.unset_title();
00060 
00061         //
00062         // Equations
00063         //
00064         g1.reset_plot();
00065         cout << endl << endl << "*** various equations" << endl;
00066 
00067         cout << "y = sin(x)" << endl;
00068         g1.plot_equation("sin(x)","sine");
00069 
00070         cout << "y = log(x)" << endl;
00071         g1.plot_equation("log(x)","logarithm");
00072 
00073         cout << "y = sin(x) * cos(2*x)" << endl;
00074         g1.plot_equation("sin(x)*cos(2*x)","sine product");
00075 
00076         //
00077         // Styles
00078         //
00079         g1.reset_plot();
00080         cout << endl << endl << "*** showing styles" << endl;
00081 
00082         cout << "sine in points" << endl;
00083         g1.set_pointsize(0.8).set_style("points");
00084         g1.plot_equation("sin(x)","points");
00085 
00086         cout << "sine in impulses" << endl;
00087         g1.set_style("impulses");
00088         g1.plot_equation("sin(x)","impulses");
00089 
00090         cout << "sine in steps" << endl;
00091         g1.set_style("steps");
00092         g1.plot_equation("sin(x)","steps");
00093 
00094         //
00095         // Save to ps
00096         //
00097         g1.reset_all();
00098         cout << endl << endl << "*** save to ps " << endl;
00099 
00100         cout << "y = sin(x) saved to test_output.ps in working directory" << endl;
00101         g1.savetops("test_output");
00102         g1.set_style("lines").set_samples(300).set_xrange(0,5);
00103         g1.plot_equation("sin(12*x)*exp(-x)").plot_equation("exp(-x)");
00104 
00105         g1.showonscreen(); // window output
00106 
00107 
00108         //
00109         // User defined 1d, 2d and 3d point sets
00110         //
00111         std::vector<double> x, y, y2, dy, z;
00112 
00113         for (int i = 0; i < NPOINTS; i++)  // fill double arrays x, y, z
00114         {
00115             x.push_back((double)i);             // x[i] = i
00116             y.push_back((double)i * (double)i); // y[i] = i^2
00117             z.push_back( x[i]*y[i] );           // z[i] = x[i]*y[i] = i^3
00118             dy.push_back((double)i * (double)i / (double) 10); // dy[i] = i^2 / 10
00119         }
00120         y2.push_back(0.00); y2.push_back(0.78); y2.push_back(0.97); y2.push_back(0.43);
00121         y2.push_back(-0.44); y2.push_back(-0.98); y2.push_back(-0.77); y2.push_back(0.02);
00122 
00123 
00124         g1.reset_all();
00125         cout << endl << endl << "*** user-defined lists of doubles" << endl;
00126         g1.set_style("impulses").plot_x(y,"user-defined doubles");
00127 
00128         g1.reset_plot();
00129         cout << endl << endl << "*** user-defined lists of points (x,y)" << endl;
00130         g1.set_grid();
00131         g1.set_style("points").plot_xy(x,y,"user-defined points 2d");
00132 
00133         g1.reset_plot();
00134         cout << endl << endl << "*** user-defined lists of points (x,y,z)" << endl;
00135         g1.unset_grid();
00136         g1.plot_xyz(x,y,z,"user-defined points 3d");
00137 
00138         g1.reset_plot();
00139         cout << endl << endl << "*** user-defined lists of points (x,y,dy)" << endl;
00140         g1.plot_xy_err(x,y,dy,"user-defined points 2d with errorbars");
00141 
00142 
00143         //
00144         // Multiple output screens
00145         //
00146         cout << endl << endl;
00147         cout << "*** multiple output windows" << endl;
00148 
00149         g1.reset_plot();
00150         g1.set_style("lines");
00151         cout << "window 1: sin(x)" << endl;
00152         g1.set_grid().set_samples(600).set_xrange(0,300);
00153         g1.plot_equation("sin(x)+sin(x*1.1)");
00154 
00155         g1.set_xautoscale().replot();
00156 
00157         Gnuplot g2;
00158         cout << "window 2: user defined points" << endl;
00159         g2.plot_x(y2,"points");
00160         g2.set_smooth().plot_x(y2,"cspline");
00161         g2.set_smooth("bezier").plot_x(y2,"bezier");
00162         g2.unset_smooth();
00163 
00164         Gnuplot g3("lines");
00165         cout << "window 3: log(x)/x" << endl;
00166         g3.set_grid();
00167         g3.plot_equation("log(x)/x","log(x)/x");
00168 
00169         Gnuplot g4("lines");
00170         cout << "window 4: splot x*x+y*y" << endl;
00171         g4.set_zrange(0,100);
00172         g4.set_xlabel("x-axis").set_ylabel("y-axis").set_zlabel("z-axis");
00173         g4.plot_equation3d("x*x+y*y");
00174 
00175         Gnuplot g5("lines");
00176         cout << "window 5: splot with hidden3d" << endl;
00177         g5.set_isosamples(25).set_hidden3d();
00178         g5.plot_equation3d("x*y*y");
00179 
00180         Gnuplot g6("lines");
00181         cout << "window 6: splot with contour" << endl;
00182         g6.set_isosamples(60).set_contour();
00183         g6.unset_surface().plot_equation3d("sin(x)*sin(y)+4");
00184 
00185         g6.set_surface().replot();
00186 
00187         Gnuplot g7("lines");
00188         cout << "window 7: set_samples" << endl;
00189         g7.set_xrange(-30,20).set_samples(40);
00190         g7.plot_equation("besj0(x)*0.12e1").plot_equation("(x**besj0(x))-2.5");
00191 
00192         g7.set_samples(400).replot();
00193 
00194         Gnuplot g8("filledcurves");
00195         cout << "window 8: filledcurves" << endl;
00196         g8.set_legend("outside right top").set_xrange(-5,5);
00197         g8.plot_equation("x*x").plot_equation("-x*x+4");
00198 
00199         //
00200         // Plot an image
00201         //
00202         Gnuplot g9;
00203         cout << "window 9: plot_image" << endl;
00204         const int iWidth  = 255;
00205         const int iHeight = 255;
00206         g9.set_xrange(0,iWidth).set_yrange(0,iHeight).set_cbrange(0,255);
00207         g9.cmd("set palette gray");
00208         unsigned char ucPicBuf[iWidth*iHeight];
00209         // generate a greyscale image
00210         for(int iIndex = 0; iIndex < iHeight*iWidth; iIndex++)
00211         {
00212             ucPicBuf[iIndex] = iIndex%255;
00213         }
00214         g9.plot_image(ucPicBuf,iWidth,iHeight,"greyscale");
00215 
00216         g9.set_pointsize(0.6).unset_legend().plot_slope(0.8,20);
00217 
00218         //
00219         // manual control
00220         //
00221         Gnuplot g10;
00222         cout << "window 10: manual control" << endl;
00223         g10.cmd("set samples 400").cmd("plot abs(x)/2"); // either with cmd()
00224         g10 << "replot sqrt(x)" << "replot sqrt(-x)";    // or with <<
00225 
00226         wait_for_key();
00227 
00228     }
00229     catch (GnuplotException ge)
00230     {
00231         cout << ge.what() << endl;
00232     }
00233 
00234 
00235     cout << endl << "*** end of gnuplot example" << endl;
00236 
00237     return 0;
00238 }
00239 
00240 
00241 
00242 void wait_for_key ()
00243 {
00244 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)  // every keypress registered, also arrow keys
00245     cout << endl << "Press any key to continue..." << endl;
00246 
00247     FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
00248     _getch();
00249 #elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
00250     cout << endl << "Press ENTER to continue..." << endl;
00251 
00252     std::cin.clear();
00253     std::cin.ignore(std::cin.rdbuf()->in_avail());
00254     std::cin.get();
00255 #endif
00256     return;
00257 }


cob_people_detection
Author(s): Richard Bormann , Thomas Zwölfer
autogenerated on Fri Aug 28 2015 10:24:12