00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "mitkPointSelectorInteractor.h"
00020 #include <mitkPointOperation.h>
00021 #include <mitkPositionEvent.h>
00022 #include <mitkOperationEvent.h>
00023
00024 #include <mitkDataNode.h>
00025 #include <mitkPointSet.h>
00026 #include <mitkInteractionConst.h>
00027 #include <mitkAction.h>
00028 #include <mitkProperties.h>
00029 #include <vtkLinearTransform.h>
00030 #include <mitkUndoController.h>
00031 #include <mitkStateEvent.h>
00032 #include <mitkState.h>
00033
00034
00035 mitk::PointSelectorInteractor::PointSelectorInteractor(const char * type, DataNode* dataNode)
00036 : Interactor(type, dataNode), m_LastPosition(0)
00037 {
00038 m_LastPoint.Fill(0);
00039 }
00040
00041 mitk::PointSelectorInteractor::~PointSelectorInteractor()
00042 {}
00043
00044 void mitk::PointSelectorInteractor::SelectPoint(int position)
00045 {
00046 mitk::PointSet* pointSet = dynamic_cast<mitk::PointSet*>(m_DataNode->GetData());
00047 if (pointSet == NULL)
00048 return;
00049 if (pointSet->GetSize()<=0)
00050 return;
00051
00052 mitk::Point3D noPoint;
00053 noPoint.Fill(0);
00054 mitk::PointOperation* doOp = new mitk::PointOperation(OpSELECTPOINT, noPoint, position);
00055 if (m_UndoEnabled)
00056 {
00057 mitk::PointOperation* undoOp = new mitk::PointOperation(OpDESELECTPOINT, noPoint, position);
00058 OperationEvent *operationEvent = new OperationEvent(pointSet, doOp, undoOp);
00059 m_UndoController->SetOperationEvent(operationEvent);
00060 }
00061 pointSet->ExecuteOperation(doOp);
00062 }
00063
00064 void mitk::PointSelectorInteractor::DeselectAllPoints()
00065 {
00066 mitk::PointSet* pointSet = dynamic_cast<mitk::PointSet*>(m_DataNode->GetData());
00067 if (pointSet == NULL)
00068 return;
00069
00070 mitk::PointSet::DataType *itkPointSet = pointSet->GetPointSet();
00071 mitk::PointSet::PointsContainer::Iterator it, end;
00072 end = itkPointSet->GetPoints()->End();
00073
00074 for (it = itkPointSet->GetPoints()->Begin(); it != end; it++)
00075 {
00076 int position = it->Index();
00077 PointSet::PointDataType pointData = {0, false, PTUNDEFINED};
00078 itkPointSet->GetPointData(position, &pointData);
00079 if ( pointData.selected )
00080 {
00081 mitk::Point3D noPoint;
00082 noPoint.Fill(0);
00083
00084 mitk::PointOperation* doOp = new mitk::PointOperation(OpDESELECTPOINT, noPoint, position);
00085 if (m_UndoEnabled)
00086 {
00087 mitk::PointOperation* undoOp = new mitk::PointOperation(OpSELECTPOINT, noPoint, position);
00088 OperationEvent *operationEvent = new OperationEvent(pointSet, doOp, undoOp);
00089 m_UndoController->SetOperationEvent(operationEvent);
00090 }
00091 pointSet->ExecuteOperation(doOp);
00092 }
00093 }
00094 }
00095
00096 float mitk::PointSelectorInteractor::CanHandleEvent(StateEvent const* stateEvent) const
00097
00098 {
00099 float returnValue = 0;
00100
00101 mitk::PositionEvent const *posEvent = dynamic_cast <const mitk::PositionEvent *> (stateEvent->GetEvent());
00102
00103 if (posEvent == NULL)
00104 {
00105
00106 if (this->GetCurrentState()->GetTransition(stateEvent->GetId())!=NULL)
00107 {
00108 return 0.5;
00109 }
00110 else
00111 {
00112 return 0;
00113 }
00114 }
00115
00116
00117
00118 if (stateEvent->GetEvent()->GetType() == mitk::Type_MouseMove)
00119 {
00120 return 0;
00121 }
00122
00123
00124 if (this->GetCurrentState()->GetTransition(stateEvent->GetId())!=NULL)
00125 {
00126 returnValue = 0.5;
00127 }
00128
00129
00130 mitk::PointSet* pointSet = dynamic_cast<mitk::PointSet*>(m_DataNode->GetData());
00131 if (pointSet == NULL)
00132 return 0;
00133
00134
00135
00136
00137 mitk::Point3D worldPoint = posEvent->GetWorldPosition();
00138 float p[3];
00139 itk2vtk(worldPoint, p);
00140
00141 m_DataNode->GetData()->GetGeometry()->GetVtkTransform()->GetInverse()->TransformPoint(p, p);
00142 vtk2itk(p, worldPoint);
00143
00144 float distance = 5;
00145 int index = pointSet->SearchPoint(worldPoint, distance);
00146 if (index>-1)
00147
00148 {
00149
00150 mitk::PointSet::PointType point;
00151 pointSet->GetPointSet()->GetPoint(index, &point);
00152 returnValue = point.EuclideanDistanceTo(worldPoint);
00153
00154
00155 returnValue = 1 - ( returnValue / distance );
00156 if (returnValue<0 || returnValue>1)
00157 {
00158 itkWarningMacro("Difficulties in calculating Jurisdiction. Check PointSelectorInteractor");
00159 return 0;
00160 }
00161
00162
00163 returnValue = 0.5 + (returnValue / 2);
00164
00165 return returnValue;
00166 }
00167 else
00168 {
00169 return returnValue;
00170 }
00171
00172 }
00173
00174
00175 bool mitk::PointSelectorInteractor::ExecuteAction( Action* action, mitk::StateEvent const* stateEvent )
00176 {
00177 bool ok = false;
00178
00179
00180 mitk::PointSet* pointSet = dynamic_cast<mitk::PointSet*>(m_DataNode->GetData());
00181 if (pointSet == NULL)
00182 return false;
00183
00184
00185
00186
00187
00188
00189 switch (action->GetActionId())
00190 {
00191 case AcADDPOINT:
00192 break;
00193 case AcDESELECTALL:
00194 this->DeselectAllPoints();
00195 ok = true;
00196 break;
00197 case AcCHECKELEMENT :
00198
00199 {
00200 mitk::PositionEvent const *posEvent = dynamic_cast <const mitk::PositionEvent *> (stateEvent->GetEvent());
00201 if (posEvent != NULL)
00202 {
00203 mitk::Point3D worldPoint = posEvent->GetWorldPosition();
00204
00205 int PRECISION = 4;
00206 mitk::IntProperty *precision = dynamic_cast<IntProperty*>(action->GetProperty("PRECISION"));
00207 if (precision != NULL)
00208 {
00209 PRECISION = precision->GetValue();
00210 }
00211
00212 int position = pointSet->SearchPoint(worldPoint, PRECISION);
00213 if (position>=0)
00214 {
00215 m_LastPosition = position;
00216 worldPoint = pointSet->GetPoint(position);
00217 mitk::Point2D displPoint;
00218 displPoint[0] = worldPoint[0];
00219 displPoint[1] = worldPoint[1];
00220
00221
00222 mitk::PositionEvent const* newPosEvent = new mitk::PositionEvent(posEvent->GetSender(), posEvent->GetType(),
00223 posEvent->GetButton(), posEvent->GetButtonState(), posEvent->GetKey(),
00224 displPoint, worldPoint);
00225 mitk::StateEvent* newStateEvent = new mitk::StateEvent(EIDYES, newPosEvent);
00226
00227 this->HandleEvent( newStateEvent );
00228 ok = true;
00229 }
00230 else
00231 {
00232
00233 mitk::StateEvent* newStateEvent = new mitk::StateEvent(EIDNO, posEvent);
00234 this->HandleEvent( newStateEvent );
00235 ok = true;
00236 }
00237 }
00238 else
00239 {
00240 mitk::DisplayPositionEvent const *disPosEvent = dynamic_cast <const mitk::DisplayPositionEvent *> (stateEvent->GetEvent());
00241 if (disPosEvent != NULL)
00242 {
00243 mitk::StateEvent* newStateEvent = new mitk::StateEvent(EIDNO, posEvent);
00244 this->HandleEvent( newStateEvent );
00245 ok = true;
00246 }
00247 }
00248 }
00249 break;
00250 case AcCHECKSELECTED:
00251
00252 {
00253 mitk::PositionEvent const *posEvent = dynamic_cast <const mitk::PositionEvent *> (stateEvent->GetEvent());
00254 if (posEvent != NULL)
00255 {
00256 mitk::Point3D worldPoint = posEvent->GetWorldPosition();
00257
00258 int PRECISION = 4;
00259 mitk::IntProperty *precision = dynamic_cast<IntProperty*>(action->GetProperty("precision"));
00260 if (precision != NULL)
00261 {
00262 PRECISION = precision->GetValue();
00263 }
00264
00265 int position = pointSet->SearchPoint(worldPoint, PRECISION);
00266 if (position <0)
00267 {
00268
00269 mitk::StateEvent* newStateEvent = new mitk::StateEvent(EIDNO, posEvent);
00270 this->HandleEvent( newStateEvent );
00271 ok = true;
00272 break;
00273 }
00274
00275
00276 unsigned int upos = (unsigned int) position;
00277 if (upos == m_LastPosition)
00278 {
00279 mitk::StateEvent* newStateEvent = new mitk::StateEvent(EIDYES, posEvent);
00280
00281 this->HandleEvent( newStateEvent );
00282 ok = true;
00283 }
00284 else
00285 {
00286
00287 mitk::StateEvent* newStateEvent = new mitk::StateEvent(EIDNO, posEvent);
00288 this->HandleEvent( newStateEvent );
00289 ok = true;
00290 }
00291 }
00292 else
00293 {
00294 mitk::DisplayPositionEvent const *disPosEvent = dynamic_cast <const mitk::DisplayPositionEvent *> (stateEvent->GetEvent());
00295 if (disPosEvent != NULL)
00296 {
00297 mitk::StateEvent* newStateEvent = new mitk::StateEvent(EIDNO, posEvent);
00298 this->HandleEvent( newStateEvent );
00299 ok = true;
00300 }
00301 }
00302 }
00303 break;
00304 case AcINITMOVEMENT:
00305 break;
00306 case AcMOVESELECTED:
00307 break;
00308 case AcFINISHMOVEMENT:
00309 break;
00310 case AcCHECKGREATERONE:
00311 break;
00312 case AcSELECTANOTHEROBJECT:
00313
00314 {
00315
00316
00317
00318 if (pointSet->GetSize()>0)
00319 {
00320 if (m_LastPosition > 0)
00321 {
00322 this->SelectPoint( m_LastPosition-1 );
00323 }
00324 else
00325 {
00326 m_LastPosition = pointSet->GetSize()-1;
00327 SelectPoint( m_LastPosition );
00328 }
00329 }
00330 ok = true;
00331 }
00332 break;
00333 case AcREMOVEPOINT:
00334 break;
00335 case AcSELECT:
00336
00337 {
00338 mitk::PositionEvent const *posEvent = dynamic_cast <const mitk::PositionEvent *> (stateEvent->GetEvent());
00339 if (posEvent == NULL)
00340 return false;
00341
00342 mitk::Point3D worldPoint = posEvent->GetWorldPosition();
00343
00344
00345
00346
00347 int position = pointSet->SearchPoint(worldPoint, 0);
00348
00349 if (position>=0)
00350 {
00351 PointOperation* doOp = new mitk::PointOperation(OpSELECTPOINT, worldPoint, position);
00352
00353
00354 if (m_UndoEnabled)
00355 {
00356 PointOperation* undoOp = new mitk::PointOperation(OpDESELECTPOINT, worldPoint, position);
00357 OperationEvent *operationEvent = new OperationEvent(pointSet, doOp, undoOp);
00358 m_UndoController->SetOperationEvent(operationEvent);
00359 }
00360
00361
00362 pointSet->ExecuteOperation(doOp);
00363 ok = true;
00364 }
00365 }
00366 break;
00367 case AcDESELECT:
00368 {
00369 mitk::PositionEvent const *posEvent = dynamic_cast <const mitk::PositionEvent *> (stateEvent->GetEvent());
00370 if (posEvent == NULL)
00371 return false;
00372
00373 mitk::Point3D worldPoint = posEvent->GetWorldPosition();
00374
00375
00376
00377
00378 int position = pointSet->SearchPoint(worldPoint, 0);
00379
00380 if (position>=0)
00381 {
00382 PointOperation* doOp = new mitk::PointOperation(OpDESELECTPOINT, worldPoint, position);
00383
00384
00385 if (m_UndoEnabled)
00386 {
00387 PointOperation* undoOp = new mitk::PointOperation(OpSELECTPOINT, worldPoint, position);
00388 OperationEvent *operationEvent = new OperationEvent(pointSet, doOp, undoOp);
00389 m_UndoController->SetOperationEvent(operationEvent);
00390 }
00391
00392
00393 pointSet->ExecuteOperation(doOp);
00394 ok = true;
00395 }
00396 }
00397 break;
00398 case AcSETSTARTPOINT:
00399 break;
00400 default:
00401 return Superclass::ExecuteAction( action, stateEvent );
00402 }
00403
00404 return ok;
00405
00406 }
00407
00408