]> Creatis software - clitk.git/blob - common/clitkDicomRT_ROI.cxx
Merge branch 'master' of /home/dsarrut/clitk3.server
[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
24 #if GDCM_MAJOR_VERSION == 2
25 #include "gdcmAttribute.h"
26 #include "gdcmItem.h"
27 #endif
28
29 //--------------------------------------------------------------------
30 clitk::DicomRT_ROI::DicomRT_ROI()
31 {
32   mName = "NoName";
33   mNumber = -1;
34   mColor.resize(3);
35   mColor[0] = mColor[1] = mColor[2] = 0;
36   mMeshIsUpToDate = false;
37   mBackgroundValue = 0;
38   mForegroundValue = 1;
39   SetDicomUptodateFlag(false);
40 }
41 //--------------------------------------------------------------------
42
43
44 //--------------------------------------------------------------------
45 clitk::DicomRT_ROI::~DicomRT_ROI()
46 {
47 }
48 //--------------------------------------------------------------------
49
50
51 //--------------------------------------------------------------------
52 void clitk::DicomRT_ROI::SetDisplayColor(double r, double v, double b)
53 {
54   mColor.resize(3);
55   mColor[0] = r;
56   mColor[1] = v;
57   mColor[2] = b;
58 }
59 //--------------------------------------------------------------------
60
61
62 //--------------------------------------------------------------------
63 int clitk::DicomRT_ROI::GetROINumber() const
64 {
65   return mNumber;
66 }
67 //--------------------------------------------------------------------
68
69
70 //--------------------------------------------------------------------
71 const std::string & clitk::DicomRT_ROI::GetName() const
72 {
73   return mName;
74 }
75 //--------------------------------------------------------------------
76
77
78 //--------------------------------------------------------------------
79 const std::string & clitk::DicomRT_ROI::GetFilename() const
80 {
81   return mFilename;
82 }
83 //--------------------------------------------------------------------
84
85
86 //--------------------------------------------------------------------
87 const std::vector<double> & clitk::DicomRT_ROI::GetDisplayColor() const
88 {
89   return mColor;
90 }
91 //--------------------------------------------------------------------
92
93
94 //--------------------------------------------------------------------
95 void clitk::DicomRT_ROI::Print(std::ostream & os) const
96 {
97   os << "ROI " << mNumber << "\t" << mName
98      << "\t(" << mColor[0] << " " << mColor[1] << " " << mColor[2] << ")"
99      << "\t Contours = " << mListOfContours.size() << std::endl;
100 }
101 //--------------------------------------------------------------------
102
103
104 //--------------------------------------------------------------------
105 void clitk::DicomRT_ROI::SetBackgroundValueLabelImage(double bg)
106 {
107   mBackgroundValue = bg;
108 }
109 //--------------------------------------------------------------------
110
111
112 //--------------------------------------------------------------------
113 double clitk::DicomRT_ROI::GetBackgroundValueLabelImage() const
114 {
115   return mBackgroundValue;
116 }
117 //--------------------------------------------------------------------
118
119
120 //--------------------------------------------------------------------
121 void clitk::DicomRT_ROI::SetForegroundValueLabelImage(double bg)
122 {
123   mForegroundValue = bg;
124 }
125 //--------------------------------------------------------------------
126
127
128 //--------------------------------------------------------------------
129 double clitk::DicomRT_ROI::GetForegroundValueLabelImage() const
130 {
131   return mForegroundValue;
132 }
133 //--------------------------------------------------------------------
134
135
136 //--------------------------------------------------------------------
137 #if GDCM_MAJOR_VERSION == 2
138 bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour)
139 {
140   // Keep dicom item
141   mItemInfo = itemInfo;
142   mItemContour = itemContour;
143   // DD(mItemInfo);
144   
145   // ROI number [Referenced ROI Number]
146   const gdcm::DataSet & nesteddsInfo = mItemInfo->GetNestedDataSet();
147   gdcm::Attribute<0x3006,0x0022> roinumber;
148   roinumber.SetFromDataSet( nesteddsInfo );
149   int nb1 = roinumber.GetValue();
150   
151   // Check this is the same with the other item
152   const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
153   gdcm::Attribute<0x3006,0x0084> referencedroinumber;
154   referencedroinumber.SetFromDataSet( nestedds );
155   int nb2 = referencedroinumber.GetValue();
156   
157   // Must never be different
158   if (nb1 != nb2) {
159     DD(nb2);
160     DD(nb1);
161     FATAL("nb1 must equal nb2" << std::endl);
162   }
163   mNumber = nb1;
164
165   // Retrieve ROI Name (in the info item)
166   gdcm::Attribute<0x3006,0x26> roiname;
167   roiname.SetFromDataSet( nesteddsInfo );
168   mName = roiname.GetValue();
169   // DD(mName);
170
171   // ROI Color [ROI Display Color]
172   gdcm::Attribute<0x3006,0x002a> color = {};
173   color.SetFromDataSet( nestedds );
174   assert( color.GetNumberOfValues() == 3 );
175   mColor[0] = color.GetValue(0);
176   mColor[1] = color.GetValue(1);
177   mColor[2] = color.GetValue(2);
178
179   // Read contours [Contour Sequence]
180   gdcm::Tag tcsq(0x3006,0x0040);
181   if( !nestedds.FindDataElement( tcsq ) )
182     {
183       std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
184       SetDicomUptodateFlag(true);
185       return false;
186     }
187   const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
188   gdcm::SmartPointer<gdcm::SequenceOfItems> sqi2 = csq.GetValueAsSQ();
189   if( !sqi2 || !sqi2->GetNumberOfItems() )
190     {
191     }
192   unsigned int nitems = sqi2->GetNumberOfItems();
193
194   for(unsigned int i = 0; i < nitems; ++i)
195     {
196       const gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
197       DicomRT_Contour::Pointer c = DicomRT_Contour::New();
198       bool b = c->Read(j);
199       if (b) {
200         mListOfContours.push_back(c);
201       }
202     }
203   SetDicomUptodateFlag(true);
204   return true;
205 }
206 #else
207 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
208 {
209   // ROI number [Referenced ROI Number]
210   mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
211
212   // Retrieve ROI Name
213   mName = rois[mNumber];
214
215   // ROI Color [ROI Display Color]
216   mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
217
218   // Read contours [Contour Sequence]
219   gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
220   if (contours) {
221     int i=0;
222     for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
223       DicomRT_Contour::Pointer c = DicomRT_Contour::New();
224       bool b = c->Read(j);
225       if (b) {
226         mListOfContours.push_back(c);
227       }
228       ++i;
229     }
230   }
231   else {
232     std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
233   }
234   SetDicomUptodateFlag(true);
235 }
236 #endif
237 //--------------------------------------------------------------------
238
239
240 //--------------------------------------------------------------------
241 void clitk::DicomRT_ROI::SetImage(vvImage * image)
242 {
243   mImage = image;
244 }
245 //--------------------------------------------------------------------
246
247
248 //--------------------------------------------------------------------
249 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
250 {
251   if (!mMeshIsUpToDate) {
252     ComputeMesh();
253   }
254   return mMesh;
255 }
256 //--------------------------------------------------------------------
257
258
259 //--------------------------------------------------------------------
260 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
261 {
262   return mListOfContours[n];
263 }
264 //--------------------------------------------------------------------
265
266
267 //--------------------------------------------------------------------
268 void clitk::DicomRT_ROI::ComputeMesh()
269 {
270   vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
271   for(unsigned int i=0; i<mListOfContours.size(); i++) {
272     append->AddInput(mListOfContours[i]->GetMesh());
273   }
274   append->Update();
275  
276   mMesh = vtkSmartPointer<vtkPolyData>::New();
277   mMesh->DeepCopy(append->GetOutput());
278   mMeshIsUpToDate = true;
279 }
280 //--------------------------------------------------------------------
281
282
283 #if GDCM_MAJOR_VERSION == 2
284
285 //--------------------------------------------------------------------
286 void clitk::DicomRT_ROI::UpdateDicomItem()
287 {
288   if (GetDicomUptoDateFlag()) return;
289   DD("ROI::UpdateDicomItem");
290   DD(GetName());  
291
292   // From now, only some item can be modified
293
294   // Set ROI Name 0x3006,0x26> 
295   gdcm::Attribute<0x3006,0x26> roiname;
296   roiname.SetValue(GetName());
297   gdcm::DataElement de = roiname.GetAsDataElement();
298   gdcm::DataSet & ds = mItemInfo->GetNestedDataSet();  
299   ds.Replace(de);
300
301   // Update contours
302   for(uint i=0; i<mListOfContours.size(); i++) {
303     DD(i);
304     DicomRT_Contour::Pointer roi = mListOfContours[i];
305     roi->UpdateDicomItem(mItemContour);
306   }
307 }
308 //--------------------------------------------------------------------
309 #endif
310
311 //--------------------------------------------------------------------
312 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage * image, int n,
313                                             std::string name,
314                                             std::vector<double> color, 
315                                             std::string filename)
316 {
317
318   // ROI number [Referenced ROI Number]
319   mNumber = n;
320
321   // ROI Name
322   mName = name;
323   mFilename = filename;
324
325   // ROI Color [ROI Display Color]
326   mColor = color;
327
328   // No contours [Contour Sequence]
329   mListOfContours.clear();
330
331   // Set image
332   mImage = image;
333 }
334 //--------------------------------------------------------------------
335
336
337 //--------------------------------------------------------------------
338 vvImage * clitk::DicomRT_ROI::GetImage() const
339 {
340   return mImage;
341 }
342 //--------------------------------------------------------------------