]> Creatis software - clitk.git/blobdiff - common/clitkDicomRTStruct2ImageFilter.cxx
Remove transparency for screenshot
[clitk.git] / common / clitkDicomRTStruct2ImageFilter.cxx
index eb54bdc0b2cd626f7fca1c7f0a879890187f5656..2e11fdc42a6ccd2a3243d8f6598b74c52cddd534 100644 (file)
@@ -31,6 +31,7 @@
 #include <vtkImageStencil.h>
 #include <vtkLinearExtrusionFilter.h>
 #include <vtkMetaImageWriter.h>
+#include <vtkXMLPolyDataWriter.h>
 
 
 //--------------------------------------------------------------------
@@ -38,6 +39,7 @@ clitk::DicomRTStruct2ImageFilter::DicomRTStruct2ImageFilter()
 {
   mROI = NULL;
   mWriteOutput = false;
+  mWriteMesh = false;
   mCropMask = true;
 }
 //--------------------------------------------------------------------
@@ -83,6 +85,14 @@ void clitk::DicomRTStruct2ImageFilter::SetCropMaskEnabled(bool b)
 //--------------------------------------------------------------------
 
 
+//--------------------------------------------------------------------
+void clitk::DicomRTStruct2ImageFilter::SetWriteMesh(bool b)
+{
+  mWriteMesh = b;
+}
+//--------------------------------------------------------------------
+
+
 //--------------------------------------------------------------------
 void clitk::DicomRTStruct2ImageFilter::SetOutputImageFilename(std::string s)
 {
@@ -102,10 +112,15 @@ void clitk::DicomRTStruct2ImageFilter::SetImage(vvImage::Pointer image)
   mSpacing.resize(3);
   mOrigin.resize(3);
   mSize.resize(3);
+  mDirection.resize(3);
+  mTransformMatrix = image->GetTransform()[0]->GetMatrix();
   for(unsigned int i=0; i<3; i++) {
     mSpacing[i] = image->GetSpacing()[i];
     mOrigin[i] = image->GetOrigin()[i];
     mSize[i] = image->GetSize()[i];
+    mDirection[i].resize(3);
+    for(unsigned int j=0; j<3; j++)
+      mDirection[i][j] = image->GetDirection()[i][j];
   }
 }
 //--------------------------------------------------------------------
@@ -125,10 +140,14 @@ void clitk::DicomRTStruct2ImageFilter::SetImageFilename(std::string f)
   mSpacing.resize(3);
   mOrigin.resize(3);
   mSize.resize(3);
+  mDirection.resize(3);
   for(unsigned int i=0; i<3; i++) {
     mSpacing[i] = header->GetSpacing(i);
     mOrigin[i] = header->GetOrigin(i);
     mSize[i] = header->GetDimensions(i);
+    mDirection[i].resize(3);
+    for(unsigned int j=0; j<3; j++)
+      mDirection[i][j] = header->GetDirection(i)[j];
   }
 }
 //--------------------------------------------------------------------
@@ -171,11 +190,55 @@ void clitk::DicomRTStruct2ImageFilter::Update()
   }
 
   // Get Mesh
-  vtkPolyData * mesh = mROI->GetMesh();  
+  vtkPolyData * mesh = mROI->GetMesh();
+  if (mWriteMesh) {
+    vtkSmartPointer<vtkXMLPolyDataWriter> meshWriter = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
+    std::string vtkName = mOutputFilename;
+    vtkName += ".vtk";
+    meshWriter->SetFileName(vtkName.c_str());
+#if VTK_MAJOR_VERSION <= 5
+    meshWriter->SetInput(mesh);
+#else
+    meshWriter->SetInputData(mesh);
+#endif
+    meshWriter->Write();
+  }
 
   // Get bounds
   double *bounds=mesh->GetBounds();
 
+  //Change mOrigin, mSize and mSpacing with respect to the directions
+  // Spacing is influenced by input direction
+  std::vector<double> tempSpacing;
+  tempSpacing.resize(3);
+  for(int i=0; i<3; i++) {
+    tempSpacing[i] = 0.0;
+    for(int j=0; j<3; j++) {
+      tempSpacing[i] += mDirection[i][j] * mSpacing[j];
+    }
+  }
+  for(int i=0; i<3; i++)
+    mSpacing[i] = tempSpacing[i];
+
+  // Size is influenced by affine transform matrix and input direction
+  // Size is converted to double, transformed and converted back to size type.
+  std::vector<double> tempSize;
+  tempSize.resize(3);
+  for(int i=0; i<3; i++) {
+    tempSize[i] = 0.0;
+    for(int j=0; j<3; j++) {
+      tempSize[i] += mDirection[i][j] * mSize[j];
+    }
+  }
+  for(int i=0; i<3; i++) {
+    if (tempSize[i] < 0.0) {
+      tempSize[i] *= -1;
+      mOrigin[i] += mSpacing[i]*(tempSize[i] -1);
+      mSpacing[i] *= -1;
+    }
+    mSize[i] = lrint(tempSize[i]);
+  }
+
   // Compute origin
   std::vector<double> origin;
   origin.resize(3);
@@ -189,7 +252,6 @@ void clitk::DicomRTStruct2ImageFilter::Update()
   extend[0] = ceil((bounds[1]-origin[0])/mSpacing[0]+4);
   extend[1] = ceil((bounds[3]-origin[1])/mSpacing[1]+4);
   extend[2] = ceil((bounds[5]-origin[2])/mSpacing[2]+4);
-
   // If no crop, set initial image size/origin
   if (!mCropMask) {
     for(int i=0; i<3; i++) {
@@ -202,18 +264,15 @@ void clitk::DicomRTStruct2ImageFilter::Update()
   mBinaryImage = vtkSmartPointer<vtkImageData>::New();
 #if VTK_MAJOR_VERSION <= 5
   mBinaryImage->SetScalarTypeToUnsignedChar();
+#endif
   mBinaryImage->SetOrigin(&origin[0]);
   mBinaryImage->SetSpacing(&mSpacing[0]);
   mBinaryImage->SetExtent(0, extend[0],
                           0, extend[1],
                           0, extend[2]);
+#if VTK_MAJOR_VERSION <= 5
   mBinaryImage->AllocateScalars();
 #else
-  mBinaryImage->SetOrigin(&origin[0]);
-  mBinaryImage->SetSpacing(&mSpacing[0]);
-  mBinaryImage->SetExtent(0, extend[0],
-                          0, extend[1],
-                          0, extend[2]);
   mBinaryImage->AllocateScalars(VTK_UNSIGNED_CHAR, 1);
 #endif
 
@@ -239,7 +298,7 @@ void clitk::DicomRTStruct2ImageFilter::Update()
 #if VTK_MAJOR_VERSION <= 5
   sts->SetInput(extrude->GetOutput());
 #else
-  sts->SetInputData(extrude->GetOutput());
+  sts->SetInputConnection(extrude->GetOutputPort(0));
 #endif
   //sts->SetInput(mesh);
 
@@ -247,7 +306,7 @@ void clitk::DicomRTStruct2ImageFilter::Update()
 #if VTK_MAJOR_VERSION <= 5
   stencil->SetStencil(sts->GetOutput());
 #else
-  stencil->SetStencilData(sts->GetOutput());
+  stencil->SetStencilConnection(sts->GetOutputPort(0));
 #endif
 #if VTK_MAJOR_VERSION <= 5
   stencil->SetInput(mBinaryImage);