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