1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
3 Main authors : XX XX XX
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
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.
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
18 =========================================================================*/
20 #include "clitkDicomRT_ROI.h"
21 #include <vtkSmartPointer.h>
22 #include <vtkAppendPolyData.h>
24 #if GDCM_MAJOR_VERSION == 2
25 #include "gdcmAttribute.h"
29 //--------------------------------------------------------------------
30 clitk::DicomRT_ROI::DicomRT_ROI()
35 mColor[0] = mColor[1] = mColor[2] = 0;
36 mMeshIsUpToDate = false;
41 //--------------------------------------------------------------------
44 //--------------------------------------------------------------------
45 clitk::DicomRT_ROI::~DicomRT_ROI()
48 //--------------------------------------------------------------------
51 //--------------------------------------------------------------------
52 void clitk::DicomRT_ROI::SetDisplayColor(double r, double v, double b)
59 //--------------------------------------------------------------------
62 //--------------------------------------------------------------------
63 int clitk::DicomRT_ROI::GetROINumber() const
67 //--------------------------------------------------------------------
70 //--------------------------------------------------------------------
71 const std::string & clitk::DicomRT_ROI::GetName() const
75 //--------------------------------------------------------------------
78 //--------------------------------------------------------------------
79 const std::string & clitk::DicomRT_ROI::GetFilename() const
83 //--------------------------------------------------------------------
86 //--------------------------------------------------------------------
87 const std::vector<double> & clitk::DicomRT_ROI::GetDisplayColor() const
91 //--------------------------------------------------------------------
94 //--------------------------------------------------------------------
95 void clitk::DicomRT_ROI::Print(std::ostream & os) const
97 os << "ROI " << mNumber << "\t" << mName
98 << "\t(" << mColor[0] << " " << mColor[1] << " " << mColor[2] << ")"
99 << "\t Contours = " << mListOfContours.size() << std::endl;
101 //--------------------------------------------------------------------
104 //--------------------------------------------------------------------
105 void clitk::DicomRT_ROI::SetBackgroundValueLabelImage(double bg)
107 mBackgroundValue = bg;
109 //--------------------------------------------------------------------
112 //--------------------------------------------------------------------
113 double clitk::DicomRT_ROI::GetBackgroundValueLabelImage() const
115 return mBackgroundValue;
117 //--------------------------------------------------------------------
120 //--------------------------------------------------------------------
121 void clitk::DicomRT_ROI::SetForegroundValueLabelImage(double bg)
123 mForegroundValue = bg;
125 //--------------------------------------------------------------------
128 //--------------------------------------------------------------------
129 double clitk::DicomRT_ROI::GetForegroundValueLabelImage() const
131 return mForegroundValue;
133 //--------------------------------------------------------------------
136 //--------------------------------------------------------------------
137 #if GDCM_MAJOR_VERSION == 2
138 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::Item const & item)
140 const gdcm::DataSet& nestedds = item.GetNestedDataSet();
142 gdcm::Attribute<0x3006,0x0084> referencedroinumber;
143 referencedroinumber.SetFromDataSet( nestedds );
144 // Change number if needed
148 // ROI number [Referenced ROI Number]
149 mNumber = referencedroinumber.GetValue();
152 mName = rois[mNumber];
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);
162 // Read contours [Contour Sequence]
163 gdcm::Tag tcsq(0x3006,0x0040);
164 if( !nestedds.FindDataElement( tcsq ) )
167 const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
168 gdcm::SmartPointer<gdcm::SequenceOfItems> sqi2 = csq.GetValueAsSQ();
169 if( !sqi2 || !sqi2->GetNumberOfItems() )
172 unsigned int nitems = sqi2->GetNumberOfItems();
174 bool contour_processed=false;
175 bool delta_computed=false;
177 for(unsigned int i = 0; i < nitems; ++i)
179 const gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
180 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
183 mListOfContours.push_back(c);
184 if (contour_processed) {
185 double delta=c->GetZ() - last_z;
187 assert(mZDelta == delta);
191 contour_processed=true;
198 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
201 // Change number if needed
205 // ROI number [Referenced ROI Number]
206 mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
209 mName = rois[mNumber];
211 // ROI Color [ROI Display Color]
212 mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
214 // Read contours [Contour Sequence]
215 gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
216 bool contour_processed=false;
217 bool delta_computed=false;
219 for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
220 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
223 mListOfContours.push_back(c);
224 if (contour_processed) {
225 double delta=c->GetZ() - last_z;
227 assert(mZDelta == delta);
231 contour_processed=true;
237 //--------------------------------------------------------------------
240 //--------------------------------------------------------------------
241 void clitk::DicomRT_ROI::SetImage(vvImage * image)
245 //--------------------------------------------------------------------
248 //--------------------------------------------------------------------
249 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
251 if (!mMeshIsUpToDate) {
256 //--------------------------------------------------------------------
257 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
259 return mListOfContours[n];
262 //--------------------------------------------------------------------
263 void clitk::DicomRT_ROI::ComputeMesh()
265 vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
266 for(unsigned int i=0; i<mListOfContours.size(); i++) {
267 append->AddInput(mListOfContours[i]->GetMesh());
271 mMesh = vtkSmartPointer<vtkPolyData>::New();
272 mMesh->DeepCopy(append->GetOutput());
273 mMeshIsUpToDate = true;
275 //--------------------------------------------------------------------
278 //--------------------------------------------------------------------
279 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage * image, int n,
281 std::vector<double> color,
282 std::string filename)
285 // ROI number [Referenced ROI Number]
290 mFilename = filename;
292 // ROI Color [ROI Display Color]
295 // No contours [Contour Sequence]
296 mListOfContours.clear();
301 //--------------------------------------------------------------------
304 //--------------------------------------------------------------------
305 vvImage * clitk::DicomRT_ROI::GetImage() const
309 //--------------------------------------------------------------------