]> Creatis software - clitk.git/blob - vv/vvMeshReader.cxx
Fusion windows level is now 4 decimals
[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 #include <clitkCommon.h>
39
40 vvMeshReader::vvMeshReader() :
41   vtk_mode(false)
42 {}
43
44 void vvMeshReader::Update()
45 {
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));
48   this->start();
49   while (this->isRunning()) {
50     progress.SetProgress(result.size(),selected_contours.size());
51     this->wait(50);
52     qApp->processEvents();
53   }
54 }
55
56 void vvMeshReader::run()
57 {
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
65     result.push_back(m);
66   } else { //Read a Dicom-struct file
67     assert(selected_contours.size() > 0);
68     assert(image);
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);
75       result.push_back(*i);
76     }
77   }
78 }
79
80 std::vector<std::pair<int,std::string> > vvMeshReader::GetROINames()
81 {
82   assert(filename!="");
83   gdcm::File reader;
84   reader.SetFileName(filename.c_str());
85   reader.SetMaxSizeLoadEntry(16384);
86   reader.Load();
87
88   gdcm::SeqEntry * roi_info=reader.GetSeqEntry(0x3006,0x0020);
89   assert(roi_info);
90   std::vector<std::pair<int, std::string> > roi_names;
91   // DD("ici");
92   //int n=0;
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)));
96   return roi_names;
97 }
98
99 std::vector<vvMesh::Pointer> vvMeshReader::readSelectedContours()
100 {
101   gdcm::File reader;
102   reader.SetFileName(filename.c_str());
103   reader.SetMaxSizeLoadEntry(16384);
104   reader.Load();
105
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
112     assert(k!=0);
113     vtkSmartPointer<vtkAppendPolyData> append=vtkSmartPointer<vtkAppendPolyData>::New();
114     std::istringstream ss(i->GetEntryValue(0x3006,0x0084));
115     int roi_number;
116     ss >> roi_number;
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
134             z0=points[2];
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());
142           vtkIdType ids[2];
143           for (unsigned int idx=0; idx<points.size(); idx+=3) {
144             contour->GetPoints()->InsertNextPoint(points[idx],points[idx+1],points[idx+2]);
145             ids[0]=idx/3;
146             ids[1]=(ids[0]+1)%point_number; //0-1,1-2,...,n-1-0
147             contour->GetLines()->InsertNextCell(2,ids);
148           }
149           append->AddInput(contour);
150         } else if (contour_type == "POINT ")
151           ; // silently ignore POINT type since we don't need them at the moment
152         else
153           std::cerr << "Warning: contour type " << contour_type << " not handled!" << std::endl;
154       }
155       append->Update();
156       current_roi->AddMesh(append->GetOutput());
157       result.push_back(current_roi);
158     } else {
159       //std::cerr << "Warning: ignoring ROI #" << roi_number << std::endl;
160     }
161     k=roi_info->GetNextSQItem(); //increment the second loop variable
162   }
163   return result;
164 }
165