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

P_UIVR.C

Go to the documentation of this file.
00001 
00002 
00003 /***************************************************************************
00004  *cr                                                                       
00005  *cr            (C) Copyright 1995-2019 The Board of Trustees of the           
00006  *cr                        University of Illinois                       
00007  *cr                         All Rights Reserved                        
00008  *cr                                                                   
00009  ***************************************************************************/
00010 
00011 /***************************************************************************
00012  * RCS INFORMATION:
00013  *
00014  *      $RCSfile: P_UIVR.C,v $
00015  *      $Author: johns $        $Locker:  $             $State: Exp $
00016  *      $Revision: 1.138 $      $Date: 2019/01/17 21:21:01 $
00017  *
00018  ***************************************************************************
00019  * DESCRIPTION:
00020  * This is Paul's new Tracker code -- pgrayson@ks.uiuc.edu
00021  *
00022  ***************************************************************************/
00023 
00024 #ifdef VMDVRPN
00025 #include "quat.h"
00026 #endif
00027 
00028 #include "TextEvent.h"
00029 #include "P_UIVR.h"
00030 #include "Molecule.h"
00031 #include "MoleculeList.h"
00032 #include "P_CmdTool.h"
00033 #include "VMDApp.h"
00034 #include "CommandQueue.h"
00035 #include "Scene.h"
00036 #include "PickList.h"
00037 #include "P_Tool.h"
00038 #include "P_RotateTool.h"
00039 #include "P_JoystickTool.h"
00040 #include "P_GrabTool.h"
00041 #include "P_PrintTool.h"
00042 #include "P_TugTool.h"
00043 #include "SpringTool.h"
00044 #include "P_PinchTool.h"
00045 #include "MaterialList.h"
00046 #include "P_SensorConfig.h"
00047 #include "P_Tracker.h"
00048 #include "P_Feedback.h"
00049 #include "P_Buttons.h"
00050 #include "CmdLabel.h"
00051 #include "Inform.h"
00052 
00053 #ifdef VMDVRPN
00054 #include "vrpn_Button.h"
00055 #include "vrpn_Tracker.h"
00056 #include "vrpn_ForceDevice.h"
00057 #endif
00058 
00059 #include "P_Tracker.h"
00060 #include "P_Buttons.h"
00061 #include "P_Feedback.h"
00062   
00063 #ifdef VMDCAVE
00064 #include "P_CaveButtons.h"
00065 #include "P_CaveTracker.h"
00066 #endif
00067 
00068 #ifdef VMDFREEVR
00069 #include "P_FreeVRButtons.h"
00070 #include "P_FreeVRTracker.h"
00071 #endif
00072 
00073 #ifdef VMDVRPN
00074 #include "P_VRPNTracker.h"
00075 #include "P_VRPNButtons.h"
00076 #include "P_VRPNFeedback.h"
00077 #endif
00078 
00079 #ifdef WINGMAN
00080 #include "wingforce.h"
00081 #include "P_JoystickButtons.h"
00082 #endif
00083 
00084 #include "MobileButtons.h"
00085 #include "MobileTracker.h"
00086 
00087 #include "SpaceballButtons.h"
00088 #include "SpaceballTracker.h"
00089 
00090 
00091 UIVR::~UIVR() {
00092   int i;
00093   for (i=0;i<tools.num();i++)
00094     delete tools[i];
00095   for (i=0; i<trackerList.num(); i++) 
00096     delete trackerList.data(i);
00097   for (i=0; i<feedbackList.num(); i++) 
00098     delete feedbackList.data(i);
00099   for (i=0; i<buttonList.num(); i++) 
00100     delete buttonList.data(i);
00101 
00102 }
00103 
00104 UIVR::UIVR(VMDApp *vmdapp) : UIObject(vmdapp) {
00105   command_wanted(Command::TOOL_OFFSET);
00106   command_wanted(Command::TOOL_REP);
00107   command_wanted(Command::TOOL_ADD_DEVICE);
00108   command_wanted(Command::TOOL_DELETE_DEVICE);
00109   command_wanted(Command::TOOL_CALLBACK);
00110 
00111   tool_serialno = 0;
00112 
00113   tool_types.add_name("grab",     0); // grabbing/rotating molecule
00114   tool_types.add_name("rotate",   1); // rotating, constrained to sphere
00115   tool_types.add_name("joystick", 2); // relative positioning 
00116   tool_types.add_name("tug",      3); // apply IMD forces to molecule
00117   tool_types.add_name("pinch",    4); // apply IMD forces, but only tracker dir
00118   tool_types.add_name("spring",   5); // attach IMD springs
00119   tool_types.add_name("print",    6); // print tracker data
00120 
00121   update_device_lists();
00122   reset();
00123 }
00124 
00125 Tool *UIVR::create_tool(const char *type) {
00126   Displayable *parent = &(app->scene->root);
00127   Tool *newtool = NULL;
00128   if(!strupncmp(type, "grab", 10)) 
00129     newtool = new GrabTool(tool_serialno++, app, parent);
00130   else if(!strupncmp(type, "joystick", 10)) 
00131     newtool = new JoystickTool(tool_serialno++, app, parent);
00132   else if(!strupncmp(type, "tug", 10)) 
00133     newtool = new TugTool(tool_serialno++,app, parent);
00134   else if(!strupncmp(type, "pinch", 10)) 
00135     newtool = new PinchTool(tool_serialno++,app, parent);
00136   else if(!strupncmp(type, "spring", 10)) 
00137     newtool = new SpringTool(tool_serialno++,app, parent);
00138   else if(!strupncmp(type, "print", 10)) 
00139     newtool = new PrintTool(tool_serialno++, app, parent);
00140   
00141 #ifdef VMDVRPN
00142   // XXX why is only this tool protected by the ifdef??
00143   else if(!strupncmp(type, "rotate", 10)) 
00144     newtool = new RotateTool(tool_serialno++,app, parent);
00145 #endif
00146   else {
00147     msgErr << "Unrecognized tool type " << type << sendmsg;
00148     msgErr << "possiblities are:";
00149     for(int i=0;i<num_tool_types();i++) msgErr << " " << tool_types.name(i);
00150     msgErr << sendmsg;
00151     return NULL;
00152   }
00153   newtool->On();
00154   newtool->grabs = 0;
00155   return newtool;
00156 }
00157 
00158 int UIVR::add_tool(const char *type) {
00159   Tool *newtool = create_tool(type);
00160   if (!newtool) return -1;
00161   tools.append(newtool);
00162   return tools.num()-1;
00163 }
00164 
00165 int UIVR::add_tool_with_USL(const char *type, int argc, const char **USL) {
00166   int num = add_tool(type);
00167   if(num==-1) return FALSE;
00168   Tool *tool = tools[num];
00169   for (int i=0; i<argc; i++) 
00170     add_device_to_tool(USL[i], tool);
00171   return TRUE;
00172 }
00173 
00174 int UIVR::change_type(int toolnum, const char *type) {
00175   if (toolnum < 0 || toolnum >= tools.num()) return FALSE;
00176   Tool *newtool = create_tool(type);
00177   if (!newtool) return FALSE;
00178   Tool *oldtool = tools[toolnum];
00179   newtool->steal_sensor(oldtool);
00180   delete oldtool;
00181   tools[toolnum] = newtool;
00182   return TRUE;
00183 }
00184 
00185 int UIVR::remove_tool(int i) {
00186   if(i<0 || i >= tools.num()) return FALSE;
00187   Tool *deadtool = tools[i];
00188   delete deadtool; 
00189   tools.remove(i);
00190   return TRUE;
00191 }
00192 
00193 int UIVR::check_event() {
00194   // XXX this is not so good - tug
00195   // should probably be in the Tool
00196   // class...
00197 
00198   int i;
00199   for(i=0;i<tools.num();i++) {
00200     /* prune the dead tools */
00201     if(!tools[i]->alive()) {
00202       // msgErr << "UIVR: Dead tool found." << sendmsg;
00203       remove_tool(i);
00204       i--;
00205       continue;
00206     }
00207 
00208     if(tools[i]->orientation()==NULL) 
00209       continue;
00210 
00211     /* possibly grab on to some stuff */
00212     if(tools[i]->isgrabbing()) {
00213       tools[i]->dograb();
00214       tools[i]->grabs = 1;
00215     } else {
00216       if(tools[i]->grabs) tools[i]->ungrab();  //TJF Changed
00217       tools[i]->grabs = 0; // TJF Changed
00218     }
00219   }
00220 
00221 
00222   return TRUE;
00223 }
00224 
00225 Tool *UIVR::gettool(int i) {
00226   if(i<0) return NULL;
00227   if(i>=tools.num()) return NULL;
00228   return tools[i];
00229 }
00230 
00231 int UIVR::set_position_scale(int toolnum, float newval) {
00232   if (newval >= 0) {
00233     Tool *tool = gettool(toolnum);
00234     if (tool) {
00235       tool->setscale(newval);
00236       return TRUE;
00237     }
00238   }
00239   return FALSE;
00240 }
00241 int UIVR::set_force_scale(int toolnum, float newval) {
00242   if (newval >= 0) {
00243     Tool *tool = gettool(toolnum);
00244     if (tool) {
00245       tool->setforcescale(newval);
00246       return TRUE;
00247     }
00248   }
00249   return FALSE;
00250 }
00251 int UIVR::set_spring_scale(int toolnum, float newval) {
00252   if (newval >= 0) {
00253     Tool *tool = gettool(toolnum);
00254     if (tool) {
00255       tool->setspringscale(newval);
00256       return TRUE;
00257     }
00258   }
00259   return FALSE;
00260 }
00261 
00262 int UIVR::act_on_command(int type, Command *c) {
00263   switch(type) {
00264   default:  return FALSE; 
00265     case Command::TOOL_OFFSET:
00266       {
00267       Tool *tool = gettool(((CmdToolScaleSpring *)c)->num);
00268       if (!tool) return FALSE;
00269       tool->setoffset(((CmdToolOffset *)c)->offset);
00270       }
00271       break;
00272     case Command::TOOL_ADD_DEVICE:
00273       {
00274       CmdToolAddDevice *cmd = (CmdToolAddDevice *)c; 
00275       Tool *tool = gettool(cmd->num);
00276       return add_device_to_tool(cmd->name, tool);
00277       }
00278       break;
00279     case Command::TOOL_DELETE_DEVICE:
00280       {
00281       CmdToolAddDevice *cmd = (CmdToolAddDevice *)c; 
00282       Tool *tool = gettool(cmd->num);
00283       if (!tool) return FALSE;
00284       if (!cmd->name) return FALSE;
00285       tool->remove_device(cmd->name);
00286       }
00287       break;
00288     case Command::TOOL_CALLBACK:
00289       {
00290       CmdToolCallback *cmd = (CmdToolCallback *)c;
00291       set_callbacks(cmd->on);
00292       break;
00293       }
00294     case Command::TOOL_REP:
00295       {
00296       CmdToolRep *cmd = (CmdToolRep *)c;
00297       Tool *tool = gettool(cmd->toolnum);
00298       if (!tool) return FALSE;
00299       if (cmd->molid < 0 || cmd->repnum < 0) {
00300         tool->clear_rep();
00301       } else {
00302         tool->assign_rep(cmd->molid, cmd->repnum);
00303       }
00304       }
00305       break;
00306   }
00307   return TRUE;
00308 }
00309 
00310 
00311 int UIVR::add_device_to_tool(const char *device, Tool *tool) {
00312 
00313   if (!tool) return FALSE;
00314   if (!device) return FALSE;
00315 
00316   // Get configuration information for this device from SensorConfig.
00317   // Cannot add device without config information.
00318   SensorConfig config(device);
00319   if (!config.getUSL()[0]) return FALSE;
00320 
00321   // search for the device in the three device lists; we chould
00322   // deprecate this command and make separate commands for adding
00323   // trackers, feedback, and buttons.  Another possibility is
00324   // to define a superclass for all three, but then we would have
00325   // to downcast at some point.
00326   const char *devtype = config.gettype();
00327   if (trackerList.typecode(devtype) >= 0) {
00328     tool->add_tracker(trackerList.data(devtype)->clone(), &config);
00329   } else if (feedbackList.typecode(devtype) >= 0) {
00330     tool->add_feedback(feedbackList.data(devtype)->clone(), &config);
00331   } else if (buttonList.typecode(devtype) >= 0) {
00332     tool->add_buttons(buttonList.data(devtype)->clone(), &config);
00333   } else {
00334     msgErr << "Device '" << device << "' of type '" << devtype << "' not available." << sendmsg;
00335   }
00336   return TRUE;
00337 }
00338   
00339 int UIVR::set_tracker(int toolnum, const char *device) {
00340   Tool *tool = gettool(toolnum);
00341   if (!tool) return FALSE;
00342   if (device == NULL) {
00343     tool->add_tracker(NULL, NULL);
00344     return TRUE;
00345   }
00346   return add_device_to_tool(device, tool);
00347 }
00348 int UIVR::set_feedback(int toolnum, const char *device) {
00349   Tool *tool = gettool(toolnum);
00350   if (!tool) return FALSE;
00351   if (device == NULL) {
00352     tool->add_feedback(NULL, NULL);
00353     return TRUE;
00354   }
00355   return add_device_to_tool(device, tool);
00356 }
00357 int UIVR::set_buttons(int toolnum, const char *device) {
00358   Tool *tool = gettool(toolnum);
00359   if (!tool) return FALSE;
00360   if (device == NULL) {
00361     tool->add_buttons(NULL, NULL);
00362     return TRUE;
00363   }
00364   return add_device_to_tool(device, tool);
00365 }
00366 
00367 ResizeArray<JString *> *UIVR::get_device_names() {
00368   return SensorConfig::getnames();
00369 }
00370 
00371 template<class T>
00372 ResizeArray<JString *> *generic_get_names(const T &devList) {
00373 
00374   ResizeArray<JString *> *names = SensorConfig::getnames();
00375   ResizeArray<JString *> *devnames = new ResizeArray<JString *>;
00376   for (int i=0; i<names->num(); i++) {
00377     JString *jstr = (*names)[i];
00378     SensorConfig config(*jstr);
00379     const char *type = config.gettype();
00380     if (devList.typecode(type) >= 0)
00381       devnames->append(jstr); 
00382     else
00383       delete jstr;
00384   } 
00385   delete names;
00386   return devnames;
00387 }
00388   
00389 ResizeArray<JString *> *UIVR::get_tracker_names() {
00390   return generic_get_names(trackerList);
00391 }
00392 ResizeArray<JString *> *UIVR::get_feedback_names() {
00393   return generic_get_names(feedbackList);
00394 }
00395 ResizeArray<JString *> *UIVR::get_button_names() {
00396   return generic_get_names(buttonList);
00397 }
00398 
00399 void UIVR::update_device_lists() {
00400   VMDTracker *tracker = NULL;
00401   Buttons *buttons = NULL;
00402 
00403   tracker = new SpaceballTracker(app);
00404   trackerList.add_name(tracker->device_name(), tracker);
00405   buttons = new SpaceballButtons(app);
00406   buttonList.add_name(buttons->device_name(), buttons);
00407 
00408   tracker = new MobileTracker(app);
00409   trackerList.add_name(tracker->device_name(), tracker);
00410   buttons = new MobileButtons(app);
00411   buttonList.add_name(buttons->device_name(), buttons);
00412 
00413 #ifdef VMDVRPN
00414   Feedback *feedback = NULL;
00415   tracker = new VRPNTracker;
00416   trackerList.add_name(tracker->device_name(), tracker);
00417   feedback = new VRPNFeedback;
00418   feedbackList.add_name(feedback->device_name(), feedback);
00419   buttons = new VRPNButtons;
00420   buttonList.add_name(buttons->device_name(), buttons);
00421 #endif
00422 
00423 #ifdef VMDCAVE
00424   tracker = new CaveTracker;
00425   trackerList.add_name(tracker->device_name(), tracker);
00426   buttons = new CaveButtons;
00427   buttonList.add_name(buttons->device_name(), buttons);
00428 #endif
00429 
00430 #ifdef VMDFREEVR
00431   tracker = new FreeVRTracker(app);
00432   trackerList.add_name(tracker->device_name(), tracker);
00433   buttons = new FreeVRButtons(app);
00434   buttonList.add_name(buttons->device_name(), buttons);
00435 #endif
00436 
00437 #ifdef WINGMAN
00438   buttons = new JoystickButtons;
00439   buttonList.add_name(buttons->device_name(), buttons);
00440 #endif
00441 }

Generated on Fri Nov 8 02:45:20 2024 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002