3 #include <QApplication>
6 #include <gdcmSQItem.h>
8 #include <vtkSmartPointer.h>
9 #include <vtkAppendPolyData.h>
10 #include <vtkCellArray.h>
11 #include <vtkMetaImageWriter.h>
12 #include <vtkMetaImageReader.h>
13 #include <vtkPolyDataWriter.h>
14 #include <vtkImageData.h>
15 #include <vtkImageCast.h>
16 #include <vtkImageGaussianSmooth.h>
18 #include "clitkCommon.h"
19 #include "vvMeshReader.h"
20 #include "vvProgressDialog.h"
22 vvMeshReader::vvMeshReader() :
26 void vvMeshReader::Update()
28 //Show a progress bar only when opening a DC-struct (ie. multiple contours)
29 vvProgressDialog progress("Opening " + filename,(!vtk_mode) && (selected_contours.size()>1));
31 while (this->isRunning())
33 progress.SetProgress(result.size(),selected_contours.size());
35 qApp->processEvents();
39 void vvMeshReader::run()
41 ///Verify the important stuff has been set
42 assert(filename != "");
43 if (vtk_mode) //Read vtkPolyData
45 vvMesh::Pointer m=vvMesh::New();
46 m->ReadFromVTK(filename.c_str());
47 if (vf) m->propagateContour(vf);
48 m->ComputeMasks(image->GetVTKImages()[0],true);
51 else //Read a Dicom-struct file
53 assert(selected_contours.size() > 0);
55 std::vector<vvMesh::Pointer> contour_stacks=readSelectedContours();
56 for (std::vector<vvMesh::Pointer>::iterator i=contour_stacks.begin();
57 i!=contour_stacks.end();i++)
59 (*i)->ComputeMasks(image->GetVTKImages()[0],true); //Remesh the contour
60 (*i)->ComputeMeshes();
61 if (vf) (*i)->propagateContour(vf);
67 template<class ElementType>
68 ElementType parse_value(std::string str)
70 std::istringstream parser(str);
73 assert(!parser.fail());
77 template<class ElementType>
78 std::vector<ElementType> parse_string(std::string str,char delim)
80 std::istringstream ss(str);
82 std::vector<ElementType> result;
83 while (getline(ss,token,delim))
85 result.push_back(parse_value<ElementType>(token));
90 std::vector<std::pair<int,std::string> > vvMeshReader::GetROINames()
94 reader.SetFileName(filename.c_str());
95 reader.SetMaxSizeLoadEntry(16384);
98 gdcm::SeqEntry * roi_info=reader.GetSeqEntry(0x3006,0x0020);
100 std::vector<std::pair<int, std::string> > roi_names;
103 for (gdcm::SQItem* i=roi_info->GetFirstSQItem();i!=0;i=roi_info->GetNextSQItem())
104 if (i->GetEntryValue(0x3006,0x0022)!= gdcm::GDCM_UNFOUND)
105 roi_names.push_back(make_pair(atoi(i->GetEntryValue(0x3006,0x0022).c_str()),i->GetEntryValue(0x3006,0x0026)));
109 std::vector<vvMesh::Pointer> vvMeshReader::readSelectedContours()
112 reader.SetFileName(filename.c_str());
113 reader.SetMaxSizeLoadEntry(16384);
116 std::vector<vvMesh::Pointer> result;
117 gdcm::SeqEntry * rois=reader.GetSeqEntry(0x3006,0x0039);
118 ///We need to iterate both on the contours themselves, and on the contour info
119 gdcm::SeqEntry * roi_info=reader.GetSeqEntry(0x3006,0x0020);
120 gdcm::SQItem* k=roi_info->GetFirstSQItem();
121 for(gdcm::SQItem* i=rois->GetFirstSQItem();i!=0;i=rois->GetNextSQItem()) //loop over ROIS
124 vtkSmartPointer<vtkAppendPolyData> append=vtkSmartPointer<vtkAppendPolyData>::New();
125 std::istringstream ss(i->GetEntryValue(0x3006,0x0084));
126 int roi_number;ss >> roi_number;
127 if (std::find(selected_contours.begin(),selected_contours.end(),roi_number) != selected_contours.end())//Only read selected ROIs
129 vvMesh::Pointer current_roi=vvMesh::New();
130 std::vector<double> rgb=parse_string<double>(i->GetEntryValue(0x3006,0x002a),'\\');
131 assert(rgb.size()==3);
132 current_roi->r=rgb[0]/255; current_roi->g=rgb[1]/255; current_roi->b=rgb[2]/255;
133 current_roi->structure_name=k->GetEntryValue(0x3006,0x0026);
134 gdcm::SeqEntry * contours=i->GetSeqEntry(0x3006,0x0040);
135 double z0=-1; //Used to determine spacing between slices, assumed to be constant
136 for(gdcm::SQItem* j=contours->GetFirstSQItem();j!=0;j=contours->GetNextSQItem()) //loop over 2D contours
138 std::string contour_type=j->GetEntryValue(0x3006,0x0042);
139 if (contour_type=="CLOSED_PLANAR ")
141 int point_number=parse_value<int>(j->GetEntryValue(0x3006,0x0046));
142 std::vector<float> points=parse_string<float>(j->GetEntryValue(0x3006,0x0050),'\\');
143 assert(points.size() == static_cast<unsigned int>(point_number)*3);
144 if (z0 == -1) //First contour
146 //2nd contour, spacing not yet set. Need to be sure we are on a different slice,
147 //sometimes there is more than one closed contour per slice
148 else if (current_roi->GetSpacing()==-1 && points[2] != z0 )
149 current_roi->SetSpacing(points[2]-z0);
150 vtkPolyData * contour=vtkPolyData::New();
151 contour->Allocate(); //for cell structures
152 contour->SetPoints(vtkPoints::New());
154 for (unsigned int idx=0;idx<points.size();idx+=3)
156 contour->GetPoints()->InsertNextPoint(points[idx],points[idx+1],points[idx+2]);
157 ids[0]=idx/3;ids[1]=(ids[0]+1)%point_number; //0-1,1-2,...,n-1-0
158 contour->GetLines()->InsertNextCell(2,ids);
160 append->AddInput(contour);
162 else if (contour_type == "POINT ")
163 ; // silently ignore POINT type since we don't need them at the moment
165 std::cerr << "Warning: contour type " << contour_type << " not handled!" << std::endl;
168 current_roi->AddMesh(append->GetOutput());
169 result.push_back(current_roi);
173 //std::cerr << "Warning: ignoring ROI #" << roi_number << std::endl;
175 k=roi_info->GetNextSQItem(); //increment the second loop variable