00001
00002
00003
00004
00005
00006
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 offset[0]=offset[1]=offset[2]=0;
00020 tugging=0;
00021 springscale = 1.0;
00022 }
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
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
00052 void SpringTool::do_event() {
00053
00054 tool_location_update();
00055
00056 if (istugging()) {
00057 if (!tugging || !is_targeted()) {
00058 if (!target(TARGET_TUG, tugged_pos, 0)) {
00059
00060 tugging = 0;
00061 return;
00062 }
00063
00064 tugging = 1;
00065
00066 vec_sub(offset, Tool::position(), tugged_pos);
00067 start_tug();
00068 }
00069 target(TARGET_TUG, tugged_pos, 1);
00070
00071 float offset_tugged_pos[3];
00072 vec_add(offset_tugged_pos,offset,tugged_pos);
00073 set_tug_constraint(offset_tugged_pos);
00074
00075 float diff[3];
00076 vec_sub(diff, Tool::position(), offset_tugged_pos);
00077
00078 vec_scale(diff, dtool->scale/getTargetScale(), diff);
00079
00080 vec_scale(diff, forcescale, diff);
00081 do_tug(diff);
00082 } else if (tugging) {
00083 int mol1=-1, mol2=-1;
00084 int atom1=-1, atom2=-1;
00085 int ret1 = get_nearby_atom(app, position(), &mol1, &atom1);
00086 int ret2 = get_targeted_atom(&mol2, &atom2);
00087
00088 if (ret1 && ret2) {
00089 if (mol1 == mol2) {
00090 if (atom1 != atom2) {
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
00101 let_go();
00102 tugging = 0;
00103 forceoff();
00104 offset[0]=offset[1]=offset[2]=0;
00105 }
00106 }
00107
00108
00109 void SpringTool::set_tug_constraint(float *newpos) {
00110 setconstraint(50, newpos);
00111 sendforce();
00112 }
00113
00114
00115 void SpringTool::do_tug(float *force) {
00116 tug(force);
00117 }
00118