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>
23 #include <vtkImageClip.h>
24 #include <vtkMarchingSquares.h>
25 #include <vtkPolyDataWriter.h>
27 #if GDCM_MAJOR_VERSION == 2
28 #include "gdcmAttribute.h"
32 //--------------------------------------------------------------------
33 clitk::DicomRT_ROI::DicomRT_ROI()
39 mColor[0] = mColor[1] = mColor[2] = 0;
40 mMeshIsUpToDate = false;
43 SetDicomUptodateFlag(false);
45 //--------------------------------------------------------------------
48 //--------------------------------------------------------------------
49 clitk::DicomRT_ROI::~DicomRT_ROI()
52 //--------------------------------------------------------------------
55 //--------------------------------------------------------------------
56 void clitk::DicomRT_ROI::SetDisplayColor(double r, double v, double b)
63 //--------------------------------------------------------------------
66 //--------------------------------------------------------------------
67 int clitk::DicomRT_ROI::GetROINumber() const
71 //--------------------------------------------------------------------
74 //--------------------------------------------------------------------
75 const std::string & clitk::DicomRT_ROI::GetName() const
79 //--------------------------------------------------------------------
82 //--------------------------------------------------------------------
83 const std::string & clitk::DicomRT_ROI::GetFilename() const
87 //--------------------------------------------------------------------
90 //--------------------------------------------------------------------
91 const std::vector<double> & clitk::DicomRT_ROI::GetDisplayColor() const
95 //--------------------------------------------------------------------
98 //--------------------------------------------------------------------
99 void clitk::DicomRT_ROI::Print(std::ostream & os) const
101 os << "ROI " << mNumber << "\t" << mName
102 << "\t(" << mColor[0] << " " << mColor[1] << " " << mColor[2] << ")"
103 << "\t Contours = " << mListOfContours.size() << std::endl;
105 //--------------------------------------------------------------------
108 //--------------------------------------------------------------------
109 void clitk::DicomRT_ROI::SetBackgroundValueLabelImage(double bg)
111 mBackgroundValue = bg;
113 //--------------------------------------------------------------------
116 //--------------------------------------------------------------------
117 double clitk::DicomRT_ROI::GetBackgroundValueLabelImage() const
119 return mBackgroundValue;
121 //--------------------------------------------------------------------
124 //--------------------------------------------------------------------
125 void clitk::DicomRT_ROI::SetForegroundValueLabelImage(double bg)
127 mForegroundValue = bg;
129 //--------------------------------------------------------------------
132 //--------------------------------------------------------------------
133 double clitk::DicomRT_ROI::GetForegroundValueLabelImage() const
135 return mForegroundValue;
137 //--------------------------------------------------------------------
140 //--------------------------------------------------------------------
141 #if GDCM_MAJOR_VERSION == 2
142 bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour)
145 mItemInfo = itemInfo;
146 mItemContour = itemContour;
149 // ROI number [Referenced ROI Number]
150 const gdcm::DataSet & nesteddsInfo = mItemInfo->GetNestedDataSet();
151 gdcm::Attribute<0x3006,0x0022> roinumber;
152 roinumber.SetFromDataSet( nesteddsInfo );
153 int nb1 = roinumber.GetValue();
155 // Check this is the same with the other item
156 const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
157 gdcm::Attribute<0x3006,0x0084> referencedroinumber;
158 referencedroinumber.SetFromDataSet( nestedds );
159 int nb2 = referencedroinumber.GetValue();
161 // Must never be different
165 FATAL("nb1 must equal nb2" << std::endl);
169 // Retrieve ROI Name (in the info item)
170 gdcm::Attribute<0x3006,0x26> roiname;
171 roiname.SetFromDataSet( nesteddsInfo );
172 mName = roiname.GetValue();
175 // ROI Color [ROI Display Color]
176 gdcm::Attribute<0x3006,0x002a> color = {};
177 color.SetFromDataSet( nestedds );
178 assert( color.GetNumberOfValues() == 3 );
179 mColor[0] = color.GetValue(0);
180 mColor[1] = color.GetValue(1);
181 mColor[2] = color.GetValue(2);
183 // Read contours [Contour Sequence]
184 gdcm::Tag tcsq(0x3006,0x0040);
185 if( !nestedds.FindDataElement( tcsq ) )
187 std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
188 SetDicomUptodateFlag(true);
191 const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
192 mContoursSequenceOfItems = csq.GetValueAsSQ();
193 gdcm::SmartPointer<gdcm::SequenceOfItems> & sqi2 = mContoursSequenceOfItems;
194 if( !sqi2 || !sqi2->GetNumberOfItems() )
197 unsigned int nitems = sqi2->GetNumberOfItems();
199 for(unsigned int i = 0; i < nitems; ++i)
201 gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
202 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
203 bool b = c->Read(&j);
205 mListOfContours.push_back(c);
208 SetDicomUptodateFlag(true);
212 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
214 // ROI number [Referenced ROI Number]
215 mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
218 mName = rois[mNumber];
220 // ROI Color [ROI Display Color]
221 mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
223 // Read contours [Contour Sequence]
224 gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
227 for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
228 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
231 mListOfContours.push_back(c);
237 std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
239 SetDicomUptodateFlag(true);
242 //--------------------------------------------------------------------
245 //--------------------------------------------------------------------
246 void clitk::DicomRT_ROI::SetImage(vvImage::Pointer image)
250 //--------------------------------------------------------------------
253 //--------------------------------------------------------------------
254 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
256 if (!mMeshIsUpToDate) {
257 ComputeMeshFromContour();
261 //--------------------------------------------------------------------
264 //--------------------------------------------------------------------
265 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
267 return mListOfContours[n];
269 //--------------------------------------------------------------------
272 //--------------------------------------------------------------------
273 void clitk::DicomRT_ROI::ComputeMeshFromContour()
275 vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
276 for(unsigned int i=0; i<mListOfContours.size(); i++) {
277 append->AddInput(mListOfContours[i]->GetMesh());
281 mMesh = vtkSmartPointer<vtkPolyData>::New();
282 mMesh->DeepCopy(append->GetOutput());
283 mMeshIsUpToDate = true;
285 //--------------------------------------------------------------------
288 #if GDCM_MAJOR_VERSION == 2
290 //--------------------------------------------------------------------
291 void clitk::DicomRT_ROI::UpdateDicomItem()
293 if (GetDicomUptoDateFlag()) return;
294 DD("ROI::UpdateDicomItem");
297 // From now, only some item can be modified
299 // Set ROI Name 0x3006,0x26>
300 gdcm::Attribute<0x3006,0x26> roiname;
301 roiname.SetValue(GetName());
302 gdcm::DataElement de = roiname.GetAsDataElement();
303 gdcm::DataSet & ds = mItemInfo->GetNestedDataSet();
306 // From MESH to CONTOURS
307 ComputeContoursFromImage();
310 DD(mListOfContours.size());
311 for(uint i=0; i<mListOfContours.size(); i++) {
313 DicomRT_Contour::Pointer contour = mListOfContours[i];
314 contour->UpdateDicomItem();//mItemContour);
318 unsigned int nitems = mContoursSequenceOfItems->GetNumberOfItems();
321 // Write [Contour Sequence] = 0x3006,0x0040)
322 gdcm::DataSet & dsc = mItemContour->GetNestedDataSet();
323 gdcm::Tag tcsq(0x3006,0x0040);
324 const gdcm::DataElement& csq = dsc.GetDataElement( tcsq );
325 gdcm::DataElement dec(csq);
326 dec.SetValue(*mContoursSequenceOfItems);
329 gdcm::DataSet & a = mContoursSequenceOfItems->GetItem(1).GetNestedDataSet();
330 gdcm::Attribute<0x3006,0x0050> at;
331 gdcm::Tag tcontourdata(0x3006,0x0050);
332 gdcm::DataElement contourdata = a.GetDataElement( tcontourdata );
333 at.SetFromDataElement( contourdata );
334 const double* points = at.GetValues();
338 //--------------------------------------------------------------------
341 //--------------------------------------------------------------------
342 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage::Pointer image, int n,
344 std::vector<double> color,
345 std::string filename)
348 // ROI number [Referenced ROI Number]
353 mFilename = filename;
355 // ROI Color [ROI Display Color]
358 // No contours [Contour Sequence]
359 mListOfContours.clear();
364 //--------------------------------------------------------------------
367 //--------------------------------------------------------------------
368 vvImage * clitk::DicomRT_ROI::GetImage() const
372 //--------------------------------------------------------------------
375 //--------------------------------------------------------------------
376 void clitk::DicomRT_ROI::ComputeContoursFromImage()
378 DD("ComputeMeshFromImage");
380 // Check that an image is loaded
383 // Only consider 3D here
384 if (mImage->GetNumberOfDimensions() != 3) {
385 FATAL("DicomRT_ROI::ComputeMeshFromImage only work with 3D images");
389 vtkImageData * image = mImage->GetVTKImages()[0];
391 // Get initial extend for the clipping
392 vtkSmartPointer<vtkImageClip> clipper = vtkSmartPointer<vtkImageClip>::New();
393 clipper->SetInput(image);
394 int* extent = image->GetExtent();
396 // std::vector<int> extend;
400 uint n = image->GetDimensions()[2];
402 DD(mListOfContours.size());
403 mListOfContours.resize(n); /// ???FIXME
404 DD(mListOfContours.size());
405 std::vector<vtkSmartPointer<vtkPolyData> > contours;
406 for(uint i=0; i<n; i++) {
409 // FIXME vtkDiscreteMarchingCubes INSTEAD
412 vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
413 squares->SetInput(image);
414 squares->SetImageRange(extent[0], extent[1], extent[2], extent[3], i, i);
415 squares->SetValue(1, 1.0);
417 DD(squares->GetNumberOfContours());
419 //clitk::DicomRT_Contour * contour = new clitk::DicomRT_Contour();
420 //mListOfContours[i]->SetMesh(squares->GetOutput());
423 vtkSmartPointer<vtkPolyData> m = squares->GetOutput();
424 contours.push_back(m);
427 // Clip to the current slice
428 extent[4] = extent[5] = image->GetOrigin()[2]+i*image->GetSpacing()[2];
430 // Prepare the marching squares
431 vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
432 clipper->SetOutputWholeExtent(extent[0],extent[1],extent[2],
433 extent[3],extent[4],extent[5]);
435 squares->SetInput(clipper->GetOutput());
437 DD(squares->GetNumberOfContours());
438 mListOfContours[i]->SetMesh(squares->GetOutput());
443 vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
444 for(unsigned int i=0; i<n; i++) {
445 append->AddInput(contours[i]);
449 mMesh = vtkSmartPointer<vtkPolyData>::New();
450 mMesh->DeepCopy(append->GetOutput());
453 vtkPolyDataWriter * w = vtkPolyDataWriter::New();
455 w->SetFileName("toto.vtk");
460 //--------------------------------------------------------------------
463 //--------------------------------------------------------------------
464 #if CLITK_USE_SYSTEM_GDCM == 1
465 void clitk::DicomRT_ROI::Read(vtkSmartPointer<vtkGDCMPolyDataReader> & reader, int roiindex)
467 vtkRTStructSetProperties * p = reader->GetRTStructSetProperties();
469 mName = p->GetStructureSetROIName(roiindex);
470 mNumber = p->GetStructureSetROINumber(roiindex);
471 //mColor = //FIXME !!
472 SetDicomUptodateFlag(true);
474 mMesh = reader->GetOutput(roiindex);
475 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
476 c->SetMesh(mMesh); // FIXME no GetZ, not GetPoints
477 mMeshIsUpToDate = true;
478 mListOfContours.push_back(c);
481 //--------------------------------------------------------------------