Medical Imaging Interaction Toolkit  2016.11.0
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,
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 #ifndef mitkDataCollectionImageIterator_CXX
18 #define mitkDataCollectionImageIterator_CXX
19 
21 #include <mitkImageCast.h>
22 
23 template <typename TDataType, int TImageDimension>
25  DataCollectionImageIterator(DataCollection::Pointer collection, std::string imageName) :
26  m_Collection(collection), m_ImageName(imageName), m_IsAtEnd(false),
27  m_IteratingImages(true), m_CurrentIndex(0),
28  m_CurrentElement(0), m_CurrentCollectionIterator(0)
29 {
30  ToBegin();
31 }
32 
33 template <typename TDataType, int TImageDimension>
34 void
37 {
38  m_IsAtEnd = false;
39  m_IteratingImages = false;
40  m_CurrentIndex = 0;
41  m_CurrentElement = 0;
42  m_ImageIndex = 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 
49  //TODO: check whether image is valid... image != 0 if empty smart pointer was inserted into collection!!!!
50  if (image != 0)
51  {
52  typename ImageType::Pointer itkImage = ImageType::New();
53  mitk::CastToItkImage(image, itkImage);
54  itk::DataObject::Pointer itkObject = dynamic_cast<itk::DataObject* >(itkImage.GetPointer());
55  m_Collection->SetData(itkObject, m_ImageName);
56  }
57  }
58  ImageType * image = dynamic_cast<ImageType*>(m_Collection->GetData(m_ImageName).GetPointer());
59  if (image != 0)
60  {
61  m_IteratingImages = true;
62  m_CurrentIterator = ImageIterator(image, image->GetLargestPossibleRegion());
63  }
64  }
65  if (!m_IteratingImages)
66  {
67  if (m_CurrentCollectionIterator != 0)
68  {
69  delete m_CurrentCollectionIterator;
70  m_CurrentCollectionIterator = 0;
71  }
72  m_CurrentElement = 0;
73  m_CurrentCollectionIterator = GetNextDataCollectionIterator(m_CurrentElement);
74  if (m_CurrentCollectionIterator == 0)
75  {
76  m_IsAtEnd = true;
77  } else
78  {
79  m_CurrentIterator = m_CurrentCollectionIterator->GetImageIterator();
80  }
81  }
82 }
83 
84 template <typename TDataType, int TImageDimension>
85 bool
88 {
89  return m_IsAtEnd;
90 }
91 
92 template <typename TDataType, int TImageDimension>
93 void
96 {
97  ++m_CurrentIndex;
98  ++m_CurrentIterator;
99 
100  if (m_CurrentIterator.IsAtEnd())
101  {
102  ++m_ImageIndex;
103  NextObject();
104  }
105 }
106 
107 template <typename TDataType, int TImageDimension>
108 void
111 {
112  ++m_CurrentIndex;
113  ++m_CurrentIterator;
114 
115  if (m_CurrentIterator.IsAtEnd())
116  {
117  ++m_ImageIndex;
118  NextObject();
119  }
120 }
121 
122 template <typename TDataType, int TImageDimension>
125  GetNextDataCollectionIterator(size_t start)
126 {
128  size_t index =start;
129  while (index < m_Collection->Size() && iterator == 0)
130  {
131  DataCollection* collection;
132  collection = dynamic_cast<DataCollection*>(m_Collection->GetData(index).GetPointer());
133  if (collection != 0)
134  {
135  iterator = new DataCollectionImageIterator<TDataType, TImageDimension>(collection, m_ImageName);
136  if (iterator->IsAtEnd())
137  {
138  delete iterator;
139  iterator = 0;
140  ++index;
141  }
142  }
143  else
144  {
145  ++index;
146  }
147  }
148  m_CurrentElement = index;
149  return iterator;
150 }
151 
152 template <typename TDataType, int TImageDimension>
153 TDataType
156 {
157  return m_CurrentIterator.Get();
158 }
159 
160 template <typename TDataType, int TImageDimension>
161 void
163  SetVoxel(TDataType value)
164 {
165  m_CurrentIterator.Set(value);
166 }
167 
168 template <typename TDataType, int TImageDimension>
169 size_t
172 {
173  return m_CurrentIndex;
174 }
175 
176 template <typename TDataType, int TImageDimension>
177 std::string
180 {
181  if (m_IteratingImages)
182  {
183  return m_ImageName;
184  }
185  else
186  {
187  return m_Collection->IndexToName(m_CurrentElement) + "/" + m_CurrentCollectionIterator->GetFilePrefix();
188  }
189 }
190 
191 template <typename TDataType, int TImageDimension>
193 {
194  if (m_IteratingImages)
195  {
196  if (m_CurrentCollectionIterator != NULL)
197  {
198  delete m_CurrentCollectionIterator;
199  m_CurrentCollectionIterator = 0;
200  }
201  m_IteratingImages = false;
202  m_CurrentElement = 0;
203  m_CurrentCollectionIterator = GetNextDataCollectionIterator(m_CurrentElement);
204  if (m_CurrentCollectionIterator == NULL)
205  {
206  m_IsAtEnd = true;
207  return;
208  }
209  }
210  else
211  {
212  m_CurrentCollectionIterator->NextObject();
213  if (m_CurrentCollectionIterator->IsAtEnd()) //Current collection is finished iterated
214  {
215  delete m_CurrentCollectionIterator;
216  m_CurrentCollectionIterator = GetNextDataCollectionIterator(m_CurrentElement+1);
217  }
218  if (m_CurrentCollectionIterator == NULL) //If no collection is known
219  {
220  m_IsAtEnd = true;
221  return;
222  }
223  }
224  m_CurrentIterator = m_CurrentCollectionIterator->GetImageIterator();
225 }
226 
227 template <typename TDataType, int TImageDimension>
230 {
231  return m_CurrentIterator;
232 };
233 
234 #endif // mitkDataCollectionImageIterator_CXX
itk::SmartPointer< Self > Pointer
DataCollectionImageIterator(DataCollection::Pointer collection, std::string imageName)
itk::ImageRegionIterator< ImageType > ImageIterator
Image class for storing images.
Definition: mitkImage.h:76
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
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.