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 void clitk::DicomRT_ROI::SetROINumber(int number)
81 //--------------------------------------------------------------------
84 //--------------------------------------------------------------------
85 const std::string & clitk::DicomRT_ROI::GetName() const
89 //--------------------------------------------------------------------
92 //--------------------------------------------------------------------
93 const std::string & clitk::DicomRT_ROI::GetFilename() const
97 //--------------------------------------------------------------------
100 //--------------------------------------------------------------------
101 const std::vector<double> & clitk::DicomRT_ROI::GetDisplayColor() const
105 //--------------------------------------------------------------------
108 //--------------------------------------------------------------------
109 void clitk::DicomRT_ROI::Print(std::ostream & os) const
111 os << "ROI " << mNumber << "\t" << mName
112 << "\t(" << mColor[0] << " " << mColor[1] << " " << mColor[2] << ")"
113 << "\t Contours = " << mListOfContours.size() << std::endl;
115 //--------------------------------------------------------------------
118 //--------------------------------------------------------------------
119 void clitk::DicomRT_ROI::SetBackgroundValueLabelImage(double bg)
121 mBackgroundValue = bg;
123 //--------------------------------------------------------------------
126 //--------------------------------------------------------------------
127 double clitk::DicomRT_ROI::GetBackgroundValueLabelImage() const
129 return mBackgroundValue;
131 //--------------------------------------------------------------------
134 //--------------------------------------------------------------------
135 void clitk::DicomRT_ROI::SetForegroundValueLabelImage(double bg)
137 mForegroundValue = bg;
139 //--------------------------------------------------------------------
142 //--------------------------------------------------------------------
143 double clitk::DicomRT_ROI::GetForegroundValueLabelImage() const
145 return mForegroundValue;
147 //--------------------------------------------------------------------
150 //--------------------------------------------------------------------
151 #if GDCM_MAJOR_VERSION >= 2
152 bool clitk::DicomRT_ROI::Read(gdcm::Item * itemInfo, gdcm::Item * itemContour, double tol)
154 //FATAL("Error : compile vv with itk4 + external gdcm");
156 mItemInfo = itemInfo;
157 mItemContour = itemContour;
160 // ROI number [Referenced ROI Number]
161 const gdcm::DataSet & nesteddsInfo = mItemInfo->GetNestedDataSet();
162 gdcm::Attribute<0x3006,0x0022> roinumber;
163 roinumber.SetFromDataSet( nesteddsInfo );
164 int nb1 = roinumber.GetValue();
166 // Check this is the same with the other item
167 const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
168 gdcm::Attribute<0x3006,0x0084> referencedroinumber;
169 referencedroinumber.SetFromDataSet( nestedds );
170 int nb2 = referencedroinumber.GetValue();
172 // Must never be different
176 FATAL("nb1 must equal nb2" << std::endl);
180 // Retrieve ROI Name (in the info item)
181 gdcm::Attribute<0x3006,0x26> roiname;
182 roiname.SetFromDataSet( nesteddsInfo );
183 mName = roiname.GetValue();
186 // ROI Color [ROI Display Color]
187 gdcm::Attribute<0x3006,0x002a> color = {};
188 color.SetFromDataSet( nestedds );
189 assert( color.GetNumberOfValues() == 3 );
190 mColor[0] = color.GetValue(0);
191 mColor[1] = color.GetValue(1);
192 mColor[2] = color.GetValue(2);
194 // Read contours [Contour Sequence]
195 gdcm::Tag tcsq(0x3006,0x0040);
196 if( !nestedds.FindDataElement( tcsq ) )
198 std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
199 SetDicomUptodateFlag(true);
202 const gdcm::DataElement& csq = nestedds.GetDataElement( tcsq );
203 mContoursSequenceOfItems = csq.GetValueAsSQ();
204 gdcm::SmartPointer<gdcm::SequenceOfItems> & sqi2 = mContoursSequenceOfItems;
205 if( !sqi2 || !sqi2->GetNumberOfItems() )
208 unsigned int nitems = sqi2->GetNumberOfItems();
210 for(unsigned int i = 0; i < nitems; ++i)
212 gdcm::Item & j = sqi2->GetItem(i+1); // Item start at #1
213 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
214 c->SetTolerance(tol);
215 c->SetTransformMatrix(mTransformMatrix);
216 bool b = c->Read(&j);
218 mListOfContours.push_back(c);
221 SetDicomUptodateFlag(true);
225 void clitk::DicomRT_ROI::Read(std::map<int, std::string> & rois, gdcm::SQItem * item, double tol)
227 // ROI number [Referenced ROI Number]
228 mNumber = atoi(item->GetEntryValue(0x3006,0x0084).c_str());
231 mName = rois[mNumber];
233 // ROI Color [ROI Display Color]
234 mColor = clitk::parse_string<double>(item->GetEntryValue(0x3006,0x002a),'\\');
236 // Read contours [Contour Sequence]
237 gdcm::SeqEntry * contours=item->GetSeqEntry(0x3006,0x0040);
240 for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) {
241 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
242 c->SetTolerance(tol);
243 c->SetTransformMatrix(mTransformMatrix);
246 mListOfContours.push_back(c);
252 std::cerr << "Warning. Could not read contour for structure <" << mName << ">, number" << mNumber << " ? I ignore it" << std::endl;
254 SetDicomUptodateFlag(true);
257 //--------------------------------------------------------------------
260 //--------------------------------------------------------------------
261 void clitk::DicomRT_ROI::SetImage(vvImage::Pointer image)
265 //--------------------------------------------------------------------
268 //--------------------------------------------------------------------
269 vtkPolyData * clitk::DicomRT_ROI::GetMesh()
271 if (!mMeshIsUpToDate) {
272 ComputeMeshFromContour();
276 //--------------------------------------------------------------------
279 //--------------------------------------------------------------------
280 clitk::DicomRT_Contour * clitk::DicomRT_ROI::GetContour(int n)
282 return mListOfContours[n];
284 //--------------------------------------------------------------------
287 //--------------------------------------------------------------------
288 void clitk::DicomRT_ROI::ComputeMeshFromContour()
290 vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
291 for(unsigned int i=0; i<mListOfContours.size(); i++) {
292 #if VTK_MAJOR_VERSION <= 5
293 append->AddInput(mListOfContours[i]->GetMesh());
295 append->AddInputData(mListOfContours[i]->GetMesh());
300 mMesh = vtkSmartPointer<vtkPolyData>::New();
301 mMesh->DeepCopy(append->GetOutput());
302 mMeshIsUpToDate = true;
304 //--------------------------------------------------------------------
307 #if GDCM_MAJOR_VERSION >= 2
308 //--------------------------------------------------------------------
309 void clitk::DicomRT_ROI::UpdateDicomItem()
311 FATAL("Error : compile vv with itk4 + external gdcm");
313 if (GetDicomUptoDateFlag()) return;
314 DD("ROI::UpdateDicomItem");
317 // From now, only some item can be modified
319 // Set ROI Name 0x3006,0x26>
320 gdcm::Attribute<0x3006,0x26> roiname;
321 roiname.SetValue(GetName());
322 gdcm::DataElement de = roiname.GetAsDataElement();
323 gdcm::DataSet & ds = mItemInfo->GetNestedDataSet();
326 // From MESH to CONTOURS
327 ComputeContoursFromImage();
330 DD(mListOfContours.size());
331 for(uint i=0; i<mListOfContours.size(); i++) {
333 DicomRT_Contour::Pointer contour = mListOfContours[i];
334 contour->UpdateDicomItem();//mItemContour);
338 unsigned int nitems = mContoursSequenceOfItems->GetNumberOfItems();
341 // Write [Contour Sequence] = 0x3006,0x0040)
342 gdcm::DataSet & dsc = mItemContour->GetNestedDataSet();
343 gdcm::Tag tcsq(0x3006,0x0040);
344 const gdcm::DataElement& csq = dsc.GetDataElement( tcsq );
345 gdcm::DataElement dec(csq);
346 dec.SetValue(*mContoursSequenceOfItems);
349 gdcm::DataSet & a = mContoursSequenceOfItems->GetItem(1).GetNestedDataSet();
350 gdcm::Attribute<0x3006,0x0050> at;
351 gdcm::Tag tcontourdata(0x3006,0x0050);
352 gdcm::DataElement contourdata = a.GetDataElement( tcontourdata );
353 at.SetFromDataElement( contourdata );
354 const double* points = at.GetValues();
358 //--------------------------------------------------------------------
361 //--------------------------------------------------------------------
362 void clitk::DicomRT_ROI::SetFromBinaryImage(vvImage::Pointer image, int n,
364 std::vector<double> color,
365 std::string filename)
367 // ROI number [Referenced ROI Number]
372 mFilename = filename;
374 // ROI Color [ROI Display Color]
377 // No contours [Contour Sequence]
378 mListOfContours.clear();
383 //--------------------------------------------------------------------
386 //--------------------------------------------------------------------
387 vvImage * clitk::DicomRT_ROI::GetImage() const
391 //--------------------------------------------------------------------
394 //--------------------------------------------------------------------
395 void clitk::DicomRT_ROI::SetTransformMatrix(vtkMatrix4x4* matrix)
397 mTransformMatrix = matrix;
399 //--------------------------------------------------------------------
402 //--------------------------------------------------------------------
403 void clitk::DicomRT_ROI::ComputeContoursFromImage()
405 FATAL("ComputeContoursFromImage should not be call. To be replace");
406 DD("ComputeMeshFromImage");
408 // Check that an image is loaded
411 // Only consider 3D here
412 if (mImage->GetNumberOfDimensions() != 3) {
413 FATAL("DicomRT_ROI::ComputeMeshFromImage only work with 3D images");
417 vtkImageData * image = mImage->GetVTKImages()[0];
419 // Get initial extend for the clipping
420 vtkSmartPointer<vtkImageClip> clipper = vtkSmartPointer<vtkImageClip>::New();
421 #if VTK_MAJOR_VERSION <= 5
422 clipper->SetInput(image);
424 clipper->SetInputData(image);
427 int* extent = image->GetExtent();
429 // std::vector<int> extend;
433 uint n = image->GetDimensions()[2];
435 DD(mListOfContours.size());
436 mListOfContours.resize(n); /// ???FIXME
437 DD(mListOfContours.size());
438 std::vector<vtkSmartPointer<vtkPolyData> > contours;
439 for(uint i=0; i<n; i++) {
442 // FIXME vtkDiscreteMarchingCubes INSTEAD
445 vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
446 #if VTK_MAJOR_VERSION <= 5
447 squares->SetInput(image);
449 squares->SetInputData(image);
451 squares->SetImageRange(extent[0], extent[1], extent[2], extent[3], i, i);
452 squares->SetValue(1, 1.0);
454 DD(squares->GetNumberOfContours());
456 //clitk::DicomRT_Contour * contour = new clitk::DicomRT_Contour();
457 //mListOfContours[i]->SetMesh(squares->GetOutput());
460 vtkSmartPointer<vtkPolyData> m = squares->GetOutput();
461 contours.push_back(m);
464 // Clip to the current slice
465 extent[4] = extent[5] = image->GetOrigin()[2]+i*image->GetSpacing()[2];
467 // Prepare the marching squares
468 vtkSmartPointer<vtkMarchingSquares> squares = vtkSmartPointer<vtkMarchingSquares>::New();
469 clipper->SetOutputWholeExtent(extent[0],extent[1],extent[2],
470 extent[3],extent[4],extent[5]);
472 squares->SetInput(clipper->GetOutput());
474 DD(squares->GetNumberOfContours());
475 mListOfContours[i]->SetMesh(squares->GetOutput());
480 vtkSmartPointer<vtkAppendPolyData> append = vtkSmartPointer<vtkAppendPolyData>::New();
481 for(unsigned int i=0; i<n; i++) {
482 #if VTK_MAJOR_VERSION <= 5
483 append->AddInput(contours[i]);
485 append->AddInputData(contours[i]);
490 mMesh = vtkSmartPointer<vtkPolyData>::New();
491 mMesh->DeepCopy(append->GetOutput());
494 vtkPolyDataWriter * w = vtkPolyDataWriter::New();
495 #if VTK_MAJOR_VERSION <= 5
498 w->SetInputData(mMesh);
500 w->SetFileName("toto.vtk");
505 //--------------------------------------------------------------------
508 //--------------------------------------------------------------------
509 #if CLITK_USE_SYSTEM_GDCM == 1
510 void clitk::DicomRT_ROI::Read(vtkSmartPointer<vtkGDCMPolyDataReader> & reader, int roiindex, double tol)
512 vtkRTStructSetProperties * p = reader->GetRTStructSetProperties();
514 mName = p->GetStructureSetROIName(roiindex);
515 mNumber = p->GetStructureSetROINumber(roiindex);
517 //mColor = //FIXME !!
519 // gdcm::Attribute<0x3006,0x002a> color = {};
521 // const gdcm::DataSet & nestedds = mItemContour->GetNestedDataSet();
522 // color.SetFromDataSet( nestedds );
523 // assert( color.GetNumberOfValues() == 3 );
524 // mColor[0] = color.GetValue(0);
525 // mColor[1] = color.GetValue(1);
526 // mColor[2] = color.GetValue(2);
529 SetDicomUptodateFlag(true);
531 mMesh = reader->GetOutput(roiindex);
532 DicomRT_Contour::Pointer c = DicomRT_Contour::New();
533 c->SetTolerance(tol);
534 c->SetTransformMatrix(mTransformMatrix);
535 c->SetMesh(mMesh); // FIXME no GetZ, not GetPoints
536 mMeshIsUpToDate = true;
537 mListOfContours.push_back(c);
540 //--------------------------------------------------------------------