]> Creatis software - clitk.git/blob - common/clitkImage2DicomRTStructFilter.txx
77c5e07044f1684b502021b8196c13ec15c5c975
[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   m_SkipInitialStructuresFlag = false;
66 }
67 //--------------------------------------------------------------------
68
69
70 //--------------------------------------------------------------------
71 template<class PixelType>
72 clitk::Image2DicomRTStructFilter<PixelType>::~Image2DicomRTStructFilter()
73 {
74 }
75 //--------------------------------------------------------------------
76
77
78 //--------------------------------------------------------------------
79 template<class PixelType>
80 void
81 clitk::Image2DicomRTStructFilter<PixelType>::SetROIType(std::string type)
82 {
83   m_ROIType = type;
84 }
85 //--------------------------------------------------------------------
86
87
88 //--------------------------------------------------------------------
89 template<class PixelType>
90 void clitk::Image2DicomRTStructFilter<PixelType>::Update() 
91 {
92   // Check this is a RT-Struct
93   gdcm::Reader gdcm_reader;
94   gdcm_reader.SetFileName(m_StructureSetFilename.c_str());
95   if (!gdcm_reader.Read()) {
96     clitkExceptionMacro("Error could not open the file '" << m_StructureSetFilename << std::endl);
97   }
98   gdcm::MediaStorage ms;
99   ms.SetFromFile(gdcm_reader.GetFile());
100   if (ms != gdcm::MediaStorage::RTStructureSetStorage) {
101     clitkExceptionMacro("File '" << m_StructureSetFilename << "' is not a DICOM-RT-Struct file." << std::endl);
102   }
103
104   // Read rt struct
105   vtkSmartPointer<vtkGDCMPolyDataReader> reader = vtkGDCMPolyDataReader::New();
106   reader->SetFileName(m_StructureSetFilename.c_str());
107   reader->Update();  
108
109   // Get properties
110   vtkRTStructSetProperties * p = reader->GetRTStructSetProperties();
111   if (GetVerboseFlag()) {
112     std::cout << "Number of structures in the dicom-rt-struct : " 
113               << p->GetNumberOfStructureSetROIs() << std::endl;
114   }
115   
116
117   // number of additional contours
118   int m = m_InputFilenames.size();
119
120   // Init writer
121   vtkGDCMPolyDataWriter * writer = vtkGDCMPolyDataWriter::New();
122   int numMasks = reader->GetNumberOfOutputPorts() + m;
123
124   if (m_SkipInitialStructuresFlag) {
125     numMasks = m;
126   }
127
128   writer->SetNumberOfInputPorts(numMasks);    
129   writer->SetFileName(m_OutputFilename.c_str());
130   writer->SetMedicalImageProperties(reader->GetMedicalImageProperties());
131
132   // List of already present rois
133   vtkStringArray* roiNames = vtkStringArray::New();
134   vtkStringArray* roiAlgorithms = vtkStringArray::New();
135   vtkStringArray* roiTypes = vtkStringArray::New();
136   roiNames->SetNumberOfValues(numMasks);
137   roiAlgorithms->SetNumberOfValues(numMasks);
138   roiTypes->SetNumberOfValues(numMasks);
139   
140   // Convert the image into a mesh
141   std::vector<vtkSmartPointer<vtkPolyData> > meshes;
142   std::vector<std::string> m_ROINames;
143   meshes.resize(m);
144   m_ROINames.resize(m);
145   for(unsigned int i=0; i<m; i++) {
146     
147     // read image
148     //    typedef float PixelType;
149     //typedef itk::Image<PixelType, 3> ImageType;
150     ImagePointer input = clitk::readImage<ImageType>(m_InputFilenames[i], false);
151
152     std::ostringstream oss;
153     oss << vtksys::SystemTools::
154       GetFilenameName(vtksys::SystemTools::GetFilenameWithoutLastExtension(m_InputFilenames[i]));
155     std::string name = oss.str();
156     m_ROINames[i] = name;
157     
158     // convert to mesh
159     typedef clitk::BinaryImageToMeshFilter<ImageType> BinaryImageToMeshFilterType;
160     typename BinaryImageToMeshFilterType::Pointer convert = BinaryImageToMeshFilterType::New();
161     convert->SetThresholdValue(m_ThresholdValue);
162     convert->SetInput(input);
163     convert->Update();
164     meshes[i] = convert->GetOutputMesh();
165     if (GetVerboseFlag()) {
166       std::cout << "Mesh has " << meshes[i]->GetNumberOfLines() << " lines." << std::endl;
167     }
168     
169     /*
170     // debug mesh write  FIXME
171     vtkSmartPointer<vtkPolyDataWriter> wr = vtkSmartPointer<vtkPolyDataWriter>::New();
172     wr->SetInputConnection(convert->GetOutputPort()); //psurface->GetOutputPort()
173     wr->SetFileName("bidon.obj");
174     wr->Update();
175     wr->Write();
176     */
177   }
178     
179   // Copy previous contours
180   for (unsigned int i = 0; i < numMasks-m; ++i) {
181     writer->SetInput(i, reader->GetOutput(i));
182     std::string theString = reader->GetRTStructSetProperties()->GetStructureSetROIName(i);
183     roiNames->InsertValue(i, theString);
184     theString = reader->GetRTStructSetProperties()->GetStructureSetROIGenerationAlgorithm(i);
185     roiAlgorithms->InsertValue(i, theString);
186     theString = reader->GetRTStructSetProperties()->GetStructureSetRTROIInterpretedType(i);
187     roiTypes->InsertValue(i, theString);
188   }  
189
190   // Add new ones
191   for (unsigned int i = numMasks-m; i < numMasks; ++i) {
192     writer->SetInput(i, meshes[i-numMasks+m]);
193     roiNames->InsertValue(i, m_ROINames[i-numMasks+m]);
194     roiAlgorithms->InsertValue(i, "CLITK_CREATED");
195     roiTypes->InsertValue(i, m_ROIType);
196   }
197     
198   /*
199   //  Visu DEBUG
200   vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
201   cubeMapper->SetInput( mesh );
202   cubeMapper->SetScalarRange(0,7);
203   vtkActor *cubeActor = vtkActor::New();
204   cubeActor->SetMapper(cubeMapper);
205   vtkProperty * property = cubeActor->GetProperty();
206   property->SetRepresentationToWireframe();
207
208   vtkRenderer *renderer = vtkRenderer::New();
209   vtkRenderWindow *renWin = vtkRenderWindow::New();
210   renWin->AddRenderer(renderer);
211
212   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
213   iren->SetRenderWindow(renWin);
214
215   renderer->AddActor(cubeActor);
216   renderer->ResetCamera();
217   renderer->SetBackground(1,1,1);
218
219   renWin->SetSize(300,300);
220
221   renWin->Render();
222   iren->Start();
223   */
224   // End visu
225
226   // Write (need to read dicom for slice id)
227   vtkRTStructSetProperties* theProperties = vtkRTStructSetProperties::New();
228   writer->SetRTStructSetProperties(theProperties);
229   if (GetVerboseFlag()) {
230     std::cout << "Looking for dicom info, study instance "
231               << p->GetStudyInstanceUID() << std::endl;
232   }
233   writer->InitializeRTStructSet(m_DicomFolder,
234                                 reader->GetRTStructSetProperties()->GetStructureSetLabel(),
235                                 reader->GetRTStructSetProperties()->GetStructureSetName(),
236                                 roiNames, roiAlgorithms, roiTypes);  
237   writer->Write();
238   reader->Delete();
239   roiNames->Delete();
240   roiTypes->Delete();
241   roiAlgorithms->Delete();
242   writer->Delete();
243 }
244 //--------------------------------------------------------------------
245
246
247
248