changed reading hint
[gromacs/adressmacs.git] / src / ngmx / nmol.c
blobbb31405c3b08f8c17c2b8d24572d30cb25134081
1 /*
2 * $Id$
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 2.0
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * Please refer to:
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
23 * or e-mail to:
24 * gromacs@chem.rug.nl
26 * And Hey:
27 * Great Red Oystrich Makes All Chemists Sane
29 static char *SRCID_nmol_c = "$Id$";
31 #include <math.h>
32 #include "sysstuff.h"
33 #include "string.h"
34 #include "smalloc.h"
35 #include "macros.h"
36 #include "xutil.h"
37 #include "3dview.h"
38 #include "fatal.h"
39 #include "buttons.h"
40 #include "manager.h"
41 #include "nmol.h"
43 #define MSIZE 4
45 static bool MWCallBack(t_x11 *x11,XEvent *event, Window w, void *data)
47 t_molwin *mw;
48 Window To;
49 XEvent letter;
51 mw=(t_molwin *)data;
52 To=mw->wd.Parent;
53 letter.type=ClientMessage;
54 letter.xclient.display=x11->disp;
55 letter.xclient.window=To;
56 letter.xclient.message_type=0;
57 letter.xclient.format=32;
58 switch(event->type) {
59 case Expose:
60 /* Do Not draw anything, but signal parent instead, he will
61 * coordinate drawing.
63 letter.xclient.data.l[0]=IDDRAWMOL;
64 letter.xclient.data.l[1]=Button1;
65 XSendEvent(x11->disp,To,True,0,&letter);
66 break;
67 case ButtonPress:
68 #ifdef DEBUG
69 printf("Molwindow: Buttonpress\n");
70 #endif
71 letter.xclient.data.l[0]=IDLABEL;
72 letter.xclient.data.l[1]=(long)event->xbutton.button;
73 letter.xclient.data.l[2]=event->xbutton.x;
74 letter.xclient.data.l[3]=event->xbutton.y;
75 XSendEvent(x11->disp,To,True,0,&letter);
76 break;
77 case ConfigureNotify:
78 mw->wd.width=event->xconfigure.width;
79 mw->wd.height=event->xconfigure.height;
80 break;
81 default:
82 break;
84 return FALSE;
87 void set_def (t_molwin *mw)
89 mw->bShowHydrogen=TRUE;
90 mw->bond_type=eBFat;
91 mw->bBoxSelect=TRUE;
94 t_molwin *init_mw(t_x11 *x11,Window Parent,
95 int x,int y,int width,int height,
96 unsigned long fg,unsigned long bg)
98 t_molwin *mw;
100 snew(mw,1);
101 set_def(mw);
103 InitWin(&mw->wd,x,y,width,height,1,"Mol Window");
105 mw->wd.Parent=Parent;
106 mw->wd.self=XCreateSimpleWindow(x11->disp,Parent,x,y,width,height,1,fg,bg);
107 x11->RegisterCallback(x11,mw->wd.self,Parent,MWCallBack,mw);
108 x11->SetInputMask(x11,mw->wd.self,
109 ExposureMask | StructureNotifyMask |
110 ButtonPressMask);
111 return mw;
114 void map_mw(t_x11 *x11,t_molwin *mw)
116 XMapWindow(x11->disp,mw->wd.self);
119 bool toggle_hydrogen(t_x11 *x11,t_molwin *mw)
121 mw->bShowHydrogen=!mw->bShowHydrogen;
122 ExposeWin(x11->disp,mw->wd.self);
124 return mw->bShowHydrogen;
127 void set_bond_type(t_x11 *x11,t_molwin *mw,int bt)
129 if (bt != mw->bond_type) {
130 mw->bond_type=bt;
131 ExposeWin(x11->disp,mw->wd.self);
135 bool toggle_box (t_x11 *x11,t_molwin *mw)
137 mw->bBoxSelect =! mw->bBoxSelect;
138 ExposeWin(x11->disp,mw->wd.self);
140 return mw->bBoxSelect;
143 void done_mw(t_x11 *x11,t_molwin *mw)
145 x11->UnRegisterCallback(x11,mw->wd.self);
146 sfree(mw);
149 static void draw_atom(Display *disp,Window w,GC gc,
150 atom_id ai,iv2 vec2[],unsigned long col[],int size[],
151 bool bBall,bool bPlus)
153 int xi,yi;
155 xi=vec2[ai][XX];
156 yi=vec2[ai][YY];
157 XSetForeground(disp,gc,col[ai]);
158 if (bBall) {
159 XFillCircle(disp,w,gc,xi,yi,size[ai]-1);
160 XSetForeground(disp,gc,BLACK);
161 XDrawCircle(disp,w,gc,xi,yi,size[ai]);
162 /* XSetForeground(disp,gc,WHITE);
163 XFillCircle(disp,w,gc,xi+4,yi-4,4); */
165 else if (bPlus) {
166 XDrawLine(disp,w,gc,xi-MSIZE,yi,xi+MSIZE+1,yi);
167 XDrawLine(disp,w,gc,xi,yi-MSIZE,xi,yi+MSIZE+1);
169 else
170 XDrawLine(disp,w,gc,xi-1,yi,xi+1,yi);
174 /* Global variables */
175 static rvec gl_fbox,gl_hbox,gl_mhbox;
177 static void init_pbc(matrix box)
179 int i;
181 for(i=0; (i<DIM); i++) {
182 gl_fbox[i] = box[i][i];
183 gl_hbox[i] = gl_fbox[i]*0.5;
184 gl_mhbox[i] = -gl_hbox[i];
188 static bool local_pbc_dx(rvec x1, rvec x2)
190 int i;
191 real dx;
193 for(i=0; (i<DIM); i++) {
194 dx=x1[i]-x2[i];
195 if (dx > gl_hbox[i])
196 return FALSE;
197 else if (dx <= gl_mhbox[i])
198 return FALSE;
200 return TRUE;
203 static void draw_bond(Display *disp,Window w,GC gc,
204 atom_id ai,atom_id aj,iv2 vec2[],
205 rvec x[],unsigned long col[],int size[],bool bBalls)
207 unsigned long ic,jc;
208 int xi,yi,xj,yj;
209 int xm,ym;
211 if (bBalls) {
212 draw_atom(disp,w,gc,ai,vec2,col,size,TRUE,FALSE);
213 draw_atom(disp,w,gc,aj,vec2,col,size,TRUE,FALSE);
215 else {
216 if (local_pbc_dx(x[ai],x[aj])) {
217 ic=col[ai];
218 jc=col[aj];
219 xi=vec2[ai][XX];
220 yi=vec2[ai][YY];
221 xj=vec2[aj][XX];
222 yj=vec2[aj][YY];
224 if (ic != jc) {
225 xm=(xi+xj) >> 1;
226 ym=(yi+yj) >> 1;
228 XSetForeground(disp,gc,ic);
229 XDrawLine(disp,w,gc,xi,yi,xm,ym);
230 XSetForeground(disp,gc,jc);
231 XDrawLine(disp,w,gc,xm,ym,xj,yj);
233 else {
234 XSetForeground(disp,gc,ic);
235 XDrawLine(disp,w,gc,xi,yi,xj,yj);
241 int compare_obj(const void *a,const void *b)
243 t_object *oa,*ob;
244 real z;
246 oa=(t_object *)a;
247 ob=(t_object *)b;
249 z=oa->z-ob->z;
251 if (z < 0)
252 return 1;
253 else if (z > 0)
254 return -1;
255 else
256 return 0;
259 void create_visibility(t_manager *man)
261 t_object *obj;
262 int i;
264 for(i=0,obj=man->obj; (i<man->nobj); i++,obj++)
265 if (obj->eV != eVHidden) {
266 man->bVis[obj->ai]=TRUE;
267 switch (obj->eO) {
268 case eOBond:
269 case eOHBond:
270 man->bVis[obj->aj]=TRUE;
271 break;
272 default:
273 break;
278 void z_fill(t_manager *man, real *zz)
280 t_object *obj;
281 int i;
283 for(i=0,obj=man->obj; (i<man->nobj); i++,obj++)
284 switch (obj->eO) {
285 case eOSingle:
286 obj->z = zz[obj->ai];
287 break;
288 case eOBond:
289 case eOHBond:
290 obj->z = (zz[obj->ai] + zz[obj->aj]) * 0.5;
291 break;
292 default:
293 break;
297 int filter_vis(t_manager *man)
299 int i,nobj,nvis,nhide;
300 atom_id ai;
301 bool bAdd,*bVis;
302 t_object *obj;
303 t_object *newobj;
305 nobj=man->nobj;
306 snew(newobj,nobj);
307 obj=man->obj;
308 bVis=man->bVis;
309 nvis=0;
310 nhide=nobj-1;
311 for(i=0; (i<nobj); i++,obj++) {
312 ai=obj->ai;
313 bAdd=bVis[ai];
314 if (bAdd)
315 if (obj->eO != eOSingle)
316 bAdd=bVis[obj->aj];
317 if (bAdd)
318 newobj[nvis++]=*obj;
319 else
320 newobj[nhide--]=*obj;
322 sfree(man->obj);
323 man->obj=newobj;
325 return nvis;
328 void draw_objects(Display *disp,Window w,GC gc,int nobj,
329 t_object objs[],iv2 vec2[],rvec x[],
330 unsigned long col[],int size[],bool bShowHydro,int bond_type,
331 bool bPlus)
333 bool bBalls;
334 int i;
335 t_object *obj;
337 bBalls=FALSE;
338 switch (bond_type) {
339 case eBThin:
340 XSetLineAttributes(disp,gc,1,LineSolid,CapNotLast,JoinRound);
341 break;
342 case eBFat:
343 XSetLineAttributes(disp,gc,3,LineSolid,CapNotLast,JoinRound);
344 break;
345 case eBVeryFat:
346 XSetLineAttributes(disp,gc,5,LineSolid,CapNotLast,JoinRound);
347 break;
348 case eBSpheres:
349 bBalls=TRUE;
350 bPlus=FALSE;
351 break;
352 default:
353 fatal_error(0,"Invalid bond_type selected: %d\n",bond_type);
354 break;
356 for(i=0; (i<nobj); i++) {
357 obj=&(objs[i]);
358 switch (obj->eO) {
359 case eOSingle:
360 draw_atom(disp,w,gc,obj->ai,vec2,col,size,bBalls,bPlus);
361 break;
362 case eOBond:
363 draw_bond(disp,w,gc,obj->ai,obj->aj,vec2,x,col,size,bBalls);
364 break;
365 case eOHBond:
366 if (bShowHydro)
367 draw_bond(disp,w,gc,obj->ai,obj->aj,vec2,x,col,size,bBalls);
368 break;
369 default:
370 break;
373 XSetLineAttributes(disp,gc,1,LineSolid,CapNotLast,JoinRound);
376 static void v4_to_iv2(vec4 x4,iv2 v2,int x0,int y0,real sx,real sy)
378 real inv_z;
380 inv_z=1.0/x4[ZZ];
381 v2[XX]=x0+sx*x4[XX]*inv_z;
382 v2[YY]=y0-sy*x4[YY]*inv_z;
385 static void draw_box(t_x11 *x11,Window w,t_3dview *view,matrix box,
386 int x0,int y0,real sx,real sy)
388 int ivec[8][4] = {
389 { 0,0,0,1 }, { 1,0,0,1 }, { 1,1,0,1 }, { 0,1,0,1 },
390 { 0,0,1,1 }, { 1,0,1,1 }, { 1,1,1,1 }, { 0,1,1,1 }
392 int bonds[12][2] = {
393 { 0,1 }, { 1,2 }, { 2,3 }, { 3,0 },
394 { 4,5 }, { 5,6 }, { 6,7 }, { 7,4 },
395 { 0,4 }, { 1,5 }, { 2,6 }, { 3,7 }
397 int i,j;
398 rvec corner[8];
399 vec4 x4;
400 iv2 vec2[12];
402 for (i=0; (i<8); i++) {
403 for (j=0; (j<DIM); j++)
404 corner[i][j] = ivec[i][j]*box[j][j];
405 m4_op(view->proj,corner[i],x4);
406 v4_to_iv2(x4,vec2[i],x0,y0,sx,sy);
408 XSetForeground(x11->disp,x11->gc,YELLOW);
409 for (i=0; (i<12); i++)
410 XDrawLine(x11->disp,w,x11->gc,
411 vec2[bonds[i][0]][XX],vec2[bonds[i][0]][YY],
412 vec2[bonds[i][1]][XX],vec2[bonds[i][1]][YY]);
415 void set_sizes(t_manager *man,real sx,real sy)
417 int i;
419 for(i=0; (i<man->natom); i++)
420 if (man->bVis[i])
421 man->size[i]=180*man->vdw[i];
424 void draw_mol(t_x11 *x11,t_manager *man)
426 static char tstr[2][20];
427 static int ntime=0;
428 t_windata *win;
429 t_3dview *view;
430 t_molwin *mw;
431 int i,x0,y0,nvis;
432 iv2 *vec2;
433 real sx,sy;
434 vec4 x4;
436 if (man->status == -1)
437 return;
439 view=man->view;
440 mw=man->molw;
442 win=&(mw->wd);
444 vec2=man->ix;
445 x0=win->width/2;
446 y0=win->height/2;
447 sx=win->width/2*view->sc_x;
448 sy=win->height/2*view->sc_y;
450 init_pbc(man->box);
452 /* create_visibility(man); */
454 for(i=0; (i<man->natom); i++) {
455 if (man->bVis[i]) {
456 m4_op(view->proj,man->x[i],x4);
457 man->zz[i]=x4[ZZ];
458 v4_to_iv2(x4,vec2[i],x0,y0,sx,sy);
461 set_sizes(man,sx,sy);
463 z_fill (man,man->zz);
465 /* Start drawing */
466 XClearWindow(x11->disp,win->self);
468 /* Draw Time */
469 sprintf(tstr[ntime],"Time: %.3f ps",man->time);
470 if (strcmp(tstr[ntime],tstr[1-ntime]) != 0) {
471 set_vbtime(x11,man->vbox,tstr[ntime]);
472 ntime=1-ntime;
475 if (mw->bBoxSelect)
476 draw_box(x11,win->self,view,man->box,x0,y0,sx,sy);
478 /* Should sort on Z-Coordinates here! */
479 nvis=filter_vis(man);
480 if (nvis && man->bSort)
481 qsort(man->obj,nvis,sizeof(man->obj[0]),compare_obj);
483 /* Draw the objects */
484 draw_objects(x11->disp,win->self,x11->gc,
485 nvis,man->obj,man->ix,man->x,man->col,man->size,
486 mw->bShowHydrogen,mw->bond_type,man->bPlus);
488 /* Draw the labels */
489 XSetForeground(x11->disp,x11->gc,WHITE);
490 for(i=0; (i<man->natom); i++) {
491 if (man->bLabel[i] && man->bVis[i]) {
492 XDrawString(x11->disp,win->self,x11->gc,vec2[i][XX]+2,vec2[i][YY]-2,
493 man->szLab[i],strlen(man->szLab[i]));
497 XSetForeground(x11->disp,x11->gc,x11->fg);