]> Creatis software - clitk.git/blob - vv/vvMeshReader.cxx
Initial revision
[clitk.git] / vv / vvMeshReader.cxx
1 /*=========================================================================
2
3  Program:   vv
4  Language:  C++
5  Author :   Joel Schaerer (joel.schaerer@insa-lyon.fr)
6
7 Copyright (C) 2008
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, version 3 of the License.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 =========================================================================*/
24 #include <algorithm>
25
26 #include <QApplication>
27
28 #include <gdcm.h>
29 #include <gdcmSQItem.h>
30
31 #include <vtkSmartPointer.h>
32 #include <vtkAppendPolyData.h>
33 #include <vtkCellArray.h>
34 #include <vtkMetaImageWriter.h>
35 #include <vtkMetaImageReader.h>
36 #include <vtkPolyDataWriter.h>
37 #include <vtkImageData.h>
38 #include <vtkImageCast.h>
39 #include <vtkImageGaussianSmooth.h>
40
41 #include "clitkCommon.h"
42 #include "vvMeshReader.h"
43 #include "vvProgressDialog.h"
44
45 vvMeshReader::vvMeshReader() :
46     vtk_mode(false)
47 {}
48
49 void vvMeshReader::Update()
50 {
51     //Show a progress bar only when opening a DC-struct (ie. multiple contours)
52     vvProgressDialog progress("Opening " + filename,(not vtk_mode) and (selected_contours.size()>1));
53     this->start();
54     while (this->isRunning())
55     {
56         progress.SetProgress(result.size(),selected_contours.size());
57         this->wait(50);
58         qApp->processEvents();
59     }
60 }
61
62 void vvMeshReader::run()
63 {
64     ///Verify the important stuff has been set
65     assert(filename != "");
66     if (vtk_mode) //Read vtkPolyData
67     {
68         vvMesh::Pointer m=vvMesh::New();
69         m->ReadFromVTK(filename.c_str());
70         if (vf) m->propagateContour(vf);
71         result.push_back(m);
72     }
73     else //Read a Dicom-struct file
74     {
75         assert(selected_contours.size() > 0);
76         assert(image);
77         std::vector<vvMesh::Pointer> contour_stacks=readSelectedContours();
78         for (std::vector<vvMesh::Pointer>::iterator i=contour_stacks.begin();
79                 i!=contour_stacks.end();i++)
80         {
81             (*i)->ComputeMasks(image->GetVTKImages()[0],true); //Remesh the contour
82             (*i)->ComputeMeshes();
83             if (vf) (*i)->propagateContour(vf);
84             result.push_back(*i);
85         }
86     }
87 }
88
89 template<class ElementType>
90 ElementType parse_value(std::string str)
91 {
92     std::istringstream parser(str);
93     ElementType value;
94     parser >> value;
95     assert(!parser.fail());
96     return value;
97 }
98
99 template<class ElementType>
100 std::vector<ElementType> parse_string(std::string str,char delim)
101 {
102     std::istringstream ss(str);
103     std::string token;
104     std::vector<ElementType> result;
105     while (getline(ss,token,delim))
106     {
107         result.push_back(parse_value<ElementType>(token));
108     }
109     return result;
110 }
111
112 std::vector<std::pair<int,std::string> > vvMeshReader::GetROINames()
113 {
114     assert(filename!="");
115     gdcm::File reader;
116     reader.SetFileName(filename.c_str());
117     reader.SetMaxSizeLoadEntry(16384);
118     reader.Load();
119
120     gdcm::SeqEntry * roi_info=reader.GetSeqEntry(0x3006,0x0020);
121     assert(roi_info);
122     std::vector<std::pair<int, std::string> > roi_names;
123     // DD("ici");
124     //int n=0;
125     for (gdcm::SQItem* i=roi_info->GetFirstSQItem();i!=0;i=roi_info->GetNextSQItem())
126         if (i->GetEntryValue(0x3006,0x0022)!= gdcm::GDCM_UNFOUND)
127             roi_names.push_back(make_pair(atoi(i->GetEntryValue(0x3006,0x0022).c_str()),i->GetEntryValue(0x3006,0x0026)));
128     return roi_names;
129 }
130
131 std::vector<vvMesh::Pointer> vvMeshReader::readSelectedContours()
132 {
133     gdcm::File reader;
134     reader.SetFileName(filename.c_str());
135     reader.SetMaxSizeLoadEntry(16384);
136     reader.Load();
137
138     std::vector<vvMesh::Pointer> result;
139     gdcm::SeqEntry * rois=reader.GetSeqEntry(0x3006,0x0039);
140     ///We need to iterate both on the contours themselves, and on the contour info
141     gdcm::SeqEntry * roi_info=reader.GetSeqEntry(0x3006,0x0020);
142     gdcm::SQItem* k=roi_info->GetFirstSQItem();
143     for(gdcm::SQItem* i=rois->GetFirstSQItem();i!=0;i=rois->GetNextSQItem()) //loop over ROIS
144     {
145         assert(k!=0);
146         vtkSmartPointer<vtkAppendPolyData> append=vtkSmartPointer<vtkAppendPolyData>::New();
147         std::istringstream ss(i->GetEntryValue(0x3006,0x0084));
148         int roi_number;ss >> roi_number;
149         if (std::find(selected_contours.begin(),selected_contours.end(),roi_number) != selected_contours.end())//Only read selected ROIs
150         {
151             vvMesh::Pointer current_roi=vvMesh::New();
152             std::vector<double> rgb=parse_string<double>(i->GetEntryValue(0x3006,0x002a),'\\');
153             assert(rgb.size()==3);
154             current_roi->r=rgb[0]/255; current_roi->g=rgb[1]/255; current_roi->b=rgb[2]/255;
155             current_roi->structure_name=k->GetEntryValue(0x3006,0x0026);
156             gdcm::SeqEntry * contours=i->GetSeqEntry(0x3006,0x0040);
157             double z0=-1; //Used to determine spacing between slices, assumed to be constant
158             for(gdcm::SQItem* j=contours->GetFirstSQItem();j!=0;j=contours->GetNextSQItem()) //loop over 2D contours
159             {
160                 std::string contour_type=j->GetEntryValue(0x3006,0x0042);
161                 if (contour_type=="CLOSED_PLANAR ")
162                 {
163                     int point_number=parse_value<int>(j->GetEntryValue(0x3006,0x0046));
164                     std::vector<float> points=parse_string<float>(j->GetEntryValue(0x3006,0x0050),'\\');
165                     assert(points.size() == static_cast<unsigned int>(point_number)*3);
166                     if (z0 == -1) //First contour
167                         z0=points[2];
168                     //2nd contour, spacing not yet set. Need to be sure we are on a different slice,
169                     //sometimes there is more than one closed contour per slice
170                     else if (current_roi->GetSpacing()==-1 && points[2] != z0 ) 
171                         current_roi->SetSpacing(points[2]-z0);
172                     vtkPolyData * contour=vtkPolyData::New();
173                     contour->Allocate(); //for cell structures
174                     contour->SetPoints(vtkPoints::New());
175                     vtkIdType ids[2];
176                     for (unsigned int idx=0;idx<points.size();idx+=3)
177                     {
178                         contour->GetPoints()->InsertNextPoint(points[idx],points[idx+1],points[idx+2]);
179                         ids[0]=idx/3;ids[1]=(ids[0]+1)%point_number; //0-1,1-2,...,n-1-0
180                         contour->GetLines()->InsertNextCell(2,ids);
181                     }
182                     append->AddInput(contour);
183                 }
184                 else if (contour_type == "POINT ")
185                     ; // silently ignore POINT type since we don't need them at the moment
186                 else
187                     std::cerr << "Warning: contour type " << contour_type << " not handled!" << std::endl;
188             }
189             append->Update();
190             current_roi->AddMesh(append->GetOutput());
191             result.push_back(current_roi);
192         }
193         else
194         {
195             //std::cerr << "Warning: ignoring ROI #" << roi_number << std::endl;
196         }
197         k=roi_info->GetNextSQItem(); //increment the second loop variable
198     }
199     return result;
200 }
201