736594e40420b1f18568060a39326991975b8b77
[alexxy/gromacs.git] / src / ngmx / molps.c
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.
14
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.
19  *
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.
26  *
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.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * Gyas ROwers Mature At Cryogenic Speed
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <math.h>
40 #include "sysstuff.h"
41 #include <string.h>
42 #include "smalloc.h"
43 #include "macros.h"
44 #include "xutil.h"
45 #include "3dview.h"
46 #include "gmx_fatal.h"
47 #include "buttons.h"
48 #include "manager.h"
49 #include "nmol.h"
50 #include "writeps.h"
51 #include "nleg.h"
52
53 #define MSIZE 4
54
55 static void ps_draw_atom(t_psdata ps, atom_id ai, iv2 vec2[], char **atomnm[])
56 {
57     int xi, yi;
58
59     xi = vec2[ai][XX];
60     yi = vec2[ai][YY];
61     ps_rgb(ps, Type2RGB(*atomnm[ai]));
62     ps_line(ps, xi-MSIZE, yi, xi+MSIZE+1, yi);
63     ps_line(ps, xi, yi-MSIZE, xi, yi+MSIZE+1);
64 }
65
66 /* Global variables */
67 static rvec gl_fbox, gl_hbox, gl_mhbox;
68
69 static void init_pbc(matrix box)
70 {
71     int i;
72
73     for (i = 0; (i < DIM); i++)
74     {
75         gl_fbox[i]  =  box[i][i];
76         gl_hbox[i]  =  gl_fbox[i]*0.5;
77         gl_mhbox[i] = -gl_hbox[i];
78     }
79 }
80
81 static gmx_bool local_pbc_dx(rvec x1, rvec x2)
82 {
83     int  i;
84     real dx;
85
86     for (i = 0; (i < DIM); i++)
87     {
88         dx = x1[i]-x2[i];
89         if (dx > gl_hbox[i])
90         {
91             return FALSE;
92         }
93         else if (dx <= gl_mhbox[i])
94         {
95             return FALSE;
96         }
97     }
98     return TRUE;
99 }
100
101 static void ps_draw_bond(t_psdata ps,
102                          atom_id ai, atom_id aj, iv2 vec2[],
103                          rvec x[], char **atomnm[], int size[], gmx_bool bBalls)
104 {
105     char    *ic, *jc;
106     int      xi, yi, xj, yj;
107     int      xm, ym;
108
109     if (bBalls)
110     {
111         ps_draw_atom(ps, ai, vec2, atomnm);
112         ps_draw_atom(ps, aj, vec2, atomnm);
113     }
114     else
115     {
116         if (local_pbc_dx(x[ai], x[aj]))
117         {
118             ic = *atomnm[ai];
119             jc = *atomnm[aj];
120             xi = vec2[ai][XX];
121             yi = vec2[ai][YY];
122             xj = vec2[aj][XX];
123             yj = vec2[aj][YY];
124
125             if (ic != jc)
126             {
127                 xm = (xi+xj) >> 1;
128                 ym = (yi+yj) >> 1;
129
130                 ps_rgb(ps, Type2RGB(ic));
131                 ps_line(ps, xi, yi, xm, ym);
132                 ps_rgb(ps, Type2RGB(jc));
133                 ps_line(ps, xm, ym, xj, yj);
134             }
135             else
136             {
137                 ps_rgb(ps, Type2RGB(ic));
138                 ps_line(ps, xi, yi, xj, yj);
139             }
140         }
141     }
142 }
143
144 void ps_draw_objects(t_psdata ps, int nobj, t_object objs[], iv2 vec2[], rvec x[],
145                      char **atomnm[], int size[], gmx_bool bShowHydro, int bond_type,
146                      gmx_bool bPlus)
147 {
148     gmx_bool     bBalls;
149     int          i;
150     t_object    *obj;
151
152     bBalls = FALSE;
153     for (i = 0; (i < nobj); i++)
154     {
155         obj = &(objs[i]);
156         switch (obj->eO)
157         {
158             case eOSingle:
159                 ps_draw_atom(ps, obj->ai, vec2, atomnm);
160                 break;
161             case eOBond:
162                 ps_draw_bond(ps, obj->ai, obj->aj, vec2, x, atomnm, size, bBalls);
163                 break;
164             case eOHBond:
165                 if (bShowHydro)
166                 {
167                     ps_draw_bond(ps, obj->ai, obj->aj, vec2, x, atomnm, size, bBalls);
168                 }
169                 break;
170             default:
171                 break;
172         }
173     }
174 }
175
176 static void v4_to_iv2(vec4 x4, iv2 v2, int x0, int y0, real sx, real sy)
177 {
178     real inv_z;
179
180     inv_z  = 1.0/x4[ZZ];
181     v2[XX] = x0+sx*x4[XX]*inv_z;
182     v2[YY] = y0-sy*x4[YY]*inv_z;
183 }
184
185 static void draw_box(t_psdata ps, t_3dview *view, matrix box,
186                      int x0, int y0, real sx, real sy)
187 {
188     int  ivec[8][4] =  {
189         { 0, 0, 0, 1 }, { 1, 0, 0, 1 }, { 1, 1, 0, 1 }, { 0, 1, 0, 1 },
190         { 0, 0, 1, 1 }, { 1, 0, 1, 1 }, { 1, 1, 1, 1 }, { 0, 1, 1, 1 }
191     };
192     int  bonds[12][2] = {
193         { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 },
194         { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 4 },
195         { 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 }
196     };
197     int  i, j;
198     rvec corner[8];
199     vec4 x4;
200     iv2  vec2[12];
201
202     for (i = 0; (i < 8); i++)
203     {
204         for (j = 0; (j < DIM); j++)
205         {
206             corner[i][j] = ivec[i][j]*box[j][j];
207         }
208         m4_op(view->proj, corner[i], x4);
209         v4_to_iv2(x4, vec2[i], x0, y0, sx, sy);
210     }
211     ps_color(ps, 0, 0, 0.5);
212     for (i = 0; (i < 12); i++)
213     {
214         ps_line(ps,
215                 vec2[bonds[i][0]][XX], vec2[bonds[i][0]][YY],
216                 vec2[bonds[i][1]][XX], vec2[bonds[i][1]][YY]);
217     }
218 }
219
220 void ps_draw_mol(t_psdata ps, t_manager *man)
221 {
222     static char tstr[2][20];
223     static int  ntime = 0;
224     t_windata  *win;
225     t_3dview   *view;
226     t_molwin   *mw;
227     int         i, x0, y0, nvis;
228     iv2        *vec2;
229     real        sx, sy;
230     vec4        x4;
231
232     if (!man->status)
233     {
234         return;
235     }
236
237     view = man->view;
238     mw   = man->molw;
239
240     win = &(mw->wd);
241
242     vec2 = man->ix;
243     x0   = win->width/2;
244     y0   = win->height/2;
245     sx   = win->width/2*view->sc_x;
246     sy   = win->height/2*view->sc_y;
247
248     init_pbc(man->box);
249
250     /* create_visibility(man); */
251
252     for (i = 0; (i < man->natom); i++)
253     {
254         if (man->bVis[i])
255         {
256             m4_op(view->proj, man->x[i], x4);
257             man->zz[i] = x4[ZZ];
258             v4_to_iv2(x4, vec2[i], x0, y0, sx, sy);
259         }
260     }
261     set_sizes(man, sx, sy);
262
263     z_fill (man, man->zz);
264
265     /* Start drawing
266        XClearWindow(x11->disp,win->self); */
267
268     /* Draw Time
269        sprintf(tstr[ntime],"Time: %.3 ps",man->time);
270        if (strcmp(tstr[ntime],tstr[1-ntime]) != 0) {
271        set_vbtime(x11,man->vbox,tstr[ntime]);
272        ntime=1-ntime;
273        }*/
274
275     if (mw->boxtype != esbNone)
276     {
277         draw_box(ps, view, man->box, x0, y0, sx, sy);
278     }
279
280     /* Should sort on Z-Coordinates here! */
281     nvis = filter_vis(man);
282     if (nvis && man->bSort)
283     {
284         qsort(man->obj, nvis, sizeof(man->obj[0]), compare_obj);
285     }
286
287     /* Draw the objects */
288     ps_draw_objects(ps,
289                     nvis, man->obj, man->ix, man->x, man->top.atoms.atomname,
290                     man->size,
291                     mw->bShowHydrogen, mw->bond_type, man->bPlus);
292
293     /* Draw the labels */
294     ps_color(ps, 0, 0, 0);
295     for (i = 0; (i < man->natom); i++)
296     {
297         if (man->bLabel[i] && man->bVis[i])
298         {
299             ps_text(ps, vec2[i][XX]+2, vec2[i][YY]-2, man->szLab[i]);
300         }
301     }
302 }