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 "mitkInteractor.h"
00020 #include <mitkDataNode.h>
00021 #include <mitkDisplayPositionEvent.h>
00022 #include <mitkPositionEvent.h>
00023 #include <mitkGeometry3D.h>
00024 #include <mitkAction.h>
00025 #include <mitkOperationEvent.h>
00026 #include <mitkStateEvent.h>
00027 #include <mitkState.h>
00028 #include <mitkUndoController.h>
00029
00030 #include <vtkCamera.h>
00031 #include <vtkRenderer.h>
00032 #include "mitkInteractionConst.h"
00033 #include <vtkLinearTransform.h>
00034 #include <itkVector.h>
00035 #include <mitkModeOperation.h>
00036 #include "mitkGlobalInteraction.h"
00037
00038 const std::string mitk::Interactor::XML_NODE_NAME = "interactor";
00039
00040 mitk::Interactor::Interactor(const char * type, DataNode* dataNode)
00041 : StateMachine(type),
00042 m_DataNode(dataNode),
00043 m_Mode(SMDESELECTED)
00044 {
00045 if (m_DataNode != NULL)
00046 m_DataNode->SetInteractor(this);
00047
00048
00049 CONNECT_ACTION( AcMODEDESELECT, OnModeDeselect );
00050 CONNECT_ACTION( AcMODESELECT, OnModeSelect );
00051 CONNECT_ACTION( AcMODESUBSELECT, OnModeSubSelect );
00052 }
00053
00054 mitk::BaseData* mitk::Interactor::GetData() const
00055 {
00056 if (m_DataNode != NULL)
00057 return m_DataNode->GetData();
00058 else
00059 return NULL;
00060 }
00061
00062 mitk::Interactor::SMMode mitk::Interactor::GetMode() const
00063 {
00064 return m_Mode;
00065 }
00066
00067 bool mitk::Interactor::IsNotSelected() const
00068 {
00069 return (m_Mode==SMDESELECTED);
00070 }
00071
00072 bool mitk::Interactor::IsSelected() const
00073 {
00074 return (m_Mode!=SMDESELECTED);
00075 }
00076
00077 void mitk::Interactor::CreateModeOperation(ModeType mode)
00078 {
00079 ModeOperation* doOp = new ModeOperation(OpMODECHANGE, mode);
00080 if (m_UndoEnabled)
00081 {
00082 ModeOperation* undoOp = new ModeOperation(OpMODECHANGE, this->GetMode());
00083 OperationEvent *operationEvent = new OperationEvent(this, doOp, undoOp);
00084 m_UndoController->SetOperationEvent(operationEvent);
00085 }
00086 this->ExecuteOperation(doOp);
00087 }
00088
00089 bool mitk::Interactor::OnModeDeselect(Action* , StateEvent const*)
00090 {
00091 GlobalInteraction* global = GlobalInteraction::GetInstance();
00092 if (global == NULL)
00093 itkWarningMacro("Message from Interactor.cpp: GlobalInteraction == NULL! Check use of Interactor!");
00094
00095 if( this->GetMode() != SMDESELECTED)
00096 {
00097 this->CreateModeOperation(SMDESELECTED);
00098 global->RemoveFromSelectedInteractors(this);
00099 }
00100 return true;
00101 }
00102
00103 bool mitk::Interactor::OnModeSelect(Action* , StateEvent const*)
00104 {
00105 GlobalInteraction* global = GlobalInteraction::GetInstance();
00106 if (global == NULL)
00107 itkWarningMacro("Message from Interactor.cpp: GlobalInteraction == NULL! Check use of Interactor!");
00108
00109 if( this->GetMode() != SMSELECTED)
00110 {
00111 this->CreateModeOperation(SMSELECTED);
00112 global->AddToSelectedInteractors(this);
00113 }
00114 return true;
00115 }
00116
00117 bool mitk::Interactor::OnModeSubSelect(Action* , StateEvent const*)
00118 {
00119
00120 return false;
00121 }
00122
00123 float mitk::Interactor::CanHandleEvent(StateEvent const* stateEvent) const
00124 {
00125
00126 float returnvalueBB = 0.0,
00127
00128 returnvalueTransition = 0.0,
00129
00130 returnvalueKey = 0.0;
00131
00132
00133 DisplayPositionEvent const *disPosEvent = dynamic_cast <const DisplayPositionEvent *> (stateEvent->GetEvent());
00134
00135
00136 if (disPosEvent == NULL)
00137 {
00138
00139 if (this->GetCurrentState()->GetTransition(stateEvent->GetId())!=NULL)
00140 {
00141 returnvalueKey = 0.5;
00142 }
00143 }
00144
00145
00146
00147 if (stateEvent->GetEvent()->GetType() == Type_MouseMove)
00148 {
00149 return 0;
00150 }
00151
00152
00153 if (this->GetCurrentState()->GetTransition(stateEvent->GetId())!=NULL)
00154 {
00155 returnvalueTransition = 0.5;
00156 }
00157
00158
00159 if (GetData() != NULL)
00160 {
00161 const BoundingBox *bBox = GetData()->GetUpdatedTimeSlicedGeometry()->GetBoundingBox();
00162 if (bBox == NULL)
00163 return 0;
00164
00165
00166 DisplayPositionEvent const *event = dynamic_cast <const DisplayPositionEvent *> (stateEvent->GetEvent());
00167 if (event != NULL)
00168 {
00169
00170 Point3D point;
00171 GetData()->GetTimeSlicedGeometry()->WorldToIndex(event->GetWorldPosition(), point);
00172
00173
00174 BoundingBox::PointType center = bBox->GetCenter();
00175 returnvalueBB = point.EuclideanDistanceTo(center);
00176
00177
00178 float bBoxSize = bBox->GetMaximum().EuclideanDistanceTo(bBox->GetMinimum() );
00179 if( bBoxSize < 0.00001 ) return 0;
00180
00181
00182 returnvalueBB = returnvalueBB/bBoxSize;
00183
00184
00185 if (returnvalueBB>1 || returnvalueBB<0)
00186 returnvalueBB = 0;
00187
00188
00189 returnvalueBB = 1 - returnvalueBB;
00190
00191
00192 if (bBox->IsInside(point))
00193 {
00194
00195 returnvalueBB = 0.5 + (returnvalueBB/ 2);
00196 }
00197 else
00198 {
00199
00200 returnvalueBB = returnvalueBB / 2;
00201 }
00202 }
00203 }
00204
00205
00206
00207 return std::max(returnvalueBB, std::max(returnvalueKey, returnvalueTransition));
00208 }
00209
00210 void mitk::Interactor::ExecuteOperation(Operation* operation)
00211 {
00212 switch (operation->GetOperationType())
00213 {
00214 case OpMODECHANGE:
00215 {
00216 ModeOperation *modeOp = dynamic_cast<ModeOperation*>(operation);
00217 if (modeOp)
00218 {
00219 m_Mode = modeOp->GetMode();
00220 }
00221 }
00222 break;
00223 default:
00224 Superclass::ExecuteOperation(operation);
00225 }
00226 }
00227
00228 const std::string& mitk::Interactor::GetXMLNodeName() const
00229 {
00230 return XML_NODE_NAME;
00231 }
00232
00233 void mitk::Interactor::SetDataNode( DataNode* dataNode )
00234 {
00235 m_DataNode = dataNode;
00236
00237
00238 if (m_DataNode != NULL)
00239 {
00240 mitk::BaseData* data = dataNode->GetData();
00241 if (data != NULL)
00242 {
00243 unsigned int timeSteps = data->GetTimeSteps();
00244
00245 if (timeSteps > 1)
00246 this->InitializeStartStates(timeSteps);
00247 }
00248 }
00249 }
00250
00251 void mitk::Interactor::UpdateTimeStep(unsigned int timeStep)
00252 {
00253
00254 if (timeStep >= 1)
00255 {
00256
00257
00258 if (m_DataNode!= NULL)
00259 if (m_DataNode->GetData()!= NULL)
00260 m_DataNode->GetData()->Expand(timeStep+1);
00261
00262
00263 this->ExpandStartStateVector(timeStep+1);
00264 }
00265
00266
00267 Superclass::UpdateTimeStep(timeStep);
00268
00269
00270
00271 if (timeStep != m_TimeStep)
00272 itkExceptionMacro(<<"Time is invalid. Take care of synchonization!");
00273 }
00274
00275 bool mitk::Interactor::HandleEvent(StateEvent const* stateEvent)
00276 {
00277
00278 if (stateEvent != NULL)
00279 {
00280 mitk::Event const* event = stateEvent->GetEvent();
00281 if (event != NULL)
00282 {
00283 mitk::BaseRenderer* sender = event->GetSender();
00284 if (sender != NULL)
00285 {
00286
00287 unsigned int currentTimeStep = sender->GetTimeStep();
00288 if (currentTimeStep != m_TimeStep)
00289 this->UpdateTimeStep(currentTimeStep);
00290 }
00291 }
00292 }
00293 return Superclass::HandleEvent(stateEvent);
00294 }
00295