Medical Imaging Interaction Toolkit  2016.11.0
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,
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 
17 
18 #include <mitkDataCollection.h>
19 #include <mitkImageCast.h>
20 
21 // ITK
22 #include <itkImage.h>
23 #include <itkComposeImageFilter.h>
24 
26 {
27 }
28 
29 
31 {
32 }
33 
34 void
35 mitk::DataCollection::Init(std::string name)
36 {
37  m_Name = name;
38  m_Parent = NULL;
39 }
40 
41 size_t mitk::DataCollection::AddData(DataObject::Pointer data, std::string name, std::string filePath)
42 {
43  mitk::DataCollection* dc = dynamic_cast<mitk::DataCollection*>(data.GetPointer());
44  if (dc) dc->SetParent(this);
45 
46  m_DataVector.push_back(data.GetPointer());
47  m_NameVector.push_back(name);
48  m_FilePathVector.push_back(filePath);
49  size_t lastIndex = m_DataVector.size()-1;
50  m_DataNames[name] = lastIndex;
51  return lastIndex;
52 }
53 
54 void
56 {
57  m_Name = name;
58 }
59 
60 std::string
62 {
63  return m_Name;
64 }
65 
66 std::string mitk::DataCollection::GetDataFilePath(size_t index) const
67 {
68  if (index >= m_FilePathVector.size())
69  mitkThrow() << "No element with the given index";
70  return m_FilePathVector[index];
71 }
72 
73 
74 size_t
76 {
77  size_t index;
78  try
79  {
80  index = m_DataNames.at(name); // only brackets [] cause map to return std element, which in this case is 0
81  }
82  catch (...)
83  {
84  mitkThrow() << "No element with the given name: " << name;
85  }
86  return index;
87 }
88 
89 std::string
91 {
92  if (index >= m_NameVector.size())
93  {
94  MITK_ERROR << "DataCollection Index does not exist";
95  mitkThrow() << "No element with the given index";
96  }
97  return m_NameVector[index];
98 }
99 
100 bool
102 {
103  bool isInside = (m_DataNames.find(name) != m_DataNames.end());
104  return isInside;
105 }
106 
107 bool
109 {
110  if (index >= m_NameVector.size())
111  return false;
112  return true;
113 }
114 
115 size_t
117 {
118  return m_DataVector.size();
119 }
120 
121 void
123 {
124  if (!HasElement(index))
125  mitkThrow() << "Unkown index. No Element with the given index";
126 
127  mitk::DataCollection::Pointer dc = dynamic_cast<mitk::DataCollection*>(data.GetPointer());
128  if (dc)
129  dc->SetParent(this);
130 
131  m_DataVector[index] = data;
132 }
133 
134 void
136 {
137  SetData(data, NameToIndex(name));
138 }
139 
142 {
143  if (!HasElement(index))
144  mitkThrow() << "Unkown index. No Element with the given index";
145 
146  return m_DataVector[index];
147 }
148 
151 {
152  return GetData(NameToIndex(name));
153 }
154 
155 template <typename TPixel>
157 ConvertItkObjectToMitkImage(itk::DataObject * itkData, mitk::Image::Pointer mitkImage)
158 {
159  if (mitkImage.IsNull())
160  {
161  typename itk::Image<TPixel, 3>::Pointer itkImage = dynamic_cast<itk::Image<TPixel, 3> *>(itkData);
162  if (itkImage.IsNotNull())
163  {
164  mitk::CastToMitkImage(itkImage,mitkImage);
165  }
166  }
167  if (mitkImage.IsNull())
168  {
169  typename itk::Image<TPixel, 2>::Pointer itkImage = dynamic_cast<itk::Image<TPixel, 2>*>(itkData);
170  if (itkImage.IsNotNull())
171  {
172  mitk::CastToMitkImage(itkImage,mitkImage);
173  }
174  }
175  return mitkImage;
176 }
177 
178 
181 {
182  itk::DataObject* itkData = GetData(index).GetPointer();
183  mitk::Image::Pointer mitkImage =
184  dynamic_cast<mitk::Image*>(itkData);
185 
186  mitkImage = ConvertItkObjectToMitkImage<double>(itkData, mitkImage);
187  mitkImage = ConvertItkObjectToMitkImage<float>(itkData, mitkImage);
188  mitkImage = ConvertItkObjectToMitkImage<unsigned char>(itkData, mitkImage);
189  mitkImage = ConvertItkObjectToMitkImage<signed char>(itkData, mitkImage);
190  mitkImage = ConvertItkObjectToMitkImage<unsigned short>(itkData, mitkImage);
191  mitkImage = ConvertItkObjectToMitkImage<signed short>(itkData, mitkImage);
192  mitkImage = ConvertItkObjectToMitkImage<unsigned int>(itkData, mitkImage);
193  mitkImage = ConvertItkObjectToMitkImage<signed int>(itkData, mitkImage);
194  mitkImage = ConvertItkObjectToMitkImage<unsigned long>(itkData, mitkImage);
195  mitkImage = ConvertItkObjectToMitkImage<signed long>(itkData, mitkImage);
196 
197  return mitkImage;
198 }
199 
202 {
203  return GetMitkImage(NameToIndex(name));
204 }
205 
206 template <class ImageType>
208 {
209  Image* image = dynamic_cast<Image*> (GetData(index).GetPointer());
210  if (image != NULL)
211  {
212  CastToItkImage(image, itkImage );
213  }
214  else if (NULL != dynamic_cast<ImageType*>(GetData(index)))
215  itkImage = dynamic_cast<ImageType*>(GetData(index));
216 }
217 
218 template <class ImageType>
220 {
221  Image* image = dynamic_cast<Image*> (GetData(NameToIndex(name)).GetPointer());
222  if (image != NULL)
223  {
224  CastToItkImage(image, itkImage );
225  }
226  else if (NULL != dynamic_cast<ImageType*> (GetData(NameToIndex(name))))
227  itkImage = dynamic_cast<ImageType*> (GetData(NameToIndex(name)));
228 }
229 
232 {
233  return m_DataVector[index];
234 }
235 
238 {
239  return operator[](NameToIndex(name));
240 }
241 
242 void mitk::DataCollection::SetXMLFile(std::string absoluteXMlFile)
243 {
244  m_XMLFile = absoluteXMlFile;
245 }
246 
248 {
249  return m_XMLFile;
250 }
251 
253 {
254  m_Parent = parent;
255 }
256 
259 {
260  return m_Parent;
261 }
262 
263 void mitk::DataCollection::SetNameForIndex(size_t index, std::string &name)
264 {
265  std::map<std::string, size_t>::iterator it = m_DataNames.find(m_NameVector[index]);
266  m_DataNames.erase(it);
267  m_DataNames[name] = index;
268  m_NameVector[index] = name;
269 
270 }
271 
273 {
274  if (!HasElement(index))
275  return false;
276 
277  m_DataVector.erase(m_DataVector.begin() + index);
278  m_NameVector.erase(m_NameVector.begin() + index);
279  m_FilePathVector.erase(m_FilePathVector.begin() + index);
280 
281  m_DataNames.clear();
282  for (size_t i = 0; i < m_NameVector.size(); ++i)
283  {
284  m_DataNames[m_NameVector[i]] = i;
285  }
286 
287  return true;
288 }
289 
290 bool mitk::DataCollection::RemoveElement(std::string &name)
291 {
292  if (!HasElement(name))
293  return false;
294 
295  return RemoveIndex(NameToIndex(name));
296 }
297 
299 {
300  for (std::vector<itk::DataObject::Pointer>::iterator it = m_DataVector.begin(); it != m_DataVector.end(); ++it)
301  {
302  DataCollection* col = dynamic_cast<DataCollection*>((*it).GetPointer()) ;
303  if (col != NULL)
304  col->Clear();
305  else
306  *it = NULL;
307 
308  }
309  m_Parent = NULL;
310 }
311 
312 /* Superclass methods, that need to be implemented */
314 {
315 
316 }
317 
319 {
320 }
321 
323 {
324  return false;
325 }
326 
328 {
329  return false;
330 }
331 
332 void mitk::DataCollection::SetRequestedRegion(const itk::DataObject *)
333 {
334 
335 }
336 
338 {
339  mitk::BaseData::Pointer data = dynamic_cast<mitk::BaseData*>(GetMitkImage(index).GetPointer());
340 
341  if (data.IsNull())
342  {
343  data = dynamic_cast<mitk::BaseData*>(GetData(index).GetPointer());
344  }
346  node->SetName(IndexToName(index));
347  node->SetData(data);
348 
349  return node;
350 }
351 
353 {
354  return GetDataNode(NameToIndex(name));
355 }
356 
357 mitk::Image::Pointer mitk::DataCollection::GetProbabilityMap(std::vector<std::string> probabilityNamesVector)
358 {
359  typedef itk::Image<double, 3> ImageType;
360  typedef itk::VectorImage<double, 3> VectorImageType;
361  typedef itk::ComposeImageFilter<ImageType, VectorImageType> ComposeFilterType;
362 
364  int i = 0;
365  for (std::vector<std::string>::iterator it = probabilityNamesVector.begin(); it != probabilityNamesVector.end(); ++it)
366  {
367  ImageType* img = dynamic_cast<ImageType*>(this->GetData((*it)).GetPointer());
368  if (img != NULL)
369  {
370  composer->SetInput(i, img);
371  ++i;
372  }
373  }
374  composer->Update();
375  VectorImageType::Pointer vecImage = composer->GetOutput();
376 
378  probImage->InitializeByItk(vecImage.GetPointer(), vecImage->GetNumberOfComponentsPerPixel());
379  probImage->SetVolume(vecImage->GetBufferPointer());
380 
381  return probImage;
382 }
mitk::DataNode::Pointer GetDataNode(vcl_size_t index)
GetDataNode - returns data node containing data at index.
itk::SmartPointer< Self > Pointer
virtual void SetRequestedRegion(const itk::DataObject *)
Set the requested region from this data object to match the requested region of the data object passe...
void Init(std::string name)
Base of all data objects.
Definition: mitkBaseData.h:39
#define MITK_ERROR
Definition: mitkLogMacros.h:24
vcl_size_t Size() const
Size - number of data items in collection.
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.
std::string GetName() const
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.
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.
virtual bool VerifyRequestedRegion()
Verify that the RequestedRegion is within the LargestPossibleRegion.
T::Pointer GetData(const std::string &name)
itk::VectorImage< float, 3 > VectorImageType
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.
map::core::discrete::Elements< 3 >::InternalImageType ImageType
virtual void SetRequestedRegionToLargestPossibleRegion()
Set the RequestedRegion to the LargestPossibleRegion.
static Pointer New()
virtual void UpdateOutputInformation()
std::string IndexToName(vcl_size_t index) const
IndexToName - Get name from index.
#define mitkThrow()
Image class for storing images.
Definition: mitkImage.h:76
virtual bool RequestedRegionIsOutsideOfTheBufferedRegion()
Determine whether the RequestedRegion is outside of the BufferedRegion.
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.
ImageType GetItkImage(vcl_size_t index, ImageType *itkImage)
GetMitkImage - casts data to privided itk::Image pointer.
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:78
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 Clear()
Clear - clears the data collection.
itk::DataObject::Pointer GetData(vcl_size_t index)
GetData Get original data by index.
std::string GetDataFilePath(vcl_size_t index) const
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.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.