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