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