1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
20 #include "vvToolRigidReg.h"
21 #include "vvImageReader.h"
25 #include <vtkImageData.h>
26 #include <vtkSmartPointer.h>
27 #include <vtkTransform.h>
30 #include "clitkTransformUtilities.h"
31 #include "clitkAffineRegistrationGenericFilter.h"
33 #include <QMessageBox>
34 #include <QFileDialog>
35 #include <QTextStream>
40 //------------------------------------------------------------------------------
41 // Create the tool and automagically (I like this word) insert it in
42 // the main window menu.
43 ADD_TOOL(vvToolRigidReg);
44 //------------------------------------------------------------------------------
46 //------------------------------------------------------------------------------
47 vvToolRigidReg::vvToolRigidReg(vvMainWindowBase * parent, Qt::WindowFlags f):
48 vvToolWidgetBase(parent, f),
49 vvToolBase<vvToolRigidReg>(parent),
53 Ui_vvToolRigidReg::setupUi(mToolWidget);
55 // qsize.setHeight(470);
56 // qsize.setWidth(850);
57 // mToolWidget->setFixedSize(qsize);
58 // Set how many inputs are needed for this tool
59 mFilter = new clitk::AffineRegistrationGenericFilter;
61 cb_interpolator->hide();
64 cb_selectoutput->hide();
70 interpollabel->hide();
74 // Set how many inputs are needed for this tool
75 AddInputSelector("Select moving image",mFilter);
76 AddInputSelector("Select fixed image",mFilter);
78 //------------------------------------------------------------------------------
80 //------------------------------------------------------------------------------
81 vvToolRigidReg::~vvToolRigidReg()
84 //------------------------------------------------------------------------------
86 //------------------------------------------------------------------------------
87 bool vvToolRigidReg::close()
91 return vvToolWidgetBase::close();
93 //------------------------------------------------------------------------------
95 //------------------------------------------------------------------------------
96 void vvToolRigidReg::reject()
99 return vvToolWidgetBase::reject();
101 //------------------------------------------------------------------------------
103 //------------------------------------------------------------------------------
104 void vvToolRigidReg::GetArgsInfoFromGUI()
106 QFont font=QFont("Courier",10);
107 tab2textedit->setTextColor(QColor(255,0,0));
108 tab2textedit->setCurrentFont(font);
109 tab2textedit->update();
111 QString file = QFileDialog::getOpenFileName(
113 "Locate the Config File",
114 mMainWindow->GetInputPathName(),
115 "Text (*.conf *.txt *.rtf *.doc)");
121 mConfigFile= file.toStdString();
122 CmdlineParser(1, 1);//1,1 - override, initialize
123 cb_transform->setCurrentIndex(mArgsInfo.transform_arg);
124 cb_interpolator->setCurrentIndex(mArgsInfo.interp_arg);
125 cb_optimizer->setCurrentIndex(mArgsInfo.optimizer_arg);
126 cb_metric->setCurrentIndex(mArgsInfo.metric_arg);
128 //------------------------------------------------------------------------------
130 //------------------------------------------------------------------------------
131 void vvToolRigidReg::Presets()
133 mConfigFile="Presets";
134 std::string matrixfilename;
135 if(cb_presets->currentIndex()==0)
137 mArgsInfo.reference_arg=new char;
138 mArgsInfo.reference_given=0;
139 mArgsInfo.target_arg=new char;
140 mArgsInfo.target_given=0;
141 mArgsInfo.output_arg=new char;
142 mArgsInfo.referenceMask_arg=new char;
143 mArgsInfo.targetMask_arg=new char;
144 mArgsInfo.initMatrix_arg=new char;
145 mArgsInfo.matrix_arg=new char;
146 mArgsInfo.referenceMask_given=0;
147 mArgsInfo.reference_given=0;
148 mArgsInfo.reference_arg=new char;
149 mArgsInfo.target_given=0;
150 mArgsInfo.target_arg=new char;
151 mArgsInfo.output_given=0;
152 mArgsInfo.output_arg=new char;
153 mArgsInfo.checker_after_given=0;
154 mArgsInfo.checker_before_given=0;
155 mArgsInfo.after_given=0;
156 mArgsInfo.before_given=0;
157 mArgsInfo.threads_given=1;
158 mArgsInfo.threads_arg=3;
159 mArgsInfo.normalize_flag=0;
160 mArgsInfo.blur_arg=0.0;
161 mArgsInfo.referenceMask_arg=new char;
162 mArgsInfo.targetMask_arg=new char;
163 mArgsInfo.targetMask_given=0;
164 mArgsInfo.levels_given=1;
165 mArgsInfo.levels_arg=2;
166 mArgsInfo.moment_flag=1;
167 mArgsInfo.intThreshold_given=0;
168 mArgsInfo.intThreshold_arg=0.0;
169 mArgsInfo.transX_arg=0.0;
170 mArgsInfo.transY_arg=0.0;
171 mArgsInfo.transZ_arg=0.0;
172 mArgsInfo.transform_arg=2;
173 mArgsInfo.gradient_flag=1;
174 mArgsInfo.interp_given=1;
175 mArgsInfo.interp_arg=1;
176 mArgsInfo.interpOrder_given=1;
177 mArgsInfo.interpOrder_arg=3;
178 mArgsInfo.interpSF_given=1;
179 mArgsInfo.interpSF_arg=20;//default
180 mArgsInfo.metric_given=1;
181 mArgsInfo.metric_arg=0;
182 mArgsInfo.samples_arg=1;//default
183 mArgsInfo.stdDev_arg=0.4;
184 mArgsInfo.step_arg=2.0;
185 mArgsInfo.relax_arg=0.7;
186 mArgsInfo.valueTol_arg=0.01;
187 mArgsInfo.stepTol_arg=0.1;
188 mArgsInfo.gradTol_arg=1e-5;
189 mArgsInfo.lineAcc_arg=0.9;
190 mArgsInfo.convFactor_arg=1e+12;
191 mArgsInfo.maxIt_arg=500;
192 mArgsInfo.maxLineIt_arg=50;
193 mArgsInfo.maxEval_arg=500;
194 mArgsInfo.maxCorr_arg=5;
195 mArgsInfo.selectBound_arg=0;
196 mArgsInfo.inc_arg=1.2;
198 mArgsInfo.optimizer_arg=1;
199 mArgsInfo.initMatrix_given=0;
200 mArgsInfo.initMatrix_arg=new char;
201 mArgsInfo.tWeight_given=1;
202 mArgsInfo.tWeight_arg=1.0;
203 mArgsInfo.rWeight_given=1.0;
204 mArgsInfo.rWeight_arg=50.0;
205 mArgsInfo.matrix_given=1;
206 matrixfilename="/home/bharath/bin/writematrix.txt";//put ur path here for retreiving your matrix
207 mArgsInfo.matrix_arg=const_cast<char*>(matrixfilename.c_str());
211 QMessageBox::information(this,"Sorry", "Other Presets are not available for the moment!");
215 //------------------------------------------------------------------------------
217 //------------------------------------------------------------------------------
218 void vvToolRigidReg::UpdateTextEditor2()
221 QString str1,str2,str3;
223 tab2textedit->clear();
224 tab2textedit->setAcceptRichText(true);
225 str2=tab2textedit->toPlainText();
226 tab2textedit->setTextColor(QColor(255,0,0));
227 str2.append(str3.append("threads="+str1.setNum(mArgsInfo.threads_arg)));
228 tab2textedit->setText(str2);
232 str2=tab2textedit->toPlainText();
234 tab2textedit->setText(str2);
236 str2=tab2textedit->toPlainText();
237 str2.append(str3.append("reference="+str1.append(mArgsInfo.reference_arg)));
238 tab2textedit->setText(str2);
242 str2=tab2textedit->toPlainText();
244 tab2textedit->setText(str2);
246 str2=tab2textedit->toPlainText();
247 str2.append(str3.append("target="+str1.append(mArgsInfo.target_arg)));
248 tab2textedit->setText(str2);
252 str2=tab2textedit->toPlainText();
254 tab2textedit->setText(str2);
256 str2=tab2textedit->toPlainText();
257 str2.append(str3.append("matrix="+str1.append(mArgsInfo.matrix_arg)));
258 tab2textedit->setText(str2);
262 str2=tab2textedit->toPlainText();
264 tab2textedit->setText(str2);
266 str2=tab2textedit->toPlainText();
267 str2.append(str3.append("interp="+str1.setNum(mArgsInfo.interp_arg)));
268 tab2textedit->setText(str2);
272 str2=tab2textedit->toPlainText();
274 tab2textedit->setText(str2);
276 str2=tab2textedit->toPlainText();
277 str2.append(str3.append("transform="+str1.setNum(mArgsInfo.transform_arg)));
278 tab2textedit->setText(str2);
282 str2=tab2textedit->toPlainText();
284 tab2textedit->setText(str2);
286 str2=tab2textedit->toPlainText();
287 str2.append(str3.append("transX="+str1.setNum(mArgsInfo.transX_arg)));
288 tab2textedit->setText(str2);
292 str2=tab2textedit->toPlainText();
294 tab2textedit->setText(str2);
296 str2=tab2textedit->toPlainText();
297 str2.append(str3.append("transY="+str1.setNum(mArgsInfo.transY_arg)));
298 tab2textedit->setText(str2);
302 str2=tab2textedit->toPlainText();
304 tab2textedit->setText(str2);
306 str2=tab2textedit->toPlainText();
307 str2.append(str3.append("transZ="+str1.setNum(mArgsInfo.transZ_arg)));
308 tab2textedit->setText(str2);
312 str2=tab2textedit->toPlainText();
314 tab2textedit->setText(str2);
316 str2=tab2textedit->toPlainText();
317 str2.append(str3.append("metric="+str1.setNum(mArgsInfo.metric_arg)));
318 tab2textedit->setText(str2);
322 str2=tab2textedit->toPlainText();
324 tab2textedit->setText(str2);
326 str2=tab2textedit->toPlainText();
327 str2.append(str3.append("samples="+str1.setNum(mArgsInfo.samples_arg)));
328 tab2textedit->setText(str2);
332 str2=tab2textedit->toPlainText();
334 tab2textedit->setText(str2);
336 str2=tab2textedit->toPlainText();
337 str2.append(str3.append("intThreshold="+str1.setNum(mArgsInfo.intThreshold_arg)));
338 tab2textedit->setText(str2);
342 str2=tab2textedit->toPlainText();
344 tab2textedit->setText(str2);
346 str2=tab2textedit->toPlainText();
347 str2.append(str3.append("stdDev="+str1.setNum(mArgsInfo.stdDev_arg)));
348 tab2textedit->setText(str2);
352 str2=tab2textedit->toPlainText();
354 tab2textedit->setText(str2);
357 str2=tab2textedit->toPlainText();
358 str2.append(str3.append("blur="+str1.setNum(mArgsInfo.blur_arg)));
359 tab2textedit->setText(str2);
363 str2=tab2textedit->toPlainText();
365 tab2textedit->setText(str2);
368 str2=tab2textedit->toPlainText();
369 str2.append(str3.append("optimizer="+str1.setNum(mArgsInfo.optimizer_arg)));
370 tab2textedit->setText(str2);
374 str2=tab2textedit->toPlainText();
376 tab2textedit->setText(str2);
379 str2=tab2textedit->toPlainText();
380 str2.append(str3.append("step="+str1.setNum(mArgsInfo.step_arg)));
381 tab2textedit->setText(str2);
385 str2=tab2textedit->toPlainText();
387 tab2textedit->setText(str2);
390 str2=tab2textedit->toPlainText();
391 str2.append(str3.append("relax="+str1.setNum(mArgsInfo.relax_arg)));
392 tab2textedit->setText(str2);
396 str2=tab2textedit->toPlainText();
398 tab2textedit->setText(str2);
400 str2=tab2textedit->toPlainText();
401 str2.append(str3.append("valueTol="+str1.setNum(mArgsInfo.valueTol_arg)));
402 tab2textedit->setText(str2);
406 str2=tab2textedit->toPlainText();
408 tab2textedit->setText(str2);
410 str2=tab2textedit->toPlainText();
411 str2.append(str3.append("stepTol="+str1.setNum(mArgsInfo.stepTol_arg)));
412 tab2textedit->setText(str2);
416 str2=tab2textedit->toPlainText();
418 tab2textedit->setText(str2);
421 str2=tab2textedit->toPlainText();
422 str2.append(str3.append("gradTol="+str1.setNum(mArgsInfo.gradTol_arg)));
423 tab2textedit->setText(str2);
427 str2=tab2textedit->toPlainText();
429 tab2textedit->setText(str2);
431 str2=tab2textedit->toPlainText();
432 str2.append(str3.append("lineAcc="+str1.setNum(mArgsInfo.lineAcc_arg)));
433 tab2textedit->setText(str2);
437 str2=tab2textedit->toPlainText();
439 tab2textedit->setText(str2);
440 str2=tab2textedit->toPlainText();
441 str2.append(str3.append("convFactor="+str1.setNum(mArgsInfo.convFactor_arg)));
442 tab2textedit->setText(str2);
446 str2=tab2textedit->toPlainText();
448 tab2textedit->setText(str2);
450 str2=tab2textedit->toPlainText();
451 str2.append(str3.append("maxIt="+str1.setNum(mArgsInfo.maxIt_arg)));
452 tab2textedit->setText(str2);
456 str2=tab2textedit->toPlainText();
458 tab2textedit->setText(str2);
460 str2=tab2textedit->toPlainText();
461 str2.append(str3.append("maxLineIt="+str1.setNum(mArgsInfo.maxLineIt_arg)));
462 tab2textedit->setText(str2);
466 str2=tab2textedit->toPlainText();
468 tab2textedit->setText(str2);
470 str2=tab2textedit->toPlainText();
471 str2.append(str3.append("maxEval="+str1.setNum(mArgsInfo.maxEval_arg)));
472 tab2textedit->setText(str2);
476 str2=tab2textedit->toPlainText();
478 tab2textedit->setText(str2);
480 str2=tab2textedit->toPlainText();
481 str2.append(str3.append("maxCorr="+str1.setNum(mArgsInfo.maxCorr_arg)));
482 tab2textedit->setText(str2);
486 str2=tab2textedit->toPlainText();
488 tab2textedit->setText(str2);
490 str2=tab2textedit->toPlainText();
491 str2.append(str3.append("selectBound="+str1.setNum(mArgsInfo.selectBound_arg)));
492 tab2textedit->setText(str2);
496 str2=tab2textedit->toPlainText();
498 tab2textedit->setText(str2);
500 str2=tab2textedit->toPlainText();
501 str2.append(str3.append("rWeight="+str1.setNum(mArgsInfo.rWeight_arg)));
502 tab2textedit->setText(str2);
506 str2=tab2textedit->toPlainText();
508 tab2textedit->setText(str2);
510 str2=tab2textedit->toPlainText();
511 str2.append(str3.append("tWeight="+str1.setNum(mArgsInfo.tWeight_arg)));
512 tab2textedit->setText(str2);
516 str2=tab2textedit->toPlainText();
518 tab2textedit->setText(str2);
520 str2=tab2textedit->toPlainText();
521 str2.append(str3.append("levels="+str1.setNum(mArgsInfo.levels_arg)));
522 tab2textedit->setText(str2);
526 str2=tab2textedit->toPlainText();
528 tab2textedit->setText(str2);
530 str2=tab2textedit->toPlainText();
531 str2.append(str3.append("inc="+str1.setNum(mArgsInfo.inc_arg)));
532 tab2textedit->setText(str2);
536 str2=tab2textedit->toPlainText();
538 tab2textedit->setText(str2);
540 str2=tab2textedit->toPlainText();
541 str2.append(str3.append("dec="+str1.setNum(mArgsInfo.dec_arg)));
542 tab2textedit->setText(str2);
546 str2=tab2textedit->toPlainText();
548 tab2textedit->setText(str2);
550 //------------------------------------------------------------------------------
552 //------------------------------------------------------------------------------
553 void vvToolRigidReg::TransformSelect()
555 if(!mConfigFile.empty()){
556 mArgsInfo.transform_arg=cb_transform->currentIndex();
560 QMessageBox::information(this,"Warning","Load the Config File First!..");
563 //------------------------------------------------------------------------------
565 //------------------------------------------------------------------------------
566 void vvToolRigidReg::CmdlineParser(int override, int initialize)
568 //0 opened, 1 not opened fine
569 int opened=cmdline_parser_clitkAffineRegistration_configfile(const_cast<char*>(mConfigFile.c_str()),&mArgsInfo,override,initialize,1);
571 mArgsInfo.gradient_flag=1;
573 //Read the transformation parameters from the path in the config file(mArgsInfo.matrix_arg) and display it on the TextBox 2
575 std::vector<QString> Qstr;
578 readfile.open(mConfigFile.c_str());
581 QMessageBox::information(this,"Warning","Load the Config File First..");
583 if (readfile.is_open())
585 while (!readfile.eof())
587 readfile >> mConfigFile;
588 Qstr.push_back(QString(mConfigFile.c_str()));
593 QMessageBox::information(this,"Warning","Cannot Open File!");
596 for(unsigned int i=0;i<Qstr.size()-1;i++)
598 str=tab2textedit->toPlainText();
599 str.append(Qstr.at(i));
600 tab2textedit->setText(str);
602 tab2textedit->setText(str);
605 //------------------------------------------------------------------------------
607 //------------------------------------------------------------------------------
608 void vvToolRigidReg::OptimizerSelect()
610 if(!mConfigFile.empty()){
611 mArgsInfo.optimizer_arg=cb_optimizer->currentIndex();
615 QMessageBox::information(this,"Warning","Load the Config File First!..");
618 //------------------------------------------------------------------------------
620 //------------------------------------------------------------------------------
621 void vvToolRigidReg::InterpolatorSelect()
623 if(!mConfigFile.empty()){
624 mArgsInfo.interp_arg=cb_interpolator->currentIndex();
628 QMessageBox::information(this,"Warning","Load the Config File First!..");
631 //------------------------------------------------------------------------------
633 //------------------------------------------------------------------------------
634 void vvToolRigidReg::MetricSelect()
636 if(!mConfigFile.empty()){
637 mArgsInfo.metric_arg=cb_metric->currentIndex();
641 QMessageBox::information(this,"Warning","Load the Config File First!..");
644 //------------------------------------------------------------------------------
646 //------------------------------------------------------------------------------
647 void vvToolRigidReg::OutputSelect()
649 std::ostringstream osstream;
650 if(!mConfigFile.empty()){
651 if(cb_selectoutput->currentIndex()==0){
652 mArgsInfo.output_given=0;
653 mArgsInfo.checker_after_given=0;
654 mArgsInfo.checker_before_given=0;
655 mArgsInfo.after_given=0;
656 mArgsInfo.before_given=0;
657 }//get transformed output image
658 if(cb_selectoutput->currentIndex()==1){
659 mArgsInfo.output_given=0;
660 mArgsInfo.checker_after_given=1;
661 mArgsInfo.checker_before_given=0;
662 mArgsInfo.after_given=0;
663 mArgsInfo.before_given=0;
664 }//get checkered image after reg
665 if(cb_selectoutput->currentIndex()==2){
666 mArgsInfo.output_given=0;
667 mArgsInfo.checker_after_given=0;
668 mArgsInfo.checker_before_given=1;
669 mArgsInfo.after_given=0;
670 mArgsInfo.before_given=0;
671 }//get checkered image before reg
672 if(cb_selectoutput->currentIndex()==3){
673 mArgsInfo.output_given=0;
674 mArgsInfo.checker_after_given=0;
675 mArgsInfo.checker_before_given=0;
676 mArgsInfo.after_given=1;
677 mArgsInfo.before_given=0;
678 }//get difference image after reg
679 if(cb_selectoutput->currentIndex()==4){
680 mArgsInfo.output_given=0;
681 mArgsInfo.checker_after_given=0;
682 mArgsInfo.checker_before_given=0;
683 mArgsInfo.after_given=0;
684 mArgsInfo.before_given=1;
685 }//get difference image before reg
688 QMessageBox::information(this,"Warning","Load the Config File First!..");
691 //------------------------------------------------------------------------------
693 //------------------------------------------------------------------------------
694 void vvToolRigidReg::SaveTextEdit()
696 DD(mArgsInfo.transform_arg);
697 QString f1 = QFileDialog::getSaveFileName(this, tr("Save Config File"),
698 mMainWindow->GetInputPathName(),
699 tr("Text (*.mat *.txt *.doc *.rtf)"));
701 if(file.open(QFile::WriteOnly | QFile::Truncate) & !mConfigFile.empty() ){
702 QTextStream stream( &file );
703 stream << tab2textedit->toPlainText();
707 QMessageBox::information(this,"Warning","Nothing to Save!");
710 tab2textedit->clear();
711 DD(mArgsInfo.transform_arg);
713 //------------------------------------------------------------------------------
715 //------------------------------------------------------------------------------
716 void vvToolRigidReg::InputIsSelected(std::vector<vvSlicerManager *> & l)
721 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix(),textEdit_2);
723 for(int i =0;i<4;i++)
727 mInitialMatrix[i*4+j]=mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()->GetElement(i,j);
731 if(mInput1->GetFileName()==mInput2->GetFileName())
733 QMessageBox::information(this, "Warning","Your Reference and Target Images are the same");
736 SetOverlay(mInput2->GetImage());
737 mImageSize=mInput1->GetImage()->GetSize();
741 //connect all sigs to slots
742 connect(resetbutton, SIGNAL(pressed()), this, SLOT(ResetTransform()));
743 connect(tab2loadbutton, SIGNAL(pressed()), this, SLOT(GetArgsInfoFromGUI()));
744 connect(tab2applybutton, SIGNAL(pressed()), this, SLOT(AutoRegister()));
747 connect(Xval, SIGNAL(editingFinished()), this, SLOT(SetXvalue()));
748 connect(Yval, SIGNAL(editingFinished()), this, SLOT(SetYvalue()));
749 connect(Zval, SIGNAL(editingFinished()), this, SLOT(SetZvalue()));
751 connect(xtrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders()));
752 connect(ytrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders()));
753 connect(ztrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders()));
756 connect(xrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders()));
757 connect(yrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders()));
758 connect(zrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateTransform_sliders()));
760 connect(xtrans_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb()));
761 connect(ytrans_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb()));
762 connect(ztrans_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb()));
763 connect(xrot_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb()));
764 connect(yrot_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb()));
765 connect(zrot_sb, SIGNAL(valueChanged(double)), this, SLOT(UpdateTransform_sb()));
767 connect(loadbutton, SIGNAL(pressed()), this, SLOT(LoadFile()));
768 connect(savebutton, SIGNAL(pressed()), this, SLOT(SaveFile()));
770 connect(checkBox_rigid, SIGNAL(clicked(bool)), this, SLOT(CheckRigidReg()));
771 connect(checkBox_deformable, SIGNAL(clicked(bool)), this, SLOT(CheckDeformableReg()));
773 connect(cb_presets, SIGNAL(activated(int)), this, SLOT(Presets()));
774 connect(cb_transform, SIGNAL(activated(int)), this, SLOT(TransformSelect()));
775 connect(cb_optimizer, SIGNAL(activated(int)), this, SLOT(OptimizerSelect()));
776 connect(cb_interpolator, SIGNAL(activated(int)), this, SLOT(InterpolatorSelect()));
777 connect(cb_metric, SIGNAL(activated(int)), this, SLOT(MetricSelect()));
778 connect(cb_selectoutput, SIGNAL(activated(int)), this, SLOT(OutputSelect()));
779 connect(tab2savebutton, SIGNAL(pressed()), this, SLOT(SaveTextEdit()));
782 //------------------------------------------------------------------------------
784 //------------------------------------------------------------------------------
785 void vvToolRigidReg::apply()
789 //------------------------------------------------------------------------------
791 //------------------------------------------------------------------------------
792 void vvToolRigidReg::CheckRigidReg()
794 checkBox_deformable->setChecked(false);
795 cb_transform->show();
798 cb_interpolator->show();
799 cb_optimizer->show();
800 cb_selectoutput->show();
805 interpollabel->show();
809 //------------------------------------------------------------------------------
811 //------------------------------------------------------------------------------
812 void vvToolRigidReg::CheckDeformableReg()
814 checkBox_rigid->setChecked(false);
816 cb_transform->show();
818 cb_interpolator->show();
819 cb_optimizer->show();
820 cb_selectoutput->show();
826 interpollabel->show();
828 //------------------------------------------------------------------------------
830 //------------------------------------------------------------------------------
831 void vvToolRigidReg::SetOverlay(vvImage::Pointer Image)
833 for (int i =0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
834 mCurrentSlicerManager->GetSlicer(i)->SetOverlay(Image);
835 mCurrentSlicerManager->GetSlicer(i)->SetActorVisibility("overlay",0,true);
836 mCurrentSlicerManager->SetColorMap();
837 mCurrentSlicerManager->Render();
840 //------------------------------------------------------------------------------
842 //------------------------------------------------------------------------------
843 void vvToolRigidReg::RemoveOverlay()
845 for(int i=0;i<mCurrentSlicerManager->NumberOfSlicers();i++)
847 mInput1->RemoveActor("overlay",0);
848 mInput1->SetColorMap(0);
853 //------------------------------------------------------------------------------
855 //------------------------------------------------------------------------------
856 void vvToolRigidReg::SetXvalue()
858 QString xstr = Xval->text();
860 //------------------------------------------------------------------------------
862 //------------------------------------------------------------------------------
863 void vvToolRigidReg::SetYvalue()
865 QString ystr = Yval->text();
867 //------------------------------------------------------------------------------
869 //------------------------------------------------------------------------------
870 void vvToolRigidReg::SetZvalue()
872 QString zstr = Zval->text();
874 //------------------------------------------------------------------------------
876 //------------------------------------------------------------------------------
877 void vvToolRigidReg::SetTransform(double tX, double tY, double tZ, double aX, double aY, double aZ,bool update)
879 vtkSmartPointer<vtkTransform> transform = mInput1->GetImage()->GetTransform();
880 transform->PostMultiply();
882 if (aX!=0 || aY!=0 || aZ!=0) {
884 x= Xval->text().toDouble();
885 y= Yval->text().toDouble();
886 z= Zval->text().toDouble();
887 transform->Translate(-x,-y,-z);
888 if (aX!=0) transform->RotateX(aX);
889 if (aY!=0) transform->RotateY(aY);
890 if (aZ!=0) transform->RotateZ(aZ);
891 transform->Translate(x,y,z);
894 if (tX!=0||tY!=0||tZ!=0)
895 transform->Translate(tX*mInput1->GetImage()->GetSpacing()[0],tY*mInput1->GetImage()->GetSpacing()[1],tZ*mInput1->GetImage()->GetSpacing()[2]);
897 //------------------------------------------------------------------------------
899 //------------------------------------------------------------------------------
900 void vvToolRigidReg::SetTransform(vtkMatrix4x4 *matrix)
902 vtkSmartPointer<vtkTransform> transform=vtkSmartPointer<vtkTransform>::New();
903 for(int i=0; i<4;i++)
905 mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()->SetElement(i,j,matrix->GetElement(i,j));
909 //------------------------------------------------------------------------------
911 //------------------------------------------------------------------------------
912 void vvToolRigidReg::LoadFile()
916 //------------------------------------------------------------------------------
918 //------------------------------------------------------------------------------
919 void vvToolRigidReg::Render()
921 for (int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
922 mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent();
923 mCurrentSlicerManager->GetSlicer(i)->Render();
926 //------------------------------------------------------------------------------
928 //------------------------------------------------------------------------------
929 void vvToolRigidReg::UpdateTextEditor(vtkMatrix4x4 *matrix,QTextEdit* textEdit)
931 QFont font=QFont("Courier",11);
932 textEdit->setCurrentFont(font);
935 QString str1,str2,str3;
938 textEdit->setAcceptRichText(true);
939 str2=textEdit->toPlainText();
940 str2.append("#Rotation Center(mm): \n#");
941 textEdit->setText(str2);
943 str2=textEdit->toPlainText();
944 textEdit->setTextColor(QColor(255,0,0));
945 str2.append(str3.append(Xval->text()));
946 textEdit->setText(str2);
949 str2=textEdit->toPlainText();
951 textEdit->setText(str2);
953 str2=textEdit->toPlainText();
954 str2.append(str3.append(Yval->text()));
955 textEdit->setText(str2);
958 str2=textEdit->toPlainText();
960 textEdit->setText(str2);
963 str2=textEdit->toPlainText();
964 str2.append(str3.append(Zval->text()));
965 textEdit->setText(str2);
968 str2=textEdit->toPlainText();
970 textEdit->setText(str2);
973 str2=textEdit->toPlainText();
974 str2.append("#Transformation Matrix(mm):\n");
975 textEdit->setText(str2);
980 str2=textEdit->toPlainText();
981 // str2.append("\t"+str1.setNum(matrix->Element[i][j]));
982 str2.append(QString("%1\t").arg(str1.setNum(matrix->Element[i][j]),2));
983 textEdit->setText(str2);
985 str2=textEdit->toPlainText();
987 textEdit->setText(str2);
989 //QString str = QFileDialog::getOpenFileName();
990 textEdit->setTextColor(QColor(255,0,0));
991 textEdit->setFont(QFont("courrier new",12,4,true));
992 textEdit->toPlainText().toAscii();
994 str2=textEdit->toPlainText();
995 textEdit->setText(str2);
997 //------------------------------------------------------------------------------
999 //------------------------------------------------------------------------------
1000 void vvToolRigidReg::UpdateTransform_sliders()
1002 InitializeSliders(xtrans_slider->value()*mInput1->GetImage()->GetSpacing()[0],
1003 ytrans_slider->value()*mInput1->GetImage()->GetSpacing()[1],
1004 ztrans_slider->value()*mInput1->GetImage()->GetSpacing()[2],
1005 xrot_slider->value(),yrot_slider->value(),zrot_slider->value(),false);
1006 UpdateTransform(true);
1009 //------------------------------------------------------------------------------
1011 //------------------------------------------------------------------------------
1012 void vvToolRigidReg::UpdateTransform_sb()
1014 InitializeSliders(xtrans_sb->value(),
1017 xrot_sb->value(),yrot_sb->value(),zrot_sb->value(),false);
1018 UpdateTransform(false);
1021 //------------------------------------------------------------------------------
1023 //------------------------------------------------------------------------------
1024 void vvToolRigidReg::AutoRegister()
1026 if (!mCurrentSlicerManager) close();
1028 if(!mConfigFile.empty()){
1029 std::vector<vvImage::Pointer> inputs;
1031 inputs.push_back(mInput1->GetImage());
1032 inputs.push_back(mInput2->GetImage());
1035 clitk::AffineRegistrationGenericFilter::Pointer filter =
1036 clitk::AffineRegistrationGenericFilter::New();
1037 filter->SetInputVVImages(inputs);
1038 filter->SetArgsInfo(mArgsInfo);
1039 filter->EnableReadOnDisk(false);
1041 std::ostringstream osstream;
1042 vvImage::Pointer output;
1043 output = filter->GetOutputVVImage();
1046 if(!cb_selectoutput->currentIndex()==0){
1047 std::string outputstring;
1048 output = filter->GetOutputVVImages()[1];
1049 if(cb_selectoutput->currentIndex()==1){outputstring="Checkered_after_reg";}
1050 if(cb_selectoutput->currentIndex()==2){outputstring="Checkered_before_reg";}
1051 if(cb_selectoutput->currentIndex()==3){outputstring="Diff_after_reg";}
1052 if(cb_selectoutput->currentIndex()==4){outputstring="Diff_before_reg";}
1053 osstream << outputstring << mCurrentSlicerManager->GetSlicer(0)->GetFileName() << ".mhd";
1054 AddImage(output,osstream.str());
1055 filter->DeleteLastOutputImage();
1057 DD(filter->GetOutputVVImages().capacity());
1058 QApplication::restoreOverrideCursor();
1063 QMessageBox::information(this, "Warning","Load the Config File First!...");
1067 //------------------------------------------------------------------------------
1069 //------------------------------------------------------------------------------
1070 void vvToolRigidReg::UpdateTransform(bool slider_enabled)
1072 vtkSmartPointer<vtkTransform> transform_final=mInput1->GetImage()->GetTransform();
1073 transform_final->SetMatrix(mInitialMatrix);
1074 transform_final->PostMultiply();
1076 double x=0, y=0 ,z=0;
1077 x= Xval->text().toDouble();
1078 y= Yval->text().toDouble();
1079 z= Zval->text().toDouble();
1080 transform_final->Translate(-x,-y,-z);
1082 transform_final->RotateY(yrot_slider->value());
1083 transform_final->RotateX(xrot_slider->value());
1084 transform_final->RotateZ(zrot_slider->value());
1087 transform_final->RotateY(yrot_sb->value());
1088 transform_final->RotateX(xrot_sb->value());
1089 transform_final->RotateZ(zrot_sb->value());
1091 transform_final->Translate(x,y,z);
1092 transform_final->PreMultiply();
1094 transform_final->Translate(xtrans_slider->value()*mInput1->GetImage()->GetSpacing()[0],0,0);
1095 transform_final->Translate(0,ytrans_slider->value()*mInput1->GetImage()->GetSpacing()[1],0);
1096 transform_final->Translate(0,0,ztrans_slider->value()*mInput1->GetImage()->GetSpacing()[2]);
1099 transform_final->Translate(xtrans_sb->value(),0,0);
1100 transform_final->Translate(0,ytrans_sb->value(),0);
1101 transform_final->Translate(0,0,ztrans_sb->value());
1103 transform_final->Update();
1105 UpdateTextEditor(transform_final->GetMatrix(),textEdit);
1107 //------------------------------------------------------------------------------
1110 //------------------------------------------------------------------------------
1111 void vvToolRigidReg::SaveFile()
1113 //Write the Transformation Matrix
1114 QString f1 = QFileDialog::getSaveFileName(this, tr("Save Transformation Matrix File"),
1115 mMainWindow->GetInputPathName(),
1116 tr("Text (*.mat *.txt *.doc *.rtf)"));
1118 std::vector<QString> transparameters;
1121 for(int i =0;i<4;i++)
1122 for(int j=0;j<4;j++)
1123 transparameters.push_back(line1.setNum(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix()->Element[i][j]));
1125 if (file1.open(QFile::WriteOnly | QFile::Truncate)) {
1126 QTextStream out1(&file1);
1127 for(int i =0;i<4;i++){
1128 for(int j=0;j<4;j++) {
1129 out1<<transparameters[i*4+j]+"\t";
1136 QMessageBox::information(this,"Warning","Error Reading Parameters");
1139 //------------------------------------------------------------------------------
1141 //------------------------------------------------------------------------------
1142 void vvToolRigidReg::ReadFile(bool matrix_given)
1146 double * orientations=new double[3];
1147 double * translations=new double[3];
1148 vtkMatrix4x4 *matrix=vtkMatrix4x4::New();
1149 std::string transfile;
1150 vtkSmartPointer<vtkTransform> transform = mCurrentSlicerManager->GetImage()->GetTransform();
1153 //Open File to read the transformation parameters
1154 QString file1 = QFileDialog::getOpenFileName(
1156 "Choose the Transformation Parameters file",
1157 mMainWindow->GetInputPathName(),
1158 "Text (*.mat *.txt *.rtf *.doc)");
1159 if (file1.isEmpty())
1161 QFile Qfile1(file1);
1162 // ifstream readfile;
1163 transfile= file1.toStdString();
1167 transfile=mArgsInfo.matrix_arg;
1170 std::string filename1(transfile);
1171 std::ifstream f1(filename1.c_str());
1175 itk::Matrix<double, 4, 4> itkMat = clitk::ReadMatrix3D(transfile);
1176 for(int j=0; j<4; j++)
1177 for(int i=0; i<4; i++)
1178 matrix->SetElement(i,j,rint(itkMat[i][j]));
1180 UpdateTextEditor(matrix,textEdit);
1181 transform->SetMatrix(matrix);
1182 transform->GetOrientation(orientations);
1183 transform->PostMultiply();
1185 //Obtain the Rotation Center , set it to origin
1186 Xval->setText(center.setNum(0));
1187 Yval->setText(center.setNum(0));
1188 Zval->setText(center.setNum(0));
1190 //In the Order or Y X Z //
1191 //now postmultiply for the rotations
1192 SetTransform(0,0,0,0,0,-rint(orientations[2]),false);
1193 SetTransform(0,0,0,-rint(orientations[0]),0,0,false);
1194 SetTransform(0,0,0,0,-rint(orientations[1]),0,false);
1196 transform->GetPosition(translations);
1197 transform->Identity();
1199 DD(translations[0]/mInput1->GetImage()->GetSpacing()[0]);
1200 DD(translations[1]/mInput1->GetImage()->GetSpacing()[1]);
1201 DD(translations[2]/mInput1->GetImage()->GetSpacing()[2]);
1202 DD(mInput1->GetImage()->GetSpacing()[0]);
1203 DD(mInput1->GetImage()->GetSpacing()[1]);
1204 DD(mInput1->GetImage()->GetSpacing()[2]);
1205 DD(orientations[0]);
1206 DD(orientations[1]);
1207 DD(orientations[2]);
1208 //set the sliders and spin box values
1209 InitializeSliders(rint(translations[0]),rint(translations[1])
1210 ,rint(translations[2]),rint(orientations[0]),rint(orientations[1]),rint(orientations[2]),true);
1211 SetTransform(matrix);
1213 //------------------------------------------------------------------------------
1215 //------------------------------------------------------------------------------
1216 void vvToolRigidReg::ResetTransform()
1218 vtkSmartPointer<vtkTransform> transform = mCurrentSlicerManager->GetImage()->GetTransform();
1219 transform->SetMatrix(mInitialMatrix);
1220 transform->Update();
1223 SetRotationCenter();
1225 UpdateTextEditor(transform->GetMatrix(),textEdit);
1227 //------------------------------------------------------------------------------
1229 //------------------------------------------------------------------------------
1230 void vvToolRigidReg::SetRotationCenter()
1232 //default image rotation center is the center of the image
1233 QString xcord,ycord,zcord;
1234 std::vector<double> imageorigin;
1235 imageorigin=mInput1->GetImage()->GetOrigin();
1237 xcord=xcord.setNum(imageorigin[0]+mImageSize[0]*mInput1->GetImage()->GetSpacing()[0]/2, 'g', 3);
1238 ycord=ycord.setNum(imageorigin[1]+mImageSize[1]*mInput1->GetImage()->GetSpacing()[1]/2, 'g', 3);
1239 zcord=zcord.setNum(imageorigin[2]+mImageSize[2]*mInput1->GetImage()->GetSpacing()[2]/2, 'g', 3);
1241 Xval->setText(xcord);
1242 Yval->setText(ycord);
1243 Zval->setText(zcord);
1244 InitializeSliders(0,0,0,0,0,0,true);
1246 //------------------------------------------------------------------------------
1248 //------------------------------------------------------------------------------
1249 void vvToolRigidReg::InitializeSliders(double xtrans,double ytrans, double ztrans, double xrot, double yrot, double zrot,bool sliders)
1252 xtrans_sb->blockSignals(true);
1253 xtrans_sb->setSingleStep(mInput1->GetImage()->GetSpacing()[0]);
1254 xtrans_sb->setValue(xtrans);
1255 xtrans_sb->blockSignals(false);
1256 ytrans_sb->blockSignals(true);
1257 ytrans_sb->setSingleStep(mInput1->GetImage()->GetSpacing()[1]);
1258 ytrans_sb->setValue(ytrans);
1259 ytrans_sb->blockSignals(false);
1260 ztrans_sb->blockSignals(true);
1261 ztrans_sb->setSingleStep(mInput1->GetImage()->GetSpacing()[2]);
1262 ztrans_sb->setValue(ztrans);
1263 ztrans_sb->blockSignals(false);
1266 xtrans_slider->blockSignals(true);
1267 xtrans_slider->setValue(rint(xtrans/mInput1->GetImage()->GetSpacing()[0]));
1268 xtrans_slider->blockSignals(false);
1269 ytrans_slider->blockSignals(true);
1270 ytrans_slider->setValue(rint(ytrans/mInput1->GetImage()->GetSpacing()[1]));
1271 ytrans_slider->blockSignals(false);
1272 ztrans_slider->blockSignals(true);
1273 ztrans_slider->setValue(rint(ztrans/mInput1->GetImage()->GetSpacing()[2]));
1274 ztrans_slider->blockSignals(false);
1276 xrot_sb->blockSignals(true);
1277 xrot_sb->setValue(xrot);
1278 xrot_sb->blockSignals(false);
1279 yrot_sb->blockSignals(true);
1280 yrot_sb->setValue(yrot);
1281 yrot_sb->blockSignals(false);
1282 zrot_sb->blockSignals(true);
1283 zrot_sb->setValue(zrot);
1284 zrot_sb->blockSignals(false);
1285 xrot_slider->blockSignals(true);
1286 xrot_slider->setValue(xrot);
1287 xrot_slider->blockSignals(false);
1288 yrot_slider->blockSignals(true);
1289 yrot_slider->setValue(yrot);
1290 yrot_slider->blockSignals(false);
1291 zrot_slider->blockSignals(true);
1292 zrot_slider->setValue(zrot);
1293 zrot_slider->blockSignals(false);
1295 //------------------------------------------------------------------------------
1297 //------------------------------------------------------------------------------
1298 void vvToolRigidReg::SetSliderRanges()
1300 xtrans_slider->blockSignals(true);
1301 xtrans_slider->setRange(-2000,2000);
1302 xtrans_slider->blockSignals(false);
1304 ytrans_slider->blockSignals(true);
1305 ytrans_slider->setRange(-2000,2000);
1306 ytrans_slider->blockSignals(false);
1308 ztrans_slider->blockSignals(true);
1309 ztrans_slider->setRange(-2000,2000);
1310 ztrans_slider->blockSignals(false);
1312 xtrans_sb->blockSignals(true);
1313 xtrans_sb->setRange(-2000,2000);
1314 xtrans_sb->setDecimals(3);
1315 xtrans_sb->blockSignals(false);
1317 ytrans_sb->blockSignals(true);
1318 ytrans_sb->setRange(-2000,2000);
1319 ytrans_sb->setDecimals(3);
1320 ytrans_sb->blockSignals(false);
1322 ztrans_sb->blockSignals(true);
1323 ztrans_sb->setRange(-2000,2000);
1324 ztrans_sb->setDecimals(3);
1325 ztrans_sb->blockSignals(false);
1327 xrot_slider->blockSignals(true);
1328 xrot_slider->setRange(-360,360);
1329 xrot_slider->blockSignals(false);
1331 yrot_slider->blockSignals(true);
1332 yrot_slider->setRange(-360,360);
1333 yrot_slider->blockSignals(false);
1335 zrot_slider->blockSignals(true);
1336 zrot_slider->setRange(-360,360);
1337 zrot_slider->blockSignals(false);
1339 xrot_sb->blockSignals(true);
1340 xrot_sb->setRange(-360,360);
1341 xrot_sb->blockSignals(false);
1343 yrot_sb->blockSignals(true);
1344 yrot_sb->setRange(-360,360);
1345 yrot_sb->blockSignals(false);
1347 zrot_sb->blockSignals(true);
1348 zrot_sb->setRange(-360,360);
1349 zrot_sb->blockSignals(false);