]> Creatis software - clitk.git/blob - common/clitkDicomRT_Contour.cxx
removing warnings: unused variable, tmpnam
[clitk.git] / common / clitkDicomRT_Contour.cxx
1 /*=========================================================================
2   Program:         vv http://www.creatis.insa-lyon.fr/rio/vv
3   Main authors :   XX XX XX
4
5   Authors belongs to:
6   - University of LYON           http://www.universite-lyon.fr/
7   - Léon Bérard cancer center    http://www.centreleonberard.fr
8   - CREATIS CNRS laboratory      http://www.creatis.insa-lyon.fr
9
10   This software is distributed WITHOUT ANY WARRANTY; without even
11   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12   PURPOSE.  See the copyright notices for more information.
13
14   It is distributed under dual licence
15   - BSD       http://www.opensource.org/licenses/bsd-license.php
16   - CeCILL-B  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17
18   =========================================================================*/
19
20 #include "clitkDicomRT_Contour.h"
21 #include <vtkCellArray.h>
22
23 #if GDCM_MAJOR_VERSION == 2
24 #include "gdcmAttribute.h"
25 #include "gdcmItem.h"
26 #endif
27
28 //--------------------------------------------------------------------
29 clitk::DicomRT_Contour::DicomRT_Contour()
30 {
31   mMeshIsUpToDate = false;
32   mNbOfPoints = 0;
33   mZ = -1;
34 }
35 //--------------------------------------------------------------------
36
37
38 //--------------------------------------------------------------------
39 clitk::DicomRT_Contour::~DicomRT_Contour()
40 {
41
42 }
43 //--------------------------------------------------------------------
44
45
46
47 //--------------------------------------------------------------------
48 void clitk::DicomRT_Contour::Print(std::ostream & os) const
49 {
50   DD("TODO : print Contours");
51 }
52 //--------------------------------------------------------------------
53
54
55 //--------------------------------------------------------------------
56 #if GDCM_MAJOR_VERSION == 2
57 bool clitk::DicomRT_Contour::Read(gdcm::Item const & item)
58 {
59   const gdcm::DataSet& nestedds2 = item.GetNestedDataSet();
60
61   // Contour type [Contour Geometric Type]
62   gdcm::Attribute<0x3006,0x0042> contgeotype;
63   contgeotype.SetFromDataSet( nestedds2 );
64
65   if (contgeotype.GetValue() != "CLOSED_PLANAR " && contgeotype.GetValue() != "POINT ") { ///WARNING to the space after the name ...
66     //std::cerr << "Skip this contour : type=" << mType << std::endl;
67     return false;
68   }
69   if (contgeotype.GetValue() == "POINT ") {
70     std::cerr << "Warning: POINT type not fully supported. (don't use GetMesh() with this!)"
71       << std::endl;
72   }
73
74   gdcm::Attribute<0x3006,0x0046> numcontpoints;
75   numcontpoints.SetFromDataSet( nestedds2 );
76   // Number of points [Number of Contour Points]
77   mNbOfPoints = numcontpoints.GetValue();
78   // DD(mNbOfPoints);
79
80   gdcm::Attribute<0x3006,0x0050> at;
81   gdcm::Tag tcontourdata(0x3006,0x0050);
82   const gdcm::DataElement & contourdata = nestedds2.GetDataElement( tcontourdata );
83   at.SetFromDataElement( contourdata );
84   const double* points = at.GetValues();
85
86   assert(at.GetNumberOfValues() == static_cast<unsigned int>(mNbOfPoints)*3);
87
88   // Organize values
89   mData = vtkSmartPointer<vtkPoints>::New();
90   mData->SetDataTypeToDouble();
91   mData->SetNumberOfPoints(mNbOfPoints);
92   for(unsigned int i=0; i<mNbOfPoints; i++) {
93     double p[3];
94     p[0] = points[i*3];
95     p[1] = points[i*3+1];
96     p[2] = points[i*3+2];
97     mData->SetPoint(i, p);
98     if (mZ == -1) mZ = p[2];
99     if (p[2] != mZ) {
100       DD(i);
101       DD(p[2]);
102       DD(mZ);
103       std::cout << "ERROR ! contour not in the same slice" << std::endl;
104       assert(p[2] == mZ);
105     }
106   }
107
108   return true;
109
110 }
111 #else
112 bool clitk::DicomRT_Contour::Read(gdcm::SQItem * item)
113 {
114
115   // Contour type [Contour Geometric Type]
116   mType = item->GetEntryValue(0x3006,0x0042);
117   // DD(mType);
118   if (mType != "CLOSED_PLANAR " && mType != "POINT ") { ///WARNING to the space after the name ...
119     //std::cerr << "Skip this contour : type=" << mType << std::endl;
120     return false;
121   }
122   if (mType == "POINT ") {
123     std::cerr << "Warning: POINT type not fully supported. (don't use GetMesh() with this!)"
124       << std::endl;
125   }
126
127   // Number of points [Number of Contour Points]
128   mNbOfPoints = parse_value<int>(item->GetEntryValue(0x3006,0x0046));
129   // DD(mNbOfPoints);
130
131   // Read values [Contour Data]
132   std::vector<float> points = parse_string<float>(item->GetEntryValue(0x3006,0x0050),'\\');
133   assert(points.size() == static_cast<unsigned int>(mNbOfPoints)*3);
134
135   // Organize values
136   mData = vtkSmartPointer<vtkPoints>::New();
137   mData->SetDataTypeToDouble();
138   mData->SetNumberOfPoints(mNbOfPoints);
139   for(unsigned int i=0; i<mNbOfPoints; i++) {
140     double p[3];
141     p[0] = points[i*3];
142     p[1] = points[i*3+1];
143     p[2] = points[i*3+2];
144     mData->SetPoint(i, p);
145     if (mZ == -1) mZ = p[2];
146     if (p[2] != mZ) {
147       DD(i);
148       DD(p[2]);
149       DD(mZ);
150       std::cout << "ERROR ! contour not in the same slice" << std::endl;
151       assert(p[2] == mZ);
152     }
153   }
154
155   return true;
156 }
157 #endif
158 //--------------------------------------------------------------------
159
160
161 //--------------------------------------------------------------------
162 vtkPolyData * clitk::DicomRT_Contour::GetMesh()
163 {
164   if (!mMeshIsUpToDate) {
165     ComputeMesh();
166   }
167   return mMesh;
168 }
169 //--------------------------------------------------------------------
170
171
172 //--------------------------------------------------------------------
173 void clitk::DicomRT_Contour::ComputeMesh()
174 {
175 //  DD("ComputeMesh Contour");
176   mMesh = vtkSmartPointer<vtkPolyData>::New();
177   mMesh->Allocate(); //for cell structures
178   mPoints = vtkSmartPointer<vtkPoints>::New();
179   mMesh->SetPoints(mPoints);
180   vtkIdType ids[2];
181   for (unsigned int idx=0 ; idx<mNbOfPoints ; idx++) {
182     mMesh->GetPoints()->InsertNextPoint(mData->GetPoint(idx)[0],
183                                         mData->GetPoint(idx)[1],
184                                         mData->GetPoint(idx)[2]);
185     ids[0]=idx;
186     ids[1]=(ids[0]+1) % mNbOfPoints; //0-1,1-2,...,n-1-0
187     // DD(ids[0]);
188 //     DD(ids[1]);
189     mMesh->GetLines()->InsertNextCell(2,ids);
190   }
191   // DD(mMesh->GetNumberOfCells());
192   mMeshIsUpToDate = true;
193 }
194 //--------------------------------------------------------------------