Initial Commit

This commit is contained in:
2025-07-04 20:33:06 +03:00
commit 8f09347ae0
9219 changed files with 2447903 additions and 0 deletions
@@ -0,0 +1,129 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using UnityEngine;
using System.Threading;
namespace ImstkUnity
{
// HS-2022-feb-02 Should be refactored to remove the "manager" functionality from the
// Device, see the VRPNDevice for the pattern. Remove `static` components
// from here
#if IMSTK_USE_OPENHAPTICS
// If this is not defined iMSTK was not built with Open Haptics enabled
// to use build iMSTK with the flag iMSTK_USE_OpenHaptics set to ON
[AddComponentMenu("iMSTK/OpenHapticsDevice")]
public class OpenHapticsDevice : TrackingDevice
{
public string deviceName = "default";
public static Imstk.OpenHapticDeviceManager openHapticDeviceManager = null;
public static bool hapticsRunning = false;
public static Thread hapticThread = null;
public static void InitManager()
{
if (openHapticDeviceManager != null)
{
openHapticDeviceManager.init();
}
}
public void Start()
{
GetDevice();
}
public static void StartManager()
{
// Launch haptics on a separate thread if using
if (openHapticDeviceManager != null)
{
hapticsRunning = true;
Debug.Log("OpenHaptics Thread Starting");
hapticThread = new Thread(() =>
{
while (hapticsRunning)
{
if (openHapticDeviceManager != null)
{
openHapticDeviceManager.update();
}
}
});
hapticThread.Start();
}
}
public static void StopManager()
{
if (openHapticDeviceManager != null)
{
hapticsRunning = false;
hapticThread.Join();
Debug.Log("OpenHaptics Thread Stopping");
openHapticDeviceManager.uninit();
openHapticDeviceManager = null;
hapticThread = null;
}
}
protected override Imstk.DeviceClient MakeDevice()
{
if (openHapticDeviceManager == null)
{
openHapticDeviceManager = new Imstk.OpenHapticDeviceManager();
}
Debug.Log("MakeDeviceClient <" + deviceName + ">");
// Creates the default device (specify name for specific one)
if (deviceName == "default")
{
return openHapticDeviceManager.makeDeviceClient();
}
else
{
return openHapticDeviceManager.makeDeviceClient(deviceName);
}
}
}
#else
public class OpenHapticsDevice : TrackingDevice
{
public string deviceName = "default";
public static void InitManager()
{
Debug.LogError("OpenHaptics is not enable in this build");
}
public void Start() {}
public static void StartManager() {}
public static void StopManager() {}
protected override Imstk.DeviceClient MakeDevice() {
Debug.LogError("OpenHaptics is not enable in this build, " +
"see the documentation for more information ");
return null;
}
}
#endif
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c8117a352c4c15c4893e11073f5a3a39
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,105 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using UnityEngine;
namespace ImstkUnity
{
enum ButtonState
{
// From Imstk
BUTTON_RELEASED = 0,
BUTTON_TOUCHED = 1,
BUTTON_UNTOUCHED = 2,
BUTTON_PRESSED = 3
}
public abstract class TrackingDevice : MonoBehaviour
{
Imstk.DeviceClient trackingDevice = null;
public Vector3 GetPosition()
{
Imstk.Vec3d pos = trackingDevice.getPosition();
return pos.ToUnityVec();
}
public Quaternion GetOrientation()
{
Imstk.Quatd quat = trackingDevice.getOrientation();
return quat.ToUnityQuat();
}
public Vector3 GetForce()
{
Imstk.Vec3d vec = trackingDevice.getForce();
return vec.ToUnityVec();
}
public bool IsButtonDown(int id)
{
var val = trackingDevice.getButton(id);
if (val == (int)ButtonState.BUTTON_PRESSED || val == (int)ButtonState.BUTTON_TOUCHED) return true;
else return false;
}
public int[] GetButtons()
{
var result = new int[8];
for (int i = 0; i<8;++i)
{
result[i] = trackingDevice.getButton(i);
}
var b = trackingDevice.getButtons();
return result;
}
public float GetAnalog(int id)
{
var values = trackingDevice.getAnalog();
if (values.Count > id) return (float)values[id];
else
{
Debug.LogWarning("Id " + id.ToString() + " not found ");
return 0;
}
}
protected abstract Imstk.DeviceClient MakeDevice();
public void Update()
{
// This is not directly used, but displayed
Transform transform = gameObject.GetComponentFatal<Transform>();
transform.SetPositionAndRotation(GetPosition(), GetOrientation());
}
public Imstk.DeviceClient GetDevice()
{
if (trackingDevice == null)
{
trackingDevice = MakeDevice();
trackingDevice.setButtonsEnabled(true);
}
return trackingDevice;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7c4b75e1c7ff86c46be94008f67ce2e0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,57 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using UnityEngine;
namespace ImstkUnity
{
[AddComponentMenu("iMSTK/VrpnDevice")]
public class VrpnDevice : TrackingDevice
{
public string Name = "Tracker0";
private int _type = 0;
public int Type { get { return _type; } }
public bool TrackAnalog = false;
public bool TrackButtons = false;
public bool TrackPosition = true;
public ImstkUnity.VrpnDeviceManager manager;
public void Awake()
{
if (TrackAnalog) _type |= 0x1;
if (TrackButtons) _type |= 0x2;
if (TrackPosition) _type |= 0x4;
}
public void Start()
{
GetDevice();
}
protected override Imstk.DeviceClient MakeDevice()
{
return manager.MakeDeviceClient(this);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 79de4454bc1384f40b18333f30326fb5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,126 @@
/*=========================================================================
Library: iMSTK-Unity
Copyright (c) Kitware, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/
using System.Threading;
using UnityEngine;
namespace ImstkUnity
{
#if IMSTK_USE_VRPN
// If this is not defined iMSTK was not built with VRPN enabled
// to use build iMSTK with the flag iMSTK_USE_VRPN set to ON
[AddComponentMenu("iMSTK/VrpnDeviceManager")]
public class VrpnDeviceManager : MonoBehaviour
{
private static VrpnDeviceManager _instance;
// Probably Refactor to Singleton base class
public static VrpnDeviceManager Instance
{
get { return _instance; }
}
public string host = "localhost";
public int port = 3883;
private Imstk.VRPNDeviceManager _manager;
private Thread thread;
private bool running = false;
public void Awake()
{
_instance = this;
}
public void InitManager()
{
if (_manager == null)
{
_manager = new Imstk.VRPNDeviceManager(host, port);
if (_manager == null) Debug.LogError("Could not create VRPNDevice Manager");
_manager.setSleepDelay(20);
_manager.init();
}
}
public Imstk.DeviceClient MakeDeviceClient(ImstkUnity.VrpnDevice device)
{
InitManager();
return _manager.makeDeviceClient(device.Name, device.Type);
}
public void StartManager()
{
if (running) return;
InitManager();
running = true;
Debug.Log("VRPN Thread Starting");
thread = new Thread(() =>
{
while (running)
{
_manager.update();
}
});
thread.Start();
}
public void StopManager()
{
if (_manager == null) return;
if (!running) return;
running = false;
thread.Join();
Debug.Log("VRPN Thread Stopping");
_manager.uninit();
thread = null;
}
}
#else
[AddComponentMenu("iMSTK/VrpnDeviceManager")]
public class VrpnDeviceManager : MonoBehaviour
{
// Probably Refactor to Singleton base class
public static VrpnDeviceManager Instance
{
get {
Debug.LogError("VRPN is not enable in this build");
return null;
}
}
public void Awake()
{
var a = Instance;
}
public void InitManager() {}
public Imstk.DeviceClient MakeDeviceClient(ImstkUnity.VrpnDevice device) {
return null;
}
public void StartManager() {}
public void StopManager() {}
}
#endif
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1644d13714b4c8446a7bc7df42c96612
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 936dbb32eb653d84a9bcbe9caad48547, type: 3}
userData:
assetBundleName:
assetBundleVariant: