added Verlet scheme and NxN non-bonded functionality
[gromacs.git] / src / gmxlib / writeps.c
blob1854893ab4452b8aedefbdce4413d8c67404a805
1 /*
2 *
3 * This source code is part of
4 *
5 * G R O M A C S
6 *
7 * GROningen MAchine for Chemical Simulations
8 *
9 * VERSION 3.2.0
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * GROningen Mixture of Alchemy and Childrens' Stories
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #include <stdio.h>
40 #include "futil.h"
41 #include "gmx_fatal.h"
42 #include "copyrite.h"
43 #include "writeps.h"
44 #include "smalloc.h"
45 #include "gmxfio.h"
47 const char *fontnm[efontNR] = {
48 "Times-Roman","Times-Italic", "Times-Bold", "Times-BoldItalic",
49 "Helvetica", "Helvetica-Oblique","Helvetica-Bold","Helvetica-BoldOblique",
50 "Courier", "Courier-Oblique", "Courier-Bold", "Courier-BoldOblique"
54 /* Internal psdata structure (abstract datatype)
55 * to maintain the current state of the ps engine.
57 struct t_int_psdata {
58 FILE *fp;
59 int maxrgb;
60 int nrgb;
61 t_rgb *rgb;
62 real gen_ybox;
63 int ostack;
67 t_psdata ps_open(const char *fn,real x1,real y1,real x2,real y2)
69 t_psdata ps;
71 snew(ps,1);
73 ps->fp=gmx_fio_fopen(fn,"w");
74 fprintf(ps->fp,"%%!PS-Adobe-2.0 EPSF-1.2\n");
75 fprintf(ps->fp,"%%%%Creator: GROMACS\n");
76 fprintf(ps->fp,"%%%%Title: %s\n",fn);
77 fprintf(ps->fp,"%%%%BoundingBox: %g %g %g %g\n",x1,y1,x2,y2);
78 fprintf(ps->fp,"%%%%EndComments\n");
79 fprintf(ps->fp,"/m {moveto} bind def\n");
80 fprintf(ps->fp,"/l {lineto} bind def\n");
81 fprintf(ps->fp,"/rm {rmoveto} bind def\n");
82 fprintf(ps->fp,"/r {rlineto} bind def\n");
83 fprintf(ps->fp,"/f {fill} bind def\n");
84 fprintf(ps->fp,"/s {stroke} bind def\n");
86 ps->nrgb=0;
87 ps->maxrgb=0;
88 ps->rgb=NULL;
89 ps->gen_ybox=0;
90 ps->ostack=0;
92 return ps;
95 void ps_linewidth(t_psdata ps, int lw)
97 fprintf(ps->fp,"%d setlinewidth\n",lw);
100 static void ps_defcolor(t_psdata ps,real r,real g,real b,char *cname)
102 fprintf(ps->fp,"/%s {%g %g %g setrgbcolor} bind def\n",cname,r,g,b);
105 static void ps_selcolor(t_psdata ps,char *cname)
107 fprintf(ps->fp,"%s\n",cname);
110 static int search_col(t_psdata ps,real r,real g,real b)
112 int i;
113 char buf[12];
115 for(i=0; (i<ps->nrgb); i++)
116 if ((ps->rgb[i].r == r) && (ps->rgb[i].g == g) && (ps->rgb[i].b == b))
117 return i;
119 if (ps->nrgb >= ps->maxrgb) {
120 ps->maxrgb+=100;
121 srenew(ps->rgb,ps->maxrgb);
124 sprintf(buf,"C%d",ps->nrgb);
125 ps_defcolor(ps,r,g,b,buf);
126 fprintf(ps->fp,"/B%d {%s b} bind def\n",ps->nrgb,buf);
127 ps->rgb[i].r=r;
128 ps->rgb[i].g=g;
129 ps->rgb[i].b=b;
130 ps->nrgb++;
132 return (ps->nrgb-1);
135 void ps_color(t_psdata ps,real r,real g,real b)
137 char buf[12];
138 int i;
140 i=search_col(ps,r,g,b);
142 sprintf(buf,"C%d",i);
143 ps_selcolor(ps,buf);
146 void ps_rgb(t_psdata ps,t_rgb *rgb)
148 ps_color(ps,rgb->r,rgb->g,rgb->b);
152 void ps_init_rgb_nbox(t_psdata ps,real xbox, real ybox)
154 ps->gen_ybox=ybox;
155 fprintf(ps->fp,"/by {def currentpoint "
156 "%g y r %g %g r %g y neg r %g %g r f y add moveto} bind def\n",
157 0.0,xbox,0.0,0.0,-xbox,0.0);
158 /* macro bn is used in ps_rgb_nbox to draw rectangular boxes */
161 void ps_rgb_nbox(t_psdata ps,t_rgb *rgb,real n)
163 int i;
165 if ( n>2 ) {
166 ps_rgb(ps,rgb);
167 fprintf(ps->fp,"/y %g by\n",n*ps->gen_ybox);
168 /* macro by is defined in ps_init_rgb_nbox */
169 } else
170 for (i=0; (i<n); i++)
171 ps_rgb_box(ps, rgb);
175 void ps_init_rgb_box(t_psdata ps,real xbox, real ybox)
177 fprintf(ps->fp,"/b {currentpoint "
178 "%g %g r %g %g r %g %g r %g %g r f %g add moveto} bind def\n",
179 0.0,ybox,xbox,0.0,0.0,-ybox,-xbox,0.0,ybox);
180 /* macro b is used in search_col to define macro B */
183 void ps_rgb_box(t_psdata ps,t_rgb *rgb)
185 fprintf(ps->fp,"B%d\n",search_col(ps,rgb->r,rgb->g,rgb->b));
186 /* macro B is defined in search_col from macro b */
189 void ps_lineto(t_psdata ps,real x,real y)
191 fprintf(ps->fp,"%g %g l\n",x,y);
194 void ps_linerel(t_psdata ps,real dx,real dy)
196 fprintf(ps->fp,"%g %g r\n",dx,dy);
199 void ps_moveto(t_psdata ps,real x,real y)
201 fprintf(ps->fp,"%g %g m\n",x,y);
204 void ps_moverel(t_psdata ps,real dx,real dy)
206 fprintf(ps->fp,"%g %g rm\n",dx,dy);
209 void ps_line(t_psdata ps,real x1,real y1,real x2,real y2)
211 ps_moveto(ps,x1,y1);
212 ps_lineto(ps,x2,y2);
213 fprintf(ps->fp,"s\n");
216 static void do_box(t_psdata ps,real x1,real y1,real x2,real y2)
218 ps_moveto(ps,x1,y1);
219 ps_linerel(ps,0,(real)(y2-y1));
220 ps_linerel(ps,(real)(x2-x1),0);
221 ps_linerel(ps,0,(real)(y1-y2));
222 ps_linerel(ps,(real)(x1-x2),0);
225 void ps_box(t_psdata ps,real x1,real y1,real x2,real y2)
227 do_box(ps,x1,y1,x2,y2);
228 fprintf(ps->fp,"s\n");
231 void ps_fillbox(t_psdata ps,real x1,real y1,real x2,real y2)
233 do_box(ps,x1,y1,x2,y2);
234 fprintf(ps->fp,"f\n");
237 void ps_arc(t_psdata ps,real x1,real y1,real rad,real a0,real a1)
239 fprintf(ps->fp,"%g %g %g %g %g arc s\n",x1,y1,rad,a0,a1);
242 void ps_fillarc(t_psdata ps,real x1,real y1,real rad,real a0,real a1)
244 fprintf(ps->fp,"%g %g %g %g %g arc f\n",x1,y1,rad,a0,a1);
247 void ps_arcslice(t_psdata ps,real xc,real yc,
248 real rad1,real rad2,real a0,real a1)
250 fprintf(ps->fp,"newpath %g %g %g %g %g arc %g %g %g %g %g arcn closepath s\n",
251 xc,yc,rad1,a0,a1,xc,yc,rad2,a1,a0);
254 void ps_fillarcslice(t_psdata ps,real xc,real yc,
255 real rad1,real rad2,real a0,real a1)
257 fprintf(ps->fp,"newpath %g %g %g %g %g arc %g %g %g %g %g arcn closepath f\n",
258 xc,yc,rad1,a0,a1,xc,yc,rad2,a1,a0);
261 void ps_circle(t_psdata ps,real x1,real y1,real rad)
263 ps_arc(ps,x1,y1,rad,0,360);
266 void ps_font(t_psdata ps,int font,real size)
269 if ((font < 0) || (font > efontNR)) {
270 fprintf(stderr,"Invalid Font: %d, using %s\n",font,fontnm[0]);
271 font=0;
273 fprintf(ps->fp,"/%s findfont\n",fontnm[font]);
274 fprintf(ps->fp,"%g scalefont setfont\n",size);
277 void ps_strfont(t_psdata ps,char *font,real size)
279 fprintf(ps->fp,"/%s findfont\n",font);
280 fprintf(ps->fp,"%g scalefont setfont\n",size);
283 void ps_text(t_psdata ps,real x1,real y1,const char *str)
285 ps_moveto(ps,x1,y1);
286 fprintf(ps->fp,"(%s) show\n",str);
289 void ps_flip(t_psdata ps,gmx_bool bPlus)
291 if (bPlus)
292 fprintf(ps->fp,"612.5 0 translate 90 rotate\n");
293 else
294 fprintf(ps->fp,"-90 rotate -612.5 0 translate\n");
297 void ps_rotate(t_psdata ps,real angle)
299 fprintf(ps->fp,"%f rotate\n",angle);
302 void ps_ctext(t_psdata ps,real x1,real y1,const char *str,int expos)
304 if (expos == eXLeft) {
305 ps_text(ps,x1,y1,str);
306 return;
308 ps_moveto(ps,x1,y1);
309 fprintf(ps->fp,"(%s) stringwidth\n",str);
310 switch (expos) {
311 case eXLeft:
312 fprintf(ps->fp,"exch 0 exch pop exch\n");
313 break;
314 case eXCenter:
315 fprintf(ps->fp,"exch 2 div neg exch\n");
316 break;
317 case eXRight:
318 fprintf(ps->fp,"exch neg exch\n");
319 break;
320 default:
321 gmx_fatal(FARGS,"invalid position index (expos=%d)",expos);
323 fprintf(ps->fp,"rmoveto (%s) show\n",str);
326 void ps_translate(t_psdata ps,real x,real y)
328 fprintf(ps->fp,"%g %g translate\n",x,y);
331 void ps_setorigin(t_psdata ps)
333 fprintf(ps->fp,"currentpoint dup 3 -1 roll dup 4 1 roll exch translate\n");
334 ps->ostack++;
337 void ps_unsetorigin(t_psdata ps)
339 if (ps->ostack <= 0)
340 gmx_fatal(FARGS,"No origin on stack!\n");
341 fprintf(ps->fp,"neg exch neg exch translate\n");
342 ps->ostack--;
345 void ps_close(t_psdata ps)
347 fprintf(ps->fp,"%%showpage\n");
348 fprintf(ps->fp,"%%%%EOF\n");
349 gmx_fio_fclose(ps->fp);
350 sfree(ps->rgb);
351 sfree(ps);
354 void ps_comment(t_psdata ps,const char *s)
356 fprintf(ps->fp,"%%%% %s\n",s);