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;
65 m_SkipInitialStructuresFlag = false;
67 //--------------------------------------------------------------------
70 //--------------------------------------------------------------------
71 template<class PixelType>
72 clitk::Image2DicomRTStructFilter<PixelType>::~Image2DicomRTStructFilter()
75 //--------------------------------------------------------------------
78 //--------------------------------------------------------------------
79 template<class PixelType>
81 clitk::Image2DicomRTStructFilter<PixelType>::SetROIType(std::string type)
85 //--------------------------------------------------------------------
88 //--------------------------------------------------------------------
89 template<class PixelType>
90 void clitk::Image2DicomRTStructFilter<PixelType>::Update()
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);
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);
105 vtkSmartPointer<vtkGDCMPolyDataReader> reader = vtkGDCMPolyDataReader::New();
106 reader->SetFileName(m_StructureSetFilename.c_str());
110 vtkRTStructSetProperties * p = reader->GetRTStructSetProperties();
111 if (GetVerboseFlag()) {
112 std::cout << "Number of structures in the dicom-rt-struct : "
113 << p->GetNumberOfStructureSetROIs() << std::endl;
117 // number of additional contours
118 int m = m_InputFilenames.size();
121 vtkGDCMPolyDataWriter * writer = vtkGDCMPolyDataWriter::New();
122 int numMasks = reader->GetNumberOfOutputPorts() + m;
124 if (m_SkipInitialStructuresFlag) {
128 writer->SetNumberOfInputPorts(numMasks);
129 writer->SetFileName(m_OutputFilename.c_str());
130 writer->SetMedicalImageProperties(reader->GetMedicalImageProperties());
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);
140 // Convert the image into a mesh
141 std::vector<vtkSmartPointer<vtkPolyData> > meshes;
142 std::vector<std::string> m_ROINames;
144 m_ROINames.resize(m);
145 for(unsigned int i=0; i<m; i++) {
148 // typedef float PixelType;
149 //typedef itk::Image<PixelType, 3> ImageType;
150 ImagePointer input = clitk::readImage<ImageType>(m_InputFilenames[i], false);
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;
159 typedef clitk::BinaryImageToMeshFilter<ImageType> BinaryImageToMeshFilterType;
160 typename BinaryImageToMeshFilterType::Pointer convert = BinaryImageToMeshFilterType::New();
161 convert->SetThresholdValue(m_ThresholdValue);
162 convert->SetInput(input);
164 meshes[i] = convert->GetOutputMesh();
165 if (GetVerboseFlag()) {
166 std::cout << "Mesh has " << meshes[i]->GetNumberOfLines() << " lines." << std::endl;
170 // debug mesh write FIXME
171 vtkSmartPointer<vtkPolyDataWriter> wr = vtkSmartPointer<vtkPolyDataWriter>::New();
172 wr->SetInputConnection(convert->GetOutputPort()); //psurface->GetOutputPort()
173 wr->SetFileName("bidon.obj");
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);
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);
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();
208 vtkRenderer *renderer = vtkRenderer::New();
209 vtkRenderWindow *renWin = vtkRenderWindow::New();
210 renWin->AddRenderer(renderer);
212 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
213 iren->SetRenderWindow(renWin);
215 renderer->AddActor(cubeActor);
216 renderer->ResetCamera();
217 renderer->SetBackground(1,1,1);
219 renWin->SetSize(300,300);
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;
233 writer->InitializeRTStructSet(m_DicomFolder,
234 reader->GetRTStructSetProperties()->GetStructureSetLabel(),
235 reader->GetRTStructSetProperties()->GetStructureSetName(),
236 roiNames, roiAlgorithms, roiTypes);
241 roiAlgorithms->Delete();
244 //--------------------------------------------------------------------