]> Creatis software - clitk.git/blob - vv/vvMeshReader.cxx
Mathieu Malaterre :
[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 <gdcmFile.h>
23 #if GDCM_MAJOR_VERSION == 2
24 #else
25 #include <gdcm.h>
26 #include <gdcmSQItem.h>
27 #endif
28
29 #include <vtkSmartPointer.h>
30 #include <vtkAppendPolyData.h>
31 #include <vtkCellArray.h>
32 #include <vtkMetaImageWriter.h>
33 #include <vtkMetaImageReader.h>
34 #include <vtkPolyDataWriter.h>
35 #include <vtkImageData.h>
36 #include <vtkImageCast.h>
37 #include <vtkImageGaussianSmooth.h>
38
39 #include "clitkCommon.h"
40 #include "vvMeshReader.h"
41 #include "vvProgressDialog.h"
42 #include <clitkCommon.h>
43
44 vvMeshReader::vvMeshReader() :
45   vtk_mode(false)
46 {}
47
48 void vvMeshReader::Update()
49 {
50   //Show a progress bar only when opening a DC-struct (ie. multiple contours)
51   vvProgressDialog progress("Opening " + filename,(!vtk_mode) && (selected_contours.size()>1));
52   this->start();
53   while (this->isRunning()) {
54     progress.SetProgress(result.size(),selected_contours.size());
55     this->wait(50);
56     qApp->processEvents();
57   }
58 }
59
60 void vvMeshReader::run()
61 {
62   ///Verify the important stuff has been set
63   assert(filename != "");
64   if (vtk_mode) { //Read vtkPolyData
65     vvMesh::Pointer m=vvMesh::New();
66     m->ReadFromVTK(filename.c_str());
67     if (vf) m->propagateContour(vf);
68     m->ComputeMasks(image->GetVTKImages()[0],false); //don't extrude the contour
69     result.push_back(m);
70   } else { //Read a Dicom-struct file
71     assert(selected_contours.size() > 0);
72     assert(image);
73     std::vector<vvMesh::Pointer> contour_stacks=readSelectedContours();
74     for (std::vector<vvMesh::Pointer>::iterator i=contour_stacks.begin();
75          i!=contour_stacks.end(); i++) {
76       (*i)->ComputeMasks(image->GetVTKImages()[0],true); //Remesh the contour
77       (*i)->ComputeMeshes();
78       if (vf) (*i)->propagateContour(vf);
79       result.push_back(*i);
80     }
81   }
82 }
83
84 std::vector<std::pair<int,std::string> > vvMeshReader::GetROINames()
85 {
86   assert(filename!="");
87   std::vector<std::pair<int, std::string> > roi_names;
88 #if GDCM_MAJOR_VERSION == 2
89 #else
90   gdcm::File reader;
91   reader.SetFileName(filename.c_str());
92   reader.SetMaxSizeLoadEntry(16384);
93   reader.Load();
94
95   gdcm::SeqEntry * roi_info=reader.GetSeqEntry(0x3006,0x0020);
96   assert(roi_info);
97   // DD("ici");
98   //int n=0;
99   for (gdcm::SQItem* i=roi_info->GetFirstSQItem(); i!=0; i=roi_info->GetNextSQItem())
100     if (i->GetEntryValue(0x3006,0x0022)!= gdcm::GDCM_UNFOUND)
101       roi_names.push_back(make_pair(atoi(i->GetEntryValue(0x3006,0x0022).c_str()),i->GetEntryValue(0x3006,0x0026)));
102 #endif
103   return roi_names;
104 }
105
106 std::vector<vvMesh::Pointer> vvMeshReader::readSelectedContours()
107 {
108   std::vector<vvMesh::Pointer> result;
109 #if GDCM_MAJOR_VERSION == 2
110 #else
111   gdcm::File reader;
112   reader.SetFileName(filename.c_str());
113   reader.SetMaxSizeLoadEntry(16384);
114   reader.Load();
115
116   gdcm::SeqEntry * rois=reader.GetSeqEntry(0x3006,0x0039);
117   ///We need to iterate both on the contours themselves, and on the contour info
118   gdcm::SeqEntry * roi_info=reader.GetSeqEntry(0x3006,0x0020);
119   gdcm::SQItem* k=roi_info->GetFirstSQItem();
120   for(gdcm::SQItem* i=rois->GetFirstSQItem(); i!=0; i=rois->GetNextSQItem()) { //loop over ROIS
121     assert(k!=0);
122     vtkSmartPointer<vtkAppendPolyData> append=vtkSmartPointer<vtkAppendPolyData>::New();
123     std::istringstream ss(i->GetEntryValue(0x3006,0x0084));
124     int roi_number;
125     ss >> roi_number;
126     if (std::find(selected_contours.begin(),selected_contours.end(),roi_number) != selected_contours.end()) { //Only read selected ROIs
127       vvMesh::Pointer current_roi=vvMesh::New();
128       std::vector<double> rgb=clitk::parse_string<double>(i->GetEntryValue(0x3006,0x002a),'\\');
129       assert(rgb.size()==3);
130       current_roi->r=rgb[0]/255;
131       current_roi->g=rgb[1]/255;
132       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
137         std::string contour_type=j->GetEntryValue(0x3006,0x0042);
138         if (contour_type=="CLOSED_PLANAR ") {
139           int point_number=clitk::parse_value<int>(j->GetEntryValue(0x3006,0x0046));
140           std::vector<float> points=clitk::parse_string<float>(j->GetEntryValue(0x3006,0x0050),'\\');
141           assert(points.size() == static_cast<unsigned int>(point_number)*3);
142           if (z0 == -1) //First contour
143             z0=points[2];
144           //2nd contour, spacing not yet set. Need to be sure we are on a different slice,
145           //sometimes there is more than one closed contour per slice
146           else if (current_roi->GetSpacing()==-1 && points[2] != z0 )
147             current_roi->SetSpacing(points[2]-z0);
148           vtkPolyData * contour=vtkPolyData::New();
149           contour->Allocate(); //for cell structures
150           contour->SetPoints(vtkPoints::New());
151           vtkIdType ids[2];
152           for (unsigned int idx=0; idx<points.size(); idx+=3) {
153             contour->GetPoints()->InsertNextPoint(points[idx],points[idx+1],points[idx+2]);
154             ids[0]=idx/3;
155             ids[1]=(ids[0]+1)%point_number; //0-1,1-2,...,n-1-0
156             contour->GetLines()->InsertNextCell(2,ids);
157           }
158           append->AddInput(contour);
159         } else if (contour_type == "POINT ")
160           ; // silently ignore POINT type since we don't need them at the moment
161         else
162           std::cerr << "Warning: contour type " << contour_type << " not handled!" << std::endl;
163       }
164       append->Update();
165       current_roi->AddMesh(append->GetOutput());
166       result.push_back(current_roi);
167     } else {
168       //std::cerr << "Warning: ignoring ROI #" << roi_number << std::endl;
169     }
170     k=roi_info->GetNextSQItem(); //increment the second loop variable
171   }
172 #endif
173   return result;
174 }
175