Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

py_trans.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2011 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: py_trans.C,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.15 $       $Date: 2010/12/16 04:08:57 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *  Python interface for transforming molecules (scale/trans/rot, etc)
00019  ***************************************************************************/
00020 
00021 #include "py_commands.h"
00022 #include "VMDApp.h"
00023 #include "CommandQueue.h"
00024 #include "MoleculeList.h"
00025 #include "Molecule.h"
00026 
00027 // rotate(axis, angle)
00028 static PyObject *rotate(PyObject *self, PyObject *args) {
00029   char axis;
00030   float angle;
00031 
00032   if (!PyArg_ParseTuple(args, (char *)"cf:trans.rotate", &axis, &angle)) 
00033     return NULL;
00034 
00035   if (axis != 'x' && axis != 'y' && axis != 'z') {
00036     PyErr_SetString(PyExc_ValueError, (char *)"axis must be 'x', 'y', or 'z'");
00037     return NULL;
00038   }
00039   VMDApp *app = get_vmdapp();
00040   app->scene_rotate_by(angle, axis);
00041 
00042   Py_INCREF(Py_None);
00043   return Py_None;
00044 }
00045 
00046 // translate(x,y,z)
00047 static PyObject *translate(PyObject *self, PyObject *args) {
00048   float x,y,z;
00049   
00050   if (!PyArg_ParseTuple(args, (char *)"fff:trans.translate", &x, &y, &z))
00051     return NULL;
00052 
00053   VMDApp *app = get_vmdapp(); 
00054   app->scene_translate_by(x,y,z);
00055   
00056   Py_INCREF(Py_None);
00057   return Py_None;
00058 }
00059 
00060 // scale(factor)
00061 static PyObject *scale(PyObject *self, PyObject *args) {
00062   float factor;
00063   if (!PyArg_ParseTuple(args, (char *)"f:trans.scale", &factor))
00064     return NULL;
00065 
00066   VMDApp *app = get_vmdapp();
00067   app->scene_scale_by(factor);
00068 
00069   Py_INCREF(Py_None);
00070   return Py_None;
00071 }
00072 
00073 static PyObject *get_center(PyObject *self, PyObject *args) {
00074   int molid;
00075   if (!PyArg_ParseTuple(args, (char *)"i:trans.get_center", &molid))
00076     return NULL;
00077 
00078   VMDApp *app = get_vmdapp();
00079   Molecule *mol = app->moleculeList->mol_from_id(molid);
00080   if (!mol) {
00081     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00082     return NULL;
00083   } 
00084   PyObject *newlist = PyList_New(3);
00085   for (int i=0; i<3; i++) 
00086     PyList_SET_ITEM(newlist, i, PyFloat_FromDouble(mol->centt[i]));
00087 
00088   return newlist;
00089 }
00090 
00091 // get_scale(molid)
00092 static PyObject *get_scale(PyObject *self, PyObject *args) {
00093   int molid;
00094   if (!PyArg_ParseTuple(args, (char *)"i:trans.get_scale", &molid))
00095     return NULL;
00096 
00097   VMDApp *app = get_vmdapp();
00098   Molecule *mol = app->moleculeList->mol_from_id(molid);
00099   if (!mol) {
00100     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00101     return NULL;
00102   } 
00103   return PyFloat_FromDouble(mol->scale);
00104 }
00105 
00106 // get_trans(molid)
00107 static PyObject *get_trans(PyObject *self, PyObject *args) {
00108   int molid;
00109   if (!PyArg_ParseTuple(args, (char *)"i:trans.get_trans", &molid))
00110     return NULL;
00111 
00112   VMDApp *app = get_vmdapp();
00113   Molecule *mol = app->moleculeList->mol_from_id(molid);
00114   if (!mol) {
00115     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00116     return NULL;
00117   } 
00118   PyObject *newlist = PyList_New(3);
00119   for (int i=0; i<3; i++) 
00120     PyList_SET_ITEM(newlist, i, PyFloat_FromDouble(mol->globt[i]));
00121 
00122   return newlist;
00123 }
00124 
00125 //get_rotation(molid)
00126 static PyObject *get_rotation(PyObject *self, PyObject *args) {
00127   int molid;
00128   if (!PyArg_ParseTuple(args, (char *)"i:trans.get_rotation", &molid))
00129     return NULL;
00130 
00131   VMDApp *app = get_vmdapp();
00132   Molecule *mol = app->moleculeList->mol_from_id(molid);
00133   if (!mol) {
00134     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00135     return NULL;
00136   } 
00137   PyObject *mat = PyList_New(16);
00138   Matrix4 &rotm = mol->rotm;
00139   for (int i=0; i<16; i++) {
00140     PyList_SET_ITEM(mat, i, PyFloat_FromDouble(rotm.mat[i]));
00141   }
00142   return mat;
00143 }
00144 
00145 // set_center(molid)
00146 static PyObject *set_center(PyObject *self, PyObject *args) {
00147   int i, molid;
00148   float c[3];
00149   PyObject *pylist;
00150   if (!PyArg_ParseTuple(args, (char *)"iO!:trans.set_center", &molid, &PyList_Type, &pylist))
00151     return NULL;
00152 
00153   VMDApp *app = get_vmdapp();
00154   Molecule *mol = app->moleculeList->mol_from_id(molid);
00155   if (!mol) {
00156     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00157     return NULL;
00158   } 
00159   if (PyList_GET_SIZE(pylist) != 3) {
00160     PyErr_SetString(PyExc_ValueError, (char *)"List must be of length 3");
00161     return NULL;
00162   }
00163   
00164   for (i=0; i<3; i++) {
00165     c[i] = PyFloat_AsDouble(PyList_GET_ITEM(pylist, i));
00166     if (PyErr_Occurred())
00167       return NULL;
00168   }
00169   mol->set_cent_trans(c[0], c[1], c[2]);
00170 
00171   Py_INCREF(Py_None);
00172   return Py_None;
00173 }
00174 
00175 // set_scale(molid)
00176 static PyObject *set_scale(PyObject *self, PyObject *args) {
00177   int molid;
00178   float scale;
00179   if (!PyArg_ParseTuple(args, (char *)"if:trans.set_scale", &molid, &scale)) 
00180     return NULL;
00181 
00182   VMDApp *app = get_vmdapp();
00183   Molecule *mol = app->moleculeList->mol_from_id(molid);
00184   if (!mol) {
00185     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00186     return NULL;
00187   }
00188   mol->set_scale(scale);
00189 
00190   Py_INCREF(Py_None);
00191   return Py_None;
00192 }
00193   
00194 // set_trans(molid)
00195 static PyObject *set_trans(PyObject *self, PyObject *args) {
00196   int i, molid;
00197   float c[3];
00198   PyObject *pylist;
00199   if (!PyArg_ParseTuple(args, (char *)"iO!:trans.set_trans", &molid, &PyList_Type, &pylist))
00200     return NULL;
00201 
00202   VMDApp *app = get_vmdapp();
00203   Molecule *mol = app->moleculeList->mol_from_id(molid);
00204   if (!mol) {
00205     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00206     return NULL;
00207   } 
00208   if (PyList_GET_SIZE(pylist) != 3) {
00209     PyErr_SetString(PyExc_ValueError, (char *)"List must be of length 3");
00210     return NULL;
00211   }
00212   
00213   for (i=0; i<3; i++) {
00214     c[i] = PyFloat_AsDouble(PyList_GET_ITEM(pylist, i));
00215     if (PyErr_Occurred())
00216       return NULL;
00217   }
00218   mol->set_glob_trans(c[0], c[1], c[2]); 
00219 
00220   Py_INCREF(Py_None);
00221   return Py_None;
00222 }
00223 
00224 // set_rotation(molid)
00225 static PyObject *set_rotation(PyObject *self, PyObject *args) {
00226   int i, molid;
00227   float c[16];
00228   PyObject *pylist;
00229   if (!PyArg_ParseTuple(args, (char *)"iO!:trans.set_rotation", &molid, &PyList_Type, &pylist))
00230     return NULL;
00231 
00232   VMDApp *app = get_vmdapp();
00233   Molecule *mol = app->moleculeList->mol_from_id(molid);
00234   if (!mol) {
00235     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00236     return NULL;
00237   } 
00238   if (PyList_GET_SIZE(pylist) != 16) {
00239     PyErr_SetString(PyExc_ValueError, (char *)"List must be of length 16");
00240     return NULL;
00241   }
00242   
00243   for (i=0; i<16; i++) {
00244     c[i] = PyFloat_AsDouble(PyList_GET_ITEM(pylist, i));
00245     if (PyErr_Occurred())
00246       return NULL;
00247   }
00248   mol->set_rot(c);
00249 
00250   Py_INCREF(Py_None);
00251   return Py_None;
00252 }
00253 
00254 // resetview(molid)
00255 // centers the view around the given molecule
00256 // Actually, this sets the top molecule, then does a resetview, then restores
00257 // the previous top molecule.
00258 // If the molid is invalid, raise a ValueError
00259 static PyObject *resetview(PyObject *self, PyObject *args) {
00260   int molid;
00261   if (!PyArg_ParseTuple(args, (char *)"i:trans.resetview", &molid))
00262     return NULL;
00263 
00264   VMDApp *app = get_vmdapp();
00265   MoleculeList *mlist = app->moleculeList;
00266   Molecule *mol = mlist->mol_from_id(molid);
00267   if (!mol) {
00268     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00269     return NULL;
00270   }
00271   Molecule *topmol = mlist->top();
00272   mlist->make_top(mol); 
00273   app->scene_resetview();
00274   mlist->make_top(topmol);
00275 
00276   Py_INCREF(Py_None);
00277   return Py_None;
00278 }
00279 
00280 // is_fixed(molid)
00281 static PyObject *is_fixed(PyObject *self, PyObject *args) {
00282   int molid;
00283   if (!PyArg_ParseTuple(args, (char *)"i:trans.is_fixed", &molid))
00284     return NULL;
00285 
00286   VMDApp *app = get_vmdapp();
00287   Molecule *mol = app->moleculeList->mol_from_id(molid);
00288   if (!mol) {
00289     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00290     return NULL;
00291   }
00292   return PyInt_FromLong(mol->fixed()); 
00293 }
00294 
00295 // fix(molid, bool)
00296 static PyObject *fix(PyObject *self, PyObject *args) {
00297   int molid;
00298   PyObject *boolobj;
00299   if (!PyArg_ParseTuple(args, (char *)"iO:trans.fix", &molid, &boolobj))
00300     return NULL;
00301 
00302   VMDApp *app = get_vmdapp();
00303   Molecule *mol = app->moleculeList->mol_from_id(molid);
00304   if (!mol) {
00305     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00306     return NULL;
00307   }
00308   app->molecule_fix(molid, PyObject_IsTrue(boolobj));
00309 
00310   Py_INCREF(Py_None);
00311   return Py_None;
00312 }
00313 
00314 // is_shown(molid)
00315 static PyObject *is_shown(PyObject *self, PyObject *args) {
00316   int molid;
00317   if (!PyArg_ParseTuple(args, (char *)"i:trans.is_shown", &molid))
00318     return NULL;
00319 
00320   VMDApp *app = get_vmdapp();
00321   Molecule *mol = app->moleculeList->mol_from_id(molid);
00322   if (!mol) {
00323     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00324     return NULL;
00325   }
00326   return PyInt_FromLong(mol->displayed()); 
00327 }
00328 
00329 // show(molid, bool) 
00330 static PyObject *show(PyObject *self, PyObject *args) {
00331   int molid;
00332   PyObject *boolobj;
00333   if (!PyArg_ParseTuple(args, (char *)"iO:trans.show", &molid, &boolobj))
00334     return NULL;
00335 
00336   VMDApp *app = get_vmdapp();
00337   Molecule *mol = app->moleculeList->mol_from_id(molid);
00338   if (!mol) {
00339     PyErr_SetString(PyExc_ValueError, (char *)"Invalid molecule id");
00340     return NULL;
00341   }
00342   app->molecule_display(molid, PyObject_IsTrue(boolobj));
00343 
00344   Py_INCREF(Py_None);  
00345   return Py_None;
00346 }
00347 
00348 static PyMethodDef TransMethods[] = {
00349   {(char *)"rotate", (vmdPyMethod)rotate, METH_VARARGS },
00350   {(char *)"translate", (vmdPyMethod)translate, METH_VARARGS},
00351   {(char *)"scale", (vmdPyMethod)scale, METH_VARARGS},
00352   {(char *)"resetview", (vmdPyMethod)resetview, METH_VARARGS},
00353   {(char *)"get_center", (vmdPyMethod)get_center, METH_VARARGS},
00354   {(char *)"get_scale", (vmdPyMethod)get_scale, METH_VARARGS},
00355   {(char *)"get_trans", (vmdPyMethod)get_trans, METH_VARARGS},
00356   {(char *)"get_rotation", (vmdPyMethod)get_rotation, METH_VARARGS},
00357   {(char *)"set_center", (vmdPyMethod)set_center, METH_VARARGS},
00358   {(char *)"set_scale", (vmdPyMethod)set_scale, METH_VARARGS},
00359   {(char *)"set_trans", (vmdPyMethod)set_trans, METH_VARARGS},
00360   {(char *)"set_rotation", (vmdPyMethod)set_rotation, METH_VARARGS},
00361   {(char *)"is_fixed", (vmdPyMethod)is_fixed, METH_VARARGS},
00362   {(char *)"fix", (vmdPyMethod)fix, METH_VARARGS},
00363   {(char *)"is_shown", (vmdPyMethod)is_shown, METH_VARARGS},
00364   {(char *)"show", (vmdPyMethod)show, METH_VARARGS},
00365   
00366   {NULL, NULL}
00367 };
00368 
00369 
00370 
00371 void inittrans() {
00372   (void) Py_InitModule((char *)"trans", TransMethods);
00373 }
00374 
00375 
00376  

Generated on Fri May 24 01:49:05 2013 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002