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