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