]> Creatis software - clitk.git/blobdiff - common/clitkDicomRT_ROI.cxx
Add New(), GetValueTag and Path (to search for image)
[clitk.git] / common / clitkDicomRT_ROI.cxx
index 88bab72d80ad025c8e2dba52fe2a97d8cad0bb3f..75fc8e97fdbc517bdc61da7dc5b60bd672d9ffe9 100644 (file)
@@ -20,6 +20,8 @@
 #include "clitkDicomRT_ROI.h"
 #include <vtkSmartPointer.h>
 #include <vtkAppendPolyData.h>
+#include <vtkImageClip.h>
+#include <vtkMarchingSquares.h>
 
 #if GDCM_MAJOR_VERSION == 2
 #include "gdcmAttribute.h"
@@ -31,11 +33,13 @@ clitk::DicomRT_ROI::DicomRT_ROI()
 {
   mName = "NoName";
   mNumber = -1;
+  mImage = NULL;
   mColor.resize(3);
   mColor[0] = mColor[1] = mColor[2] = 0;
   mMeshIsUpToDate = false;
   mBackgroundValue = 0;
   mForegroundValue = 1;
+  SetDicomUptodateFlag(false);
 }
 //--------------------------------------------------------------------
 
@@ -134,21 +138,38 @@ double clitk::DicomRT_ROI::GetForegroundValueLabelImage() const
 
 //--------------------------------------------------------------------
 #if GDCM_MAJOR_VERSION == 2
-void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::Item const & item)
+bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour)
 {
-  const gdcm::DataSet& nestedds = item.GetNestedDataSet();
-
+  // Keep dicom item
+  mItemInfo = itemInfo;
+  mItemContour = itemContour;
+  // DD(mItemInfo);
+  
+  // ROI number [Referenced ROI Number]
+  const gdcm::DataSet & nesteddsInfo = mItemInfo->GetNestedDataSet();
+  gdcm::Attribute<0x3006,0x0022> roinumber;
+  roinumber.SetFromDataSet( nesteddsInfo );
+  int nb1 = roinumber.GetValue();
+  
+  // Check this is the same with the other item
+  const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
   gdcm::Attribute<0x3006,0x0084> referencedroinumber;
   referencedroinumber.SetFromDataSet( nestedds );
-  // Change number if needed
-
-  // TODO
+  int nb2 = referencedroinumber.GetValue();
+  
+  // Must never be different
+  if (nb1 != nb2) {
+    DD(nb2);
+    DD(nb1);
+    FATAL("nb1 must equal nb2" << std::endl);
+  }
+  mNumber = nb1;
 
-  // ROI number [Referenced ROI Number]
-  mNumber = referencedroinumber.GetValue();
-
-  // Retrieve ROI Name
-  mName = rois[mNumber];
+  // Retrieve ROI Name (in the info item)
+  gdcm::Attribute<0x3006,0x26> roiname;
+  roiname.SetFromDataSet( nesteddsInfo );
+  mName = roiname.GetValue();
+  // DD(mName);
 
   // ROI Color [ROI Display Color]
   gdcm::Attribute<0x3006,0x002a> color = {};
@@ -163,10 +184,12 @@ void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::Item cons
   if( !nestedds.FindDataElement( tcsq ) )
     {
       std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
-      return;
+      SetDicomUptodateFlag(true);
+      return false;
     }
   const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
-  gdcm::SmartPointer<gdcm::SequenceOfItems> sqi2 = csq.GetValueAsSQ();
+  mContoursSequenceOfItems = csq.GetValueAsSQ();
+  gdcm::SmartPointer<gdcm::SequenceOfItems> & sqi2 = mContoursSequenceOfItems;
   if( !sqi2 || !sqi2->GetNumberOfItems() )
     {
     }
@@ -174,22 +197,19 @@ void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::Item cons
 
   for(unsigned int i = 0; i < nitems; ++i)
     {
-      const gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
+      gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
       DicomRT_Contour::Pointer c = DicomRT_Contour::New();
-      bool b = c->Read(j);
+      bool b = c->Read(&j);
       if (b) {
         mListOfContours.push_back(c);
       }
     }
+  SetDicomUptodateFlag(true);
+  return true;
 }
 #else
 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
 {
-
-  // Change number if needed
-
-  // TODO
-
   // ROI number [Referenced ROI Number]
   mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
 
@@ -215,6 +235,7 @@ void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem *
   else {
     std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
   }
+  SetDicomUptodateFlag(true);
 }
 #endif
 //--------------------------------------------------------------------
@@ -232,18 +253,23 @@ void clitk::DicomRT_ROI::SetImage(vvImage * image)
 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
 {
   if (!mMeshIsUpToDate) {
-    ComputeMesh();
+    ComputeMeshFromContour();
   }
   return mMesh;
 }
+//--------------------------------------------------------------------
+
+
 //--------------------------------------------------------------------
 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
 {
   return mListOfContours[n];
 }
+//--------------------------------------------------------------------
+
 
 //--------------------------------------------------------------------
-void clitk::DicomRT_ROI::ComputeMesh()
+void clitk::DicomRT_ROI::ComputeMeshFromContour()
 {
   vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
   for(unsigned int i=0; i<mListOfContours.size(); i++) {
@@ -258,6 +284,59 @@ void clitk::DicomRT_ROI::ComputeMesh()
 //--------------------------------------------------------------------
 
 
+#if GDCM_MAJOR_VERSION == 2
+
+//--------------------------------------------------------------------
+void clitk::DicomRT_ROI::UpdateDicomItem()
+{
+  if (GetDicomUptoDateFlag()) return;
+  DD("ROI::UpdateDicomItem");
+  DD(GetName());  
+
+  // From now, only some item can be modified
+
+  // Set ROI Name 0x3006,0x26> 
+  gdcm::Attribute<0x3006,0x26> roiname;
+  roiname.SetValue(GetName());
+  gdcm::DataElement de = roiname.GetAsDataElement();
+  gdcm::DataSet & ds = mItemInfo->GetNestedDataSet();  
+  ds.Replace(de);
+
+  // From MESH to CONTOURS
+  ComputeContoursFromImage();
+
+  // Update contours
+  DD(mListOfContours.size());
+  for(uint i=0; i<mListOfContours.size(); i++) {
+    DD(i);
+    DicomRT_Contour::Pointer contour = mListOfContours[i];
+    contour->UpdateDicomItem();//mItemContour);
+  }
+
+  // Nb of contours
+  unsigned int nitems = mContoursSequenceOfItems->GetNumberOfItems();
+  DD(nitems);
+
+  // Write [Contour Sequence] = 0x3006,0x0040)
+  gdcm::DataSet & dsc = mItemContour->GetNestedDataSet();
+  gdcm::Tag tcsq(0x3006,0x0040);
+  const gdcm::DataElement& csq = dsc.GetDataElement( tcsq );
+  gdcm::DataElement dec(csq);
+  dec.SetValue(*mContoursSequenceOfItems);
+  dsc.Replace(dec);
+
+  gdcm::DataSet & a = mContoursSequenceOfItems->GetItem(1).GetNestedDataSet();
+  gdcm::Attribute<0x3006,0x0050> at;
+  gdcm::Tag tcontourdata(0x3006,0x0050);
+  gdcm::DataElement contourdata = a.GetDataElement( tcontourdata );
+  at.SetFromDataElement( contourdata );
+  const double* points = at.GetValues();
+  DD(points[0]);
+
+}
+//--------------------------------------------------------------------
+#endif
+
 //--------------------------------------------------------------------
 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage * image, int n,
                                             std::string name,
@@ -290,3 +369,49 @@ vvImage * clitk::DicomRT_ROI::GetImage() const
   return mImage;
 }
 //--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+void clitk::DicomRT_ROI::ComputeContoursFromImage()
+{
+  DD("ComputeMeshFromImage");
+
+  // Check that an image is loaded
+  if (!mImage) return;
+
+  // Only consider 3D here
+  if (mImage->GetNumberOfDimensions() != 3) {
+    FATAL("DicomRT_ROI::ComputeMeshFromImage only work with 3D images");
+  }
+
+  // Get the VTK image
+  vtkImageData * image = mImage->GetVTKImages()[0];
+  
+  // Get initial extend for the clipping
+  vtkSmartPointer<vtkImageClip> clipper = vtkSmartPointer<vtkImageClip>::New();
+  clipper->SetInput(image);
+  int* extent = image->GetExtent();
+  DDV(extent, 6);
+  //  std::vector<int> extend;
+
+  // Prepare the marching squares
+  vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
+  squares->SetInput(clipper->GetOutput());
+
+  // Loop on slice
+  uint n = image->GetDimensions()[2];
+  DD(n);
+  for(uint i=0; i<n; i++) {
+    // Clip to the current slice
+    extent[4] = extent[5] = image->GetOrigin()[2]+i*image->GetSpacing()[2];
+    DDV(extent, 6);
+    clipper->SetOutputWholeExtent(extent[0],extent[1],extent[2],
+                                extent[3],extent[4],extent[5]);
+    
+
+    squares->Update();
+    DD(squares->GetNumberOfContours());
+    mListOfContours[i]->SetMesh(squares->GetOutput());
+  }
+}
+//--------------------------------------------------------------------