diff --git a/Examples/BoneShaving/BoneShavingExample.cpp b/Examples/BoneShaving/BoneShavingExample.cpp index 94ad4f0410a72dfb728627b55a142c3471d18821..47d52f8cbf2b278c89588411b68af3312320e244 100644 --- a/Examples/BoneShaving/BoneShavingExample.cpp +++ b/Examples/BoneShaving/BoneShavingExample.cpp @@ -47,9 +47,6 @@ #include "imstkCollisionGraph.h" #include "imstkObjectInteractionFactory.h" -// global variables -const std::string phantomOmni1Name = "Default Device"; - using namespace imstk; /// @@ -67,7 +64,8 @@ main() // Device Server imstkNew<HapticDeviceManager> server; - std::shared_ptr<HapticDeviceClient> client = server->makeDeviceClient(phantomOmni1Name); + const std::string deviceName = ""; + std::shared_ptr<HapticDeviceClient> client = server->makeDeviceClient(deviceName); // Create bone scene object // Load the mesh diff --git a/Examples/CameraController/CameraControllerExample.cpp b/Examples/CameraController/CameraControllerExample.cpp index 5cb6ef3b6424a821f5c229d0ebc9bd9ecc889444..51d0b54b784ee238249611199375764e59e41eed 100644 --- a/Examples/CameraController/CameraControllerExample.cpp +++ b/Examples/CameraController/CameraControllerExample.cpp @@ -36,8 +36,6 @@ #include "imstkSurfaceMesh.h" #include "imstkVTKViewer.h" -const std::string phantomOmni1Name = "Default Device"; - using namespace imstk; /// @@ -55,7 +53,8 @@ main() // Device Server imstkNew<HapticDeviceManager> server; - std::shared_ptr<HapticDeviceClient> client = server->makeDeviceClient(phantomOmni1Name); + const std::string deviceName = ""; + std::shared_ptr<HapticDeviceClient> client = server->makeDeviceClient(deviceName); // Load Mesh auto mesh = MeshIO::read<SurfaceMesh>(iMSTK_DATA_ROOT "/asianDragon/asianDragon.obj"); diff --git a/Examples/ObjectController/ObjectControllerExample.cpp b/Examples/ObjectController/ObjectControllerExample.cpp index 5d17119b8ac7785fe616a4625fdd22139ba7fdd0..06369e45c0129da31b7c90ad6f230d66ed5ee0ec 100644 --- a/Examples/ObjectController/ObjectControllerExample.cpp +++ b/Examples/ObjectController/ObjectControllerExample.cpp @@ -34,9 +34,6 @@ #include "imstkSceneObjectController.h" #include "imstkVTKViewer.h" -// global variables -const std::string phantomOmni1Name = "Default Device"; - using namespace imstk; /// @@ -54,7 +51,8 @@ main() // Device Server imstkNew<HapticDeviceManager> server; - std::shared_ptr<HapticDeviceClient> client = server->makeDeviceClient(phantomOmni1Name); + const std::string deviceName = ""; + std::shared_ptr<HapticDeviceClient> client = server->makeDeviceClient(deviceName); // Object imstkNew<Cube> geom(Vec3d(0.0, 1.0, 0.0), 2.0); diff --git a/Examples/OpenVRController/OpenVRControllerExample.cpp b/Examples/OpenVRController/OpenVRControllerExample.cpp index deeb899735f84b184350dbc00b94d3a63eb35bd6..f6559cac3ab23a14313000a23ce4fdde750db65b 100644 --- a/Examples/OpenVRController/OpenVRControllerExample.cpp +++ b/Examples/OpenVRController/OpenVRControllerExample.cpp @@ -135,10 +135,11 @@ main() imstkNew<SceneObjectController> controller2(scalpelBlade10, viewer->getVRDeviceClient(OPENVR_RIGHT_CONTROLLER)); scene->addController(controller2); - // VRButtonPress emitted from viewer, recieve it in the scene that way the scene is not running whilst it is swapped + // This button event is emitted from the viewer's thread, thus it is queued to the scene so that we do not + // run it while the scene is updating bool blade10InHand = true; - queueConnect<VRButtonEvent>(viewer->getVRDeviceClient(OPENVR_RIGHT_CONTROLLER), EventType::VRButtonPress, sceneManager, - [&](VRButtonEvent* e) + queueConnect<ButtonEvent>(viewer->getVRDeviceClient(OPENVR_RIGHT_CONTROLLER), EventType::DeviceButtonPress, sceneManager, + [&](ButtonEvent* e) { // When any button pressed, swap blade // todo: distance metric not working diff --git a/Examples/RigidBodyVirtualCoupling/RigidBodyVirtualCouplingExample.cpp b/Examples/RigidBodyVirtualCoupling/RigidBodyVirtualCouplingExample.cpp index ed6df35219da121c25c5dd32e838fb06fb7114e3..994258118bb5d378f8ebb69c93220bebb9339e88 100644 --- a/Examples/RigidBodyVirtualCoupling/RigidBodyVirtualCouplingExample.cpp +++ b/Examples/RigidBodyVirtualCoupling/RigidBodyVirtualCouplingExample.cpp @@ -43,9 +43,6 @@ #include "imstkVisualModel.h" #include "imstkVTKViewer.h" -// global variables -const std::string phantomOmni1Name = "Default Device"; - using namespace imstk; std::shared_ptr<imstk::RigidObject> @@ -201,7 +198,8 @@ main() // Device Server imstkNew<HapticDeviceManager> server; - std::shared_ptr<HapticDeviceClient> client = server->makeDeviceClient(phantomOmni1Name); + const std::string deviceName = ""; + std::shared_ptr<HapticDeviceClient> client = server->makeDeviceClient(deviceName); // Create a virtual coupling object imstkNew<Sphere> visualGeom(Vec3d(0.0, 0.0, 0.0), 5.0); diff --git a/Examples/VirtualCoupling/VirtualCouplingExample.cpp b/Examples/VirtualCoupling/VirtualCouplingExample.cpp index 7d2253acb84c707cefc6c5211abb8503e9782786..e7122bb91cb17e92df3765d7f762250247b0ba9a 100644 --- a/Examples/VirtualCoupling/VirtualCouplingExample.cpp +++ b/Examples/VirtualCoupling/VirtualCouplingExample.cpp @@ -41,9 +41,6 @@ #include "imstkVisualModel.h" #include "imstkVTKViewer.h" -// global variables -const std::string phantomOmni1Name = "Default Device"; - using namespace imstk; /// @@ -72,7 +69,8 @@ main() // Device Server imstkNew<HapticDeviceManager> server; - std::shared_ptr<HapticDeviceClient> client = server->makeDeviceClient(phantomOmni1Name); + const std::string deviceName = ""; + std::shared_ptr<HapticDeviceClient> client = server->makeDeviceClient(deviceName); // Create a virtual coupling object imstkNew<Sphere> visualGeom(Vec3d(0.0, 0.0, 0.0), 20.0); diff --git a/Source/Common/imstkEventObject.h b/Source/Common/imstkEventObject.h index 8582fb7b82f241dd86325c680b0991a65f86e86c..877be3abaf4e6adfaf6d2493d48ca025798606a2 100644 --- a/Source/Common/imstkEventObject.h +++ b/Source/Common/imstkEventObject.h @@ -40,12 +40,11 @@ enum class EventType Pause, Modified, KeyPress, - VRButtonPress, + DeviceButtonPress, MouseEvent, PreUpdate, PostUpdate, Configure - //?? add one for collision? }; /// diff --git a/Source/Devices/imstkDeviceClient.h b/Source/Devices/imstkDeviceClient.h index 53b2989375de05bfa284e3f5e469116447edaa20..964c091dbad9b2241366df8e1b3b073983a78496 100644 --- a/Source/Devices/imstkDeviceClient.h +++ b/Source/Devices/imstkDeviceClient.h @@ -28,6 +28,35 @@ namespace imstk { +using DeviceType = int; +#define UNKNOWN_DEVICE 0 +#define OPENHAPTICS_DEVICE 1 +#define OPENVR_LEFT_CONTROLLER 3 +#define OPENVR_RIGHT_CONTROLLER 4 +#define OPENVR_HMD 5 + +using ButtonStateType = int; +#define BUTTON_RELEASED 0 +#define BUTTON_TOUCHED 1 +#define BUTTON_UNTOUCHED 2 +#define BUTTON_PRESSED 3 + +class ButtonEvent : public Event +{ +public: + ButtonEvent(const int button, const ButtonStateType keyPressType) : Event(EventType::DeviceButtonPress, 1), + m_buttonState(keyPressType), + m_button(button) + { + } + + virtual ~ButtonEvent() override = default; + +public: + ButtonStateType m_buttonState; + const int m_button = -1; +}; + /// /// \class DeviceClient /// diff --git a/Source/Devices/imstkHapticDeviceClient.cpp b/Source/Devices/imstkHapticDeviceClient.cpp index beac639ef00c9643ed2429d57fbe4669256eb366..06bf0992859326e65ea80e3174c60dcf2ed1524c 100644 --- a/Source/Devices/imstkHapticDeviceClient.cpp +++ b/Source/Devices/imstkHapticDeviceClient.cpp @@ -40,18 +40,36 @@ HapticDeviceClient::initialize() while (HD_DEVICE_ERROR(errorFlush = hdGetError())) {} // Initialize the device - m_handle = hdInitDevice(this->getDeviceName().c_str()); + if (m_deviceName == "") + { + m_handle = hdInitDevice(HD_DEFAULT_DEVICE); + } + else + { + m_handle = hdInitDevice(getDeviceName().c_str()); + } // If failed HDErrorInfo error; - LOG_IF(FATAL, HD_DEVICE_ERROR(error = hdGetError())) << "Failed to initialize Phantom Omni " << this->getDeviceName(); + LOG_IF(FATAL, HD_DEVICE_ERROR(error = hdGetError())) << "Failed to initialize Phantom Omni \"" << this->getDeviceName() + "\""; + + // If initialized as default, set the name + if (m_deviceName == "") + { + // Worth noting that in this case the name will not match the actual device name and is + // now only useful for scene level identification, OpenHaptics provides no mechanisms + // for querying device names + hdMakeCurrentDevice(m_handle); + HDstring str = hdGetString(HD_DEVICE_SERIAL_NUMBER); + m_deviceName = "Device_" + std::string(str); + } // Enable forces hdEnable(HD_FORCE_OUTPUT); hdEnable(HD_FORCE_RAMPING); // Success - LOG(INFO) << this->getDeviceName() << " successfully initialized."; + LOG(INFO) << "\"" << this->getDeviceName() << "\" successfully initialized."; } void @@ -92,10 +110,22 @@ HapticDeviceClient::hapticCallback(void* pData) client->m_position << state.pos[0], state.pos[1], state.pos[2]; client->m_velocity << state.vel[0], state.vel[1], state.vel[2]; client->m_orientation = (Eigen::Affine3d(Eigen::Matrix4d(state.trans))).rotation(); - client->m_buttons[0] = state.buttons & HD_DEVICE_BUTTON_1; - client->m_buttons[1] = state.buttons & HD_DEVICE_BUTTON_2; - client->m_buttons[2] = state.buttons & HD_DEVICE_BUTTON_3; - client->m_buttons[3] = state.buttons & HD_DEVICE_BUTTON_4; + + for (int i = 0; i < 4; i++) + { + // If button down and not previously down + if ((state.buttons & (1 << i)) && !client->m_buttons[i]) + { + client->m_buttons[i] = true; + client->postEvent(ButtonEvent(i, BUTTON_PRESSED)); + } + // If button not down, and previously down + else if (!(state.buttons & (1 << i)) && client->m_buttons[i]) + { + client->m_buttons[i] = false; + client->postEvent(ButtonEvent(i, BUTTON_RELEASED)); + } + } client->m_trackingEnabled = true; diff --git a/Source/Devices/imstkHapticDeviceClient.h b/Source/Devices/imstkHapticDeviceClient.h index ed9b11ff57fcc49d9d4e8e6c58eb671e5c702056..35bc72f251ac7c10ee14d403467019f832706329 100644 --- a/Source/Devices/imstkHapticDeviceClient.h +++ b/Source/Devices/imstkHapticDeviceClient.h @@ -52,8 +52,9 @@ public: protected: /// /// \brief Constructor/Destructor, only the DeviceManager can construct + /// \param Device name or use empty string for default device /// - HapticDeviceClient(const std::string& name) : DeviceClient(name, "localhost"), m_handle(HD_INVALID_HANDLE) { } + HapticDeviceClient(std::string name = "") : DeviceClient(name, "localhost"), m_handle(HD_INVALID_HANDLE) { } /// /// \brief Initialize the phantom omni device diff --git a/Source/Devices/imstkHapticDeviceManager.cpp b/Source/Devices/imstkHapticDeviceManager.cpp index 08a45a5ea512b1b0f6c8711976164c3e76513d36..fab91e0b94848c8d4b887816ec34cdcf1257e60c 100644 --- a/Source/Devices/imstkHapticDeviceManager.cpp +++ b/Source/Devices/imstkHapticDeviceManager.cpp @@ -29,7 +29,7 @@ namespace imstk { std::shared_ptr<HapticDeviceClient> -HapticDeviceManager::makeDeviceClient(const std::string& name) +HapticDeviceManager::makeDeviceClient(std::string name) { auto deviceClient = std::shared_ptr<HapticDeviceClient>(new HapticDeviceClient(name)); m_deviceClients.push_back(deviceClient); diff --git a/Source/Devices/imstkHapticDeviceManager.h b/Source/Devices/imstkHapticDeviceManager.h index bc6dda73210389f03eebf80a335ab3059eca4a01..26eeb9c92d5e02a4a6bcb57f8ab31e2489aff20c 100644 --- a/Source/Devices/imstkHapticDeviceManager.h +++ b/Source/Devices/imstkHapticDeviceManager.h @@ -50,8 +50,9 @@ public: /// /// \brief Create a haptic device client and add it to the internal list + /// \param Device name or use empty string for default device /// - std::shared_ptr<HapticDeviceClient> makeDeviceClient(const std::string& name); + std::shared_ptr<HapticDeviceClient> makeDeviceClient(std::string name = ""); /// /// \brief Initialize the client devices and start the scheduler diff --git a/Source/Devices/imstkOpenVRDeviceClient.cpp b/Source/Devices/imstkOpenVRDeviceClient.cpp index 6605e0a3d642b1822ac9ac07662b0f69f361c9ea..7e9da67a07c44f3349eccec937fb047926465690 100644 --- a/Source/Devices/imstkOpenVRDeviceClient.cpp +++ b/Source/Devices/imstkOpenVRDeviceClient.cpp @@ -24,7 +24,7 @@ namespace imstk { std::shared_ptr<OpenVRDeviceClient> -OpenVRDeviceClient::New(VRDeviceType deviceType) +OpenVRDeviceClient::New(DeviceType deviceType) { return std::shared_ptr<OpenVRDeviceClient>(new OpenVRDeviceClient(deviceType)); } @@ -36,7 +36,7 @@ OpenVRDeviceClient::emitButtonTouched(const int buttonId) m_buttons[buttonId] = BUTTON_TOUCHED; if (prevButtonState != BUTTON_TOUCHED) { - this->postEvent(VRButtonEvent(buttonId, BUTTON_TOUCHED)); + this->postEvent(ButtonEvent(buttonId, BUTTON_TOUCHED)); } } @@ -47,7 +47,7 @@ OpenVRDeviceClient::emitButtonUntouched(const int buttonId) m_buttons[buttonId] = BUTTON_UNTOUCHED; if (prevButtonState != BUTTON_UNTOUCHED) { - this->postEvent(VRButtonEvent(buttonId, BUTTON_UNTOUCHED)); + this->postEvent(ButtonEvent(buttonId, BUTTON_UNTOUCHED)); } } @@ -58,7 +58,7 @@ OpenVRDeviceClient::emitButtonPress(const int buttonId) m_buttons[buttonId] = BUTTON_PRESSED; if (prevButtonState != BUTTON_PRESSED) { - this->postEvent(VRButtonEvent(buttonId, BUTTON_PRESSED)); + this->postEvent(ButtonEvent(buttonId, BUTTON_PRESSED)); } } @@ -69,7 +69,7 @@ OpenVRDeviceClient::emitButtonRelease(const int buttonId) m_buttons[buttonId] = BUTTON_RELEASED; if (prevButtonState != BUTTON_RELEASED) { - this->postEvent(VRButtonEvent(buttonId, BUTTON_RELEASED)); + this->postEvent(ButtonEvent(buttonId, BUTTON_RELEASED)); } } } \ No newline at end of file diff --git a/Source/Devices/imstkOpenVRDeviceClient.h b/Source/Devices/imstkOpenVRDeviceClient.h index 42aa17b13bc98771e690de7af136edae8f423bc4..9a00831a4d800fcc6128dc526ffbb73602109871 100644 --- a/Source/Devices/imstkOpenVRDeviceClient.h +++ b/Source/Devices/imstkOpenVRDeviceClient.h @@ -28,33 +28,6 @@ class vtkInteractorStyleVR; namespace imstk { -using VRDeviceType = int; -#define OPENVR_LEFT_CONTROLLER 0 -#define OPENVR_RIGHT_CONTROLLER 1 -#define OPENVR_HMD 2 - -using VRButtonStateType = int; -#define BUTTON_RELEASED 0 -#define BUTTON_TOUCHED 1 -#define BUTTON_UNTOUCHED 2 -#define BUTTON_PRESSED 3 - -class VRButtonEvent : public Event -{ -public: - VRButtonEvent(const int button, const VRButtonStateType keyPressType) : Event(EventType::VRButtonPress, 1), - m_buttonState(keyPressType), - m_button(button) - { - } - - virtual ~VRButtonEvent() override = default; - -public: - VRButtonStateType m_buttonState; - const int m_button = -1; -}; - /// /// \class OpenVRDeviceClient /// @@ -68,13 +41,13 @@ protected: /// /// This object is only creatable through its New method /// - OpenVRDeviceClient(VRDeviceType deviceType) : DeviceClient("OpenVRDevice", ""), + OpenVRDeviceClient(DeviceType deviceType) : DeviceClient("OpenVRDevice", ""), m_deviceType(deviceType) { } /// /// This object is only creatable through this method /// - static std::shared_ptr<OpenVRDeviceClient> New(VRDeviceType deviceType); + static std::shared_ptr<OpenVRDeviceClient> New(DeviceType deviceType); public: virtual ~OpenVRDeviceClient() override = default; @@ -83,7 +56,7 @@ public: friend class ::vtkInteractorStyleVR; public: - VRDeviceType getDeviceType() const { return m_deviceType; } + DeviceType getDeviceType() const { return m_deviceType; } /// /// \brief Set the current position and orientation @@ -105,6 +78,6 @@ protected: void emitButtonRelease(const int buttonId); private: - VRDeviceType m_deviceType; + DeviceType m_deviceType; }; } \ No newline at end of file