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

SpringTool.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 #include "SpringTool.h"
00010 #include "Matrix4.h"
00011 #include "utilities.h"
00012 #include "Molecule.h"
00013 #include "VMDApp.h"
00014 #include "PickList.h"
00015 #include "MoleculeList.h"
00016 
00017 SpringTool::SpringTool(int id, VMDApp *vmdapp, Displayable *disp) 
00018 : Tool(id, vmdapp, disp) {
00019   int i;
00020   for(i=0;i<3;i++) offset[i]=0;
00021   tugging=0;
00022   springscale = 1.0;
00023 }
00024 
00025 static int get_nearby_atom(VMDApp *app, const float *pos,
00026              int *molret, int *atomret) {
00027   int mol = -1;
00028   int atom = -1;
00029   int tag; 
00030   
00031   Pickable *p;
00032   if (app->display) {
00033     // pick a nearby atom
00034     p = app->pickList->pick_check(3, pos, tag, NULL, 0.2f);
00035 
00036     if (p) {
00037       Molecule *m = app->moleculeList->check_pickable(p);
00038       if (m) {
00039         mol = m->id();
00040         atom = tag;
00041       }
00042     }
00043   }
00044 
00045   if (atom == -1 || mol == -1) return 0;
00046   *molret = mol;
00047   *atomret = atom;
00048   return 1;
00049 }
00050 
00051 void SpringTool::do_event() {
00052 
00053   // always update!
00054   tool_location_update();
00055 
00056   if (istugging()) {  // Tugging is enabled...
00057     if (!tugging || !is_targeted()) { // but we're not tugging now
00058       if(!target(TARGET_TUG, tugged_pos, 0)) {
00059         // Didn't pick anything, so return
00060         tugging = 0;
00061         return;
00062       }
00063       tugging = 1;
00064       // We're starting the force field, so set the offset
00065       vec_sub(offset, Tool::position(), tugged_pos);
00066       start_tug();
00067     }
00068     target(TARGET_TUG, tugged_pos, 1);
00069     // Apply the force field...
00070     float offset_tugged_pos[3]; // offset+tugged_pos
00071     vec_add(offset_tugged_pos,offset,tugged_pos);
00072     set_tug_constraint(offset_tugged_pos);
00073     
00074     float diff[3];
00075     vec_sub(diff, Tool::position(), offset_tugged_pos);
00076     // diff now is in my units, but we should do it in mol units
00077     vec_scale(diff,dtool->scale/getTargetScale(),diff);
00078     // scale by the force scaling spring constant
00079     vec_scale(diff,forcescale,diff); 
00080     do_tug(diff);
00081   }
00082   else if (tugging) { // Tugging has been disabled
00083     int mol1,mol2;
00084     int atom1,atom2;
00085     int ret1 = get_nearby_atom(app,position(), &mol1,&atom1);
00086     int ret2 = get_targeted_atom(&mol2,&atom2);
00087 
00088     if(ret1 && ret2) { // got two atoms successfully
00089       if(mol1 == mol2) { // on the same molecule
00090         if(atom1 != atom2) { // two *different* atoms
00091     int atombuf[2];
00092     int molbuf[2];
00093     atombuf[0] = atom1; atombuf[1] = atom2;
00094     molbuf[0] = mol1; molbuf[1] = mol2;
00095     app->label_add("Springs", 2, molbuf, atombuf, NULL, 0.0f, 1);
00096   }     
00097       }
00098     }
00099 
00100     // now let go
00101     let_go();
00102     tugging = 0;
00103     forceoff();
00104     offset[0]=offset[1]=offset[2]=0;
00105   } 
00106 }
00107 
00108 void SpringTool::set_tug_constraint(float *newpos) {
00109   setconstraint(50, newpos);
00110   sendforce();
00111 }
00112 
00113 void SpringTool::do_tug(float *force) {
00114   tug(force);
00115 }
00116 

Generated on Tue May 22 01:48:12 2012 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002