From: Michal Sojka Date: Wed, 16 Feb 2011 11:40:02 +0000 (+0100) Subject: Add local copy of c2gnuplot.h X-Git-Url: http://rtime.felk.cvut.cz/gitweb/eurobot/public.git/commitdiff_plain/6ec9ebd520babcdffaca081c433e0c404e762d4b Add local copy of c2gnuplot.h The is needed to compile barcam correctly on systems without c2gnuplot installed. --- diff --git a/src/camera/barcam/barcam.cxx b/src/camera/barcam/barcam.cxx index 0fd5bec9..a0772d03 100644 --- a/src/camera/barcam/barcam.cxx +++ b/src/camera/barcam/barcam.cxx @@ -56,7 +56,7 @@ #include // gnuplot fifo plot -#include +#include "c2gnuplot.h" extern "C" { #include diff --git a/src/camera/barcam/c2gnuplot.h b/src/camera/barcam/c2gnuplot.h new file mode 100644 index 00000000..7fb882bf --- /dev/null +++ b/src/camera/barcam/c2gnuplot.h @@ -0,0 +1,304 @@ +/* + * c2gnuplot.h + * + * Copyright 2007 Zoltan Kuscsik + * + * This file is GPLv2 as found in COPYING. + */ + + +/* + @fille c2gnuplo.h + @author Z. Kuscsik + @date jan 2007 + @version $Revision: 0.2 $ + @brief C interface to gnuplot. + + gnuplot is a freely available, command-driven graphical display tool for + Unix. It compiles and works quite well on a number of Unix flavours as + well as other operating systems. The following module enables sending + display requests to gnuplot through simple C calls. + This code is partially based on the gnuplot_i.h library: + http://ndevilla.free.fr/gnuplot/ + +--------------------------------------------------------------------------*/ + +#include +#include +#include +#include +#include +#include +//#include "operations.h" + +using namespace std; + +#define GNPL_COMM_SIZE 2000 +#define MAX_COMM_NAME_LENGTH 100 +#define MAX_PATH_NAME_LENGTH 1000 +#define tmp_name "/tmp/tmpdataXXXXXXX" +unsigned int FIRST_INIT = 1; + +char command_list[][MAX_COMM_NAME_LENGTH] = {"gnuplot","ppmto4ym","mpeg2enc"}; + +#define NUMBER_OF_EXTERNAL_COMMANDS (sizeof command_list)/MAX_COMM_NAME_LENGTH //number of external commands + +map path_list; + + + + +class gnuplot_window{ + public: + gnuplot_window(); + void splot_data(); //plot 3d data file + void splot_data(char * cmd, ...); + void plot_data(); //plot 2d data file + void plot_data(char * cmd, ...); + void flush(); //flush gnuplot data + void data(char * cmd, ...); //write data to gnuplot session + void command(char * cmd, ...); //set gnuplot variables and draw functions + void mpeg_out(char * cmd, ...); + char* fit_range; + char* fit_function; + void fit(char * cmd, ...); + float get_variable(char * cmd, ...); + private: + FILE *pipe; + FILE *input_param; + FILE *input; + char gnuplot_fifo[(sizeof tmp_name)]; + char gnuplot_output_fifo[(sizeof tmp_name)]; + }; + + + +char * get_program_path(char * pname) +{ + int i, j, lg; + char * path; + static char buf[MAX_PATH_NAME_LENGTH]; + + /* Trivial case: try in CWD */ + sprintf(buf, "./%s", pname) ; + if (access(buf, X_OK)==0) { + sprintf(buf, "."); + return buf ; + } + /* Try out in all paths given in the PATH variable */ + buf[0] = 0; + path = getenv("PATH") ; + if (path!=NULL) { + for (i=0; path[i]; ) { + for (j=i ; (path[j]) && (path[j]!=':') ; j++); + lg = j - i; + strncpy(buf, path + i, lg); + if (lg == 0) buf[lg++] = '.'; + buf[lg++] = '/'; + strcpy(buf + lg, pname); + if (access(buf, X_OK) == 0) { + /* Found it! */ + break ; + } + buf[0] = 0; + i = j; + if (path[i] == ':') i++ ; + } + } else { + fprintf(stderr, "PATH variable not set\n"); + } + /* If the buffer is still empty, the command was not found */ + if (buf[0] == 0) return NULL ; + /* Otherwise truncate the command name to yield path only */ + lg = strlen(buf) - 1 ; + while (buf[lg]!='/') { + buf[lg]=0 ; + lg -- ; + } + buf[lg] = 0; + return buf ; +} + + + +void gnuplot_window::mpeg_out(char * cmd, ...) +{ + va_list ap ; + char local_cmd[GNPL_COMM_SIZE]; + + va_start(ap, cmd); + vsprintf(local_cmd, cmd, ap); + va_end(ap); + strcat(local_cmd, "\n"); + command("set term pbm color "); + command("set output '| ppmtoy4m -S 420mpeg2 | mpeg2enc -o %s",local_cmd); + fit_range="[*:*]"; + fit_function=""; + +} + +void gnuplot_window::data(char * cmd, ...) +{ + + va_list ap ; + char local_cmd[GNPL_COMM_SIZE]; + + if(FIRST_INIT) + { + FIRST_INIT=0; + } + + va_start(ap, cmd); + vsprintf(local_cmd, cmd, ap); + va_end(ap); + + strcat(local_cmd, "\n"); + fputs(local_cmd, input); + fflush(input); +} + + +void gnuplot_window::command(char * cmd, ...) +{ + va_list ap ; + char local_cmd[GNPL_COMM_SIZE]; + + va_start(ap, cmd); + vsprintf(local_cmd, cmd, ap); + va_end(ap); + + strcat(local_cmd, "\n"); + +fprintf(pipe,local_cmd); +fflush(pipe); +} + +void gnuplot_window::plot_data() +{ +fprintf(pipe,"plot '%s'\n",gnuplot_fifo); +fflush(pipe); +input = fopen(gnuplot_fifo,"w"); +} + +void gnuplot_window::splot_data() +{ +fprintf(pipe,"splot '%s'\n",gnuplot_fifo); +fflush(pipe); +input = fopen(gnuplot_fifo,"w"); +} + +void gnuplot_window::splot_data(char * cmd, ...) +{ + va_list ap ; + char local_cmd[GNPL_COMM_SIZE]; + + va_start(ap, cmd); + vsprintf(local_cmd, cmd, ap); + va_end(ap); + + strcat(local_cmd, "\n"); + + +fprintf(pipe,"splot '%s' %s\n",gnuplot_fifo,local_cmd); +fflush(pipe); +input = fopen(gnuplot_fifo,"w"); +} +void gnuplot_window::plot_data(char * cmd, ...) +{ + va_list ap ; + char local_cmd[GNPL_COMM_SIZE]; + + va_start(ap, cmd); + vsprintf(local_cmd, cmd, ap); + va_end(ap); + + strcat(local_cmd, "\n"); + + +fprintf(pipe,"plot '%s' %s\n",gnuplot_fifo,local_cmd); +fflush(pipe); +input = fopen(gnuplot_fifo,"w"); +} + +/* + * Fit function + * + */ + +void gnuplot_window::fit(char * cmd, ...) +{ + va_list ap ; + char local_cmd[GNPL_COMM_SIZE]; + va_start(ap, cmd); + vsprintf(local_cmd, cmd, ap); + va_end(ap); + + strcat(local_cmd, "\n"); + + if(fit_function=="") + fprintf(stderr,"Function for datafit is not defined. Please define the fit_function parameter\n"); + + fprintf(pipe,"fit [%s] %s '%s' %s\n",fit_range, fit_function,gnuplot_fifo,local_cmd); + fflush(pipe); + input = fopen(gnuplot_fifo,"w"); + + + + +} + +void gnuplot_window::flush() +{ +fclose(input); +fflush(pipe); +} + + +gnuplot_window::gnuplot_window() +{ + int fd; + pipe=popen("/usr/bin/gnuplot 2>/dev/null","w"); + setvbuf(pipe, NULL, _IONBF, 0 ); + + strcpy(gnuplot_fifo,tmp_name); + strcpy(gnuplot_output_fifo,tmp_name); + if((fd=mkstemp(gnuplot_fifo) ) < 0) { fprintf(stderr,"Creating temporary filename failed\n"); exit(0);} + close(fd); remove(gnuplot_fifo); + + if (mkfifo(gnuplot_fifo, S_IRUSR | S_IWUSR ) != 0) {fprintf(stderr,"Make fifo file %s failed\n",gnuplot_fifo); exit(0);} + + + if((fd=mkstemp(gnuplot_output_fifo) ) < 0) { fprintf(stderr,"Creating temporary filename failed\n"); exit(0);} + close(fd); remove(gnuplot_output_fifo); + + if (mkfifo(gnuplot_output_fifo, S_IRUSR | S_IWUSR ) != 0) {fprintf(stderr,"Make fifo file %s failed\n",gnuplot_output_fifo); exit(0);} + + fflush(pipe); + command("set fit logfile '/dev/null'"); + +} + +float gnuplot_window::get_variable(char * cmd, ...) +{ + va_list ap ; + char local_cmd[GNPL_COMM_SIZE]; + va_start(ap, cmd); + vsprintf(local_cmd, cmd, ap); + va_end(ap); + + float param; + command("set print '%s'",gnuplot_output_fifo); + + command("print %s",local_cmd); + + usleep(10000); /* There must be a little sleep because of asynchronous relation to the gnuplot pipe*/ + + input_param=fopen(gnuplot_output_fifo,"r"); + + fscanf(input_param,"%f",¶m); + + fclose(input_param); + + return param; +} +