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 ======================================================================-====*/
18 #include "vvToolRigidReg.h"
21 #include <QApplication>
22 #include <vtkImageData.h>
23 #include <vtkSmartPointer.h>
24 #include <vtkTransform.h>
29 #include <QMessageBox>
30 #include <vvMainWindow.h>
31 #include <QFileDialog>
32 #include <QTextStream>
33 //------------------------------------------------------------------------------
34 // Create the tool and automagically (I like this word) insert it in
35 // the main window menu.
36 ADD_TOOL(vvToolRigidReg);
37 //------------------------------------------------------------------------------
39 //------------------------------------------------------------------------------
40 vvToolRigidReg::vvToolRigidReg(vvMainWindowBase * parent, Qt::WindowFlags f):
41 vvToolWidgetBase(parent, f),
42 vvToolBase<vvToolRigidReg>(parent),
46 Ui_vvToolRigidReg::setupUi(mToolWidget);
50 textEdit->setFixedSize(qsize);
52 // Set how many inputs are needed for this tool
53 AddInputSelector("Select the Reference Image");
54 AddInputSelector("Select the Target Image");
56 //------------------------------------------------------------------------------
58 //------------------------------------------------------------------------------
59 vvToolRigidReg::~vvToolRigidReg()
62 //------------------------------------------------------------------------------
64 //------------------------------------------------------------------------------
65 bool vvToolRigidReg::close()
68 return vvToolWidgetBase::close();
70 //------------------------------------------------------------------------------
72 //------------------------------------------------------------------------------
73 void vvToolRigidReg::reject()
76 return vvToolWidgetBase::reject();
78 //------------------------------------------------------------------------------
80 //------------------------------------------------------------------------------
81 void vvToolRigidReg::InputIsSelected(std::vector<vvSlicerManager *> & l)
86 if(mInput1->GetFileName()==mInput2->GetFileName())
88 QMessageBox::information(this, "Warning","Your Reference and Target Images are the same");
92 mImageSize=mInput1->GetImage()->GetSize();
97 //connect all sigs to slots
98 connect(resetbutton, SIGNAL(pressed()), this, SLOT(ResetTransform()));
100 connect(Xval, SIGNAL(editingFinished()), this, SLOT(SetXvalue()));
101 connect(Yval, SIGNAL(editingFinished()), this, SLOT(SetYvalue()));
102 connect(Zval, SIGNAL(editingFinished()), this, SLOT(SetZvalue()));
103 connect(xtrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateXtranslider()));
105 connect(ytrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateYtranslider()));
106 connect(ztrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateZtranslider()));
109 connect(xrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateXrotslider()));
110 connect(yrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateYrotslider()));
111 connect(zrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateZrotslider()));
114 connect(xtrans_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateXtransb()));
115 connect(ytrans_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateYtransb()));
116 connect(ztrans_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateZtransb()));
117 connect(xrot_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateXrotsb()));
118 connect(yrot_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateYrotsb()));
119 connect(zrot_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateZrotsb()));
122 connect(loadbutton, SIGNAL(pressed()), this, SLOT(ReadFile()));
123 connect(savebutton, SIGNAL(pressed()), this, SLOT(SaveFile()));
125 //------------------------------------------------------------------------------
127 //------------------------------------------------------------------------------
128 void vvToolRigidReg::apply()
130 QMessageBox mb( "Keep Overlay",
131 "Do you want to keep the overlay on?\n"
132 "You can switch it off anytime by pressing 'x'",
133 QMessageBox::Information,
134 QMessageBox::Yes | QMessageBox::Default,
136 QMessageBox::Cancel | QMessageBox::Escape );
137 mb.setButtonText( QMessageBox::Yes, "Yes" );
138 mb.setButtonText( QMessageBox::No, "No" );
139 switch( mb.exec() ) {
140 case QMessageBox::Yes:
143 case QMessageBox::No:
148 for(int i=0;i<mCurrentSlicerManager->NumberOfSlicers();i++)
150 mInput1->RemoveActor("overlay",0);
151 mInput1->SetColorMap(0);
156 case QMessageBox::Cancel:
160 //------------------------------------------------------------------------------
162 //------------------------------------------------------------------------------
163 void vvToolRigidReg::SetOverlay()
165 for (int i =0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
166 mCurrentSlicerManager->GetSlicer(i)->SetOverlay(mInput2->GetImage());
167 mCurrentSlicerManager->GetSlicer(i)->SetActorVisibility("overlay",0,true);
168 mCurrentSlicerManager->SetColorMap();
169 mCurrentSlicerManager->Render();
172 //------------------------------------------------------------------------------
174 //------------------------------------------------------------------------------
175 void vvToolRigidReg::SetXvalue()
177 QString xstr = Xval->text();
179 //------------------------------------------------------------------------------
181 //------------------------------------------------------------------------------
182 void vvToolRigidReg::SetYvalue()
184 QString ystr = Yval->text();
186 //------------------------------------------------------------------------------
188 //------------------------------------------------------------------------------
189 void vvToolRigidReg::SetZvalue()
191 QString zstr = Zval->text();
193 //------------------------------------------------------------------------------
195 //------------------------------------------------------------------------------
196 void vvToolRigidReg::SetTransform(double tX, double tY, double tZ, double aX, double aY, double aZ,bool update)
199 vtkSmartPointer<vtkTransform> transform = mCurrentSlicerManager->GetImage()->GetTransform();
200 transform->PostMultiply();
202 if (aX!=0 || aY!=0 || aZ!=0) {
204 x= Xval->text().toDouble();
205 y= Yval->text().toDouble();
206 z= Zval->text().toDouble();
207 transform->Translate(-x,-y,-z);
208 if (aX!=0) transform->RotateX(aX);
209 if (aY!=0) transform->RotateY(aY);
210 if (aZ!=0) transform->RotateZ(aZ);
211 transform->Translate(x,y,z);
214 if (tX!=0||tY!=0||tZ!=0)
215 transform->Translate(tX*mInput1->GetImage()->GetSpacing()[0],tY*mInput1->GetImage()->GetSpacing()[1],tZ*mInput1->GetImage()->GetSpacing()[2]);
220 for (int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
221 mCurrentSlicerManager->GetImage()->UpdateReslice();
222 mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent();
223 mCurrentSlicerManager->GetSlicer(i)->Render();
227 //------------------------------------------------------------------------------
229 //------------------------------------------------------------------------------
230 void vvToolRigidReg::SetTransform(vtkMatrix4x4 *matrix)
232 vtkSmartPointer<vtkTransform> transform =mCurrentSlicerManager->GetImage()->GetTransform();
233 transform->SetMatrix(matrix);
234 transform->PostMultiply();
235 transform->Concatenate(matrix);
237 mCurrentSlicerManager->GetImage()->SetTransform(transform);
239 for (int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
240 mCurrentSlicerManager->GetImage()->UpdateReslice();
241 mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent();
242 mCurrentSlicerManager->GetSlicer(i)->Render();
245 //------------------------------------------------------------------------------
247 //------------------------------------------------------------------------------
248 void vvToolRigidReg::UpdateTextEditor(vtkMatrix4x4 *matrix)
250 QString str1,str2,str3;
253 textEdit->setAcceptRichText(true);
255 str2=textEdit->toPlainText();
256 str2.append("Rotation Center(mm): \n");
257 textEdit->setText(str2);
259 str2=textEdit->toPlainText();
260 textEdit->setTextColor(QColor(255,0,0));
261 str2.append(str3.append(Xval->text()));
262 textEdit->setText(str2);
265 str2=textEdit->toPlainText();
267 textEdit->setText(str2);
269 str2=textEdit->toPlainText();
270 str2.append(str3.append(Yval->text()));
271 textEdit->setText(str2);
274 str2=textEdit->toPlainText();
276 textEdit->setText(str2);
279 str2=textEdit->toPlainText();
280 str2.append(str3.append(Zval->text()));
281 textEdit->setText(str2);
284 str2=textEdit->toPlainText();
286 textEdit->setText(str2);
289 str2=textEdit->toPlainText();
290 str2.append("Transformation Matrix(mm):\n");
291 textEdit->setText(str2);
297 str2=textEdit->toPlainText();
298 // str2.append("\t"+str1.setNum(matrix->Element[i][j]));
299 str2.append(QString("%1\t").arg(str1.setNum(matrix->Element[i][j]),20));
300 textEdit->setText(str2);
302 str2=textEdit->toPlainText();
304 textEdit->setText(str2);
306 //QString str = QFileDialog::getOpenFileName();
307 textEdit->setTextColor(QColor(255,0,0));
308 textEdit->setFont(QFont("courrier new",12,4,true));
309 textEdit->toPlainText().toAscii();
311 str2=textEdit->toPlainText();
312 textEdit->setText(str2);
314 //------------------------------------------------------------------------------
316 //------------------------------------------------------------------------------
317 void vvToolRigidReg::UpdateXtransb()
320 diff=xtrans_sb->value()-xtrans_slider->value();
321 this->SetTransform(diff, 0, 0, 0, 0, 0,true);
322 xtrans_slider->setValue(xtrans_sb->value());
323 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
326 //------------------------------------------------------------------------------
328 //------------------------------------------------------------------------------
329 void vvToolRigidReg::UpdateXtranslider()
332 diff=xtrans_slider->value()-xtrans_sb->value();
333 this->SetTransform(diff, 0, 0, 0, 0, 0,true);
334 xtrans_sb->setValue(xtrans_slider->value());
335 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
337 //------------------------------------------------------------------------------
339 //------------------------------------------------------------------------------
340 void vvToolRigidReg::UpdateYtransb()
344 diff=ytrans_sb->value()-ytrans_slider->value();
345 this->SetTransform(0, diff, 0, 0, 0, 0,true);
346 ytrans_slider->setValue(ytrans_sb->value());
347 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
350 //------------------------------------------------------------------------------
352 //------------------------------------------------------------------------------
353 void vvToolRigidReg::UpdateYtranslider()
356 diff=ytrans_slider->value()-ytrans_sb->value();
357 this->SetTransform(0, diff, 0, 0, 0, 0,true);
358 ytrans_sb->setValue(ytrans_slider->value());
359 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
361 //------------------------------------------------------------------------------
363 //------------------------------------------------------------------------------
364 void vvToolRigidReg::UpdateZtransb()
368 diff=ztrans_sb->value()-ztrans_slider->value();
369 this->SetTransform(0, 0, diff, 0, 0, 0,true);
370 ztrans_slider->setValue(ztrans_sb->value());
371 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
374 //------------------------------------------------------------------------------
376 //------------------------------------------------------------------------------
377 void vvToolRigidReg::UpdateZtranslider()
380 diff=ztrans_slider->value()-ztrans_sb->value();
381 this->SetTransform(0, 0, diff, 0, 0, 0,true);
382 ztrans_sb->setValue(ztrans_slider->value());
383 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
385 //------------------------------------------------------------------------------
387 //------------------------------------------------------------------------------
388 void vvToolRigidReg::UpdateXrotsb()
392 diff=xrot_sb->value()-xrot_slider->value();
393 this->SetTransform(0, 0, 0, diff, 0, 0,true);
394 xrot_slider->setValue(xrot_sb->value());
395 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
398 //------------------------------------------------------------------------------
400 //------------------------------------------------------------------------------
401 void vvToolRigidReg::UpdateXrotslider()
404 diff=xrot_slider->value()-xrot_sb->value();
405 this->SetTransform(0, 0, 0, diff, 0, 0,true);
406 xrot_sb->setValue(xrot_slider->value());
407 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
409 //------------------------------------------------------------------------------
411 //------------------------------------------------------------------------------
412 void vvToolRigidReg::UpdateYrotsb()
416 diff=yrot_sb->value()-yrot_slider->value();
417 this->SetTransform(0, 0, 0, 0, diff, 0,true);
418 yrot_slider->setValue(yrot_sb->value());
419 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
422 //------------------------------------------------------------------------------
424 //------------------------------------------------------------------------------
425 void vvToolRigidReg::UpdateYrotslider()
428 diff=yrot_slider->value()-yrot_sb->value();
429 this->SetTransform(0, 0, 0, 0, diff, 0,true);
430 yrot_sb->setValue(yrot_slider->value());
431 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
433 //------------------------------------------------------------------------------
435 //------------------------------------------------------------------------------
436 void vvToolRigidReg::UpdateZrotsb()
440 diff=zrot_sb->value()-zrot_slider->value();
441 this->SetTransform(0, 0, 0, 0, 0,diff,true);
442 zrot_slider->setValue(zrot_sb->value());
443 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
446 //------------------------------------------------------------------------------
448 //------------------------------------------------------------------------------
449 void vvToolRigidReg::UpdateZrotslider()
452 diff=zrot_slider->value()-zrot_sb->value();
453 this->SetTransform(0, 0, 0, 0, 0, diff,true);
454 zrot_sb->setValue(zrot_slider->value());
455 UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
457 //------------------------------------------------------------------------------
459 //------------------------------------------------------------------------------
460 void vvToolRigidReg::SaveFile()
462 QString s = QFileDialog::getSaveFileName(this, tr("Save File"),
464 tr("Text (*.txt *.doc *.rtf)"));
466 if (file.open(QFile::WriteOnly | QFile::Truncate)) {
467 QTextStream out(&file);
468 out<<textEdit->toPlainText() ;
471 //------------------------------------------------------------------------------
473 //------------------------------------------------------------------------------
474 void vvToolRigidReg::ReadFile()
476 vtkMatrix4x4 *matrix=vtkMatrix4x4::New();
477 vtkSmartPointer<vtkTransform> transform = mCurrentSlicerManager->GetImage()->GetTransform();
478 QString s = QFileDialog::getOpenFileName(
482 "Text (*.txt *.rtf *.doc)");
488 std::string x= s.toStdString();
489 std::vector<QString> Qstr;
490 readfile.open(x.c_str());
491 if (readfile.is_open())
493 while (!readfile.eof())
496 Qstr.push_back(QString(x.c_str()));
501 cout << "Unable to open file";
503 //Set the transformation matrix
509 matrix->Element[i][j]=Qstr.at(index).toDouble();
513 transform->SetMatrix(matrix);
514 // SetTransform(matrix);
515 double * orientations=new double[3];
516 double * translations=new double[3];
517 transform->GetOrientation(orientations);
518 transform->PostMultiply();
520 //Obtain the Rotation Center
521 Xval->setText(Qstr.at(2));
522 Yval->setText(Qstr.at(3));
523 Zval->setText(Qstr.at(4));
525 //In the Order or Y X Z //
526 //now postmultiply for the rotations
527 SetTransform(0,0,0,0,0,-orientations[2],false);
528 SetTransform(0,0,0,-orientations[0],0,0,false);
529 SetTransform(0,0,0,0,-orientations[1],0,false);
531 transform->GetPosition(translations);
532 transform->Identity();
534 /* DD(translations[0]/mInput1->GetImage()->GetSpacing()[0]);
535 DD(translations[1]/mInput1->GetImage()->GetSpacing()[1]);
536 DD(translations[2]/mInput1->GetImage()->GetSpacing()[2]);
539 DD(orientations[2]);*/
540 //set the sliders and spin box values
541 xtrans_slider->setValue(rint(translations[0]/mInput1->GetImage()->GetSpacing()[0]));
543 ytrans_slider->setValue(rint(translations[1]/mInput1->GetImage()->GetSpacing()[1]));
545 ztrans_slider->setValue(rint(translations[2]/mInput1->GetImage()->GetSpacing()[2]));
547 yrot_slider->setValue(rint(orientations[1]));
549 xrot_slider->setValue(rint(orientations[0]));
551 zrot_slider->setValue(rint(orientations[2]));
554 //------------------------------------------------------------------------------
556 //------------------------------------------------------------------------------
557 void vvToolRigidReg::ResetTransform()
559 vtkMatrix4x4 *matrix = vtkMatrix4x4::New();
567 SetTransform(matrix);
568 UpdateTextEditor(matrix);
570 //------------------------------------------------------------------------------
572 //------------------------------------------------------------------------------
573 void vvToolRigidReg::SetRotationCenter()
575 //default image rotation center is the center of the image
576 QString xcord,ycord,zcord;
577 std::vector<double> imageorigin;
578 imageorigin=mInput1->GetImage()->GetOrigin();
580 xcord=xcord.setNum(imageorigin[0]+mImageSize[0]*mInput1->GetImage()->GetSpacing()[0]/2, 'g', 3);
581 ycord=ycord.setNum(imageorigin[1]+mImageSize[1]*mInput1->GetImage()->GetSpacing()[1]/2, 'g', 3);
582 zcord=zcord.setNum(imageorigin[2]+mImageSize[2]*mInput1->GetImage()->GetSpacing()[2]/2, 'g', 3);
584 Xval->setText(xcord);
585 Yval->setText(ycord);
586 Zval->setText(zcord);
589 //------------------------------------------------------------------------------
591 //------------------------------------------------------------------------------
592 void vvToolRigidReg::InitializeSliders()
594 xtrans_slider->signalsBlocked();
595 ytrans_slider->signalsBlocked();
596 ztrans_slider->signalsBlocked();
597 xtrans_sb->signalsBlocked();
598 ytrans_sb->signalsBlocked();
599 ztrans_sb->signalsBlocked();
600 xrot_slider->signalsBlocked();
601 yrot_slider->signalsBlocked();
602 zrot_slider->signalsBlocked();
603 xrot_sb->signalsBlocked();
604 yrot_sb->signalsBlocked();
605 zrot_sb->signalsBlocked();
607 xtrans_sb->setValue(0);
608 ytrans_sb->setValue(0);
609 ztrans_sb->setValue(0);
611 xtrans_slider->setValue(0);
612 ytrans_slider->setValue(0);
613 ztrans_slider->setValue(0);
615 xrot_sb->setValue(0);
616 yrot_sb->setValue(0);
617 zrot_sb->setValue(0);
619 xrot_slider->setValue(0);
620 yrot_slider->setValue(0);
621 zrot_slider->setValue(0);
623 //------------------------------------------------------------------------------
625 //------------------------------------------------------------------------------
626 void vvToolRigidReg::SetSliderRanges()
628 xtrans_slider->setMaximum(1000);
629 xtrans_slider->setMinimum(-1000);
630 ytrans_slider->setMaximum(1000);
631 ytrans_slider->setMinimum(-1000);
632 ztrans_slider->setMaximum(1000);
633 ztrans_slider->setMinimum(-1000);
635 xtrans_sb->setMaximum(1000);
636 xtrans_sb->setMinimum(-1000);
637 ytrans_sb->setMaximum(1000);
638 ytrans_sb->setMinimum(-1000);
639 ztrans_sb->setMaximum(1000);
640 ztrans_sb->setMinimum(-1000);
642 xrot_slider->setMaximum(360);
643 xrot_slider->setMinimum(-360);
644 yrot_slider->setMaximum(360);
645 yrot_slider->setMinimum(-360);
646 zrot_slider->setMaximum(360);
647 zrot_slider->setMinimum(-360);
649 xrot_sb->setMaximum(360);
650 xrot_sb->setMinimum(-360);
651 yrot_sb->setMaximum(360);
652 yrot_sb->setMinimum(-360);
653 zrot_sb->setMaximum(360);
654 zrot_sb->setMinimum(-360);