Medical Imaging Interaction Toolkit  2018.4.99-b585543d
Medical Imaging Interaction Toolkit
mitkDataCollectionSingleImageIterator.cxx
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 #ifndef mitkDataCollectionSingleImageIterator_CXX
14 #define mitkDataCollectionSingleImageIterator_CXX
15 
17 #include <mitkImageCast.h>
18 
19 template <typename TDataType, int TImageDimension>
21 DataCollectionSingleImageIterator(DataCollection *collection, std::string imageName) :
22  m_Collection(collection),
23  m_ImageName(imageName),
24  m_IsAtEnd(false),
25  m_IteratingImages(true),
26  m_CurrentIndex(0),
27  m_CurrentElement(0),
28  m_CurrentSingleCollectionIterator(nullptr)
29 {
30  ToBegin();
31 }
32 
33 template <typename TDataType, int TImageDimension>
34 void
37 {
38  m_IsAtEnd = false;
39  m_Image = nullptr;
40  m_IteratingImages = false;
41  m_CurrentIndex = 0;
42  m_CurrentElement = 0;
43 
44  if (m_Collection->HasElement(m_ImageName))
45  {
46  {
47  mitk::Image *image = dynamic_cast<mitk::Image*>(m_Collection->GetData(m_ImageName).GetPointer());
48  if (image != nullptr)
49  {
50  typename ImageType::Pointer itkImage = ImageType::New();
51  mitk::CastToItkImage(image, itkImage);
52  itk::DataObject::Pointer itkObject = dynamic_cast<itk::DataObject* >(itkImage.GetPointer());
53  m_Collection->SetData(itkObject, m_ImageName);
54  }
55  }
56  ImageType * image = dynamic_cast<ImageType*>(m_Collection->GetData(m_ImageName).GetPointer());
57  if (image != nullptr)
58  {
59  m_Image = image;
60  }
61  }
62  if (m_Image == nullptr)
63  {
64  m_CurrentElement = 0;
65  m_CurrentSingleCollectionIterator = GetNextDataCollectionIterator(m_CurrentElement);
66  if (m_Image == nullptr)
67  {
68  m_IsAtEnd = true;
69  } else
70  {
71  m_Image = m_CurrentSingleCollectionIterator->GetImage();
72  }
73  }
74 }
75 
76 template <typename TDataType, int TImageDimension>
77 bool
80 {
81  return m_IsAtEnd;
82 }
83 
84 template <typename TDataType, int TImageDimension>
85 void
88 {
89  ++m_CurrentIndex;
90  NextObject();
91 }
92 
93 template <typename TDataType, int TImageDimension>
94 void
97 {
98  ++m_CurrentIndex;
99  NextObject();
100 }
101 
102 template <typename TDataType, int TImageDimension>
103 size_t
106 {
107  return m_CurrentIndex;
108 }
109 
110 template <typename TDataType, int TImageDimension>
111 std::string
114 {
115  if (m_IteratingImages)
116  {
117  return m_ImageName;
118  }
119  else
120  {
121  return m_Collection->IndexToName(m_CurrentElement) + "/" + m_CurrentSingleCollectionIterator->GetFilePrefix();
122  }
123 }
124 
125 template <typename TDataType, int TImageDimension>
128 {
129  if (m_Collection->HasElement(m_ImageName))
130  {
131  if (m_CurrentSingleCollectionIterator != nullptr)
132  {
133  delete m_CurrentSingleCollectionIterator;
134  m_CurrentSingleCollectionIterator = nullptr;
135  }
136  m_CurrentElement = 0;
137  m_Image = nullptr;
138  m_CurrentSingleCollectionIterator = GetNextDataCollectionIterator(m_CurrentElement);
139  if (m_CurrentSingleCollectionIterator == nullptr)
140  {
141  m_IsAtEnd = true;
142  return;
143  }
144  }
145  else
146  {
147  m_CurrentSingleCollectionIterator->NextObject();
148  if (m_CurrentSingleCollectionIterator->IsAtEnd()) //Current collection is finished iterated
149  {
150  delete m_CurrentSingleCollectionIterator;
151  m_CurrentSingleCollectionIterator = GetNextDataCollectionIterator(m_CurrentElement+1);
152  }
153  if (m_CurrentSingleCollectionIterator == nullptr) //If no collection is known
154  {
155  m_IsAtEnd = true;
156  return;
157  }
158  }
159 }
160 
161 template <typename TDataType, int TImageDimension>
165 {
166  return m_Image;
167 }
168 
169 template <typename TDataType, int TImageDimension>
170 void
172 AddImage(ImageType *image, std::string name)
173 {
174  if (m_Collection->HasElement(m_ImageName))
175  {
176  itk::DataObject::Pointer dataObject = dynamic_cast<itk::DataObject*>(image);
177  m_Collection->AddData(dataObject, name);
178  } else
179  {
180  m_CurrentSingleCollectionIterator->AddImage(image, name);
181  }
182 }
183 
184 template <typename TDataType, int TImageDimension>
187 GetNextDataCollectionIterator(size_t start)
188 {
190  size_t index =start;
191  while (index < m_Collection->Size() && iterator == nullptr)
192  {
193  DataCollection* collection;
194  collection = dynamic_cast<DataCollection*>(m_Collection->GetData(index).GetPointer());
195  if (collection != nullptr)
196  {
197  iterator = new DataCollectionSingleImageIterator<TDataType, TImageDimension>(collection, m_ImageName);
198  if (iterator->IsAtEnd())
199  {
200  delete iterator;
201  iterator = nullptr;
202  ++index;
203  }
204  }
205  else
206  {
207  ++index;
208  }
209  }
210  m_CurrentElement = index;
211  if (iterator != nullptr)
212  m_Image = iterator->GetImage();
213  return iterator;
214 }
215 
216 // mitkDataCollectionSingleImageIterator_CXX
217 #endif
vcl_size_t AddData(DataObject::Pointer data, std::string name, std::string filePath="")
AddData Add a data item.
DataCollectionSingleImageIterator(mitk::DataCollection *collection, std::string imageName)
void SetData(itk::DataObject::Pointer data, vcl_size_t index)
SetData - set/update data item by index.
bool HasElement(std::string name)
HasElement - check if element with this name exists in collection.
Image class for storing images.
Definition: mitkImage.h:72
mitk::Image::Pointer image
std::string IndexToName(vcl_size_t index) const
IndexToName - Get name from index.
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
itk::DataObject::Pointer GetData(vcl_size_t index)
GetData Get original data by index.