X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=vv%2FvvToolRigidReg.cxx;h=5a11c7f8e367dad022cf08bc8b54cf06a92e92ed;hb=HEAD;hp=41c226424539f769d20084eea0c0e1b73c2e7ece;hpb=367f3d5c15e1793b0fe02fc5ff68f96647ae47f1;p=clitk.git diff --git a/vv/vvToolRigidReg.cxx b/vv/vvToolRigidReg.cxx index 41c2264..5a11c7f 100644 --- a/vv/vvToolRigidReg.cxx +++ b/vv/vvToolRigidReg.cxx @@ -3,7 +3,7 @@ Authors belong to: - University of LYON http://www.universite-lyon.fr/ - - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + - Léon Bérard cancer center http://www.centreleonberard.fr - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr This software is distributed WITHOUT ANY WARRANTY; without even @@ -14,22 +14,35 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html -======================================================================-====*/ +===========================================================================**/ + +// vv #include "vvToolRigidReg.h" -#include -#include -#include +#include "vvSlicer.h" +#include + +// vtk #include #include +#include #include -#include "vvImage.h" -#include "vvSlicer.h" -#include -#include +#include +#include +#include + +// itk +#include + +// clitk +#include "clitkTransformUtilities.h" +#include "clitkMatrix.h" + +// qt #include -#include #include #include + + //------------------------------------------------------------------------------ // Create the tool and automagically (I like this word) insert it in // the main window menu. @@ -44,14 +57,28 @@ vvToolRigidReg::vvToolRigidReg(vvMainWindowBase * parent, Qt::WindowFlags f): { // GUI Initialization Ui_vvToolRigidReg::setupUi(mToolWidget); - QSize qsize; - qsize.setHeight(170); - qsize.setWidth(650); - textEdit->setFixedSize(qsize); - + // Set how many inputs are needed for this tool - AddInputSelector("Select the Reference Image"); - AddInputSelector("Select the Target Image"); + AddInputSelector("Select moving image"); + + QFont font = transformationLabel->font(); + font.setStyleHint(QFont::TypeWriter); + transformationLabel->setFont(font); + + mInitialMatrix = vtkSmartPointer::New(); + + // Set slider ranges, assume degrees, will not be changed for radians + std::vector transSliders, rotSliders; + std::vector transSBs, rotSBs; + GetSlidersAndSpinBoxes(transSliders, rotSliders, transSBs, rotSBs); + for(int i=0; i<3; i++) { + transSliders[i]->setRange(-2000,2000); + rotSliders[i]->setRange(-360,360); + transSBs[i]->setRange(-2000,2000); + transSBs[i]->setDecimals(3); + rotSBs[i]->setRange(-360,360); + rotSBs[i]->setDecimals(3); + } } //------------------------------------------------------------------------------ @@ -62,594 +89,396 @@ vvToolRigidReg::~vvToolRigidReg() //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -bool vvToolRigidReg::close() -{ - - return vvToolWidgetBase::close(); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::reject() +void vvToolRigidReg::Initialize() { - ResetTransform(); - return vvToolWidgetBase::reject(); + SetToolName("Register"); + SetToolMenuName("Register manually"); + SetToolIconFilename(":/common/icons/register.png"); + SetToolTip("Register manually."); + SetToolExperimental(false); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::InputIsSelected(std::vector & l) +void vvToolRigidReg::InputIsSelected(vvSlicerManager *input) { - //inputs - mInput1 = l[0]; - mInput2 = l[1]; - if(mInput1->GetFileName()==mInput2->GetFileName()) - { - QMessageBox::information(this, "Warning","Your Reference and Target Images are the same"); - } - mTwoInputs = true; - SetOverlay(); - mImageSize=mInput1->GetImage()->GetSize(); - - SetRotationCenter(); - SetSliderRanges(); - - //connect all sigs to slots + mInput = input; + HideInputSelector(); + QTabWidget * tab = dynamic_cast(mMainWindow)->GetTab(); + move(tab->mapToGlobal(tab->pos())); + resize(tab->width(), 0); + + //default image rotation center is the center of the image + QString xcord,ycord,zcord; + std::vector imageorigin; + imageorigin=mInput->GetImage()->GetOrigin(); + std::vector imageSize = mInput->GetImage()->GetSize(); + std::vector imageSpacing = mInput->GetImage()->GetSpacing(); + xcord=xcord.setNum(imageorigin[0]+(imageSize[0]-1)*imageSpacing[0]*0.5, 'g', 3); + ycord=ycord.setNum(imageorigin[1]+(imageSize[1]-1)*imageSpacing[1]*0.5, 'g', 3); + zcord=zcord.setNum(imageorigin[2]+(imageSize[2]-1)*imageSpacing[2]*0.5, 'g', 3); + Xval->setText(xcord); + Yval->setText(ycord); + Zval->setText(zcord); + + //backup original matrix + for(int j=0; j<4; j++) + for(int i=0; i<4; i++) + // TODO SR and BP: check on the list of transforms and not the first only + mInitialMatrix->SetElement(i,j, mCurrentSlicerManager->GetImage()->GetTransform()[0]->GetMatrix()->GetElement(i,j)); + QString origTransformString(clitk::Get4x4MatrixDoubleAsString(mInitialMatrix).c_str()); + transformationLabel->setText(origTransformString); + SetTransform(mInitialMatrix); + + //connect all sigs to slots connect(resetbutton, SIGNAL(pressed()), this, SLOT(ResetTransform())); - - connect(Xval, SIGNAL(editingFinished()), this, SLOT(SetXvalue())); - connect(Yval, SIGNAL(editingFinished()), this, SLOT(SetYvalue())); - connect(Zval, SIGNAL(editingFinished()), this, SLOT(SetZvalue())); - connect(xtrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateXtranslider())); - - connect(ytrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateYtranslider())); - connect(ztrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateZtranslider())); - - - connect(xrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateXrotslider())); - connect(yrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateYrotslider())); - connect(zrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateZrotslider())); - - - connect(xtrans_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateXtransb())); - connect(ytrans_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateYtransb())); - connect(ztrans_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateZtransb())); - connect(xrot_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateXrotsb())); - connect(yrot_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateYrotsb())); - connect(zrot_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateZrotsb())); - - - connect(loadbutton, SIGNAL(pressed()), this, SLOT(ReadFile())); + connect(loadbutton, SIGNAL(pressed()), this, SLOT(LoadFile())); connect(savebutton, SIGNAL(pressed()), this, SLOT(SaveFile())); + + connect(xtrans_slider, SIGNAL(valueChanged(int)), this, SLOT(SliderChange(int))); + connect(ytrans_slider, SIGNAL(valueChanged(int)), this, SLOT(SliderChange(int))); + connect(ztrans_slider, SIGNAL(valueChanged(int)), this, SLOT(SliderChange(int))); + connect(xrot_slider, SIGNAL(valueChanged(int)), this, SLOT(SliderChange(int))); + connect(yrot_slider, SIGNAL(valueChanged(int)), this, SLOT(SliderChange(int))); + connect(zrot_slider, SIGNAL(valueChanged(int)), this, SLOT(SliderChange(int))); + connect(xtrans_sb, SIGNAL(valueChanged(double)), this, SLOT(SpinBoxChange(double))); + connect(ytrans_sb, SIGNAL(valueChanged(double)), this, SLOT(SpinBoxChange(double))); + connect(ztrans_sb, SIGNAL(valueChanged(double)), this, SLOT(SpinBoxChange(double))); + connect(xrot_sb, SIGNAL(valueChanged(double)), this, SLOT(SpinBoxChange(double))); + connect(yrot_sb, SIGNAL(valueChanged(double)), this, SLOT(SpinBoxChange(double))); + connect(zrot_sb, SIGNAL(valueChanged(double)), this, SLOT(SpinBoxChange(double))); + + connect(stepTransSpinBox, SIGNAL(valueChanged(double)), this, SLOT(SetTranslationStep(double))); + connect(stepRotSpinBox, SIGNAL(valueChanged(double)), this, SLOT(SetRotationStep(double))); + + connect(checkBoxDegrees, SIGNAL(stateChanged(int)), this, SLOT(ToggleSpinBoxAnglesUnit())); + + connect(Xval, SIGNAL(editingFinished()), this, SLOT(ChangeOfRotationCenter())); + connect(Yval, SIGNAL(editingFinished()), this, SLOT(ChangeOfRotationCenter())); + connect(Zval, SIGNAL(editingFinished()), this, SLOT(ChangeOfRotationCenter())); + + // Init step modifiers + stepTransSpinBox->setValue(1.); + stepRotSpinBox->setValue(1.); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ void vvToolRigidReg::apply() { - QMessageBox mb( "Keep Overlay", - "Do you want to keep the overlay on?\n" - "You can switch it off anytime by pressing 'x'", - QMessageBox::Information, - QMessageBox::Yes | QMessageBox::Default, - QMessageBox::No, - QMessageBox::Cancel | QMessageBox::Escape ); - mb.setButtonText( QMessageBox::Yes, "Yes" ); - mb.setButtonText( QMessageBox::No, "No" ); - switch( mb.exec() ) { - case QMessageBox::Yes: - hide(); - break; - case QMessageBox::No: - mInput2->Reload(); - mInput2->Render(); - mInput1->Reload(); - mInput1->Render(); - for(int i=0;iNumberOfSlicers();i++) - { - mInput1->RemoveActor("overlay",0); - mInput1->SetColorMap(0); - mInput1->Render(); - hide(); - } - break; - case QMessageBox::Cancel: - break; - } + vvToolWidgetBase::close(); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::SetOverlay() +bool vvToolRigidReg::close() { - for (int i =0; iNumberOfSlicers(); i++) { - mCurrentSlicerManager->GetSlicer(i)->SetOverlay(mInput2->GetImage()); - mCurrentSlicerManager->GetSlicer(i)->SetActorVisibility("overlay",0,true); - mCurrentSlicerManager->SetColorMap(); - mCurrentSlicerManager->Render(); - } + QString warning = "Are you sure you want to reset the original transform?"; + QMessageBox msgBox(QMessageBox::Warning, tr("Reset transform"),warning, 0, this); + msgBox.addButton(tr("Yes"), QMessageBox::AcceptRole); + msgBox.addButton(tr("No"), QMessageBox::RejectRole); + if (msgBox.exec() == QMessageBox::AcceptRole) { + if (mCurrentSlicerManager) + SetTransform(mInitialMatrix); + return vvToolWidgetBase::close(); } + return false; +} //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::SetXvalue() +void vvToolRigidReg::reject() { - QString xstr = Xval->text(); + return vvToolWidgetBase::reject(); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::SetYvalue() +void vvToolRigidReg::SetTranslationStep(double v) { - QString ystr = Yval->text(); + xtrans_sb->setSingleStep(v); + ytrans_sb->setSingleStep(v); + ztrans_sb->setSingleStep(v); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::SetZvalue() +void vvToolRigidReg::SetRotationStep(double v) { - QString zstr = Zval->text(); + xrot_sb->setSingleStep(v); + yrot_sb->setSingleStep(v); + zrot_sb->setSingleStep(v); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::SetTransform(double tX, double tY, double tZ, double aX, double aY, double aZ,bool update) +void vvToolRigidReg::SliderChange(int newVal) { - - vtkSmartPointer transform = mCurrentSlicerManager->GetImage()->GetTransform(); - transform->PostMultiply(); - //Rotations - if (aX!=0 || aY!=0 || aZ!=0) { - double x, y ,z; - x= Xval->text().toDouble(); - y= Yval->text().toDouble(); - z= Zval->text().toDouble(); - transform->Translate(-x,-y,-z); - if (aX!=0) transform->RotateX(aX); - if (aY!=0) transform->RotateY(aY); - if (aZ!=0) transform->RotateZ(aZ); - transform->Translate(x,y,z); - } - //Translations - if (tX!=0||tY!=0||tZ!=0) - transform->Translate(tX*mInput1->GetImage()->GetSpacing()[0],tY*mInput1->GetImage()->GetSpacing()[1],tZ*mInput1->GetImage()->GetSpacing()[2]); - - transform->Update(); - if(update) - { - for (int i=0; iNumberOfSlicers(); i++) { - mCurrentSlicerManager->GetImage()->UpdateReslice(); - mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent(); - mCurrentSlicerManager->GetSlicer(i)->Render(); + std::vector transSliders, rotSliders; + std::vector transSBs, rotSBs; + GetSlidersAndSpinBoxes(transSliders, rotSliders, transSBs, rotSBs); + for(int i=0; i<3; i++) { + if(transSliders[i] == QObject::sender()) { + transSBs[i]->setValue(newVal); + } + if(rotSliders[i] == QObject::sender()) { + double rad = (checkBoxDegrees->checkState()==Qt::Unchecked)?itk::Math::pi/180.:1.; + rotSBs[i]->setValue(newVal*rad); } } } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::SetTransform(vtkMatrix4x4 *matrix) -{ - vtkSmartPointer transform =mCurrentSlicerManager->GetImage()->GetTransform(); - transform->SetMatrix(matrix); - transform->PostMultiply(); - transform->Concatenate(matrix); - transform->Update(); - mCurrentSlicerManager->GetImage()->SetTransform(transform); - - for (int i=0; iNumberOfSlicers(); i++) { - mCurrentSlicerManager->GetImage()->UpdateReslice(); - mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent(); - mCurrentSlicerManager->GetSlicer(i)->Render(); - } -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ - void vvToolRigidReg::UpdateTextEditor(vtkMatrix4x4 *matrix) +void vvToolRigidReg::SpinBoxChange(double newVal) { - QString str1,str2,str3; - QColor color; - textEdit->clear(); - textEdit->setAcceptRichText(true); - - str2=textEdit->toPlainText(); - str2.append("Rotation Center(mm): \n"); - textEdit->setText(str2); - - str2=textEdit->toPlainText(); - textEdit->setTextColor(QColor(255,0,0)); - str2.append(str3.append(Xval->text())); - textEdit->setText(str2); - str3.clear(); - - str2=textEdit->toPlainText(); - str2.append("\t"); - textEdit->setText(str2); - - str2=textEdit->toPlainText(); - str2.append(str3.append(Yval->text())); - textEdit->setText(str2); - str3.clear(); - - str2=textEdit->toPlainText(); - str2.append("\t"); - textEdit->setText(str2); - - - str2=textEdit->toPlainText(); - str2.append(str3.append(Zval->text())); - textEdit->setText(str2); - - - str2=textEdit->toPlainText(); - str2.append("\n"); - textEdit->setText(str2); - - - str2=textEdit->toPlainText(); - str2.append("Transformation Matrix(mm):\n"); - textEdit->setText(str2); - - for(int i=0;i<4;i++) - { - for(int j=0;j<4;j++) - { - str2=textEdit->toPlainText(); - // str2.append("\t"+str1.setNum(matrix->Element[i][j])); - str2.append(QString("%1\t").arg(str1.setNum(matrix->Element[i][j]),20)); - textEdit->setText(str2); - } - str2=textEdit->toPlainText(); - str2.append("\n"); - textEdit->setText(str2); + std::vector transSliders, rotSliders; + std::vector transSBs, rotSBs; + GetSlidersAndSpinBoxes(transSliders, rotSliders, transSBs, rotSBs); + for(int i=0; i<3; i++) { + if(transSBs[i] == QObject::sender()) { + transSliders[i]->blockSignals(true); + transSliders[i]->setValue(itk::Math::Round(newVal)); + transSliders[i]->blockSignals(false); } - //QString str = QFileDialog::getOpenFileName(); - textEdit->setTextColor(QColor(255,0,0)); - textEdit->setFont(QFont("courrier new",12,4,true)); - textEdit->toPlainText().toAscii(); - - str2=textEdit->toPlainText(); - textEdit->setText(str2); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateXtransb() -{ - int diff=0; - diff=xtrans_sb->value()-xtrans_slider->value(); - this->SetTransform(diff, 0, 0, 0, 0, 0,true); - xtrans_slider->setValue(xtrans_sb->value()); - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); - -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateXtranslider() -{ - int diff=0; - diff=xtrans_slider->value()-xtrans_sb->value(); - this->SetTransform(diff, 0, 0, 0, 0, 0,true); - xtrans_sb->setValue(xtrans_slider->value()); - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateYtransb() -{ - - int diff=0; - diff=ytrans_sb->value()-ytrans_slider->value(); - this->SetTransform(0, diff, 0, 0, 0, 0,true); - ytrans_slider->setValue(ytrans_sb->value()); - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); -} - -//------------------------------------------------------------------------------ + if(rotSBs[i] == QObject::sender()) { + double rad = (checkBoxDegrees->checkState()==Qt::Unchecked)?180./itk::Math::pi:1.; + rotSliders[i]->blockSignals(true); + rotSliders[i]->setValue(itk::Math::Round(newVal*rad)); + rotSliders[i]->blockSignals(false); + } + } -//------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateYtranslider() -{ - int diff=0; - diff=ytrans_slider->value()-ytrans_sb->value(); - this->SetTransform(0, diff, 0, 0, 0, 0,true); - ytrans_sb->setValue(ytrans_slider->value()); - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); + // Compute transform and set + // TODO SR and BP: check on the list of transforms and not the first only + vtkSmartPointer transform_final=mInput->GetImage()->GetTransform()[0]; + transform_final->Identity(); + transform_final->PostMultiply(); + + // Rotations + double x=0, y=0 ,z=0; + x= Xval->text().toDouble(); + y= Yval->text().toDouble(); + z= Zval->text().toDouble(); + double rad = (checkBoxDegrees->checkState()==Qt::Unchecked)?180./itk::Math::pi:1.; + transform_final->Translate(-x,-y,-z); + transform_final->RotateY(yrot_sb->value()*rad); + transform_final->RotateX(xrot_sb->value()*rad); + transform_final->RotateZ(zrot_sb->value()*rad); + transform_final->Translate(x,y,z); + + // Translation + transform_final->Translate(xtrans_sb->value(), + ytrans_sb->value(), + ztrans_sb->value()); + transform_final->Update(); + SetTransform(transform_final->GetMatrix()); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateZtransb() +void vvToolRigidReg::ToggleSpinBoxAnglesUnit() { - - int diff=0; - diff=ztrans_sb->value()-ztrans_slider->value(); - this->SetTransform(0, 0, diff, 0, 0, 0,true); - ztrans_slider->setValue(ztrans_sb->value()); - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); + double rad = (checkBoxDegrees->checkState()==Qt::Unchecked)?itk::Math::pi/180.:180./itk::Math::pi; + std::vector transSliders, rotSliders; + std::vector transSBs, rotSBs; + GetSlidersAndSpinBoxes(transSliders, rotSliders, transSBs, rotSBs); + for(int i=0; i<3; i++) { + rotSBs[i]->blockSignals(true); + rotSBs[i]->setValue(rotSBs[i]->value()*rad); + rotSBs[i]->blockSignals(false); + } } - //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateZtranslider() +void vvToolRigidReg::SaveFile() { - int diff=0; - diff=ztrans_slider->value()-ztrans_sb->value(); - this->SetTransform(0, 0, diff, 0, 0, 0,true); - ztrans_sb->setValue(ztrans_slider->value()); - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); + //Write the Transformation Matrix + std::string absPath = mCurrentSlicerManager->GetFileName(); + absPath = itksys::SystemTools::GetFilenameWithoutExtension(absPath) + std::string(".mat"); + QString filename = QFileDialog::getSaveFileName(this, tr("Save Transformation Matrix File"), + absPath.c_str(), + tr("Text (*.mat *.txt *.doc *.rtf)")); + + QFile file(filename); + if (file.open(QFile::WriteOnly | QFile::Truncate)) { + // TODO SR and BP: check on the list of transforms and not the first only + vtkMatrix4x4* matrix = mCurrentSlicerManager->GetImage()->GetTransform()[0]->GetMatrix(); + QString matrixStr = clitk::Get4x4MatrixDoubleAsString(matrix,16).c_str(); + QTextStream out(&file); + out << matrixStr; + } + else + { + QMessageBox::information(this,"Error","Unable to open file for writing"); + } } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateXrotsb() +void vvToolRigidReg::LoadFile() { - - int diff=0; - diff=xrot_sb->value()-xrot_slider->value(); - this->SetTransform(0, 0, 0, diff, 0, 0,true); - xrot_slider->setValue(xrot_sb->value()); - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); + //Open File to read the transformation parameters + QString file = QFileDialog::getOpenFileName( + this, + "Choose the filename for the transformation matrix", + vtksys::SystemTools::GetFilenamePath(mCurrentSlicerManager->GetFileName()).c_str(), + "Text (*.mat *.txt *.rtf *.doc)"); + if (file.isEmpty()) + return; + + + itk::Matrix itkMat = clitk::ReadMatrix3D(file.toStdString()); + vtkSmartPointer matrix = vtkSmartPointer::New(); + matrix->Identity(); + for(int j=0; j<4; j++) + for(int i=0; i<4; i++) + matrix->SetElement(j,i,itkMat[j][i]); + SetTransform(matrix); } - //------------------------------------------------------------------------------ - + //------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateXrotslider() +void vvToolRigidReg::ChangeOfRotationCenter() { - int diff=0; - diff=xrot_slider->value()-xrot_sb->value(); - this->SetTransform(0, 0, 0, diff, 0, 0,true); - xrot_sb->setValue(xrot_slider->value()); - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); + // TODO SR and BP: check on the list of transforms and not the first only + SetTransform(mCurrentSlicerManager->GetImage()->GetTransform()[0]->GetMatrix()); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateYrotsb() +void vvToolRigidReg::ResetTransform() { - - int diff=0; - diff=yrot_sb->value()-yrot_slider->value(); - this->SetTransform(0, 0, 0, 0, diff, 0,true); - yrot_slider->setValue(yrot_sb->value()); - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); + SetTransform(mInitialMatrix); } - //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateYrotslider() +void vvToolRigidReg::SetTransform(vtkMatrix4x4 *matrix) { - int diff=0; - diff=yrot_slider->value()-yrot_sb->value(); - this->SetTransform(0, 0, 0, 0, diff, 0,true); - yrot_sb->setValue(yrot_slider->value()); - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); + vtkSmartPointer transform=vtkSmartPointer::New(); + // TODO SR and BP: check on the list of transforms and not the first only + mCurrentSlicerManager->GetImage()->GetTransform()[0]->SetMatrix(matrix); + transform->Update(); + Render(); + dynamic_cast(mMainWindow)->ImageInfoChanged(); + + // Compute parameters from transfer using itk Euler transform + itk::Euler3DTransform::CenterType center; + center[0] = Xval->text().toDouble(); + center[1] = Yval->text().toDouble(); + center[2] = Zval->text().toDouble(); + itk::Euler3DTransform::MatrixType rotMat; + itk::Euler3DTransform::OutputVectorType transVec; + for(int i=0; i<3; i++) { + transVec[i] = matrix->GetElement(i,3); + for(int j=0; j<3; j++) + rotMat[i][j] = matrix->GetElement(i,j); + } + itk::Euler3DTransform::Pointer euler; + euler = itk::Euler3DTransform::New(); + euler->SetCenter(center); + try { +#if ITK_VERSION_MAJOR > 4 || (ITK_VERSION_MAJOR == 4 && ITK_VERSION_MINOR > 6) + euler->SetMatrix(rotMat,0.00001); +#else + euler->SetMatrix(rotMat); +#endif + } catch (itk::ExceptionObject) { + QString warning = "The matrice is a non-orthogonal rotation matrix.\nThe manual registration doesn't work."; + QMessageBox msgBox(QMessageBox::Warning, tr("Reset transform"),warning, 0, this); + msgBox.addButton(tr("OK"), QMessageBox::AcceptRole); + if (msgBox.exec() == QMessageBox::AcceptRole) { + //SetTransform(mInitialMatrix); + vvToolWidgetBase::close(); + } + } + euler->SetOffset(transVec); + + // Modify GUI according to the new parameters + std::vector transSliders, rotSliders; + std::vector transSBs, rotSBs; + GetSlidersAndSpinBoxes(transSliders, rotSliders, transSBs, rotSBs); + for(int i=0; i<3; i++) { + // Translations + transSBs[i]->blockSignals(true); + transSBs[i]->setValue( euler->GetParameters()[i+3] ); + transSBs[i]->blockSignals(false); + transSliders[i]->blockSignals(true); + transSliders[i]->setValue( itk::Math::Round(euler->GetParameters()[i+3]) ); + transSliders[i]->blockSignals(false); + + // Rotations + double rad = (checkBoxDegrees->checkState()==Qt::Checked)?180./itk::Math::pi:1.; + double angleDiff = euler->GetParameters()[i]-rotSBs[i]->value()/rad+2*itk::Math::pi; + angleDiff = angleDiff - 2*itk::Math::pi*itk::Math::Round(angleDiff/(2*itk::Math::pi)); + if(std::abs(angleDiff)>1.e-4) { + rotSBs[i]->blockSignals(true); + rotSBs[i]->setValue( euler->GetParameters()[i]*rad ); + rotSBs[i]->blockSignals(false); + } + int iAngle = itk::Math::Round(euler->GetParameters()[i]*180./itk::Math::pi); + if((iAngle-rotSliders[i]->value()+360)%360!=0) { + rotSliders[i]->blockSignals(true); + rotSliders[i]->setValue(iAngle); + rotSliders[i]->blockSignals(false); + } + } } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateZrotsb() +// Just an helper function to shorten the code with loops on sliders and spinboxes +void vvToolRigidReg::GetSlidersAndSpinBoxes(std::vector&transSliders, std::vector&rotSliders, + std::vector&transSBs, std::vector&rotSBs) { + transSliders.push_back(xtrans_slider); + transSliders.push_back(ytrans_slider); + transSliders.push_back(ztrans_slider); - int diff=0; - diff=zrot_sb->value()-zrot_slider->value(); - this->SetTransform(0, 0, 0, 0, 0,diff,true); - zrot_slider->setValue(zrot_sb->value()); - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); -} - -//------------------------------------------------------------------------------ + rotSliders.push_back(xrot_slider); + rotSliders.push_back(yrot_slider); + rotSliders.push_back(zrot_slider); -//------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateZrotslider() -{ - int diff=0; - diff=zrot_slider->value()-zrot_sb->value(); - this->SetTransform(0, 0, 0, 0, 0, diff,true); - zrot_sb->setValue(zrot_slider->value()); - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); -} -//------------------------------------------------------------------------------ + transSBs.push_back(xtrans_sb); + transSBs.push_back(ytrans_sb); + transSBs.push_back(ztrans_sb); -//------------------------------------------------------------------------------ -void vvToolRigidReg::SaveFile() -{ - QString s = QFileDialog::getSaveFileName(this, tr("Save File"), - "/home", - tr("Text (*.txt *.doc *.rtf)")); - QFile file(s); - if (file.open(QFile::WriteOnly | QFile::Truncate)) { - QTextStream out(&file); - out<toPlainText() ; - } + rotSBs.push_back(xrot_sb); + rotSBs.push_back(yrot_sb); + rotSBs.push_back(zrot_sb); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::ReadFile() +void vvToolRigidReg::ExtentMax(const double pointExtent[8][4], double maxExtent[2][3]) { - vtkMatrix4x4 *matrix=vtkMatrix4x4::New(); - vtkSmartPointer transform = mCurrentSlicerManager->GetImage()->GetTransform(); - QString s = QFileDialog::getOpenFileName( - this, - "Choose a file", - "/home", - "Text (*.txt *.rtf *.doc)"); - if (s.isEmpty()) - return; - - QFile file(s); - ifstream readfile; - std::string x= s.toStdString(); - std::vector Qstr; - readfile.open(x.c_str()); - if (readfile.is_open()) - { - while (!readfile.eof()) - { - readfile >> x; - Qstr.push_back(QString(x.c_str())); - } - readfile.close(); - } - else { - cout << "Unable to open file"; + double max, min; + for (int i=0; i<3; ++i) { + max = pointExtent[0][i]; + min = pointExtent[0][i]; + for (int j=1; j<8; ++j) { + if (pointExtent[j][i] > max) { + max = pointExtent[j][i]; + } + if (pointExtent[j][i] < min) { + min = pointExtent[j][i]; + } + } + maxExtent[0][i] = min; + maxExtent[1][i] = max; } - //Set the transformation matrix - int index =7; - for(int i=0;i<4;i++) - { - for(int j=0;j<4;j++) - { - matrix->Element[i][j]=Qstr.at(index).toDouble(); - index++; - } - } - transform->SetMatrix(matrix); - // SetTransform(matrix); - double * orientations=new double[3]; - double * translations=new double[3]; - transform->GetOrientation(orientations); - transform->PostMultiply(); - - //Obtain the Rotation Center - Xval->setText(Qstr.at(2)); - Yval->setText(Qstr.at(3)); - Zval->setText(Qstr.at(4)); - - //In the Order or Y X Z // - //now postmultiply for the rotations - SetTransform(0,0,0,0,0,-orientations[2],false); - SetTransform(0,0,0,-orientations[0],0,0,false); - SetTransform(0,0,0,0,-orientations[1],0,false); - - transform->GetPosition(translations); - transform->Identity(); - - /* DD(translations[0]/mInput1->GetImage()->GetSpacing()[0]); - DD(translations[1]/mInput1->GetImage()->GetSpacing()[1]); - DD(translations[2]/mInput1->GetImage()->GetSpacing()[2]); - DD(orientations[0]); - DD(orientations[1]); - DD(orientations[2]);*/ - //set the sliders and spin box values - xtrans_slider->setValue(round(translations[0]/mInput1->GetImage()->GetSpacing()[0])); - UpdateXtranslider(); - ytrans_slider->setValue(round(translations[1]/mInput1->GetImage()->GetSpacing()[1])); - UpdateYtranslider(); - ztrans_slider->setValue(round(translations[2]/mInput1->GetImage()->GetSpacing()[2])); - UpdateZtranslider(); - yrot_slider->setValue(round(orientations[1])); - UpdateYrotslider(); - xrot_slider->setValue(round(orientations[0])); - UpdateXrotslider(); - zrot_slider->setValue(round(orientations[2])); - UpdateZrotslider(); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::ResetTransform() -{ - vtkMatrix4x4 *matrix = vtkMatrix4x4::New(); - matrix->Identity(); - mInput1->Reload(); - mInput1->Render(); - mInput2->Reload(); - mInput2->Render(); - SetRotationCenter(); - SetSliderRanges(); - SetTransform(matrix); - UpdateTextEditor(matrix); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::SetRotationCenter() +void vvToolRigidReg::Render() { - //default image rotation center is the center of the image - QString xcord,ycord,zcord; - std::vector imageorigin; - imageorigin=mInput1->GetImage()->GetOrigin(); - - xcord=xcord.setNum(imageorigin[0]+mImageSize[0]*mInput1->GetImage()->GetSpacing()[0]/2, 'g', 3); - ycord=ycord.setNum(imageorigin[1]+mImageSize[1]*mInput1->GetImage()->GetSpacing()[1]/2, 'g', 3); - zcord=zcord.setNum(imageorigin[2]+mImageSize[2]*mInput1->GetImage()->GetSpacing()[2]/2, 'g', 3); - - Xval->setText(xcord); - Yval->setText(ycord); - Zval->setText(zcord); - InitializeSliders(); +for (int i=0; iGetNumberOfSlicers(); i++) { + mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent(); + mCurrentSlicerManager->GetSlicer(i)->Render(); } -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::InitializeSliders() -{ - xtrans_slider->signalsBlocked(); - ytrans_slider->signalsBlocked(); - ztrans_slider->signalsBlocked(); - xtrans_sb->signalsBlocked(); - ytrans_sb->signalsBlocked(); - ztrans_sb->signalsBlocked(); - xrot_slider->signalsBlocked(); - yrot_slider->signalsBlocked(); - zrot_slider->signalsBlocked(); - xrot_sb->signalsBlocked(); - yrot_sb->signalsBlocked(); - zrot_sb->signalsBlocked(); - - xtrans_sb->setValue(0); - ytrans_sb->setValue(0); - ztrans_sb->setValue(0); - - xtrans_slider->setValue(0); - ytrans_slider->setValue(0); - ztrans_slider->setValue(0); - - xrot_sb->setValue(0); - yrot_sb->setValue(0); - zrot_sb->setValue(0); - - xrot_slider->setValue(0); - yrot_slider->setValue(0); - zrot_slider->setValue(0); } //------------------------------------------------------------------------------ -//------------------------------------------------------------------------------ -void vvToolRigidReg::SetSliderRanges() -{ - xtrans_slider->setMaximum(1000); - xtrans_slider->setMinimum(-1000); - ytrans_slider->setMaximum(1000); - ytrans_slider->setMinimum(-1000); - ztrans_slider->setMaximum(1000); - ztrans_slider->setMinimum(-1000); - - xtrans_sb->setMaximum(1000); - xtrans_sb->setMinimum(-1000); - ytrans_sb->setMaximum(1000); - ytrans_sb->setMinimum(-1000); - ztrans_sb->setMaximum(1000); - ztrans_sb->setMinimum(-1000); - - xrot_slider->setMaximum(360); - xrot_slider->setMinimum(-360); - yrot_slider->setMaximum(360); - yrot_slider->setMinimum(-360); - zrot_slider->setMaximum(360); - zrot_slider->setMinimum(-360); - - xrot_sb->setMaximum(360); - xrot_sb->setMinimum(-360); - yrot_sb->setMaximum(360); - yrot_sb->setMinimum(-360); - zrot_sb->setMaximum(360); - zrot_sb->setMinimum(-360); -}