00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "QmitkPointBasedRegistrationView.h"
00019 #include "ui_QmitkPointBasedRegistrationViewControls.h"
00020 #include "QmitkPointListWidget.h"
00021
00022 #include <vtkIterativeClosestPointTransform.h>
00023 #include <vtkMatrix4x4.h>
00024 #include <vtkMath.h>
00025
00026 #include <vtkLandmarkTransform.h>
00027 #include <QmitkStdMultiWidget.h>
00028 #include "QmitkCommonFunctionality.h"
00029 #include "qradiobutton.h"
00030 #include "qapplication.h"
00031 #include <qcursor.h>
00032 #include <qinputdialog.h>
00033 #include <qlcdnumber.h>
00034 #include <qlabel.h>
00035 #include "qmessagebox.h"
00036
00037 #include "mitkLandmarkWarping.h"
00038 #include <mitkPointOperation.h>
00039 #include <mitkPositionEvent.h>
00040 #include "mitkOperationEvent.h"
00041 #include "mitkUndoController.h"
00042 #include <mitkPointSetWriter.h>
00043 #include <mitkPointSetReader.h>
00044 #include "mitkNodePredicateDataType.h"
00045 #include "mitkNodePredicateProperty.h"
00046 #include "mitkNodePredicateAnd.h"
00047 #include "mitkNodePredicateNot.h"
00048 #include <mitkMessage.h>
00049
00050 #include <itkCommand.h>
00051
00052 #include "mitkDataNodeObject.h"
00053
00054 #include "berryIWorkbenchWindow.h"
00055 #include "berryISelectionService.h"
00056
00057
00058 const std::string QmitkPointBasedRegistrationView::VIEW_ID = "org.mitk.views.pointbasedregistration";
00059
00060 using namespace berry;
00061
00062 struct SelListenerPointBasedRegistration : ISelectionListener
00063 {
00064 berryObjectMacro(SelListenerPointBasedRegistration);
00065
00066 SelListenerPointBasedRegistration(QmitkPointBasedRegistrationView* view)
00067 {
00068 m_View = view;
00069 }
00070
00071 void DoSelectionChanged(ISelection::ConstPointer selection)
00072 {
00073
00074
00075
00076 m_View->m_CurrentSelection = selection.Cast<const IStructuredSelection>();
00077
00078
00079 if(m_View->m_CurrentSelection)
00080 {
00081 if (m_View->m_CurrentSelection->Size() != 2)
00082 {
00083 if (m_View->m_FixedNode.IsNull() || m_View->m_MovingNode.IsNull())
00084 {
00085 m_View->m_Controls.m_StatusLabel->show();
00086 m_View->m_Controls.TextLabelFixed->hide();
00087 m_View->m_Controls.m_FixedLabel->hide();
00088 m_View->m_Controls.line2->hide();
00089 m_View->m_Controls.m_FixedPointListWidget->hide();
00090 m_View->m_Controls.TextLabelMoving->hide();
00091 m_View->m_Controls.m_MovingLabel->hide();
00092 m_View->m_Controls.line1->hide();
00093 m_View->m_Controls.m_MovingPointListWidget->hide();
00094 m_View->m_Controls.m_OpacityLabel->hide();
00095 m_View->m_Controls.m_OpacitySlider->hide();
00096 m_View->m_Controls.label->hide();
00097 m_View->m_Controls.label_2->hide();
00098 m_View->m_Controls.m_SwitchImages->hide();
00099 m_View->m_Controls.m_ShowRedGreenValues->setEnabled(false);
00100 }
00101 }
00102 else
00103 {
00104 m_View->m_Controls.m_StatusLabel->hide();
00105 bool foundFixedImage = false;
00106 mitk::DataNode::Pointer fixedNode;
00107
00108 for (IStructuredSelection::iterator i = m_View->m_CurrentSelection->Begin();
00109 i != m_View->m_CurrentSelection->End(); ++i)
00110 {
00111
00112 if (mitk::DataNodeObject::Pointer nodeObj = i->Cast<mitk::DataNodeObject>())
00113 {
00114 mitk::TNodePredicateDataType<mitk::BaseData>::Pointer isBaseData(mitk::TNodePredicateDataType<mitk::BaseData>::New());
00115 mitk::TNodePredicateDataType<mitk::PointSet>::Pointer isPointSet(mitk::TNodePredicateDataType<mitk::PointSet>::New());
00116 mitk::NodePredicateNot::Pointer notPointSet = mitk::NodePredicateNot::New(isPointSet);
00117 mitk::TNodePredicateDataType<mitk::Geometry2DData>::Pointer isGeometry2DData(mitk::TNodePredicateDataType<mitk::Geometry2DData>::New());
00118 mitk::NodePredicateNot::Pointer notGeometry2DData = mitk::NodePredicateNot::New(isGeometry2DData);
00119 mitk::NodePredicateAnd::Pointer notPointSetAndNotGeometry2DData = mitk::NodePredicateAnd::New( notPointSet, notGeometry2DData );
00120 mitk::NodePredicateAnd::Pointer predicate = mitk::NodePredicateAnd::New( isBaseData, notPointSetAndNotGeometry2DData );
00121
00122
00123 mitk::DataStorage::SetOfObjects::ConstPointer setOfObjects = m_View->GetDataStorage()->GetSubset(predicate);
00124
00125 mitk::DataNode::Pointer node = nodeObj->GetDataNode();
00126
00127
00128 for (mitk::DataStorage::SetOfObjects::ConstIterator nodeIt = setOfObjects->Begin()
00129 ; nodeIt != setOfObjects->End(); ++nodeIt)
00130 {
00131 if(nodeIt->Value().GetPointer() == node.GetPointer())
00132 {
00133 if(QString("Image").compare(node->GetData()->GetNameOfClass())==0)
00134 {
00135 if (dynamic_cast<mitk::Image*>(node->GetData())->GetDimension() == 4)
00136 {
00137 m_View->m_Controls.m_StatusLabel->show();
00138 QMessageBox::information( NULL, "PointBasedRegistration", "Only 2D or 3D images can be processed.", QMessageBox::Ok );
00139 return;
00140 }
00141 if (foundFixedImage == false)
00142 {
00143 fixedNode = node;
00144 foundFixedImage = true;
00145 }
00146 else
00147 {
00148 m_View->SetImagesVisible(selection);
00149 m_View->FixedSelected(fixedNode);
00150 m_View->MovingSelected(node);
00151 m_View->m_Controls.m_StatusLabel->hide();
00152 m_View->m_Controls.TextLabelFixed->show();
00153 m_View->m_Controls.m_FixedLabel->show();
00154 m_View->m_Controls.line2->show();
00155 m_View->m_Controls.m_FixedPointListWidget->show();
00156 m_View->m_Controls.TextLabelMoving->show();
00157 m_View->m_Controls.m_MovingLabel->show();
00158 m_View->m_Controls.line1->show();
00159 m_View->m_Controls.m_MovingPointListWidget->show();
00160 m_View->m_Controls.m_OpacityLabel->show();
00161 m_View->m_Controls.m_OpacitySlider->show();
00162 m_View->m_Controls.label->show();
00163 m_View->m_Controls.label_2->show();
00164 m_View->m_Controls.m_SwitchImages->show();
00165 m_View->m_Controls.m_ShowRedGreenValues->setEnabled(true);
00166 }
00167 }
00168
00169 else
00170 {
00171 m_View->m_Controls.m_StatusLabel->show();
00172 return;
00173 }
00174
00175 }
00176 }
00177 }
00178 }
00179 if (m_View->m_FixedNode.IsNull() || m_View->m_MovingNode.IsNull())
00180 {
00181 m_View->m_Controls.m_StatusLabel->show();
00182 }
00183 }
00184 }
00185 else if (m_View->m_FixedNode.IsNull() || m_View->m_MovingNode.IsNull())
00186 {
00187 m_View->m_Controls.m_StatusLabel->show();
00188 }
00189 }
00190
00191 void SelectionChanged(IWorkbenchPart::Pointer part, ISelection::ConstPointer selection)
00192 {
00193
00194 if (part)
00195 {
00196 QString partname(part->GetPartName().c_str());
00197 if(partname.compare("Datamanager")==0)
00198 {
00199
00200 DoSelectionChanged(selection);
00201 }
00202 }
00203 }
00204
00205 QmitkPointBasedRegistrationView* m_View;
00206 };
00207
00208
00209 QmitkPointBasedRegistrationView::QmitkPointBasedRegistrationView(QObject * , const char * )
00210 : QmitkFunctionality(), m_SelListener(0), m_MultiWidget(NULL), m_FixedLandmarks(NULL), m_MovingLandmarks(NULL), m_MovingNode(NULL),
00211 m_FixedNode(NULL), m_ShowRedGreen(false), m_Opacity(0.5), m_OriginalOpacity(1.0), m_Transformation(0), m_HideFixedImage(false), m_HideMovingImage(false),
00212 m_OldFixedLabel(""), m_OldMovingLabel(""), m_Deactivated (false), m_CurrentFixedLandmarksObserverID(0), m_CurrentMovingLandmarksObserverID(0)
00213 {
00214 m_FixedLandmarksChangedCommand = itk::SimpleMemberCommand<QmitkPointBasedRegistrationView>::New();
00215 m_FixedLandmarksChangedCommand->SetCallbackFunction(this, &QmitkPointBasedRegistrationView::updateFixedLandmarksList);
00216 m_MovingLandmarksChangedCommand = itk::SimpleMemberCommand<QmitkPointBasedRegistrationView>::New();
00217 m_MovingLandmarksChangedCommand->SetCallbackFunction(this, &QmitkPointBasedRegistrationView::updateMovingLandmarksList);
00218
00219 this->GetDataStorage()->RemoveNodeEvent.AddListener(mitk::MessageDelegate1<QmitkPointBasedRegistrationView,
00220 const mitk::DataNode*> ( this, &QmitkPointBasedRegistrationView::DataNodeHasBeenRemoved ));
00221
00222 }
00223
00224 QmitkPointBasedRegistrationView::~QmitkPointBasedRegistrationView()
00225 {
00226 if(m_SelListener.IsNotNull())
00227 {
00228 berry::ISelectionService* s = GetSite()->GetWorkbenchWindow()->GetSelectionService();
00229 if(s)
00230 s->RemovePostSelectionListener(m_SelListener);
00231 m_SelListener = NULL;
00232 }
00233 if (m_FixedPointSetNode.IsNotNull())
00234 {
00235 m_Controls.m_FixedPointListWidget->DeactivateInteractor(true);
00236 m_FixedPointSetNode->SetProperty("label", mitk::StringProperty::New(m_OldFixedLabel));
00237 }
00238 if (m_MovingPointSetNode.IsNotNull())
00239 {
00240 m_Controls.m_MovingPointListWidget->DeactivateInteractor(true);
00241 m_MovingPointSetNode->SetProperty("label", mitk::StringProperty::New(m_OldMovingLabel));
00242 }
00243 m_Controls.m_FixedPointListWidget->SetPointSetNode(NULL);
00244 m_Controls.m_MovingPointListWidget->SetPointSetNode(NULL);
00245 }
00246
00247 void QmitkPointBasedRegistrationView::CreateQtPartControl(QWidget* parent)
00248 {
00249 m_Controls.setupUi(parent);
00250 m_Parent->setEnabled(false);
00251 m_Controls.m_MeanErrorLCD->hide();
00252 m_Controls.m_MeanError->hide();
00253 m_Controls.TextLabelFixed->hide();
00254 m_Controls.line2->hide();
00255 m_Controls.m_FixedPointListWidget->hide();
00256 m_Controls.m_FixedLabel->hide();
00257 m_Controls.TextLabelMoving->hide();
00258 m_Controls.m_MovingLabel->hide();
00259 m_Controls.line1->hide();
00260 m_Controls.m_MovingPointListWidget->hide();
00261 m_Controls.m_OpacityLabel->hide();
00262 m_Controls.m_OpacitySlider->hide();
00263 m_Controls.label->hide();
00264 m_Controls.label_2->hide();
00265 m_Controls.m_SwitchImages->hide();
00266 m_Controls.m_ShowRedGreenValues->setEnabled(false);
00267
00268 this->CreateConnections();
00269
00270
00271 m_Controls.m_FixedPointListWidget->SetMultiWidget( m_MultiWidget );
00272 m_Controls.m_MovingPointListWidget->SetMultiWidget( m_MultiWidget );
00273 }
00274
00275 void QmitkPointBasedRegistrationView::StdMultiWidgetAvailable (QmitkStdMultiWidget &stdMultiWidget)
00276 {
00277 m_Parent->setEnabled(true);
00278 m_MultiWidget = &stdMultiWidget;
00279 m_MultiWidget->SetWidgetPlanesVisibility(true);
00280 m_Controls.m_FixedPointListWidget->SetMultiWidget( m_MultiWidget );
00281 m_Controls.m_MovingPointListWidget->SetMultiWidget( m_MultiWidget );
00282 }
00283
00284 void QmitkPointBasedRegistrationView::StdMultiWidgetNotAvailable()
00285 {
00286 m_Parent->setEnabled(false);
00287 m_MultiWidget = NULL;
00288 m_Controls.m_FixedPointListWidget->SetMultiWidget( NULL );
00289 m_Controls.m_MovingPointListWidget->SetMultiWidget( NULL );
00290 }
00291
00292 void QmitkPointBasedRegistrationView::CreateConnections()
00293 {
00294 connect( (QObject*)(m_Controls.m_FixedPointListWidget), SIGNAL(EditPointSets(bool)), (QObject*)(m_Controls.m_MovingPointListWidget), SLOT(DeactivateInteractor(bool)));
00295 connect( (QObject*)(m_Controls.m_MovingPointListWidget), SIGNAL(EditPointSets(bool)), (QObject*)(m_Controls.m_FixedPointListWidget), SLOT(DeactivateInteractor(bool)));
00296 connect( (QObject*)(m_Controls.m_FixedPointListWidget), SIGNAL(EditPointSets(bool)), this, SLOT(HideMovingImage(bool)));
00297 connect( (QObject*)(m_Controls.m_MovingPointListWidget), SIGNAL(EditPointSets(bool)), this, SLOT(HideFixedImage(bool)));
00298 connect( (QObject*)(m_Controls.m_FixedPointListWidget), SIGNAL(PointListChanged()), this, SLOT(updateFixedLandmarksList()));
00299 connect( (QObject*)(m_Controls.m_MovingPointListWidget), SIGNAL(PointListChanged()), this, SLOT(updateMovingLandmarksList()));
00300 connect((QObject*)(m_Controls.m_Calculate),SIGNAL(clicked()),this,SLOT(calculate()));
00301 connect((QObject*)(m_Controls.m_SwitchImages),SIGNAL(clicked()),this,SLOT(SwitchImages()));
00302 connect((QObject*)(m_Controls.m_UndoTransformation),SIGNAL(clicked()),this,SLOT(UndoTransformation()));
00303 connect((QObject*)(m_Controls.m_RedoTransformation),SIGNAL(clicked()),this,SLOT(RedoTransformation()));
00304 connect((QObject*)(m_Controls.m_ShowRedGreenValues),SIGNAL(toggled(bool)),this,SLOT(showRedGreen(bool)));
00305 connect((QObject*)(m_Controls.m_OpacitySlider),SIGNAL(valueChanged(int)),this,SLOT(OpacityUpdate(int)));
00306 connect((QObject*)(m_Controls.m_SelectedTransformationClass),SIGNAL(activated(int)), this,SLOT(transformationChanged(int)));
00307 connect((QObject*)(m_Controls.m_UseICP),SIGNAL(toggled(bool)), this,SLOT(checkCalculateEnabled()));
00308 connect((QObject*)(m_Controls.m_UseICP),SIGNAL(toggled(bool)), this,SLOT(checkLandmarkError()));
00309 }
00310
00311 void QmitkPointBasedRegistrationView::Activated()
00312 {
00313
00314 m_Deactivated = false;
00315 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00316 QmitkFunctionality::Activated();
00317 this->clearTransformationLists();
00318 if (m_SelListener.IsNull())
00319 {
00320 m_SelListener = berry::ISelectionListener::Pointer(new SelListenerPointBasedRegistration(this));
00321 this->GetSite()->GetWorkbenchWindow()->GetSelectionService()->AddPostSelectionListener( m_SelListener);
00322 berry::ISelection::ConstPointer sel(
00323 this->GetSite()->GetWorkbenchWindow()->GetSelectionService()->GetSelection("org.mitk.views.datamanager"));
00324 m_CurrentSelection = sel.Cast<const IStructuredSelection>();
00325 m_SelListener.Cast<SelListenerPointBasedRegistration>()->DoSelectionChanged(sel);
00326 }
00327 this->OpacityUpdate(m_Controls.m_OpacitySlider->value());
00328 this->showRedGreen(m_Controls.m_ShowRedGreenValues->isChecked());
00329
00330
00331
00332 }
00333
00334 void QmitkPointBasedRegistrationView::Visible()
00335 {
00336
00337 }
00338
00339 void QmitkPointBasedRegistrationView::Deactivated()
00340 {
00341
00342 m_Deactivated = true;
00343 if (m_FixedPointSetNode.IsNotNull())
00344 m_FixedPointSetNode->SetProperty("label", mitk::StringProperty::New(m_OldFixedLabel));
00345 m_Controls.m_FixedPointListWidget->SetPointSetNode(NULL);
00346 m_Controls.m_FixedPointListWidget->DeactivateInteractor(true);
00347 if (m_MovingPointSetNode.IsNotNull())
00348 m_MovingPointSetNode->SetProperty("label", mitk::StringProperty::New(m_OldMovingLabel));
00349 m_Controls.m_MovingPointListWidget->SetPointSetNode(NULL);
00350 m_Controls.m_MovingPointListWidget->DeactivateInteractor(true);
00351 this->setImageColor(false);
00352 if (m_FixedNode.IsNotNull())
00353 m_FixedNode->SetOpacity(1.0);
00354 if (m_MovingNode.IsNotNull())
00355 {
00356 m_MovingNode->SetOpacity(m_OriginalOpacity);
00357 }
00358 this->clearTransformationLists();
00359 if (m_FixedPointSetNode.IsNotNull() && m_FixedLandmarks.IsNotNull() && m_FixedLandmarks->GetSize() == 0)
00360 {
00361 this->GetDataStorage()->Remove(m_FixedPointSetNode);
00362 }
00363 if (m_MovingPointSetNode.IsNotNull() && m_MovingLandmarks.IsNotNull() && m_MovingLandmarks->GetSize() == 0)
00364 {
00365 this->GetDataStorage()->Remove(m_MovingPointSetNode);
00366 }
00367 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00368 m_FixedNode = NULL;
00369 m_MovingNode = NULL;
00370 if(m_FixedLandmarks.IsNotNull())
00371 m_FixedLandmarks->RemoveObserver(m_CurrentFixedLandmarksObserverID);
00372 m_FixedLandmarks = NULL;
00373 if(m_MovingLandmarks.IsNotNull())
00374 m_MovingLandmarks->RemoveObserver(m_CurrentMovingLandmarksObserverID);
00375 m_MovingLandmarks = NULL;
00376 m_FixedPointSetNode = NULL;
00377 m_MovingPointSetNode = NULL;
00378 m_Controls.m_FixedLabel->hide();
00379 m_Controls.TextLabelFixed->hide();
00380 m_Controls.line2->hide();
00381 m_Controls.m_FixedPointListWidget->hide();
00382 m_Controls.m_MovingLabel->hide();
00383 m_Controls.TextLabelMoving->hide();
00384 m_Controls.line1->hide();
00385 m_Controls.m_MovingPointListWidget->hide();
00386 m_Controls.m_OpacityLabel->hide();
00387 m_Controls.m_OpacitySlider->hide();
00388 m_Controls.label->hide();
00389 m_Controls.label_2->hide();
00390 m_Controls.m_SwitchImages->hide();
00391 berry::ISelectionService* s = GetSite()->GetWorkbenchWindow()->GetSelectionService();
00392 if(s)
00393 s->RemovePostSelectionListener(m_SelListener);
00394 m_SelListener = NULL;
00395
00396 }
00397
00398 void QmitkPointBasedRegistrationView::Hidden()
00399 {
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454 }
00455
00456
00457 void QmitkPointBasedRegistrationView::DataNodeHasBeenRemoved(const mitk::DataNode* node)
00458 {
00459 if(node == m_FixedNode || node == m_MovingNode)
00460 {
00461 m_Controls.m_StatusLabel->show();
00462 m_Controls.TextLabelFixed->hide();
00463 m_Controls.m_FixedLabel->hide();
00464 m_Controls.line2->hide();
00465 m_Controls.m_FixedPointListWidget->hide();
00466 m_Controls.TextLabelMoving->hide();
00467 m_Controls.m_MovingLabel->hide();
00468 m_Controls.line1->hide();
00469 m_Controls.m_MovingPointListWidget->hide();
00470 m_Controls.m_OpacityLabel->hide();
00471 m_Controls.m_OpacitySlider->hide();
00472 m_Controls.label->hide();
00473 m_Controls.label_2->hide();
00474 m_Controls.m_SwitchImages->hide();
00475 m_Controls.m_ShowRedGreenValues->setEnabled(false);
00476 }
00477
00478 }
00479
00480 void QmitkPointBasedRegistrationView::FixedSelected(mitk::DataNode::Pointer fixedImage)
00481 {
00482 if(m_FixedLandmarks.IsNotNull())
00483 m_FixedLandmarks->RemoveObserver(m_CurrentFixedLandmarksObserverID);
00484 if (fixedImage.IsNotNull())
00485 {
00486 if (m_FixedNode != fixedImage)
00487 {
00488
00489 if (m_FixedNode.IsNotNull())
00490 {
00491 this->setImageColor(false);
00492 m_FixedNode->SetOpacity(1.0);
00493 if (m_FixedPointSetNode.IsNotNull())
00494 {
00495 m_FixedPointSetNode->SetProperty("label", mitk::StringProperty::New(m_OldFixedLabel));
00496 }
00497 }
00498
00499 m_FixedNode = fixedImage;
00500 m_FixedNode->SetOpacity(0.5);
00501 m_FixedNode->SetVisibility(true);
00502 m_Controls.m_FixedLabel->setText(QString::fromStdString(m_FixedNode->GetName()));
00503 m_Controls.m_FixedLabel->show();
00504 m_Controls.m_SwitchImages->show();
00505 m_Controls.TextLabelFixed->show();
00506 m_Controls.line2->show();
00507 m_Controls.m_FixedPointListWidget->show();
00508 mitk::ColorProperty::Pointer colorProperty;
00509 colorProperty = dynamic_cast<mitk::ColorProperty*>(m_FixedNode->GetProperty("color"));
00510 if ( colorProperty.IsNotNull() )
00511 {
00512 m_FixedColor = colorProperty->GetColor();
00513 }
00514 this->setImageColor(m_ShowRedGreen);
00515
00516 bool hasPointSetNode = false;
00517 mitk::DataStorage::SetOfObjects::ConstPointer children = this->GetDataStorage()->GetDerivations(m_FixedNode);
00518 unsigned long size;
00519 size = children->Size();
00520 for (unsigned long i = 0; i < size; ++i)
00521 {
00522 mitk::StringProperty::Pointer nameProp = dynamic_cast<mitk::StringProperty*>(children->GetElement(i)->GetProperty("name"));
00523 if(nameProp.IsNotNull() && nameProp->GetValueAsString()=="PointBasedRegistrationNode")
00524 {
00525 m_FixedPointSetNode=children->GetElement(i);
00526 m_FixedLandmarks = dynamic_cast<mitk::PointSet*> (m_FixedPointSetNode->GetData());
00527 this->GetDataStorage()->Remove(m_FixedPointSetNode);
00528 hasPointSetNode = true;
00529 break;
00530 }
00531 }
00532 if (!hasPointSetNode)
00533 {
00534 m_FixedLandmarks = mitk::PointSet::New();
00535 m_FixedPointSetNode = mitk::DataNode::New();
00536 m_FixedPointSetNode->SetData(m_FixedLandmarks);
00537 m_FixedPointSetNode->SetProperty("name", mitk::StringProperty::New("PointBasedRegistrationNode"));
00538 }
00539 m_FixedPointSetNode->GetStringProperty("label", m_OldFixedLabel);
00540 m_FixedPointSetNode->SetProperty("label", mitk::StringProperty::New("F "));
00541 m_FixedPointSetNode->SetProperty("color", mitk::ColorProperty::New(0.0f, 1.0f, 1.0f));
00542 m_FixedPointSetNode->SetVisibility(true);
00543 m_Controls.m_FixedPointListWidget->SetPointSetNode(m_FixedPointSetNode);
00544 this->GetDataStorage()->Add(m_FixedPointSetNode, m_FixedNode);
00545
00546 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00547 }
00548 if (m_FixedPointSetNode.IsNull())
00549 {
00550 m_FixedLandmarks = mitk::PointSet::New();
00551 m_FixedPointSetNode = mitk::DataNode::New();
00552 m_FixedPointSetNode->SetData(m_FixedLandmarks);
00553 m_FixedPointSetNode->SetProperty("name", mitk::StringProperty::New("PointBasedRegistrationNode"));
00554
00555 m_FixedPointSetNode->GetStringProperty("label", m_OldFixedLabel);
00556 m_FixedPointSetNode->SetProperty("label", mitk::StringProperty::New("F "));
00557 m_FixedPointSetNode->SetProperty("color", mitk::ColorProperty::New(0.0f, 1.0f, 1.0f));
00558 m_FixedPointSetNode->SetVisibility(true);
00559 m_Controls.m_FixedPointListWidget->SetPointSetNode(m_FixedPointSetNode);
00560 this->GetDataStorage()->Add(m_FixedPointSetNode, m_FixedNode);
00561
00562 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00563 }
00564 }
00565 else
00566 {
00567 m_FixedNode = NULL;
00568 if (m_FixedPointSetNode.IsNotNull())
00569 m_FixedPointSetNode->SetProperty("label", mitk::StringProperty::New(m_OldFixedLabel));
00570 m_FixedPointSetNode = NULL;
00571 m_FixedLandmarks = NULL;
00572 m_Controls.m_FixedPointListWidget->SetPointSetNode(m_FixedPointSetNode);
00573 m_Controls.m_FixedLabel->hide();
00574 m_Controls.TextLabelFixed->hide();
00575 m_Controls.line2->hide();
00576 m_Controls.m_FixedPointListWidget->hide();
00577 m_Controls.m_SwitchImages->hide();
00578 }
00579 if(m_FixedLandmarks.IsNotNull())
00580 m_CurrentFixedLandmarksObserverID = m_FixedLandmarks->AddObserver(itk::ModifiedEvent(), m_FixedLandmarksChangedCommand);
00581 }
00582
00583 void QmitkPointBasedRegistrationView::MovingSelected(mitk::DataNode::Pointer movingImage)
00584 {
00585 if(m_MovingLandmarks.IsNotNull())
00586 m_MovingLandmarks->RemoveObserver(m_CurrentMovingLandmarksObserverID);
00587 if (movingImage.IsNotNull())
00588 {
00589 if (m_MovingNode != movingImage)
00590 {
00591 if (m_MovingNode.IsNotNull())
00592 {
00593 m_MovingNode->SetOpacity(m_OriginalOpacity);
00594 if (m_FixedNode == m_MovingNode)
00595 m_FixedNode->SetOpacity(0.5);
00596 this->setImageColor(false);
00597 if (m_MovingNode != m_FixedNode)
00598 {
00599 m_MovingPointSetNode->SetProperty("label", mitk::StringProperty::New(m_OldMovingLabel));
00600 }
00601 else
00602 {
00603 m_OldFixedLabel = m_OldMovingLabel;
00604 }
00605 }
00606 if (m_MovingPointSetNode.IsNotNull())
00607 m_MovingPointSetNode->SetProperty("label", mitk::StringProperty::New(m_OldMovingLabel));
00608 m_MovingNode = movingImage;
00609 m_MovingNode->SetVisibility(true);
00610 m_Controls.m_MovingLabel->setText(QString::fromStdString(m_MovingNode->GetName()));
00611 m_Controls.m_MovingLabel->show();
00612 m_Controls.TextLabelMoving->show();
00613 m_Controls.line1->show();
00614 m_Controls.m_MovingPointListWidget->show();
00615 m_Controls.m_OpacityLabel->show();
00616 m_Controls.m_OpacitySlider->show();
00617 m_Controls.label->show();
00618 m_Controls.label_2->show();
00619 mitk::ColorProperty::Pointer colorProperty;
00620 colorProperty = dynamic_cast<mitk::ColorProperty*>(m_MovingNode->GetProperty("color"));
00621 if ( colorProperty.IsNotNull() )
00622 {
00623 m_MovingColor = colorProperty->GetColor();
00624 }
00625 this->setImageColor(m_ShowRedGreen);
00626 m_MovingNode->GetFloatProperty("opacity", m_OriginalOpacity);
00627 this->OpacityUpdate(m_Opacity);
00628 bool hasPointSetNode = false;
00629 mitk::DataStorage::SetOfObjects::ConstPointer children = this->GetDataStorage()->GetDerivations(m_MovingNode);
00630 unsigned long size;
00631 size = children->Size();
00632 for (unsigned long i = 0; i < size; ++i)
00633 {
00634 mitk::StringProperty::Pointer nameProp = dynamic_cast<mitk::StringProperty*>(children->GetElement(i)->GetProperty("name"));
00635 if(nameProp.IsNotNull() && nameProp->GetValueAsString()=="PointBasedRegistrationNode")
00636 {
00637 m_MovingPointSetNode=children->GetElement(i);
00638 m_MovingLandmarks = dynamic_cast<mitk::PointSet*> (m_MovingPointSetNode->GetData());
00639 this->GetDataStorage()->Remove(m_MovingPointSetNode);
00640 hasPointSetNode = true;
00641 break;
00642 }
00643 }
00644 if (!hasPointSetNode)
00645 {
00646 m_MovingLandmarks = mitk::PointSet::New();
00647 m_MovingPointSetNode = mitk::DataNode::New();
00648 m_MovingPointSetNode->SetData(m_MovingLandmarks);
00649 m_MovingPointSetNode->SetProperty("name", mitk::StringProperty::New("PointBasedRegistrationNode"));
00650 }
00651 this->GetDataStorage()->Add(m_MovingPointSetNode, m_MovingNode);
00652 m_MovingPointSetNode->GetStringProperty("label", m_OldMovingLabel);
00653 m_MovingPointSetNode->SetProperty("label", mitk::StringProperty::New("M "));
00654 m_MovingPointSetNode->SetProperty("color", mitk::ColorProperty::New(1.0f, 1.0f, 0.0f));
00655 m_MovingPointSetNode->SetVisibility(true);
00656 m_Controls.m_MovingPointListWidget->SetPointSetNode(m_MovingPointSetNode);
00657 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00658 this->clearTransformationLists();
00659 this->OpacityUpdate(m_Opacity);
00660 }
00661 if (m_MovingPointSetNode.IsNull())
00662 {
00663 m_MovingLandmarks = mitk::PointSet::New();
00664 m_MovingPointSetNode = mitk::DataNode::New();
00665 m_MovingPointSetNode->SetData(m_MovingLandmarks);
00666 m_MovingPointSetNode->SetProperty("name", mitk::StringProperty::New("PointBasedRegistrationNode"));
00667
00668 m_MovingPointSetNode->GetStringProperty("label", m_OldMovingLabel);
00669 m_MovingPointSetNode->SetProperty("label", mitk::StringProperty::New("M "));
00670 m_MovingPointSetNode->SetProperty("color", mitk::ColorProperty::New(1.0f, 1.0f, 0.0f));
00671 m_MovingPointSetNode->SetVisibility(true);
00672 m_Controls.m_MovingPointListWidget->SetPointSetNode(m_MovingPointSetNode);
00673 this->GetDataStorage()->Add(m_MovingPointSetNode, m_MovingNode);
00674
00675 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00676
00677 }
00678
00679
00680
00681
00682 }
00683 else
00684 {
00685 m_MovingNode = NULL;
00686 if (m_MovingPointSetNode.IsNotNull())
00687 m_MovingPointSetNode->SetProperty("label", mitk::StringProperty::New(m_OldMovingLabel));
00688 m_MovingPointSetNode = NULL;
00689 m_MovingLandmarks = NULL;
00690 m_Controls.m_MovingPointListWidget->SetPointSetNode(m_MovingPointSetNode);
00691 m_Controls.m_MovingLabel->hide();
00692 m_Controls.TextLabelMoving->hide();
00693 m_Controls.line1->hide();
00694 m_Controls.m_MovingPointListWidget->hide();
00695 m_Controls.m_OpacityLabel->hide();
00696 m_Controls.m_OpacitySlider->hide();
00697 m_Controls.label->hide();
00698 m_Controls.label_2->hide();
00699 }
00700 if(m_MovingLandmarks.IsNotNull())
00701 m_CurrentMovingLandmarksObserverID = m_MovingLandmarks->AddObserver(itk::ModifiedEvent(), m_MovingLandmarksChangedCommand);
00702 }
00703
00704 void QmitkPointBasedRegistrationView::updateMovingLandmarksList()
00705 {
00706
00707
00708
00709
00710
00711 m_MovingLandmarks = dynamic_cast<mitk::PointSet*>(m_MovingPointSetNode->GetData());
00712
00713 this->checkLandmarkError();
00714 this->CheckCalculate();
00715 }
00716
00717 void QmitkPointBasedRegistrationView::updateFixedLandmarksList()
00718 {
00719 m_FixedLandmarks = dynamic_cast<mitk::PointSet*>(m_FixedPointSetNode->GetData());
00720 this->checkLandmarkError();
00721 this->CheckCalculate();
00722 }
00723
00724 void QmitkPointBasedRegistrationView::HideFixedImage(bool hide)
00725 {
00726 m_HideFixedImage = hide;
00727 if(m_FixedNode.IsNotNull())
00728 {
00729 m_FixedNode->SetVisibility(!hide);
00730 }
00731 if (hide)
00732 {
00733
00734 }
00735 if (!m_HideMovingImage && !m_HideFixedImage)
00736 {
00737
00738 }
00739 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00740 }
00741
00742 void QmitkPointBasedRegistrationView::HideMovingImage(bool hide)
00743 {
00744 m_HideMovingImage = hide;
00745 if(m_MovingNode.IsNotNull())
00746 {
00747 m_MovingNode->SetVisibility(!hide);
00748 }
00749 if (hide)
00750 {
00751
00752 }
00753 if (!m_HideMovingImage && !m_HideFixedImage)
00754 {
00755
00756 }
00757 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00758 }
00759
00760 bool QmitkPointBasedRegistrationView::CheckCalculate()
00761 {
00762 if((m_MovingPointSetNode.IsNull())||(m_FixedPointSetNode.IsNull()||m_FixedLandmarks.IsNull()||m_MovingLandmarks.IsNull()))
00763 return false;
00764 if(m_MovingNode==m_FixedNode)
00765 return false;
00766 return this->checkCalculateEnabled();
00767 }
00768
00769 void QmitkPointBasedRegistrationView::UndoTransformation()
00770 {
00771 if(!m_UndoPointsGeometryList.empty())
00772 {
00773 mitk::AffineGeometryFrame3D::Pointer movingLandmarksGeometry = m_MovingLandmarks->GetGeometry(0)->Clone();
00774 m_RedoPointsGeometryList.push_back(static_cast<mitk::Geometry3D *>(movingLandmarksGeometry.GetPointer()));
00775 m_MovingLandmarks->SetGeometry(m_UndoPointsGeometryList.back());
00776 m_UndoPointsGeometryList.pop_back();
00777
00778
00779 m_MovingPointSetNode->SetMapper(1, NULL);
00780
00781 mitk::BaseData::Pointer movingData = m_MovingNode->GetData();
00782 mitk::AffineGeometryFrame3D::Pointer movingGeometry = movingData->GetGeometry(0)->Clone();
00783 m_RedoGeometryList.push_back(static_cast<mitk::Geometry3D *>(movingGeometry.GetPointer()));
00784 movingData->SetGeometry(m_UndoGeometryList.back());
00785 m_UndoGeometryList.pop_back();
00786
00787
00788 m_MovingNode->SetMapper(1, NULL);
00789 mitk::RenderingManager::GetInstance()->RequestUpdate(m_MultiWidget->mitkWidget4->GetRenderWindow());
00790
00791 movingData->GetTimeSlicedGeometry()->UpdateInformation();
00792 m_MovingLandmarks->GetTimeSlicedGeometry()->UpdateInformation();
00793 m_Controls.m_RedoTransformation->setEnabled(true);
00794 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00795 this->checkLandmarkError();
00796 }
00797 if(!m_UndoPointsGeometryList.empty())
00798 {
00799 m_Controls.m_UndoTransformation->setEnabled(true);
00800 }
00801 else
00802 {
00803 m_Controls.m_UndoTransformation->setEnabled(false);
00804 }
00805 }
00806
00807 void QmitkPointBasedRegistrationView::RedoTransformation()
00808 {
00809 if(!m_RedoPointsGeometryList.empty())
00810 {
00811 mitk::AffineGeometryFrame3D::Pointer movingLandmarksGeometry = m_MovingLandmarks->GetGeometry(0)->Clone();
00812 m_UndoPointsGeometryList.push_back(static_cast<mitk::Geometry3D *>(movingLandmarksGeometry.GetPointer()));
00813 m_MovingLandmarks->SetGeometry(m_RedoPointsGeometryList.back());
00814 m_RedoPointsGeometryList.pop_back();
00815
00816
00817 m_MovingPointSetNode->SetMapper(1, NULL);
00818
00819 mitk::BaseData::Pointer movingData = m_MovingNode->GetData();
00820 mitk::AffineGeometryFrame3D::Pointer movingGeometry = movingData->GetGeometry(0)->Clone();
00821 m_UndoGeometryList.push_back(static_cast<mitk::Geometry3D *>(movingGeometry.GetPointer()));
00822 movingData->SetGeometry(m_RedoGeometryList.back());
00823 m_RedoGeometryList.pop_back();
00824
00825
00826 m_MovingNode->SetMapper(1, NULL);
00827 mitk::RenderingManager::GetInstance()->RequestUpdate(m_MultiWidget->mitkWidget4->GetRenderWindow());
00828
00829 movingData->GetTimeSlicedGeometry()->UpdateInformation();
00830 m_MovingLandmarks->GetTimeSlicedGeometry()->UpdateInformation();
00831 m_Controls.m_UndoTransformation->setEnabled(true);
00832 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00833 this->checkLandmarkError();
00834 }
00835 if(!m_RedoPointsGeometryList.empty())
00836 {
00837 m_Controls.m_RedoTransformation->setEnabled(true);
00838 }
00839 else
00840 {
00841 m_Controls.m_RedoTransformation->setEnabled(false);
00842 }
00843 }
00844
00845 void QmitkPointBasedRegistrationView::showRedGreen(bool redGreen)
00846 {
00847 m_ShowRedGreen = redGreen;
00848 this->setImageColor(m_ShowRedGreen);
00849 }
00850
00851 void QmitkPointBasedRegistrationView::setImageColor(bool redGreen)
00852 {
00853 if (!redGreen && m_FixedNode.IsNotNull())
00854 {
00855 m_FixedNode->SetColor(m_FixedColor);
00856 }
00857 if (!redGreen && m_MovingNode.IsNotNull())
00858 {
00859 m_MovingNode->SetColor(m_MovingColor);
00860 }
00861 if (redGreen && m_FixedNode.IsNotNull())
00862 {
00863 m_FixedNode->SetColor(1.0f, 0.0f, 0.0f);
00864 }
00865 if (redGreen && m_MovingNode.IsNotNull())
00866 {
00867 m_MovingNode->SetColor(0.0f, 1.0f, 0.0f);
00868 }
00869 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00870 }
00871
00872 void QmitkPointBasedRegistrationView::OpacityUpdate(float opacity)
00873 {
00874 if (opacity > 1)
00875 {
00876 opacity = opacity/100.0f;
00877 }
00878 m_Opacity = opacity;
00879 if (m_MovingNode.IsNotNull())
00880 {
00881 m_MovingNode->SetOpacity(m_Opacity);
00882 }
00883 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
00884 }
00885
00886 void QmitkPointBasedRegistrationView::OpacityUpdate(int opacity)
00887 {
00888 float fValue = ((float)opacity)/100.0f;
00889 this->OpacityUpdate(fValue);
00890 }
00891
00892 void QmitkPointBasedRegistrationView::clearTransformationLists()
00893 {
00894 m_Controls.m_UndoTransformation->setEnabled(false);
00895 m_Controls.m_RedoTransformation->setEnabled(false);
00896 m_Controls.m_MeanErrorLCD->hide();
00897 m_Controls.m_MeanError->hide();
00898 m_UndoGeometryList.clear();
00899 m_UndoPointsGeometryList.clear();
00900 m_RedoGeometryList.clear();
00901 m_RedoPointsGeometryList.clear();
00902 }
00903
00904 void QmitkPointBasedRegistrationView::checkLandmarkError()
00905 {
00906 double totalDist = 0, dist = 0, dist2 = 0;
00907 mitk::Point3D point1, point2, point3;
00908 double p1[3], p2[3];
00909 if(m_Transformation < 3)
00910 {
00911 if (m_Controls.m_UseICP->isChecked())
00912 {
00913 if (m_MovingLandmarks.IsNotNull() && m_FixedLandmarks.IsNotNull()&& m_MovingLandmarks->GetSize() != 0 && m_FixedLandmarks->GetSize() != 0)
00914 {
00915 for(int pointId = 0; pointId < m_MovingLandmarks->GetSize(); ++pointId)
00916 {
00917 point1 = m_MovingLandmarks->GetPoint(pointId);
00918 point2 = m_FixedLandmarks->GetPoint(0);
00919 p1[0] = point1[0]; p1[1] = point1[1]; p1[2] = point1[2];
00920 p2[0] = point2[0]; p2[1] = point2[1]; p2[2] = point2[2];
00921 dist = vtkMath::Distance2BetweenPoints(p1, p2);
00922 for(int pointId2 = 1; pointId2 < m_FixedLandmarks->GetSize(); ++pointId2)
00923 {
00924 point2 = m_FixedLandmarks->GetPoint(pointId2);
00925 p1[0] = point1[0]; p1[1] = point1[1]; p1[2] = p1[2];
00926 p2[0] = point2[0]; p2[1] = point2[1]; p2[2] = p2[2];
00927 dist2 = vtkMath::Distance2BetweenPoints(p1, p2);
00928 if (dist2 < dist)
00929 {
00930 dist = dist2;
00931 }
00932 }
00933 totalDist += dist;
00934 }
00935 m_Controls.m_MeanErrorLCD->display(sqrt(totalDist/m_FixedLandmarks->GetSize()));
00936 m_Controls.m_MeanErrorLCD->show();
00937 m_Controls.m_MeanError->show();
00938 }
00939 else
00940 {
00941 m_Controls.m_MeanErrorLCD->hide();
00942 m_Controls.m_MeanError->hide();
00943 }
00944 }
00945 else
00946 {
00947 if (m_MovingLandmarks.IsNotNull() && m_FixedLandmarks.IsNotNull() && m_MovingLandmarks->GetSize() != 0 && m_FixedLandmarks->GetSize() != 0 && m_MovingLandmarks->GetSize() == m_FixedLandmarks->GetSize())
00948 {
00949 for(int pointId = 0; pointId < m_MovingLandmarks->GetSize(); ++pointId)
00950 {
00951 point1 = m_MovingLandmarks->GetPoint(pointId);
00952 point2 = m_FixedLandmarks->GetPoint(pointId);
00953 p1[0] = point1[0]; p1[1] = point1[1]; p1[2] = point1[2];
00954 p2[0] = point2[0]; p2[1] = point2[1]; p2[2] = point2[2];
00955 totalDist += vtkMath::Distance2BetweenPoints(p1, p2);
00956 }
00957 m_Controls.m_MeanErrorLCD->display(sqrt(totalDist/m_FixedLandmarks->GetSize()));
00958 m_Controls.m_MeanErrorLCD->show();
00959 m_Controls.m_MeanError->show();
00960 }
00961 else
00962 {
00963 m_Controls.m_MeanErrorLCD->hide();
00964 m_Controls.m_MeanError->hide();
00965 }
00966 }
00967 }
00968 else
00969 {
00970 if (m_MovingLandmarks.IsNotNull() && m_FixedLandmarks.IsNotNull() && m_MovingLandmarks->GetSize() != 0 && m_FixedLandmarks->GetSize() != 0 && m_MovingLandmarks->GetSize() == m_FixedLandmarks->GetSize())
00971 {
00972 for(int pointId = 0; pointId < m_MovingLandmarks->GetSize(); ++pointId)
00973 {
00974 point1 = m_MovingLandmarks->GetPoint(pointId);
00975 point2 = m_FixedLandmarks->GetPoint(pointId);
00976 p1[0] = point1[0]; p1[1] = point1[1]; p1[2] = point1[2];
00977 p2[0] = point2[0]; p2[1] = point2[1]; p2[2] = point2[2];
00978 totalDist += vtkMath::Distance2BetweenPoints(p1, p2);
00979 }
00980 m_Controls.m_MeanErrorLCD->display(sqrt(totalDist/m_FixedLandmarks->GetSize()));
00981 m_Controls.m_MeanErrorLCD->show();
00982 m_Controls.m_MeanError->show();
00983 }
00984 else
00985 {
00986 m_Controls.m_MeanErrorLCD->hide();
00987 m_Controls.m_MeanError->hide();
00988 }
00989 }
00990 }
00991
00992 void QmitkPointBasedRegistrationView::transformationChanged(int transform)
00993 {
00994 m_Transformation = transform;
00995 this->checkCalculateEnabled();
00996 this->checkLandmarkError();
00997 }
00998
00999
01000 void QmitkPointBasedRegistrationView::calculateLandmarkbasedWithICP()
01001 {
01002 if(CheckCalculate())
01003 {
01004 mitk::Geometry3D::Pointer pointsGeometry = m_MovingLandmarks->GetGeometry(0);
01005 mitk::AffineGeometryFrame3D::Pointer movingLandmarksGeometry = m_MovingLandmarks->GetGeometry(0)->Clone();
01006 m_UndoPointsGeometryList.push_back(static_cast<mitk::Geometry3D *>(movingLandmarksGeometry.GetPointer()));
01007
01008 mitk::BaseData::Pointer originalData = m_MovingNode->GetData();
01009 mitk::AffineGeometryFrame3D::Pointer originalDataGeometry = originalData->GetGeometry(0)->Clone();
01010 m_UndoGeometryList.push_back(static_cast<mitk::Geometry3D *>(originalDataGeometry.GetPointer()));
01011
01012 vtkIdType pointId;
01013 vtkPoints* vPointsSource=vtkPoints::New();
01014 vtkCellArray* vCellsSource=vtkCellArray::New();
01015 for(pointId=0; pointId<m_MovingLandmarks->GetSize();++pointId)
01016 {
01017 mitk::Point3D pointSource=m_MovingLandmarks->GetPoint(pointId);
01018 vPointsSource->InsertNextPoint(pointSource[0],pointSource[1],pointSource[2]);
01019 vCellsSource->InsertNextCell(1, &pointId);
01020 }
01021
01022 vtkPoints* vPointsTarget=vtkPoints::New();
01023 vtkCellArray* vCellsTarget = vtkCellArray::New();
01024 for(pointId=0; pointId<m_FixedLandmarks->GetSize();++pointId)
01025 {
01026 mitk::Point3D pointTarget=m_FixedLandmarks->GetPoint(pointId);
01027 vPointsTarget->InsertNextPoint(pointTarget[0],pointTarget[1],pointTarget[2]);
01028 vCellsTarget->InsertNextCell(1, &pointId);
01029 }
01030
01031 vtkPolyData* vPointSetSource=vtkPolyData::New();
01032 vtkPolyData* vPointSetTarget=vtkPolyData::New();
01033
01034 vPointSetTarget->SetPoints(vPointsTarget);
01035 vPointSetTarget->SetVerts(vCellsTarget);
01036 vPointSetSource->SetPoints(vPointsSource);
01037 vPointSetSource->SetVerts(vCellsSource);
01038
01039 vtkIterativeClosestPointTransform * icp=vtkIterativeClosestPointTransform::New();
01040 icp->SetCheckMeanDistance(1);
01041 icp->SetSource(vPointSetSource);
01042 icp->SetTarget(vPointSetTarget);
01043 icp->SetMaximumNumberOfIterations(50);
01044 icp->StartByMatchingCentroidsOn();
01045 vtkLandmarkTransform * transform=icp->GetLandmarkTransform();
01046 if(m_Transformation==0)
01047 {
01048 transform->SetModeToRigidBody();
01049 }
01050 if(m_Transformation==1)
01051 {
01052 transform->SetModeToSimilarity();
01053 }
01054 if(m_Transformation==2)
01055 {
01056 transform->SetModeToAffine();
01057 }
01058
01059 vtkMatrix4x4 * matrix=icp->GetMatrix();
01060
01061 double determinant = fabs(matrix->Determinant());
01062 if((determinant < mitk::eps) || (determinant > 100) || (determinant < 0.01)
01063 || (determinant==itk::NumericTraits<double>::infinity())
01064 || (determinant==itk::NumericTraits<double>::quiet_NaN())
01065 || (determinant==itk::NumericTraits<double>::signaling_NaN())
01066 || (determinant==-itk::NumericTraits<double>::infinity())
01067 || (determinant==-itk::NumericTraits<double>::quiet_NaN())
01068 || (determinant==-itk::NumericTraits<double>::signaling_NaN())
01069 || (!(determinant <= 0) && !(determinant > 0)))
01070 {
01071 QMessageBox msgBox;
01072 msgBox.setText("Suspicious determinant of matrix calculated by ICP.\n"
01073 "Please select more points or other points!" );
01074 msgBox.exec();
01075 return;
01076 }
01077
01078 pointsGeometry->Compose(matrix);
01079 m_MovingLandmarks->GetTimeSlicedGeometry()->UpdateInformation();
01080
01081 mitk::BaseData::Pointer movingData = m_MovingNode->GetData();
01082 mitk::Geometry3D::Pointer movingGeometry = movingData->GetGeometry(0);
01083 movingGeometry->Compose(matrix);
01084 movingData->GetTimeSlicedGeometry()->UpdateInformation();
01085 m_Controls.m_UndoTransformation->setEnabled(true);
01086 m_Controls.m_RedoTransformation->setEnabled(false);
01087 m_RedoGeometryList.clear();
01088 m_RedoPointsGeometryList.clear();
01089 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
01090 this->checkLandmarkError();
01091 }
01092 }
01093
01094
01095 void QmitkPointBasedRegistrationView::calculateLandmarkbased()
01096 {
01097 if(CheckCalculate())
01098 {
01099 mitk::Geometry3D::Pointer pointsGeometry = m_MovingLandmarks->GetGeometry(0);
01100 mitk::AffineGeometryFrame3D::Pointer movingLandmarksGeometry = m_MovingLandmarks->GetGeometry(0)->Clone();
01101 m_UndoPointsGeometryList.push_back(static_cast<mitk::Geometry3D *>(movingLandmarksGeometry.GetPointer()));
01102
01103 mitk::BaseData::Pointer originalData = m_MovingNode->GetData();
01104 mitk::AffineGeometryFrame3D::Pointer originalDataGeometry = originalData->GetGeometry(0)->Clone();
01105 m_UndoGeometryList.push_back(static_cast<mitk::Geometry3D *>(originalDataGeometry.GetPointer()));
01106
01107 vtkIdType pointId;
01108 vtkPoints* vPointsSource=vtkPoints::New();
01109 for(pointId = 0; pointId < m_MovingLandmarks->GetSize(); ++pointId)
01110 {
01111 mitk::Point3D sourcePoint = m_MovingLandmarks->GetPoint(pointId);
01112 vPointsSource->InsertNextPoint(sourcePoint[0],sourcePoint[1],sourcePoint[2]);
01113 }
01114 vtkPoints* vPointsTarget=vtkPoints::New();
01115 for(pointId=0; pointId<m_FixedLandmarks->GetSize();++pointId)
01116 {
01117 mitk::Point3D targetPoint=m_FixedLandmarks->GetPoint(pointId);
01118 vPointsTarget->InsertNextPoint(targetPoint[0],targetPoint[1],targetPoint[2]);
01119 }
01120
01121 vtkLandmarkTransform * transform= vtkLandmarkTransform::New();
01122 transform->SetSourceLandmarks(vPointsSource);
01123 transform->SetTargetLandmarks(vPointsTarget);
01124 if(m_Transformation==0)
01125 {
01126 transform->SetModeToRigidBody();
01127 }
01128 if(m_Transformation==1)
01129 {
01130 transform->SetModeToSimilarity();
01131 }
01132 if(m_Transformation==2)
01133 {
01134 transform->SetModeToAffine();
01135 }
01136 vtkMatrix4x4 * matrix=transform->GetMatrix();
01137 double determinant = fabs(matrix->Determinant());
01138 if((determinant < mitk::eps) || (determinant > 100) || (determinant < 0.01)
01139 || (determinant==itk::NumericTraits<double>::infinity())
01140 || (determinant==itk::NumericTraits<double>::quiet_NaN())
01141 || (determinant==itk::NumericTraits<double>::signaling_NaN())
01142 || (determinant==-itk::NumericTraits<double>::infinity())
01143 || (determinant==-itk::NumericTraits<double>::quiet_NaN())
01144 || (determinant==-itk::NumericTraits<double>::signaling_NaN())
01145 || (!(determinant <= 0) && !(determinant > 0)))
01146 {
01147 QMessageBox msgBox;
01148 msgBox.setText("Suspicious determinant of matrix calculated.\n"
01149 "Please select more points or other points!" );
01150 msgBox.exec();
01151 return;
01152 }
01153 pointsGeometry->Compose(matrix);
01154 m_MovingLandmarks->GetTimeSlicedGeometry()->UpdateInformation();
01155
01156 mitk::BaseData::Pointer movingData = m_MovingNode->GetData();
01157 mitk::Geometry3D::Pointer movingGeometry = movingData->GetGeometry(0);
01158 movingGeometry->Compose(matrix);
01159 movingData->GetTimeSlicedGeometry()->UpdateInformation();
01160 m_Controls.m_UndoTransformation->setEnabled(true);
01161 m_Controls.m_RedoTransformation->setEnabled(false);
01162 m_RedoGeometryList.clear();
01163 m_RedoPointsGeometryList.clear();
01164 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
01165 this->checkLandmarkError();
01166 }
01167 }
01168
01169 void QmitkPointBasedRegistrationView::calculateLandmarkWarping()
01170 {
01171 mitk::LandmarkWarping* registration = new mitk::LandmarkWarping();
01172
01173 mitk::LandmarkWarping::FixedImageType::Pointer fixedImage = mitk::LandmarkWarping::FixedImageType::New();
01174 mitk::Image::Pointer fimage = dynamic_cast<mitk::Image*>(m_FixedNode->GetData());
01175 mitk::LandmarkWarping::MovingImageType::Pointer movingImage = mitk::LandmarkWarping::MovingImageType::New();
01176 mitk::Image::Pointer mimage = dynamic_cast<mitk::Image*>(m_MovingNode->GetData());
01177 if (fimage.IsNotNull() && fimage->GetDimension() == 3 && mimage.IsNotNull() && mimage->GetDimension() == 3)
01178 {
01179 mitk::CastToItkImage(fimage, fixedImage);
01180 mitk::CastToItkImage(mimage, movingImage);
01181
01182 registration->SetFixedImage(fixedImage);
01183 registration->SetMovingImage(movingImage);
01184 unsigned int pointId;
01185 mitk::Point3D sourcePoint, targetPoint;
01186 mitk::LandmarkWarping::LandmarkContainerType::Pointer fixedLandmarks = mitk::LandmarkWarping::LandmarkContainerType::New();
01187 mitk::LandmarkWarping::LandmarkPointType point;
01188 for(pointId = 0; pointId < (unsigned int)m_FixedLandmarks->GetSize(); ++pointId)
01189 {
01190 fimage->GetGeometry(0)->WorldToItkPhysicalPoint(m_FixedLandmarks->GetPoint(pointId), point);
01191 fixedLandmarks->InsertElement( pointId, point);
01192 }
01193 mitk::LandmarkWarping::LandmarkContainerType::Pointer movingLandmarks = mitk::LandmarkWarping::LandmarkContainerType::New();
01194 for(pointId = 0; pointId < (unsigned int)m_MovingLandmarks->GetSize(); ++pointId)
01195 {
01196 mitk::BaseData::Pointer fixedData = m_FixedNode->GetData();
01197 mitk::Geometry3D::Pointer fixedGeometry = fixedData->GetGeometry(0);
01198 fixedGeometry->WorldToItkPhysicalPoint(m_MovingLandmarks->GetPoint(pointId), point);
01199 movingLandmarks->InsertElement( pointId, point);
01200 }
01201 registration->SetLandmarks(fixedLandmarks.GetPointer(), movingLandmarks.GetPointer());
01202 mitk::LandmarkWarping::MovingImageType::Pointer output = registration->Register();
01203 if (output.IsNotNull())
01204 {
01205 mitk::Image::Pointer image = mitk::Image::New();
01206 mitk::CastToMitkImage(output, image);
01207 m_MovingNode->SetData(image);
01208 mitk::LevelWindowProperty::Pointer levWinProp = mitk::LevelWindowProperty::New();
01209 mitk::LevelWindow levelWindow;
01210 levelWindow.SetAuto( image );
01211 levWinProp->SetLevelWindow(levelWindow);
01212 m_MovingNode->GetPropertyList()->SetProperty("levelwindow",levWinProp);
01213 movingLandmarks = registration->GetTransformedTargetLandmarks();
01214 mitk::PointSet::PointDataIterator it;
01215 it = m_MovingLandmarks->GetPointSet()->GetPointData()->Begin();
01216
01217 mitk::OperationEvent::IncCurrObjectEventId();
01218 mitk::OperationEvent::ExecuteIncrement();
01219 for(pointId=0; pointId<movingLandmarks->Size();++pointId, ++it)
01220 {
01221 int position = it->Index();
01222 mitk::PointSet::PointType pt = m_MovingLandmarks->GetPoint(position);
01223 mitk::Point3D undoPoint = ( pt );
01224 point = movingLandmarks->GetElement(pointId);
01225 fimage->GetGeometry(0)->ItkPhysicalPointToWorld(point, pt);
01226 mitk::PointOperation* doOp = new mitk::PointOperation(mitk::OpMOVE, pt, position);
01227
01228 mitk::PointOperation* undoOp = new mitk::PointOperation(mitk::OpMOVE, undoPoint, position);
01229 mitk::OperationEvent* operationEvent = new mitk::OperationEvent(m_MovingLandmarks, doOp, undoOp, "Move point");
01230 mitk::UndoController::GetCurrentUndoModel()->SetOperationEvent(operationEvent);
01231
01232
01233 m_MovingLandmarks->ExecuteOperation(doOp);
01234 }
01235 mitk::RenderingManager::GetInstance()->RequestUpdateAll();
01236 this->clearTransformationLists();
01237 this->checkLandmarkError();
01238 }
01239 }
01240 }
01241
01242 bool QmitkPointBasedRegistrationView::checkCalculateEnabled()
01243 {
01244 if (m_FixedLandmarks.IsNotNull() && m_MovingLandmarks.IsNotNull())
01245 {
01246 int fixedPoints = m_FixedLandmarks->GetSize();
01247 int movingPoints = m_MovingLandmarks->GetSize();
01248 if (m_Transformation == 0 || m_Transformation == 1 || m_Transformation == 2)
01249 {
01250 if (m_Controls.m_UseICP->isChecked())
01251 {
01252 if((movingPoints > 0 && fixedPoints > 0))
01253 {
01254 m_Controls.m_Calculate->setEnabled(true);
01255 return true;
01256 }
01257 else
01258 {
01259 m_Controls.m_Calculate->setEnabled(false);
01260 return false;
01261 }
01262 }
01263 else
01264 {
01265 if ((movingPoints == fixedPoints) && movingPoints > 0)
01266 {
01267 m_Controls.m_Calculate->setEnabled(true);
01268 return true;
01269 }
01270 else
01271 {
01272 m_Controls.m_Calculate->setEnabled(false);
01273 return false;
01274 }
01275 }
01276 }
01277 else
01278 {
01279 m_Controls.m_Calculate->setEnabled(true);
01280 return true;
01281 }
01282 }
01283 else
01284 {
01285 return false;
01286 }
01287 }
01288
01289 void QmitkPointBasedRegistrationView::calculate()
01290 {
01291 if (m_Transformation == 0 || m_Transformation == 1 || m_Transformation == 2)
01292 {
01293 if (m_Controls.m_UseICP->isChecked())
01294 {
01295 if (m_MovingLandmarks->GetSize() == 1 && m_FixedLandmarks->GetSize() == 1)
01296 {
01297 this->calculateLandmarkbased();
01298 }
01299 else
01300 {
01301 this->calculateLandmarkbasedWithICP();
01302 }
01303 }
01304 else
01305 {
01306 this->calculateLandmarkbased();
01307 }
01308 }
01309 else
01310 {
01311 this->calculateLandmarkWarping();
01312 }
01313 }
01314
01315 void QmitkPointBasedRegistrationView::SetImagesVisible(berry::ISelection::ConstPointer )
01316 {
01317 if (this->m_CurrentSelection->Size() == 0)
01318 {
01319
01320 mitk::DataStorage::SetOfObjects::ConstPointer setOfObjects = this->GetDataStorage()->GetAll();
01321 for (mitk::DataStorage::SetOfObjects::ConstIterator nodeIt = setOfObjects->Begin()
01322 ; nodeIt != setOfObjects->End(); ++nodeIt)
01323 {
01324 if ( (nodeIt->Value().IsNotNull()) && (nodeIt->Value()->GetProperty("visible")) && dynamic_cast<mitk::Geometry2DData*>(nodeIt->Value()->GetData())==NULL)
01325 {
01326 nodeIt->Value()->SetVisibility(true);
01327 }
01328 }
01329 }
01330 else
01331 {
01332
01333 mitk::DataStorage::SetOfObjects::ConstPointer setOfObjects = this->GetDataStorage()->GetAll();
01334 for (mitk::DataStorage::SetOfObjects::ConstIterator nodeIt = setOfObjects->Begin()
01335 ; nodeIt != setOfObjects->End(); ++nodeIt)
01336 {
01337 if ( (nodeIt->Value().IsNotNull()) && (nodeIt->Value()->GetProperty("visible")) && dynamic_cast<mitk::Geometry2DData*>(nodeIt->Value()->GetData())==NULL)
01338 {
01339 nodeIt->Value()->SetVisibility(false);
01340 }
01341 }
01342 }
01343 }
01344
01345 void QmitkPointBasedRegistrationView::SwitchImages()
01346 {
01347 mitk::DataNode::Pointer newMoving = m_FixedNode;
01348 mitk::DataNode::Pointer newFixed = m_MovingNode;
01349 this->FixedSelected(newFixed);
01350 this->MovingSelected(newMoving);
01351 }
01352
01353