Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkRTStructureSetReader.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 #include <mitkIOUtil.h>
19 
20 #include <mitkBaseRenderer.h>
21 #include "mitkDICOMTagPath.h"
22 
23 namespace mitk
24 {
26 
28 
29  RTStructureSetReader::RoiEntry::RoiEntry()
30  {
31  Number = 0;
32  DisplayColor[0] = 1.0;
33  DisplayColor[1] = 0.0;
34  DisplayColor[2] = 0.0;
36  }
37 
38  RTStructureSetReader::RoiEntry::RoiEntry(const RoiEntry& src)
39  {
40  Number = src.Number;
41  Name = src.Name;
42  Description = src.Description;
43  DisplayColor[0] = src.DisplayColor[0];
44  DisplayColor[1] = src.DisplayColor[1];
45  DisplayColor[2] = src.DisplayColor[2];
46  ContourModelSet = mitk::ContourModelSet::New();
47  SetPolyData(src.ContourModelSet);
48  }
49 
50  RTStructureSetReader::RoiEntry::~RoiEntry() {}
51 
52  RTStructureSetReader::RoiEntry& RTStructureSetReader::
53  RoiEntry::operator =(const RoiEntry& src)
54  {
55  Number = src.Number;
56  Name = src.Name;
57  Description = src.Description;
58  DisplayColor[0] = src.DisplayColor[0];
59  DisplayColor[1] = src.DisplayColor[1];
60  DisplayColor[2] = src.DisplayColor[2];
61  SetPolyData(src.ContourModelSet);
62  return (*this);
63  }
64 
65  void RTStructureSetReader::RoiEntry::
66  SetPolyData(mitk::ContourModelSet::Pointer roiPolyData)
67  {
68  if (roiPolyData == this->ContourModelSet)
69  {
70  return;
71  }
72 
73  this->ContourModelSet = roiPolyData;
74  }
75 
77  {
78  return this->ROISequenceVector.size();
79  }
80 
81  RTStructureSetReader::RoiEntry* RTStructureSetReader::
82  FindRoiByNumber(unsigned int roiNum)
83  {
84  for (unsigned int i = 0; i < this->ROISequenceVector.size(); ++i)
85  {
86  if (this->ROISequenceVector[i].Number == roiNum)
87  {
88  return &this->ROISequenceVector[i];
89  }
90  }
91 
92  return NULL;
93  }
94 
95  RTStructureSetReader::ContourModelSetNodes RTStructureSetReader::
96  ReadStructureSet(const char* filepath)
97  {
98  DcmFileFormat file;
99  OFCondition output = file.loadFile(filepath, EXS_Unknown);
100 
101  if (output.bad())
102  {
103  MITK_ERROR << "Cant read the file" << std::endl;
104  }
105 
106  DcmDataset* dataset = file.getDataset();
107 
108  DRTStructureSetIOD structureSetObject;
109  OFCondition outp = structureSetObject.read(*dataset);
110 
111  if (!outp.good())
112  {
113  MITK_ERROR << "Error reading the file" << std::endl;
114  RTStructureSetReader::ContourModelSetNodes x;
115  return x;
116  }
117 
118  DRTStructureSetROISequence& roiSequence =
119  structureSetObject.getStructureSetROISequence();
120 
121  if (!roiSequence.gotoFirstItem().good())
122  {
123  MITK_ERROR << "Error reading the structure sequence" << std::endl;
124  RTStructureSetReader::ContourModelSetNodes x;
125  return x;
126  }
127 
128  do
129  {
130  DRTStructureSetROISequence::Item& currentSequence =
131  roiSequence.getCurrentItem();
132 
133  if (!currentSequence.isValid())
134  {
135  continue;
136  }
137 
138  OFString roiName;
139  OFString roiDescription;
140  Sint32 roiNumber;
141  RoiEntry roi;
142 
143  currentSequence.getROIName(roiName);
144  currentSequence.getROIDescription(roiDescription);
145  currentSequence.getROINumber(roiNumber);
146 
147  roi.Name = roiName.c_str();
148  roi.Description = roiDescription.c_str();
149  roi.Number = roiNumber;
150 
151  this->ROISequenceVector.push_back(roi);
152  } while (roiSequence.gotoNextItem().good());
153 
154  Sint32 refRoiNumber;
155  DRTROIContourSequence& roiContourSeqObject =
156  structureSetObject.getROIContourSequence();
157 
158  if (!roiContourSeqObject.gotoFirstItem().good())
159  {
160  MITK_ERROR << "Error reading the contour sequence" << std::endl;
161  RTStructureSetReader::ContourModelSetNodes x;
162  return x;
163  }
164 
165  do
166  {
168  DRTROIContourSequence::Item& currentRoiObject =
169  roiContourSeqObject.getCurrentItem();
170 
171  if (!currentRoiObject.isValid())
172  {
173  continue;
174  }
175 
176  currentRoiObject.getReferencedROINumber(refRoiNumber);
177  DRTContourSequence& contourSeqObject =
178  currentRoiObject.getContourSequence();
179 
180  if (contourSeqObject.gotoFirstItem().good())
181  {
182  do
183  {
184  DRTContourSequence::Item& contourItem =
185  contourSeqObject.getCurrentItem();
186 
187  if (!contourItem.isValid())
188  {
189  continue;
190  }
191 
192  OFString contourNumber;
193  OFString numberOfPoints;
194  OFVector<Float64> contourData_LPS;
195  mitk::ContourModel::Pointer contourSequence =
197 
198  contourItem.getContourNumber(contourNumber);
199  contourItem.getNumberOfContourPoints(numberOfPoints);
200  contourItem.getContourData(contourData_LPS);
201 
202  for (unsigned int i = 0; i < contourData_LPS.size() / 3; i++)
203  {
204  mitk::Point3D point;
205  point[0] = contourData_LPS.at(3 * i);
206  point[1] = contourData_LPS.at(3 * i + 1);
207  point[2] = contourData_LPS.at(3 * i + 2);
208  contourSequence->AddVertex(point);
209  }
210 
211  contourSequence->Close();
212  contourSet->AddContourModel(contourSequence);
213  } while (contourSeqObject.gotoNextItem().good());
214  }
215  else
216  {
217  MITK_ERROR << "Error reading contourSeqObject" << std::endl;
218  }
219 
220  RoiEntry* refROI = this->FindRoiByNumber(refRoiNumber);
221 
222  if (refROI == NULL)
223  {
224  MITK_ERROR << "Can not find references ROI" << std::endl;
225  continue;
226  }
227 
228  Sint32 roiColor;
229 
230  for (int j = 0; j < 3; j++)
231  {
232  currentRoiObject.getROIDisplayColor(roiColor, j);
233  refROI->DisplayColor[j] = roiColor / 255.0;
234  }
235 
236  refROI->ContourModelSet = contourSet;
237  contourSet->SetProperty("name", mitk::StringProperty::New(refROI->Name));
238  contourSet->SetProperty("contour.color", mitk::ColorProperty::New(
239  refROI->DisplayColor[0],
240  refROI->DisplayColor[1],
241  refROI->DisplayColor[2]));
242 
243  //add uid and patient uid to property
244  OFString uid;
245  structureSetObject.getSeriesInstanceUID(uid);
246  std::string uidPropertyName = mitk::DICOMTagPathToPropertyName(mitk::DICOMTagPath(0x0020, 0x000e));
247  contourSet->SetProperty(uidPropertyName.c_str(), mitk::StringProperty::New(uid.c_str()));
248 
249  OFString patientUid;
250  structureSetObject.getPatientID(patientUid);
251  std::string patientUidPropertyName = mitk::DICOMTagPathToPropertyName(mitk::DICOMTagPath(0x0010,
252  0x0020));
253  contourSet->SetProperty(patientUidPropertyName.c_str(),
254  mitk::StringProperty::New(patientUid.c_str()));
255 
256  } while (roiContourSeqObject.gotoNextItem().good());
257 
258  std::deque<mitk::DataNode::Pointer> nodes;
259 
260  for (unsigned int i = 0; i < ROISequenceVector.size(); i++)
261  {
263  node->SetData(ROISequenceVector.at(i).ContourModelSet);
264  node->SetProperty("name", ROISequenceVector.at(i).ContourModelSet->GetProperty("name"));
265  node->SetProperty("color", ROISequenceVector.at(i).ContourModelSet->GetProperty("contour.color"));
266  node->SetProperty("contour.color",
267  ROISequenceVector.at(i).ContourModelSet->GetProperty("contour.color"));
268  node->SetProperty("includeInBoundingBox", mitk::BoolProperty::New(false));
269  node->SetVisibility(true, mitk::BaseRenderer::GetInstance(
270  mitk::BaseRenderer::GetRenderWindowByName("stdmulti.widget1")));
271  node->SetVisibility(false, mitk::BaseRenderer::GetInstance(
272  mitk::BaseRenderer::GetRenderWindowByName("stdmulti.widget2")));
273  node->SetVisibility(false, mitk::BaseRenderer::GetInstance(
274  mitk::BaseRenderer::GetRenderWindowByName("stdmulti.widget3")));
275  node->SetVisibility(true, mitk::BaseRenderer::GetInstance(
276  mitk::BaseRenderer::GetRenderWindowByName("stdmulti.widget4")));
277 
278  nodes.push_back(node);
279  }
280 
281  return nodes;
282  }
283 }
Class is used to identify (nested) attributes in a DICOM dataset. In contrast to the class DICOMTag...
RoiEntry * FindRoiByNumber(unsigned int roiNum)
static BaseRenderer * GetInstance(vtkRenderWindow *renWin)
static vtkRenderWindow * GetRenderWindowByName(const std::string &name)
#define MITK_ERROR
Definition: mitkLogMacros.h:24
static Pointer New()
DataCollection - Class to facilitate loading/accessing structured data.
std::vector< RoiEntry > ROISequenceVector
static Pointer New()
MITKDICOMREADER_EXPORT std::string DICOMTagPathToPropertyName(const DICOMTagPath &tagPath)
static Pointer New()
static Pointer New()
static Pointer New()
static Pointer New()
ContourModelSetNodes ReadStructureSet(const char *filepath)
Reading a RT StructureSet from the DICOM file and returns the ROIs (region of interest) as a ContourM...