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;
39 SetDicomUptodateFlag(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 bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour)
141 mItemInfo = itemInfo;
142 mItemContour = itemContour;
145 // ROI number [Referenced ROI Number]
146 const gdcm::DataSet & nesteddsInfo = mItemInfo->GetNestedDataSet();
147 gdcm::Attribute<0x3006,0x0022> roinumber;
148 roinumber.SetFromDataSet( nesteddsInfo );
149 int nb1 = roinumber.GetValue();
151 // Check this is the same with the other item
152 const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
153 gdcm::Attribute<0x3006,0x0084> referencedroinumber;
154 referencedroinumber.SetFromDataSet( nestedds );
155 int nb2 = referencedroinumber.GetValue();
157 // Must never be different
161 FATAL("nb1 must equal nb2" << std::endl);
165 // Retrieve ROI Name (in the info item)
166 gdcm::Attribute<0x3006,0x26> roiname;
167 roiname.SetFromDataSet( nesteddsInfo );
168 mName = roiname.GetValue();
171 // ROI Color [ROI Display Color]
172 gdcm::Attribute<0x3006,0x002a> color = {};
173 color.SetFromDataSet( nestedds );
174 assert( color.GetNumberOfValues() == 3 );
175 mColor[0] = color.GetValue(0);
176 mColor[1] = color.GetValue(1);
177 mColor[2] = color.GetValue(2);
179 // Read contours [Contour Sequence]
180 gdcm::Tag tcsq(0x3006,0x0040);
181 if( !nestedds.FindDataElement( tcsq ) )
183 std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
184 SetDicomUptodateFlag(true);
187 const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
188 gdcm::SmartPointer<gdcm::SequenceOfItems> sqi2 = csq.GetValueAsSQ();
189 if( !sqi2 || !sqi2->GetNumberOfItems() )
192 unsigned int nitems = sqi2->GetNumberOfItems();
194 for(unsigned int i = 0; i < nitems; ++i)
196 const gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
197 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
200 mListOfContours.push_back(c);
203 SetDicomUptodateFlag(true);
207 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
209 // ROI number [Referenced ROI Number]
210 mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
213 mName = rois[mNumber];
215 // ROI Color [ROI Display Color]
216 mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
218 // Read contours [Contour Sequence]
219 gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
222 for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
223 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
226 mListOfContours.push_back(c);
232 std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
234 SetDicomUptodateFlag(true);
237 //--------------------------------------------------------------------
240 //--------------------------------------------------------------------
241 void clitk::DicomRT_ROI::SetImage(vvImage * image)
245 //--------------------------------------------------------------------
248 //--------------------------------------------------------------------
249 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
251 if (!mMeshIsUpToDate) {
256 //--------------------------------------------------------------------
259 //--------------------------------------------------------------------
260 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
262 return mListOfContours[n];
264 //--------------------------------------------------------------------
267 //--------------------------------------------------------------------
268 void clitk::DicomRT_ROI::ComputeMesh()
270 vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
271 for(unsigned int i=0; i<mListOfContours.size(); i++) {
272 append->AddInput(mListOfContours[i]->GetMesh());
276 mMesh = vtkSmartPointer<vtkPolyData>::New();
277 mMesh->DeepCopy(append->GetOutput());
278 mMeshIsUpToDate = true;
280 //--------------------------------------------------------------------
283 #if GDCM_MAJOR_VERSION == 2
285 //--------------------------------------------------------------------
286 void clitk::DicomRT_ROI::UpdateDicomItem()
288 if (GetDicomUptoDateFlag()) return;
289 DD("ROI::UpdateDicomItem");
292 // From now, only some item can be modified
294 // Set ROI Name 0x3006,0x26>
295 gdcm::Attribute<0x3006,0x26> roiname;
296 roiname.SetValue(GetName());
297 gdcm::DataElement de = roiname.GetAsDataElement();
298 gdcm::DataSet & ds = mItemInfo->GetNestedDataSet();
302 for(uint i=0; i<mListOfContours.size(); i++) {
304 DicomRT_Contour::Pointer roi = mListOfContours[i];
305 roi->UpdateDicomItem(mItemContour);
308 //--------------------------------------------------------------------
311 //--------------------------------------------------------------------
312 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage * image, int n,
314 std::vector<double> color,
315 std::string filename)
318 // ROI number [Referenced ROI Number]
323 mFilename = filename;
325 // ROI Color [ROI Display Color]
328 // No contours [Contour Sequence]
329 mListOfContours.clear();
334 //--------------------------------------------------------------------
337 //--------------------------------------------------------------------
338 vvImage * clitk::DicomRT_ROI::GetImage() const
342 //--------------------------------------------------------------------