]> Creatis software - clitk.git/blob - common/clitkDicomRT_ROI.cxx
changes in license header
[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     }
167   const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
168   gdcm::SmartPointer<gdcm::SequenceOfItems> sqi2 = csq.GetValueAsSQ();
169   if( !sqi2 || !sqi2->GetNumberOfItems() )
170     {
171     }
172   unsigned int nitems = sqi2->GetNumberOfItems();
173
174   bool contour_processed=false;
175   bool delta_computed=false;
176   double last_z=0;
177   for(unsigned int i = 0; i < nitems; ++i)
178     {
179     const gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
180     DicomRT_Contour::Pointer c = DicomRT_Contour::New();
181     bool b = c->Read(j);
182     if (b) {
183       mListOfContours.push_back(c);
184       if (contour_processed) {
185         double delta=c->GetZ() - last_z;
186         if (delta_computed)
187           assert(mZDelta == delta);
188         else
189           mZDelta = delta;
190       } else
191         contour_processed=true;
192       last_z=c->GetZ();
193     }
194   }
195
196 }
197 #else
198 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
199 {
200
201   // Change number if needed
202
203   // TODO
204
205   // ROI number [Referenced ROI Number]
206   mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
207
208   // Retrieve ROI Name
209   mName = rois[mNumber];
210
211   // ROI Color [ROI Display Color]
212   mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
213
214   // Read contours [Contour Sequence]
215   gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
216   bool contour_processed=false;
217   bool delta_computed=false;
218   double last_z=0;
219   for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
220     DicomRT_Contour::Pointer c = DicomRT_Contour::New();
221     bool b = c->Read(j);
222     if (b) {
223       mListOfContours.push_back(c);
224       if (contour_processed) {
225         double delta=c->GetZ() - last_z;
226         if (delta_computed)
227           assert(mZDelta == delta);
228         else
229           mZDelta = delta;
230       } else
231         contour_processed=true;
232       last_z=c->GetZ();
233     }
234   }
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 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
258 {
259   return mListOfContours[n];
260 }
261
262 //--------------------------------------------------------------------
263 void clitk::DicomRT_ROI::ComputeMesh()
264 {
265   vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
266   for(unsigned int i=0; i<mListOfContours.size(); i++) {
267     append->AddInput(mListOfContours[i]->GetMesh());
268   }
269   append->Update();
270  
271   mMesh = vtkSmartPointer<vtkPolyData>::New();
272   mMesh->DeepCopy(append->GetOutput());
273   mMeshIsUpToDate = true;
274 }
275 //--------------------------------------------------------------------
276
277
278 //--------------------------------------------------------------------
279 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage * image, int n,
280                                             std::string name,
281                                             std::vector<double> color, 
282                                             std::string filename)
283 {
284
285   // ROI number [Referenced ROI Number]
286   mNumber = n;
287
288   // ROI Name
289   mName = name;
290   mFilename = filename;
291
292   // ROI Color [ROI Display Color]
293   mColor = color;
294
295   // No contours [Contour Sequence]
296   mListOfContours.clear();
297
298   // Set image
299   mImage = image;
300 }
301 //--------------------------------------------------------------------
302
303
304 //--------------------------------------------------------------------
305 vvImage * clitk::DicomRT_ROI::GetImage() const
306 {
307   return mImage;
308 }
309 //--------------------------------------------------------------------