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 #include "mitkAffineInteractor3D.h"
00020 #include "mitkPointOperation.h"
00021 #include "mitkPositionEvent.h"
00022 #include "mitkStatusBar.h"
00023 #include "mitkDataNode.h"
00024 #include "mitkInteractionConst.h"
00025 #include "mitkAction.h"
00026 #include "mitkStateEvent.h"
00027 #include "mitkOperationEvent.h"
00028 #include "mitkUndoController.h"
00029 #include "mitkStateMachineFactory.h"
00030 #include "mitkStateTransitionOperation.h"
00031 #include "mitkBaseRenderer.h"
00032 #include "mitkRenderingManager.h"
00033 #include "mitkRotationOperation.h"
00034
00035 #include <vtkRenderWindow.h>
00036 #include <vtkRenderWindowInteractor.h>
00037 #include <vtkInteractorStyle.h>
00038 #include <vtkRenderer.h>
00039 #include <vtkCamera.h>
00040 #include <vtkPoints.h>
00041 #include <vtkPointData.h>
00042 #include <vtkDataArray.h>
00043
00044
00045 namespace mitk
00046 {
00047
00048
00049
00050 AffineInteractor3D
00051 ::AffineInteractor3D(const char * type, DataNode* dataNode, int )
00052 : Interactor( type, dataNode ),
00053 m_Precision( 6.5 ),
00054 m_InteractionMode( INTERACTION_MODE_TRANSLATION )
00055 {
00056 m_OriginalGeometry = Geometry3D::New();
00057
00058
00059 m_ObjectNormal[0] = 0.0;
00060 m_ObjectNormal[1] = 0.0;
00061 m_ObjectNormal[2] = 1.0;
00062 }
00063
00064
00065 AffineInteractor3D::~AffineInteractor3D()
00066 {
00067 }
00068
00069
00070 void AffineInteractor3D::SetInteractionMode( unsigned int interactionMode )
00071 {
00072 m_InteractionMode = interactionMode;
00073 }
00074
00075
00076 void AffineInteractor3D::SetInteractionModeToTranslation()
00077 {
00078 m_InteractionMode = INTERACTION_MODE_TRANSLATION;
00079 }
00080
00081
00082 void AffineInteractor3D::SetInteractionModeToRotation()
00083 {
00084 m_InteractionMode = INTERACTION_MODE_ROTATION;
00085 }
00086
00087
00088 unsigned int AffineInteractor3D::GetInteractionMode() const
00089 {
00090 return m_InteractionMode;
00091 }
00092
00093
00094 void AffineInteractor3D::SetPrecision( ScalarType precision )
00095 {
00096 m_Precision = precision;
00097 }
00098
00099
00100 float AffineInteractor3D
00101 ::CanHandleEvent(StateEvent const* stateEvent) const
00102 {
00103 float returnValue = 0.5;
00104
00105
00106
00107
00108 DisplayPositionEvent const *disPosEvent =
00109 dynamic_cast <const DisplayPositionEvent *> (stateEvent->GetEvent());
00110
00111
00112 if (disPosEvent == NULL)
00113 {
00114
00115 if (this->GetCurrentState()->GetTransition(stateEvent->GetId())!=NULL)
00116 {
00117 return 0.5;
00118 }
00119 else
00120 {
00121 return 0.0;
00122 }
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132 if (this->GetCurrentState()->GetTransition(stateEvent->GetId())!=NULL)
00133 {
00134 returnValue = 0.5;
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 return returnValue;
00158 }
00159
00160
00161 bool AffineInteractor3D
00162 ::ExecuteAction( Action *action, StateEvent const *stateEvent )
00163 {
00164 bool ok = false;
00165
00166
00167 BaseData *data = m_DataNode->GetData();
00168 if ( data == NULL )
00169 {
00170 MITK_ERROR << "No data object present!";
00171 return ok;
00172 }
00173
00174
00175 const Event *event = stateEvent->GetEvent();
00176 BaseRenderer *renderer = NULL;
00177 vtkRenderWindow *renderWindow = NULL;
00178 vtkRenderWindowInteractor *renderWindowInteractor = NULL;
00179 vtkRenderer *currentVtkRenderer = NULL;
00180 vtkCamera *camera = NULL;
00181
00182 if ( event != NULL )
00183 {
00184 renderer = event->GetSender();
00185 if ( renderer != NULL )
00186 {
00187 renderWindow = renderer->GetRenderWindow();
00188 if ( renderWindow != NULL )
00189 {
00190 renderWindowInteractor = renderWindow->GetInteractor();
00191 if ( renderWindowInteractor != NULL )
00192 {
00193 currentVtkRenderer = renderWindowInteractor
00194 ->GetInteractorStyle()->GetCurrentRenderer();
00195 if ( currentVtkRenderer != NULL )
00196 {
00197 camera = currentVtkRenderer->GetActiveCamera();
00198 }
00199 }
00200 }
00201 }
00202 }
00203
00204
00205 const DisplayPositionEvent *dpe =
00206 dynamic_cast< const DisplayPositionEvent * >( stateEvent->GetEvent() );
00207 if ( dpe != NULL )
00208 {
00209 m_CurrentPickedPoint = dpe->GetWorldPosition();
00210 m_CurrentPickedDisplayPoint = dpe->GetDisplayPosition();
00211 }
00212
00213
00214 int timeStep = 0;
00215 ScalarType timeInMS = 0.0;
00216 if ( renderer != NULL )
00217 {
00218 timeStep = renderer->GetTimeStep( data );
00219 timeInMS = renderer->GetTime();
00220 }
00221
00222
00223 Surface *surface = dynamic_cast< Surface * >( data );
00224 vtkPolyData *polyData = NULL;
00225 if ( surface != NULL )
00226 {
00227 polyData = surface->GetVtkPolyData( timeStep );
00228
00229
00230 vtkPointData *pointData = polyData->GetPointData();
00231 if ( pointData != NULL )
00232 {
00233 vtkDataArray *normal = polyData->GetPointData()->GetVectors( "planeNormal" );
00234 if ( normal != NULL )
00235 {
00236 m_ObjectNormal[0] = normal->GetComponent( 0, 0 );
00237 m_ObjectNormal[1] = normal->GetComponent( 0, 1 );
00238 m_ObjectNormal[2] = normal->GetComponent( 0, 2 );
00239 }
00240 }
00241 }
00242
00243
00244 m_Geometry = data->GetGeometry( timeStep );
00245
00246
00247
00248
00249 data->Expand( timeStep+1 );
00250
00251
00252 switch (action->GetActionId())
00253 {
00254 case AcDONOTHING:
00255 ok = true;
00256 break;
00257
00258
00259 case AcCHECKOBJECT:
00260 {
00261
00262 if ( renderWindowInteractor != NULL )
00263 {
00264 renderWindowInteractor->Enable();
00265 }
00266
00267
00268 const DisplayPositionEvent *dpe =
00269 dynamic_cast< const DisplayPositionEvent * >( stateEvent->GetEvent() );
00270 if ( dpe == NULL )
00271 {
00272 ok = true;
00273 break;
00274 }
00275
00276
00277 DataNode *pickedNode = dpe->GetPickedObjectNode();
00278 StateEvent *newStateEvent;
00279 if ( pickedNode == m_DataNode )
00280 {
00281
00282 newStateEvent = new StateEvent( EIDYES );
00283 }
00284 else
00285 {
00286
00287 newStateEvent = new StateEvent( EIDNO );
00288 }
00289
00290 this->HandleEvent( newStateEvent );
00291
00292 ok = true;
00293 break;
00294 }
00295
00296 case AcDESELECTOBJECT:
00297 {
00298
00299 m_DataNode->SetColor( 1.0, 1.0, 1.0 );
00300 RenderingManager::GetInstance()->RequestUpdateAll();
00301
00302
00303 this->ColorizeSurface( polyData,
00304 m_CurrentPickedPoint, -1.0 );
00305
00306 ok = true;
00307 break;
00308 }
00309
00310 case AcSELECTPICKEDOBJECT:
00311 {
00312
00313 m_DataNode->SetColor( 1.0, 0.0, 0.0 );
00314 RenderingManager::GetInstance()->RequestUpdateAll();
00315
00316
00317 this->ColorizeSurface( polyData,
00318 m_CurrentPickedPoint, 0.0 );
00319
00320 ok = true;
00321 break;
00322 }
00323
00324 case AcINITMOVE:
00325 {
00326
00327 if ( renderWindowInteractor != NULL )
00328 {
00329 renderWindowInteractor->Disable();
00330 }
00331
00332
00333 const DisplayPositionEvent *dpe =
00334 dynamic_cast< const DisplayPositionEvent * >( stateEvent->GetEvent() );
00335 if ( dpe == NULL )
00336 {
00337 ok = true;
00338 break;
00339 }
00340
00341
00342
00343 m_InitialPickedPoint = m_CurrentPickedPoint;
00344 m_InitialPickedDisplayPoint = m_CurrentPickedDisplayPoint;
00345
00346 if ( currentVtkRenderer != NULL )
00347 {
00348 vtkInteractorObserver::ComputeDisplayToWorld(
00349 currentVtkRenderer,
00350 m_InitialPickedDisplayPoint[0],
00351 m_InitialPickedDisplayPoint[1],
00352 0.0,
00353 m_InitialPickedPointWorld );
00354 }
00355
00356
00357
00358 data->UpdateOutputInformation();
00359 m_OriginalGeometry = static_cast< Geometry3D * >(
00360 data->GetGeometry( timeStep )->Clone().GetPointer() );
00361
00362 ok = true;
00363 break;
00364 }
00365
00366 case AcMOVE:
00367 {
00368
00369 const DisplayPositionEvent *dpe =
00370 dynamic_cast< const DisplayPositionEvent * >( stateEvent->GetEvent() );
00371 if ( dpe == NULL )
00372 {
00373 ok = true;
00374 break;
00375 }
00376
00377 if ( currentVtkRenderer != NULL )
00378 {
00379 vtkInteractorObserver::ComputeDisplayToWorld(
00380 currentVtkRenderer,
00381 m_CurrentPickedDisplayPoint[0],
00382 m_CurrentPickedDisplayPoint[1],
00383 0.0,
00384 m_CurrentPickedPointWorld );
00385 }
00386
00387
00388 Vector3D interactionMove;
00389 interactionMove[0] = m_CurrentPickedPointWorld[0] - m_InitialPickedPointWorld[0];
00390 interactionMove[1] = m_CurrentPickedPointWorld[1] - m_InitialPickedPointWorld[1];
00391 interactionMove[2] = m_CurrentPickedPointWorld[2] - m_InitialPickedPointWorld[2];
00392
00393 if ( m_InteractionMode == INTERACTION_MODE_TRANSLATION )
00394 {
00395 Point3D origin = m_OriginalGeometry->GetOrigin();
00396
00397 Vector3D transformedObjectNormal;
00398 data->GetGeometry( timeStep )->IndexToWorld(
00399 origin, m_ObjectNormal, transformedObjectNormal );
00400
00401 data->GetGeometry( timeStep )->SetOrigin(
00402 origin + transformedObjectNormal * (interactionMove * transformedObjectNormal) );
00403 }
00404 else if ( m_InteractionMode == INTERACTION_MODE_ROTATION )
00405 {
00406 if ( camera )
00407 {
00408 vtkFloatingPointType vpn[3];
00409 camera->GetViewPlaneNormal( vpn );
00410
00411 Vector3D viewPlaneNormal;
00412 viewPlaneNormal[0] = vpn[0];
00413 viewPlaneNormal[1] = vpn[1];
00414 viewPlaneNormal[2] = vpn[2];
00415
00416 Vector3D rotationAxis =
00417 itk::CrossProduct( viewPlaneNormal, interactionMove );
00418 rotationAxis.Normalize();
00419
00420 int *size = currentVtkRenderer->GetSize();
00421 double l2 =
00422 (m_CurrentPickedDisplayPoint[0] - m_InitialPickedDisplayPoint[0]) *
00423 (m_CurrentPickedDisplayPoint[0] - m_InitialPickedDisplayPoint[0]) +
00424 (m_CurrentPickedDisplayPoint[1] - m_InitialPickedDisplayPoint[1]) *
00425 (m_CurrentPickedDisplayPoint[1] - m_InitialPickedDisplayPoint[1]);
00426
00427 double rotationAngle = 360.0 * sqrt(l2/(size[0]*size[0]+size[1]*size[1]));
00428
00429
00430 Point3D rotationCenter = m_OriginalGeometry->GetCenter();;
00431
00432
00433
00434 RotationOperation op( OpROTATE, rotationCenter, rotationAxis, rotationAngle );
00435 Geometry3D::Pointer newGeometry = static_cast< Geometry3D * >(
00436 m_OriginalGeometry->Clone().GetPointer() );
00437 newGeometry->ExecuteOperation( &op );
00438 data->SetGeometry( newGeometry, timeStep );
00439
00440 }
00441 }
00442
00443 RenderingManager::GetInstance()->RequestUpdateAll();
00444 ok = true;
00445 break;
00446 }
00447
00448
00449
00450 default:
00451 return Superclass::ExecuteAction( action, stateEvent );
00452 }
00453
00454 return ok;
00455 }
00456
00457 bool AffineInteractor3D::ColorizeSurface( vtkPolyData *polyData,
00458 const Point3D & , double scalar )
00459 {
00460 if ( polyData == NULL )
00461 {
00462 return false;
00463 }
00464
00465
00466 vtkPointData *pointData = polyData->GetPointData();
00467 if ( pointData == NULL )
00468 {
00469 return false;
00470 }
00471
00472 vtkDataArray *scalars = pointData->GetScalars();
00473 if ( scalars == NULL )
00474 {
00475 return false;
00476 }
00477
00478 for ( unsigned int i = 0; i < pointData->GetNumberOfTuples(); ++i )
00479 {
00480 scalars->SetComponent( i, 0, scalar );
00481 }
00482
00483 polyData->Modified();
00484 pointData->Update();
00485
00486 return true;
00487 }
00488
00489
00490 }