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