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);
46 //--------------------------------------------------------------------
49 //--------------------------------------------------------------------
50 clitk::DicomRT_ROI::~DicomRT_ROI()
53 //--------------------------------------------------------------------
56 //--------------------------------------------------------------------
57 void clitk::DicomRT_ROI::SetDisplayColor(double r, double v, double b)
64 //--------------------------------------------------------------------
67 //--------------------------------------------------------------------
68 int clitk::DicomRT_ROI::GetROINumber() const
72 //--------------------------------------------------------------------
75 //--------------------------------------------------------------------
76 const std::string & clitk::DicomRT_ROI::GetName() const
80 //--------------------------------------------------------------------
83 //--------------------------------------------------------------------
84 const std::string & clitk::DicomRT_ROI::GetFilename() const
88 //--------------------------------------------------------------------
91 //--------------------------------------------------------------------
92 const std::vector<double> & clitk::DicomRT_ROI::GetDisplayColor() const
96 //--------------------------------------------------------------------
99 //--------------------------------------------------------------------
100 void clitk::DicomRT_ROI::Print(std::ostream & os) const
102 os << "ROI " << mNumber << "\t" << mName
103 << "\t(" << mColor[0] << " " << mColor[1] << " " << mColor[2] << ")"
104 << "\t Contours = " << mListOfContours.size() << std::endl;
106 //--------------------------------------------------------------------
109 //--------------------------------------------------------------------
110 void clitk::DicomRT_ROI::SetBackgroundValueLabelImage(double bg)
112 mBackgroundValue = bg;
114 //--------------------------------------------------------------------
117 //--------------------------------------------------------------------
118 double clitk::DicomRT_ROI::GetBackgroundValueLabelImage() const
120 return mBackgroundValue;
122 //--------------------------------------------------------------------
125 //--------------------------------------------------------------------
126 void clitk::DicomRT_ROI::SetForegroundValueLabelImage(double bg)
128 mForegroundValue = bg;
130 //--------------------------------------------------------------------
133 //--------------------------------------------------------------------
134 double clitk::DicomRT_ROI::GetForegroundValueLabelImage() const
136 return mForegroundValue;
138 //--------------------------------------------------------------------
141 //--------------------------------------------------------------------
142 #if GDCM_MAJOR_VERSION == 2
143 bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour)
145 FATAL("Error : compile vv with itk4 + external gdcm");
147 mItemInfo = itemInfo;
148 mItemContour = itemContour;
151 // ROI number [Referenced ROI Number]
152 const gdcm::DataSet & nesteddsInfo = mItemInfo->GetNestedDataSet();
153 gdcm::Attribute<0x3006,0x0022> roinumber;
154 roinumber.SetFromDataSet( nesteddsInfo );
155 int nb1 = roinumber.GetValue();
157 // Check this is the same with the other item
158 const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
159 gdcm::Attribute<0x3006,0x0084> referencedroinumber;
160 referencedroinumber.SetFromDataSet( nestedds );
161 int nb2 = referencedroinumber.GetValue();
163 // Must never be different
167 FATAL("nb1 must equal nb2" << std::endl);
171 // Retrieve ROI Name (in the info item)
172 gdcm::Attribute<0x3006,0x26> roiname;
173 roiname.SetFromDataSet( nesteddsInfo );
174 mName = roiname.GetValue();
177 // ROI Color [ROI Display Color]
178 gdcm::Attribute<0x3006,0x002a> color = {};
179 color.SetFromDataSet( nestedds );
180 assert( color.GetNumberOfValues() == 3 );
181 mColor[0] = color.GetValue(0);
182 mColor[1] = color.GetValue(1);
183 mColor[2] = color.GetValue(2);
185 // Read contours [Contour Sequence]
186 gdcm::Tag tcsq(0x3006,0x0040);
187 if( !nestedds.FindDataElement( tcsq ) )
189 std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
190 SetDicomUptodateFlag(true);
193 const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
194 mContoursSequenceOfItems = csq.GetValueAsSQ();
195 gdcm::SmartPointer<gdcm::SequenceOfItems> & sqi2 = mContoursSequenceOfItems;
196 if( !sqi2 || !sqi2->GetNumberOfItems() )
199 unsigned int nitems = sqi2->GetNumberOfItems();
201 for(unsigned int i = 0; i < nitems; ++i)
203 gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
204 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
205 bool b = c->Read(&j);
207 mListOfContours.push_back(c);
210 SetDicomUptodateFlag(true);
214 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
216 // ROI number [Referenced ROI Number]
217 mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
220 mName = rois[mNumber];
222 // ROI Color [ROI Display Color]
223 mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
225 // Read contours [Contour Sequence]
226 gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
229 for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
230 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
233 mListOfContours.push_back(c);
239 std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
241 SetDicomUptodateFlag(true);
244 //--------------------------------------------------------------------
247 //--------------------------------------------------------------------
248 void clitk::DicomRT_ROI::SetImage(vvImage::Pointer image)
252 //--------------------------------------------------------------------
255 //--------------------------------------------------------------------
256 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
258 if (!mMeshIsUpToDate) {
259 ComputeMeshFromContour();
263 //--------------------------------------------------------------------
266 //--------------------------------------------------------------------
267 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
269 return mListOfContours[n];
271 //--------------------------------------------------------------------
274 //--------------------------------------------------------------------
275 void clitk::DicomRT_ROI::ComputeMeshFromContour()
277 vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
278 for(unsigned int i=0; i<mListOfContours.size(); i++) {
279 append->AddInput(mListOfContours[i]->GetMesh());
283 mMesh = vtkSmartPointer<vtkPolyData>::New();
284 mMesh->DeepCopy(append->GetOutput());
285 mMeshIsUpToDate = true;
287 //--------------------------------------------------------------------
290 #if GDCM_MAJOR_VERSION == 2
291 //--------------------------------------------------------------------
292 void clitk::DicomRT_ROI::UpdateDicomItem()
294 FATAL("Error : compile vv with itk4 + external gdcm");
296 if (GetDicomUptoDateFlag()) return;
297 DD("ROI::UpdateDicomItem");
300 // From now, only some item can be modified
302 // Set ROI Name 0x3006,0x26>
303 gdcm::Attribute<0x3006,0x26> roiname;
304 roiname.SetValue(GetName());
305 gdcm::DataElement de = roiname.GetAsDataElement();
306 gdcm::DataSet & ds = mItemInfo->GetNestedDataSet();
309 // From MESH to CONTOURS
310 ComputeContoursFromImage();
313 DD(mListOfContours.size());
314 for(uint i=0; i<mListOfContours.size(); i++) {
316 DicomRT_Contour::Pointer contour = mListOfContours[i];
317 contour->UpdateDicomItem();//mItemContour);
321 unsigned int nitems = mContoursSequenceOfItems->GetNumberOfItems();
324 // Write [Contour Sequence] = 0x3006,0x0040)
325 gdcm::DataSet & dsc = mItemContour->GetNestedDataSet();
326 gdcm::Tag tcsq(0x3006,0x0040);
327 const gdcm::DataElement& csq = dsc.GetDataElement( tcsq );
328 gdcm::DataElement dec(csq);
329 dec.SetValue(*mContoursSequenceOfItems);
332 gdcm::DataSet & a = mContoursSequenceOfItems->GetItem(1).GetNestedDataSet();
333 gdcm::Attribute<0x3006,0x0050> at;
334 gdcm::Tag tcontourdata(0x3006,0x0050);
335 gdcm::DataElement contourdata = a.GetDataElement( tcontourdata );
336 at.SetFromDataElement( contourdata );
337 const double* points = at.GetValues();
341 //--------------------------------------------------------------------
344 //--------------------------------------------------------------------
345 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage::Pointer image, int n,
347 std::vector<double> color,
348 std::string filename)
351 // ROI number [Referenced ROI Number]
356 mFilename = filename;
358 // ROI Color [ROI Display Color]
361 // No contours [Contour Sequence]
362 mListOfContours.clear();
367 //--------------------------------------------------------------------
370 //--------------------------------------------------------------------
371 vvImage * clitk::DicomRT_ROI::GetImage() const
375 //--------------------------------------------------------------------
378 //--------------------------------------------------------------------
379 void clitk::DicomRT_ROI::ComputeContoursFromImage()
381 FATAL("ComputeContoursFromImage should not be call. To be replace");
382 DD("ComputeMeshFromImage");
384 // Check that an image is loaded
387 // Only consider 3D here
388 if (mImage->GetNumberOfDimensions() != 3) {
389 FATAL("DicomRT_ROI::ComputeMeshFromImage only work with 3D images");
393 vtkImageData * image = mImage->GetVTKImages()[0];
395 // Get initial extend for the clipping
396 vtkSmartPointer<vtkImageClip> clipper = vtkSmartPointer<vtkImageClip>::New();
397 clipper->SetInput(image);
398 int* extent = image->GetExtent();
400 // std::vector<int> extend;
404 uint n = image->GetDimensions()[2];
406 DD(mListOfContours.size());
407 mListOfContours.resize(n); /// ???FIXME
408 DD(mListOfContours.size());
409 std::vector<vtkSmartPointer<vtkPolyData> > contours;
410 for(uint i=0; i<n; i++) {
413 // FIXME vtkDiscreteMarchingCubes INSTEAD
416 vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
417 squares->SetInput(image);
418 squares->SetImageRange(extent[0], extent[1], extent[2], extent[3], i, i);
419 squares->SetValue(1, 1.0);
421 DD(squares->GetNumberOfContours());
423 //clitk::DicomRT_Contour * contour = new clitk::DicomRT_Contour();
424 //mListOfContours[i]->SetMesh(squares->GetOutput());
427 vtkSmartPointer<vtkPolyData> m = squares->GetOutput();
428 contours.push_back(m);
431 // Clip to the current slice
432 extent[4] = extent[5] = image->GetOrigin()[2]+i*image->GetSpacing()[2];
434 // Prepare the marching squares
435 vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
436 clipper->SetOutputWholeExtent(extent[0],extent[1],extent[2],
437 extent[3],extent[4],extent[5]);
439 squares->SetInput(clipper->GetOutput());
441 DD(squares->GetNumberOfContours());
442 mListOfContours[i]->SetMesh(squares->GetOutput());
447 vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
448 for(unsigned int i=0; i<n; i++) {
449 append->AddInput(contours[i]);
453 mMesh = vtkSmartPointer<vtkPolyData>::New();
454 mMesh->DeepCopy(append->GetOutput());
457 vtkPolyDataWriter * w = vtkPolyDataWriter::New();
459 w->SetFileName("toto.vtk");
464 //--------------------------------------------------------------------
467 //--------------------------------------------------------------------
468 #if CLITK_USE_SYSTEM_GDCM == 1
469 void clitk::DicomRT_ROI::Read(vtkSmartPointer<vtkGDCMPolyDataReader> & reader, int roiindex)
471 vtkRTStructSetProperties * p = reader->GetRTStructSetProperties();
473 mName = p->GetStructureSetROIName(roiindex);
474 mNumber = p->GetStructureSetROINumber(roiindex);
476 //mColor = //FIXME !!
478 // gdcm::Attribute<0x3006,0x002a> color = {};
480 // const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
481 // color.SetFromDataSet( nestedds );
482 // assert( color.GetNumberOfValues() == 3 );
483 // mColor[0] = color.GetValue(0);
484 // mColor[1] = color.GetValue(1);
485 // mColor[2] = color.GetValue(2);
488 SetDicomUptodateFlag(true);
490 mMesh = reader->GetOutput(roiindex);
491 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
492 c->SetMesh(mMesh); // FIXME no GetZ, not GetPoints
493 mMeshIsUpToDate = true;
494 mListOfContours.push_back(c);
497 //--------------------------------------------------------------------