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

panda/src/vrpn/vrpnTracker.cxx

Go to the documentation of this file.
00001 // Filename: vrpnTracker.cxx
00002 // Created by:  drose (25Jan01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "vrpnTracker.h"
00020 #include "vrpnTrackerDevice.h"
00021 #include "vrpnClient.h"
00022 #include "config_vrpn.h"
00023 
00024 #include <indent.h>
00025 
00026 #include <algorithm>
00027 
00028 ////////////////////////////////////////////////////////////////////
00029 //     Function: VrpnTracker::Constructor
00030 //       Access: Public
00031 //  Description:
00032 ////////////////////////////////////////////////////////////////////
00033 VrpnTracker::
00034 VrpnTracker(const string &tracker_name, vrpn_Connection *connection) :
00035   _tracker_name(tracker_name)
00036 {
00037   _tracker = new vrpn_Tracker_Remote(_tracker_name.c_str(), connection);
00038 
00039   _tracker->register_change_handler((void*)this, &vrpn_position_callback);
00040   _tracker->register_change_handler((void*)this, &vrpn_velocity_callback);
00041   _tracker->register_change_handler((void*)this, &vrpn_acceleration_callback);
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: VrpnTracker::Destructor
00046 //       Access: Public
00047 //  Description:
00048 ////////////////////////////////////////////////////////////////////
00049 VrpnTracker::
00050 ~VrpnTracker() {
00051   delete _tracker;
00052 }
00053 
00054 ////////////////////////////////////////////////////////////////////
00055 //     Function: VrpnTracker::mark
00056 //       Access: Public
00057 //  Description: Adds the indicated VrpnTrackerDevice to the list of
00058 //               devices that are sharing this VrpnTracker.
00059 ////////////////////////////////////////////////////////////////////
00060 void VrpnTracker::
00061 mark(VrpnTrackerDevice *device) {
00062   if (vrpn_cat.is_debug()) {
00063     vrpn_cat.debug() << *this << " marking " << *device << "\n";
00064   }
00065   _devices.push_back(device);
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: VrpnTracker::unmark
00070 //       Access: Public
00071 //  Description: Removes the indicated VrpnTrackerDevice from the list
00072 //               of devices that are sharing this VrpnTracker.
00073 ////////////////////////////////////////////////////////////////////
00074 void VrpnTracker::
00075 unmark(VrpnTrackerDevice *device) {
00076   if (vrpn_cat.is_debug()) {
00077     vrpn_cat.debug() << *this << " unmarking " << *device << "\n";
00078   }
00079 
00080   Devices::iterator di =
00081     find(_devices.begin(), _devices.end(), device);
00082 
00083   if (di != _devices.end()) {
00084     _devices.erase(di);
00085   }
00086 }
00087 
00088 ////////////////////////////////////////////////////////////////////
00089 //     Function: VrpnTracker::output
00090 //       Access: Public
00091 //  Description:
00092 ////////////////////////////////////////////////////////////////////
00093 void VrpnTracker::
00094 output(ostream &out) const {
00095   out << _tracker_name;
00096 }
00097 
00098 ////////////////////////////////////////////////////////////////////
00099 //     Function: VrpnTracker::write
00100 //       Access: Public
00101 //  Description:
00102 ////////////////////////////////////////////////////////////////////
00103 void VrpnTracker::
00104 write(ostream &out, int indent_level) const {
00105   indent(out, indent_level)
00106     << get_tracker_name() << " ("
00107     << _devices.size() << " devices)\n";
00108 }
00109 
00110 ////////////////////////////////////////////////////////////////////
00111 //     Function: VrpnTracker::vrpn_position_callback
00112 //       Access: Private, Static
00113 //  Description: Receives the tracker positional data from the VRPN
00114 //               code and sends it to any interested
00115 //               VrpnTrackerDevices.
00116 ////////////////////////////////////////////////////////////////////
00117 void VrpnTracker::
00118 vrpn_position_callback(void *userdata, const vrpn_TRACKERCB info) {
00119   VrpnTracker *self = (VrpnTracker *)userdata;
00120   if (vrpn_cat.is_spam()) {
00121     vrpn_cat.spam()
00122       << *self << " position_callback\n";
00123   }
00124 
00125   Devices::iterator di;
00126   for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
00127     VrpnTrackerDevice *device = (*di);
00128     if (device->get_sensor() == info.sensor &&
00129         device->get_data_type() == VrpnTrackerDevice::DT_position) {
00130       device->lock();
00131       device->_data.set_time(VrpnClient::convert_to_secs(info.msg_time));
00132       device->_data.set_pos(LPoint3f(info.pos[0], info.pos[1], info.pos[2]));
00133       device->_data.set_orient(LOrientationf(info.quat[3], info.quat[0], info.quat[1], info.quat[2]));
00134       device->unlock();
00135     }
00136   }
00137 }
00138 
00139 ////////////////////////////////////////////////////////////////////
00140 //     Function: VrpnTracker::vrpn_velocity_callback
00141 //       Access: Private, Static
00142 //  Description: Receives the tracker velocity data from the VRPN
00143 //               code and sends it to any interested
00144 //               VrpnTrackerDevices.
00145 ////////////////////////////////////////////////////////////////////
00146 void VrpnTracker::
00147 vrpn_velocity_callback(void *userdata, const vrpn_TRACKERVELCB info) {
00148   VrpnTracker *self = (VrpnTracker *)userdata;
00149   if (vrpn_cat.is_spam()) {
00150     vrpn_cat.spam()
00151       << *self << " velocity_callback\n";
00152   }
00153 
00154   Devices::iterator di;
00155   for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
00156     VrpnTrackerDevice *device = (*di);
00157     if (device->get_sensor() == info.sensor &&
00158         device->get_data_type() == VrpnTrackerDevice::DT_velocity) {
00159       device->lock();
00160       device->_data.set_time(VrpnClient::convert_to_secs(info.msg_time));
00161       device->_data.set_pos(LPoint3f(info.vel[0], info.vel[1], info.vel[2]));
00162       device->_data.set_orient(LOrientationf(info.vel_quat[3], info.vel_quat[0],
00163                                              info.vel_quat[1], info.vel_quat[2]));
00164       device->_data.set_dt(info.vel_quat_dt);
00165       device->unlock();
00166     }
00167   }
00168 }
00169 
00170 ////////////////////////////////////////////////////////////////////
00171 //     Function: VrpnTracker::vrpn_acceleration_callback
00172 //       Access: Private, Static
00173 //  Description: Receives the tracker acceleration data from the VRPN
00174 //               code and sends it to any interested
00175 //               VrpnTrackerDevices.
00176 ////////////////////////////////////////////////////////////////////
00177 void VrpnTracker::
00178 vrpn_acceleration_callback(void *userdata, const vrpn_TRACKERACCCB info) {
00179   VrpnTracker *self = (VrpnTracker *)userdata;
00180   if (vrpn_cat.is_spam()) {
00181     vrpn_cat.spam()
00182       << *self << " acceleration_callback\n";
00183   }
00184 
00185   Devices::iterator di;
00186   for (di = self->_devices.begin(); di != self->_devices.end(); ++di) {
00187     VrpnTrackerDevice *device = (*di);
00188     if (device->get_sensor() == info.sensor &&
00189         device->get_data_type() == VrpnTrackerDevice::DT_acceleration) {
00190       device->lock();
00191       device->_data.set_time(VrpnClient::convert_to_secs(info.msg_time));
00192       device->_data.set_pos(LPoint3f(info.acc[0], info.acc[1], info.acc[2]));
00193       device->_data.set_orient(LOrientationf(info.acc_quat[3], info.acc_quat[0],
00194                                              info.acc_quat[1], info.acc_quat[2]));
00195       device->_data.set_dt(info.acc_quat_dt);
00196       device->unlock();
00197     }
00198   }
00199 }

Generated on Fri May 2 00:44:37 2003 for Panda by doxygen1.3