Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkDICOMHelper.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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 // semantic relations module
14 #include "mitkDICOMHelper.h"
16 #include "mitkUIDGeneratorBoost.h"
17 
18 // mitk core
19 #include <mitkPropertyNameHelper.h>
20 
21 // c++
22 #include <algorithm>
23 
25 {
26  // date expected to be YYYYMMDD (8 characters)
27  if (dateAsString.size() != 8)
28  {
29  // string does not represent a DICOM date
30  mitkThrowException(mitk::SemanticRelationException) << "Not a valid DICOM date format.";
31  }
32 
34  controlPoint.SetDateFromString(dateAsString);
35 
36  return controlPoint;
37 }
38 
40 {
41  // extract suitable DICOM tag to use as the case id
42  // two alternatives can be used:
43  // - DICOM tag "0x0010, 0x0010" is PatientName
44  // - DICOM tag "0x0010, 0x0020" is PatientID
45  // in the current implementation the PatientName (0x0010, 0x0010) is used
46  return GeneratePropertyNameForDICOMTag(0x0010, 0x0010);
47 }
48 
50 {
51  // extract suitable DICOM tag to use as the data node id
52  // DICOM tag "0x0020, 0x000e" is SeriesInstanceUID
53  return GeneratePropertyNameForDICOMTag(0x0020, 0x000e);
54 }
55 
57 {
58  // extract suitable DICOM tag to use as the data node id
59  // DICOM tag "0x0008, 0x0022" is AcquisitionDate
60  return GeneratePropertyNameForDICOMTag(0x0008, 0x0022);
61 }
62 
64 {
65  // extract suitable DICOM tag to use as the information type
66  // DICOM tag "0x0008, 0x0060" is Modality
67  return GeneratePropertyNameForDICOMTag(0x0008, 0x0060);
68 }
69 
71 {
72  if (nullptr == dataNode)
73  {
74  mitkThrowException(SemanticRelationException) << "Not a valid data node.";
75  }
76 
77  BaseData* baseData = dataNode->GetData();
78  if (nullptr == baseData)
79  {
80  mitkThrowException(SemanticRelationException) << "No valid base data.";
81  }
82 
83  BaseProperty* dicomTag = baseData->GetProperty(GetCaseIDDICOMProperty().c_str());
84  if (nullptr == dicomTag)
85  {
86  mitkThrowException(SemanticRelationException) << "Not a valid DICOM property.";
87  }
88 
89  std::string dicomTagAsString = dicomTag->GetValueAsString();
90  return dicomTagAsString;
91 }
92 
94 {
95  if (nullptr == dataNode)
96  {
97  mitkThrowException(SemanticRelationException) << "Not a valid data node.";
98  }
99 
100  BaseData* baseData = dataNode->GetData();
101  if (nullptr == baseData)
102  {
103  mitkThrowException(SemanticRelationException) << "No valid base data.";
104  }
105 
106  BaseProperty* dicomTag = baseData->GetProperty(GetNodeIDDICOMProperty().c_str());
107  if (nullptr == dicomTag)
108  {
109  mitkThrowException(SemanticRelationException) << "Not a valid DICOM property.";
110  }
111  std::string dicomTagAsString = dicomTag->GetValueAsString();
112  return dicomTagAsString;
113 }
114 
116 {
117  if (nullptr == dataNode)
118  {
119  mitkThrowException(SemanticRelationException) << "Not a valid data node.";
120  }
121 
122  BaseData* baseData = dataNode->GetData();
123  if (nullptr == baseData)
124  {
125  mitkThrowException(SemanticRelationException) << "No valid base data.";
126  }
127 
128  BaseProperty* acquisitionDateProperty = baseData->GetProperty(GetDateDICOMProperty().c_str());
129  if (nullptr == acquisitionDateProperty)
130  {
131  mitkThrowException(SemanticRelationException) << "Not a valid DICOM property.";
132  }
133  std::string acquisitionDateAsString = acquisitionDateProperty->GetValueAsString();
134 
135  SemanticTypes::ControlPoint controlPoint;
136  try
137  {
138  controlPoint = GetControlPointFromString(acquisitionDateAsString);
139  }
140  catch (SemanticRelationException &e)
141  {
142  mitkReThrow(e) << "Cannot retrieve a valid DICOM date.";
143  }
144 
145  return controlPoint;
146 }
147 
149 {
150  if (nullptr == dataNode)
151  {
152  mitkThrowException(SemanticRelationException) << "Not a valid data node.";
153  }
154 
155  BaseData* baseData = dataNode->GetData();
156  if (nullptr == baseData)
157  {
158  mitkThrowException(SemanticRelationException) << "No valid base data.";
159  }
160 
161  BaseProperty* dicomTag = baseData->GetProperty(GetModalityDICOMProperty().c_str());
162  if (nullptr == dicomTag)
163  {
164  mitkThrowException(SemanticRelationException) << "Not a valid DICOM property.";
165  }
166  std::string dicomTagAsString = dicomTag->GetValueAsString();
167  return dicomTagAsString;
168 }
169 
170 std::string mitk::TrimDICOM(const std::string& identifier)
171 {
172  if (identifier.empty())
173  {
174  return identifier;
175  }
176 
177  // leading whitespace
178  std::size_t first = identifier.find_first_not_of(' ');
179  if (std::string::npos == first)
180  {
181  return "";
182  }
183  // trailing whitespace
184  std::size_t last = identifier.find_last_not_of(' ');
185  return identifier.substr(first, last - first + 1);
186 }
Base of all data objects.
Definition: mitkBaseData.h:37
void SetDateFromString(const std::string &dateAsString)
std::string MITKCORE_EXPORT GeneratePropertyNameForDICOMTag(unsigned int group, unsigned int element)
MITKSEMANTICRELATIONS_EXPORT std::string GetCaseIDDICOMProperty()
Creates a property name for a DICOM tag. The tag is "0x0010, 0x0010" (PatientName) ...
#define mitkReThrow(mitkexception)
MITKSEMANTICRELATIONS_EXPORT std::string GetNodeIDDICOMProperty()
Creates a property name for a DICOM tag. The tag is "0x0020, 0x000e" (SeriesInstanceUID) ...
MITKSEMANTICRELATIONS_EXPORT SemanticTypes::CaseID GetCaseIDFromDataNode(const mitk::DataNode *dataNode)
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
virtual std::string GetValueAsString() const
Abstract base class for properties.
MITKSEMANTICRELATIONS_EXPORT SemanticTypes::ID GetIDFromDataNode(const mitk::DataNode *dataNode)
mitk::BaseProperty::Pointer GetProperty(const char *propertyKey) const
Get the property (instance of BaseProperty) with key propertyKey from the PropertyList, and set it to this, respectively;.
#define mitkThrowException(classname)
std::string InformationType
MITKSEMANTICRELATIONS_EXPORT SemanticTypes::InformationType GetDICOMModalityFromDataNode(const mitk::DataNode *dataNode)
Extracts a specific DICOM tag from the node&#39;s base data and returns the tag as a information type (a ...
MITKSEMANTICRELATIONS_EXPORT std::string GetModalityDICOMProperty()
Creates a property name for a DICOM tag. The tag is "0x0008, 0x0060" (Modality)
MITKSEMANTICRELATIONS_EXPORT SemanticTypes::ControlPoint GetDICOMDateFromDataNode(const mitk::DataNode *dataNode)
MITKSEMANTICRELATIONS_EXPORT std::string GetDateDICOMProperty()
Creates a property name for a DICOM tag. The tag is "0x0008, 0x0022" (AcquisitionDate) ...
MITKSEMANTICRELATIONS_EXPORT std::string TrimDICOM(const std::string &identifier)
mitk::SemanticTypes::ControlPoint GetControlPointFromString(const std::string &dateAsString)
Class for nodes of the DataTree.
Definition: mitkDataNode.h:64