]> Creatis software - clitk.git/blob - vv/vvMeshReader.cxx
Reformatted using new coding style
[clitk.git] / vv / vvMeshReader.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #include <algorithm>
19
20 #include <QApplication>
21
22 #include <gdcm.h>
23 #include <gdcmSQItem.h>
24
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>
34
35 #include "clitkCommon.h"
36 #include "vvMeshReader.h"
37 #include "vvProgressDialog.h"
38
39 vvMeshReader::vvMeshReader() :
40   vtk_mode(false)
41 {}
42
43 void vvMeshReader::Update()
44 {
45   //Show a progress bar only when opening a DC-struct (ie. multiple contours)
46   vvProgressDialog progress("Opening " + filename,(!vtk_mode) && (selected_contours.size()>1));
47   this->start();
48   while (this->isRunning()) {
49     progress.SetProgress(result.size(),selected_contours.size());
50     this->wait(50);
51     qApp->processEvents();
52   }
53 }
54
55 void vvMeshReader::run()
56 {
57   ///Verify the important stuff has been set
58   assert(filename != "");
59   if (vtk_mode) { //Read vtkPolyData
60     vvMesh::Pointer m=vvMesh::New();
61     m->ReadFromVTK(filename.c_str());
62     if (vf) m->propagateContour(vf);
63     m->ComputeMasks(image->GetVTKImages()[0],true);
64     result.push_back(m);
65   } else { //Read a Dicom-struct file
66     assert(selected_contours.size() > 0);
67     assert(image);
68     std::vector<vvMesh::Pointer> contour_stacks=readSelectedContours();
69     for (std::vector<vvMesh::Pointer>::iterator i=contour_stacks.begin();
70          i!=contour_stacks.end(); i++) {
71       (*i)->ComputeMasks(image->GetVTKImages()[0],true); //Remesh the contour
72       (*i)->ComputeMeshes();
73       if (vf) (*i)->propagateContour(vf);
74       result.push_back(*i);
75     }
76   }
77 }
78
79 template<class ElementType>
80 ElementType parse_value(std::string str)
81 {
82   std::istringstream parser(str);
83   ElementType value;
84   parser >> value;
85   assert(!parser.fail());
86   return value;
87 }
88
89 template<class ElementType>
90 std::vector<ElementType> parse_string(std::string str,char delim)
91 {
92   std::istringstream ss(str);
93   std::string token;
94   std::vector<ElementType> result;
95   while (getline(ss,token,delim)) {
96     result.push_back(parse_value<ElementType>(token));
97   }
98   return result;
99 }
100
101 std::vector<std::pair<int,std::string> > vvMeshReader::GetROINames()
102 {
103   assert(filename!="");
104   gdcm::File reader;
105   reader.SetFileName(filename.c_str());
106   reader.SetMaxSizeLoadEntry(16384);
107   reader.Load();
108
109   gdcm::SeqEntry * roi_info=reader.GetSeqEntry(0x3006,0x0020);
110   assert(roi_info);
111   std::vector<std::pair<int, std::string> > roi_names;
112   // DD("ici");
113   //int n=0;
114   for (gdcm::SQItem* i=roi_info->GetFirstSQItem(); i!=0; i=roi_info->GetNextSQItem())
115     if (i->GetEntryValue(0x3006,0x0022)!= gdcm::GDCM_UNFOUND)
116       roi_names.push_back(make_pair(atoi(i->GetEntryValue(0x3006,0x0022).c_str()),i->GetEntryValue(0x3006,0x0026)));
117   return roi_names;
118 }
119
120 std::vector<vvMesh::Pointer> vvMeshReader::readSelectedContours()
121 {
122   gdcm::File reader;
123   reader.SetFileName(filename.c_str());
124   reader.SetMaxSizeLoadEntry(16384);
125   reader.Load();
126
127   std::vector<vvMesh::Pointer> result;
128   gdcm::SeqEntry * rois=reader.GetSeqEntry(0x3006,0x0039);
129   ///We need to iterate both on the contours themselves, and on the contour info
130   gdcm::SeqEntry * roi_info=reader.GetSeqEntry(0x3006,0x0020);
131   gdcm::SQItem* k=roi_info->GetFirstSQItem();
132   for(gdcm::SQItem* i=rois->GetFirstSQItem(); i!=0; i=rois->GetNextSQItem()) { //loop over ROIS
133     assert(k!=0);
134     vtkSmartPointer<vtkAppendPolyData> append=vtkSmartPointer<vtkAppendPolyData>::New();
135     std::istringstream ss(i->GetEntryValue(0x3006,0x0084));
136     int roi_number;
137     ss >> roi_number;
138     if (std::find(selected_contours.begin(),selected_contours.end(),roi_number) != selected_contours.end()) { //Only read selected ROIs
139       vvMesh::Pointer current_roi=vvMesh::New();
140       std::vector<double> rgb=parse_string<double>(i->GetEntryValue(0x3006,0x002a),'\\');
141       assert(rgb.size()==3);
142       current_roi->r=rgb[0]/255;
143       current_roi->g=rgb[1]/255;
144       current_roi->b=rgb[2]/255;
145       current_roi->structure_name=k->GetEntryValue(0x3006,0x0026);
146       gdcm::SeqEntry * contours=i->GetSeqEntry(0x3006,0x0040);
147       double z0=-1; //Used to determine spacing between slices, assumed to be constant
148       for(gdcm::SQItem* j=contours->GetFirstSQItem(); j!=0; j=contours->GetNextSQItem()) { //loop over 2D contours
149         std::string contour_type=j->GetEntryValue(0x3006,0x0042);
150         if (contour_type=="CLOSED_PLANAR ") {
151           int point_number=parse_value<int>(j->GetEntryValue(0x3006,0x0046));
152           std::vector<float> points=parse_string<float>(j->GetEntryValue(0x3006,0x0050),'\\');
153           assert(points.size() == static_cast<unsigned int>(point_number)*3);
154           if (z0 == -1) //First contour
155             z0=points[2];
156           //2nd contour, spacing not yet set. Need to be sure we are on a different slice,
157           //sometimes there is more than one closed contour per slice
158           else if (current_roi->GetSpacing()==-1 && points[2] != z0 )
159             current_roi->SetSpacing(points[2]-z0);
160           vtkPolyData * contour=vtkPolyData::New();
161           contour->Allocate(); //for cell structures
162           contour->SetPoints(vtkPoints::New());
163           vtkIdType ids[2];
164           for (unsigned int idx=0; idx<points.size(); idx+=3) {
165             contour->GetPoints()->InsertNextPoint(points[idx],points[idx+1],points[idx+2]);
166             ids[0]=idx/3;
167             ids[1]=(ids[0]+1)%point_number; //0-1,1-2,...,n-1-0
168             contour->GetLines()->InsertNextCell(2,ids);
169           }
170           append->AddInput(contour);
171         } else if (contour_type == "POINT ")
172           ; // silently ignore POINT type since we don't need them at the moment
173         else
174           std::cerr << "Warning: contour type " << contour_type << " not handled!" << std::endl;
175       }
176       append->Update();
177       current_roi->AddMesh(append->GetOutput());
178       result.push_back(current_roi);
179     } else {
180       //std::cerr << "Warning: ignoring ROI #" << roi_number << std::endl;
181     }
182     k=roi_info->GetNextSQItem(); //increment the second loop variable
183   }
184   return result;
185 }
186