From f08c6ee2ae829766aa8871f76feab9bca7d8afd1 Mon Sep 17 00:00:00 2001 From: Simon Rit Date: Mon, 13 Feb 2012 14:28:08 +0100 Subject: [PATCH] Put back and refactored manual rigid registration --- vv/CMakeLists.txt | 3 +- vv/qt_ui/vvToolRigidReg.ui | 1009 +++++++++---------------- vv/vvToolRigidReg.cxx | 1454 +++++++----------------------------- vv/vvToolRigidReg.h | 67 +- 4 files changed, 641 insertions(+), 1892 deletions(-) diff --git a/vv/CMakeLists.txt b/vv/CMakeLists.txt index 8ecd3cc..320e194 100644 --- a/vv/CMakeLists.txt +++ b/vv/CMakeLists.txt @@ -12,7 +12,7 @@ endif(COMMAND cmake_policy) #List of vv tools to compile SET(vv_TOOLS vvToolMedianFilter -# vvToolRigidReg ## Very long to compile + vvToolRigidReg vvToolCropImage vvToolBinarize vvToolImageArithm @@ -32,7 +32,6 @@ SET(vv_TOOLS # dependencies of the tools # Future issues : when tool depend on other tools ... how to manage that ? # >> add the relevant $_LIBS variable to the tool that has dependencies? -SET(vvToolRigidReg_LIBS clitkAffineRegistrationLib) SET(vvToolBinarize_LIBS clitkBinarizeImageLib) SET(vvToolResample_LIBS clitkResampleImageLib) SET(vvToolConvert_LIBS clitkImageConvertLib) diff --git a/vv/qt_ui/vvToolRigidReg.ui b/vv/qt_ui/vvToolRigidReg.ui index 296e6d5..10e77d6 100644 --- a/vv/qt_ui/vvToolRigidReg.ui +++ b/vv/qt_ui/vvToolRigidReg.ui @@ -6,8 +6,8 @@ 0 0 - 757 - 472 + 266 + 333 @@ -15,669 +15,376 @@ - - - 1 - - - - Manual - - - - - - - - - - Rotation Center(X,Y,Z) - - - - - - - - - - - - - - - - Reset - - - - + + + + + + + X + + + + + + + + + + Qt::Horizontal + + + + + + + + + + + + + + X + + + + + + + + + + Qt::Horizontal + + + + + + + + + + + + + + Z + + + + + + + + + + Qt::Horizontal + + + + + + + + + + + + + + + 0 + 0 + + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600; color:#0000ff;">Original Transformation Matrix:</span></p></body></html> + + + + + + + + 0 + 0 + + + + + Monospace + 8 + + + + + + + Qt::PlainText + + + + + + + + + Save + + - - - - - - X Trans - - - - - - - - - - Qt::Horizontal - - - - - - - + + + + Load + + - - - - - - Y Rot - - - - - - - - - - Qt::Horizontal - - - - - - - + + + + Reset + + - - - - - - X Rot - - - - - - - - - - Qt::Horizontal - - - - - - - + + + + + + + + + + Z + + + + + + + + + + Qt::Horizontal + + + + + + + + + + + + + + Y + + + + + + + + + + Qt::Horizontal + + + + + + + + + + + + + + + + + Y + + + + + + + + + + Qt::Horizontal + + + + + + + + + + + + + + + 14 + 75 + true + + + + Translations + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Steps + + + + + + + Translations + + + 1.000000000000000 + + + 1.000000000000000 + + + + + + + + + + + + 14 + 75 + true + + + + Rotations + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Deg + + + true + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + - - - - - - Z Rot - - - - - - - - - - Qt::Horizontal - - - - - - - + + + + Steps + + - - - - - - - 0 - 0 - - - - - - - - Load - - - - - - - Save - - - - - - - - 0 - 0 - - - - - - - - Initial Transform - - - Qt::AlignBottom|Qt::AlignHCenter - - - - - - - Current Transform - - - Qt::AlignCenter - - - - + + + + Rotations + + + 1.000000000000000 + + - - - - - - Z Trans - - - - - - - - - - Qt::Horizontal - - - - - - - + + + + + + + + + + Center + + + + + + + - - - - - - Y Trans - - - - - - - - - - Qt::Horizontal - - - - - - - + + - - + + - - - - Automatic - - - - - 0 - 0 - 721 - 421 - - - - - - - - - - - - - - - - - - - - - - 20 - 228 - 90 - 32 - - - - Run! - - - - - - 20 - 183 - 91 - 32 - - - - font: 9pt "Arial"; - - - Load - - - - - - 30 - 113 - 87 - 20 - - - - Rigid - - - - - - 30 - 143 - 101 - 20 - - - - Deformable - - - - - - 20 - 273 - 91 - 32 - - - - font: 9pt "Arial"; - - - Save - - - - - - 160 - 90 - 151 - 26 - - - - - Identity - - - - - Translations - - - - - Rigid - - - - - Affine - - - - - - - 204 - 70 - 71 - 16 - - - - Transform - - - - - - 201 - 120 - 81 - 16 - - - - Interpolator - - - - - - 160 - 140 - 151 - 26 - - - - - Nearest Neighborhood - - - - - Linear - - - - - BSpline - - - - - BLUT - - - - - - - 204 - 170 - 71 - 16 - - - - Optimizer - - - - - - 160 - 190 - 151 - 26 - - - - - Simplex - - - - - Powell - - - - - FRPR - - - - - Regular Step GD - - - - - VersorRigid3D - - - - - Conjugated Gradient - - - - - L-BFGS - - - - - L-BFGS-B - - - - - - - 218 - 220 - 60 - 16 - - - - Metric - - - - - - 160 - 240 - 151 - 26 - - - - - Mean Squares - - - - - Normalized CC - - - - - Histogram CC - - - - - Gradient Difference - - - - - Viola Wells MI - - - - - Histogram MI - - - - - Mattes MI - - - - - Normalized MI - - - - - CR - - - - - SSD (BLUT FFD) - - - - - - - 162 - 294 - 151 - 26 - - - - - Transformed Image - - - - - Checker After Reg - - - - - Checker Before Reg - - - - - Difference After Reg - - - - - Difference Before Reg - - - - - - - 195 - 274 - 91 - 20 - - - - Select Output - - - - - - 208 - 20 - 71 - 16 - - - - Presets - - - - - - 160 - 40 - 151 - 26 - - - - - Mono Modal Registration - - - - - Multi Modal Registration - - - - - - - - - - - + + - - - + diff --git a/vv/vvToolRigidReg.cxx b/vv/vvToolRigidReg.cxx index e99d867..1a2d1c7 100644 --- a/vv/vvToolRigidReg.cxx +++ b/vv/vvToolRigidReg.cxx @@ -18,7 +18,6 @@ // vv #include "vvToolRigidReg.h" -#include "vvImageReader.h" #include "vvSlicer.h" // vtk @@ -26,15 +25,16 @@ #include #include +// itk +#include + // clitk #include "clitkTransformUtilities.h" -#include "clitkAffineRegistrationGenericFilter.h" + // qt #include #include #include -#include -#include //------------------------------------------------------------------------------ @@ -51,1301 +51,379 @@ vvToolRigidReg::vvToolRigidReg(vvMainWindowBase * parent, Qt::WindowFlags f): { // GUI Initialization Ui_vvToolRigidReg::setupUi(mToolWidget); - // QSize qsize; -// qsize.setHeight(470); -// qsize.setWidth(850); -// mToolWidget->setFixedSize(qsize); - // Set how many inputs are needed for this tool - cb_transform->hide(); - cb_interpolator->hide(); - cb_optimizer->hide(); - cb_metric->hide(); - cb_selectoutput->hide(); - cb_presets->hide(); - translabel->hide(); - metriclabel->hide(); - outputlabel->hide(); - optimlabel->hide(); - interpollabel->hide(); - presetlabel->hide(); - // Set how many inputs are needed for this tool AddInputSelector("Select moving image"); - AddInputSelector("Select fixed image"); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -vvToolRigidReg::~vvToolRigidReg() -{ -} -//------------------------------------------------------------------------------ -//------------------------------------------------------------------------------ -bool vvToolRigidReg::close() -{ - ResetTransform(); - RemoveOverlay(); - return vvToolWidgetBase::close(); + 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); + } } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::reject() +vvToolRigidReg::~vvToolRigidReg() { - - return vvToolWidgetBase::reject(); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::GetArgsInfoFromGUI() -{ - QFont font=QFont("Courier",10); - tab2textedit->setTextColor(QColor(255,0,0)); - tab2textedit->setCurrentFont(font); - tab2textedit->update(); - - QString file = QFileDialog::getOpenFileName( - this, - "Locate the Config File", - mMainWindow->GetInputPathName(), - "Text (*.conf *.txt *.rtf *.doc)"); - - if (file.isEmpty()) - return; - - QFile Qfile1(file); - mConfigFile= file.toStdString(); - CmdlineParser(1, 1);//1,1 - override, initialize - cb_transform->setCurrentIndex(mArgsInfo.transform_arg); - cb_interpolator->setCurrentIndex(mArgsInfo.interp_arg); - cb_optimizer->setCurrentIndex(mArgsInfo.optimizer_arg); - cb_metric->setCurrentIndex(mArgsInfo.metric_arg); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::Presets() -{ - mConfigFile="Presets"; - std::string matrixfilename; -if(cb_presets->currentIndex()==0) +void vvToolRigidReg::Initialize() { - mArgsInfo.reference_arg=new char; - mArgsInfo.reference_given=0; - mArgsInfo.target_arg=new char; - mArgsInfo.target_given=0; - mArgsInfo.output_arg=new char; - mArgsInfo.referenceMask_arg=new char; - mArgsInfo.targetMask_arg=new char; - mArgsInfo.initMatrix_arg=new char; - mArgsInfo.matrix_arg=new char; - mArgsInfo.referenceMask_given=0; - mArgsInfo.reference_given=0; - mArgsInfo.reference_arg=new char; - mArgsInfo.target_given=0; - mArgsInfo.target_arg=new char; - mArgsInfo.output_given=0; - mArgsInfo.output_arg=new char; - mArgsInfo.checker_after_given=0; - mArgsInfo.checker_before_given=0; - mArgsInfo.after_given=0; - mArgsInfo.before_given=0; - mArgsInfo.threads_given=1; - mArgsInfo.threads_arg=3; - mArgsInfo.normalize_flag=0; - mArgsInfo.blur_arg=0.0; - mArgsInfo.referenceMask_arg=new char; - mArgsInfo.targetMask_arg=new char; - mArgsInfo.targetMask_given=0; - mArgsInfo.levels_given=1; - mArgsInfo.levels_arg=2; - mArgsInfo.moment_flag=1; - mArgsInfo.intThreshold_given=0; - mArgsInfo.intThreshold_arg=0.0; - mArgsInfo.transX_arg=0.0; - mArgsInfo.transY_arg=0.0; - mArgsInfo.transZ_arg=0.0; - mArgsInfo.transform_arg=2; - mArgsInfo.gradient_flag=1; - mArgsInfo.interp_given=1; - mArgsInfo.interp_arg=1; - mArgsInfo.interpOrder_given=1; - mArgsInfo.interpOrder_arg=3; - mArgsInfo.interpSF_given=1; - mArgsInfo.interpSF_arg=20;//default - mArgsInfo.metric_given=1; - mArgsInfo.metric_arg=0; - mArgsInfo.samples_arg=1;//default - mArgsInfo.stdDev_arg=0.4; - mArgsInfo.step_arg=2.0; - mArgsInfo.relax_arg=0.7; - mArgsInfo.valueTol_arg=0.01; - mArgsInfo.stepTol_arg=0.1; - mArgsInfo.gradTol_arg=1e-5; - mArgsInfo.lineAcc_arg=0.9; - mArgsInfo.convFactor_arg=1e+12; - mArgsInfo.maxIt_arg=500; - mArgsInfo.maxLineIt_arg=50; - mArgsInfo.maxEval_arg=500; - mArgsInfo.maxCorr_arg=5; - mArgsInfo.selectBound_arg=0; - mArgsInfo.inc_arg=1.2; - mArgsInfo.dec_arg=4; - mArgsInfo.optimizer_arg=1; - mArgsInfo.initMatrix_given=0; - mArgsInfo.initMatrix_arg=new char; - mArgsInfo.tWeight_given=1; - mArgsInfo.tWeight_arg=1.0; - mArgsInfo.rWeight_given=1.0; - mArgsInfo.rWeight_arg=50.0; - mArgsInfo.matrix_given=1; - matrixfilename="/home/bharath/bin/writematrix.txt";//put ur path here for retreiving your matrix - mArgsInfo.matrix_arg=const_cast(matrixfilename.c_str()); - UpdateTextEditor2(); -} -else { - QMessageBox::information(this,"Sorry", "Other Presets are not available for the moment!"); - return; - } + SetToolName("Register"); + SetToolMenuName("Register manually"); + SetToolIconFilename(":/common/icons/register.png"); + SetToolTip("Register manually."); + SetToolExperimental(false); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateTextEditor2() +void vvToolRigidReg::InputIsSelected(vvSlicerManager *input) { - - QString str1,str2,str3; - QColor color; - tab2textedit->clear(); - tab2textedit->setAcceptRichText(true); - str2=tab2textedit->toPlainText(); - tab2textedit->setTextColor(QColor(255,0,0)); - str2.append(str3.append("threads="+str1.setNum(mArgsInfo.threads_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("reference="+str1.append(mArgsInfo.reference_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("target="+str1.append(mArgsInfo.target_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("matrix="+str1.append(mArgsInfo.matrix_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("interp="+str1.setNum(mArgsInfo.interp_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("transform="+str1.setNum(mArgsInfo.transform_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("transX="+str1.setNum(mArgsInfo.transX_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("transY="+str1.setNum(mArgsInfo.transY_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("transZ="+str1.setNum(mArgsInfo.transZ_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("metric="+str1.setNum(mArgsInfo.metric_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("samples="+str1.setNum(mArgsInfo.samples_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("intThreshold="+str1.setNum(mArgsInfo.intThreshold_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); + mInput = input; + HideInputSelector(); + QTabWidget * tab = dynamic_cast(mMainWindow)->GetTab(); + move(tab->mapToGlobal(tab->pos())); + resize(tab->width(), 0); - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("stdDev="+str1.setNum(mArgsInfo.stdDev_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); + //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]*imageSpacing[0]/2, 'g', 3); + ycord=ycord.setNum(imageorigin[1]+imageSize[1]*imageSpacing[1]/2, 'g', 3); + zcord=zcord.setNum(imageorigin[2]+imageSize[2]*imageSpacing[2]/2, 'g', 3); + Xval->setText(xcord); + Yval->setText(ycord); + Zval->setText(zcord); - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("blur="+str1.setNum(mArgsInfo.blur_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); + //backup original matrix + for(int j=0; j<4; j++) + for(int i=0; i<4; i++) + mInitialMatrix->SetElement(i,j, mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()->GetElement(i,j)); + QString origTransformString = dynamic_cast(mMainWindow)->Get4x4MatrixDoubleAsString(mInitialMatrix); + transformationLabel->setText(origTransformString); + SetTransform(mInitialMatrix); - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("optimizer="+str1.setNum(mArgsInfo.optimizer_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); + //connect all sigs to slots + connect(resetbutton, SIGNAL(pressed()), this, SLOT(ResetTransform())); + connect(loadbutton, SIGNAL(pressed()), this, SLOT(LoadFile())); + connect(savebutton, SIGNAL(pressed()), this, SLOT(SaveFile())); - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("step="+str1.setNum(mArgsInfo.step_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); + 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))); - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("relax="+str1.setNum(mArgsInfo.relax_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); + connect(stepTransSpinBox, SIGNAL(valueChanged(double)), this, SLOT(SetTranslationStep(double))); + connect(stepRotSpinBox, SIGNAL(valueChanged(double)), this, SLOT(SetRotationStep(double))); - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("valueTol="+str1.setNum(mArgsInfo.valueTol_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("stepTol="+str1.setNum(mArgsInfo.stepTol_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); + connect(checkBoxDegrees, SIGNAL(stateChanged(int)), this, SLOT(ToggleSpinBoxAnglesUnit())); - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("gradTol="+str1.setNum(mArgsInfo.gradTol_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); + connect(Xval, SIGNAL(editingFinished()), this, SLOT(ChangeOfRotationCenter())); + connect(Yval, SIGNAL(editingFinished()), this, SLOT(ChangeOfRotationCenter())); + connect(Zval, SIGNAL(editingFinished()), this, SLOT(ChangeOfRotationCenter())); - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("lineAcc="+str1.setNum(mArgsInfo.lineAcc_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - str2=tab2textedit->toPlainText(); - str2.append(str3.append("convFactor="+str1.setNum(mArgsInfo.convFactor_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("maxIt="+str1.setNum(mArgsInfo.maxIt_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("maxLineIt="+str1.setNum(mArgsInfo.maxLineIt_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("maxEval="+str1.setNum(mArgsInfo.maxEval_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("maxCorr="+str1.setNum(mArgsInfo.maxCorr_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("selectBound="+str1.setNum(mArgsInfo.selectBound_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("rWeight="+str1.setNum(mArgsInfo.rWeight_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("tWeight="+str1.setNum(mArgsInfo.tWeight_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("levels="+str1.setNum(mArgsInfo.levels_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("inc="+str1.setNum(mArgsInfo.inc_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); - - str2=tab2textedit->toPlainText(); - str2.append(str3.append("dec="+str1.setNum(mArgsInfo.dec_arg))); - tab2textedit->setText(str2); - str3.clear(); - str1.clear(); - - str2=tab2textedit->toPlainText(); - str2.append("\n"); - tab2textedit->setText(str2); + // Init step modifiers + stepTransSpinBox->setValue(1.); + stepRotSpinBox->setValue(1.); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::TransformSelect() -{ - if(!mConfigFile.empty()){ - mArgsInfo.transform_arg=cb_transform->currentIndex(); - UpdateTextEditor2(); - } - else{ - QMessageBox::information(this,"Warning","Load the Config File First!.."); - } -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::CmdlineParser(int override, int initialize) +void vvToolRigidReg::apply() { - //0 opened, 1 not opened fine - int opened=cmdline_parser_clitkAffineRegistration_configfile(const_cast(mConfigFile.c_str()),&mArgsInfo,override,initialize,1); - DD(opened); - mArgsInfo.gradient_flag=1; - QString str; - //Read the transformation parameters from the path in the config file(mArgsInfo.matrix_arg) and display it on the TextBox 2 - ifstream readfile; - std::vector Qstr; - - if(!opened){ - readfile.open(mConfigFile.c_str()); - } - else{ - QMessageBox::information(this,"Warning","Load the Config File First.."); - } - if (readfile.is_open()) - { - while (!readfile.eof()) - { - readfile >> mConfigFile; - Qstr.push_back(QString(mConfigFile.c_str())); - } - readfile.close(); - } - else { - QMessageBox::information(this,"Warning","Cannot Open File!"); - return; - } - for(unsigned int i=0;itoPlainText(); - str.append(Qstr.at(i)); - tab2textedit->setText(str); - str.append("\n"); - tab2textedit->setText(str); - } + vvToolWidgetBase::close(); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::OptimizerSelect() +bool vvToolRigidReg::close() { - if(!mConfigFile.empty()){ - mArgsInfo.optimizer_arg=cb_optimizer->currentIndex(); - UpdateTextEditor2(); - } - else{ - QMessageBox::information(this,"Warning","Load the Config File First!.."); + 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) { + SetTransform(mInitialMatrix); + return vvToolWidgetBase::close(); } + return false; } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::InterpolatorSelect() +void vvToolRigidReg::reject() { - if(!mConfigFile.empty()){ - mArgsInfo.interp_arg=cb_interpolator->currentIndex(); - UpdateTextEditor2(); - } - else{ - QMessageBox::information(this,"Warning","Load the Config File First!.."); - } + return vvToolWidgetBase::reject(); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::MetricSelect() +void vvToolRigidReg::SetTranslationStep(double v) { - if(!mConfigFile.empty()){ - mArgsInfo.metric_arg=cb_metric->currentIndex(); - UpdateTextEditor2(); - } - else{ - QMessageBox::information(this,"Warning","Load the Config File First!.."); - } + xtrans_sb->setSingleStep(v); + ytrans_sb->setSingleStep(v); + ztrans_sb->setSingleStep(v); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::OutputSelect() +void vvToolRigidReg::SetRotationStep(double v) { - std::ostringstream osstream; - if(!mConfigFile.empty()){ - if(cb_selectoutput->currentIndex()==0){ - mArgsInfo.output_given=0; - mArgsInfo.checker_after_given=0; - mArgsInfo.checker_before_given=0; - mArgsInfo.after_given=0; - mArgsInfo.before_given=0; - }//get transformed output image - if(cb_selectoutput->currentIndex()==1){ - mArgsInfo.output_given=0; - mArgsInfo.checker_after_given=1; - mArgsInfo.checker_before_given=0; - mArgsInfo.after_given=0; - mArgsInfo.before_given=0; - }//get checkered image after reg - if(cb_selectoutput->currentIndex()==2){ - mArgsInfo.output_given=0; - mArgsInfo.checker_after_given=0; - mArgsInfo.checker_before_given=1; - mArgsInfo.after_given=0; - mArgsInfo.before_given=0; - }//get checkered image before reg - if(cb_selectoutput->currentIndex()==3){ - mArgsInfo.output_given=0; - mArgsInfo.checker_after_given=0; - mArgsInfo.checker_before_given=0; - mArgsInfo.after_given=1; - mArgsInfo.before_given=0; - }//get difference image after reg - if(cb_selectoutput->currentIndex()==4){ - mArgsInfo.output_given=0; - mArgsInfo.checker_after_given=0; - mArgsInfo.checker_before_given=0; - mArgsInfo.after_given=0; - mArgsInfo.before_given=1; - }//get difference image before reg - } - else{ - QMessageBox::information(this,"Warning","Load the Config File First!.."); - } + xrot_sb->setSingleStep(v); + yrot_sb->setSingleStep(v); + zrot_sb->setSingleStep(v); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::SaveTextEdit() +void vvToolRigidReg::SliderChange(int newVal) { - DD(mArgsInfo.transform_arg); - QString f1 = QFileDialog::getSaveFileName(this, tr("Save Config File"), - mMainWindow->GetInputPathName(), - tr("Text (*.mat *.txt *.doc *.rtf)")); - QFile file(f1); - if(file.open(QFile::WriteOnly | QFile::Truncate) & !mConfigFile.empty() ){ - QTextStream stream( &file ); - stream << tab2textedit->toPlainText(); - } - else - { - QMessageBox::information(this,"Warning","Nothing to Save!"); - return; - } - tab2textedit->clear(); - DD(mArgsInfo.transform_arg); + 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::InputIsSelected(std::vector & l) +void vvToolRigidReg::SpinBoxChange(double newVal) { - //inputs - mInput1 = l[0]; - mInput2 = l[1]; - UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix(),textEdit_2); - - for(int i =0;i<4;i++) - { - for(int j=0;j<4;j++) - { - mInitialMatrix[i*4+j]=mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()->GetElement(i,j); + 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); + } + 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); } } - if(mInput1->GetFileName()==mInput2->GetFileName()) - { - QMessageBox::information(this, "Warning","Your Reference and Target Images are the same"); + // Compute transform and set + vtkSmartPointer transform_final=mInput->GetImage()->GetTransform(); + 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::ToggleSpinBoxAnglesUnit() +{ + 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); } - mTwoInputs = true; - SetOverlay(mInput2->GetImage()); - mImageSize=mInput1->GetImage()->GetSize(); - SetRotationCenter(); - SetSliderRanges(); - - //connect all sigs to slots - connect(resetbutton, SIGNAL(pressed()), this, SLOT(ResetTransform())); - connect(tab2loadbutton, SIGNAL(pressed()), this, SLOT(GetArgsInfoFromGUI())); - connect(tab2applybutton, SIGNAL(pressed()), this, SLOT(AutoRegister())); - - - 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(UpdateTransform_sliders())); - connect(ytrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders())); - connect(ztrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders())); - - - connect(xrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders())); - connect(yrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders())); - connect(zrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders())); - - connect(xtrans_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb())); - connect(ytrans_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb())); - connect(ztrans_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb())); - connect(xrot_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb())); - connect(yrot_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb())); - connect(zrot_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb())); - - connect(loadbutton, SIGNAL(pressed()), this, SLOT(LoadFile())); - connect(savebutton, SIGNAL(pressed()), this, SLOT(SaveFile())); - - connect(checkBox_rigid, SIGNAL(clicked(bool)), this, SLOT(CheckRigidReg())); - connect(checkBox_deformable, SIGNAL(clicked(bool)), this, SLOT(CheckDeformableReg())); - - connect(cb_presets, SIGNAL(activated(int)), this, SLOT(Presets())); - connect(cb_transform, SIGNAL(activated(int)), this, SLOT(TransformSelect())); - connect(cb_optimizer, SIGNAL(activated(int)), this, SLOT(OptimizerSelect())); - connect(cb_interpolator, SIGNAL(activated(int)), this, SLOT(InterpolatorSelect())); - connect(cb_metric, SIGNAL(activated(int)), this, SLOT(MetricSelect())); - connect(cb_selectoutput, SIGNAL(activated(int)), this, SLOT(OutputSelect())); - connect(tab2savebutton, SIGNAL(pressed()), this, SLOT(SaveTextEdit())); - } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::apply() -{ - RemoveOverlay(); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::CheckRigidReg() -{ - checkBox_deformable->setChecked(false); - cb_transform->show(); - cb_presets->show(); - cb_metric->show(); - cb_interpolator->show(); - cb_optimizer->show(); - cb_selectoutput->show(); - translabel->show(); - metriclabel->show(); - outputlabel->show(); - optimlabel->show(); - interpollabel->show(); - presetlabel->show(); - -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::CheckDeformableReg() -{ - checkBox_rigid->setChecked(false); - presetlabel->hide(); - cb_transform->show(); - cb_metric->show(); - cb_interpolator->show(); - cb_optimizer->show(); - cb_selectoutput->show(); - cb_presets->hide(); - translabel->show(); - metriclabel->show(); - outputlabel->show(); - optimlabel->show(); - interpollabel->show(); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::SetOverlay(vvImage::Pointer Image) +void vvToolRigidReg::SaveFile() { - for (int i =0; iGetNumberOfSlicers(); i++) { - mCurrentSlicerManager->GetSlicer(i)->SetOverlay(Image); - mCurrentSlicerManager->GetSlicer(i)->SetActorVisibility("overlay",0,true); - mCurrentSlicerManager->SetColorMap(); - mCurrentSlicerManager->Render(); - } + //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)) { + vtkMatrix4x4* matrix = mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix(); + QString matrixStr = dynamic_cast(mMainWindow)->Get4x4MatrixDoubleAsString(matrix,16); + QTextStream out(&file); + out << matrixStr; } -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::RemoveOverlay() -{ - for(int i=0;iGetNumberOfSlicers();i++) + else { - mInput1->RemoveActor("overlay",0); - mInput1->SetColorMap(0); - mInput1->Render(); - hide(); + QMessageBox::information(this,"Error","Unable to open file for writing"); } } //------------------------------------------------------------------------------ -//------------------------------------------------------------------------------ -void vvToolRigidReg::SetXvalue() -{ - QString xstr = Xval->text(); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::SetYvalue() -{ - QString ystr = Yval->text(); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::SetZvalue() -{ - QString zstr = Zval->text(); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::SetTransform(double tX, double tY, double tZ, double aX, double aY, double aZ,bool update) -{ - vtkSmartPointer transform = mInput1->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]); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::SetTransform(vtkMatrix4x4 *matrix) -{ - vtkSmartPointer transform=vtkSmartPointer::New(); - for(int i=0; i<4;i++) - for(int j=0;j<4;j++) - mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()->SetElement(i,j,matrix->GetElement(i,j)); - - Render(); -} -//------------------------------------------------------------------------------ - //------------------------------------------------------------------------------ void vvToolRigidReg::LoadFile() { - ReadFile(false); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::Render() -{ - for (int i=0; iGetNumberOfSlicers(); i++) { - mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent(); - mCurrentSlicerManager->GetSlicer(i)->Render(); - } -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ - void vvToolRigidReg::UpdateTextEditor(vtkMatrix4x4 *matrix,QTextEdit* textEdit) -{ - QFont font=QFont("Courier",11); - textEdit->setCurrentFont(font); - textEdit->update(); - - 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); + //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; - 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]),2)); - textEdit->setText(str2); - } - str2=textEdit->toPlainText(); - str2.append("\n"); - textEdit->setText(str2); - } - //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); + 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::UpdateTransform_sliders() -{ - InitializeSliders(xtrans_slider->value()*mInput1->GetImage()->GetSpacing()[0], - ytrans_slider->value()*mInput1->GetImage()->GetSpacing()[1], - ztrans_slider->value()*mInput1->GetImage()->GetSpacing()[2], - xrot_slider->value(),yrot_slider->value(),zrot_slider->value(),false); - UpdateTransform(true); - Render(); -} -//------------------------------------------------------------------------------ - + //------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateTransform_sb() +void vvToolRigidReg::ChangeOfRotationCenter() { - InitializeSliders(xtrans_sb->value(), - ytrans_sb->value(), - ztrans_sb->value(), - xrot_sb->value(),yrot_sb->value(),zrot_sb->value(),false); - UpdateTransform(false); - Render(); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::AutoRegister() -{ - if (!mCurrentSlicerManager) close(); - - if(!mConfigFile.empty()){ - std::vector inputs; - // Input - inputs.push_back(mInput1->GetImage()); - inputs.push_back(mInput2->GetImage()); - // Check input type - // Main filter - clitk::AffineRegistrationGenericFilter::Pointer filter = - clitk::AffineRegistrationGenericFilter::New(); - filter->SetInputVVImages(inputs); - filter->SetArgsInfo(mArgsInfo); - filter->EnableReadOnDisk(false); - filter->Update(); - std::ostringstream osstream; - vvImage::Pointer output; - output = filter->GetOutputVVImage(); - SetOverlay(output); - - if(!cb_selectoutput->currentIndex()==0){ - std::string outputstring; - output = filter->GetOutputVVImages()[1]; - if(cb_selectoutput->currentIndex()==1){outputstring="Checkered_after_reg";} - if(cb_selectoutput->currentIndex()==2){outputstring="Checkered_before_reg";} - if(cb_selectoutput->currentIndex()==3){outputstring="Diff_after_reg";} - if(cb_selectoutput->currentIndex()==4){outputstring="Diff_before_reg";} - osstream << outputstring << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd"; - AddImage(output,osstream.str()); - filter->DeleteLastOutputImage(); - } - DD(filter->GetOutputVVImages().capacity()); - QApplication::restoreOverrideCursor(); - ReadFile(true); - } - else - { - QMessageBox::information(this, "Warning","Load the Config File First!..."); - return; - } + SetTransform(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::UpdateTransform(bool slider_enabled) +void vvToolRigidReg::ResetTransform() { - vtkSmartPointer transform_final=mInput1->GetImage()->GetTransform(); - transform_final->SetMatrix(mInitialMatrix); - transform_final->PostMultiply(); - //Rotations - double x=0, y=0 ,z=0; - x= Xval->text().toDouble(); - y= Yval->text().toDouble(); - z= Zval->text().toDouble(); - transform_final->Translate(-x,-y,-z); - if(slider_enabled){ - transform_final->RotateY(yrot_slider->value()); - transform_final->RotateX(xrot_slider->value()); - transform_final->RotateZ(zrot_slider->value()); - } - else{ - transform_final->RotateY(yrot_sb->value()); - transform_final->RotateX(xrot_sb->value()); - transform_final->RotateZ(zrot_sb->value()); - } - transform_final->Translate(x,y,z); - transform_final->PreMultiply(); - if(slider_enabled){ - transform_final->Translate(xtrans_slider->value()*mInput1->GetImage()->GetSpacing()[0],0,0); - transform_final->Translate(0,ytrans_slider->value()*mInput1->GetImage()->GetSpacing()[1],0); - transform_final->Translate(0,0,ztrans_slider->value()*mInput1->GetImage()->GetSpacing()[2]); - } - else{ - transform_final->Translate(xtrans_sb->value(),0,0); - transform_final->Translate(0,ytrans_sb->value(),0); - transform_final->Translate(0,0,ztrans_sb->value()); - } - transform_final->Update(); - Render(); - UpdateTextEditor(transform_final->GetMatrix(),textEdit); + SetTransform(mInitialMatrix); } //------------------------------------------------------------------------------ - //------------------------------------------------------------------------------ -void vvToolRigidReg::SaveFile() +void vvToolRigidReg::SetTransform(vtkMatrix4x4 *matrix) { - //Write the Transformation Matrix - QString f1 = QFileDialog::getSaveFileName(this, tr("Save Transformation Matrix File"), - mMainWindow->GetInputPathName(), - tr("Text (*.mat *.txt *.doc *.rtf)")); - QFile file1(f1); - std::vector transparameters; - QString line1; - - for(int i =0;i<4;i++) - for(int j=0;j<4;j++) - transparameters.push_back(line1.setNum(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()->Element[i][j])); - - if (file1.open(QFile::WriteOnly | QFile::Truncate)) { - QTextStream out1(&file1); - for(int i =0;i<4;i++){ - for(int j=0;j<4;j++) { - out1< transform=vtkSmartPointer::New(); + mCurrentSlicerManager->GetImage()->GetTransform()->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); + euler->SetMatrix(rotMat); + 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++) { + 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); + double rad = (checkBoxDegrees->checkState()==Qt::Checked)?180./itk::Math::pi:1.; + rotSBs[i]->blockSignals(true); + rotSBs[i]->setValue( euler->GetParameters()[i]*rad ); + rotSBs[i]->blockSignals(false); + rotSliders[i]->blockSignals(true); + rotSliders[i]->setValue( itk::Math::Round(euler->GetParameters()[i]*180./itk::Math::pi) ); + rotSliders[i]->blockSignals(false); + } } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::ReadFile(bool matrix_given) +// 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) { - std::string x; - QString center; - double * orientations=new double[3]; - double * translations=new double[3]; - vtkMatrix4x4 *matrix=vtkMatrix4x4::New(); - std::string transfile; - vtkSmartPointer transform = mCurrentSlicerManager->GetImage()->GetTransform(); - if(!matrix_given) - { - //Open File to read the transformation parameters - QString file1 = QFileDialog::getOpenFileName( - this, - "Choose the Transformation Parameters file", - mMainWindow->GetInputPathName(), - "Text (*.mat *.txt *.rtf *.doc)"); - if (file1.isEmpty()) - return; - QFile Qfile1(file1); - // ifstream readfile; - transfile= file1.toStdString(); - } - else - { - transfile=mArgsInfo.matrix_arg; - DD(transfile); - } - std::string filename1(transfile); - std::ifstream f1(filename1.c_str()); - if(f1.is_open()) - { - f1.close(); - itk::Matrix itkMat = clitk::ReadMatrix3D(transfile); - for(int j=0; j<4; j++) - for(int i=0; i<4; i++) - matrix->SetElement(i,j,rint(itkMat[i][j])); - } - UpdateTextEditor(matrix,textEdit); - transform->SetMatrix(matrix); - transform->GetOrientation(orientations); - transform->PostMultiply(); - - //Obtain the Rotation Center , set it to origin - Xval->setText(center.setNum(0)); - Yval->setText(center.setNum(0)); - Zval->setText(center.setNum(0)); + transSliders.push_back(xtrans_slider); + transSliders.push_back(ytrans_slider); + transSliders.push_back(ztrans_slider); - //In the Order or Y X Z // - //now postmultiply for the rotations - SetTransform(0,0,0,0,0,-rint(orientations[2]),false); - SetTransform(0,0,0,-rint(orientations[0]),0,0,false); - SetTransform(0,0,0,0,-rint(orientations[1]),0,false); + rotSliders.push_back(xrot_slider); + rotSliders.push_back(yrot_slider); + rotSliders.push_back(zrot_slider); - transform->GetPosition(translations); - transform->Identity(); + transSBs.push_back(xtrans_sb); + transSBs.push_back(ytrans_sb); + transSBs.push_back(ztrans_sb); - DD(translations[0]/mInput1->GetImage()->GetSpacing()[0]); - DD(translations[1]/mInput1->GetImage()->GetSpacing()[1]); - DD(translations[2]/mInput1->GetImage()->GetSpacing()[2]); - DD(mInput1->GetImage()->GetSpacing()[0]); - DD(mInput1->GetImage()->GetSpacing()[1]); - DD(mInput1->GetImage()->GetSpacing()[2]); - DD(orientations[0]); - DD(orientations[1]); - DD(orientations[2]); - //set the sliders and spin box values - InitializeSliders(rint(translations[0]),rint(translations[1]) - ,rint(translations[2]),rint(orientations[0]),rint(orientations[1]),rint(orientations[2]),true); - SetTransform(matrix); + rotSBs.push_back(xrot_sb); + rotSBs.push_back(yrot_sb); + rotSBs.push_back(zrot_sb); } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void vvToolRigidReg::ResetTransform() -{ - vtkSmartPointer transform = mCurrentSlicerManager->GetImage()->GetTransform(); - transform->SetMatrix(mInitialMatrix); - transform->Update(); - - Render(); - SetRotationCenter(); - SetSliderRanges(); - UpdateTextEditor(transform->GetMatrix(),textEdit); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::SetRotationCenter() -{ - //default image rotation center is the center of the image - QString xcord,ycord,zcord; - std::vector imageorigin; - imageorigin=mInput1->GetImage()->GetOrigin(); - DD("before bug"); - 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(0,0,0,0,0,0,true); -} -//------------------------------------------------------------------------------ - -//------------------------------------------------------------------------------ -void vvToolRigidReg::InitializeSliders(double xtrans,double ytrans, double ztrans, double xrot, double yrot, double zrot,bool sliders) +void vvToolRigidReg::Render() { - - xtrans_sb->blockSignals(true); - xtrans_sb->setSingleStep(mInput1->GetImage()->GetSpacing()[0]); - xtrans_sb->setValue(xtrans); - xtrans_sb->blockSignals(false); - ytrans_sb->blockSignals(true); - ytrans_sb->setSingleStep(mInput1->GetImage()->GetSpacing()[1]); - ytrans_sb->setValue(ytrans); - ytrans_sb->blockSignals(false); - ztrans_sb->blockSignals(true); - ztrans_sb->setSingleStep(mInput1->GetImage()->GetSpacing()[2]); - ztrans_sb->setValue(ztrans); - ztrans_sb->blockSignals(false); - - if(sliders){ - xtrans_slider->blockSignals(true); - xtrans_slider->setValue(rint(xtrans/mInput1->GetImage()->GetSpacing()[0])); - xtrans_slider->blockSignals(false); - ytrans_slider->blockSignals(true); - ytrans_slider->setValue(rint(ytrans/mInput1->GetImage()->GetSpacing()[1])); - ytrans_slider->blockSignals(false); - ztrans_slider->blockSignals(true); - ztrans_slider->setValue(rint(ztrans/mInput1->GetImage()->GetSpacing()[2])); - ztrans_slider->blockSignals(false); + for (int i=0; iGetNumberOfSlicers(); i++) + { + mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent(); + mCurrentSlicerManager->GetSlicer(i)->Render(); } - xrot_sb->blockSignals(true); - xrot_sb->setValue(xrot); - xrot_sb->blockSignals(false); - yrot_sb->blockSignals(true); - yrot_sb->setValue(yrot); - yrot_sb->blockSignals(false); - zrot_sb->blockSignals(true); - zrot_sb->setValue(zrot); - zrot_sb->blockSignals(false); - xrot_slider->blockSignals(true); - xrot_slider->setValue(xrot); - xrot_slider->blockSignals(false); - yrot_slider->blockSignals(true); - yrot_slider->setValue(yrot); - yrot_slider->blockSignals(false); - zrot_slider->blockSignals(true); - zrot_slider->setValue(zrot); - zrot_slider->blockSignals(false); } //------------------------------------------------------------------------------ -//------------------------------------------------------------------------------ -void vvToolRigidReg::SetSliderRanges() -{ - xtrans_slider->blockSignals(true); - xtrans_slider->setRange(-2000,2000); - xtrans_slider->blockSignals(false); - - ytrans_slider->blockSignals(true); - ytrans_slider->setRange(-2000,2000); - ytrans_slider->blockSignals(false); - - ztrans_slider->blockSignals(true); - ztrans_slider->setRange(-2000,2000); - ztrans_slider->blockSignals(false); - - xtrans_sb->blockSignals(true); - xtrans_sb->setRange(-2000,2000); - xtrans_sb->setDecimals(3); - xtrans_sb->blockSignals(false); - - ytrans_sb->blockSignals(true); - ytrans_sb->setRange(-2000,2000); - ytrans_sb->setDecimals(3); - ytrans_sb->blockSignals(false); - - ztrans_sb->blockSignals(true); - ztrans_sb->setRange(-2000,2000); - ztrans_sb->setDecimals(3); - ztrans_sb->blockSignals(false); - - xrot_slider->blockSignals(true); - xrot_slider->setRange(-360,360); - xrot_slider->blockSignals(false); - - yrot_slider->blockSignals(true); - yrot_slider->setRange(-360,360); - yrot_slider->blockSignals(false); - - zrot_slider->blockSignals(true); - zrot_slider->setRange(-360,360); - zrot_slider->blockSignals(false); - - xrot_sb->blockSignals(true); - xrot_sb->setRange(-360,360); - xrot_sb->blockSignals(false); - - yrot_sb->blockSignals(true); - yrot_sb->setRange(-360,360); - yrot_sb->blockSignals(false); - - zrot_sb->blockSignals(true); - zrot_sb->setRange(-360,360); - zrot_sb->blockSignals(false); -} diff --git a/vv/vvToolRigidReg.h b/vv/vvToolRigidReg.h index f543ff5..559aca5 100644 --- a/vv/vvToolRigidReg.h +++ b/vv/vvToolRigidReg.h @@ -1,4 +1,3 @@ - /*========================================================================= Program: vv http://www.creatis.insa-lyon.fr/rio/vv @@ -18,16 +17,12 @@ ===========================================================================**/ #ifndef VVTOOLRIGIDREG_H #define VVTOOLRIGIDREG_H -#include -#include #include "vvToolBase.h" #include "vvToolWidgetBase.h" -#include "vvMainWindowBase.h" #include "vvMainWindow.h" #include "ui_vvToolRigidReg.h" #include "vtkMatrix4x4.h" -#include "clitkAffineRegistration_ggo.h" //------------------------------------------------------------------------------ class vvToolRigidReg: @@ -39,61 +34,31 @@ class vvToolRigidReg: public: vvToolRigidReg(vvMainWindowBase * parent=0, Qt::WindowFlags f=0); ~vvToolRigidReg(); - virtual void InputIsSelected(std::vector & m); + static void Initialize(); + virtual void InputIsSelected(vvSlicerManager *input); + public slots: virtual void apply(); virtual bool close(); virtual void reject(); - //----------------------------------------------------- - static void Initialize() { - SetToolName("Register"); - SetToolMenuName("Register"); - SetToolIconFilename(":/common/icons/register.png"); - SetToolTip("Register Image."); - SetToolExperimental(true); - } - virtual void GetArgsInfoFromGUI(); - void SetOverlay(vvImage::Pointer Image); - void RemoveOverlay(); - void SetXvalue(); - void SetYvalue(); - void SetZvalue(); - void Render(); - void UpdateTextEditor(vtkMatrix4x4 *matrix,QTextEdit *textEdit); - void UpdateTransform_sliders(); - void UpdateTransform_sb(); - void UpdateTransform(bool slider_enabled); - void AutoRegister(); + void SetTranslationStep(double v); + void SetRotationStep(double v); + void SliderChange(int newVal); + void SpinBoxChange(double newVal); + void ToggleSpinBoxAnglesUnit(); void SaveFile(); - void ReadFile(bool matrix_given); void LoadFile(); + void ChangeOfRotationCenter(); void ResetTransform(); - void SetRotationCenter(); - void SetSliderRanges(); - void InitializeSliders(double xtrans,double ytrans, double ztrans, double xrot, double yrot, double zrot,bool sliders); - void TransformSelect(); - void OptimizerSelect(); - void InterpolatorSelect(); - void MetricSelect(); - void OutputSelect(); - void SaveTextEdit(); - void CmdlineParser(int override, int initialize); - void Presets(); - void UpdateTextEditor2(); - void CheckRigidReg(); //Deformable or Rigid - void CheckDeformableReg(); - protected: + +protected: Ui::vvToolRigidReg ui; - vvSlicerManager * mInput1; - vvSlicerManager * mInput2; - vvMainWindow * mWindow; - std::string mConfigFile; - bool mTwoInputs; - std::vector mImageSize; - args_info_clitkAffineRegistration mArgsInfo; - double mInitialMatrix[16]; - void SetTransform(double tX, double tY, double tZ, double aX, double aY, double aZ,bool update); + vvSlicerManager * mInput; + vtkSmartPointer mInitialMatrix; void SetTransform(vtkMatrix4x4 * matrix); + void GetSlidersAndSpinBoxes(std::vector&transSliders, std::vector&rotSliders, + std::vector&transSBs, std::vector&rotSBs); + void Render(); }; // end class vvToolRigidReg //------------------------------------------------------------------------------ -- 2.47.1