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