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