Tagged files with gromacs 3.0 header and added some license info
[alexxy/gromacs.git] / src / ngmx / molps.c
1 /*
2  * $Id$
3  * 
4  *                This source code is part of
5  * 
6  *                 G   R   O   M   A   C   S
7  * 
8  *          GROningen MAchine for Chemical Simulations
9  * 
10  *                        VERSION 3.0
11  * 
12  * Copyright (c) 1991-2001
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  * 
21  * If you want to redistribute modifications, please consider that
22  * scientific software is very special. Version control is crucial -
23  * bugs must be traceable. We will be happy to consider code for
24  * inclusion in the official distribution, but derived work must not
25  * be called official GROMACS. Details are found in the README & COPYING
26  * files - if they are missing, get the official version at www.gromacs.org.
27  * 
28  * To help us fund GROMACS development, we humbly ask that you cite
29  * the papers on the package - you can find them in the top README file.
30  * 
31  * Do check out http://www.gromacs.org , or mail us at gromacs@gromacs.org .
32  * 
33  * And Hey:
34  * Giving Russians Opium May Alter Current Situation
35  */
36 static char *SRCID_molps_c = "$Id$";
37 #include <math.h>
38 #include "sysstuff.h"
39 #include "string.h"
40 #include "smalloc.h"
41 #include "macros.h"
42 #include "xutil.h"
43 #include "3dview.h"
44 #include "fatal.h"
45 #include "buttons.h"
46 #include "manager.h"
47 #include "nmol.h"
48 #include "writeps.h"
49 #include "nleg.h"
50
51 #define MSIZE 4
52
53 static void ps_draw_atom(FILE *ps,atom_id ai,iv2 vec2[],char **atomnm[])
54 {
55   int xi,yi;
56   
57   xi=vec2[ai][XX];
58   yi=vec2[ai][YY];
59   ps_rgb(ps,Type2RGB(*atomnm[ai]));
60   ps_line(ps,xi-MSIZE,yi,xi+MSIZE+1,yi);
61   ps_line(ps,xi,yi-MSIZE,xi,yi+MSIZE+1);
62 }
63
64 /* Global variables */
65 static rvec gl_fbox,gl_hbox,gl_mhbox;
66
67 static void init_pbc(matrix box)
68 {
69   int i;
70
71   for(i=0; (i<DIM); i++) {
72     gl_fbox[i]  =  box[i][i];
73     gl_hbox[i]  =  gl_fbox[i]*0.5;
74     gl_mhbox[i] = -gl_hbox[i];
75   }
76 }
77
78 static bool local_pbc_dx(rvec x1, rvec x2)
79 {
80   int  i;
81   real dx;
82   
83   for(i=0; (i<DIM); i++) {
84     dx=x1[i]-x2[i];
85     if (dx > gl_hbox[i])
86       return FALSE;
87     else if (dx <= gl_mhbox[i])
88       return FALSE;
89   }
90   return TRUE;
91 }
92
93 static void ps_draw_bond(FILE *ps,
94                          atom_id ai,atom_id aj,iv2 vec2[],
95                          rvec x[],char **atomnm[],int size[],bool bBalls)
96 {
97   char    *ic,*jc;
98   int     xi,yi,xj,yj;
99   int     xm,ym;
100
101   if (bBalls) {
102     ps_draw_atom(ps,ai,vec2,atomnm);
103     ps_draw_atom(ps,aj,vec2,atomnm);
104   }
105   else {
106     if (local_pbc_dx(x[ai],x[aj])) {
107       ic=*atomnm[ai];
108       jc=*atomnm[aj];
109       xi=vec2[ai][XX];
110       yi=vec2[ai][YY];
111       xj=vec2[aj][XX];
112       yj=vec2[aj][YY];
113       
114       if (ic != jc) {
115         xm=(xi+xj) >> 1;
116         ym=(yi+yj) >> 1;
117       
118         ps_rgb(ps,Type2RGB(ic));
119         ps_line(ps,xi,yi,xm,ym);
120         ps_rgb(ps,Type2RGB(jc));
121         ps_line(ps,xm,ym,xj,yj);
122       }
123       else {
124         ps_rgb(ps,Type2RGB(ic));
125         ps_line(ps,xi,yi,xj,yj);
126       }
127     }
128   }
129 }
130
131 void ps_draw_objects(FILE *ps,int nobj,t_object objs[],iv2 vec2[],rvec x[],
132                      char **atomnm[],int size[],bool bShowHydro,int bond_type,
133                      bool bPlus)
134 {
135   bool     bBalls;
136   int      i;
137   t_object *obj;
138
139   bBalls=FALSE;
140   for(i=0; (i<nobj); i++) {
141     obj=&(objs[i]);
142     switch (obj->eO) {
143     case eOSingle:
144       ps_draw_atom(ps,obj->ai,vec2,atomnm);
145       break;
146     case eOBond:
147       ps_draw_bond(ps,obj->ai,obj->aj,vec2,x,atomnm,size,bBalls);
148       break;
149     case eOHBond:
150       if (bShowHydro)
151         ps_draw_bond(ps,obj->ai,obj->aj,vec2,x,atomnm,size,bBalls);
152       break;
153     default:
154       break;
155     }
156   }
157 }
158
159 static void v4_to_iv2(vec4 x4,iv2 v2,int x0,int y0,real sx,real sy)
160 {
161   real inv_z;
162
163   inv_z=1.0/x4[ZZ];
164   v2[XX]=x0+sx*x4[XX]*inv_z;
165   v2[YY]=y0-sy*x4[YY]*inv_z;
166 }
167
168 static void draw_box(FILE *ps,t_3dview *view,matrix box,
169                      int x0,int y0,real sx,real sy)
170 {
171   int  ivec[8][4] =  { 
172     { 0,0,0,1 }, { 1,0,0,1 }, { 1,1,0,1 }, { 0,1,0,1 },
173     { 0,0,1,1 }, { 1,0,1,1 }, { 1,1,1,1 }, { 0,1,1,1 }
174   };
175   int  bonds[12][2] = {
176     { 0,1 }, { 1,2 }, { 2,3 }, { 3,0 }, 
177     { 4,5 }, { 5,6 }, { 6,7 }, { 7,4 },
178     { 0,4 }, { 1,5 }, { 2,6 }, { 3,7 }
179   };
180   int  i,j;
181   rvec corner[8];
182   vec4 x4;
183   iv2  vec2[12];
184
185   for (i=0; (i<8); i++) {
186     for (j=0; (j<DIM); j++)
187       corner[i][j] = ivec[i][j]*box[j][j];
188     m4_op(view->proj,corner[i],x4);
189     v4_to_iv2(x4,vec2[i],x0,y0,sx,sy);
190   }
191   ps_color(ps,0,0,0.5);
192   for (i=0; (i<12); i++)
193     ps_line(ps,
194             vec2[bonds[i][0]][XX],vec2[bonds[i][0]][YY],
195             vec2[bonds[i][1]][XX],vec2[bonds[i][1]][YY]);
196 }
197
198 void ps_draw_mol(FILE *ps,t_manager *man)
199 {
200   static char tstr[2][20];
201   static int  ntime=0;
202   t_windata *win;
203   t_3dview  *view;
204   t_molwin  *mw;
205   int       i,x0,y0,nvis;
206   iv2       *vec2;
207   real      sx,sy;
208   vec4      x4;
209
210   if (man->status == -1)
211     return;
212
213   view=man->view;
214   mw=man->molw;
215
216   win=&(mw->wd);
217
218   vec2=man->ix;
219   x0=win->width/2;
220   y0=win->height/2;
221   sx=win->width/2*view->sc_x;
222   sy=win->height/2*view->sc_y;
223
224   init_pbc(man->box);
225
226   /* create_visibility(man); */
227
228   for(i=0; (i<man->natom); i++) {
229     if (man->bVis[i]) {
230       m4_op(view->proj,man->x[i],x4);
231       man->zz[i]=x4[ZZ];
232       v4_to_iv2(x4,vec2[i],x0,y0,sx,sy);
233     }
234   }
235   set_sizes(man,sx,sy);
236   
237   z_fill (man,man->zz);
238   
239   /* Start drawing 
240   XClearWindow(x11->disp,win->self); */
241
242   /* Draw Time 
243   sprintf(tstr[ntime],"Time: %.3 ps",man->time);
244   if (strcmp(tstr[ntime],tstr[1-ntime]) != 0) {
245     set_vbtime(x11,man->vbox,tstr[ntime]);
246     ntime=1-ntime;
247   }*/
248
249   if (mw->boxtype != esbNone)
250     draw_box(ps,view,man->box,x0,y0,sx,sy);
251
252   /* Should sort on Z-Coordinates here! */
253   nvis=filter_vis(man);
254   if (nvis && man->bSort)
255     qsort(man->obj,nvis,sizeof(man->obj[0]),compare_obj);
256   
257   /* Draw the objects */
258   ps_draw_objects(ps,
259                   nvis,man->obj,man->ix,man->x,man->top.atoms.atomname,
260                   man->size,
261                   mw->bShowHydrogen,mw->bond_type,man->bPlus);
262
263   /* Draw the labels */
264   ps_color(ps,0,0,0);
265   for(i=0; (i<man->natom); i++) 
266     if (man->bLabel[i] && man->bVis[i]) 
267       ps_text(ps,vec2[i][XX]+2,vec2[i][YY]-2,man->szLab[i]);
268 }
269