]> Creatis software - clitk.git/blob - common/clitkDicomRT_ROI.cxx
Debug vvMainWinsow.ui
[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     }
208   unsigned int nitems = sqi2->GetNumberOfItems();
209
210   for(unsigned int i = 0; i < nitems; ++i)
211     {
212       gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
213       DicomRT_Contour::Pointer c = DicomRT_Contour::New();
214       bool b = c->Read(&j);
215       if (b) {
216         mListOfContours.push_back(c);
217       }
218     }
219   SetDicomUptodateFlag(true);
220   return true;
221 }
222 #else
223 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
224 {
225   // ROI number [Referenced ROI Number]
226   mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
227
228   // Retrieve ROI Name
229   mName = rois[mNumber];
230
231   // ROI Color [ROI Display Color]
232   mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
233
234   // Read contours [Contour Sequence]
235   gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
236   if (contours) {
237     int i=0;
238     for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
239       DicomRT_Contour::Pointer c = DicomRT_Contour::New();
240       bool b = c->Read(j);
241       if (b) {
242         mListOfContours.push_back(c);
243       }
244       ++i;
245     }
246   }
247   else {
248     std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
249   }
250   SetDicomUptodateFlag(true);
251 }
252 #endif
253 //--------------------------------------------------------------------
254
255
256 //--------------------------------------------------------------------
257 void clitk::DicomRT_ROI::SetImage(vvImage::Pointer image)
258 {
259   mImage = image;
260 }
261 //--------------------------------------------------------------------
262
263
264 //--------------------------------------------------------------------
265 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
266 {
267   if (!mMeshIsUpToDate) {
268     ComputeMeshFromContour();
269   }
270   return mMesh;
271 }
272 //--------------------------------------------------------------------
273
274
275 //--------------------------------------------------------------------
276 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
277 {
278   return mListOfContours[n];
279 }
280 //--------------------------------------------------------------------
281
282
283 //--------------------------------------------------------------------
284 void clitk::DicomRT_ROI::ComputeMeshFromContour()
285 {
286   vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
287   for(unsigned int i=0; i<mListOfContours.size(); i++) {
288 #if VTK_MAJOR_VERSION <= 5
289         append->AddInput(mListOfContours[i]->GetMesh());
290 #else
291         append->AddInputData(mListOfContours[i]->GetMesh());
292 #endif
293   }
294   append->Update();
295  
296   mMesh = vtkSmartPointer<vtkPolyData>::New();
297   mMesh->DeepCopy(append->GetOutput());
298   mMeshIsUpToDate = true;
299 }
300 //--------------------------------------------------------------------
301
302
303 #if GDCM_MAJOR_VERSION == 2
304 //--------------------------------------------------------------------
305 void clitk::DicomRT_ROI::UpdateDicomItem()
306 {
307   FATAL("Error : compile vv with itk4 + external gdcm");
308
309   if (GetDicomUptoDateFlag()) return;
310   DD("ROI::UpdateDicomItem");
311   DD(GetName());  
312
313   // From now, only some item can be modified
314
315   // Set ROI Name 0x3006,0x26> 
316   gdcm::Attribute<0x3006,0x26> roiname;
317   roiname.SetValue(GetName());
318   gdcm::DataElement de = roiname.GetAsDataElement();
319   gdcm::DataSet & ds = mItemInfo->GetNestedDataSet();  
320   ds.Replace(de);
321
322   // From MESH to CONTOURS
323   ComputeContoursFromImage();
324
325   // Update contours
326   DD(mListOfContours.size());
327   for(uint i=0; i<mListOfContours.size(); i++) {
328     DD(i);
329     DicomRT_Contour::Pointer contour = mListOfContours[i];
330     contour->UpdateDicomItem();//mItemContour);
331   }
332
333   // Nb of contours
334   unsigned int nitems = mContoursSequenceOfItems->GetNumberOfItems();
335   DD(nitems);
336
337   // Write [Contour Sequence] = 0x3006,0x0040)
338   gdcm::DataSet & dsc = mItemContour->GetNestedDataSet();
339   gdcm::Tag tcsq(0x3006,0x0040);
340   const gdcm::DataElement& csq = dsc.GetDataElement( tcsq );
341   gdcm::DataElement dec(csq);
342   dec.SetValue(*mContoursSequenceOfItems);
343   dsc.Replace(dec);
344
345   gdcm::DataSet & a = mContoursSequenceOfItems->GetItem(1).GetNestedDataSet();
346   gdcm::Attribute<0x3006,0x0050> at;
347   gdcm::Tag tcontourdata(0x3006,0x0050);
348   gdcm::DataElement contourdata = a.GetDataElement( tcontourdata );
349   at.SetFromDataElement( contourdata );
350   const double* points = at.GetValues();
351   DD(points[0]);
352
353 }
354 //--------------------------------------------------------------------
355 #endif
356
357 //--------------------------------------------------------------------
358 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage::Pointer image, int n,
359                                             std::string name,
360                                             std::vector<double> color, 
361                                             std::string filename)
362 {
363
364   // ROI number [Referenced ROI Number]
365   mNumber = n;
366
367   // ROI Name
368   mName = name;
369   mFilename = filename;
370
371   // ROI Color [ROI Display Color]
372   mColor = color;
373
374   // No contours [Contour Sequence]
375   mListOfContours.clear();
376
377   // Set image
378   mImage = image;
379 }
380 //--------------------------------------------------------------------
381
382
383 //--------------------------------------------------------------------
384 vvImage * clitk::DicomRT_ROI::GetImage() const
385 {
386   return mImage;
387 }
388 //--------------------------------------------------------------------
389
390
391 //--------------------------------------------------------------------
392 void clitk::DicomRT_ROI::ComputeContoursFromImage()
393 {
394   FATAL("ComputeContoursFromImage should not be call. To be replace");
395   DD("ComputeMeshFromImage");
396
397   // Check that an image is loaded
398   if (!mImage) return;
399
400   // Only consider 3D here
401   if (mImage->GetNumberOfDimensions() != 3) {
402     FATAL("DicomRT_ROI::ComputeMeshFromImage only work with 3D images");
403   }
404
405   // Get the VTK image
406   vtkImageData * image = mImage->GetVTKImages()[0];
407   
408   // Get initial extend for the clipping
409   vtkSmartPointer<vtkImageClip> clipper = vtkSmartPointer<vtkImageClip>::New();
410 #if VTK_MAJOR_VERSION <= 5
411   clipper->SetInput(image);
412 #else
413   clipper->SetInputData(image);
414 #endif
415   
416   int* extent = image->GetExtent();
417   DDV(extent, 6);
418   //  std::vector<int> extend;
419
420
421   // Loop on slice
422   uint n = image->GetDimensions()[2];
423   DD(n);
424   DD(mListOfContours.size());
425   mListOfContours.resize(n); /// ???FIXME
426   DD(mListOfContours.size());
427   std::vector<vtkSmartPointer<vtkPolyData> > contours;
428   for(uint i=0; i<n; i++) {
429     DD(i);
430
431     // FIXME     vtkDiscreteMarchingCubes INSTEAD
432
433
434     vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
435 #if VTK_MAJOR_VERSION <= 5
436     squares->SetInput(image);
437 #else
438     squares->SetInputData(image);
439 #endif
440     squares->SetImageRange(extent[0], extent[1], extent[2], extent[3], i, i);
441     squares->SetValue(1, 1.0);
442     squares->Update();
443     DD(squares->GetNumberOfContours());
444     
445     //clitk::DicomRT_Contour * contour = new clitk::DicomRT_Contour();
446     //mListOfContours[i]->SetMesh(squares->GetOutput());
447
448     
449     vtkSmartPointer<vtkPolyData> m = squares->GetOutput();
450     contours.push_back(m);
451
452     /*
453     // Clip to the current slice
454     extent[4] = extent[5] = image->GetOrigin()[2]+i*image->GetSpacing()[2];
455     DDV(extent, 6);
456     // Prepare the marching squares
457     vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
458     clipper->SetOutputWholeExtent(extent[0],extent[1],extent[2],
459                                 extent[3],extent[4],extent[5]); 
460
461     squares->SetInput(clipper->GetOutput());
462     squares->Update();
463     DD(squares->GetNumberOfContours());
464     mListOfContours[i]->SetMesh(squares->GetOutput());
465     */
466   }
467   DD("done");
468  
469   vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
470   for(unsigned int i=0; i<n; i++) {
471 #if VTK_MAJOR_VERSION <= 5
472     append->AddInput(contours[i]);
473 #else
474     append->AddInputData(contours[i]);
475 #endif
476   }
477   append->Update();
478  
479   mMesh = vtkSmartPointer<vtkPolyData>::New();
480   mMesh->DeepCopy(append->GetOutput());
481   
482   // Write vtk
483   vtkPolyDataWriter * w = vtkPolyDataWriter::New();
484 #if VTK_MAJOR_VERSION <= 5
485   w->SetInput(mMesh);
486 #else
487   w->SetInputData(mMesh);
488 #endif
489   w->SetFileName("toto.vtk");
490   w->Write();
491
492   DD("done");
493 }
494 //--------------------------------------------------------------------
495
496
497 //--------------------------------------------------------------------
498 #if CLITK_USE_SYSTEM_GDCM == 1
499 void clitk::DicomRT_ROI::Read(vtkSmartPointer<vtkGDCMPolyDataReader> & reader, int roiindex)
500 {
501   vtkRTStructSetProperties * p = reader->GetRTStructSetProperties();
502   
503   mName = p->GetStructureSetROIName(roiindex);
504   mNumber = p->GetStructureSetROINumber(roiindex);
505
506   //mColor = //FIXME !!  
507
508   // gdcm::Attribute<0x3006,0x002a> color = {};
509   
510   // const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
511   // color.SetFromDataSet( nestedds );
512   // assert( color.GetNumberOfValues() == 3 );
513   // mColor[0] = color.GetValue(0);
514   // mColor[1] = color.GetValue(1);
515   // mColor[2] = color.GetValue(2);
516
517
518   SetDicomUptodateFlag(true);
519   // Get the contour
520   mMesh =  reader->GetOutput(roiindex);  
521   DicomRT_Contour::Pointer c = DicomRT_Contour::New();
522   c->SetMesh(mMesh); // FIXME no GetZ, not GetPoints  
523   mMeshIsUpToDate = true;
524   mListOfContours.push_back(c);
525 }
526 #endif
527 //--------------------------------------------------------------------
528