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