]> Creatis software - clitk.git/blob - common/clitkDicomRT_ROI.cxx
Debug RTStruct conversion with empty struc
[clitk.git] / common / clitkDicomRT_ROI.cxx
1 /*=========================================================================
2   Program:         vv http://www.creatis.insa-lyon.fr/rio/vv
3   Main authors :   XX XX XX
4
5   Authors belongs to:
6   - University of LYON           http://www.universite-lyon.fr/
7   - Léon Bérard cancer center    http://www.centreleonberard.fr
8   - CREATIS CNRS laboratory      http://www.creatis.insa-lyon.fr
9
10   This software is distributed WITHOUT ANY WARRANTY; without even
11   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12   PURPOSE.  See the copyright notices for more information.
13
14   It is distributed under dual licence
15   - BSD       http://www.opensource.org/licenses/bsd-license.php
16   - CeCILL-B  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17
18   =========================================================================*/
19
20 #include "clitkDicomRT_ROI.h"
21 #include <vtkSmartPointer.h>
22 #include <vtkAppendPolyData.h>
23 #include <vtkImageClip.h>
24 #include <vtkMarchingSquares.h>
25 #include <vtkPolyDataWriter.h>
26 #include <vtkVersion.h>
27
28 #if GDCM_MAJOR_VERSION >= 2
29 #include "gdcmAttribute.h"
30 #include "gdcmItem.h"
31 #endif
32
33 //--------------------------------------------------------------------
34 clitk::DicomRT_ROI::DicomRT_ROI()
35 {
36   mName = "NoName";
37   mNumber = -1;
38   mImage = NULL;
39   mColor.resize(3);
40   mColor[0] = mColor[1] = mColor[2] = 0;
41   mMeshIsUpToDate = false;
42   mBackgroundValue = 0;
43   mForegroundValue = 1;
44   SetDicomUptodateFlag(false);
45   mFilename = "";
46 }
47 //--------------------------------------------------------------------
48
49
50 //--------------------------------------------------------------------
51 clitk::DicomRT_ROI::~DicomRT_ROI()
52 {
53 }
54 //--------------------------------------------------------------------
55
56
57 //--------------------------------------------------------------------
58 void clitk::DicomRT_ROI::SetDisplayColor(double r, double v, double b)
59 {
60   mColor.resize(3);
61   mColor[0] = r;
62   mColor[1] = v;
63   mColor[2] = b;
64 }
65 //--------------------------------------------------------------------
66
67
68 //--------------------------------------------------------------------
69 int clitk::DicomRT_ROI::GetROINumber() const
70 {
71   return mNumber;
72 }
73 //--------------------------------------------------------------------
74
75
76 //--------------------------------------------------------------------
77 void clitk::DicomRT_ROI::SetROINumber(int number)
78 {
79   mNumber = number;
80 }
81 //--------------------------------------------------------------------
82
83
84 //--------------------------------------------------------------------
85 const std::string & clitk::DicomRT_ROI::GetName() const
86 {
87   return mName;
88 }
89 //--------------------------------------------------------------------
90
91
92 //--------------------------------------------------------------------
93 const std::string & clitk::DicomRT_ROI::GetFilename() const
94 {
95   return mFilename;
96 }
97 //--------------------------------------------------------------------
98
99
100 //--------------------------------------------------------------------
101 const std::vector<double> & clitk::DicomRT_ROI::GetDisplayColor() const
102 {
103   return mColor;
104 }
105 //--------------------------------------------------------------------
106
107
108 //--------------------------------------------------------------------
109 void clitk::DicomRT_ROI::Print(std::ostream & os) const
110 {
111   os << "ROI " << mNumber << "\t" << mName
112      << "\t(" << mColor[0] << " " << mColor[1] << " " << mColor[2] << ")"
113      << "\t Contours = " << mListOfContours.size() << std::endl;
114 }
115 //--------------------------------------------------------------------
116
117
118 //--------------------------------------------------------------------
119 void clitk::DicomRT_ROI::SetBackgroundValueLabelImage(double bg)
120 {
121   mBackgroundValue = bg;
122 }
123 //--------------------------------------------------------------------
124
125
126 //--------------------------------------------------------------------
127 double clitk::DicomRT_ROI::GetBackgroundValueLabelImage() const
128 {
129   return mBackgroundValue;
130 }
131 //--------------------------------------------------------------------
132
133
134 //--------------------------------------------------------------------
135 void clitk::DicomRT_ROI::SetForegroundValueLabelImage(double bg)
136 {
137   mForegroundValue = bg;
138 }
139 //--------------------------------------------------------------------
140
141
142 //--------------------------------------------------------------------
143 double clitk::DicomRT_ROI::GetForegroundValueLabelImage() const
144 {
145   return mForegroundValue;
146 }
147 //--------------------------------------------------------------------
148
149
150 //--------------------------------------------------------------------
151 #if GDCM_MAJOR_VERSION >= 2
152 bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour)
153 {
154   //FATAL("Error : compile vv with itk4 + external gdcm");
155   // Keep dicom item
156   mItemInfo = itemInfo;
157   mItemContour = itemContour;
158   // DD(mItemInfo);
159   
160   // ROI number [Referenced ROI Number]
161   const gdcm::DataSet & nesteddsInfo = mItemInfo->GetNestedDataSet();
162   gdcm::Attribute<0x3006,0x0022> roinumber;
163   roinumber.SetFromDataSet( nesteddsInfo );
164   int nb1 = roinumber.GetValue();
165   
166   // Check this is the same with the other item
167   const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
168   gdcm::Attribute<0x3006,0x0084> referencedroinumber;
169   referencedroinumber.SetFromDataSet( nestedds );
170   int nb2 = referencedroinumber.GetValue();
171   
172   // Must never be different
173   if (nb1 != nb2) {
174     DD(nb2);
175     DD(nb1);
176     FATAL("nb1 must equal nb2" << std::endl);
177   }
178   mNumber = nb1;
179
180   // Retrieve ROI Name (in the info item)
181   gdcm::Attribute<0x3006,0x26> roiname;
182   roiname.SetFromDataSet( nesteddsInfo );
183   mName = roiname.GetValue();
184   // DD(mName);
185
186   // ROI Color [ROI Display Color]
187   gdcm::Attribute<0x3006,0x002a> color = {};
188   color.SetFromDataSet( nestedds );
189   assert( color.GetNumberOfValues() == 3 );
190   mColor[0] = color.GetValue(0);
191   mColor[1] = color.GetValue(1);
192   mColor[2] = color.GetValue(2);
193
194   // Read contours [Contour Sequence]
195   gdcm::Tag tcsq(0x3006,0x0040);
196   if( !nestedds.FindDataElement( tcsq ) )
197     {
198       std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
199       SetDicomUptodateFlag(true);
200       return false;
201     }
202   const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
203   mContoursSequenceOfItems = csq.GetValueAsSQ();
204   gdcm::SmartPointer<gdcm::SequenceOfItems> & sqi2 = mContoursSequenceOfItems;
205   if( !sqi2 || !sqi2->GetNumberOfItems() )
206     {
207     return false;
208     }
209   unsigned int nitems = sqi2->GetNumberOfItems();
210
211   for(unsigned int i = 0; i < nitems; ++i)
212     {
213       gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
214       DicomRT_Contour::Pointer c = DicomRT_Contour::New();
215       c->SetTransformMatrix(mTransformMatrix);
216       bool b = c->Read(&j);
217       if (b) {
218         mListOfContours.push_back(c);
219       }
220     }
221   SetDicomUptodateFlag(true);
222   return true;
223 }
224 #else
225 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
226 {
227   // ROI number [Referenced ROI Number]
228   mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
229
230   // Retrieve ROI Name
231   mName = rois[mNumber];
232
233   // ROI Color [ROI Display Color]
234   mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
235
236   // Read contours [Contour Sequence]
237   gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
238   if (contours) {
239     int i=0;
240     for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
241       DicomRT_Contour::Pointer c = DicomRT_Contour::New();
242       c->SetTransformMatrix(mTransformMatrix);
243       bool b = c->Read(j);
244       if (b) {
245         mListOfContours.push_back(c);
246       }
247       ++i;
248     }
249   }
250   else {
251     std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
252   }
253   SetDicomUptodateFlag(true);
254 }
255 #endif
256 //--------------------------------------------------------------------
257
258
259 //--------------------------------------------------------------------
260 void clitk::DicomRT_ROI::SetImage(vvImage::Pointer image)
261 {
262   mImage = image;
263 }
264 //--------------------------------------------------------------------
265
266
267 //--------------------------------------------------------------------
268 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
269 {
270   if (!mMeshIsUpToDate) {
271     ComputeMeshFromContour();
272   }
273   return mMesh;
274 }
275 //--------------------------------------------------------------------
276
277
278 //--------------------------------------------------------------------
279 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
280 {
281   return mListOfContours[n];
282 }
283 //--------------------------------------------------------------------
284
285
286 //--------------------------------------------------------------------
287 void clitk::DicomRT_ROI::ComputeMeshFromContour()
288 {
289   vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
290   for(unsigned int i=0; i<mListOfContours.size(); i++) {
291 #if VTK_MAJOR_VERSION <= 5
292         append->AddInput(mListOfContours[i]->GetMesh());
293 #else
294         append->AddInputData(mListOfContours[i]->GetMesh());
295 #endif
296   }
297   append->Update();
298  
299   mMesh = vtkSmartPointer<vtkPolyData>::New();
300   mMesh->DeepCopy(append->GetOutput());
301   mMeshIsUpToDate = true;
302 }
303 //--------------------------------------------------------------------
304
305
306 #if GDCM_MAJOR_VERSION >= 2
307 //--------------------------------------------------------------------
308 void clitk::DicomRT_ROI::UpdateDicomItem()
309 {
310   FATAL("Error : compile vv with itk4 + external gdcm");
311
312   if (GetDicomUptoDateFlag()) return;
313   DD("ROI::UpdateDicomItem");
314   DD(GetName());  
315
316   // From now, only some item can be modified
317
318   // Set ROI Name 0x3006,0x26> 
319   gdcm::Attribute<0x3006,0x26> roiname;
320   roiname.SetValue(GetName());
321   gdcm::DataElement de = roiname.GetAsDataElement();
322   gdcm::DataSet & ds = mItemInfo->GetNestedDataSet();  
323   ds.Replace(de);
324
325   // From MESH to CONTOURS
326   ComputeContoursFromImage();
327
328   // Update contours
329   DD(mListOfContours.size());
330   for(uint i=0; i<mListOfContours.size(); i++) {
331     DD(i);
332     DicomRT_Contour::Pointer contour = mListOfContours[i];
333     contour->UpdateDicomItem();//mItemContour);
334   }
335
336   // Nb of contours
337   unsigned int nitems = mContoursSequenceOfItems->GetNumberOfItems();
338   DD(nitems);
339
340   // Write [Contour Sequence] = 0x3006,0x0040)
341   gdcm::DataSet & dsc = mItemContour->GetNestedDataSet();
342   gdcm::Tag tcsq(0x3006,0x0040);
343   const gdcm::DataElement& csq = dsc.GetDataElement( tcsq );
344   gdcm::DataElement dec(csq);
345   dec.SetValue(*mContoursSequenceOfItems);
346   dsc.Replace(dec);
347
348   gdcm::DataSet & a = mContoursSequenceOfItems->GetItem(1).GetNestedDataSet();
349   gdcm::Attribute<0x3006,0x0050> at;
350   gdcm::Tag tcontourdata(0x3006,0x0050);
351   gdcm::DataElement contourdata = a.GetDataElement( tcontourdata );
352   at.SetFromDataElement( contourdata );
353   const double* points = at.GetValues();
354   DD(points[0]);
355
356 }
357 //--------------------------------------------------------------------
358 #endif
359
360 //--------------------------------------------------------------------
361 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage::Pointer image, int n,
362                                             std::string name,
363                                             std::vector<double> color, 
364                                             std::string filename)
365 {
366   // ROI number [Referenced ROI Number]
367   mNumber = n;
368
369   // ROI Name
370   mName = name;
371   mFilename = filename;
372
373   // ROI Color [ROI Display Color]
374   mColor = color;
375
376   // No contours [Contour Sequence]
377   mListOfContours.clear();
378
379   // Set image
380   mImage = image;
381 }
382 //--------------------------------------------------------------------
383
384
385 //--------------------------------------------------------------------
386 vvImage * clitk::DicomRT_ROI::GetImage() const
387 {
388   return mImage;
389 }
390 //--------------------------------------------------------------------
391
392
393 //--------------------------------------------------------------------
394 void clitk::DicomRT_ROI::SetTransformMatrix(vtkMatrix4x4* matrix)
395 {
396   mTransformMatrix = matrix;
397 }
398 //--------------------------------------------------------------------
399
400
401 //--------------------------------------------------------------------
402 void clitk::DicomRT_ROI::ComputeContoursFromImage()
403 {
404   FATAL("ComputeContoursFromImage should not be call. To be replace");
405   DD("ComputeMeshFromImage");
406
407   // Check that an image is loaded
408   if (!mImage) return;
409
410   // Only consider 3D here
411   if (mImage->GetNumberOfDimensions() != 3) {
412     FATAL("DicomRT_ROI::ComputeMeshFromImage only work with 3D images");
413   }
414
415   // Get the VTK image
416   vtkImageData * image = mImage->GetVTKImages()[0];
417   
418   // Get initial extend for the clipping
419   vtkSmartPointer<vtkImageClip> clipper = vtkSmartPointer<vtkImageClip>::New();
420 #if VTK_MAJOR_VERSION <= 5
421   clipper->SetInput(image);
422 #else
423   clipper->SetInputData(image);
424 #endif
425   
426   int* extent = image->GetExtent();
427   DDV(extent, 6);
428   //  std::vector<int> extend;
429
430
431   // Loop on slice
432   uint n = image->GetDimensions()[2];
433   DD(n);
434   DD(mListOfContours.size());
435   mListOfContours.resize(n); /// ???FIXME
436   DD(mListOfContours.size());
437   std::vector<vtkSmartPointer<vtkPolyData> > contours;
438   for(uint i=0; i<n; i++) {
439     DD(i);
440
441     // FIXME     vtkDiscreteMarchingCubes INSTEAD
442
443
444     vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
445 #if VTK_MAJOR_VERSION <= 5
446     squares->SetInput(image);
447 #else
448     squares->SetInputData(image);
449 #endif
450     squares->SetImageRange(extent[0], extent[1], extent[2], extent[3], i, i);
451     squares->SetValue(1, 1.0);
452     squares->Update();
453     DD(squares->GetNumberOfContours());
454     
455     //clitk::DicomRT_Contour * contour = new clitk::DicomRT_Contour();
456     //mListOfContours[i]->SetMesh(squares->GetOutput());
457
458     
459     vtkSmartPointer<vtkPolyData> m = squares->GetOutput();
460     contours.push_back(m);
461
462     /*
463     // Clip to the current slice
464     extent[4] = extent[5] = image->GetOrigin()[2]+i*image->GetSpacing()[2];
465     DDV(extent, 6);
466     // Prepare the marching squares
467     vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
468     clipper->SetOutputWholeExtent(extent[0],extent[1],extent[2],
469                                 extent[3],extent[4],extent[5]); 
470
471     squares->SetInput(clipper->GetOutput());
472     squares->Update();
473     DD(squares->GetNumberOfContours());
474     mListOfContours[i]->SetMesh(squares->GetOutput());
475     */
476   }
477   DD("done");
478  
479   vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
480   for(unsigned int i=0; i<n; i++) {
481 #if VTK_MAJOR_VERSION <= 5
482     append->AddInput(contours[i]);
483 #else
484     append->AddInputData(contours[i]);
485 #endif
486   }
487   append->Update();
488  
489   mMesh = vtkSmartPointer<vtkPolyData>::New();
490   mMesh->DeepCopy(append->GetOutput());
491   
492   // Write vtk
493   vtkPolyDataWriter * w = vtkPolyDataWriter::New();
494 #if VTK_MAJOR_VERSION <= 5
495   w->SetInput(mMesh);
496 #else
497   w->SetInputData(mMesh);
498 #endif
499   w->SetFileName("toto.vtk");
500   w->Write();
501
502   DD("done");
503 }
504 //--------------------------------------------------------------------
505
506
507 //--------------------------------------------------------------------
508 #if CLITK_USE_SYSTEM_GDCM == 1
509 void clitk::DicomRT_ROI::Read(vtkSmartPointer<vtkGDCMPolyDataReader> & reader, int roiindex)
510 {
511   vtkRTStructSetProperties * p = reader->GetRTStructSetProperties();
512   
513   mName = p->GetStructureSetROIName(roiindex);
514   mNumber = p->GetStructureSetROINumber(roiindex);
515
516   //mColor = //FIXME !!  
517
518   // gdcm::Attribute<0x3006,0x002a> color = {};
519   
520   // const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
521   // color.SetFromDataSet( nestedds );
522   // assert( color.GetNumberOfValues() == 3 );
523   // mColor[0] = color.GetValue(0);
524   // mColor[1] = color.GetValue(1);
525   // mColor[2] = color.GetValue(2);
526
527
528   SetDicomUptodateFlag(true);
529   // Get the contour
530   mMesh =  reader->GetOutput(roiindex);  
531   DicomRT_Contour::Pointer c = DicomRT_Contour::New();
532   c->SetTransformMatrix(mTransformMatrix);
533   c->SetMesh(mMesh); // FIXME no GetZ, not GetPoints  
534   mMeshIsUpToDate = true;
535   mListOfContours.push_back(c);
536 }
537 #endif
538 //--------------------------------------------------------------------
539