Medical Imaging Interaction Toolkit  2018.4.99-f51274ea
Medical Imaging Interaction Toolkit
mitkDataCollection.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 
14 #include <mitkDataCollection.h>
15 #include <mitkImageCast.h>
16 
17 // ITK
18 #include <itkImage.h>
19 #include <itkComposeImageFilter.h>
20 
22 {
23 }
24 
25 
27 {
28 }
29 
30 void
31 mitk::DataCollection::Init(std::string name)
32 {
33  m_Name = name;
34  m_Parent = nullptr;
35 }
36 
37 size_t mitk::DataCollection::AddData(DataObject::Pointer data, std::string name, std::string filePath)
38 {
39  mitk::DataCollection* dc = dynamic_cast<mitk::DataCollection*>(data.GetPointer());
40  if (dc) dc->SetParent(this);
41 
42  m_DataVector.push_back(data.GetPointer());
43  m_NameVector.push_back(name);
44  m_FilePathVector.push_back(filePath);
45  size_t lastIndex = m_DataVector.size()-1;
46  m_DataNames[name] = lastIndex;
47  return lastIndex;
48 }
49 
50 void
52 {
53  m_Name = name;
54 }
55 
56 std::string
58 {
59  return m_Name;
60 }
61 
62 std::string mitk::DataCollection::GetDataFilePath(size_t index) const
63 {
64  if (index >= m_FilePathVector.size())
65  mitkThrow() << "No element with the given index";
66  return m_FilePathVector[index];
67 }
68 
69 
70 size_t
72 {
73  size_t index;
74  try
75  {
76  index = m_DataNames.at(name); // only brackets [] cause map to return std element, which in this case is 0
77  }
78  catch (...)
79  {
80  mitkThrow() << "No element with the given name: " << name;
81  }
82  return index;
83 }
84 
85 std::string
87 {
88  if (index >= m_NameVector.size())
89  {
90  MITK_ERROR << "DataCollection Index does not exist";
91  mitkThrow() << "No element with the given index";
92  }
93  return m_NameVector[index];
94 }
95 
96 bool
98 {
99  bool isInside = (m_DataNames.find(name) != m_DataNames.end());
100  return isInside;
101 }
102 
103 bool
105 {
106  if (index >= m_NameVector.size())
107  return false;
108  return true;
109 }
110 
111 size_t
113 {
114  return m_DataVector.size();
115 }
116 
117 void
118 mitk::DataCollection::SetData(itk::DataObject::Pointer data, size_t index)
119 {
120  if (!HasElement(index))
121  mitkThrow() << "Unkown index. No Element with the given index";
122 
123  mitk::DataCollection::Pointer dc = dynamic_cast<mitk::DataCollection*>(data.GetPointer());
124  if (dc)
125  dc->SetParent(this);
126 
127  m_DataVector[index] = data;
128 }
129 
130 void
131 mitk::DataCollection::SetData(itk::DataObject::Pointer data, std::string name)
132 {
133  SetData(data, NameToIndex(name));
134 }
135 
136 itk::DataObject::Pointer
138 {
139  if (!HasElement(index))
140  mitkThrow() << "Unkown index. No Element with the given index";
141 
142  return m_DataVector[index];
143 }
144 
145 itk::DataObject::Pointer
147 {
148  return GetData(NameToIndex(name));
149 }
150 
151 template <typename TPixel>
153 ConvertItkObjectToMitkImage(itk::DataObject * itkData, mitk::Image::Pointer mitkImage)
154 {
155  if (mitkImage.IsNull())
156  {
157  typename itk::Image<TPixel, 3>::Pointer itkImage = dynamic_cast<itk::Image<TPixel, 3> *>(itkData);
158  if (itkImage.IsNotNull())
159  {
160  mitk::CastToMitkImage(itkImage,mitkImage);
161  }
162  }
163  if (mitkImage.IsNull())
164  {
165  typename itk::Image<TPixel, 2>::Pointer itkImage = dynamic_cast<itk::Image<TPixel, 2>*>(itkData);
166  if (itkImage.IsNotNull())
167  {
168  mitk::CastToMitkImage(itkImage,mitkImage);
169  }
170  }
171  return mitkImage;
172 }
173 
174 
177 {
178  itk::DataObject* itkData = GetData(index).GetPointer();
179  mitk::Image::Pointer mitkImage =
180  dynamic_cast<mitk::Image*>(itkData);
181 
182  mitkImage = ConvertItkObjectToMitkImage<double>(itkData, mitkImage);
183  mitkImage = ConvertItkObjectToMitkImage<float>(itkData, mitkImage);
184  mitkImage = ConvertItkObjectToMitkImage<unsigned char>(itkData, mitkImage);
185  mitkImage = ConvertItkObjectToMitkImage<signed char>(itkData, mitkImage);
186  mitkImage = ConvertItkObjectToMitkImage<unsigned short>(itkData, mitkImage);
187  mitkImage = ConvertItkObjectToMitkImage<signed short>(itkData, mitkImage);
188  mitkImage = ConvertItkObjectToMitkImage<unsigned int>(itkData, mitkImage);
189  mitkImage = ConvertItkObjectToMitkImage<signed int>(itkData, mitkImage);
190  mitkImage = ConvertItkObjectToMitkImage<unsigned long>(itkData, mitkImage);
191  mitkImage = ConvertItkObjectToMitkImage<signed long>(itkData, mitkImage);
192 
193  return mitkImage;
194 }
195 
198 {
199  return GetMitkImage(NameToIndex(name));
200 }
201 
202 template <class ImageType>
204 {
205  Image* image = dynamic_cast<Image*> (GetData(index).GetPointer());
206  if (image != nullptr)
207  {
208  CastToItkImage(image, itkImage );
209  }
210  else if (nullptr != dynamic_cast<ImageType*>(GetData(index)))
211  itkImage = dynamic_cast<ImageType*>(GetData(index));
212 }
213 
214 template <class ImageType>
216 {
217  Image* image = dynamic_cast<Image*> (GetData(NameToIndex(name)).GetPointer());
218  if (image != nullptr)
219  {
220  CastToItkImage(image, itkImage );
221  }
222  else if (nullptr != dynamic_cast<ImageType*> (GetData(NameToIndex(name))))
223  itkImage = dynamic_cast<ImageType*> (GetData(NameToIndex(name)));
224 }
225 
226 itk::DataObject::Pointer&
228 {
229  return m_DataVector[index];
230 }
231 
232 itk::DataObject::Pointer&
234 {
235  return operator[](NameToIndex(name));
236 }
237 
238 void mitk::DataCollection::SetXMLFile(std::string absoluteXMlFile)
239 {
240  m_XMLFile = absoluteXMlFile;
241 }
242 
244 {
245  return m_XMLFile;
246 }
247 
249 {
250  m_Parent = parent;
251 }
252 
255 {
256  return m_Parent;
257 }
258 
259 void mitk::DataCollection::SetNameForIndex(size_t index, std::string &name)
260 {
261  std::map<std::string, size_t>::iterator it = m_DataNames.find(m_NameVector[index]);
262  m_DataNames.erase(it);
263  m_DataNames[name] = index;
264  m_NameVector[index] = name;
265 
266 }
267 
269 {
270  if (!HasElement(index))
271  return false;
272 
273  m_DataVector.erase(m_DataVector.begin() + index);
274  m_NameVector.erase(m_NameVector.begin() + index);
275  m_FilePathVector.erase(m_FilePathVector.begin() + index);
276 
277  m_DataNames.clear();
278  for (size_t i = 0; i < m_NameVector.size(); ++i)
279  {
280  m_DataNames[m_NameVector[i]] = i;
281  }
282 
283  return true;
284 }
285 
286 bool mitk::DataCollection::RemoveElement(std::string &name)
287 {
288  if (!HasElement(name))
289  return false;
290 
291  return RemoveIndex(NameToIndex(name));
292 }
293 
295 {
296  for (std::vector<itk::DataObject::Pointer>::iterator it = m_DataVector.begin(); it != m_DataVector.end(); ++it)
297  {
298  DataCollection* col = dynamic_cast<DataCollection*>((*it).GetPointer()) ;
299  if (col != nullptr)
300  col->Clear();
301  else
302  *it = nullptr;
303 
304  }
305  m_Parent = nullptr;
306 }
307 
308 /* Superclass methods, that need to be implemented */
310 {
311 
312 }
313 
315 {
316 }
317 
319 {
320  return false;
321 }
322 
324 {
325  return false;
326 }
327 
328 void mitk::DataCollection::SetRequestedRegion(const itk::DataObject *)
329 {
330 
331 }
332 
334 {
335  mitk::BaseData::Pointer data = dynamic_cast<mitk::BaseData*>(GetMitkImage(index).GetPointer());
336 
337  if (data.IsNull())
338  {
339  data = dynamic_cast<mitk::BaseData*>(GetData(index).GetPointer());
340  }
342  node->SetName(IndexToName(index));
343  node->SetData(data);
344 
345  return node;
346 }
347 
349 {
350  return GetDataNode(NameToIndex(name));
351 }
352 
353 mitk::Image::Pointer mitk::DataCollection::GetProbabilityMap(std::vector<std::string> probabilityNamesVector)
354 {
355  typedef itk::Image<double, 3> ImageType;
356  typedef itk::VectorImage<double, 3> VectorImageType;
357  typedef itk::ComposeImageFilter<ImageType, VectorImageType> ComposeFilterType;
358 
359  ComposeFilterType::Pointer composer = ComposeFilterType::New();
360  int i = 0;
361  for (std::vector<std::string>::iterator it = probabilityNamesVector.begin(); it != probabilityNamesVector.end(); ++it)
362  {
363  ImageType* img = dynamic_cast<ImageType*>(this->GetData((*it)).GetPointer());
364  if (img != nullptr)
365  {
366  composer->SetInput(i, img);
367  ++i;
368  }
369  }
370  composer->Update();
371  VectorImageType::Pointer vecImage = composer->GetOutput();
372 
374  probImage->InitializeByItk(vecImage.GetPointer(), vecImage->GetNumberOfComponentsPerPixel());
375  probImage->SetVolume(vecImage->GetBufferPointer());
376 
377  return probImage;
378 }
vcl_size_t Size() const
Size - number of data items in collection.
mitk::DataNode::Pointer GetDataNode(vcl_size_t index)
GetDataNode - returns data node containing data at index.
bool RequestedRegionIsOutsideOfTheBufferedRegion() override
Determine whether the RequestedRegion is outside of the BufferedRegion.
void Init(std::string name)
Base of all data objects.
Definition: mitkBaseData.h:42
bool VerifyRequestedRegion() override
Verify that the RequestedRegion is within the LargestPossibleRegion.
itk::Image< unsigned char, 3 > ImageType
#define MITK_ERROR
Definition: mitkLogMacros.h:20
vcl_size_t AddData(DataObject::Pointer data, std::string name, std::string filePath="")
AddData Add a data item.
bool RemoveIndex(vcl_size_t index)
RemoveIndex - removes element at index.
void SetRequestedRegionToLargestPossibleRegion() override
Set the RequestedRegion to the LargestPossibleRegion.
vcl_size_t NameToIndex(std::string name)
NameToIndex - Get index from data item name.
mitk::Image::Pointer GetMitkImage(vcl_size_t index)
GetMitkImage - casts data to mitk::Image and returns it.
std::string GetDataFilePath(vcl_size_t index) const
mitk::DataCollection * GetParent()
GetParent - returns the parent collection if available else null is returned.
void SetXMLFile(std::string absoluteXMlFile)
SetXMLFile - sets xml file to which data collection is saved.
void SetData(itk::DataObject::Pointer data, vcl_size_t index)
SetData - set/update data item by index.
mitk::Image::Pointer ConvertItkObjectToMitkImage(itk::DataObject *itkData, mitk::Image::Pointer mitkImage)
bool HasElement(std::string name)
HasElement - check if element with this name exists in collection.
static Pointer New()
#define mitkThrow()
void SetRequestedRegion(const itk::DataObject *) override
Set the requested region from this data object to match the requested region of the data object passe...
Image class for storing images.
Definition: mitkImage.h:72
mitk::Image::Pointer image
void Clear() override
Clear - clears the data collection.
static Pointer New()
void SetNameForIndex(vcl_size_t index, std::string &name)
SetNameForIndex - sets name for given data item by index.
bool RemoveElement(std::string &name)
RemoveElement - removes element with name.
std::string IndexToName(vcl_size_t index) const
IndexToName - Get name from index.
ImageType GetItkImage(vcl_size_t index, ImageType *itkImage)
GetMitkImage - casts data to privided itk::Image pointer.
std::string GetName() const
void CastToMitkImage(const itk::SmartPointer< ItkOutputImageType > &itkimage, itk::SmartPointer< mitk::Image > &mitkoutputimage)
Cast an itk::Image (with a specific type) to an mitk::Image.
Definition: mitkImageCast.h:74
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
void SetName(std::string name)
SetName - Sets name of DataCollection.
std::string GetXMLFile()
SetXMLFile - gets xml file to which data collection is supposed to be saved.
void UpdateOutputInformation() override
itk::DataObject::Pointer GetData(vcl_size_t index)
GetData Get original data by index.
itk::DataObject::Pointer & operator[](vcl_size_t index)
mitk::Image::Pointer GetProbabilityMap(std::vector< std::string > probabilityNamesVector)
GetProbabilityMap - returns vectorimage generated out of images with names in the probabilityNamesVec...
void SetParent(mitk::DataCollection *parent)
SetParent - sets the parent collection.