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 void clitk::DicomRT_Contour::UpdateDicomItem()
58 DD("DicomRT_Contour::UpdateDicomItem");
60 gdcm::DataSet & nestedds = mItem->GetNestedDataSet();
62 //NON CONSIDER CONTOUR ITEM NOT SEQ ITEM ?
64 // Contour type [Contour Geometric Type]
65 gdcm::Attribute<0x3006,0x0042> contgeotype;
66 contgeotype.SetFromDataSet( nestedds );
68 // Number of points [Number of Contour Points]
69 gdcm::Attribute<0x3006,0x0046> numcontpoints;
70 numcontpoints.SetFromDataSet( nestedds );
72 mNbOfPoints = numcontpoints.GetValue();
75 // Contour dicom values from DataPoints
76 //ComputeDataPointsFromMesh();
77 uint nb = mData->GetNumberOfPoints();
78 std::vector<double> points;
79 points.resize(mNbOfPoints*3);
80 for(unsigned int i=0; i<nb; i++) {
81 double * p = mData->GetPoint(i);
88 gdcm::Attribute<0x3006,0x0050> at;
89 gdcm::Tag tcontourdata(0x3006,0x0050);
90 gdcm::DataElement contourdata = nestedds.GetDataElement( tcontourdata );
91 at.SetFromDataElement( contourdata );
94 at.SetValues(&points[0], points.size(), false);
95 DD(at.GetValues()[0]);
98 nestedds.Replace(at.GetAsDataElement());
101 // Change Number of points [Number of Contour Points]
102 numcontpoints.SetValue(nb);
103 nestedds.Replace(numcontpoints.GetAsDataElement());
106 gdcm::DataElement aa = nestedds.GetDataElement( tcontourdata );
107 at.SetFromDataElement( aa );
108 const double* bb = at.GetValues();
112 //--------------------------------------------------------------------
115 //--------------------------------------------------------------------
116 #if GDCM_MAJOR_VERSION == 2
117 bool clitk::DicomRT_Contour::Read(gdcm::Item * item)
121 const gdcm::DataSet& nestedds2 = item->GetNestedDataSet();
123 // Contour type [Contour Geometric Type]
124 gdcm::Attribute<0x3006,0x0042> contgeotype;
125 contgeotype.SetFromDataSet( nestedds2 );
127 if (contgeotype.GetValue() != "CLOSED_PLANAR " && contgeotype.GetValue() != "POINT ") { ///WARNING to the space after the name ...
128 //std::cerr << "Skip this contour : type=" << mType << std::endl;
131 if (contgeotype.GetValue() == "POINT ") {
132 std::cerr << "Warning: POINT type not fully supported. (don't use GetMesh() with this!)"
136 gdcm::Attribute<0x3006,0x0046> numcontpoints;
137 numcontpoints.SetFromDataSet( nestedds2 );
138 // Number of points [Number of Contour Points]
139 mNbOfPoints = numcontpoints.GetValue();
142 gdcm::Attribute<0x3006,0x0050> at;
143 gdcm::Tag tcontourdata(0x3006,0x0050);
144 const gdcm::DataElement & contourdata = nestedds2.GetDataElement( tcontourdata );
145 at.SetFromDataElement( contourdata );
146 const double* points = at.GetValues();
147 <<<<<<< Updated upstream
149 // unsigned int npts = at.GetNumberOfValues() / 3;
150 >>>>>>> Stashed changes
152 assert(at.GetNumberOfValues() == static_cast<unsigned int>(mNbOfPoints)*3);
155 mData = vtkSmartPointer<vtkPoints>::New();
156 mData->SetDataTypeToDouble();
157 mData->SetNumberOfPoints(mNbOfPoints);
158 for(unsigned int i=0; i<mNbOfPoints; i++) {
161 p[1] = points[i*3+1];
162 p[2] = points[i*3+2];
163 mData->SetPoint(i, p);
164 if (mZ == -1) mZ = p[2];
169 std::cout << "ERROR ! contour not in the same slice" << std::endl;
178 bool clitk::DicomRT_Contour::Read(gdcm::SQItem * item)
181 // Contour type [Contour Geometric Type]
182 mType = item->GetEntryValue(0x3006,0x0042);
184 if (mType != "CLOSED_PLANAR " && mType != "POINT ") { ///WARNING to the space after the name ...
185 //std::cerr << "Skip this contour : type=" << mType << std::endl;
188 if (mType == "POINT ") {
189 std::cerr << "Warning: POINT type not fully supported. (don't use GetMesh() with this!)"
193 // Number of points [Number of Contour Points]
194 mNbOfPoints = parse_value<int>(item->GetEntryValue(0x3006,0x0046));
197 // Read values [Contour Data]
198 std::vector<float> points = parse_string<float>(item->GetEntryValue(0x3006,0x0050),'\\');
199 assert(points.size() == static_cast<unsigned int>(mNbOfPoints)*3);
202 mData = vtkSmartPointer<vtkPoints>::New();
203 mData->SetDataTypeToDouble();
204 mData->SetNumberOfPoints(mNbOfPoints);
205 for(unsigned int i=0; i<mNbOfPoints; i++) {
208 p[1] = points[i*3+1];
209 p[2] = points[i*3+2];
210 mData->SetPoint(i, p);
211 if (mZ == -1) mZ = p[2];
216 std::cout << "ERROR ! contour not in the same slice" << std::endl;
224 //--------------------------------------------------------------------
227 //--------------------------------------------------------------------
228 vtkPolyData * clitk::DicomRT_Contour::GetMesh()
230 if (!mMeshIsUpToDate) {
231 ComputeMeshFromDataPoints();
235 //--------------------------------------------------------------------
238 //--------------------------------------------------------------------
239 void clitk::DicomRT_Contour::SetMesh(vtkPolyData * mesh)
243 //--------------------------------------------------------------------
246 //--------------------------------------------------------------------
247 void clitk::DicomRT_Contour::ComputeMeshFromDataPoints()
249 // DD("ComputeMesh Contour");
250 mMesh = vtkSmartPointer<vtkPolyData>::New();
251 mMesh->Allocate(); //for cell structures
252 mPoints = vtkSmartPointer<vtkPoints>::New();
253 mMesh->SetPoints(mPoints);
255 for (unsigned int idx=0 ; idx<mNbOfPoints ; idx++) {
256 mMesh->GetPoints()->InsertNextPoint(mData->GetPoint(idx)[0],
257 mData->GetPoint(idx)[1],
258 mData->GetPoint(idx)[2]);
260 ids[1]=(ids[0]+1) % mNbOfPoints; //0-1,1-2,...,n-1-0
261 mMesh->GetLines()->InsertNextCell(2,ids);
263 mMeshIsUpToDate = true;
265 //--------------------------------------------------------------------
268 //--------------------------------------------------------------------
269 void clitk::DicomRT_Contour::ComputeDataPointsFromMesh()
271 DD("ComputeDataPointsFromMesh");
276 mMesh = vtkSmartPointer<vtkPolyData>::New();
277 mMesh->Allocate(); //for cell structures
278 mPoints = vtkSmartPointer<vtkPoints>::New();
279 mMesh->SetPoints(mPoints);
281 for (unsigned int idx=0 ; idx<mNbOfPoints ; idx++) {
282 mMesh->GetPoints()->InsertNextPoint(mData->GetPoint(idx)[0],
283 mData->GetPoint(idx)[1],
284 mData->GetPoint(idx)[2]);
286 ids[1]=(ids[0]+1) % mNbOfPoints; //0-1,1-2,...,n-1-0
287 mMesh->GetLines()->InsertNextCell(2,ids);
289 mMeshIsUpToDate = true;
292 //--------------------------------------------------------------------