Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifdef MITK_USE_SPACENAVIGATOR_DRIVER
00019 #define SPACE_NAVIGATOR_MAIN_SOURCE
00020 #endif
00021
00022 #include "mitkSpaceNavigatorDriver.h"
00023 #include "mitkSpaceNavigatorAddOn.h"
00024
00025 SpaceNavigatorDriver* SpaceNavigatorDriver::GetInstance()
00026 {
00027
00028 static SpaceNavigatorDriver instance;
00029 return &instance;
00030 }
00031
00032 #ifdef MITK_USE_SPACENAVIGATOR_DRIVER
00033
00034 SpaceNavigatorDriver::SpaceNavigatorDriver()
00035 {
00036 LRESULT result=0;
00037
00038 HRESULT hr=::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED );
00039 if (!SUCCEEDED(hr))
00040 {
00041 MITK_ERROR("Fehler");
00042
00043
00044 result = -1;
00045 }
00046 else
00047 {
00048 HRESULT hr = InitializeCOM();
00049 if (!SUCCEEDED(hr))
00050 {
00051
00052 result = -1;
00053 }
00054 }
00055 }
00056
00057 SpaceNavigatorDriver::~SpaceNavigatorDriver()
00058 {
00059 UninitializeCOM();
00060 }
00061
00062 inline HRESULT SpaceNavigatorDriver::HookEvent1(CComPtr<ISimpleDevice> device)
00063 {
00064
00065 return device->get_Sensor(&m_pISensor);
00066 }
00067
00068 inline HRESULT SpaceNavigatorDriver::HookEvent2()
00069 {
00070 return __hook(&_ISensorEvents::SensorInput, m_pISensor,
00071 &SpaceNavigatorDriver::OnSensorInput);
00072 }
00073
00074 inline HRESULT SpaceNavigatorDriver::HookEvent3(CComPtr<ISimpleDevice> device)
00075 {
00076 return device->get_Keyboard(&m_pIKeyboard);
00077 }
00078
00079 inline HRESULT SpaceNavigatorDriver::HookEvent4()
00080 {
00081 return __hook(&_IKeyboardEvents::KeyDown, m_pIKeyboard,
00082 &SpaceNavigatorDriver::OnKeyDown);
00083 }
00084
00085 inline HRESULT SpaceNavigatorDriver::HookEvent5()
00086 {
00087 return __hook(&_IKeyboardEvents::KeyUp, m_pIKeyboard,
00088 &SpaceNavigatorDriver::OnKeyUp);
00089 }
00090
00091
00092
00093 HRESULT SpaceNavigatorDriver::InitializeCOM()
00094 {
00095 HRESULT hr;
00096 CComPtr<IUnknown> _3DxDevice;
00097
00098
00099 hr = _3DxDevice.CoCreateInstance(__uuidof(Device));
00100 if (SUCCEEDED(hr))
00101 {
00102
00103 CComPtr<ISimpleDevice> _3DxSimpleDevice;
00104
00105 hr = _3DxDevice.QueryInterface(&_3DxSimpleDevice);
00106 if (SUCCEEDED(hr))
00107 {
00108 hr = __hook(&_ISimpleDeviceEvents::DeviceChange, _3DxSimpleDevice,
00109 &SpaceNavigatorDriver::OnDeviceChange);
00110
00111
00112 hr = this->HookEvent1(_3DxSimpleDevice);
00113 hr = this->HookEvent2();
00114 hr = this->HookEvent3(_3DxSimpleDevice);
00115 hr = this->HookEvent4();
00116 hr = this->HookEvent5();
00117
00118
00119 _3DxSimpleDevice->Connect();
00120
00121 MITK_INFO << "Connected to COM-object for space navigation...";
00122 }
00123 }
00124 else
00125 MITK_ERROR << "CoCreateInstance failed";
00126
00127 return hr;
00128 }
00129
00130 HRESULT SpaceNavigatorDriver::OnDeviceChange(long reserved )
00131 {
00132 HRESULT result = S_OK;
00133
00134
00135 HRESULT hr;
00136 ISimpleDevicePtr _p3DxDevice;
00137 hr = m_pIKeyboard->get_Device((IDispatch**)&_p3DxDevice);
00138 if (SUCCEEDED(hr))
00139 {
00140 long device;
00141 _p3DxDevice->get_Type(&device);
00142
00143 _p3DxDevice.Release();
00144
00145 long keys, programmableKeys;
00146 m_pIKeyboard->get_Keys(&keys);
00147 m_pIKeyboard->get_ProgrammableKeys(&programmableKeys);
00148
00149
00150 for (long i=1; i<=keys; i++)
00151 {
00152 BSTR bstrLabel;
00153 BSTR bstrName;
00154 m_pIKeyboard->GetKeyLabel(i, &bstrLabel);
00155 m_pIKeyboard->GetKeyName(i, &bstrName);
00156 CString strLabel(bstrLabel);
00157 CString strName(bstrName);
00158
00159
00160
00161
00162 }
00163 mitk::SpaceNavigatorAddOn::GetInstance()->DeviceChange (device, keys, programmableKeys);
00164 }
00165 return result;
00166 }
00167
00168 HRESULT SpaceNavigatorDriver::OnKeyDown(int keyCode )
00169 {
00170 HRESULT result = S_OK;
00171 mitk::SpaceNavigatorAddOn::GetInstance()->KeyDown (keyCode);
00172 return result;
00173 }
00174
00175 HRESULT SpaceNavigatorDriver::OnKeyUp(int keyCode )
00176 {
00177 HRESULT result = S_OK;
00178 mitk::SpaceNavigatorAddOn::GetInstance()->KeyUp (keyCode);
00179 return result;
00180 }
00181
00182 #define ScaleRotation 1024.0
00183 #define ScaleTranslation 512.0
00184 #define Sensitivity 1.0
00185
00186 HRESULT SpaceNavigatorDriver::OnSensorInput(void)
00187 {
00188 HRESULT result = S_OK;
00189 static DWORD s_dwLastDraw = 0;
00190
00191
00192 try {
00193 CComPtr<IAngleAxis> pRotation;
00194 HRESULT hr = m_pISensor->get_Rotation(&pRotation);
00195
00196 double angle;
00197 pRotation->get_Angle(&angle);
00198
00199 CComPtr<IVector3D> pTranslation;
00200 hr = m_pISensor->get_Translation(&pTranslation);
00201
00202 double length;
00203 pTranslation->get_Length(&length);
00204
00205 if (angle > 0. || length > 0.)
00206 {
00207 mitk::Vector3D translation;
00208 mitk::Vector3D rotation;
00209 double val;
00210
00211 double timeFactor = 1.;
00212
00213 DWORD dwNow = ::GetTickCount();
00214 if (s_dwLastDraw)
00215 {
00216 double period;
00217 m_pISensor->get_Period(&period);
00218 timeFactor = (double)(dwNow-s_dwLastDraw)/period;
00219 }
00220 s_dwLastDraw = dwNow;
00221
00222 length *= timeFactor;
00223 length /= ScaleTranslation*Sensitivity;
00224
00225 pTranslation->put_Length(length);
00226
00227 pTranslation->get_X(&val);
00228 translation[0] = (float)val;
00229 pTranslation->get_Y(&val);
00230 translation[1] = (float)val;
00231 pTranslation->get_Z(&val);
00232 translation[2] = (float)val;
00233
00234 pRotation->get_X(&val);
00235 rotation[0] = (float)val;
00236 pRotation->get_Y(&val);
00237 rotation[1] = (float)val;
00238 pRotation->get_Z(&val);
00239 rotation[2] = (float)val;
00240
00241 angle *= timeFactor;
00242 angle /= ScaleRotation*Sensitivity;
00243
00244 mitk::SpaceNavigatorAddOn::GetInstance()->SensorInput( translation, rotation, angle);
00245
00246 }
00247 else
00248 s_dwLastDraw = 0;
00249
00250 pRotation.Release();
00251 pTranslation.Release();
00252 }
00253 catch (...)
00254 {
00255 MITK_ERROR << "Some sort of exception handling";
00256 }
00257
00258 return result;
00259 }
00260
00261 HRESULT SpaceNavigatorDriver::UninitializeCOM()
00262 {
00263 HRESULT hr;
00264 CComPtr<IUnknown> _3DxDevice;
00265
00266
00267 hr = _3DxDevice.CoCreateInstance(__uuidof(Device));
00268 if (SUCCEEDED(hr))
00269 {
00270 CComPtr<ISimpleDevice> _3DxSimpleDevice;
00271 _3DxDevice.QueryInterface(&_3DxSimpleDevice);
00272 _3DxSimpleDevice->Disconnect();
00273 }
00274
00275
00276 if (m_pISensor)
00277 {
00278
00279 __unhook(&_ISensorEvents::SensorInput, m_pISensor,
00280 &SpaceNavigatorDriver::OnSensorInput);
00281
00282 m_pISensor.Release();
00283 }
00284
00285 if (m_pIKeyboard)
00286 {
00287 __unhook(&_IKeyboardEvents::KeyDown, m_pIKeyboard,
00288 &SpaceNavigatorDriver::OnKeyDown);
00289
00290 m_pIKeyboard.Release();
00291 }
00292
00293 return hr;
00294 }
00295 #endif // MITK_USE_SPACENAVIGATOR_DRIVER