]> Creatis software - clitk.git/blob - common/clitkImage2DicomRTStructFilter.txx
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[clitk.git] / common / clitkImage2DicomRTStructFilter.txx
1 /*=========================================================================
2   Program:         vv http://www.creatis.insa-lyon.fr/rio/vv
3   Main authors :   XX XX XX
4
5   Authors belongs to:
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
9
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.
13
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
17
18   =========================================================================*/
19
20 // std
21 #include <iterator>
22 #include <algorithm>
23
24 // clitk
25 #include "clitkImage2DicomRTStructFilter.h"
26 #include "clitkBinaryImageToMeshFilter.h"
27 #include "clitkImageCommon.h"
28 #include "vvFromITK.h"
29
30 // vtk
31 #include <vtkPolyDataToImageStencil.h>
32 #include <vtkSmartPointer.h>
33 #include <vtkImageStencil.h>
34 #include <vtkLinearExtrusionFilter.h>
35 #include <vtkMetaImageWriter.h>
36 #include <vtkImageData.h>
37
38 // FIXME to remove
39 #include "vtkPolyDataMapper.h"
40 #include "vtkPolyDataMapper2D.h"
41 #include "vtkRenderWindowInteractor.h"
42 #include "vtkPolyDataReader.h"
43 #include "vtkRenderWindow.h"
44 #include "vtkRenderer.h"
45 #include "vtkCamera.h"
46 #include "vtkProperty.h"
47 #include "vtkProperty2D.h"
48 #include <vtksys/SystemTools.hxx>
49
50 // itk
51 #include <itkImage.h>
52 #include <itkVTKImageToImageFilter.h>
53
54 // gdcm
55 #include <vtkGDCMPolyDataWriter.h>
56
57 //--------------------------------------------------------------------
58 template<class PixelType>
59 clitk::Image2DicomRTStructFilter<PixelType>::Image2DicomRTStructFilter()
60 {
61   m_StructureSetFilename = "";
62   m_DicomFolder = "";
63   m_OutputFilename = "default-output.dcm";
64   m_ThresholdValue = 0.5;
65 }
66 //--------------------------------------------------------------------
67
68
69 //--------------------------------------------------------------------
70 template<class PixelType>
71 clitk::Image2DicomRTStructFilter<PixelType>::~Image2DicomRTStructFilter()
72 {
73 }
74 //--------------------------------------------------------------------
75
76
77 //--------------------------------------------------------------------
78 template<class PixelType>
79 void
80 clitk::Image2DicomRTStructFilter<PixelType>::SetROIType(std::string type)
81 {
82   m_ROIType = type;
83 }
84 //--------------------------------------------------------------------
85
86
87 //--------------------------------------------------------------------
88 template<class PixelType>
89 void clitk::Image2DicomRTStructFilter<PixelType>::Update() 
90 {
91   // Check this is a RT-Struct
92   gdcm::Reader gdcm_reader;
93   gdcm_reader.SetFileName(m_StructureSetFilename.c_str());
94   if (!gdcm_reader.Read()) {
95     clitkExceptionMacro("Error could not open the file '" << m_StructureSetFilename << std::endl);
96   }
97   gdcm::MediaStorage ms;
98   ms.SetFromFile(gdcm_reader.GetFile());
99   if (ms != gdcm::MediaStorage::RTStructureSetStorage) {
100     clitkExceptionMacro("File '" << m_StructureSetFilename << "' is not a DICOM-RT-Struct file." << std::endl);
101   }
102
103   // Read rt struct
104   vtkSmartPointer<vtkGDCMPolyDataReader> reader = vtkGDCMPolyDataReader::New();
105   reader->SetFileName(m_StructureSetFilename.c_str());
106   reader->Update();  
107
108   // Get properties
109   vtkRTStructSetProperties * p = reader->GetRTStructSetProperties();
110   if (GetVerboseFlag()) {
111     std::cout << "Number of structures in the dicom-rt-struct : " 
112               << p->GetNumberOfStructureSetROIs() << std::endl;
113   }
114   
115
116   // number of additional contours
117   int m = m_InputFilenames.size();
118
119   // Init writer
120   vtkGDCMPolyDataWriter * writer = vtkGDCMPolyDataWriter::New();
121   int numMasks = reader->GetNumberOfOutputPorts() + m;
122   writer->SetNumberOfInputPorts(numMasks);    
123   writer->SetFileName(m_OutputFilename.c_str());
124   writer->SetMedicalImageProperties(reader->GetMedicalImageProperties());
125
126   // List of already present rois
127   vtkStringArray* roiNames = vtkStringArray::New();
128   vtkStringArray* roiAlgorithms = vtkStringArray::New();
129   vtkStringArray* roiTypes = vtkStringArray::New();
130   roiNames->SetNumberOfValues(numMasks);
131   roiAlgorithms->SetNumberOfValues(numMasks);
132   roiTypes->SetNumberOfValues(numMasks);
133   
134   // Convert the image into a mesh
135   std::vector<vtkSmartPointer<vtkPolyData> > meshes;
136   std::vector<std::string> m_ROINames;
137   meshes.resize(m);
138   m_ROINames.resize(m);
139   for(unsigned int i=0; i<m; i++) {
140     
141     // read image
142     //    typedef float PixelType;
143     //typedef itk::Image<PixelType, 3> ImageType;
144     ImagePointer input = clitk::readImage<ImageType>(m_InputFilenames[i], false);
145
146     std::ostringstream oss;
147     oss << vtksys::SystemTools::
148       GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(m_InputFilenames[i]));
149     std::string name = oss.str();
150     m_ROINames[i] = name;
151     
152     // convert to mesh
153     typedef clitk::BinaryImageToMeshFilter<ImageType> BinaryImageToMeshFilterType;
154     typename BinaryImageToMeshFilterType::Pointer convert = BinaryImageToMeshFilterType::New();
155     convert->SetThresholdValue(m_ThresholdValue);
156     convert->SetInput(input);
157     convert->Update();
158     meshes[i] = convert->GetOutputMesh();
159     if (GetVerboseFlag()) {
160       std::cout << "Mesh has " << meshes[i]->GetNumberOfLines() << " lines." << std::endl;
161     }
162     
163     /*
164     // debug mesh write  FIXME
165     vtkSmartPointer<vtkPolyDataWriter> wr = vtkSmartPointer<vtkPolyDataWriter>::New();
166     wr->SetInputConnection(convert->GetOutputPort()); //psurface->GetOutputPort()
167     wr->SetFileName("bidon.obj");
168     wr->Update();
169     wr->Write();
170     */
171   }
172     
173   // Copy previous contours
174   for (unsigned int i = 0; i < numMasks-m; ++i) {
175     writer->SetInput(i, reader->GetOutput(i));
176     std::string theString = reader->GetRTStructSetProperties()->GetStructureSetROIName(i);
177     roiNames->InsertValue(i, theString);
178     theString = reader->GetRTStructSetProperties()->GetStructureSetROIGenerationAlgorithm(i);
179     roiAlgorithms->InsertValue(i, theString);
180     theString = reader->GetRTStructSetProperties()->GetStructureSetRTROIInterpretedType(i);
181     roiTypes->InsertValue(i, theString);
182   }  
183
184   // Add new ones
185   for (unsigned int i = numMasks-m; i < numMasks; ++i) {
186     writer->SetInput(i, meshes[i-numMasks+m]);
187     roiNames->InsertValue(i, m_ROINames[i-numMasks+m]);
188     roiAlgorithms->InsertValue(i, "CLITK_CREATED");
189     roiTypes->InsertValue(i, m_ROIType);
190   }
191     
192   /*
193   //  Visu DEBUG
194   vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
195   cubeMapper->SetInput( mesh );
196   cubeMapper->SetScalarRange(0,7);
197   vtkActor *cubeActor = vtkActor::New();
198   cubeActor->SetMapper(cubeMapper);
199   vtkProperty * property = cubeActor->GetProperty();
200   property->SetRepresentationToWireframe();
201
202   vtkRenderer *renderer = vtkRenderer::New();
203   vtkRenderWindow *renWin = vtkRenderWindow::New();
204   renWin->AddRenderer(renderer);
205
206   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
207   iren->SetRenderWindow(renWin);
208
209   renderer->AddActor(cubeActor);
210   renderer->ResetCamera();
211   renderer->SetBackground(1,1,1);
212
213   renWin->SetSize(300,300);
214
215   renWin->Render();
216   iren->Start();
217   */
218   // End visu
219
220   // Write (need to read dicom for slice id)
221   vtkRTStructSetProperties* theProperties = vtkRTStructSetProperties::New();
222   writer->SetRTStructSetProperties(theProperties);
223   if (GetVerboseFlag()) {
224     std::cout << "Looking for dicom info, study instance "
225               << p->GetStudyInstanceUID() << std::endl;
226   }
227   writer->InitializeRTStructSet(m_DicomFolder,
228                                 reader->GetRTStructSetProperties()->GetStructureSetLabel(),
229                                 reader->GetRTStructSetProperties()->GetStructureSetName(),
230                                 roiNames, roiAlgorithms, roiTypes);  
231   writer->Write();
232   reader->Delete();
233   roiNames->Delete();
234   roiTypes->Delete();
235   roiAlgorithms->Delete();
236   writer->Delete();
237 }
238 //--------------------------------------------------------------------
239
240
241
242