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

GeometryList.C

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr                                                                       
00003  *cr            (C) Copyright 1995-2008 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: GeometryList.C,v $
00013  *      $Author: johns $        $Locker:  $                $State: Exp $
00014  *      $Revision: 1.61 $      $Date: 2008/03/27 19:36:39 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *
00019  * This maintains a set of lists of geometry monitors, and draws them to
00020  * the scene.  This is a Displayable which keeps the graphical representations
00021  * of the geometry monitors; this is also a Pickable, and creates and controls
00022  * the PickMode objects which create new geometry monitors via the pointers.
00023  * This object keeps a set of ResizeArray, each of which is a 'category' for
00024  * geometry monitors (i.e Atoms, Bonds, etc.) which contain GeometryMol objects. 
00025  *
00026  ***************************************************************************/
00027 
00028 #include "GeometryList.h"
00029 #include "GeometryAtom.h"
00030 #include "GeometryBond.h"
00031 #include "GeometryAngle.h"
00032 #include "GeometryDihedral.h"
00033 #include "GeometrySpring.h"
00034 #include "DisplayDevice.h"
00035 #include "DispCmds.h"
00036 #include "MoleculeList.h"
00037 #include "utilities.h"
00038 #include "VMDApp.h"
00039 #include "Scene.h"
00040 #include "Inform.h"
00041 
00042 // default colors to use
00043 #define ATOMGEOMCOL  REGGREEN
00044 #define BONDGEOMCOL     REGWHITE
00045 #define ANGLEGEOMCOL  REGYELLOW
00046 #define DIHEGEOMCOL  REGCYAN
00047 #define TUGGEOMCOL  REGWHITE
00048 #define SPRINGGEOMCOL  REGORANGE
00049 
00050 
00052 GeometryList::GeometryList(VMDApp *vmdapp, Displayable *disp)
00053   : Displayable(disp) {
00054 
00055   // indicate we don't yet have a color object to use
00056   colorCat = (-1);
00057 
00058   // save the list of molecules to use for data
00059   app = vmdapp;
00060 
00061   // default size of text labels is 1.0 (no scaling)
00062   labelsize = 1.0f;
00063 
00064   // create default lists for atom, bond, angle, and dihedral measurements
00065   add_geom_list("Atoms", ATOMGEOMCOL);
00066   add_geom_list("Bonds", BONDGEOMCOL);
00067   add_geom_list("Angles", ANGLEGEOMCOL);
00068   add_geom_list("Dihedrals", DIHEGEOMCOL);
00069   add_geom_list("Springs", SPRINGGEOMCOL);
00070 
00071   colorCat = scene->add_color_category("Labels");
00072   for (int i=0; i<num_lists(); i++) {
00073     scene->add_color_item(colorCat, geomLists.name(i), 
00074         geomLists.data(i)->defaultColor);
00075   }
00076 
00077   // Displayable characteristics
00078   rot_off();
00079   scale_off();
00080   glob_trans_off();
00081   cent_trans_off();
00082 }
00083 
00084 
00086 GeometryList::~GeometryList(void) {
00087 
00088   // for all the lists, delete all geometry monitors
00089   for(int i=(num_lists() - 1); i >= 0; i--)
00090     del_geom_list(i);
00091 }
00092 
00093 
00095 
00096 void GeometryList::do_color_changed(int clr) {
00097   if (clr == colorCat) {
00098     for (int i=0; i<num_lists(); i++) {
00099       GeomListPtr glist = geom_list(i);
00100       GeomListStruct *s = geomLists.data(i);
00101       int ind = scene->category_item_index(colorCat, geom_list_name(i));
00102       int c = scene->category_item_value(colorCat, ind);
00103       s->curColor = c;
00104       for (int j=0; j<glist->num(); j++)
00105         (*glist)[j]->set_color(c);
00106     }
00107   }
00108 }
00109 
00111 
00112 // add a new category: specify the name, and default color.  Return index of
00113 // new list.
00114 int GeometryList::add_geom_list(const char *nm, int clr) {
00115 
00116   // make sure we do not already have a category with this name
00117   int oldlist = geom_list_index(nm);
00118   if(oldlist >= 0)
00119     return oldlist;
00120     
00121   // create a new struct
00122   GeomListStruct *newlist = new GeomListStruct;
00123   newlist->geomList = new ResizeArray<GeometryMol *>(8);
00124   newlist->defaultColor = clr;
00125   newlist->curColor = clr;
00126   
00127   // add the new category to the big list
00128   return geomLists.add_name(nm, newlist);
00129 }
00130 
00131 
00132 // delete the Nth category.  Return success.
00133 int GeometryList::del_geom_list(int n) {
00134   GeomListStruct *oldlist = NULL;
00135 
00136   // make sure we do have a category with this name
00137   if(n >= 0 && n < num_lists()) {
00138     // get data for Nth list
00139     oldlist = geomLists.data(n);
00140     GeomListPtr glist = oldlist->geomList;
00141     
00142     // go through the list and delete all current GeometryMol objects
00143     for(int i=(glist->num() - 1); i >= 0; i--) {
00144       delete (*glist)[i];
00145     }
00146 
00147     // delete the old list storage and structure
00148     delete glist;
00149     delete oldlist;
00150     geomLists.set_data(n, (GeomListStruct *) NULL);
00151   }
00152 
00153   // return whether we were successful
00154   return (oldlist != NULL);
00155 }
00156 
00157 int GeometryList::add_geometry(const char *geomcatname, const int *molids,
00158     const int *atomids, const int *cells, float k, int toggle) {
00159 
00160   // check that geometry category name is valid
00161   int geomcat = geom_list_index(geomcatname);
00162   GeometryMol *g = NULL;
00163   MoleculeList *mlist = app->moleculeList;
00164   if (geomcat == geom_list_index("Atoms")) 
00165     g = new GeometryAtom(*molids, *atomids, cells, mlist, app->commandQueue, this);
00166   else if (geomcat == geom_list_index("Bonds"))
00167     g = new GeometryBond((int *)molids, (int *)atomids, cells, mlist, app->commandQueue, this);
00168   else if (geomcat == geom_list_index("Angles"))
00169     g = new GeometryAngle((int *)molids, (int *)atomids, cells, mlist, app->commandQueue, this);
00170   else if (geomcat == geom_list_index("Dihedrals"))
00171     g = new GeometryDihedral((int *)molids, (int *)atomids, cells, mlist, app->commandQueue, this);
00172   else if (geomcat == geom_list_index("Springs"))
00173     g = new GeometrySpring((int *)molids, (int *)atomids, mlist, app->commandQueue, k, this);
00174   else {
00175     msgErr << "Unknown geometry category '" << geomcatname << "'." << sendmsg;
00176     return -1;
00177   }
00178   if(g && g->ok()) {
00179     GeomListPtr glist = geom_list(geomcat);
00180 
00181     // if there is already an identical label in the list,
00182     // do not add this one, instead toggle the displayed
00183     // status of the old one and return the index.
00184     for(int i=(glist->num() - 1); i >= 0; i--) {
00185       GeometryMol *g2 = (*glist)[i];
00186       if(!strcmp(g2->unique_name(), g->unique_name())) {
00187         // name matches
00188         if (toggle) {
00189           if (g2->displayed())
00190             g2->off();
00191           else
00192             g2->on();
00193         }
00194       
00195         delete g;
00196         return i;
00197       }
00198     }
00199 
00200     // spam the console
00201     msgInfo << "Added new " << geomcatname << " label " << g->name();
00202     if (g->has_value())
00203       msgInfo << " = " << g->calculate();
00204     msgInfo << sendmsg;
00205 
00206     // add the geometry object
00207     glist->append(g);
00208     
00209     // calculate the value for the first time
00210     g->calculate();
00211 
00212     // set the color, which also causes the display list to be generated
00213     g->set_color(geomLists.data(geomcat)->curColor);
00214     g->set_text_size(labelsize);
00215 
00216     // set the pick variables
00217     g->set_pick();
00218 
00219     // indicate we were successful; return index of this object
00220     return glist->num() - 1;
00221   }
00222   
00223   // if here, something did not work
00224   return -1;
00225 }
00226 
00227 
00228 // del a new geometry object from the list with the given index. Return success.
00229 // if n < 0, delete all markers
00230 int GeometryList::del_geometry(int glindex, int n) {
00231 
00232   // make sure the given GeometryMol object is OK
00233   if(glindex >= 0 && glindex < num_lists()) {
00234 
00235     // get the list of geometry objects for the given index
00236     GeomListPtr glist = geom_list(glindex);
00237 
00238     // make sure the geometry index is ok
00239     if(n >= 0 && n < glist->num())  {
00240       // delete and remove the geometry object
00241       delete (*glist)[n];
00242       glist->remove(n);
00243       
00244       // indicate we were successful
00245       return TRUE;
00246     } else if(n < 0) {
00247       // delete and remove all geometry objects
00248       for(int j=(glist->num() - 1); j >= 0; j--) {
00249         delete (*glist)[j];
00250         glist->remove(j);
00251       }
00252       
00253       // indicate we were successful
00254       return TRUE;
00255     }
00256   }
00257   
00258   // if here, something did not work
00259   return FALSE;
00260 }
00261 
00262 // toggle whether to show or hide a geometry monitor.  If the monitor 
00263 // specified is < 0, does so for all monitors in the given category
00264 int GeometryList::show_geometry(int glindex, int n, int s) {
00265 
00266   // make sure the given GeometryMol object is OK
00267   if(glindex >= 0 && glindex < num_lists()) {
00268     
00269     // get the list of geometry objects for the given index
00270     GeomListPtr glist = geom_list(glindex);
00271 
00272     // make sure the geometry index is ok
00273     if(n >= 0 && n < glist->num())  {
00274       // hide or show the specified object
00275       if (s)
00276         (*glist)[n] -> on();
00277       else 
00278         (*glist)[n] -> off();
00279 
00280       
00281       // indicate we were successful
00282       return TRUE;
00283     } else if(n < 0) {
00284       // delete and remove all geometry objects
00285       for(int j=(glist->num() - 1); j >= 0; j--) {
00286         if (s)
00287           (*glist)[j] -> on();
00288         else
00289           (*glist)[j] -> off();
00290       }
00291      
00292       
00293       // indicate we were successful
00294       return TRUE;
00295     }
00296   }
00297   
00298   // if here, something did not work
00299   return FALSE;
00300 }
00301 
00302 int GeometryList::setTextSize(float newsize) {
00303   if (newsize <= 0)
00304     return FALSE;
00305 
00306   if (newsize == labelsize) return TRUE;
00307 
00308   labelsize = newsize;
00309   for(int i=(num_lists() - 1); i >= 0; i--) {
00310     GeomListPtr glist = geom_list(i);
00311     for(int j=(glist->num() - 1); j >= 0; j--) {
00312       GeometryMol *g = (*glist)[j];
00313       g->set_text_size(newsize);
00314     }
00315   }
00316 
00317   return TRUE;
00318 }
00319 
00320 const float *GeometryList::getTextOffset(const char *nm, int n) {
00321   GeomListPtr glist = geom_list(nm);
00322   if (!glist) return NULL;
00323   if (n < 0 || n >= glist->num()) return NULL;
00324   GeometryMol *g = (*glist)[n];
00325   return g->text_offset();
00326 }
00327 
00328 int GeometryList::setTextOffset(const char *nm, int n, const float delta[2]) {
00329 
00330   GeomListPtr glist = geom_list(nm);
00331   if (!glist) return FALSE;
00332   if (n < 0 || n >= glist->num()) return FALSE;
00333   GeometryMol *g = (*glist)[n];
00334   g->set_text_offset(delta);
00335   return TRUE;
00336 }
00337 
00338 const char *GeometryList::getTextFormat(const char *nm, int n) {
00339   // XXX  get/set Text Offset/Format duplicate their first four lines...
00340   GeomListPtr glist = geom_list(nm);
00341   if (!glist) return NULL;
00342   if (n < 0 || n >= glist->num()) return NULL;
00343   GeometryMol *g = (*glist)[n];
00344   return g->text_format();
00345 }
00346 
00347 int GeometryList::setTextFormat(const char *nm, int n, const char *format) {
00348   GeomListPtr glist = geom_list(nm);
00349   if (!glist) return FALSE;
00350   if (n < 0 || n >= glist->num()) return FALSE;
00351   GeometryMol *g = (*glist)[n];
00352   g->set_text_format(format);
00353   return TRUE;
00354 }
00355 
00357 
00358 // prepare for drawing ... do any updates needed right before draw.
00359 // For now, this always recreates the display list
00360 void GeometryList::prepare() {
00361 
00362   // go through all the geometry objects, recalculate, and find out if
00363   // something is no longer 'ok'.  If not, delete it.
00364   for(int i=(num_lists() - 1); i >= 0; i--) {
00365     GeomListPtr glist = geom_list(i);
00366     for(int j=(glist->num() - 1); j >= 0; j--) {
00367       GeometryMol *g = (*glist)[j];
00368       if(!g->ok()) {
00369         del_geometry(i, j);
00370       }
00371     }
00372   }
00373 }
00374 

Generated on Mon Oct 6 01:26:13 2008 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002