]> Creatis software - clitk.git/blob - common/clitkDicomRT_ROI.cxx
improvements to the Dicom struct handling code, including better computation of extruding
[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://oncora1.lyon.fnclcc.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 //--------------------------------------------------------------------
25 clitk::DicomRT_ROI::DicomRT_ROI()
26 {
27   mName = "NoName";
28   mNumber = -1;
29   mColor.resize(3);
30   mColor[0] = mColor[1] = mColor[2] = 0;
31   mMeshIsUpToDate = false;
32   mBackgroundValue = 0;
33   mForegroundValue = 1;
34   mZDelta = 0;
35 }
36 //--------------------------------------------------------------------
37
38
39 //--------------------------------------------------------------------
40 clitk::DicomRT_ROI::~DicomRT_ROI()
41 {
42
43 }
44 //--------------------------------------------------------------------
45
46
47 //--------------------------------------------------------------------
48 void clitk::DicomRT_ROI::SetDisplayColor(double r, double v, double b)
49 {
50   mColor.resize(3);
51   mColor[0] = r;
52   mColor[1] = v;
53   mColor[2] = b;
54 }
55 //--------------------------------------------------------------------
56
57
58 //--------------------------------------------------------------------
59 int clitk::DicomRT_ROI::GetROINumber() const
60 {
61   return mNumber;
62 }
63 //--------------------------------------------------------------------
64
65
66 //--------------------------------------------------------------------
67 const std::string & clitk::DicomRT_ROI::GetName() const
68 {
69   return mName;
70 }
71 //--------------------------------------------------------------------
72
73
74 //--------------------------------------------------------------------
75 const std::string & clitk::DicomRT_ROI::GetFilename() const
76 {
77   return mFilename;
78 }
79 //--------------------------------------------------------------------
80
81
82 //--------------------------------------------------------------------
83 const std::vector<double> & clitk::DicomRT_ROI::GetDisplayColor() const
84 {
85   return mColor;
86 }
87 //--------------------------------------------------------------------
88
89
90 //--------------------------------------------------------------------
91 void clitk::DicomRT_ROI::Print(std::ostream & os) const
92 {
93   os << "ROI " << mNumber << "\t" << mName
94      << "\t(" << mColor[0] << " " << mColor[1] << " " << mColor[2] << ")"
95      << "\t Contours = " << mListOfContours.size() << std::endl;
96 }
97 //--------------------------------------------------------------------
98
99
100 //--------------------------------------------------------------------
101 void clitk::DicomRT_ROI::SetBackgroundValueLabelImage(double bg)
102 {
103   mBackgroundValue = bg;
104 }
105 //--------------------------------------------------------------------
106
107
108 //--------------------------------------------------------------------
109 double clitk::DicomRT_ROI::GetBackgroundValueLabelImage() const
110 {
111   return mBackgroundValue;
112 }
113 //--------------------------------------------------------------------
114
115
116 //--------------------------------------------------------------------
117 void clitk::DicomRT_ROI::SetForegroundValueLabelImage(double bg)
118 {
119   mForegroundValue = bg;
120 }
121 //--------------------------------------------------------------------
122
123
124 //--------------------------------------------------------------------
125 double clitk::DicomRT_ROI::GetForegroundValueLabelImage() const
126 {
127   return mForegroundValue;
128 }
129 //--------------------------------------------------------------------
130
131
132 //--------------------------------------------------------------------
133 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
134 {
135
136   // Change number if needed
137
138   // TODO
139
140   // ROI number [Referenced ROI Number]
141   mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
142
143   // Retrieve ROI Name
144   mName = rois[mNumber];
145
146   // ROI Color [ROI Display Color]
147   mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
148
149   // Read contours [Contour Sequence]
150   gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
151   bool contour_processed=false;
152   bool delta_computed=false;
153   double last_z=0;
154   for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
155     DicomRT_Contour * c = new DicomRT_Contour;
156     bool b = c->Read(j);
157     if (b) {
158       mListOfContours.push_back(c);
159       if (contour_processed) {
160         double delta=c->GetZ() - last_z;
161         if (delta_computed)
162           assert(mZDelta == delta);
163         else
164           mZDelta = delta;
165       } else
166         contour_processed=true;
167       last_z=c->GetZ();
168     }
169   }
170 }
171 //--------------------------------------------------------------------
172
173
174 //--------------------------------------------------------------------
175 void clitk::DicomRT_ROI::SetImage(vvImage * image)
176 {
177   mImage = image;
178 }
179 //--------------------------------------------------------------------
180
181
182 //--------------------------------------------------------------------
183 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
184 {
185   if (!mMeshIsUpToDate) {
186     ComputeMesh();
187   }
188   return mMesh;
189 }
190 //--------------------------------------------------------------------
191 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
192 {
193   return mListOfContours[n];
194 }
195
196 //--------------------------------------------------------------------
197 void clitk::DicomRT_ROI::ComputeMesh()
198 {
199   vtkAppendPolyData * append = vtkAppendPolyData::New();
200   for(unsigned int i=0; i<mListOfContours.size(); i++) {
201     append->AddInput(mListOfContours[i]->GetMesh());
202   }
203   append->Update();
204   mMesh = append->GetOutput();
205   mMeshIsUpToDate = true;
206 }
207 //--------------------------------------------------------------------
208
209
210 //--------------------------------------------------------------------
211 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage::Pointer image, int n,
212                                             std::string name,
213                                             std::vector<double> color, 
214                                             std::string filename)
215 {
216
217   // ROI number [Referenced ROI Number]
218   mNumber = n;
219
220   // ROI Name
221   mName = name;
222   mFilename = filename;
223
224   // ROI Color [ROI Display Color]
225   mColor = color;
226
227   // No contours [Contour Sequence]
228   mListOfContours.clear();
229
230   // Set image
231   mImage = image;
232 }
233 //--------------------------------------------------------------------
234
235
236 //--------------------------------------------------------------------
237 const vvImage::Pointer clitk::DicomRT_ROI::GetImage() const
238 {
239   return mImage;
240 }
241 //--------------------------------------------------------------------