]> Creatis software - clitk.git/blob - common/clitkDicomRT_ROI.cxx
Remove DeltaZ computation (false)
[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 }
40 //--------------------------------------------------------------------
41
42
43 //--------------------------------------------------------------------
44 clitk::DicomRT_ROI::~DicomRT_ROI()
45 {
46 }
47 //--------------------------------------------------------------------
48
49
50 //--------------------------------------------------------------------
51 void clitk::DicomRT_ROI::SetDisplayColor(double r, double v, double b)
52 {
53   mColor.resize(3);
54   mColor[0] = r;
55   mColor[1] = v;
56   mColor[2] = b;
57 }
58 //--------------------------------------------------------------------
59
60
61 //--------------------------------------------------------------------
62 int clitk::DicomRT_ROI::GetROINumber() const
63 {
64   return mNumber;
65 }
66 //--------------------------------------------------------------------
67
68
69 //--------------------------------------------------------------------
70 const std::string & clitk::DicomRT_ROI::GetName() const
71 {
72   return mName;
73 }
74 //--------------------------------------------------------------------
75
76
77 //--------------------------------------------------------------------
78 const std::string & clitk::DicomRT_ROI::GetFilename() const
79 {
80   return mFilename;
81 }
82 //--------------------------------------------------------------------
83
84
85 //--------------------------------------------------------------------
86 const std::vector<double> & clitk::DicomRT_ROI::GetDisplayColor() const
87 {
88   return mColor;
89 }
90 //--------------------------------------------------------------------
91
92
93 //--------------------------------------------------------------------
94 void clitk::DicomRT_ROI::Print(std::ostream & os) const
95 {
96   os << "ROI " << mNumber << "\t" << mName
97      << "\t(" << mColor[0] << " " << mColor[1] << " " << mColor[2] << ")"
98      << "\t Contours = " << mListOfContours.size() << std::endl;
99 }
100 //--------------------------------------------------------------------
101
102
103 //--------------------------------------------------------------------
104 void clitk::DicomRT_ROI::SetBackgroundValueLabelImage(double bg)
105 {
106   mBackgroundValue = bg;
107 }
108 //--------------------------------------------------------------------
109
110
111 //--------------------------------------------------------------------
112 double clitk::DicomRT_ROI::GetBackgroundValueLabelImage() const
113 {
114   return mBackgroundValue;
115 }
116 //--------------------------------------------------------------------
117
118
119 //--------------------------------------------------------------------
120 void clitk::DicomRT_ROI::SetForegroundValueLabelImage(double bg)
121 {
122   mForegroundValue = bg;
123 }
124 //--------------------------------------------------------------------
125
126
127 //--------------------------------------------------------------------
128 double clitk::DicomRT_ROI::GetForegroundValueLabelImage() const
129 {
130   return mForegroundValue;
131 }
132 //--------------------------------------------------------------------
133
134
135 //--------------------------------------------------------------------
136 #if GDCM_MAJOR_VERSION == 2
137 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::Item const & item)
138 {
139   const gdcm::DataSet& nestedds = item.GetNestedDataSet();
140
141   gdcm::Attribute<0x3006,0x0084> referencedroinumber;
142   referencedroinumber.SetFromDataSet( nestedds );
143   // Change number if needed
144
145   // TODO
146
147   // ROI number [Referenced ROI Number]
148   mNumber = referencedroinumber.GetValue();
149
150   // Retrieve ROI Name
151   mName = rois[mNumber];
152
153   // ROI Color [ROI Display Color]
154   gdcm::Attribute<0x3006,0x002a> color = {};
155   color.SetFromDataSet( nestedds );
156   assert( color.GetNumberOfValues() == 3 );
157   mColor[0] = color.GetValue(0);
158   mColor[1] = color.GetValue(1);
159   mColor[2] = color.GetValue(2);
160
161   // Read contours [Contour Sequence]
162   gdcm::Tag tcsq(0x3006,0x0040);
163   if( !nestedds.FindDataElement( tcsq ) )
164     {
165       std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
166       return;
167     }
168   const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
169   gdcm::SmartPointer<gdcm::SequenceOfItems> sqi2 = csq.GetValueAsSQ();
170   if( !sqi2 || !sqi2->GetNumberOfItems() )
171     {
172     }
173   unsigned int nitems = sqi2->GetNumberOfItems();
174
175   for(unsigned int i = 0; i < nitems; ++i)
176     {
177       const gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
178       DicomRT_Contour::Pointer c = DicomRT_Contour::New();
179       bool b = c->Read(j);
180       if (b) {
181         mListOfContours.push_back(c);
182       }
183     }
184 }
185 #else
186 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
187 {
188
189   // Change number if needed
190
191   // TODO
192
193   // ROI number [Referenced ROI Number]
194   mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
195
196   // Retrieve ROI Name
197   mName = rois[mNumber];
198
199   // ROI Color [ROI Display Color]
200   mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
201
202   // Read contours [Contour Sequence]
203   gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
204   if (contours) {
205     int i=0;
206     for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
207       DicomRT_Contour::Pointer c = DicomRT_Contour::New();
208       bool b = c->Read(j);
209       if (b) {
210         mListOfContours.push_back(c);
211       }
212       ++i;
213     }
214   }
215   else {
216     std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
217   }
218 }
219 #endif
220 //--------------------------------------------------------------------
221
222
223 //--------------------------------------------------------------------
224 void clitk::DicomRT_ROI::SetImage(vvImage * image)
225 {
226   mImage = image;
227 }
228 //--------------------------------------------------------------------
229
230
231 //--------------------------------------------------------------------
232 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
233 {
234   if (!mMeshIsUpToDate) {
235     ComputeMesh();
236   }
237   return mMesh;
238 }
239 //--------------------------------------------------------------------
240 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
241 {
242   return mListOfContours[n];
243 }
244
245 //--------------------------------------------------------------------
246 void clitk::DicomRT_ROI::ComputeMesh()
247 {
248   vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
249   for(unsigned int i=0; i<mListOfContours.size(); i++) {
250     append->AddInput(mListOfContours[i]->GetMesh());
251   }
252   append->Update();
253  
254   mMesh = vtkSmartPointer<vtkPolyData>::New();
255   mMesh->DeepCopy(append->GetOutput());
256   mMeshIsUpToDate = true;
257 }
258 //--------------------------------------------------------------------
259
260
261 //--------------------------------------------------------------------
262 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage * image, int n,
263                                             std::string name,
264                                             std::vector<double> color, 
265                                             std::string filename)
266 {
267
268   // ROI number [Referenced ROI Number]
269   mNumber = n;
270
271   // ROI Name
272   mName = name;
273   mFilename = filename;
274
275   // ROI Color [ROI Display Color]
276   mColor = color;
277
278   // No contours [Contour Sequence]
279   mListOfContours.clear();
280
281   // Set image
282   mImage = image;
283 }
284 //--------------------------------------------------------------------
285
286
287 //--------------------------------------------------------------------
288 vvImage * clitk::DicomRT_ROI::GetImage() const
289 {
290   return mImage;
291 }
292 //--------------------------------------------------------------------