1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
20 #include <QApplication>
23 #include <gdcmSQItem.h>
25 #include <vtkSmartPointer.h>
26 #include <vtkAppendPolyData.h>
27 #include <vtkCellArray.h>
28 #include <vtkMetaImageWriter.h>
29 #include <vtkMetaImageReader.h>
30 #include <vtkPolyDataWriter.h>
31 #include <vtkImageData.h>
32 #include <vtkImageCast.h>
33 #include <vtkImageGaussianSmooth.h>
35 #include "clitkCommon.h"
36 #include "vvMeshReader.h"
37 #include "vvProgressDialog.h"
38 #include <clitkCommon.h>
40 vvMeshReader::vvMeshReader() :
44 void vvMeshReader::Update()
46 //Show a progress bar only when opening a DC-struct (ie. multiple contours)
47 vvProgressDialog progress("Opening " + filename,(!vtk_mode) && (selected_contours.size()>1));
49 while (this->isRunning()) {
50 progress.SetProgress(result.size(),selected_contours.size());
52 qApp->processEvents();
56 void vvMeshReader::run()
58 ///Verify the important stuff has been set
59 assert(filename != "");
60 if (vtk_mode) { //Read vtkPolyData
61 vvMesh::Pointer m=vvMesh::New();
62 m->ReadFromVTK(filename.c_str());
63 if (vf) m->propagateContour(vf);
64 m->ComputeMasks(image->GetVTKImages()[0],false); //don't extrude the contour
66 } else { //Read a Dicom-struct file
67 assert(selected_contours.size() > 0);
69 std::vector<vvMesh::Pointer> contour_stacks=readSelectedContours();
70 for (std::vector<vvMesh::Pointer>::iterator i=contour_stacks.begin();
71 i!=contour_stacks.end(); i++) {
72 (*i)->ComputeMasks(image->GetVTKImages()[0],true); //Remesh the contour
73 (*i)->ComputeMeshes();
74 if (vf) (*i)->propagateContour(vf);
80 std::vector<std::pair<int,std::string> > vvMeshReader::GetROINames()
84 reader.SetFileName(filename.c_str());
85 reader.SetMaxSizeLoadEntry(16384);
88 gdcm::SeqEntry * roi_info=reader.GetSeqEntry(0x3006,0x0020);
90 std::vector<std::pair<int, std::string> > roi_names;
93 for (gdcm::SQItem* i=roi_info->GetFirstSQItem(); i!=0; i=roi_info->GetNextSQItem())
94 if (i->GetEntryValue(0x3006,0x0022)!= gdcm::GDCM_UNFOUND)
95 roi_names.push_back(make_pair(atoi(i->GetEntryValue(0x3006,0x0022).c_str()),i->GetEntryValue(0x3006,0x0026)));
99 std::vector<vvMesh::Pointer> vvMeshReader::readSelectedContours()
102 reader.SetFileName(filename.c_str());
103 reader.SetMaxSizeLoadEntry(16384);
106 std::vector<vvMesh::Pointer> result;
107 gdcm::SeqEntry * rois=reader.GetSeqEntry(0x3006,0x0039);
108 ///We need to iterate both on the contours themselves, and on the contour info
109 gdcm::SeqEntry * roi_info=reader.GetSeqEntry(0x3006,0x0020);
110 gdcm::SQItem* k=roi_info->GetFirstSQItem();
111 for(gdcm::SQItem* i=rois->GetFirstSQItem(); i!=0; i=rois->GetNextSQItem()) { //loop over ROIS
113 vtkSmartPointer<vtkAppendPolyData> append=vtkSmartPointer<vtkAppendPolyData>::New();
114 std::istringstream ss(i->GetEntryValue(0x3006,0x0084));
117 if (std::find(selected_contours.begin(),selected_contours.end(),roi_number) != selected_contours.end()) { //Only read selected ROIs
118 vvMesh::Pointer current_roi=vvMesh::New();
119 std::vector<double> rgb=clitk::parse_string<double>(i->GetEntryValue(0x3006,0x002a),'\\');
120 assert(rgb.size()==3);
121 current_roi->r=rgb[0]/255;
122 current_roi->g=rgb[1]/255;
123 current_roi->b=rgb[2]/255;
124 current_roi->structure_name=k->GetEntryValue(0x3006,0x0026);
125 gdcm::SeqEntry * contours=i->GetSeqEntry(0x3006,0x0040);
126 double z0=-1; //Used to determine spacing between slices, assumed to be constant
127 for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) { //loop over 2D contours
128 std::string contour_type=j->GetEntryValue(0x3006,0x0042);
129 if (contour_type=="CLOSED_PLANAR ") {
130 int point_number=clitk::parse_value<int>(j->GetEntryValue(0x3006,0x0046));
131 std::vector<float> points=clitk::parse_string<float>(j->GetEntryValue(0x3006,0x0050),'\\');
132 assert(points.size() == static_cast<unsigned int>(point_number)*3);
133 if (z0 == -1) //First contour
135 //2nd contour, spacing not yet set. Need to be sure we are on a different slice,
136 //sometimes there is more than one closed contour per slice
137 else if (current_roi->GetSpacing()==-1 && points[2] != z0 )
138 current_roi->SetSpacing(points[2]-z0);
139 vtkPolyData * contour=vtkPolyData::New();
140 contour->Allocate(); //for cell structures
141 contour->SetPoints(vtkPoints::New());
143 for (unsigned int idx=0; idx<points.size(); idx+=3) {
144 contour->GetPoints()->InsertNextPoint(points[idx],points[idx+1],points[idx+2]);
146 ids[1]=(ids[0]+1)%point_number; //0-1,1-2,...,n-1-0
147 contour->GetLines()->InsertNextCell(2,ids);
149 append->AddInput(contour);
150 } else if (contour_type == "POINT ")
151 ; // silently ignore POINT type since we don't need them at the moment
153 std::cerr << "Warning: contour type " << contour_type << " not handled!" << std::endl;
156 current_roi->AddMesh(append->GetOutput());
157 result.push_back(current_roi);
159 //std::cerr << "Warning: ignoring ROI #" << roi_number << std::endl;
161 k=roi_info->GetNextSQItem(); //increment the second loop variable