iMSTK
Interactive Medical Simulation Toolkit
imstkVRPNDeviceClient.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkVRPNDeviceClient.h"
8 
9 #include "quat.h"
10 
11 #include "imstkLogger.h"
12 
13 namespace imstk
14 {
15 VRPNDeviceClient::VRPNDeviceClient(const std::string& deviceName, VRPNDeviceType type, const std::string& ip /*= "localhost"*/) :
16  DeviceClient(deviceName, ip), m_type(type)
17 {
18  m_trackingEnabled = (type & VRPNTracker) != 0;
19  m_buttonsEnabled = (type & VRPNButton) != 0;
20  m_analogicEnabled = (type & VRPNAnalog) != 0;
21  m_forceEnabled = (type & VRPNForce) != 0;
22 }
23 
24 void VRPN_CALLBACK
25 VRPNDeviceClient::trackerPositionChangeHandler(void* userData, const _vrpn_TRACKERCB t)
26 {
27  auto deviceClient = static_cast<VRPNDeviceClient*>(userData);
28 
29  Quatd quat;
30  quat.x() = t.quat[1];
31  quat.y() = t.quat[2];
32  quat.z() = t.quat[3];
33  quat.w() = t.quat[0];
34 
35  deviceClient->m_transformLock.lock();
36  deviceClient->m_position << t.pos[0], t.pos[1], t.pos[2];
37  deviceClient->m_orientation = quat;
38  deviceClient->m_transformLock.unlock();
39 }
40 
41 void VRPN_CALLBACK
42 VRPNDeviceClient::analogChangeHandler(void* userData, const _vrpn_ANALOGCB a)
43 {
44  auto deviceClient = static_cast<VRPNDeviceClient*>(userData);
45  deviceClient->m_dataLock.lock();
46  deviceClient->m_analogChannels.resize(a.num_channel);
47  for (int i = 0; i < a.num_channel; i++)
48  {
49  deviceClient->m_analogChannels[i] = a.channel[i];
50  }
51  deviceClient->m_dataLock.unlock();
52 }
53 
54 void VRPN_CALLBACK
55 VRPNDeviceClient::trackerVelocityChangeHandler(void* userData, const _vrpn_TRACKERVELCB v)
56 {
57  auto deviceClient = reinterpret_cast<VRPNDeviceClient*>(userData);
58  Quatd quat(v.vel_quat[1], v.vel_quat[2], v.vel_quat[3], v.vel_quat[0]);
59 
60  deviceClient->m_transformLock.lock();
61  deviceClient->m_velocity << v.vel[0], v.vel[1], v.vel[2];
62  // \todo translate velocity quaternion to imstk
63  // deviceClient->m_angularVelocity = quat;
64  //
65  deviceClient->m_transformLock.unlock();
66 }
67 
68 void VRPN_CALLBACK
69 VRPNDeviceClient::buttonChangeHandler(void* userData, const _vrpn_BUTTONCB b)
70 {
71  auto deviceClient = reinterpret_cast<VRPNDeviceClient*>(userData);
72  deviceClient->m_buttons[b.button] = (b.state == 1);
73 }
74 
75 imstk::VRPNDeviceType
77 {
78  return m_type;
79 }
80 } // imstk
static void VRPN_CALLBACK trackerPositionChangeHandler(void *userData, const _vrpn_TRACKERCB t)
VRPN call back for position and orientation data.
The device client&#39;s represents the device and provides an interface to acquire data from a device...
static void VRPN_CALLBACK trackerVelocityChangeHandler(void *userData, const _vrpn_TRACKERVELCB v)
VRPN call back for velocity data.
bool m_analogicEnabled
Analogic enabled if true.
ParallelUtils::SpinLock m_dataLock
Used for button and analog data.
Compound Geometry.
bool m_trackingEnabled
Tracking enabled if true.
ParallelUtils::SpinLock m_transformLock
Used for devices filling data from other threads.
static void VRPN_CALLBACK buttonChangeHandler(void *userData, const _vrpn_BUTTONCB b)
VRPN call back for button changed (pressed or released)
void lock()
Start a thread-safe region, where only one thread can execute at a time until a call to the unlock fu...
Definition: imstkSpinLock.h:45
VRPNDeviceClient(const std::string &deviceName, VRPNDeviceType type, const std::string &ip="localhost")
Constructor.
bool m_buttonsEnabled
Buttons enabled if true.
static void VRPN_CALLBACK analogChangeHandler(void *userData, const _vrpn_ANALOGCB a)
VRPN call back analog data.
VRPNDeviceType getType() const
bool m_forceEnabled
Force enabled if true.
This class is the receiver of the updates sent by the vrpn_server.