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