]> Creatis software - clitk.git/commitdiff
Added Loading initial transformation functionality
authorbharath <bharath>
Fri, 9 Jul 2010 18:23:10 +0000 (18:23 +0000)
committerbharath <bharath>
Fri, 9 Jul 2010 18:23:10 +0000 (18:23 +0000)
vv/vvToolRigidReg.cxx

index a307971d2de4d2d614cbbfd3cb3357c2ee14685f..b6525f72da5b4ad9d49d7dd8783e73ab2882bbaf 100644 (file)
 #include <vtkImageData.h>
 #include <vtkSmartPointer.h>
 #include <vtkTransform.h>
+#include <vvImageReader.h>
 #include "vvImage.h"
 #include "vvSlicer.h"
 #include <QFile>
+#include "clitkTransformUtilities.h"
 #include <QString>
 #include <QMessageBox>
 #include <vvMainWindow.h>
@@ -45,13 +47,17 @@ vvToolRigidReg::vvToolRigidReg(vvMainWindowBase * parent, Qt::WindowFlags f):
   // GUI Initialization
   Ui_vvToolRigidReg::setupUi(mToolWidget);
    QSize qsize;
-   qsize.setHeight(170);
-   qsize.setWidth(650);
-   textEdit->setFixedSize(qsize);
+   qsize.setHeight(470);
+   qsize.setWidth(850);
+   mToolWidget->setFixedSize(qsize);
+
+
+  // Set how many inputs are needed for this tool
+ // mFilter = new clitk::AffineTransformGenericFilter<args_info_clitkAffineTransform>;
 
   // Set how many inputs are needed for this tool
 AddInputSelector("Select the Reference Image");
 AddInputSelector("Select the Target Image");
AddInputSelector("Select moving image");
AddInputSelector("Select fixed image");
 }
 //------------------------------------------------------------------------------
 
@@ -63,8 +69,9 @@ vvToolRigidReg::~vvToolRigidReg()
 
 //------------------------------------------------------------------------------
 bool vvToolRigidReg::close()
-{ 
-  
+{
+  ResetTransform();
+  RemoveOverlay();
   return vvToolWidgetBase::close();
 }
 //------------------------------------------------------------------------------
@@ -72,8 +79,14 @@ bool vvToolRigidReg::close()
 //------------------------------------------------------------------------------
 void vvToolRigidReg::reject()
 {
-    ResetTransform();
-  return vvToolWidgetBase::reject();
+   return vvToolWidgetBase::reject();
+}
+//------------------------------------------------------------------------------
+
+//------------------------------------------------------------------------------
+void vvToolRigidReg::GetArgsInfoFromGUI()
+{
+  DD("works");
 }
 //------------------------------------------------------------------------------
 
@@ -83,79 +96,57 @@ void vvToolRigidReg::InputIsSelected(std::vector<vvSlicerManager *> & l)
   //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);
+
   if(mInput1->GetFileName()==mInput2->GetFileName())
-  {  
+  {
     QMessageBox::information(this, "Warning","Your Reference and Target Images are the same");
-  } 
+  }
   mTwoInputs = true;
   SetOverlay();
   mImageSize=mInput1->GetImage()->GetSize();
-  
   SetRotationCenter();
   SetSliderRanges();
-  
-    //connect all sigs to slots        
-  connect(resetbutton, SIGNAL(pressed()), this, SLOT(ResetTransform()));
-  
-  connect(Xval, SIGNAL(editingFinished()), this, SLOT(SetXvalue()));
-  connect(Yval, SIGNAL(editingFinished()), this, SLOT(SetYvalue()));
-  connect(Zval, SIGNAL(editingFinished()), this, SLOT(SetZvalue()));
-  connect(xtrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateXtranslider()));
-    
-  connect(ytrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateYtranslider()));
-  connect(ztrans_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateZtranslider()));
-  
-  
-  connect(xrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateXrotslider()));
-  connect(yrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateYrotslider()));
-  connect(zrot_slider, SIGNAL(valueChanged(int)), this, SLOT(UpdateZrotslider()));
-  
-       
-   connect(xtrans_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateXtransb()));
-   connect(ytrans_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateYtransb()));
-   connect(ztrans_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateZtransb()));
-   connect(xrot_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateXrotsb()));
-   connect(yrot_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateYrotsb()));
-   connect(zrot_sb, SIGNAL(valueChanged(int)), this, SLOT(UpdateZrotsb()));
-   
-  
-  connect(loadbutton, SIGNAL(pressed()), this, SLOT(ReadFile()));
-  connect(savebutton, SIGNAL(pressed()), this, SLOT(SaveFile()));
+
+    //connect all sigs to slots
+   connect(resetbutton, SIGNAL(pressed()), this, SLOT(ResetTransform()));
+   connect(tab2loadbutton, 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(ReadFile()));
+   connect(savebutton, SIGNAL(pressed()), this, SLOT(SaveFile()));
+
 }
 //------------------------------------------------------------------------------
 
 //------------------------------------------------------------------------------
 void vvToolRigidReg::apply()
 {
- QMessageBox mb( "Keep Overlay",
-                    "Do you want to keep the overlay on?\n"
-                    "You can switch it off anytime by pressing 'x'",
-                    QMessageBox::Information,
-                    QMessageBox::Yes | QMessageBox::Default,
-                    QMessageBox::No,
-                    QMessageBox::Cancel | QMessageBox::Escape );
-    mb.setButtonText( QMessageBox::Yes, "Yes" );
-    mb.setButtonText( QMessageBox::No, "No" );
-    switch( mb.exec() ) {
-        case QMessageBox::Yes:
-          hide();
-         break;
-        case QMessageBox::No:
-         mInput2->Reload();
-        mInput2->Render();
-        mInput1->Reload(); 
-        mInput1->Render();
-        for(int i=0;i<mCurrentSlicerManager->NumberOfSlicers();i++)
-        {
-          mInput1->RemoveActor("overlay",0);
-          mInput1->SetColorMap(0);
-          mInput1->Render();
-          hide();
-        }
-        break;
-        case QMessageBox::Cancel:
-            break;
-    }
+  RemoveOverlay();
 }
 //------------------------------------------------------------------------------
 
@@ -164,13 +155,26 @@ void vvToolRigidReg::SetOverlay()
 {
     for (int i =0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
    mCurrentSlicerManager->GetSlicer(i)->SetOverlay(mInput2->GetImage());
-   mCurrentSlicerManager->GetSlicer(i)->SetActorVisibility("overlay",0,true);  
+   mCurrentSlicerManager->GetSlicer(i)->SetActorVisibility("overlay",0,true);
    mCurrentSlicerManager->SetColorMap();
    mCurrentSlicerManager->Render();
    }
   }
 //------------------------------------------------------------------------------
 
+//------------------------------------------------------------------------------
+void vvToolRigidReg::RemoveOverlay()
+{
+   for(int i=0;i<mCurrentSlicerManager->NumberOfSlicers();i++)
+        {
+          mInput1->RemoveActor("overlay",0);
+          mInput1->SetColorMap(0);
+          mInput1->Render();
+          hide();
+        }
+}
+//------------------------------------------------------------------------------
+
 //------------------------------------------------------------------------------
 void vvToolRigidReg::SetXvalue()
 {
@@ -195,8 +199,7 @@ void vvToolRigidReg::SetZvalue()
 //------------------------------------------------------------------------------
 void vvToolRigidReg::SetTransform(double tX, double tY, double tZ, double aX, double aY, double aZ,bool update)
 {
-   
-  vtkSmartPointer<vtkTransform> transform = mCurrentSlicerManager->GetImage()->GetTransform();
+  vtkSmartPointer<vtkTransform> transform = mInput1->GetImage()->GetTransform();
   transform->PostMultiply();
   //Rotations
   if (aX!=0 || aY!=0 || aZ!=0) {
@@ -213,16 +216,6 @@ void vvToolRigidReg::SetTransform(double tX, double tY, double tZ, double aX, do
   //Translations
   if (tX!=0||tY!=0||tZ!=0)
     transform->Translate(tX*mInput1->GetImage()->GetSpacing()[0],tY*mInput1->GetImage()->GetSpacing()[1],tZ*mInput1->GetImage()->GetSpacing()[2]);
-
-    transform->Update();
-  if(update)
-  {
-    for (int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
-      mCurrentSlicerManager->GetImage()->UpdateReslice();
-      mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent();
-      mCurrentSlicerManager->GetSlicer(i)->Render();
-    }
-  }
 }
 //------------------------------------------------------------------------------
 
@@ -231,13 +224,14 @@ void vvToolRigidReg::SetTransform(vtkMatrix4x4 *matrix)
 {
     vtkSmartPointer<vtkTransform> transform =mCurrentSlicerManager->GetImage()->GetTransform();
     transform->SetMatrix(matrix);
-    transform->PostMultiply();
-    transform->Concatenate(matrix);
-    transform->Update();
-//    mCurrentSlicerManager->GetImage()->SetTransform(transform);
-   
+    Render();
+}
+//------------------------------------------------------------------------------
+
+//------------------------------------------------------------------------------
+void vvToolRigidReg::Render()
+{
     for (int i=0; i<mCurrentSlicerManager->NumberOfSlicers(); i++) {
-      mCurrentSlicerManager->GetImage()->UpdateReslice();
       mCurrentSlicerManager->GetSlicer(i)->ForceUpdateDisplayExtent();
       mCurrentSlicerManager->GetSlicer(i)->Render();
       }
@@ -245,17 +239,20 @@ void vvToolRigidReg::SetTransform(vtkMatrix4x4 *matrix)
 //------------------------------------------------------------------------------
 
 //------------------------------------------------------------------------------
-  void vvToolRigidReg::UpdateTextEditor(vtkMatrix4x4 *matrix)
+  void vvToolRigidReg::UpdateTextEditor(vtkMatrix4x4 *matrix,QTextEdit* textEdit)
 {
-   QString str1,str2,str3;
+    QFont font=QFont("Times New Roman",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");
+    str2.append("#Rotation Center(mm): \n#");
     textEdit->setText(str2);
-    
+
     str2=textEdit->toPlainText();
     textEdit->setTextColor(QColor(255,0,0));
     str2.append(str3.append(Xval->text()));
@@ -270,11 +267,11 @@ void vvToolRigidReg::SetTransform(vtkMatrix4x4 *matrix)
     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()));
@@ -284,272 +281,200 @@ void vvToolRigidReg::SetTransform(vtkMatrix4x4 *matrix)
     str2=textEdit->toPlainText();
     str2.append("\n");
     textEdit->setText(str2);
-    
+
 
     str2=textEdit->toPlainText();
-    str2.append("Transformation Matrix(mm):\n");
+    str2.append("#Transformation Matrix(mm):\n");
     textEdit->setText(str2);
-
     for(int i=0;i<4;i++)
     {
     for(int j=0;j<4;j++)
       {
     str2=textEdit->toPlainText();
    // str2.append("\t"+str1.setNum(matrix->Element[i][j]));
-    str2.append(QString("%1\t").arg(str1.setNum(matrix->Element[i][j]),20));  
+    str2.append(QString("%1\t").arg(str1.setNum(matrix->Element[i][j]),2));
     textEdit->setText(str2);
       }
     str2=textEdit->toPlainText();
-    str2.append("\n");  
+    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);
 }
 //------------------------------------------------------------------------------
 
 //------------------------------------------------------------------------------
-void vvToolRigidReg::UpdateXtransb()
-{
-  int diff=0;
-  diff=xtrans_sb->value()-xtrans_slider->value();
-  this->SetTransform(diff, 0, 0, 0, 0, 0,true);
-  xtrans_slider->setValue(xtrans_sb->value());
-   UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
-    
-}
-//------------------------------------------------------------------------------
-
-//------------------------------------------------------------------------------
-void vvToolRigidReg::UpdateXtranslider()
-{
-  int diff=0;
-  diff=xtrans_slider->value()-xtrans_sb->value();
-  this->SetTransform(diff, 0, 0, 0, 0, 0,true);
-  xtrans_sb->setValue(xtrans_slider->value());
-  UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
-}
-//------------------------------------------------------------------------------
-
-//------------------------------------------------------------------------------
-void vvToolRigidReg::UpdateYtransb()
-{
-
-  int diff=0;
-  diff=ytrans_sb->value()-ytrans_slider->value();
-  this->SetTransform(0, diff, 0, 0, 0, 0,true);
-  ytrans_slider->setValue(ytrans_sb->value());
- UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
-}
-
-//------------------------------------------------------------------------------
-
-//------------------------------------------------------------------------------
-void vvToolRigidReg::UpdateYtranslider()
-{
-  int diff=0;
-  diff=ytrans_slider->value()-ytrans_sb->value();
-  this->SetTransform(0, diff, 0, 0, 0, 0,true);
-  ytrans_sb->setValue(ytrans_slider->value());
-  UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
-}
-//------------------------------------------------------------------------------
-
-//------------------------------------------------------------------------------
-void vvToolRigidReg::UpdateZtransb()
+void vvToolRigidReg::UpdateTransform_sliders()
 {
-
-  int diff=0;
-  diff=ztrans_sb->value()-ztrans_slider->value();
-  this->SetTransform(0, 0, diff, 0, 0, 0,true);
-  ztrans_slider->setValue(ztrans_sb->value());
-   UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
+       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::UpdateZtranslider()
+void vvToolRigidReg::UpdateTransform_sb()
 {
-  int diff=0;
-  diff=ztrans_slider->value()-ztrans_sb->value();
-  this->SetTransform(0, 0, diff, 0, 0, 0,true);
-  ztrans_sb->setValue(ztrans_slider->value());
-  UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
+   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::UpdateXrotsb()
+void vvToolRigidReg::AutoRegister()
 {
+ GetArgsInfoFromGUI();
+    //clitk::AffineRegistrationGenericFilter::Pointer filter =
+   // clitk::AffineRegistrationGenericFilter::New();
+    //filter->SetArgsInfo(mArgsInfo);
+   // filter->Update();
 
-  int diff=0;
-  diff=xrot_sb->value()-xrot_slider->value();
-  this->SetTransform(0, 0, 0, diff, 0, 0,true);
-  xrot_slider->setValue(xrot_sb->value());
-  UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
-}
-
-//------------------------------------------------------------------------------
-
-//------------------------------------------------------------------------------
-void vvToolRigidReg::UpdateXrotslider()
-{
-  int diff=0;
-  diff=xrot_slider->value()-xrot_sb->value();
-  this->SetTransform(0, 0, 0, diff, 0, 0,true);
-  xrot_sb->setValue(xrot_slider->value());
-  UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
 }
 //------------------------------------------------------------------------------
 
 //------------------------------------------------------------------------------
-void vvToolRigidReg::UpdateYrotsb()
+void vvToolRigidReg::UpdateTransform(bool slider_enabled)
 {
-
-  int diff=0;
-  diff=yrot_sb->value()-yrot_slider->value();
-  this->SetTransform(0, 0, 0, 0, diff, 0,true);
-  yrot_slider->setValue(yrot_sb->value());
-  UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
-}
-
-//------------------------------------------------------------------------------
-
-//------------------------------------------------------------------------------
-void vvToolRigidReg::UpdateYrotslider()
-{
-  int diff=0;
-  diff=yrot_slider->value()-yrot_sb->value();
-  this->SetTransform(0, 0, 0, 0, diff, 0,true);
-  yrot_sb->setValue(yrot_slider->value());
-  UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
-}
-//------------------------------------------------------------------------------
-
-//------------------------------------------------------------------------------
-void vvToolRigidReg::UpdateZrotsb()
-{
-
-  int diff=0;
-  diff=zrot_sb->value()-zrot_slider->value();
-  this->SetTransform(0, 0, 0, 0, 0,diff,true);
-  zrot_slider->setValue(zrot_sb->value());
-  UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
-}
-
-//------------------------------------------------------------------------------
-
-//------------------------------------------------------------------------------
-void vvToolRigidReg::UpdateZrotslider()
-{
-  int diff=0;
-  diff=zrot_slider->value()-zrot_sb->value();
-  this->SetTransform(0, 0, 0, 0, 0, diff,true);
-  zrot_sb->setValue(zrot_slider->value());
-  UpdateTextEditor(mCurrentSlicerManager->GetImage()->GetTransform()->GetMatrix());
+     vtkSmartPointer<vtkTransform> transform_final=vtkSmartPointer<vtkTransform>::New();
+     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());
+    }
+    SetTransform(transform_final->GetMatrix());
+    UpdateTextEditor(transform_final->GetMatrix(),textEdit);
 }
 //------------------------------------------------------------------------------
 
 //------------------------------------------------------------------------------
 void vvToolRigidReg::SaveFile()
-{      
-   QString s = QFileDialog::getSaveFileName(this, tr("Save File"),
-                            "/home",
-                            tr("Text (*.txt *.doc *.rtf)"));
-    QFile file(s);
-    if (file.open(QFile::WriteOnly | QFile::Truncate)) {
-    QTextStream out(&file);
-    out<<textEdit->toPlainText() ;
+{
+  //Write the Transformation Matrix
+    QString f1 = QFileDialog::getSaveFileName(this, tr("Save Transformation Matrix File"),
+                           "/home",
+                            tr("Text (*.mat *.txt *.doc *.rtf)"));
+    QFile file1(f1);
+    std::vector<QString> 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<<transparameters[i*4+j]+"\t";
+      }
+      out1<<"\n";
+     }
     }
+     else
+     {
+      QMessageBox::information(this,"Warning","Error Reading Parameters");
+     }
 }
 //------------------------------------------------------------------------------
 
 //------------------------------------------------------------------------------
 void vvToolRigidReg::ReadFile()
 {
-  vtkMatrix4x4 *matrix=vtkMatrix4x4::New();
+   std::string x;
+   QString center;
+   double * orientations=new double[3];
+   double * translations=new double[3];
+   vtkMatrix4x4 *matrix=vtkMatrix4x4::New();
    vtkSmartPointer<vtkTransform> transform = mCurrentSlicerManager->GetImage()->GetTransform();
-   QString s = QFileDialog::getOpenFileName(
+
+   //Open File to read the transformation parameters
+   QString file1 = QFileDialog::getOpenFileName(
                     this,
-                    "Choose a file",
+                   "Choose the Transformation Parameters file",
                     "/home",
-                    "Text (*.txt *.rtf *.doc)");
-    if (s.isEmpty())
+                    "Text (*.mat *.txt *.rtf *.doc)");
+    if (file1.isEmpty())
     return;
-    
-   QFile file(s);
-   ifstream readfile;
-   std::string x= s.toStdString();
-   std::vector<QString> Qstr;
-   readfile.open(x.c_str());
-   if (readfile.is_open()) 
-   {
-    while (!readfile.eof())
-      {
-      readfile >> x;
-      Qstr.push_back(QString(x.c_str()));
-      }
-      readfile.close();
-   }
-    else {
-      cout << "Unable to open file";
-    }
-    //Set the transformation matrix
-    int index =7;
-   for(int i=0;i<4;i++)
+   QFile Qfile1(file1);
+  // ifstream readfile;
+   std::string transfile= file1.toStdString();
+   std::string filename1(transfile);
+   std::ifstream f1(filename1.c_str());
+   if(f1.is_open())
    {
-     for(int j=0;j<4;j++)
-     {
-       matrix->Element[i][j]=Qstr.at(index).toDouble();
-       index++;
-     }
+   f1.close();
+   itk::Matrix<double, 4, 4> itkMat = clitk::ReadMatrix3D(transfile);
+   for(int j=0; j<4; j++)
+      for(int i=0; i<4; i++)
+    matrix->SetElement(i,j,itkMat[i][j]);
    }
-   transform->SetMatrix(matrix);
-   // SetTransform(matrix);
-    double * orientations=new double[3];
-    double * translations=new double[3];
+    UpdateTextEditor(matrix,textEdit);
+    transform->SetMatrix(matrix);
     transform->GetOrientation(orientations);
     transform->PostMultiply();
-    
-       //Obtain the Rotation Center 
-    Xval->setText(Qstr.at(2));
-    Yval->setText(Qstr.at(3));
-    Zval->setText(Qstr.at(4));
+
+     //Obtain the Rotation Center , set it to origin
+    Xval->setText(center.setNum(0));
+    Yval->setText(center.setNum(0));
+    Zval->setText(center.setNum(0));
+
     //In the Order or Y X Z //
-    //now  postmultiply for the rotations 
-     SetTransform(0,0,0,0,0,-orientations[2],false);
-     SetTransform(0,0,0,-orientations[0],0,0,false);
-     SetTransform(0,0,0,0,-orientations[1],0,false);
-      
+    //now  postmultiply for the rotations
+    SetTransform(0,0,0,0,0,-round(orientations[2]),false);
+    SetTransform(0,0,0,-round(orientations[0]),0,0,false);
+    SetTransform(0,0,0,0,-round(orientations[1]),0,false);
+
     transform->GetPosition(translations);
-    transform->Identity();     
-  /*  DD(translations[0]/mInput1->GetImage()->GetSpacing()[0]);
+    transform->Identity();
+
+    DD(translations[0]/mInput1->GetImage()->GetSpacing()[0]);
     DD(translations[1]/mInput1->GetImage()->GetSpacing()[1]);
     DD(translations[2]/mInput1->GetImage()->GetSpacing()[2]);
     DD(orientations[0]);
     DD(orientations[1]);
-    DD(orientations[2]);*/
-  //set the sliders  and spin box values 
-    xtrans_slider->setValue(rint(translations[0]/mInput1->GetImage()->GetSpacing()[0]));
-    UpdateXtranslider();
-    ytrans_slider->setValue(rint(translations[1]/mInput1->GetImage()->GetSpacing()[1]));
-    UpdateYtranslider();       
-    ztrans_slider->setValue(rint(translations[2]/mInput1->GetImage()->GetSpacing()[2]));
-    UpdateZtranslider();
-    yrot_slider->setValue(rint(orientations[1]));
-    UpdateYrotslider();
-    xrot_slider->setValue(rint(orientations[0]));
-    UpdateXrotslider();        
-    zrot_slider->setValue(rint(orientations[2]));
-    UpdateZrotslider();
+    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);
 }
 //------------------------------------------------------------------------------
 
@@ -557,15 +482,13 @@ void vvToolRigidReg::ReadFile()
 void vvToolRigidReg::ResetTransform()
 {
   vtkMatrix4x4 *matrix = vtkMatrix4x4::New();
-   matrix->Identity();
-   mInput1->Reload();
-   mInput1->Render();
-   mInput2->Reload();
-   mInput2->Render(); 
+  for(int i=0;i<4;i++)
+    for(int j=0;j<4;j++)
+          matrix->SetElement(i,j,mInitialMatrix[i*4+j]);
+   SetTransform(matrix);
    SetRotationCenter();
    SetSliderRanges();
-   SetTransform(matrix);
-   UpdateTextEditor(matrix);
+   UpdateTextEditor(matrix,textEdit);
 }
 //------------------------------------------------------------------------------
 
@@ -576,7 +499,7 @@ void vvToolRigidReg::SetRotationCenter()
     QString xcord,ycord,zcord;
     std::vector<double> imageorigin;
     imageorigin=mInput1->GetImage()->GetOrigin();
-    
+
     xcord=xcord.setNum(imageorigin[0]+mImageSize[0]*mInput1->GetImage()->GetSpacing()[0]/2, 'g', 3);
     ycord=ycord.setNum(imageorigin[1]+mImageSize[1]*mInput1->GetImage()->GetSpacing()[1]/2, 'g', 3);
     zcord=zcord.setNum(imageorigin[2]+mImageSize[2]*mInput1->GetImage()->GetSpacing()[2]/2, 'g', 3);
@@ -584,72 +507,112 @@ void vvToolRigidReg::SetRotationCenter()
     Xval->setText(xcord);
     Yval->setText(ycord);
     Zval->setText(zcord);
-    InitializeSliders();
+    InitializeSliders(0,0,0,0,0,0,true);
 }
 //------------------------------------------------------------------------------
 
 //------------------------------------------------------------------------------
-void vvToolRigidReg::InitializeSliders()
+void vvToolRigidReg::InitializeSliders(double xtrans,double ytrans, double ztrans, double xrot, double yrot, double zrot,bool sliders)
 {
-     xtrans_slider->signalsBlocked();
-     ytrans_slider->signalsBlocked();
-     ztrans_slider->signalsBlocked();
-     xtrans_sb->signalsBlocked();
-     ytrans_sb->signalsBlocked();
-     ztrans_sb->signalsBlocked();
-     xrot_slider->signalsBlocked();
-     yrot_slider->signalsBlocked();
-     zrot_slider->signalsBlocked();
-     xrot_sb->signalsBlocked();
-     yrot_sb->signalsBlocked();
-     zrot_sb->signalsBlocked();
-     
-    xtrans_sb->setValue(0);
-    ytrans_sb->setValue(0);
-    ztrans_sb->setValue(0);
-    
-    xtrans_slider->setValue(0);
-    ytrans_slider->setValue(0);
-    ztrans_slider->setValue(0);
-    
-    xrot_sb->setValue(0);
-    yrot_sb->setValue(0);
-    zrot_sb->setValue(0);
-
-    xrot_slider->setValue(0);
-    yrot_slider->setValue(0);
-    zrot_slider->setValue(0);
+
+    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);
+    DD(xtrans_slider->value());
+    xtrans_slider->setValue(rint(xtrans));
+    xtrans_slider->blockSignals(false);
+    ytrans_slider->blockSignals(true);
+    ytrans_slider->setValue(rint(ytrans));
+    ytrans_slider->blockSignals(false);
+    ztrans_slider->blockSignals(true);
+    ztrans_slider->setValue(rint(ztrans));
+    ztrans_slider->blockSignals(false);
+    }
+    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->setMaximum(1000);
-  xtrans_slider->setMinimum(-1000);
-  ytrans_slider->setMaximum(1000);
-  ytrans_slider->setMinimum(-1000);
-  ztrans_slider->setMaximum(1000);
-  ztrans_slider->setMinimum(-1000);
-
-  xtrans_sb->setMaximum(1000);
-  xtrans_sb->setMinimum(-1000);
-  ytrans_sb->setMaximum(1000);
-  ytrans_sb->setMinimum(-1000);
-  ztrans_sb->setMaximum(1000);
-  ztrans_sb->setMinimum(-1000);
-
-  xrot_slider->setMaximum(360);
-  xrot_slider->setMinimum(-360);
-  yrot_slider->setMaximum(360);
-  yrot_slider->setMinimum(-360);
-  zrot_slider->setMaximum(360);
-  zrot_slider->setMinimum(-360);
-
-  xrot_sb->setMaximum(360);
-  xrot_sb->setMinimum(-360);
-  yrot_sb->setMaximum(360);
-  yrot_sb->setMinimum(-360);
-  zrot_sb->setMaximum(360);
-  zrot_sb->setMinimum(-360);
+  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);
 }