]> Creatis software - clitk.git/blob - common/clitkDicomRT_Contour.cxx
e39928ad345b39337d35a97518b76a0a073e9757
[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://oncora1.lyon.fnclcc.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   unsigned int npts = at.GetNumberOfValues() / 3;
86
87   assert(at.GetNumberOfValues() == static_cast<unsigned int>(mNbOfPoints)*3);
88
89   // Organize values
90   mData = vtkSmartPointer<vtkPoints>::New();
91   mData->SetDataTypeToDouble();
92   mData->SetNumberOfPoints(mNbOfPoints);
93   for(unsigned int i=0; i<mNbOfPoints; i++) {
94     double p[3];
95     p[0] = points[i*3];
96     p[1] = points[i*3+1];
97     p[2] = points[i*3+2];
98     mData->SetPoint(i, p);
99     if (mZ == -1) mZ = p[2];
100     if (p[2] != mZ) {
101       DD(i);
102       DD(p[2]);
103       DD(mZ);
104       std::cout << "ERROR ! contour not in the same slice" << std::endl;
105       assert(p[2] == mZ);
106     }
107   }
108
109   return true;
110
111 }
112 #else
113 bool clitk::DicomRT_Contour::Read(gdcm::SQItem * item)
114 {
115
116   // Contour type [Contour Geometric Type]
117   mType = item->GetEntryValue(0x3006,0x0042);
118   // DD(mType);
119   if (mType != "CLOSED_PLANAR " && mType != "POINT ") { ///WARNING to the space after the name ...
120     //std::cerr << "Skip this contour : type=" << mType << std::endl;
121     return false;
122   }
123   if (mType == "POINT ") {
124     std::cerr << "Warning: POINT type not fully supported. (don't use GetMesh() with this!)"
125       << std::endl;
126   }
127
128   // Number of points [Number of Contour Points]
129   mNbOfPoints = parse_value<int>(item->GetEntryValue(0x3006,0x0046));
130   // DD(mNbOfPoints);
131
132   // Read values [Contour Data]
133   std::vector<float> points = parse_string<float>(item->GetEntryValue(0x3006,0x0050),'\\');
134   assert(points.size() == static_cast<unsigned int>(mNbOfPoints)*3);
135
136   // Organize values
137   mData = vtkSmartPointer<vtkPoints>::New();
138   mData->SetDataTypeToDouble();
139   mData->SetNumberOfPoints(mNbOfPoints);
140   for(unsigned int i=0; i<mNbOfPoints; i++) {
141     double p[3];
142     p[0] = points[i*3];
143     p[1] = points[i*3+1];
144     p[2] = points[i*3+2];
145     mData->SetPoint(i, p);
146     if (mZ == -1) mZ = p[2];
147     if (p[2] != mZ) {
148       DD(i);
149       DD(p[2]);
150       DD(mZ);
151       std::cout << "ERROR ! contour not in the same slice" << std::endl;
152       assert(p[2] == mZ);
153     }
154   }
155
156   return true;
157 }
158 #endif
159 //--------------------------------------------------------------------
160
161
162 //--------------------------------------------------------------------
163 vtkPolyData * clitk::DicomRT_Contour::GetMesh()
164 {
165   if (!mMeshIsUpToDate) {
166     ComputeMesh();
167   }
168   return mMesh;
169 }
170 //--------------------------------------------------------------------
171
172
173 //--------------------------------------------------------------------
174 void clitk::DicomRT_Contour::ComputeMesh()
175 {
176 //  DD("ComputeMesh Contour");
177   mMesh = vtkSmartPointer<vtkPolyData>::New();
178   mMesh->Allocate(); //for cell structures
179   mPoints = vtkSmartPointer<vtkPoints>::New();
180   mMesh->SetPoints(mPoints);
181   vtkIdType ids[2];
182   for (unsigned int idx=0 ; idx<mNbOfPoints ; idx++) {
183     mMesh->GetPoints()->InsertNextPoint(mData->GetPoint(idx)[0],
184                                         mData->GetPoint(idx)[1],
185                                         mData->GetPoint(idx)[2]);
186     ids[0]=idx;
187     ids[1]=(ids[0]+1) % mNbOfPoints; //0-1,1-2,...,n-1-0
188     // DD(ids[0]);
189 //     DD(ids[1]);
190     mMesh->GetLines()->InsertNextCell(2,ids);
191   }
192   // DD(mMesh->GetNumberOfCells());
193   mMeshIsUpToDate = true;
194 }
195 //--------------------------------------------------------------------