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 =========================================================================*/
25 #include "clitkImage2DicomRTStructFilter.h"
26 #include "clitkBinaryImageToMeshFilter.h"
27 #include "clitkImageCommon.h"
28 #include "vvFromITK.h"
31 #include <vtkPolyDataToImageStencil.h>
32 #include <vtkSmartPointer.h>
33 #include <vtkImageStencil.h>
34 #include <vtkLinearExtrusionFilter.h>
35 #include <vtkMetaImageWriter.h>
36 #include <vtkImageData.h>
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>
52 #include <itkVTKImageToImageFilter.h>
55 #include <vtkGDCMPolyDataWriter.h>
57 //--------------------------------------------------------------------
58 template<class PixelType>
59 clitk::Image2DicomRTStructFilter<PixelType>::Image2DicomRTStructFilter()
61 m_StructureSetFilename = "";
63 m_OutputFilename = "default-output.dcm";
64 m_ThresholdValue = 0.5;
66 //--------------------------------------------------------------------
69 //--------------------------------------------------------------------
70 template<class PixelType>
71 clitk::Image2DicomRTStructFilter<PixelType>::~Image2DicomRTStructFilter()
74 //--------------------------------------------------------------------
77 //--------------------------------------------------------------------
78 template<class PixelType>
80 clitk::Image2DicomRTStructFilter<PixelType>::SetROIType(std::string type)
84 //--------------------------------------------------------------------
87 //--------------------------------------------------------------------
88 template<class PixelType>
89 void clitk::Image2DicomRTStructFilter<PixelType>::Update()
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);
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);
104 vtkSmartPointer<vtkGDCMPolyDataReader> reader = vtkGDCMPolyDataReader::New();
105 reader->SetFileName(m_StructureSetFilename.c_str());
109 vtkRTStructSetProperties * p = reader->GetRTStructSetProperties();
110 if (GetVerboseFlag()) {
111 std::cout << "Number of structures in the dicom-rt-struct : "
112 << p->GetNumberOfStructureSetROIs() << std::endl;
116 // number of additional contours
117 int m = m_InputFilenames.size();
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());
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);
134 // Convert the image into a mesh
135 std::vector<vtkSmartPointer<vtkPolyData> > meshes;
136 std::vector<std::string> m_ROINames;
138 m_ROINames.resize(m);
139 for(unsigned int i=0; i<m; i++) {
142 // typedef float PixelType;
143 //typedef itk::Image<PixelType, 3> ImageType;
144 ImagePointer input = clitk::readImage<ImageType>(m_InputFilenames[i], false);
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;
153 typedef clitk::BinaryImageToMeshFilter<ImageType> BinaryImageToMeshFilterType;
154 typename BinaryImageToMeshFilterType::Pointer convert = BinaryImageToMeshFilterType::New();
155 convert->SetThresholdValue(m_ThresholdValue);
156 convert->SetInput(input);
158 meshes[i] = convert->GetOutputMesh();
159 if (GetVerboseFlag()) {
160 std::cout << "Mesh has " << meshes[i]->GetNumberOfLines() << " lines." << std::endl;
164 // debug mesh write FIXME
165 vtkSmartPointer<vtkPolyDataWriter> wr = vtkSmartPointer<vtkPolyDataWriter>::New();
166 wr->SetInputConnection(convert->GetOutputPort()); //psurface->GetOutputPort()
167 wr->SetFileName("bidon.obj");
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);
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);
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();
202 vtkRenderer *renderer = vtkRenderer::New();
203 vtkRenderWindow *renWin = vtkRenderWindow::New();
204 renWin->AddRenderer(renderer);
206 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
207 iren->SetRenderWindow(renWin);
209 renderer->AddActor(cubeActor);
210 renderer->ResetCamera();
211 renderer->SetBackground(1,1,1);
213 renWin->SetSize(300,300);
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;
227 writer->InitializeRTStructSet(m_DicomFolder,
228 reader->GetRTStructSetProperties()->GetStructureSetLabel(),
229 reader->GetRTStructSetProperties()->GetStructureSetName(),
230 roiNames, roiAlgorithms, roiTypes);
235 roiAlgorithms->Delete();
238 //--------------------------------------------------------------------