1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
3 Main authors : XX XX XX
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
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.
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
18 =========================================================================*/
20 #include "clitkDicomRT_Contour.h"
21 #include <vtkCellArray.h>
23 #if GDCM_MAJOR_VERSION == 2
24 #include "gdcmAttribute.h"
28 //--------------------------------------------------------------------
29 clitk::DicomRT_Contour::DicomRT_Contour()
31 mMeshIsUpToDate = false;
35 //--------------------------------------------------------------------
38 //--------------------------------------------------------------------
39 clitk::DicomRT_Contour::~DicomRT_Contour()
43 //--------------------------------------------------------------------
47 //--------------------------------------------------------------------
48 void clitk::DicomRT_Contour::Print(std::ostream & os) const
50 DD("TODO : print Contours");
52 //--------------------------------------------------------------------
55 //--------------------------------------------------------------------
56 #if GDCM_MAJOR_VERSION == 2
57 void clitk::DicomRT_Contour::UpdateDicomItem()
59 DD("DicomRT_Contour::UpdateDicomItem");
61 gdcm::DataSet & nestedds = mItem->GetNestedDataSet();
63 //NON CONSIDER CONTOUR ITEM NOT SEQ ITEM ?
65 // Contour type [Contour Geometric Type]
66 gdcm::Attribute<0x3006,0x0042> contgeotype;
67 contgeotype.SetFromDataSet( nestedds );
69 // Number of points [Number of Contour Points]
70 gdcm::Attribute<0x3006,0x0046> numcontpoints;
71 numcontpoints.SetFromDataSet( nestedds );
73 mNbOfPoints = numcontpoints.GetValue();
76 // Contour dicom values from DataPoints
77 //ComputeDataPointsFromMesh();
78 uint nb = mData->GetNumberOfPoints();
79 std::vector<double> points;
80 points.resize(mNbOfPoints*3);
81 for(unsigned int i=0; i<nb; i++) {
82 double * p = mData->GetPoint(i);
85 #if VTK_MAJOR_VERSION <= 5
88 points[i*3+1] = p[2]-0.5;
93 gdcm::Attribute<0x3006,0x0050> at;
94 gdcm::Tag tcontourdata(0x3006,0x0050);
95 gdcm::DataElement contourdata = nestedds.GetDataElement( tcontourdata );
96 at.SetFromDataElement( contourdata );
99 at.SetValues(&points[0], points.size(), false);
100 DD(at.GetValues()[0]);
103 nestedds.Replace(at.GetAsDataElement());
106 // Change Number of points [Number of Contour Points]
107 numcontpoints.SetValue(nb);
108 nestedds.Replace(numcontpoints.GetAsDataElement());
111 gdcm::DataElement aa = nestedds.GetDataElement( tcontourdata );
112 at.SetFromDataElement( aa );
113 const double* bb = at.GetValues();
118 //--------------------------------------------------------------------
121 //--------------------------------------------------------------------
122 #if GDCM_MAJOR_VERSION == 2
123 bool clitk::DicomRT_Contour::Read(gdcm::Item * item)
127 const gdcm::DataSet& nestedds2 = item->GetNestedDataSet();
129 // Contour type [Contour Geometric Type]
130 gdcm::Attribute<0x3006,0x0042> contgeotype;
131 contgeotype.SetFromDataSet( nestedds2 );
133 if (contgeotype.GetValue() != "CLOSED_PLANAR " && contgeotype.GetValue() != "POINT ") { ///WARNING to the space after the name ...
134 //std::cerr << "Skip this contour : type=" << mType << std::endl;
137 if (contgeotype.GetValue() == "POINT ") {
138 std::cerr << "Warning: POINT type not fully supported. (don't use GetMesh() with this!)"
142 gdcm::Attribute<0x3006,0x0046> numcontpoints;
143 numcontpoints.SetFromDataSet( nestedds2 );
144 // Number of points [Number of Contour Points]
145 mNbOfPoints = numcontpoints.GetValue();
148 gdcm::Attribute<0x3006,0x0050> at;
149 gdcm::Tag tcontourdata(0x3006,0x0050);
150 const gdcm::DataElement & contourdata = nestedds2.GetDataElement( tcontourdata );
151 at.SetFromDataElement( contourdata );
152 const double* points = at.GetValues();
153 // unsigned int npts = at.GetNumberOfValues() / 3;
154 assert(at.GetNumberOfValues() == static_cast<unsigned int>(mNbOfPoints)*3);
157 mData = vtkSmartPointer<vtkPoints>::New();
158 mData->SetDataTypeToDouble();
159 mData->SetNumberOfPoints(mNbOfPoints);
160 for(unsigned int i=0; i<mNbOfPoints; i++) {
163 p[1] = points[i*3+1];
164 #if VTK_MAJOR_VERSION <= 5
165 p[2] = points[i*3+2];
167 p[2] = points[i*3+2]+0.5;
169 mData->SetPoint(i, p);
170 if (mZ == -1) mZ = p[2];
175 std::cout << "ERROR ! contour not in the same slice" << std::endl;
184 bool clitk::DicomRT_Contour::Read(gdcm::SQItem * item)
187 // Contour type [Contour Geometric Type]
188 mType = item->GetEntryValue(0x3006,0x0042);
190 if (mType != "CLOSED_PLANAR " && mType != "POINT ") { ///WARNING to the space after the name ...
191 //std::cerr << "Skip this contour : type=" << mType << std::endl;
194 if (mType == "POINT ") {
195 std::cerr << "Warning: POINT type not fully supported. (don't use GetMesh() with this!)"
199 // Number of points [Number of Contour Points]
200 mNbOfPoints = parse_value<int>(item->GetEntryValue(0x3006,0x0046));
203 // Read values [Contour Data]
204 std::vector<float> points = parse_string<float>(item->GetEntryValue(0x3006,0x0050),'\\');
205 assert(points.size() == static_cast<unsigned int>(mNbOfPoints)*3);
208 mData = vtkSmartPointer<vtkPoints>::New();
209 mData->SetDataTypeToDouble();
210 mData->SetNumberOfPoints(mNbOfPoints);
211 for(unsigned int i=0; i<mNbOfPoints; i++) {
214 p[1] = points[i*3+1];
215 #if VTK_MAJOR_VERSION <= 5
216 p[2] = points[i*3+2];
218 p[2] = points[i*3+2]+0.5;
220 mData->SetPoint(i, p);
221 if (mZ == -1) mZ = p[2];
226 std::cout << "ERROR ! contour not in the same slice" << std::endl;
234 //--------------------------------------------------------------------
237 //--------------------------------------------------------------------
238 vtkPolyData * clitk::DicomRT_Contour::GetMesh()
240 if (!mMeshIsUpToDate) {
241 ComputeMeshFromDataPoints();
245 //--------------------------------------------------------------------
248 //--------------------------------------------------------------------
249 void clitk::DicomRT_Contour::SetMesh(vtkPolyData * mesh)
253 //--------------------------------------------------------------------
256 //--------------------------------------------------------------------
257 void clitk::DicomRT_Contour::ComputeMeshFromDataPoints()
259 // DD("ComputeMesh Contour");
260 mMesh = vtkSmartPointer<vtkPolyData>::New();
261 mMesh->Allocate(); //for cell structures
262 mPoints = vtkSmartPointer<vtkPoints>::New();
263 mMesh->SetPoints(mPoints);
265 for (unsigned int idx=0 ; idx<mNbOfPoints ; idx++) {
266 mMesh->GetPoints()->InsertNextPoint(mData->GetPoint(idx)[0],
267 mData->GetPoint(idx)[1],
268 mData->GetPoint(idx)[2]);
270 ids[1]=(ids[0]+1) % mNbOfPoints; //0-1,1-2,...,n-1-0
271 mMesh->GetLines()->InsertNextCell(2,ids);
273 mMeshIsUpToDate = true;
275 //--------------------------------------------------------------------
278 //--------------------------------------------------------------------
279 void clitk::DicomRT_Contour::ComputeDataPointsFromMesh()
281 DD("ComputeDataPointsFromMesh");
286 mMesh = vtkSmartPointer<vtkPolyData>::New();
287 mMesh->Allocate(); //for cell structures
288 mPoints = vtkSmartPointer<vtkPoints>::New();
289 mMesh->SetPoints(mPoints);
291 for (unsigned int idx=0 ; idx<mNbOfPoints ; idx++) {
292 mMesh->GetPoints()->InsertNextPoint(mData->GetPoint(idx)[0],
293 mData->GetPoint(idx)[1],
294 mData->GetPoint(idx)[2]);
296 ids[1]=(ids[0]+1) % mNbOfPoints; //0-1,1-2,...,n-1-0
297 mMesh->GetLines()->InsertNextCell(2,ids);
299 mMeshIsUpToDate = true;
302 //--------------------------------------------------------------------