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;
40 //--------------------------------------------------------------------
43 //--------------------------------------------------------------------
44 clitk::DicomRT_ROI::~DicomRT_ROI()
47 //--------------------------------------------------------------------
50 //--------------------------------------------------------------------
51 void clitk::DicomRT_ROI::SetDisplayColor(double r, double v, double b)
58 //--------------------------------------------------------------------
61 //--------------------------------------------------------------------
62 int clitk::DicomRT_ROI::GetROINumber() const
66 //--------------------------------------------------------------------
69 //--------------------------------------------------------------------
70 const std::string & clitk::DicomRT_ROI::GetName() const
74 //--------------------------------------------------------------------
77 //--------------------------------------------------------------------
78 const std::string & clitk::DicomRT_ROI::GetFilename() const
82 //--------------------------------------------------------------------
85 //--------------------------------------------------------------------
86 const std::vector<double> & clitk::DicomRT_ROI::GetDisplayColor() const
90 //--------------------------------------------------------------------
93 //--------------------------------------------------------------------
94 void clitk::DicomRT_ROI::Print(std::ostream & os) const
96 os << "ROI " << mNumber << "\t" << mName
97 << "\t(" << mColor[0] << " " << mColor[1] << " " << mColor[2] << ")"
98 << "\t Contours = " << mListOfContours.size() << std::endl;
100 //--------------------------------------------------------------------
103 //--------------------------------------------------------------------
104 void clitk::DicomRT_ROI::SetBackgroundValueLabelImage(double bg)
106 mBackgroundValue = bg;
108 //--------------------------------------------------------------------
111 //--------------------------------------------------------------------
112 double clitk::DicomRT_ROI::GetBackgroundValueLabelImage() const
114 return mBackgroundValue;
116 //--------------------------------------------------------------------
119 //--------------------------------------------------------------------
120 void clitk::DicomRT_ROI::SetForegroundValueLabelImage(double bg)
122 mForegroundValue = bg;
124 //--------------------------------------------------------------------
127 //--------------------------------------------------------------------
128 double clitk::DicomRT_ROI::GetForegroundValueLabelImage() const
130 return mForegroundValue;
132 //--------------------------------------------------------------------
135 //--------------------------------------------------------------------
136 #if GDCM_MAJOR_VERSION == 2
137 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::Item const & item)
139 const gdcm::DataSet& nestedds = item.GetNestedDataSet();
141 gdcm::Attribute<0x3006,0x0084> referencedroinumber;
142 referencedroinumber.SetFromDataSet( nestedds );
143 // Change number if needed
147 // ROI number [Referenced ROI Number]
148 mNumber = referencedroinumber.GetValue();
151 mName = rois[mNumber];
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);
161 // Read contours [Contour Sequence]
162 gdcm::Tag tcsq(0x3006,0x0040);
163 if( !nestedds.FindDataElement( tcsq ) )
165 std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
168 const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
169 gdcm::SmartPointer<gdcm::SequenceOfItems> sqi2 = csq.GetValueAsSQ();
170 if( !sqi2 || !sqi2->GetNumberOfItems() )
173 unsigned int nitems = sqi2->GetNumberOfItems();
175 for(unsigned int i = 0; i < nitems; ++i)
177 const gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
178 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
181 mListOfContours.push_back(c);
186 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
189 // Change number if needed
193 // ROI number [Referenced ROI Number]
194 mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
197 mName = rois[mNumber];
199 // ROI Color [ROI Display Color]
200 mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
202 // Read contours [Contour Sequence]
203 gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
206 for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
207 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
210 mListOfContours.push_back(c);
216 std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
220 //--------------------------------------------------------------------
223 //--------------------------------------------------------------------
224 void clitk::DicomRT_ROI::SetImage(vvImage * image)
228 //--------------------------------------------------------------------
231 //--------------------------------------------------------------------
232 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
234 if (!mMeshIsUpToDate) {
239 //--------------------------------------------------------------------
240 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
242 return mListOfContours[n];
245 //--------------------------------------------------------------------
246 void clitk::DicomRT_ROI::ComputeMesh()
248 vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
249 for(unsigned int i=0; i<mListOfContours.size(); i++) {
250 append->AddInput(mListOfContours[i]->GetMesh());
254 mMesh = vtkSmartPointer<vtkPolyData>::New();
255 mMesh->DeepCopy(append->GetOutput());
256 mMeshIsUpToDate = true;
258 //--------------------------------------------------------------------
261 //--------------------------------------------------------------------
262 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage * image, int n,
264 std::vector<double> color,
265 std::string filename)
268 // ROI number [Referenced ROI Number]
273 mFilename = filename;
275 // ROI Color [ROI Display Color]
278 // No contours [Contour Sequence]
279 mListOfContours.clear();
284 //--------------------------------------------------------------------
287 //--------------------------------------------------------------------
288 vvImage * clitk::DicomRT_ROI::GetImage() const
292 //--------------------------------------------------------------------