Institut de Robòtica i Informàtica Industrial
KRD Group

The CuikSuite Project

plot.c

Go to the documentation of this file.
00001 #include "plot.h"
00002 
00003 #include "defines.h"
00004 
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 
00023 #define CM2POINTS 450.0 /*number of points of each cm in Xfig*/
00024 
00030 #define POINTS2CM (1.0/CM2POINTS)
00031 
00041 #define USER2XFIG(x,y,p) (signed int)(((p)->offset_x+(x))*CM2POINTS),\
00042                          (signed int)(((p)->offset_y-(y))*CM2POINTS)
00043 
00052 #define PRINTXFIGPREAMBLE(p) fprintf((p)->f," %d %d %d %d %d -1 %d %f ",\
00053                                      (p)->line_style,(p)->line_width,(p)->pen_color,\
00054                                      (p)->fill_color,(p)->depth,(p)->fill_intensity,\
00055                                      (((p)->line_style==SOLID_LINE)?0.0:4.0))
00056 
00057 /*
00058  * Inits the plot structure and start the Xfig file (writes the header)
00059  */
00060 void InitPlot(const char *name,Tplot *p)
00061 {
00062 
00063   p->f=fopen(name,"w");
00064   if (!p->f)
00065     {
00066       fprintf(stderr,"File %s can not be created in function InitPlot\n",name);
00067       exit(1);
00068     }
00069 
00070   p->offset_x=0.0;
00071   p->offset_y=0.0;
00072 
00073   p->line_width=1;
00074   p->line_style=SOLID_LINE;
00075 
00076   p->pen_color=-1; /*default*/
00077   p->fill_color=-1; /*default*/
00078   p->fill_intensity=-1; /* no fill (20 = full intensity)*/
00079   p->depth=50;  /*standard depth*/
00080 
00081   /*print de preamble for the xfig file*/
00082   fprintf(p->f,"#FIG 3.2\n");
00083   fprintf(p->f,"Landscape\n");
00084   fprintf(p->f,"Center\n");
00085   fprintf(p->f,"Metric\n");
00086   fprintf(p->f,"A4\n");
00087   fprintf(p->f,"100.00\n");
00088   fprintf(p->f,"Single\n");
00089   fprintf(p->f,"-2\n");
00090   fprintf(p->f,"1200 2\n");
00091 }
00092 
00093 /*
00094  * Changhes the line width for the object that are drawn after calling this
00095  * function
00096  */
00097 void SetLineWidth(unsigned int width,Tplot *p)
00098 {
00099   p->line_width=width;
00100 }
00101 
00102 /*
00103  * Sets a new line style for the new objects
00104  */
00105 void SetLineStyle(unsigned int style,Tplot *p)
00106 {
00107   p->line_style=style;
00108 }
00109 
00110 /*
00111  * Defines a new coordinate frame for the objects. The offsets are defined from
00112  * the top left corner of the sheet (and positive towards the bottom right corner)
00113  * This origin modification is useful to avoid the picture to be drawn out of the 
00114  * sheet.
00115  */
00116 void SetOrigin(double offset_x,double offset_y,Tplot *p)
00117 {
00118   p->offset_x=offset_x;
00119   p->offset_y=offset_y;
00120 }
00121 
00122 void SetPenColor(int color,Tplot *p)
00123 {
00124   if ((color>-2)&&(color<32))
00125     p->pen_color=color;
00126 }
00127 
00128 void SetFillColor(int color,Tplot *p)
00129 {
00130   if ((color>-2)&&(color<32))
00131     p->fill_color=color;
00132 }
00133 
00134 void SetFillIntensity(unsigned int intensity,Tplot *p)
00135 {
00136   if (intensity<21)
00137     p->fill_intensity=intensity;
00138 }
00139 
00140 
00141 void SetDepth(unsigned int depth,Tplot *p)
00142 {
00143   p->depth=depth;
00144 }
00145 
00146 /*
00147  * Draws a circle at position cx, cy and with radius r
00148  */
00149 void PlotCircle(double cx,double cy,double r,Tplot *p)
00150 {
00151   fprintf(p->f,"1 3");
00152 
00153   PRINTXFIGPREAMBLE(p);
00154 
00155   fprintf(p->f,"1 0.0000 %d %d %d %d %d %d %d %d\n",
00156           USER2XFIG(cx,cy,p),
00157           (signed int)(r*CM2POINTS),(signed int)(r*CM2POINTS), /*1 cm radius*/
00158           USER2XFIG(cx,cy,p),
00159           USER2XFIG(-r,cy,p));
00160 }
00161 
00162 /*
00163  * Draws a rectangle
00164  */
00165 void PlotRectangle(double x_left,double y_sup,double x_right,double y_inf,Tplot *p)
00166 {
00167   fprintf(p->f,"2 2");
00168 
00169   PRINTXFIGPREAMBLE(p);
00170 
00171   fprintf(p->f,"0 0 -1 0 0 5\n");
00172 
00173   fprintf(p->f,"         ");
00174 
00175   /*add the five (first repeated) extremes points of the rectangle*/
00176 
00177   fprintf(p->f,"%d %d  ",USER2XFIG(x_left -POINTS2CM,y_inf-POINTS2CM,p)); 
00178   fprintf(p->f,"%d %d  ",USER2XFIG(x_right+POINTS2CM,y_inf-POINTS2CM,p)); 
00179   fprintf(p->f,"%d %d  ",USER2XFIG(x_right+POINTS2CM,y_sup+POINTS2CM,p)); 
00180   fprintf(p->f,"%d %d  ",USER2XFIG(x_left -POINTS2CM,y_sup+POINTS2CM,p)); 
00181   fprintf(p->f,"%d %d\n",USER2XFIG(x_left -POINTS2CM,y_inf-POINTS2CM,p));
00182 
00183 /*
00184   fprintf(p->f,"%d %d  ",USER2XFIG(x_left ,y_inf,p));
00185   fprintf(p->f,"%d %d  ",USER2XFIG(x_right,y_inf,p));
00186   fprintf(p->f,"%d %d  ",USER2XFIG(x_right,y_sup,p));
00187   fprintf(p->f,"%d %d  ",USER2XFIG(x_left ,y_sup,p));
00188   fprintf(p->f,"%d %d\n",USER2XFIG(x_left ,y_inf,p));
00189 */
00190 
00191   fflush(p->f);
00192 }
00193 
00194 void PlotTriangle(double x1,double y1,double x2,double y2,double x3,double y3,Tplot *p)
00195 {
00196   
00197   fprintf(p->f,"2 3");
00198 
00199   PRINTXFIGPREAMBLE(p);
00200 
00201   fprintf(p->f,"0 0 -1 0 0 4\n");
00202 
00203   fprintf(p->f,"         ");
00204   fprintf(p->f,"%d %d  ",USER2XFIG(x1,y1,p)); 
00205   fprintf(p->f,"%d %d  ",USER2XFIG(x2,y2,p)); 
00206   fprintf(p->f,"%d %d  ",USER2XFIG(x3,y3,p));  
00207   fprintf(p->f,"%d %d\n",USER2XFIG(x1,y1,p)); 
00208 
00209   fflush(p->f);
00210 }
00211 
00212 /*
00213  * Draws a line 
00214  */
00215 void PlotLine(double x0,double y0,double x1,double y1,Tplot *p)
00216 {
00217   fprintf(p->f,"2 1");
00218 
00219   PRINTXFIGPREAMBLE(p);
00220 
00221   fprintf(p->f," 0 0 -1 0 0 2 \n      %d %d %d %d\n",USER2XFIG(x0,y0,p),USER2XFIG(x1,y1,p));
00222 }
00223 
00224 /*
00225  * Draws an arrow with a pre-defined arrow style 
00226  */
00227 void PlotArrow(double x0,double y0,double x1,double y1,Tplot *p)
00228 {
00229   fprintf(p->f,"2 1");
00230 
00231   PRINTXFIGPREAMBLE(p);
00232 
00233   fprintf(p->f," 0 0 -1 1 0 2 \n      1 1 5.00 60.00 120.00 \n     %d %d %d %d\n",USER2XFIG(x0,y0,p),USER2XFIG(x1,y1,p));
00234 }
00235 
00236 /*
00237  * Writes a string at a given position (with predefined font and size (14 points))
00238  */
00239 void PlotText(double x,double y,const char *text,Tplot *p)
00240 {
00241   fprintf(p->f,"4 0 0 50 0 0 14 0.0000 4 150 150 %d %d %s\\001\n",USER2XFIG(x,y,p),text);
00242 }
00243 
00244 /*
00245  * Draw X.Y axis naming axis as 'nx' and 'ny'
00246  */
00247 void PlotAxis(const char *nx,double min_x,double max_x,
00248               const char *ny,double min_y,double max_y,
00249               double step,
00250               Tplot *p)
00251 {
00252   signed int i;
00253 
00254   /*print the axis*/
00255   SetLineStyle(SOLID_LINE,p);
00256 
00257   PlotArrow(min_x,0.0,max_x,0.0,p);
00258   PlotText(max_x+0.5,0.0,nx,p);
00259 
00260   for(i=(signed int)(min_x/step);i<(signed int)(max_x/step);i++)
00261     PlotLine(1.0*i*step,-0.1*step,1.0*i*step,0.1*step,p);
00262 
00263   PlotArrow(0.0,min_y,0.0,max_y,p);
00264   PlotText(0.5,max_y-0.5,ny,p);
00265 
00266   for(i=(signed int)(min_y/step);i<(signed int)(max_y/step);i++)
00267     PlotLine(-0.1*step,1.0*i*step,0.1*step,1.0*i*step,p);
00268 
00269 }
00270 
00271 /*
00272  * Finishes the plot. It just draws the coordinate axis (the last defined ones) and
00273  * closes the file.
00274  */
00275 void ClosePlot(Tplot *p)
00276 {
00277   if (p->f!=stdout)
00278     fclose(p->f);
00279 }