GLYLIB  0.3.0b
rotation.c
Go to the documentation of this file.
00001 /* Author: Michael Tessier*/
00002 #include <mylib.h>
00003 #include <molecules.h>
00004 void rollMolecule(molecule* mol, double rad)
00005 {
00006   //Tells the user what's going on
00007   printf("Rolling molecule approx. %.0lf degrees...\n",(rad*57.2957795));
00008   int i,j;
00009   residue* curRes; atom* curAtm;
00010   coord_3D temp; temp.i = 0; temp.j = 0; temp.k = 0;
00011   for(i = 0; i < (*mol).nr; i++)
00012   {
00013    curRes = ((*mol).r+i);
00014  
00015    for(j = 0; j < (*curRes).na; j++)
00016    {
00017     curAtm = ((*curRes).a+j);
00018     //Rotating about the X-axis
00019     (*curAtm).x.i = (*curAtm).x.i;
00020     temp.j=((*curAtm).x.j*cos(rad))+((*curAtm).x.k*sin(rad));
00021     temp.k=((*curAtm).x.j*-sin(rad))+((*curAtm).x.k*cos(rad));
00022     (*curAtm).x.j = temp.j; (*curAtm).x.k = temp.k;
00023    }
00024   }
00025 }
00026 
00027 void pitchMolecule(molecule* mol, double rad)
00028 {
00029   //Tells the user what's going on
00030   printf("Pitching molecule approx. %.0lf degrees...\n",(rad*57.2957795));
00031   int i,j;
00032   residue* curRes; atom* curAtm;
00033   coord_3D temp; temp.i = 0; temp.j = 0; temp.k = 0;
00034   for(i = 0; i < (*mol).nr; i++)
00035   {
00036    curRes = ((*mol).r+i);
00037  
00038    for(j = 0; j < (*curRes).na; j++)
00039    {
00040     curAtm = ((*curRes).a+j);
00041     //Rotating about the Y-axis
00042     temp.i=((*curAtm).x.i*cos(rad))+((*curAtm).x.k*-sin(rad));
00043     (*curAtm).x.j = (*curAtm).x.j;
00044     temp.k=((*curAtm).x.i*sin(rad))+((*curAtm).x.k*cos(rad));
00045     (*curAtm).x.i = temp.i; (*curAtm).x.k = temp.k;
00046    }
00047   }
00048 }
00049 
00050 void yawMolecule(molecule* mol, double rad)
00051 {
00052   //Tells the user what's going on
00053   //printf("Yawing molecule approx. %.0lf degrees...\n",(rad*57.2957795));
00054   int i,j;
00055   residue* curRes; atom* curAtm;
00056   coord_3D temp; temp.i = 0; temp.j = 0; temp.k = 0;
00057   for(i = 0; i < (*mol).nr; i++)
00058   {
00059    curRes = ((*mol).r+i);
00060  
00061    for(j = 0; j < (*curRes).na; j++)
00062    {
00063     curAtm = ((*curRes).a+j);
00064     //Rotating about the Z-axis
00065     temp.i=((*curAtm).x.i*cos(rad))+((*curAtm).x.j*sin(rad));
00066     temp.j=((*curAtm).x.i*-sin(rad))+((*curAtm).x.j*cos(rad));
00067     (*curAtm).x.i = temp.i; (*curAtm).x.j = temp.j;
00068     (*curAtm).x.k = (*curAtm).x.k;
00069 
00070    }
00071   }
00072 }
00073 void rollAssembly(assembly* asmbl, double rad)
00074 {
00075   //Tells the user what's going on
00076   printf("Rolling assembly approx. %.0lf degrees...\n",(rad*57.2957795));
00077   int i,j,k;
00078   molecule* mol; residue* curRes; atom* curAtm;
00079   coord_3D temp; temp.i = 0; temp.j = 0; temp.k = 0;
00080   for(k = 0; k < (*asmbl).nm; k++)
00081   {
00082    mol = ((*asmbl).m+k)[0]; // changed by BLF on 20080622 -- might need revisiting
00083    for(i = 0; i < (*mol).nr; i++)
00084    {
00085     curRes = ((*mol).r+i);
00086   
00087     for(j = 0; j < (*curRes).na; j++)
00088     {
00089      curAtm = ((*curRes).a+j);
00090      //Rotating about the X-axis
00091      (*curAtm).x.i = (*curAtm).x.i;
00092      temp.j=((*curAtm).x.j*cos(rad))+((*curAtm).x.k*sin(rad));
00093      temp.k=((*curAtm).x.j*-sin(rad))+((*curAtm).x.k*cos(rad));
00094      (*curAtm).x.j = temp.j; (*curAtm).x.k = temp.k;
00095     }
00096    }
00097   }
00098 }
00099 
00100 void pitchAssembly(assembly* asmbl, double rad)
00101 {
00102   //Tells the user what's going on
00103   printf("Pitching assembly approx. %.0lf degrees...\n",(rad*57.2957795));
00104   int i,j,k;
00105   molecule* mol; residue* curRes; atom* curAtm;
00106   coord_3D temp; temp.i = 0; temp.j = 0; temp.k = 0;
00107   for(k = 0; k < (*asmbl).nm; k++)
00108   {
00109    mol = ((*asmbl).m+k)[0]; // changed by BLF on 20080622 -- might need revisiting
00110    for(i = 0; i < (*mol).nr; i++)
00111    {
00112     curRes = ((*mol).r+i);
00113   
00114     for(j = 0; j < (*curRes).na; j++)
00115     {
00116      curAtm = ((*curRes).a+j);
00117      //Rotating about the Y-axis
00118      temp.i=((*curAtm).x.i*cos(rad))+((*curAtm).x.k*-sin(rad));
00119      (*curAtm).x.j = (*curAtm).x.j;
00120      temp.k=((*curAtm).x.i*sin(rad))+((*curAtm).x.k*cos(rad));
00121      (*curAtm).x.i = temp.i; (*curAtm).x.k = temp.k;
00122     }
00123    }
00124   }
00125 }
00126 
00127 void yawAssembly(assembly* asmbl, double rad)
00128 {
00129   //Tells the user what's going on
00130   printf("Yawing assembly approx. %.0lf degrees...\n",(rad*57.2957795));
00131   int i,j,k;
00132   molecule* mol; residue* curRes; atom* curAtm;
00133   coord_3D temp; temp.i = 0; temp.j = 0; temp.k = 0;
00134   for(k = 0; k < (*asmbl).nm; k++)
00135   {
00136    mol = ((*asmbl).m+k)[0]; // changed by BLF on 20080622 -- might need revisiting
00137    for(i = 0; i < (*mol).nr; i++)
00138    {
00139     curRes = ((*mol).r+i);
00140   
00141     for(j = 0; j < (*curRes).na; j++)
00142     {
00143      curAtm = ((*curRes).a+j);
00144      //Rotating about the Z-axis
00145      temp.i=((*curAtm).x.i*cos(rad))+((*curAtm).x.j*sin(rad));
00146      temp.j=((*curAtm).x.i*-sin(rad))+((*curAtm).x.j*cos(rad));
00147      (*curAtm).x.i = temp.i; (*curAtm).x.j = temp.j;
00148      (*curAtm).x.k = (*curAtm).x.k;
00149  
00150     }
00151    }
00152   }
00153 }
00154 
00155 
00156 /* 
00157  * Functions for rotation about an axis
00158  *
00159  * author: Robert Davis
00160  *      integrated with glylib by BLFoley 20110405
00161  */
00162 
00163 /** Create a rotation matrix for a rotation of theta degrees
00164  * about an axis through the given point in the given direction
00165  */
00166 double *
00167 create_rotation_matrix(coord_3D point, vectormag_3D direction, double theta)
00168 {
00169     direction=normalize_vec(direction);
00170     if(direction.d==0){fprintf(stderr,"Attempting to rotate about a vector of zero length in create_rotation_matrix.  Ignoring.\n");}
00171     double u = direction.i;
00172     double v = direction.j;
00173     double w = direction.k;
00174 
00175     double a = point.i;
00176     double b = point.j;
00177     double c = point.k;
00178 
00179     double *matrix = (double *)malloc(12 * sizeof(double));
00180     double u2 = u*u;
00181     double v2 = v*v;
00182     double w2 = w*w;
00183     double cosT = cos(theta);
00184     double sinT = sin(theta);
00185  
00186     matrix[3] = a*(v2 + w2) - u*(b*v + c*w)
00187                  + (u*(b*v + c*w) - a*(v2 + w2))*cosT + (b*w - c*v)*sinT;
00188 
00189     matrix[7] = b*(u2 + w2) - v*(a*u + c*w)
00190                 + (v*(a*u + c*w) - b*(u2 + w2))*cosT + (c*u - a*w)*sinT;
00191 
00192     matrix[11] = c*(u2 + v2) - w*(a*u + b*v)
00193                  + (w*(a*u + b*v) - c*(u2 + v2))*cosT + (a*v - b*u)*sinT;
00194 
00195     matrix[0] = (u2 + (v2 + w2) * cosT);
00196     matrix[1] = (u*v * (1 - cosT) - w*sinT);
00197     matrix[2] = (u*w * (1 - cosT) + v*sinT);
00198 
00199     matrix[4] = (u*v * (1 - cosT) + w*sinT);
00200     matrix[5] = (v2 + (u2 + w2) * cosT);
00201     matrix[6] = (v*w * (1 - cosT) - u*sinT);
00202 
00203     matrix[8] = (u*w * (1 - cosT) - v*sinT);
00204     matrix[9] = (v*w * (1 - cosT) + u*sinT);
00205     matrix[10] = (w2 + (u2 + v2) * cosT);
00206 
00207     return matrix;
00208 }
00209 
00210 void
00211 destroy_rotation_matrix(double *matrix)
00212 {
00213     free(matrix);
00214 }
00215 
00216 void
00217 apply_rotation_matrix_to_coord_p(coord_3D *c, double *matrix)
00218 {
00219     coord_3D temp = *c;
00220 
00221     c->i = matrix[0]*temp.i + matrix[1]*temp.j + matrix[2]*temp.k + matrix[3];
00222     c->j = matrix[4]*temp.i + matrix[5]*temp.j + matrix[6]*temp.k + matrix[7];
00223     c->k = matrix[8]*temp.i + matrix[9]*temp.j + matrix[10]*temp.k + matrix[11];
00224 }
00225 
00226 void 
00227 rotate_coords_about_axis_dp_list(coord_3D **coords, int num_coords, coord_3D point, 
00228               vectormag_3D direction, double theta)
00229 {
00230     int i;
00231     double *matrix;
00232 
00233     matrix = create_rotation_matrix(point, direction, theta);
00234     for(i = 0; i < num_coords; i++)
00235         apply_rotation_matrix_to_coord_p(coords[i], matrix);
00236 
00237     destroy_rotation_matrix(matrix);
00238 }
00239 
00240 coord_3D
00241 get_cartesian_point_from_internal_coords(coord_3D a, coord_3D b, coord_3D c,
00242                       double theta, double phi, double distance)
00243 {
00244     vectormag_3D lmn_x, lmn_y, lmn_z;
00245     double x_p, y_p, z_p;
00246 
00247     vectormag_3D cb = get_vector_from_coords(c, b);
00248     vectormag_3D ba = get_vector_from_coords(b, a);
00249 
00250     lmn_y = normalize_vec(get_crossprod(ba, cb));
00251     lmn_z = normalize_vec(cb);
00252     lmn_x = get_crossprod(lmn_y, lmn_z);
00253 
00254     x_p = distance * sin(theta) * cos(phi);
00255     y_p = distance * sin(theta) * sin(phi);
00256     z_p = distance * cos(theta);
00257 
00258     return (coord_3D) {lmn_x.i*x_p + lmn_y.i*y_p + lmn_z.i*z_p + c.i,
00259                     lmn_x.j*x_p + lmn_y.j*y_p + lmn_z.j*z_p + c.j,
00260                     lmn_x.k*x_p + lmn_y.k*y_p + lmn_z.k*z_p + c.k};
00261 }
00262 
 All Classes Files Functions Variables Typedefs Defines