SpaceMouseModule.cxx
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
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00042
00043
00044
00045 #include "../tool/disable4786.h"
00046
00047 #include "SpaceMouseSource.h"
00048 #include "SpaceMouseModule.h"
00049
00050 #ifdef USE_SPACEMOUSE
00051
00052 #include <stdio.h>
00053 #if defined (WIN32) || defined (GCC3)
00054 #include <iostream>
00055 #else
00056 #include <iostream.h>
00057 #endif
00058
00059 #include <Windows.h>
00060 #include <math.h>
00061
00062
00063 #include "spwmacro.h"
00064 #include "si.h"
00065 #include "siapp.h"
00066
00067 #include "..\core\MathUtils.h"
00068
00069
00070
00071 namespace ot {
00072
00073 HWND SpaceMouseModule::hWndSpaceMouse = NULL;
00074 SiHdl devHdl;
00075 SiOpenData oData;
00076
00077
00078 LRESULT FAR PASCAL WndSpaceMouseProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
00079 {
00080 return DefWindowProc(hWnd, uMsg, wParam, lParam);
00081 };
00082
00083
00084 SpaceMouseModule::~SpaceMouseModule()
00085 {
00086 nodes.clear();
00087 }
00088
00089
00090 Node * SpaceMouseModule::createNode( const std::string& name, StringTable& attributes)
00091 {
00092 if( name.compare("SpaceMouseSource") == 0 )
00093 {
00094 SpaceMouseSource * source = new SpaceMouseSource;
00095 source->event.getConfidence() = 1.0f;
00096 nodes.push_back( source );
00097 ACE_LOG_INFO("ot:Built SpaceMouseSource node\n");
00098 initialized = 1;
00099 return source;
00100 }
00101 return NULL;
00102 }
00103
00104
00105 void SpaceMouseModule::start()
00106 {
00107 if( isInitialized() == 1 && !nodes.empty())
00108 {
00109 ThreadModule::start();
00110 }
00111 }
00112
00113
00114 void SpaceMouseModule::close()
00115 {
00116
00117 lock();
00118 stop = 1;
00119 unlock();
00120
00121 if( (isInitialized() == 1) && (devHdl != NULL))
00122 {
00123
00124 SiTerminate();
00125 }
00126 }
00127
00128
00129
00130
00131 void SpaceMouseModule::run()
00132 {
00133 WNDCLASS wndclass ;
00134 static int init = 0;
00135
00136 if( init == 0 )
00137 {
00138
00139
00140 if (SiInitialize() == SPW_DLL_LOAD_ERROR)
00141 {
00142 LOG_ACE_ERROR("ot:SpaceMouseModule Error Loading SIAPPDLL.DLL.\n");
00143 exit(1);
00144 }
00145
00146
00147 wndclass.style = 0;
00148 wndclass.lpfnWndProc = WndSpaceMouseProc ;
00149 wndclass.cbClsExtra = 0;
00150 wndclass.cbWndExtra = 0;
00151 wndclass.hInstance = NULL;
00152 wndclass.hIcon = NULL;
00153 wndclass.hCursor = NULL;
00154 wndclass.hbrBackground = 0;
00155 wndclass.lpszMenuName = NULL ;
00156 wndclass.lpszClassName = "dummyClassSpaceMouseModule";
00157
00158
00159 if ( RegisterClass(&wndclass) == 0)
00160 {
00161 LOG_ACE_ERROR("ot:SpaceMouseModule Could not register windowclass.\n");
00162 exit(1);
00163 }
00164
00165 hWndSpaceMouse = CreateWindow(
00166 "dummyClassSpaceMouseModule",
00167 NULL,
00168 NULL,
00169 0,
00170 0,
00171 CW_USEDEFAULT,
00172 CW_USEDEFAULT,
00173 NULL,
00174 NULL,
00175 NULL,
00176 NULL
00177 );
00178
00179 if (hWndSpaceMouse == NULL)
00180 {
00181 LOG_ACE_ERROR("ot:SpaceMouseModule Could not create message-only window.\n");
00182 exit(1);
00183 }
00184
00185 SiOpenWinInit (&oData, hWndSpaceMouse);
00186 SiSetUiMode(devHdl, SI_UI_ALL_CONTROLS);
00187
00188
00189
00190 if ( (devHdl = SiOpen ("ot_spacemouse", SI_ANY_DEVICE, SI_NO_MASK,
00191 SI_EVENT, &oData)) == NULL)
00192 {
00193
00194 SiTerminate();
00195 ACE_LOG_INFO("ot:Could not fetch device handle for SpaceMouse.\n");
00196
00197 initialized = 0;
00198 exit(1);
00199 }
00200
00201 initialized = 1;
00202 init = 1;
00203
00204
00205
00206
00207
00208
00209
00210 }
00211
00212
00213 while(stop == 0)
00214 {
00215 processMessages();
00216 }
00217 }
00218
00219
00220 void SpaceMouseModule::pushEvent()
00221 {
00222 SpaceMouseSource *source;
00223
00224 if( isInitialized() == 1 )
00225 {
00226 for( NodeVector::iterator it = nodes.begin(); it != nodes.end(); it++ )
00227 {
00228 source = (SpaceMouseSource *) *it;
00229
00230 lock();
00231 if (source->changed == 1)
00232 {
00233 source->event = source->tmpEvent;
00234 source->changed = 0;
00235 unlock();
00236 source->push();
00237 }
00238 else
00239 unlock();
00240 }
00241 }
00242 }
00243
00244
00245
00246 void SpaceMouseModule::processMessages()
00247 {
00248 MSG msg;
00249 BOOL handled;
00250 SiSpwEvent Event;
00251 SiGetEventData EData;
00252
00253 int num;
00254 unsigned short newBut;
00255 float newPosX;
00256 float newPosY;
00257 float newPosZ;
00258 float newRotX;
00259 float newRotY;
00260 float newRotZ;
00261
00262 if( isInitialized() == 1 )
00263 {
00264
00265 WaitMessage();
00266 while ( PeekMessage( &msg, hWndSpaceMouse, 0, 0, PM_REMOVE ) )
00267 {
00268
00269 handled = SPW_FALSE;
00270
00271
00272 SiGetEventWinInit(&EData, msg.message, msg.wParam, msg.lParam);
00273
00274
00275 if (SiGetEvent (devHdl, 0, &EData, &Event) == SI_IS_EVENT)
00276 {
00277 if (Event.type == SI_MOTION_EVENT)
00278 {
00279
00280
00281 newPosX = Event.u.spwData.mData[SI_TX] / 330.0;
00282 newPosY = Event.u.spwData.mData[SI_TY] / 330.0;
00283 newPosZ = -Event.u.spwData.mData[SI_TZ] / 330.0;
00284 newRotX = (Event.u.spwData.mData[SI_RX] / 330.0)*SPW_PI/2.0;
00285 newRotY = (Event.u.spwData.mData[SI_RY] / 330.0)*SPW_PI/2.0;
00286 newRotZ = -(Event.u.spwData.mData[SI_RZ] / 330.0)*SPW_PI/2.0;
00287
00288 for( NodeVector::iterator it = nodes.begin(); it != nodes.end(); it++ )
00289 {
00290 lock();
00291 SpaceMouseSource * source = (SpaceMouseSource *)(*it);
00292 source->tmpEvent.getPosition()[0] = newPosX;
00293 source->tmpEvent.getPosition()[1] = newPosY;
00294 source->tmpEvent.getPosition()[2] = newPosZ;
00295 MathUtils::eulerToQuaternion(newRotX, newRotY, newRotZ, source->tmpEvent.orientation);
00296 MathUtils::normalizeQuaternion(source->tmpEvent.orientation);
00297 source->changed = 1;
00298 unlock();
00299 }
00300 }
00301 if (Event.type == SI_ZERO_EVENT)
00302 {
00303
00304 for( NodeVector::iterator it = nodes.begin(); it != nodes.end(); it++ )
00305 {
00306 lock();
00307 SpaceMouseSource * source = (SpaceMouseSource *)(*it);
00308 source->tmpEvent.getPosition()[0] = 0.0;
00309 source->tmpEvent.getPosition()[1] = 0.0;
00310 source->tmpEvent.getPosition()[2] = 0.0;
00311 source->tmpEvent.getOrientation()[0] = 0.0;
00312 source->tmpEvent.getOrientation()[1] = 0.0;
00313 source->tmpEvent.getOrientation()[2] = 0.0;
00314 source->tmpEvent.getOrientation()[3] = 1.0;
00315 source->changed = 1;
00316 unlock();
00317 }
00318 }
00319
00320 if (Event.type == SI_BUTTON_EVENT)
00321 {
00322 if ((num = SiButtonPressed (&Event)) != SI_NO_BUTTON)
00323 {
00324
00325 newBut = pow(2, num - 1);
00326 for( NodeVector::iterator it = nodes.begin(); it != nodes.end(); it++ )
00327 {
00328 lock();
00329 SpaceMouseSource * source = (SpaceMouseSource *)(*it);
00330 source->tmpEvent.getButton() |= newBut;
00331 source->changed = 1;
00332 unlock();
00333 }
00334 }
00335 if ((num = SiButtonReleased (&Event)) != SI_NO_BUTTON)
00336 {
00337
00338 newBut = pow(2, num - 1);
00339 for( NodeVector::iterator it = nodes.begin(); it != nodes.end(); it++ )
00340 {
00341 lock();
00342 SpaceMouseSource * source = (SpaceMouseSource *)(*it);
00343 source->tmpEvent.getButton() ^= newBut;
00344 source->changed = 1;
00345 unlock();
00346 }
00347 }
00348 }
00349
00350 handled = SPW_TRUE;
00351 }
00352
00353
00354 if (handled == SPW_FALSE)
00355 {
00356 TranslateMessage( &msg );
00357 DispatchMessage( &msg );
00358 }
00359 }
00360 }
00361 }
00362
00363 }
00364
00365 #else
00366 #pragma message(">>> no space mouse support")
00367 #endif
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383