2 * This file is part of the GROMACS molecular simulation package.
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,2016,2017,2019, 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.
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.
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.
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.
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.
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.
45 #include "gromacs/math/vec.h"
46 #include "gromacs/math/vecdump.h"
47 #include "gromacs/pbcutil/pbc.h"
48 #include "gromacs/utility/fatalerror.h"
49 #include "gromacs/utility/smalloc.h"
58 static bool MWCallBack(t_x11* x11, XEvent* event, Window /*w*/, void* data)
64 mw = static_cast<t_molwin*>(data);
66 letter.type = ClientMessage;
67 letter.xclient.display = x11->disp;
68 letter.xclient.window = To;
69 letter.xclient.message_type = 0;
70 letter.xclient.format = 32;
74 /* Do Not draw anything, but signal parent instead, he will
77 letter.xclient.data.l[0] = IDDRAWMOL;
78 letter.xclient.data.l[1] = Button1;
79 XSendEvent(x11->disp, To, True, 0, &letter);
83 std::printf("Molwindow: Buttonpress\n");
85 letter.xclient.data.l[0] = IDLABEL;
86 letter.xclient.data.l[1] = (long)event->xbutton.button;
87 letter.xclient.data.l[2] = event->xbutton.x;
88 letter.xclient.data.l[3] = event->xbutton.y;
89 XSendEvent(x11->disp, To, True, 0, &letter);
92 mw->wd.width = event->xconfigure.width;
93 mw->wd.height = event->xconfigure.height;
100 static void set_def(t_molwin* mw, int ePBC, matrix box)
102 mw->bShowHydrogen = true;
103 mw->bond_type = eBFat;
105 mw->boxtype = esbRect;
106 mw->realbox = TRICLINIC(box) ? esbTri : esbRect;
109 t_molwin* init_mw(t_x11* x11,
123 set_def(mw, ePBC, box);
125 InitWin(&mw->wd, x, y, width, height, 1, "Mol Window");
127 mw->wd.Parent = Parent;
128 mw->wd.self = XCreateSimpleWindow(x11->disp, Parent, x, y, width, height, 1, fg, bg);
129 x11->RegisterCallback(x11, mw->wd.self, Parent, MWCallBack, mw);
130 x11->SetInputMask(x11, mw->wd.self, ExposureMask | StructureNotifyMask | ButtonPressMask);
134 void map_mw(t_x11* x11, t_molwin* mw)
136 XMapWindow(x11->disp, mw->wd.self);
139 bool toggle_hydrogen(t_x11* x11, t_molwin* mw)
141 mw->bShowHydrogen = !mw->bShowHydrogen;
142 ExposeWin(x11->disp, mw->wd.self);
144 return mw->bShowHydrogen;
147 void set_bond_type(t_x11* x11, t_molwin* mw, int bt)
149 if (bt != mw->bond_type)
152 ExposeWin(x11->disp, mw->wd.self);
156 void set_box_type(t_x11* x11, t_molwin* mw, int bt)
159 std::fprintf(stderr, "mw->boxtype = %d, bt = %d\n", mw->boxtype, bt);
161 if (bt != mw->boxtype)
163 if ((bt == esbTrunc && mw->realbox == esbTri) || bt == esbTri || bt == esbNone)
166 ExposeWin(x11->disp, mw->wd.self);
170 std::fprintf(stderr, "Can not change rectangular box to truncated octahedron\n");
175 void done_mw(t_x11* x11, t_molwin* mw)
177 x11->UnRegisterCallback(x11, mw->wd.self);
182 draw_atom(Display* disp, Window w, GC gc, int ai, iv2 vec2[], unsigned long col[], int size[], bool bBall, bool bPlus)
188 XSetForeground(disp, gc, col[ai]);
191 XFillCircle(disp, w, gc, xi, yi, size[ai] - 1);
192 XSetForeground(disp, gc, BLACK);
193 XDrawCircle(disp, w, gc, xi, yi, size[ai]);
194 /* XSetForeground(disp,gc,WHITE);
195 XFillCircle(disp,w,gc,xi+4,yi-4,4); */
199 XDrawLine(disp, w, gc, xi - MSIZE, yi, xi + MSIZE + 1, yi);
200 XDrawLine(disp, w, gc, xi, yi - MSIZE, xi, yi + MSIZE + 1);
204 XDrawLine(disp, w, gc, xi - 1, yi, xi + 1, yi);
208 /* Global variables */
209 static rvec gl_fbox, gl_hbox, gl_mhbox;
211 static void my_init_pbc(matrix box)
215 for (i = 0; (i < DIM); i++)
217 gl_fbox[i] = box[i][i];
218 gl_hbox[i] = gl_fbox[i] * 0.5;
219 gl_mhbox[i] = -gl_hbox[i];
223 static bool local_pbc_dx(rvec x1, rvec x2)
228 for (i = 0; (i < DIM); i++)
235 else if (dx <= gl_mhbox[i])
244 draw_bond(Display* disp, Window w, GC gc, int ai, int aj, iv2 vec2[], rvec x[], unsigned long col[], int size[], bool bBalls)
246 unsigned long ic, jc;
252 draw_atom(disp, w, gc, ai, vec2, col, size, true, false);
253 draw_atom(disp, w, gc, aj, vec2, col, size, true, false);
257 if (local_pbc_dx(x[ai], x[aj]))
271 XSetForeground(disp, gc, ic);
272 XDrawLine(disp, w, gc, xi, yi, xm, ym);
273 XSetForeground(disp, gc, jc);
274 XDrawLine(disp, w, gc, xm, ym, xj, yj);
278 XSetForeground(disp, gc, ic);
279 XDrawLine(disp, w, gc, xi, yi, xj, yj);
285 int compare_obj(const void* a, const void* b)
287 const t_object *oa, *ob;
290 oa = static_cast<const t_object*>(a);
291 ob = static_cast<const t_object*>(b);
309 void z_fill(t_manager* man, real* zz)
314 for (i = 0, obj = man->obj; (i < man->nobj); i++, obj++)
318 case eOSingle: obj->z = zz[obj->ai]; break;
320 case eOHBond: obj->z = (zz[obj->ai] + zz[obj->aj]) * 0.5; break;
326 int filter_vis(t_manager* man)
328 int i, nobj, nvis, nhide;
340 for (i = 0; (i < nobj); i++, obj++)
346 if (obj->eO != eOSingle)
348 bAdd = bVis[obj->aj];
353 newobj[nvis++] = *obj;
357 newobj[nhide--] = *obj;
366 static void draw_objects(Display* disp,
386 case eBThin: XSetLineAttributes(disp, gc, 1, LineSolid, CapNotLast, JoinRound); break;
387 case eBFat: XSetLineAttributes(disp, gc, 3, LineSolid, CapNotLast, JoinRound); break;
388 case eBVeryFat: XSetLineAttributes(disp, gc, 5, LineSolid, CapNotLast, JoinRound); break;
393 default: gmx_fatal(FARGS, "Invalid bond_type selected: %d\n", bond_type); break;
395 for (i = 0; (i < nobj); i++)
400 case eOSingle: draw_atom(disp, w, gc, obj->ai, vec2, col, size, bBalls, bPlus); break;
402 draw_bond(disp, w, gc, obj->ai, obj->aj, vec2, x, col, size, bBalls);
407 draw_bond(disp, w, gc, obj->ai, obj->aj, vec2, x, col, size, bBalls);
413 XSetLineAttributes(disp, gc, 1, LineSolid, CapNotLast, JoinRound);
416 static void v4_to_iv2(vec4 x4, iv2 v2, int x0, int y0, real sx, real sy)
420 inv_z = 1.0 / x4[ZZ];
421 v2[XX] = x0 + sx * x4[XX] * inv_z;
422 v2[YY] = y0 - sy * x4[YY] * inv_z;
425 static void draw_box(t_x11* x11, Window w, t_3dview* view, matrix box, int x0, int y0, real sx, real sy, int boxtype)
427 rvec rect_tri[8] = { { 0, 0, 0 }, { 1, 0, 0 }, { 1, 1, 0 }, { 0, 1, 0 },
428 { 0, 0, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 0, 1, 1 } };
429 int tr_bonds[12][2] = { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 }, { 4, 5 }, { 5, 6 },
430 { 6, 7 }, { 7, 4 }, { 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 } };
431 static int* edge = nullptr;
433 rvec corner[NCUCEDGE], box_center;
437 calc_box_center(view->ecenter, box, box_center);
438 if (boxtype == esbTrunc)
440 calc_compact_unitcell_vertices(view->ecenter, box, corner);
443 edge = compact_unitcell_edges();
446 for (i = 0; (i < NCUCEDGE); i++)
448 gmx_mat4_transform_point(view->proj, corner[i], x4);
449 v4_to_iv2(x4, vec2[i], x0, y0, sx, sy);
451 XSetForeground(x11->disp, x11->gc, YELLOW);
452 for (i = 0; i < NCUCEDGE; i++)
455 i1 = edge[2 * i + 1];
456 XDrawLine(x11->disp, w, x11->gc, vec2[i0][XX], vec2[i0][YY], vec2[i1][XX], vec2[i1][YY]);
461 if (boxtype == esbRect)
463 for (j = 0; (j < DIM); j++)
465 box_center[j] -= 0.5 * box[j][j];
470 for (i = 0; (i < DIM); i++)
472 for (j = 0; (j < DIM); j++)
474 box_center[j] -= 0.5 * box[i][j];
478 for (i = 0; (i < 8); i++)
480 clear_rvec(corner[i]);
481 for (j = 0; (j < DIM); j++)
483 if (boxtype == esbTri)
485 for (k = 0; (k < DIM); k++)
487 corner[i][k] += rect_tri[i][j] * box[j][k];
492 corner[i][j] = rect_tri[i][j] * box[j][j];
495 rvec_inc(corner[i], box_center);
496 gmx_mat4_transform_point(view->proj, corner[i], x4);
497 v4_to_iv2(x4, vec2[i], x0, y0, sx, sy);
501 pr_rvecs(debug, 0, "box", box, DIM);
502 pr_rvecs(debug, 0, "corner", corner, 8);
504 XSetForeground(x11->disp, x11->gc, YELLOW);
505 for (i = 0; (i < 12); i++)
509 XDrawLine(x11->disp, w, x11->gc, vec2[i0][XX], vec2[i0][YY], vec2[i1][XX], vec2[i1][YY]);
514 void set_sizes(t_manager* man)
516 for (int i = 0; i < man->natom; i++)
520 man->size[i] = 180 * man->vdw[i];
525 void draw_mol(t_x11* x11, t_manager* man)
527 static char tstr[2][20];
528 static int ntime = 0;
549 y0 = win->height / 2;
550 sx = win->width / 2 * view->sc_x;
551 sy = win->height / 2 * view->sc_y;
553 my_init_pbc(man->box);
555 for (i = 0; (i < man->natom); i++)
559 gmx_mat4_transform_point(view->proj, man->x[i], x4);
561 v4_to_iv2(x4, vec2[i], x0, y0, sx, sy);
566 z_fill(man, man->zz);
569 XClearWindow(x11->disp, win->self);
572 std::sprintf(tstr[ntime], "Time: %.3f ps", man->time);
573 if (std::strcmp(tstr[ntime], tstr[1 - ntime]) != 0)
575 set_vbtime(x11, man->vbox, tstr[ntime]);
579 if (mw->boxtype != esbNone)
581 draw_box(x11, win->self, view, man->box, x0, y0, sx, sy, mw->boxtype);
584 /* Should sort on Z-Coordinates here! */
585 nvis = filter_vis(man);
586 if (nvis && man->bSort)
588 std::qsort(man->obj, nvis, sizeof(man->obj[0]), compare_obj);
591 /* Draw the objects */
592 draw_objects(x11->disp, win->self, x11->gc, nvis, man->obj, man->ix, man->x, man->col,
593 man->size, mw->bShowHydrogen, mw->bond_type, man->bPlus);
595 /* Draw the labels */
596 XSetForeground(x11->disp, x11->gc, WHITE);
597 for (i = 0; (i < man->natom); i++)
599 if (man->bLabel[i] && man->bVis[i])
601 XDrawString(x11->disp, win->self, x11->gc, vec2[i][XX] + 2, vec2[i][YY] - 2,
602 man->szLab[i], std::strlen(man->szLab[i]));
606 XSetForeground(x11->disp, x11->gc, x11->fg);