![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsHapticManager.cpp 00003 00004 Inherited from class BasePointManager 00005 00006 SUKITTI PUNAK (05/07/2006) 00007 UPDATE (09/03/2010) 00008 ******************************************************************************/ 00009 #include "TAPsHapticManager.hpp" 00010 // Using Inclusion Model (i.e. definitions are included in declarations) 00011 // (this name.cpp is included in name.hpp) 00012 // Each friend is defined directly inside its declaration. 00013 00014 //----------------------------------------------------------------------------- 00015 // DEBUG ENABLE 00016 #ifdef TAPs_ENABLE_DEBUG 00017 #define DEBUG_MESSAGE_TAPs_HAPTIC_MANAGER_HPP 00018 #endif 00019 //----------------------------------------------------------------------------- 00020 00021 BEGIN_NAMESPACE_TAPs__Haptic 00022 //============================================================================= 00023 //----------------------------------------------------------------------------- 00024 template <typename T> const T HapticManager<T>::g_tSnapDistanceConst = 2.0; 00025 template <typename T> T HapticManager<T>::g_tSnapDistance = g_tSnapDistanceConst; 00026 //============================================================================= 00027 //----------------------------------------------------------------------------- 00028 // Default Constructor 00029 template <typename T> 00030 HapticManager<T>::HapticManager () 00031 : BaseHapticManager<T>(), 00032 m_pModelManager( NULL ), 00033 m_pViewManager( NULL ), 00034 m_iManipPoint( -1 ), 00035 m_uiPointNameBase( 0 ), 00036 m_uiEffectName( 0 ), 00037 m_tCursorScale( 1.0 ), 00038 m_uiCursorDisplayList( 0 ), 00039 m_hHD( HD_INVALID_HANDLE ), 00040 m_hHLRC( NULL ) 00041 { 00042 m_uiHapticObjects = 0; 00043 InitHapticData(); 00044 InitCursor(); 00045 } 00046 //----------------------------------------------------------------------------- 00047 // Constructor 00048 template <typename T> 00049 HapticManager<T>::HapticManager ( const char * toolOneFileName ) 00050 : BaseHapticManager<T>(), 00051 m_pModelManager( NULL ), 00052 m_pViewManager( NULL ), 00053 m_iManipPoint( -1 ), 00054 m_uiPointNameBase( 0 ), 00055 m_uiEffectName( 0 ), 00056 m_tCursorScale( 1.0 ), 00057 m_uiCursorDisplayList( 0 ), 00058 m_hHD( HD_INVALID_HANDLE ), 00059 m_hHLRC( NULL ) 00060 { 00061 m_uiHapticObjects = 0; 00062 InitHapticData(); 00063 InitCursor( toolOneFileName ); 00064 } 00065 //----------------------------------------------------------------------------- 00066 // Destructor 00067 template <typename T> 00068 HapticManager<T>::~HapticManager () 00069 {} 00070 //----------------------------------------------------------------------------- 00071 // Setup 00072 // creates a random cloud of points with random colors within a canonical view. 00073 template <typename T> 00074 void HapticManager<T>::Setup ( 00075 TAPs::OpenGL::BaseModelManager<T> * pModelManager, 00076 TAPs::OpenGL::BaseOpenGLViewManager<T> * pViewManager ) 00077 { 00078 m_pModelManager = pModelManager; 00079 assert( m_pModelManager ); 00080 m_pViewManager = pViewManager; 00081 assert( m_pViewManager ); 00082 //---------------------------------------------------------------- 00083 // Check Haptic Device 00084 HDErrorInfo error; 00085 m_hHD = hdInitDevice( HD_DEFAULT_DEVICE ); 00086 if ( HD_DEVICE_ERROR( error = hdGetError() ) ) { 00087 hduPrintError( stderr, &error, 00088 "Failed to initialize haptic device!" ); 00089 fprintf( stderr, "Press any key to exit" ); 00090 getchar(); 00091 exit(1); 00092 } 00093 //---------------------------------------------------------------- 00094 // Create a haptic context for the device. 00095 // The haptic context maintains the state that persists between 00096 // frame intervals and is used for haptic rendering. 00097 m_hHLRC = hlCreateContext( m_hHD ); 00098 hlMakeCurrent( m_hHLRC ); 00099 //---------------------------------------------------------------- 00100 // Enable optimization of the viewing parameters when rendering 00101 // geometry for OpenHaptics. 00102 //hlEnable(HL_HAPTIC_CAMERA_VIEW); 00103 //---------------------------------------------------------------- 00104 // Front face touchable only - no need to feel back faces 00105 hlTouchableFace( HL_FRONT ); 00106 //---------------------------------------------------------------- 00107 int iNumPoints = pModelManager->strandObj->GetNumberOfLinks() + 1; 00108 m_uiPointNameBase = hlGenShapes( iNumPoints + 2 ); 00109 for ( int i = 0; i < iNumPoints; ++i ) { 00110 // Register event callbacks for all of the points, so we can 00111 // be notified when the points are touched and untouched. 00112 HLuint uiPointName = m_uiPointNameBase + i; 00113 hlAddEventCallback( HL_EVENT_TOUCH, uiPointName, 00114 HL_CLIENT_THREAD, hlTouchCB, this ); 00115 hlAddEventCallback( HL_EVENT_UNTOUCH, uiPointName, 00116 HL_CLIENT_THREAD, hlTouchCB, this ); 00117 hlAddEventCallback( HL_EVENT_1BUTTONDOWN, uiPointName, 00118 HL_CLIENT_THREAD, hlButtonDownCB, this ); 00119 } 00120 //---------------------------------------------------------------- 00121 // Add a callback to handle button up in the collision thread 00122 hlAddEventCallback( HL_EVENT_1BUTTONUP, HL_OBJECT_ANY, 00123 HL_CLIENT_THREAD, hlButtonUpCB, this ); 00124 //---------------------------------------------------------------- 00125 // Add a callback to handle button down in the collision thread 00126 // hlAddEventCallback( HL_EVENT_1BUTTONDOWN, HL_OBJECT_ANY, 00127 // HL_COLLISION_THREAD, 00128 // buttonDownCollisionThreadCallback, NULL ); 00129 //---------------------------------------------------------------- 00130 // Start an ambient drag friction effect 00131 m_uiEffectName = hlGenEffects( 1 ); 00132 hlBeginFrame(); 00133 hlEffectd( HL_EFFECT_PROPERTY_GAIN, 0.4 ); 00134 hlEffectd( HL_EFFECT_PROPERTY_MAGNITUDE, 0.1 ); 00135 hlStartEffect( HL_EFFECT_FRICTION, m_uiEffectName ); 00136 hlEndFrame(); 00137 //---------------------------------------------------------------- 00138 // Tie Cursor with ToolOne of pModelManager (BaseModelManager) 00139 pModelManager->ToolOne = ToolOne; 00140 //---------------------------------------------------------------- 00141 } 00142 //----------------------------------------------------------------------------- 00143 // Cleanup 00144 template <typename T> 00145 void HapticManager<T>::Cleanup () 00146 { 00147 if ( m_hHLRC != NULL ) { 00148 assert( m_pModelManager ); 00149 hlBeginFrame(); 00150 hlStopEffect( m_uiEffectName ); 00151 hlEndFrame(); 00152 //------------------------------------------------------ 00153 hlDeleteEffects( m_uiEffectName, 1 ); 00154 int iNumPoints = m_pModelManager->strandObj->GetNumberOfLinks() + 1; 00155 for ( int i = 0; i < iNumPoints; ++i ) { 00156 // Unregister event callbacks for all of the points 00157 HLuint uiPointName = m_uiPointNameBase + i; 00158 hlRemoveEventCallback( HL_EVENT_TOUCH, uiPointName, 00159 HL_CLIENT_THREAD, hlTouchCB ); 00160 hlRemoveEventCallback( HL_EVENT_UNTOUCH, uiPointName, 00161 HL_CLIENT_THREAD, hlTouchCB ); 00162 hlRemoveEventCallback( HL_EVENT_1BUTTONDOWN, uiPointName, 00163 HL_CLIENT_THREAD, hlButtonDownCB ); 00164 } 00165 //------------------------------------------------------ 00166 hlRemoveEventCallback( HL_EVENT_1BUTTONUP, HL_OBJECT_ANY, 00167 HL_CLIENT_THREAD, hlButtonUpCB ); 00168 hlDeleteShapes( m_uiPointNameBase, iNumPoints ); 00169 hlMakeCurrent( NULL ); 00170 hlDeleteContext( m_hHLRC ); 00171 } 00172 //---------------------------------------------------------------- 00173 if ( m_hHD != HD_INVALID_HANDLE ) hdDisableDevice( m_hHD ); 00174 //---------------------------------------------------------------- 00175 // Deallocate the object ID(s) 00176 //hlDeleteShapes( m_uiHapticObjects, 2 ); 00177 } 00178 //----------------------------------------------------------------------------- 00179 // UpdateWorkSpace 00180 // uses the current OpenGL viewing transforms to initialize a transform for the 00181 // haptic device workspace so that it's properly mapped to world coordinates. 00182 template <typename T> 00183 void HapticManager<T>::UpdateWorkSpace () 00184 { 00185 GLdouble modelview[16]; 00186 GLdouble projection[16]; 00187 GLint viewport[4]; 00188 glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); 00189 glGetDoublev( GL_PROJECTION_MATRIX, projection ); 00190 glGetIntegerv( GL_VIEWPORT, viewport ); 00191 hlMatrixMode( HL_TOUCHWORKSPACE ); 00192 hlLoadIdentity(); 00193 // Use of a non-uniform workspace fit, which offer a maximal fit to the view 00194 // frustum but introduces non-uniform scale in the workspace. 00195 // This non-uniform scale is fine for point and line based haptics, like 00196 // this example, but can introduct distortion when actually feeling surfaces 00197 // (i.e. a sphere might feel like an ellipsoid) 00198 //hluFitWorkspaceNonUniform( projection ); 00199 hluFitWorkspace( projection ); 00200 } 00201 //----------------------------------------------------------------------------- 00202 // DrawCursor 00203 // displays a cursor using the current haptic device proxy transform and the 00204 // mapping between the workspace and the world coordinates 00205 template <typename T> 00206 void HapticManager<T>::DrawCursor ( OpenGL::Enum::DrawMode DM ) 00207 { 00208 static const double K_CURSOR_SIZE_PIXELS = 15; 00209 static const double K_CURSOR_RADIUS = 0.5; 00210 static const double K_CURSOR_HEIGHT = 5.0; 00211 static const int K_CURSOR_TESS = 15; 00212 HLdouble transform[16]; 00213 GLUquadricObj *qObj = NULL; 00214 //-------------------------------------------------------------------- 00215 glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT ); 00216 glPushMatrix(); 00217 //-------------------------------------------------------------------- 00218 if ( !m_uiCursorDisplayList ) { 00219 m_uiCursorDisplayList = glGenLists( 2 ); 00220 //------------------------------------------------------------ 00221 // Cone Cursor 00222 glNewList( m_uiCursorDisplayList, GL_COMPILE ); 00223 qObj = gluNewQuadric(); 00224 glScaled( 0.2, 0.2, 0.2 ); 00225 //gluSphere( qObj, K_CURSOR_RADIUS, K_CURSOR_TESS, K_CURSOR_TESS ); 00226 gluCylinder( qObj, 0.0, K_CURSOR_RADIUS, K_CURSOR_HEIGHT, 00227 K_CURSOR_TESS, K_CURSOR_TESS ); 00228 glTranslated( 0.0, 0.0, K_CURSOR_HEIGHT ); 00229 gluCylinder( qObj, K_CURSOR_RADIUS, 0.0, K_CURSOR_HEIGHT/5.0, 00230 K_CURSOR_TESS, K_CURSOR_TESS ); 00231 gluDeleteQuadric( qObj ); 00232 glEndList(); 00233 //------------------------------------------------------------ 00234 // Tool One Cursor 00235 glNewList( m_uiCursorDisplayList+1, GL_COMPILE ); 00236 ToolOne->DisplayGL( DM ); 00237 glEndList(); 00238 } 00239 //-------------------------------------------------------------------- 00240 // Apply the local position/rotation transform of the haptic device 00241 // proxy 00242 // I.e. get the proxy transform in world coordinates 00243 hlGetDoublev( HL_PROXY_TRANSFORM, transform ); 00244 //glCallList( m_uiCursorDisplayList+1 ); 00245 Matrix4x4<T> tooloneTransformMatrix( 00246 transform[ 0], transform[ 4], transform[ 8], transform[12], 00247 transform[ 1], transform[ 5], transform[ 9], transform[13], 00248 transform[ 2], transform[ 6], transform[10], transform[14], 00249 transform[ 3], transform[ 7], transform[11], transform[15] 00250 ); 00251 ToolOne->GetTransform().SetMatrixTransform( tooloneTransformMatrix ); 00252 glMultMatrixd( transform ); 00253 //-------------------------------------------------------------------- 00254 // Apply the local cursor scale factor 00255 //double scale = m_pViewManager->GetWindowToWorldScale() * K_CURSOR_SIZE_PIXELS; 00256 //glScaled( scale, scale, scale ); 00257 //glEnable( GL_NORMALIZE ); 00258 //ToolOne->DisplayGL( DM ); 00259 glCallList( m_uiCursorDisplayList+1 ); // Tool One Cursor 00260 //ToolOne->GetBVHTree()->DrawByOpenGL( 0, 0, 10 ); 00261 //glPushAttrib( GL_DEPTH_BUFFER_BIT ); 00263 //glEnable( GL_COLOR_MATERIAL ); 00264 //glColor3f( 0.0, 0.5, 1.0 ); 00265 //glCallList( m_uiCursorDisplayList ); // Cone Cursor 00266 //glDisable( GL_COLOR_MATERIAL ); 00267 //glPopAttrib(); 00268 //-------------------------------------------------------------------- 00269 glPopMatrix(); 00270 glPopAttrib(); 00271 } 00272 //----------------------------------------------------------------------------- 00273 // DrawTooltipSphere 00274 // displays a tooltip sphere using the current haptic device proxy transform and the 00275 // mapping between the workspace and the world coordinates 00276 template <typename T> 00277 void HapticManager<T>::DrawTooltipSphere ( OpenGL::Enum::DrawMode DM ) 00278 { 00279 //-------------------------------------------------------------------- 00280 // Apply the local position/rotation transform of the haptic device 00281 // proxy 00282 // I.e. get the proxy transform in world coordinates 00283 hlGetDoublev( HL_PROXY_TRANSFORM, transform ); 00284 //glCallList( m_uiCursorDisplayList+1 ); 00285 Matrix4x4<T> tooloneTransformMatrix( 00286 transform[ 0], transform[ 4], transform[ 8], transform[12], 00287 transform[ 1], transform[ 5], transform[ 9], transform[13], 00288 transform[ 2], transform[ 6], transform[10], transform[14], 00289 transform[ 3], transform[ 7], transform[11], transform[15] 00290 ); 00291 //-------------------------------------------------------------------- 00292 glPushAttrib( GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT ); 00293 glPushMatrix(); 00294 //-------------------------------------------------------------------- 00295 glMultMatrixd( transform ); 00296 glTranslatef( 0, 0, ToolOneSphereTip[3] / 2 ); 00297 pModelManager->ReturnPtrToOpenGLUsefulObj()->DrawSphere( 00298 Vector3<Real>( ToolOneSphereTip[3], ToolOneSphereTip[3], ToolOneSphereTip[3] ) ); 00299 //-------------------------------------------------------------------- 00300 glPopMatrix(); 00301 glPopAttrib(); 00302 } 00303 //----------------------------------------------------------------------------- 00304 // DrawHapticObjects 00305 // DESC: Uses OpenGL to draw objects that can be feel by the haptic device. 00306 template <typename T> 00307 void HapticManager<T>::DrawHapticObjects ( OpenGL::Enum::DrawMode DM ) 00308 { 00309 //--------------------------------------------------------------- 00310 hlBeginFrame(); 00311 //--------------------------------------------------------------- 00312 FeelPoints(); 00313 FeelObjects(); 00314 //--------------------------------------------------------------- 00315 hlEndFrame(); 00316 } 00317 //----------------------------------------------------------------------------- 00318 // FeelObjects 00319 // DESC: Uses OpenGL to draw objects that can be feel by the haptic device. 00320 template <typename T> 00321 void HapticManager<T>::FeelObjects ( OpenGL::Enum::DrawMode DM ) 00322 { 00323 //hlBeginFrame(); 00324 int iNumPoints = m_pModelManager->strandObj->GetNumberOfLinks() + 1; 00325 hlBeginShape( HL_SHAPE_FEEDBACK_BUFFER, m_uiPointNameBase + iNumPoints ); 00326 hlTouchModel( HL_CONTACT ); 00327 hlMaterialf( HL_FRONT, HL_STIFFNESS, 0.5 ); 00328 hlMaterialf( HL_FRONT, HL_DAMPING, 0.5 ); 00329 hlMaterialf( HL_FRONT, HL_STATIC_FRICTION, 0.3 ); 00330 hlMaterialf( HL_FRONT, HL_DYNAMIC_FRICTION, 0.2 ); 00331 // Use OpenGL commands to create geometry. 00332 m_pModelManager->leftTube->DisplayGL( DM ); 00333 hlEndShape(); 00334 hlBeginShape( HL_SHAPE_FEEDBACK_BUFFER, m_uiPointNameBase + iNumPoints + 1); 00335 hlTouchModel( HL_CONTACT ); 00336 hlMaterialf( HL_FRONT, HL_STIFFNESS, 0.7 ); 00337 hlMaterialf( HL_FRONT, HL_DAMPING, 0.1 ); 00338 hlMaterialf( HL_FRONT, HL_STATIC_FRICTION, 0.2 ); 00339 hlMaterialf( HL_FRONT, HL_DYNAMIC_FRICTION, 0.3 ); 00340 // Use OpenGL commands to create geometry. 00341 m_pModelManager->rightTube->DisplayGL( DM ); 00342 hlEndShape(); 00343 //------------------------------------------------------------------- 00344 //hlEndFrame(); 00345 } 00346 //----------------------------------------------------------------------------- 00347 // FeelPoints 00348 // draw points and lines for haptic device 00349 template <typename T> 00350 void HapticManager<T>::FeelPoints () 00351 { 00352 //hlBeginFrame(); 00353 //------------------------------------------------------------------- 00354 int iNumPoints = m_pModelManager->strandObj->GetNumberOfLinks() + 1; 00355 for ( int i = 0; i < iNumPoints; ++i ) { 00356 //------------------------------------------------------ 00357 // Don't haptically draw a selected point, since it's 00358 // being manipulated 00359 if ( m_iManipPoint == i ) continue; 00360 //------------------------------------------------------ 00361 // Draw a shape that represents the projection of the 00362 // points to the near and far planes 00363 //hlHinti( HL_SHAPE_FEEDBACK_BUFFER_VERTICES, 3 ); 00364 hlBeginShape( HL_SHAPE_FEEDBACK_BUFFER, m_uiPointNameBase + i ); 00365 hlTouchModel( HL_CONSTRAINT ); 00366 hlTouchModelf( HL_SNAP_DISTANCE, g_tSnapDistance ); 00367 //hlMaterialf( HL_FRONT_AND_BACK, HL_STIFFNESS, 0.5 ); 00368 hlMaterialf( HL_FRONT_AND_BACK, HL_STIFFNESS, 0.5 ); 00369 hlMaterialf( HL_FRONT_AND_BACK, HL_DAMPING, 0.25 ); 00370 hlMaterialf( HL_FRONT_AND_BACK, HL_STATIC_FRICTION, 0.5 ); 00371 hlMaterialf( HL_FRONT_AND_BACK, HL_DYNAMIC_FRICTION, 0.25 ); 00372 TAPs::Vector3<T> pt = m_pModelManager->strandObj->GetPointPosition(i); 00373 //--------------------------------------------- 00374 // Draw a line that represents the projection 00375 // of the point to the near and far of the view 00376 // volume 00377 TAPs::Vector3<T> winPt, nearPt, farPt; 00378 m_pViewManager->ToScreen( pt, winPt ); 00379 winPt[2] = 0; 00380 m_pViewManager->FromScreen( winPt, nearPt ); 00381 winPt[2] = 1; 00382 m_pViewManager->FromScreen( winPt, farPt ); 00383 glBegin( GL_LINES ); 00384 glVertex3dv( nearPt ); 00385 glVertex3dv( farPt ); 00386 glEnd(); 00387 glPointSize( 10 ); 00388 glBegin( GL_POINTS ); 00389 glVertex3dv( pt ); 00390 glEnd(); 00391 hlEndShape(); 00392 } 00393 //------------------------------------------------------------------- 00394 // Call any event callbacks that have been triggered 00395 hlCheckEvents(); 00396 //------------------------------------------------------------------- 00397 //hlEndFrame(); 00398 } 00399 //----------------------------------------------------------------------------- 00400 // IsManipulating 00401 template <typename T> 00402 bool HapticManager<T>::IsManipulating () const 00403 { 00404 return m_iManipPoint != -1; 00405 } 00406 //----------------------------------------------------------------------------- 00407 //============================================================================= 00408 // Helper Fn(s) 00409 //----------------------------------------------------------------------------- 00410 // StartManipulating 00411 template <typename T> 00412 void HapticManager<T>::StartManipulating ( int i ) 00413 { 00414 assert( !IsManipulating() ); 00415 assert( 0 <= i && i <= m_pModelManager->strandObj->GetNumberOfLinks() ); 00416 hlAddEventCallback( HL_EVENT_MOTION, HL_OBJECT_ANY, 00417 HL_CLIENT_THREAD, hlMotionCB, this ); 00418 m_iManipPoint = i; 00419 static TAPs::Vector3<T> proxyPos; 00420 //---------------------------------------------------------------- 00421 // Store off proxy position so we can compute how much it moves each 00422 // frame which is how much the drag object should also move 00423 hlGetDoublev( HL_PROXY_POSITION, proxyPos ); 00424 //---------------------------------------------------------------- 00425 // Current Rotation at the start of dragging 00426 hlGetDoublev( HL_PROXY_TRANSFORM, startDragProxyTransform ); 00427 // Current Proxy Rotation (Transposed) 00428 startDragProxyRotation = Matrix3x3<T>( 00429 startDragProxyTransform[0], startDragProxyTransform[1], startDragProxyTransform[2], 00430 startDragProxyTransform[4], startDragProxyTransform[5], startDragProxyTransform[6], 00431 startDragProxyTransform[8], startDragProxyTransform[9], startDragProxyTransform[10] 00432 ); 00433 // [Start Drag Rotation] = [Current Proxy Rotation]transpose * [Needle Rotation] 00434 //startDragProxyRotation *= m_pModelManager->strandObj->GetLeftNeedleRotationMatrix(); 00435 //startDragProxyRotation *= m_pModelManager->strandObj->GetTransform().GetMatrixTransform().GetMatrix3x3(); 00436 startDragProxyRotation *= m_pModelManager->strandObj->GetLeftNeedleTransform().GetMatrixRotation(); 00437 //---------------------------------------------------------------- 00438 // Store off initial position and orientation of drag object 00439 //startDragObjTransform = draggableObjects[currentDragObj].transform; 00440 //---------------------------------------------------------------- 00441 00442 //TAPs::Vector3<T> offsetWC = m_pModelManager->strandObj->GetPointPosition(i) - proxyPos; 00443 //---------------------------------------------------------------- 00444 // This is a very useful technique for offsetting the user in 00445 // space so that they begin their manipulation exactly at the 00446 // location of the selected point. 00447 //PushWorkSpaceOffset( offsetWC ); 00448 } 00449 //----------------------------------------------------------------------------- 00450 // StopManipulating 00451 template <typename T> 00452 void HapticManager<T>::StopManipulating () 00453 { 00454 assert( IsManipulating() ); 00455 hlRemoveEventCallback( HL_EVENT_MOTION, HL_OBJECT_ANY, 00456 HL_CLIENT_THREAD, hlMotionCB ); 00457 m_iManipPoint = -1; 00458 PopWorkSpaceOffset(); 00459 } 00460 //----------------------------------------------------------------------------- 00461 // UpdateManipPoint 00462 template <typename T> 00463 void HapticManager<T>::UpdateManipPoint ( 00464 const TAPs::Vector3<T> & proxyPos, 00465 const TAPs::Matrix4x4<T> & proxyTransform ) 00466 { 00467 // Current Rotation of this motion 00468 TAPs::Matrix3x3<T> currRotation( 00469 proxyTransform[0], proxyTransform[4], proxyTransform[8], 00470 proxyTransform[1], proxyTransform[5], proxyTransform[9], 00471 proxyTransform[2], proxyTransform[6], proxyTransform[10] 00472 ); 00473 //-------------------------------------------------------------------- 00474 TAPs::Matrix3x3<T> rotation = currRotation * startDragProxyRotation; 00475 //rotation = m_pModelManager->strandObj->m_mRotMatForLeftNeedle * rotation; 00476 //rotation = m_pModelManager->strandObj->m_mRotMatForLeftNeedle.GetTranspose(); 00477 m_pModelManager->strandObj->SetPointPosition( m_iManipPoint, proxyPos, rotation ); 00478 if ( m_pModelManager->CollisionDetection() ) { 00479 //gModelManager->strandObj->RestorePreviousPositions(); 00480 //gModelManager->strandObj->SetPointPosition( gManipPoint, oldPosition ); 00481 } 00482 } 00483 //----------------------------------------------------------------------------- 00484 // PushWorkSpaceOffset 00485 template <typename T> 00486 void HapticManager<T>::PushWorkSpaceOffset ( const TAPs::Vector3<T> &offsetWC ) 00487 { 00488 const TAPs::Matrix4x4<T> &mWorldToView = m_pViewManager->GetViewTransform(); 00489 TAPs::Matrix4x4<T> mViewToTouch; 00490 TAPs::Matrix4x4<T> mTouchToWorkSpace; 00491 hlGetDoublev( HL_VIEWTOUCH_MATRIX, mViewToTouch ); 00492 hlGetDoublev( HL_TOUCHWORKSPACE_MATRIX, mTouchToWorkSpace ); 00493 TAPs::Matrix4x4<T> mWorldToWorkSpace = mWorldToView * mViewToTouch * mTouchToWorkSpace; 00494 TAPs::Vector3<T> offsetWS; 00495 //mWorldToWorkSpace.multDirMatrix( offsetWC, offsetWS ); 00496 offsetWS[0] = mWorldToWorkSpace[ 0]*offsetWC[0] + 00497 mWorldToWorkSpace[ 4]*offsetWC[1] + 00498 mWorldToWorkSpace[ 8]*offsetWC[2]; 00499 offsetWS[1] = mWorldToWorkSpace[ 1]*offsetWC[0] + 00500 mWorldToWorkSpace[ 5]*offsetWC[1] + 00501 mWorldToWorkSpace[ 9]*offsetWC[2]; 00502 offsetWS[2] = mWorldToWorkSpace[ 2]*offsetWC[0] + 00503 mWorldToWorkSpace[ 6]*offsetWC[1] + 00504 mWorldToWorkSpace[10]*offsetWC[2]; 00505 //---------------------------------------------------------------- 00506 // Apply the translation in the local coordinates of the workspace 00507 //TAPs::Matrix4x4<T> mTranslateWS = hduMatrix::createTranslation( -offsetWS ); 00508 TAPs::Matrix4x4<T> mTranslateWS( 1, 0, 0, 0, 00509 0, 1, 0, 0, 00510 0, 0, 1, 0, 00511 -offsetWS[0], -offsetWS[1], -offsetWS[2], 1 ); 00512 mTouchToWorkSpace.MultRight( mTranslateWS ); 00513 hlMatrixMode( HL_TOUCHWORKSPACE ); 00514 hlPushMatrix(); 00515 hlLoadMatrixd( mTouchToWorkSpace ); 00516 } 00517 //----------------------------------------------------------------------------- 00518 // PopWorkSpaceOffset 00519 template <typename T> 00520 void HapticManager<T>::PopWorkSpaceOffset () 00521 { 00522 hlMatrixMode( HL_TOUCHWORKSPACE ); 00523 hlPopMatrix(); 00524 } 00525 //----------------------------------------------------------------------------- 00526 // GetPointIndexFromTouchID 00527 template <typename T> 00528 int HapticManager<T>::GetPointIndexFromTouchID ( HLuint id ) const 00529 { 00530 int i = id - m_uiPointNameBase; 00531 assert( 0 <= i && i <= m_pModelManager->strandObj->GetNumberOfLinks() ); 00532 return i; 00533 } 00534 //----------------------------------------------------------------------------- 00535 // 00536 template <typename T> 00537 void HLCALLBACK HapticManager<T>::hlTouchCB ( 00538 HLenum event, HLuint object, HLenum thread, 00539 HLcache *cache, void *userData ) 00540 { 00541 HapticManager *pThis = static_cast<HapticManager *>(userData); 00542 int i = pThis->GetPointIndexFromTouchID( object ); 00543 if ( event == HL_EVENT_TOUCH ) { 00544 //pThis->m_pModelManager->SetPointHighlighted( i, true ); 00545 } 00546 else if ( event == HL_EVENT_UNTOUCH ) { 00547 //pThis->m_pModelManager->SetPointHighlighted( i, false ); 00548 } 00549 } 00550 //----------------------------------------------------------------------------- 00551 // 00552 template <typename T> 00553 void HLCALLBACK HapticManager<T>::hlButtonDownCB ( 00554 HLenum event, HLuint object, HLenum thread, 00555 HLcache *cache, void *userData ) 00556 { 00557 HapticManager *pThis = static_cast<HapticManager *>(userData); 00558 int i = pThis->GetPointIndexFromTouchID( object ); 00559 if ( !pThis->IsManipulating() ) { 00560 //glutIdleFunc( NULL ); 00561 //pThis->m_pModelManager->strandObj->SetFixStatusOfPtNo( i, true ); 00562 pThis->StartManipulating( i ); 00563 g_tSnapDistance = 0.0; 00564 } 00565 } 00566 //----------------------------------------------------------------------------- 00567 // 00568 template <typename T> 00569 void HLCALLBACK HapticManager<T>::hlButtonUpCB ( 00570 HLenum event, HLuint object, HLenum thread, 00571 HLcache *cache, void *userData ) 00572 { 00573 HapticManager *pThis = static_cast<HapticManager *>(userData); 00574 if ( pThis->IsManipulating() ) { 00575 //pThis->m_pModelManager->strandObj->SetFixStatusOfPtNo( pThis->m_iManipPoint, false ); 00576 pThis->StopManipulating(); 00577 g_tSnapDistance = g_tSnapDistanceConst; 00578 } 00579 } 00580 //----------------------------------------------------------------------------- 00581 // 00582 template <typename T> 00583 void HLCALLBACK HapticManager<T>::hlMotionCB ( 00584 HLenum event, HLuint object, HLenum thread, 00585 HLcache *cache, void *userData ) 00586 { 00587 HapticManager *pThis = static_cast<HapticManager *>(userData); 00588 00589 if ( pThis->IsManipulating() ) { 00590 // Get the position of the proxy when the motion was detected 00591 TAPs::Vector3<T> proxyPos; 00592 TAPs::Matrix4x4<T> proxyTransform; 00593 hlCacheGetDoublev( cache, HL_PROXY_POSITION, proxyPos ); 00594 hlGetDoublev( HL_PROXY_TRANSFORM, proxyTransform ); 00595 pThis->UpdateManipPoint( proxyPos, proxyTransform ); 00596 } 00597 } 00598 //----------------------------------------------------------------------------- 00599 // InitHapticData 00600 // initializes haptic data 00601 template <typename T> 00602 void HapticManager<T>::InitHapticData () 00603 { 00604 // Flag for enabling/disabling axis snap on drag 00605 m_bHapticAxisSnapOn = true; 00606 // Flag for enabling/disabling rotation 00607 m_bHapticRotateOn = true; 00608 00609 // Generate ID's for haptic objects 00610 //m_uiHapticObjects = hlGenShapes(2); 00611 } 00612 //----------------------------------------------------------------------------- 00613 // InitCursor 00614 // initializes a cursor reading from a data file 00615 template <typename T> 00616 void HapticManager<T>::InitCursor () 00617 { 00618 char toolOneFile[] = "Data/3dsMax/Tweezers.ASE"; 00619 InitCursor( toolOneFile ); 00620 } 00621 //----------------------------------------------------------------------------- 00622 // InitCursor 00623 // initializes a cursor reading from a data file 00624 template <typename T> 00625 void HapticManager<T>::InitCursor ( const char * toolOneFile ) 00626 { 00627 //---------------------------------------------------------------- 00628 // Set Center, then Rotations of x,y,z axes, and Scale 00629 T scale = 1.5; 00630 Vector3<T> vCenter( 0, 0, 0 ); 00631 Vector3<T> vRotationAngle( 0, 0, 0 ); 00632 Vector3<T> vScale( scale, scale, scale); 00633 //---------------------------------------------------------------- 00634 ToolOne = new TAPs::OpenGL::OpenGLHalfEdgeModel<T>(); 00635 //ToolRight = new OpenGLHalfEdgeModel<T>(); 00636 //---------------------------------------------------------------- 00637 if ( !ReadModels<Real>::readFile( toolOneFile, ToolOne ) ) { 00638 std::cout << "ERROR: In File Reading! for Tool#1" << std::endl; 00639 exit ( EXIT_FAILURE ); 00640 } 00641 else { 00642 std::cout << "Create Tool#1" << std::endl; 00643 ToolOne->SetMaterial( OpenGL::Enum::METAL_SILVER ); 00644 ToolOne->GetTransform().SetTranslation( vCenter ); 00645 ToolOne->GetTransform().SetRotationAngles( vRotationAngle ); 00646 ToolOne->GetTransform().SetScale( vScale ); 00647 ToolOne->TransformByTranslationRatationAndScale(); 00648 ToolOne->BuildBVHTree( TAPs::Enum::BVH_TREE_BINARY_SPHERE ); 00649 } 00650 } 00651 //----------------------------------------------------------------------------- 00652 //============================================================================= 00653 END_NAMESPACE_TAPs__Haptic 00654 //----------------------------------------------------------------------------- 00655 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00656 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8