1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
/*******************************************************************************
* *
* Viewmol *
* *
* M O D I F Y G E O . C *
* *
* Copyright (c) Joerg-R. Hill, October 2003 *
* *
********************************************************************************
*
* $Id: modifygeo.c,v 1.4 2003/11/07 11:07:25 jrh Exp $
* $Log: modifygeo.c,v $
* Revision 1.4 2003/11/07 11:07:25 jrh
* Release 2.4
*
* Revision 1.3 2000/12/10 15:12:10 jrh
* Release 2.3
*
* Revision 1.2 1999/05/24 01:28:58 jrh
* Release 2.2.1
*
* Revision 1.1 1999/02/07 21:53:09 jrh
* Initial revision
*
*
*/
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<Xm/Xm.h>
#include<GL/gl.h>
#include "viewmol.h"
#include "dialog.h"
extern double angle(double, double, double, double, double, double,double,double,double);
extern double bondLength(struct ATOM *, int, int);
extern double bondAngle(struct ATOM *, int, int, int);
extern double torsionAngle(struct ATOM *, int, int, int, int);
extern int messgb(Widget, int, char *, struct PushButtonRow *, int);
extern void GetMessageBoxButton(Widget, XtPointer, caddr_t);
extern struct SAVE *saveGeometry(struct MOLECULE *);
extern void restoreGeometry(struct SAVE *, int);
extern int calcInternal(struct MOLECULE *, int);
extern void makeConnectivity(struct MOLECULE *, int, int);
extern char *getStringResource(Widget, char *);
extern void vectorProduct(double *, double *, double *);
extern double scalarProduct(double *, double *);
extern void *getmem(size_t, size_t);
extern void *expmem(void *, size_t, size_t);
extern void fremem(void **);
extern void redraw(int);
extern void setAtom(struct MOLECULE *);
extern void deleteMolecule(Widget, caddr_t, XmAnyCallbackStruct *);
extern void clearGeometry(Widget, caddr_t, XmAnyCallbackStruct *);
extern void setMenuItem(int, int);
int findBondPartners(int *, int, int);
int findAttachedAtoms(int *, int, int);
void changeBondLength(int, int, int *, double);
int changeAngle(int, int, int *, double);
void makeRotationMatrix(double, double, double, double, double matrix[3][3]);
void addUndo(struct MOLECULE *);
extern struct MOLECULE *molecules;
extern struct WINDOW windows[];
extern struct UNDO *undo;
extern Widget topShell, undoButton;
extern double bndfac;
extern int debug, nUndo;
extern int showWarning;
void modifyGeometry(int which, char *text)
{
static struct PushButtonRow buttons[] = {{"ok", GetMessageBoxButton,
(XtPointer)0, NULL}};
struct MOLECULE *mol;
double factor, value, diff;
int *move, *tmove, nmove, m, save;
char *word;
register int i;
if (windows[VIEWER].set >= 0)
mol=&molecules[windows[VIEWER].set];
else
mol=&molecules[0];
move=(int *)getmem(mol->na, sizeof(int));
move[0]=mol->internals[which].atoms[0];
if (mol->atoms[move[0]].nbonds > 1)
nmove=findAttachedAtoms(move, 1, mol->internals[which].atoms[1]);
else
nmove=1;
if (nmove < 0 && (mol->internals[which].type == ANGLE || mol->internals[which].type == TORSION))
{
move[0]=mol->internals[which].atoms[1];
nmove=findAttachedAtoms(move, 1, mol->internals[which].atoms[2]);
}
if (nmove < 0)
{
if (showWarning)
{
word=getStringResource(topShell, "notChangeable");
messgb(topShell, 1, word, buttons, 1);
}
fremem((void *)&move);
return;
}
addUndo(mol);
value=atof(text);
switch (mol->internals[which].type)
{
case BONDLENGTH: factor=1.0;
if (strstr(text, "pm")) factor=0.01;
if (strstr(text, "bohr") || strstr(text, "au")) factor=0.52917706;
changeBondLength(which, nmove, move, factor*value);
break;
case ANGLE: (void)changeAngle(which, nmove, move, value);
break;
case TORSION: diff=value-torsionAngle(mol->atoms,
mol->internals[which].atoms[0],
mol->internals[which].atoms[1],
mol->internals[which].atoms[2],
mol->internals[which].atoms[3]);
(void)changeAngle(which, nmove, move, value);
move[0]=mol->internals[which].atoms[1];
if ((nmove=findBondPartners(move, 1, mol->internals[which].atoms[0])) > 1)
{
tmove=(int *)getmem(mol->na, sizeof(int));
save=mol->internals[which].atoms[0];
for (i=1; i<nmove; i++)
{
if (move[i] != mol->internals[which].atoms[2])
{
tmove[0]=move[i];
if ((m=findAttachedAtoms(tmove, 1, move[0])) > 0)
{
value=torsionAngle(mol->atoms, move[i], move[0],
mol->internals[which].atoms[2],
mol->internals[which].atoms[3])+diff;
mol->internals[which].atoms[0]=move[i];
(void)changeAngle(which, m, tmove, value);
}
}
}
mol->internals[which].atoms[0]=save;
fremem((void *)&tmove);
}
break;
}
fremem((void *)&move);
makeConnectivity(mol, FALSE, TRUE);
for (i=0; i<mol->ninternal; i++)
(void)calcInternal(mol, i);
}
int findBondPartners(int *move, int n, int except)
{
struct MOLECULE *mol;
int found;
register int i, j, k, l;
/* Walk through list of bonds and add all atoms connected to
the atoms in move[] to move[], ignore the bond between
move[0] and except and all hydrogen bonds */
if (windows[VIEWER].set >= 0)
mol=&molecules[windows[VIEWER].set];
else
mol=&molecules[0];
l=n;
for (i=0; i<mol->nb; i++)
{
for (j=0; j<n; j++)
{
if (mol->bonds[i].order != (-1)) /* ignore hydrogen bonds */
{
if ((mol->bonds[i].first == move[0] &&
mol->bonds[i].second == except) ||
(mol->bonds[i].first == except &&
mol->bonds[i].second == move[0])) continue;
if (mol->bonds[i].first == move[j])
{
found=FALSE;
for (k=0; k<l; k++)
if (mol->bonds[i].second == move[k]) found=TRUE;
if (!found) move[l++]=mol->bonds[i].second;
}
if (mol->bonds[i].second == move[j])
{
found=FALSE;
for (k=0; k<l; k++)
if (mol->bonds[i].first == move[k]) found=TRUE;
if (!found) move[l++]=mol->bonds[i].first;
}
}
}
}
return(l);
}
int findAttachedAtoms(int *move, int n, int except)
{
int nmove=1, nmoveold=0, save=except;
register int i;
while (nmove != nmoveold)
{
nmoveold=nmove;
nmove=findBondPartners(move, nmoveold, except);
}
for (i=1; i<nmove; i++)
{
if (move[i] == save)
{
nmove=(-nmove);
break;
}
}
if (debug)
{
printf("Atoms to move: ");
if (nmove < 0)
printf("ring detected, no atoms to move.");
else
{
for (i=0; i<nmove; i++)
printf("%d ", move[i]);
}
printf("\n");
}
return(nmove);
}
void changeBondLength(int which, int nmove, int *move, double newLength)
{
struct MOLECULE *mol;
double oldLength, factor;
double vector[3];
int i, j;
if (windows[VIEWER].set >= 0)
mol=&molecules[windows[VIEWER].set];
else
mol=&molecules[0];
i=mol->internals[which].atoms[0];
j=mol->internals[which].atoms[1];
oldLength=bondLength(mol->atoms, i, j);
factor=newLength/oldLength-1.0;
vector[0]=factor*(mol->atoms[i].x-mol->atoms[j].x);
vector[1]=factor*(mol->atoms[i].y-mol->atoms[j].y);
vector[2]=factor*(mol->atoms[i].z-mol->atoms[j].z);
mol->internals[which].value=bndfac*newLength;
mol->internals[which].x=(mol->atoms[i].x+vector[0]+mol->atoms[j].x)*0.5+0.05;
mol->internals[which].y=(mol->atoms[i].y+vector[1]+mol->atoms[j].y)*0.5+0.05;
mol->internals[which].z=(mol->atoms[i].z+vector[2]+mol->atoms[j].z)*0.5+0.05;
for (i=0; i<nmove; i++)
{
mol->atoms[move[i]].x+=vector[0];
mol->atoms[move[i]].y+=vector[1];
mol->atoms[move[i]].z+=vector[2];
}
}
int changeAngle(int which, int nmove, int *move, double newAngle)
{
struct MOLECULE *mol;
double oldAngle, matrix[3][3];
double v1[3], v2[3], v3[3];
register double x, y, z;
register int i, j, k;
if (windows[VIEWER].set >= 0)
mol=&molecules[windows[VIEWER].set];
else
mol=&molecules[0];
i=mol->internals[which].atoms[0];
j=mol->internals[which].atoms[1];
k=mol->internals[which].atoms[2];
if (mol->internals[which].type == ANGLE)
{
oldAngle=bondAngle(mol->atoms, i, j, k);
v1[0]=mol->atoms[i].x-mol->atoms[j].x;
v1[1]=mol->atoms[i].y-mol->atoms[j].y;
v1[2]=mol->atoms[i].z-mol->atoms[j].z;
v2[0]=mol->atoms[k].x-mol->atoms[j].x;
v2[1]=mol->atoms[k].y-mol->atoms[j].y;
v2[2]=mol->atoms[k].z-mol->atoms[j].z;
vectorProduct(v1, v2, v3);
/* The two bonds are co-linear, find an arbitrary vector perpendicular to the first bond */
if (fabs(v3[0]) < 1.0e-6 && fabs(v3[1]) < 1.0e-6 && fabs(v3[2]) < 1.0e-6)
{
v3[0]=-v1[1]/v1[0];
v3[1]=1.0;
v3[2]=0.0;
}
}
else /* torsion angle */
{
oldAngle=torsionAngle(mol->atoms, i, j, k,
mol->internals[which].atoms[3]);
/* Torsion angle is undefined (one of the bond angles in 180 deg) */
if (oldAngle > 360.) return(FALSE);
v3[0]=mol->atoms[k].x-mol->atoms[j].x;
v3[1]=mol->atoms[k].y-mol->atoms[j].y;
v3[2]=mol->atoms[k].z-mol->atoms[j].z;
}
makeRotationMatrix(newAngle-oldAngle, v3[0], v3[1], v3[2], matrix);
for (i=0; i<nmove; i++)
{
x=mol->atoms[move[i]].x-mol->atoms[j].x;
y=mol->atoms[move[i]].y-mol->atoms[j].y;
z=mol->atoms[move[i]].z-mol->atoms[j].z;
mol->atoms[move[i]].x=x*matrix[0][0]+y*matrix[0][1]+z*matrix[0][2]+mol->atoms[j].x;
mol->atoms[move[i]].y=x*matrix[1][0]+y*matrix[1][1]+z*matrix[1][2]+mol->atoms[j].y;
mol->atoms[move[i]].z=x*matrix[2][0]+y*matrix[2][1]+z*matrix[2][2]+mol->atoms[j].z;
}
mol->internals[which].value=newAngle;
return(TRUE);
}
void makeRotationMatrix(double angle, double x, double y, double z, double matrix[3][3])
{
double r, ca, sa, xx, xy, xz, yy, yz, zz;
register double ca1;
r=1.0/sqrt(x*x+y*y+z*z);
x*=r;
y*=r;
z*=r;
angle*=atan(1.0)/45.0;
ca=cos(angle);
sa=sin(angle);
xx=x*x;
xy=x*y;
xz=x*z;
yy=y*y;
yz=y*z;
zz=z*z;
ca1=1.0-ca;
matrix[0][0]=ca1*xx+ca;
matrix[1][0]=ca1*xy-sa*z;
matrix[2][0]=ca1*xz+sa*y;
matrix[0][1]=ca1*xy+sa*z;
matrix[1][1]=ca1*yy+ca;
matrix[2][1]=ca1*yz-sa*x;
matrix[0][2]=ca1*xz-sa*y;
matrix[1][2]=ca1*yz+sa*x;
matrix[2][2]=ca1*zz+ca;
}
void addUndo(struct MOLECULE *mol)
{
if (nUndo++ == 0)
undo=(struct UNDO *)getmem((size_t)nUndo, sizeof(struct UNDO));
else
undo=(struct UNDO *)expmem((void *)undo, (size_t)nUndo, sizeof(struct UNDO));
undo[nUndo-1].coord=saveGeometry(mol);
undo[nUndo-1].mol=windows[VIEWER].set;
undo[nUndo-1].na=mol->na;
setMenuItem(VIEWER2_UNDO, True);
if (undoButton != NULL) XtVaSetValues(undoButton, XmNsensitive, True, NULL);
}
void undoGeometry(Widget w, caddr_t dummy, XmAnyCallbackStruct *data)
{
int imolSave;
register int i, imol;
if (undo != NULL)
{
imolSave=windows[VIEWER].set;
imol=windows[VIEWER].set=undo[--nUndo].mol;
if (imol == (-1))
deleteMolecule((Widget)0, (caddr_t)TRUE, (XmAnyCallbackStruct *)0);
else
{
if (undo[nUndo].na >= molecules[imol].na)
{
molecules[imol].atoms=(struct ATOM *)expmem((void *)molecules[imol].atoms,
(size_t)undo[nUndo].na, sizeof(struct ATOM));
}
molecules[imol].na=undo[nUndo].na;
restoreGeometry(undo[nUndo].coord, imol);
for (i=0; i<molecules[imol].ninternal; i++)
{
if (molecules[imol].internals[i].atoms[0] >= molecules[imol].na ||
molecules[imol].internals[i].atoms[1] >= molecules[imol].na ||
molecules[imol].internals[i].atoms[2] >= molecules[imol].na ||
molecules[imol].internals[i].atoms[3] >= molecules[imol].na)
clearGeometry((Widget)0, (caddr_t)i, (XmAnyCallbackStruct *)0);
else
{
(void)calcInternal(&molecules[imol], i);
if (molecules[imol].internals[i].type == TORSION &&
molecules[imol].internals[i].value > 360.)
clearGeometry((Widget)0, (caddr_t)i, (XmAnyCallbackStruct *)0);
}
}
makeConnectivity(&molecules[imol], FALSE, TRUE);
setAtom(&molecules[imol]);
}
if (nUndo)
undo=(struct UNDO *)expmem((void *)undo, (size_t)nUndo, sizeof(struct UNDO));
else
{
fremem((void *)&undo);
setMenuItem(VIEWER2_UNDO, False);
if (undoButton != NULL) XtVaSetValues(undoButton, XmNsensitive, False, NULL);
}
windows[VIEWER].set=imolSave;
redraw(VIEWER);
}
}
|