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