]> 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   mZDelta = 0;
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 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::Item const & item)
139 {
140   const gdcm::DataSet& nestedds = item.GetNestedDataSet();
141
142   gdcm::Attribute<0x3006,0x0084> referencedroinumber;
143   referencedroinumber.SetFromDataSet( nestedds );
144   // Change number if needed
145
146   // TODO
147
148   // ROI number [Referenced ROI Number]
149   mNumber = referencedroinumber.GetValue();
150
151   // Retrieve ROI Name
152   mName = rois[mNumber];
153
154   // ROI Color [ROI Display Color]
155   gdcm::Attribute<0x3006,0x002a> color = {};
156   color.SetFromDataSet( nestedds );
157   assert( color.GetNumberOfValues() == 3 );
158   mColor[0] = color.GetValue(0);
159   mColor[1] = color.GetValue(1);
160   mColor[2] = color.GetValue(2);
161
162   // Read contours [Contour Sequence]
163   gdcm::Tag tcsq(0x3006,0x0040);
164   if( !nestedds.FindDataElement( tcsq ) )
165     {
166       std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
167       return;
168     }
169   const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
170   gdcm::SmartPointer<gdcm::SequenceOfItems> sqi2 = csq.GetValueAsSQ();
171   if( !sqi2 || !sqi2->GetNumberOfItems() )
172     {
173     }
174   unsigned int nitems = sqi2->GetNumberOfItems();
175
176   bool contour_processed=false;
177   bool delta_computed=false;
178   double last_z=0;
179   for(unsigned int i = 0; i < nitems; ++i)
180     {
181       const gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
182       DicomRT_Contour::Pointer c = DicomRT_Contour::New();
183       bool b = c->Read(j);
184       if (b) {
185         mListOfContours.push_back(c);
186         if (contour_processed) {
187           double delta=c->GetZ() - last_z;
188           if (delta_computed)
189             assert(mZDelta == delta);
190           else
191             mZDelta = delta;
192         } else
193           contour_processed=true;
194         last_z=c->GetZ();
195       }
196     }
197
198 }
199 #else
200 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
201 {
202
203   // Change number if needed
204
205   // TODO
206
207   // ROI number [Referenced ROI Number]
208   mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
209
210   // Retrieve ROI Name
211   mName = rois[mNumber];
212
213   // ROI Color [ROI Display Color]
214   mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
215
216   // Read contours [Contour Sequence]
217   gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
218   if (contours) {
219     bool contour_processed=false;
220     bool delta_computed=false;
221     double last_z=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         if (contour_processed) {
228           double delta=c->GetZ() - last_z;
229           if (delta_computed)
230             assert(mZDelta == delta);
231           else
232             mZDelta = delta;
233         } else
234           contour_processed=true;
235         last_z=c->GetZ();
236       }
237     }
238   }
239   else {
240     std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
241   }
242 }
243 #endif
244 //--------------------------------------------------------------------
245
246
247 //--------------------------------------------------------------------
248 void clitk::DicomRT_ROI::SetImage(vvImage * image)
249 {
250   mImage = image;
251 }
252 //--------------------------------------------------------------------
253
254
255 //--------------------------------------------------------------------
256 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
257 {
258   if (!mMeshIsUpToDate) {
259     ComputeMesh();
260   }
261   return mMesh;
262 }
263 //--------------------------------------------------------------------
264 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
265 {
266   return mListOfContours[n];
267 }
268
269 //--------------------------------------------------------------------
270 void clitk::DicomRT_ROI::ComputeMesh()
271 {
272   vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
273   for(unsigned int i=0; i<mListOfContours.size(); i++) {
274     append->AddInput(mListOfContours[i]->GetMesh());
275   }
276   append->Update();
277  
278   mMesh = vtkSmartPointer<vtkPolyData>::New();
279   mMesh->DeepCopy(append->GetOutput());
280   mMeshIsUpToDate = true;
281 }
282 //--------------------------------------------------------------------
283
284
285 //--------------------------------------------------------------------
286 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage * image, int n,
287                                             std::string name,
288                                             std::vector<double> color, 
289                                             std::string filename)
290 {
291
292   // ROI number [Referenced ROI Number]
293   mNumber = n;
294
295   // ROI Name
296   mName = name;
297   mFilename = filename;
298
299   // ROI Color [ROI Display Color]
300   mColor = color;
301
302   // No contours [Contour Sequence]
303   mListOfContours.clear();
304
305   // Set image
306   mImage = image;
307 }
308 //--------------------------------------------------------------------
309
310
311 //--------------------------------------------------------------------
312 vvImage * clitk::DicomRT_ROI::GetImage() const
313 {
314   return mImage;
315 }
316 //--------------------------------------------------------------------