]> Creatis software - clitk.git/blobdiff - common/clitkImage2DicomRTStructFilter.txx
Revert previsous commit with Roi name
[clitk.git] / common / clitkImage2DicomRTStructFilter.txx
index cc90104216d6ca2ef9d37f77e92156bf47c4fe9f..eba1251cfa0377927923016446135b22be4fda3f 100644 (file)
 #include "vtkCamera.h"
 #include "vtkProperty.h"
 #include "vtkProperty2D.h"
+#include <vtksys/SystemTools.hxx>
 
 // itk
 #include <itkImage.h>
 #include <itkVTKImageToImageFilter.h>
 
 // gdcm
+#include <vtkRTStructSetProperties.h>
+#include <vtkGDCMPolyDataReader.h>
 #include <vtkGDCMPolyDataWriter.h>
 
 //--------------------------------------------------------------------
 template<class PixelType>
 clitk::Image2DicomRTStructFilter<PixelType>::Image2DicomRTStructFilter()
 {
-  DD("Image2DicomRTStructFilter Const");
+  m_StructureSetFilename = "";
+  m_DicomFolder = "";
+  m_OutputFilename = "default-output.dcm";
+  m_ThresholdValue = 0.5;
+  m_SkipInitialStructuresFlag = false;
 }
 //--------------------------------------------------------------------
 
@@ -66,7 +73,16 @@ clitk::Image2DicomRTStructFilter<PixelType>::Image2DicomRTStructFilter()
 template<class PixelType>
 clitk::Image2DicomRTStructFilter<PixelType>::~Image2DicomRTStructFilter()
 {
-  DD("Image2DicomRTStructFilter Destructor");
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+template<class PixelType>
+void
+clitk::Image2DicomRTStructFilter<PixelType>::SetROIType(std::string type)
+{
+  m_ROIType = type;
 }
 //--------------------------------------------------------------------
 
@@ -75,26 +91,40 @@ clitk::Image2DicomRTStructFilter<PixelType>::~Image2DicomRTStructFilter()
 template<class PixelType>
 void clitk::Image2DicomRTStructFilter<PixelType>::Update() 
 {
-  DD("Image2DicomRTStructFilter::GenerateData");
+  // Check this is a RT-Struct
+  gdcm::Reader gdcm_reader;
+  gdcm_reader.SetFileName(m_StructureSetFilename.c_str());
+  if (!gdcm_reader.Read()) {
+    clitkExceptionMacro("Error could not open the file '" << m_StructureSetFilename << std::endl);
+  }
+  gdcm::MediaStorage ms;
+  ms.SetFromFile(gdcm_reader.GetFile());
+  if (ms != gdcm::MediaStorage::RTStructureSetStorage) {
+    clitkExceptionMacro("File '" << m_StructureSetFilename << "' is not a DICOM-RT-Struct file." << std::endl);
+  }
 
   // Read rt struct
   vtkSmartPointer<vtkGDCMPolyDataReader> reader = vtkGDCMPolyDataReader::New();
   reader->SetFileName(m_StructureSetFilename.c_str());
-  reader->Update();
-  DD("reader done");
+  reader->Update();  
 
   // Get properties
   vtkRTStructSetProperties * p = reader->GetRTStructSetProperties();
-  DD(p->GetNumberOfStructureSetROIs());
-  DD(p->GetStructureSetROIName(0));
-  DD(p->GetStructureSetROINumber(0));  
-  
+  if (GetVerboseFlag()) {
+    std::cout << "Number of structures in the dicom-rt-struct : " 
+              << p->GetNumberOfStructureSetROIs() << std::endl;
+  }
+
+  // number of additional contours
+  int m = m_InputFilenames.size();
+
   // Init writer
   vtkGDCMPolyDataWriter * writer = vtkGDCMPolyDataWriter::New();
-  int numMasks = reader->GetNumberOfOutputPorts() + 1;//add one more
-  DD(numMasks);
-  
-  // numMasks = 3; //FIXME temporary
+  int numMasks = reader->GetNumberOfOutputPorts() + m;
+
+  if (m_SkipInitialStructuresFlag) {
+    numMasks = m;
+  }
 
   writer->SetNumberOfInputPorts(numMasks);    
   writer->SetFileName(m_OutputFilename.c_str());
@@ -107,42 +137,75 @@ void clitk::Image2DicomRTStructFilter<PixelType>::Update()
   roiNames->SetNumberOfValues(numMasks);
   roiAlgorithms->SetNumberOfValues(numMasks);
   roiTypes->SetNumberOfValues(numMasks);
-  
-  typedef clitk::BinaryImageToMeshFilter<ImageType> BinaryImageToMeshFilterType;
-  typename BinaryImageToMeshFilterType::Pointer convert = BinaryImageToMeshFilterType::New();
-  convert->SetInput(m_Input);
-  DD("Update");
-  convert->Update();
-  DD("here");
-  DD("end update");
-  vtkPolyData* mesh = convert->GetOutputMesh();
-  DD(mesh->GetNumberOfVerts());
-  DD(mesh->GetNumberOfLines());
-  DD(mesh->GetNumberOfPolys());
-  DD(mesh->GetNumberOfStrips());
-
-  // Add on (FIXME) to replace with binary image
-  // vtkPolyData* blank = vtkPolyData::New();
-  // writer->SetInput(0, blank);
-  writer->SetInput(0, mesh);
-  roiNames->InsertValue(0, "blank");
-  roiAlgorithms->InsertValue(0, "blank");
-  roiTypes->InsertValue(0, "ORGAN");
-
-  for (unsigned int i = 1; i < numMasks; ++i) {
-    // DD(i);
-    writer->SetInput(i, reader->GetOutput(i-1));
-    std::string theString = reader->GetRTStructSetProperties()->GetStructureSetROIName(i-1);
+
+  // Convert the image into a mesh
+  std::vector<vtkSmartPointer<vtkPolyData> > meshes;
+  std::vector<std::string> m_ROINames;
+  meshes.resize(m);
+  m_ROINames.resize(m);
+  for(unsigned int i=0; i<m; i++) {
+
+    // read image
+    //    typedef float PixelType;
+    //typedef itk::Image<PixelType, 3> ImageType;
+    ImagePointer input = clitk::readImage<ImageType>(m_InputFilenames[i], false);
+
+    std::ostringstream oss;
+    oss << vtksys::SystemTools::
+      GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(m_InputFilenames[i]));
+    std::string name = oss.str();
+    m_ROINames[i] = name;
+
+    // convert to mesh
+    typedef clitk::BinaryImageToMeshFilter<ImageType> BinaryImageToMeshFilterType;
+    typename BinaryImageToMeshFilterType::Pointer convert = BinaryImageToMeshFilterType::New();
+    convert->SetThresholdValue(m_ThresholdValue);
+    convert->SetInput(input);
+    convert->Update();
+    meshes[i] = convert->GetOutputMesh();
+    if (GetVerboseFlag()) {
+      std::cout << "Mesh has " << meshes[i]->GetNumberOfLines() << " lines." << std::endl;
+    }
+
+    /*
+    // debug mesh write  FIXME
+    vtkSmartPointer<vtkPolyDataWriter> wr = vtkSmartPointer<vtkPolyDataWriter>::New();
+    wr->SetInputConnection(convert->GetOutputPort()); //psurface->GetOutputPort()
+    wr->SetFileName("bidon.obj");
+    wr->Update();
+    wr->Write();
+    */
+  }
+
+  // Copy previous contours
+  for (unsigned int i = 0; i < numMasks-m; ++i) {
+#if VTK_MAJOR_VERSION <= 5
+    writer->SetInput(i, reader->GetOutput(i));
+#else
+    writer->SetInputData(i, reader->GetOutput(i));
+#endif
+    std::string theString = reader->GetRTStructSetProperties()->GetStructureSetROIName(i);
     roiNames->InsertValue(i, theString);
-    theString = reader->GetRTStructSetProperties()->GetStructureSetROIGenerationAlgorithm(i-1);
+    theString = reader->GetRTStructSetProperties()->GetStructureSetROIGenerationAlgorithm(i);
     roiAlgorithms->InsertValue(i, theString);
-    theString = reader->GetRTStructSetProperties()->GetStructureSetRTROIInterpretedType(i-1);
+    theString = reader->GetRTStructSetProperties()->GetStructureSetRTROIInterpretedType(i);
     roiTypes->InsertValue(i, theString);
   }
-  
+
+  // Add new ones
+  for (unsigned int i = numMasks-m; i < numMasks; ++i) {
+#if VTK_MAJOR_VERSION <= 5
+    writer->SetInput(i, meshes[i-numMasks+m]);
+#else
+    writer->SetInputData(i, meshes[i-numMasks+m]);
+#endif
+    roiNames->InsertValue(i, m_ROIType);
+    roiAlgorithms->InsertValue(i, "CLITK_CREATED");
+    roiTypes->InsertValue(i, m_ROIType);
+  }
 
   /*
-  //  Visu
+  //  Visu DEBUG
   vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
   cubeMapper->SetInput( mesh );
   cubeMapper->SetScalarRange(0,7);
@@ -167,80 +230,25 @@ void clitk::Image2DicomRTStructFilter<PixelType>::Update()
   renWin->Render();
   iren->Start();
   */
-  
-
   // End visu
 
-
+  // Write (need to read dicom for slice id)
   vtkRTStructSetProperties* theProperties = vtkRTStructSetProperties::New();
-  DD(p->GetStudyInstanceUID());
   writer->SetRTStructSetProperties(theProperties);
-  /*writer->InitializeRTStructSet2(p,"./",
-                                 reader->GetRTStructSetProperties()->GetStructureSetLabel(),
-                                 reader->GetRTStructSetProperties()->GetStructureSetName(),
-                                 roiNames, roiAlgorithms, roiTypes);*/  
+  if (GetVerboseFlag()) {
+    std::cout << "Looking for dicom info, study instance "
+              << p->GetStudyInstanceUID() << std::endl;
+  }
   writer->InitializeRTStructSet(m_DicomFolder,
                                 reader->GetRTStructSetProperties()->GetStructureSetLabel(),
                                 reader->GetRTStructSetProperties()->GetStructureSetName(),
                                 roiNames, roiAlgorithms, roiTypes);
-  
-  DD("after init");
   writer->Write();
-  DD("write done");
-
   reader->Delete();
   roiNames->Delete();
   roiTypes->Delete();
-  //theProperties->Delete();
   roiAlgorithms->Delete();
-  //blank->Delete();
   writer->Delete();
-  
-  ////////////////////
-  
-
-  /*
-
-  // DicomRTStruct
-  DD(m_StructureSet->GetName());
-  clitk::DicomRT_ROI * roi = m_StructureSet->GetROIFromROINumber(1); // Aorta
-  DD(roi->GetName());
-  DD(roi->GetROINumber());
-
-
-  // Get 
-  
-
-
-
-  // Add an image to the roi
-  vvImage::Pointer im = vvImageFromITK<3, PixelType>(m_Input);
-  roi->SetImage(im);
-
-  // Get one contour
-  DD("Compute Mesh");
-  roi->ComputeContoursFromImage(); // FIXME do the set mesh for the moment (to change)
-  // roi->ComputeMeshFromContour();
-  vtkSmartPointer<vtkPolyData> mesh = roi->GetMesh();
-  DD("done");
-  
-  // Change the mesh (shift by 10);
-  // const vtkSmartPointer<vtkPoints> & points = mesh->GetPoints();
-  // for(uint i=0; i<mesh->GetNumberOfVerts (); i++) {
-  //   DD(i);
-  //   double * p = points->GetPoint(i);
-  //   p[0] += 30;
-  //   points->SetPoint(i, p);
-  // }
-  roi->SetName("TOTO");
-  roi->SetDicomUptodateFlag(true); // indicate that dicom info must be updated from the mesh.
-    
-  // Convert to dicom ?
-  DD("TODO");
-  
-  // Write
-  structset->Write("toto.dcm");
-  */
 }
 //--------------------------------------------------------------------