Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkDataCollectionImageIterator.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 mitkDataCollectionImageIterator_CXX
14 #define mitkDataCollectionImageIterator_CXX
15 
17 #include <mitkImageCast.h>
18 
19 template <typename TDataType, int TImageDimension>
21  DataCollectionImageIterator(DataCollection::Pointer collection, std::string imageName) :
22  m_Collection(collection), m_ImageName(imageName), m_IsAtEnd(false),
23  m_IteratingImages(true), m_CurrentIndex(0),
24  m_CurrentElement(0), m_CurrentCollectionIterator(nullptr)
25 {
26  ToBegin();
27 }
28 
29 template <typename TDataType, int TImageDimension>
30 void
33 {
34  m_IsAtEnd = false;
35  m_IteratingImages = false;
36  m_CurrentIndex = 0;
37  m_CurrentElement = 0;
38  m_ImageIndex = 0;
39 
40  if (m_Collection->HasElement(m_ImageName))
41  {
42  {
43  mitk::Image *image = dynamic_cast<mitk::Image*>(m_Collection->GetData(m_ImageName).GetPointer());
44 
45  //TODO: check whether image is valid... image != 0 if empty smart pointer was inserted into collection!!!!
46  if (image != nullptr)
47  {
48  typename ImageType::Pointer itkImage = ImageType::New();
49  mitk::CastToItkImage(image, itkImage);
50  itk::DataObject::Pointer itkObject = dynamic_cast<itk::DataObject* >(itkImage.GetPointer());
51  m_Collection->SetData(itkObject, m_ImageName);
52  }
53  }
54  ImageType * image = dynamic_cast<ImageType*>(m_Collection->GetData(m_ImageName).GetPointer());
55  if (image != nullptr)
56  {
57  m_IteratingImages = true;
58  m_CurrentIterator = ImageIterator(image, image->GetLargestPossibleRegion());
59  }
60  }
61  if (!m_IteratingImages)
62  {
63  if (m_CurrentCollectionIterator != nullptr)
64  {
65  delete m_CurrentCollectionIterator;
66  m_CurrentCollectionIterator = nullptr;
67  }
68  m_CurrentElement = 0;
69  m_CurrentCollectionIterator = GetNextDataCollectionIterator(m_CurrentElement);
70  if (m_CurrentCollectionIterator == nullptr)
71  {
72  m_IsAtEnd = true;
73  } else
74  {
75  m_CurrentIterator = m_CurrentCollectionIterator->GetImageIterator();
76  }
77  }
78 }
79 
80 template <typename TDataType, int TImageDimension>
81 bool
84 {
85  return m_IsAtEnd;
86 }
87 
88 template <typename TDataType, int TImageDimension>
89 void
92 {
93  ++m_CurrentIndex;
94  ++m_CurrentIterator;
95 
96  if (m_CurrentIterator.IsAtEnd())
97  {
98  ++m_ImageIndex;
99  NextObject();
100  }
101 }
102 
103 template <typename TDataType, int TImageDimension>
104 void
107 {
108  ++m_CurrentIndex;
109  ++m_CurrentIterator;
110 
111  if (m_CurrentIterator.IsAtEnd())
112  {
113  ++m_ImageIndex;
114  NextObject();
115  }
116 }
117 
118 template <typename TDataType, int TImageDimension>
121  GetNextDataCollectionIterator(size_t start)
122 {
124  size_t index =start;
125  while (index < m_Collection->Size() && iterator == nullptr)
126  {
127  DataCollection* collection;
128  collection = dynamic_cast<DataCollection*>(m_Collection->GetData(index).GetPointer());
129  if (collection != nullptr)
130  {
131  iterator = new DataCollectionImageIterator<TDataType, TImageDimension>(collection, m_ImageName);
132  if (iterator->IsAtEnd())
133  {
134  delete iterator;
135  iterator = nullptr;
136  ++index;
137  }
138  }
139  else
140  {
141  ++index;
142  }
143  }
144  m_CurrentElement = index;
145  return iterator;
146 }
147 
148 template <typename TDataType, int TImageDimension>
149 TDataType
152 {
153  return m_CurrentIterator.Get();
154 }
155 
156 template <typename TDataType, int TImageDimension>
157 void
159  SetVoxel(TDataType value)
160 {
161  m_CurrentIterator.Set(value);
162 }
163 
164 template <typename TDataType, int TImageDimension>
165 size_t
168 {
169  return m_CurrentIndex;
170 }
171 
172 template <typename TDataType, int TImageDimension>
173 std::string
176 {
177  if (m_IteratingImages)
178  {
179  return m_ImageName;
180  }
181  else
182  {
183  return m_Collection->IndexToName(m_CurrentElement) + "/" + m_CurrentCollectionIterator->GetFilePrefix();
184  }
185 }
186 
187 template <typename TDataType, int TImageDimension>
189 {
190  if (m_IteratingImages)
191  {
192  if (m_CurrentCollectionIterator != nullptr)
193  {
194  delete m_CurrentCollectionIterator;
195  m_CurrentCollectionIterator = nullptr;
196  }
197  m_IteratingImages = false;
198  m_CurrentElement = 0;
199  m_CurrentCollectionIterator = GetNextDataCollectionIterator(m_CurrentElement);
200  if (m_CurrentCollectionIterator == nullptr)
201  {
202  m_IsAtEnd = true;
203  return;
204  }
205  }
206  else
207  {
208  m_CurrentCollectionIterator->NextObject();
209  if (m_CurrentCollectionIterator->IsAtEnd()) //Current collection is finished iterated
210  {
211  delete m_CurrentCollectionIterator;
212  m_CurrentCollectionIterator = GetNextDataCollectionIterator(m_CurrentElement+1);
213  }
214  if (m_CurrentCollectionIterator == nullptr) //If no collection is known
215  {
216  m_IsAtEnd = true;
217  return;
218  }
219  }
220  m_CurrentIterator = m_CurrentCollectionIterator->GetImageIterator();
221 }
222 
223 template <typename TDataType, int TImageDimension>
226 {
227  return m_CurrentIterator;
228 };
229 
230 #endif // mitkDataCollectionImageIterator_CXX
DataCollectionImageIterator(DataCollection::Pointer collection, std::string imageName)
itk::ImageRegionIterator< ImageType > ImageIterator
Image class for storing images.
Definition: mitkImage.h:72
mitk::Image::Pointer image
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::Image< TDataType, ImageDimension > ImageType