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>
26 #include <vtkVersion.h>
28 #if GDCM_MAJOR_VERSION == 2
29 #include "gdcmAttribute.h"
33 //--------------------------------------------------------------------
34 clitk::DicomRT_ROI::DicomRT_ROI()
40 mColor[0] = mColor[1] = mColor[2] = 0;
41 mMeshIsUpToDate = false;
44 SetDicomUptodateFlag(false);
47 //--------------------------------------------------------------------
50 //--------------------------------------------------------------------
51 clitk::DicomRT_ROI::~DicomRT_ROI()
54 //--------------------------------------------------------------------
57 //--------------------------------------------------------------------
58 void clitk::DicomRT_ROI::SetDisplayColor(double r, double v, double b)
65 //--------------------------------------------------------------------
68 //--------------------------------------------------------------------
69 int clitk::DicomRT_ROI::GetROINumber() const
73 //--------------------------------------------------------------------
76 //--------------------------------------------------------------------
77 const std::string & clitk::DicomRT_ROI::GetName() const
81 //--------------------------------------------------------------------
84 //--------------------------------------------------------------------
85 const std::string & clitk::DicomRT_ROI::GetFilename() const
89 //--------------------------------------------------------------------
92 //--------------------------------------------------------------------
93 const std::vector<double> & clitk::DicomRT_ROI::GetDisplayColor() const
97 //--------------------------------------------------------------------
100 //--------------------------------------------------------------------
101 void clitk::DicomRT_ROI::Print(std::ostream & os) const
103 os << "ROI " << mNumber << "\t" << mName
104 << "\t(" << mColor[0] << " " << mColor[1] << " " << mColor[2] << ")"
105 << "\t Contours = " << mListOfContours.size() << std::endl;
107 //--------------------------------------------------------------------
110 //--------------------------------------------------------------------
111 void clitk::DicomRT_ROI::SetBackgroundValueLabelImage(double bg)
113 mBackgroundValue = bg;
115 //--------------------------------------------------------------------
118 //--------------------------------------------------------------------
119 double clitk::DicomRT_ROI::GetBackgroundValueLabelImage() const
121 return mBackgroundValue;
123 //--------------------------------------------------------------------
126 //--------------------------------------------------------------------
127 void clitk::DicomRT_ROI::SetForegroundValueLabelImage(double bg)
129 mForegroundValue = bg;
131 //--------------------------------------------------------------------
134 //--------------------------------------------------------------------
135 double clitk::DicomRT_ROI::GetForegroundValueLabelImage() const
137 return mForegroundValue;
139 //--------------------------------------------------------------------
142 //--------------------------------------------------------------------
143 #if GDCM_MAJOR_VERSION == 2
144 bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour)
146 FATAL("Error : compile vv with itk4 + external gdcm");
148 mItemInfo = itemInfo;
149 mItemContour = itemContour;
152 // ROI number [Referenced ROI Number]
153 const gdcm::DataSet & nesteddsInfo = mItemInfo->GetNestedDataSet();
154 gdcm::Attribute<0x3006,0x0022> roinumber;
155 roinumber.SetFromDataSet( nesteddsInfo );
156 int nb1 = roinumber.GetValue();
158 // Check this is the same with the other item
159 const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
160 gdcm::Attribute<0x3006,0x0084> referencedroinumber;
161 referencedroinumber.SetFromDataSet( nestedds );
162 int nb2 = referencedroinumber.GetValue();
164 // Must never be different
168 FATAL("nb1 must equal nb2" << std::endl);
172 // Retrieve ROI Name (in the info item)
173 gdcm::Attribute<0x3006,0x26> roiname;
174 roiname.SetFromDataSet( nesteddsInfo );
175 mName = roiname.GetValue();
178 // ROI Color [ROI Display Color]
179 gdcm::Attribute<0x3006,0x002a> color = {};
180 color.SetFromDataSet( nestedds );
181 assert( color.GetNumberOfValues() == 3 );
182 mColor[0] = color.GetValue(0);
183 mColor[1] = color.GetValue(1);
184 mColor[2] = color.GetValue(2);
186 // Read contours [Contour Sequence]
187 gdcm::Tag tcsq(0x3006,0x0040);
188 if( !nestedds.FindDataElement( tcsq ) )
190 std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
191 SetDicomUptodateFlag(true);
194 const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
195 mContoursSequenceOfItems = csq.GetValueAsSQ();
196 gdcm::SmartPointer<gdcm::SequenceOfItems> & sqi2 = mContoursSequenceOfItems;
197 if( !sqi2 || !sqi2->GetNumberOfItems() )
200 unsigned int nitems = sqi2->GetNumberOfItems();
202 for(unsigned int i = 0; i < nitems; ++i)
204 gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
205 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
206 bool b = c->Read(&j);
208 mListOfContours.push_back(c);
211 SetDicomUptodateFlag(true);
215 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item)
217 // ROI number [Referenced ROI Number]
218 mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
221 mName = rois[mNumber];
223 // ROI Color [ROI Display Color]
224 mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
226 // Read contours [Contour Sequence]
227 gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
230 for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
231 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
234 mListOfContours.push_back(c);
240 std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
242 SetDicomUptodateFlag(true);
245 //--------------------------------------------------------------------
248 //--------------------------------------------------------------------
249 void clitk::DicomRT_ROI::SetImage(vvImage::Pointer image)
253 //--------------------------------------------------------------------
256 //--------------------------------------------------------------------
257 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
259 if (!mMeshIsUpToDate) {
260 ComputeMeshFromContour();
264 //--------------------------------------------------------------------
267 //--------------------------------------------------------------------
268 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
270 return mListOfContours[n];
272 //--------------------------------------------------------------------
275 //--------------------------------------------------------------------
276 void clitk::DicomRT_ROI::ComputeMeshFromContour()
278 vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
279 for(unsigned int i=0; i<mListOfContours.size(); i++) {
280 #if VTK_MAJOR_VERSION <= 5
281 append->AddInput(mListOfContours[i]->GetMesh());
283 append->AddInputData(mListOfContours[i]->GetMesh());
288 mMesh = vtkSmartPointer<vtkPolyData>::New();
289 mMesh->DeepCopy(append->GetOutput());
290 mMeshIsUpToDate = true;
292 //--------------------------------------------------------------------
295 #if GDCM_MAJOR_VERSION == 2
296 //--------------------------------------------------------------------
297 void clitk::DicomRT_ROI::UpdateDicomItem()
299 FATAL("Error : compile vv with itk4 + external gdcm");
301 if (GetDicomUptoDateFlag()) return;
302 DD("ROI::UpdateDicomItem");
305 // From now, only some item can be modified
307 // Set ROI Name 0x3006,0x26>
308 gdcm::Attribute<0x3006,0x26> roiname;
309 roiname.SetValue(GetName());
310 gdcm::DataElement de = roiname.GetAsDataElement();
311 gdcm::DataSet & ds = mItemInfo->GetNestedDataSet();
314 // From MESH to CONTOURS
315 ComputeContoursFromImage();
318 DD(mListOfContours.size());
319 for(uint i=0; i<mListOfContours.size(); i++) {
321 DicomRT_Contour::Pointer contour = mListOfContours[i];
322 contour->UpdateDicomItem();//mItemContour);
326 unsigned int nitems = mContoursSequenceOfItems->GetNumberOfItems();
329 // Write [Contour Sequence] = 0x3006,0x0040)
330 gdcm::DataSet & dsc = mItemContour->GetNestedDataSet();
331 gdcm::Tag tcsq(0x3006,0x0040);
332 const gdcm::DataElement& csq = dsc.GetDataElement( tcsq );
333 gdcm::DataElement dec(csq);
334 dec.SetValue(*mContoursSequenceOfItems);
337 gdcm::DataSet & a = mContoursSequenceOfItems->GetItem(1).GetNestedDataSet();
338 gdcm::Attribute<0x3006,0x0050> at;
339 gdcm::Tag tcontourdata(0x3006,0x0050);
340 gdcm::DataElement contourdata = a.GetDataElement( tcontourdata );
341 at.SetFromDataElement( contourdata );
342 const double* points = at.GetValues();
346 //--------------------------------------------------------------------
349 //--------------------------------------------------------------------
350 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage::Pointer image, int n,
352 std::vector<double> color,
353 std::string filename)
356 // ROI number [Referenced ROI Number]
361 mFilename = filename;
363 // ROI Color [ROI Display Color]
366 // No contours [Contour Sequence]
367 mListOfContours.clear();
372 //--------------------------------------------------------------------
375 //--------------------------------------------------------------------
376 vvImage * clitk::DicomRT_ROI::GetImage() const
380 //--------------------------------------------------------------------
383 //--------------------------------------------------------------------
384 void clitk::DicomRT_ROI::ComputeContoursFromImage()
386 FATAL("ComputeContoursFromImage should not be call. To be replace");
387 DD("ComputeMeshFromImage");
389 // Check that an image is loaded
392 // Only consider 3D here
393 if (mImage->GetNumberOfDimensions() != 3) {
394 FATAL("DicomRT_ROI::ComputeMeshFromImage only work with 3D images");
398 vtkImageData * image = mImage->GetVTKImages()[0];
400 // Get initial extend for the clipping
401 vtkSmartPointer<vtkImageClip> clipper = vtkSmartPointer<vtkImageClip>::New();
402 #if VTK_MAJOR_VERSION <= 5
403 clipper->SetInput(image);
405 clipper->SetInputData(image);
408 int* extent = image->GetExtent();
410 // std::vector<int> extend;
414 uint n = image->GetDimensions()[2];
416 DD(mListOfContours.size());
417 mListOfContours.resize(n); /// ???FIXME
418 DD(mListOfContours.size());
419 std::vector<vtkSmartPointer<vtkPolyData> > contours;
420 for(uint i=0; i<n; i++) {
423 // FIXME vtkDiscreteMarchingCubes INSTEAD
426 vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
427 #if VTK_MAJOR_VERSION <= 5
428 squares->SetInput(image);
430 squares->SetInputData(image);
432 squares->SetImageRange(extent[0], extent[1], extent[2], extent[3], i, i);
433 squares->SetValue(1, 1.0);
435 DD(squares->GetNumberOfContours());
437 //clitk::DicomRT_Contour * contour = new clitk::DicomRT_Contour();
438 //mListOfContours[i]->SetMesh(squares->GetOutput());
441 vtkSmartPointer<vtkPolyData> m = squares->GetOutput();
442 contours.push_back(m);
445 // Clip to the current slice
446 extent[4] = extent[5] = image->GetOrigin()[2]+i*image->GetSpacing()[2];
448 // Prepare the marching squares
449 vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
450 clipper->SetOutputWholeExtent(extent[0],extent[1],extent[2],
451 extent[3],extent[4],extent[5]);
453 squares->SetInput(clipper->GetOutput());
455 DD(squares->GetNumberOfContours());
456 mListOfContours[i]->SetMesh(squares->GetOutput());
461 vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
462 for(unsigned int i=0; i<n; i++) {
463 #if VTK_MAJOR_VERSION <= 5
464 append->AddInput(contours[i]);
466 append->AddInputData(contours[i]);
471 mMesh = vtkSmartPointer<vtkPolyData>::New();
472 mMesh->DeepCopy(append->GetOutput());
475 vtkPolyDataWriter * w = vtkPolyDataWriter::New();
476 #if VTK_MAJOR_VERSION <= 5
479 w->SetInputData(mMesh);
481 w->SetFileName("toto.vtk");
486 //--------------------------------------------------------------------
489 //--------------------------------------------------------------------
490 #if CLITK_USE_SYSTEM_GDCM == 1
491 void clitk::DicomRT_ROI::Read(vtkSmartPointer<vtkGDCMPolyDataReader> & reader, int roiindex)
493 vtkRTStructSetProperties * p = reader->GetRTStructSetProperties();
495 mName = p->GetStructureSetROIName(roiindex);
496 mNumber = p->GetStructureSetROINumber(roiindex);
498 //mColor = //FIXME !!
500 // gdcm::Attribute<0x3006,0x002a> color = {};
502 // const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
503 // color.SetFromDataSet( nestedds );
504 // assert( color.GetNumberOfValues() == 3 );
505 // mColor[0] = color.GetValue(0);
506 // mColor[1] = color.GetValue(1);
507 // mColor[2] = color.GetValue(2);
510 SetDicomUptodateFlag(true);
512 mMesh = reader->GetOutput(roiindex);
513 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
514 c->SetMesh(mMesh); // FIXME no GetZ, not GetPoints
515 mMeshIsUpToDate = true;
516 mListOfContours.push_back(c);
519 //--------------------------------------------------------------------