Remove some unnecessary declarations
[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 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include <math.h>
42 #include "sysstuff.h"
43 #include <string.h>
44 #include "smalloc.h"
45 #include "macros.h"
46 #include "xutil.h"
47 #include "gmx_fatal.h"
48 #include "buttons.h"
49 #include "manager.h"
50 #include "nmol.h"
51 #include "nleg.h"
52
53 #include "gromacs/fileio/writeps.h"
54 #include "gromacs/math/3dview.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     {
78         gl_fbox[i]  =  box[i][i];
79         gl_hbox[i]  =  gl_fbox[i]*0.5;
80         gl_mhbox[i] = -gl_hbox[i];
81     }
82 }
83
84 static bool local_pbc_dx(rvec x1, rvec x2)
85 {
86     int  i;
87     real dx;
88
89     for (i = 0; (i < DIM); i++)
90     {
91         dx = x1[i]-x2[i];
92         if (dx > gl_hbox[i])
93         {
94             return false;
95         }
96         else if (dx <= gl_mhbox[i])
97         {
98             return false;
99         }
100     }
101     return true;
102 }
103
104 static void ps_draw_bond(t_psdata ps,
105                          atom_id ai, atom_id aj, iv2 vec2[],
106                          rvec x[], char **atomnm[])
107 {
108     char    *ic, *jc;
109     int      xi, yi, xj, yj;
110     int      xm, ym;
111
112     if (local_pbc_dx(x[ai], x[aj]))
113     {
114         ic = *atomnm[ai];
115         jc = *atomnm[aj];
116         xi = vec2[ai][XX];
117         yi = vec2[ai][YY];
118         xj = vec2[aj][XX];
119         yj = vec2[aj][YY];
120
121         if (ic != jc)
122         {
123             xm = (xi+xj) >> 1;
124             ym = (yi+yj) >> 1;
125
126             ps_rgb(ps, Type2RGB(ic));
127             ps_line(ps, xi, yi, xm, ym);
128             ps_rgb(ps, Type2RGB(jc));
129             ps_line(ps, xm, ym, xj, yj);
130         }
131         else
132         {
133             ps_rgb(ps, Type2RGB(ic));
134             ps_line(ps, xi, yi, xj, yj);
135         }
136     }
137 }
138
139 static void ps_draw_objects(t_psdata ps, int nobj, t_object objs[], iv2 vec2[],
140                             rvec x[], char **atomnm[], bool bShowHydro)
141 {
142     int          i;
143     t_object    *obj;
144
145     for (i = 0; (i < nobj); i++)
146     {
147         obj = &(objs[i]);
148         switch (obj->eO)
149         {
150             case eOSingle:
151                 ps_draw_atom(ps, obj->ai, vec2, atomnm);
152                 break;
153             case eOBond:
154                 ps_draw_bond(ps, obj->ai, obj->aj, vec2, x, atomnm);
155                 break;
156             case eOHBond:
157                 if (bShowHydro)
158                 {
159                     ps_draw_bond(ps, obj->ai, obj->aj, vec2, x, atomnm);
160                 }
161                 break;
162             default:
163                 break;
164         }
165     }
166 }
167
168 static void v4_to_iv2(vec4 x4, iv2 v2, int x0, int y0, real sx, real sy)
169 {
170     real inv_z;
171
172     inv_z  = 1.0/x4[ZZ];
173     v2[XX] = x0+sx*x4[XX]*inv_z;
174     v2[YY] = y0-sy*x4[YY]*inv_z;
175 }
176
177 static void draw_box(t_psdata ps, t_3dview *view, matrix box,
178                      int x0, int y0, real sx, real sy)
179 {
180     int  ivec[8][4] =  {
181         { 0, 0, 0, 1 }, { 1, 0, 0, 1 }, { 1, 1, 0, 1 }, { 0, 1, 0, 1 },
182         { 0, 0, 1, 1 }, { 1, 0, 1, 1 }, { 1, 1, 1, 1 }, { 0, 1, 1, 1 }
183     };
184     int  bonds[12][2] = {
185         { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 },
186         { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 4 },
187         { 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 }
188     };
189     int  i, j;
190     rvec corner[8];
191     vec4 x4;
192     iv2  vec2[12];
193
194     for (i = 0; (i < 8); i++)
195     {
196         for (j = 0; (j < DIM); j++)
197         {
198             corner[i][j] = ivec[i][j]*box[j][j];
199         }
200         m4_op(view->proj, corner[i], x4);
201         v4_to_iv2(x4, vec2[i], x0, y0, sx, sy);
202     }
203     ps_color(ps, 0, 0, 0.5);
204     for (i = 0; (i < 12); i++)
205     {
206         ps_line(ps,
207                 vec2[bonds[i][0]][XX], vec2[bonds[i][0]][YY],
208                 vec2[bonds[i][1]][XX], vec2[bonds[i][1]][YY]);
209     }
210 }
211
212 void ps_draw_mol(t_psdata ps, t_manager *man)
213 {
214     t_windata  *win;
215     t_3dview   *view;
216     t_molwin   *mw;
217     int         i, x0, y0, nvis;
218     iv2        *vec2;
219     real        sx, sy;
220     vec4        x4;
221
222     if (!man->status)
223     {
224         return;
225     }
226
227     view = man->view;
228     mw   = man->molw;
229
230     win = &(mw->wd);
231
232     vec2 = man->ix;
233     x0   = win->width/2;
234     y0   = win->height/2;
235     sx   = win->width/2*view->sc_x;
236     sy   = win->height/2*view->sc_y;
237
238     init_pbc(man->box);
239
240     for (i = 0; (i < man->natom); i++)
241     {
242         if (man->bVis[i])
243         {
244             m4_op(view->proj, man->x[i], x4);
245             man->zz[i] = x4[ZZ];
246             v4_to_iv2(x4, vec2[i], x0, y0, sx, sy);
247         }
248     }
249     set_sizes(man);
250
251     z_fill (man, man->zz);
252
253     /* Start drawing
254        XClearWindow(x11->disp,win->self); */
255
256     if (mw->boxtype != esbNone)
257     {
258         draw_box(ps, view, man->box, x0, y0, sx, sy);
259     }
260
261     /* Should sort on Z-Coordinates here! */
262     nvis = filter_vis(man);
263     if (nvis && man->bSort)
264     {
265         qsort(man->obj, nvis, sizeof(man->obj[0]), compare_obj);
266     }
267
268     /* Draw the objects */
269     ps_draw_objects(ps,
270                     nvis, man->obj, man->ix, man->x, man->top.atoms.atomname,
271                     mw->bShowHydrogen);
272
273     /* Draw the labels */
274     ps_color(ps, 0, 0, 0);
275     for (i = 0; (i < man->natom); i++)
276     {
277         if (man->bLabel[i] && man->bVis[i])
278         {
279             ps_text(ps, vec2[i][XX]+2, vec2[i][YY]-2, man->szLab[i]);
280         }
281     }
282 }