Medical Imaging Interaction Toolkit  2016.11.0
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,
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 mitkDataCollectionSingleImageIterator_CXX
18 #define mitkDataCollectionSingleImageIterator_CXX
19 
21 #include <mitkImageCast.h>
22 
23 template <typename TDataType, int TImageDimension>
25 DataCollectionSingleImageIterator(DataCollection *collection, std::string imageName) :
26  m_Collection(collection),
27  m_ImageName(imageName),
28  m_IsAtEnd(false),
29  m_IteratingImages(true),
30  m_CurrentIndex(0),
31  m_CurrentElement(0),
32  m_CurrentSingleCollectionIterator(NULL)
33 {
34  ToBegin();
35 }
36 
37 template <typename TDataType, int TImageDimension>
38 void
41 {
42  m_IsAtEnd = false;
43  m_Image = NULL;
44  m_IteratingImages = false;
45  m_CurrentIndex = 0;
46  m_CurrentElement = 0;
47 
48  if (m_Collection->HasElement(m_ImageName))
49  {
50  {
51  mitk::Image *image = dynamic_cast<mitk::Image*>(m_Collection->GetData(m_ImageName).GetPointer());
52  if (image != 0)
53  {
54  typename ImageType::Pointer itkImage = ImageType::New();
55  mitk::CastToItkImage(image, itkImage);
56  itk::DataObject::Pointer itkObject = dynamic_cast<itk::DataObject* >(itkImage.GetPointer());
57  m_Collection->SetData(itkObject, m_ImageName);
58  }
59  }
60  ImageType * image = dynamic_cast<ImageType*>(m_Collection->GetData(m_ImageName).GetPointer());
61  if (image != 0)
62  {
63  m_Image = image;
64  }
65  }
66  if (m_Image == nullptr)
67  {
68  m_CurrentElement = 0;
69  m_CurrentSingleCollectionIterator = GetNextDataCollectionIterator(m_CurrentElement);
70  if (m_Image == nullptr)
71  {
72  m_IsAtEnd = true;
73  } else
74  {
75  m_Image = m_CurrentSingleCollectionIterator->GetImage();
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  NextObject();
95 }
96 
97 template <typename TDataType, int TImageDimension>
98 void
101 {
102  ++m_CurrentIndex;
103  NextObject();
104 }
105 
106 template <typename TDataType, int TImageDimension>
107 size_t
110 {
111  return m_CurrentIndex;
112 }
113 
114 template <typename TDataType, int TImageDimension>
115 std::string
118 {
119  if (m_IteratingImages)
120  {
121  return m_ImageName;
122  }
123  else
124  {
125  return m_Collection->IndexToName(m_CurrentElement) + "/" + m_CurrentSingleCollectionIterator->GetFilePrefix();
126  }
127 }
128 
129 template <typename TDataType, int TImageDimension>
132 {
133  if (m_Collection->HasElement(m_ImageName))
134  {
135  if (m_CurrentSingleCollectionIterator != NULL)
136  {
137  delete m_CurrentSingleCollectionIterator;
138  m_CurrentSingleCollectionIterator = 0;
139  }
140  m_CurrentElement = 0;
141  m_Image = 0;
142  m_CurrentSingleCollectionIterator = GetNextDataCollectionIterator(m_CurrentElement);
143  if (m_CurrentSingleCollectionIterator == NULL)
144  {
145  m_IsAtEnd = true;
146  return;
147  }
148  }
149  else
150  {
151  m_CurrentSingleCollectionIterator->NextObject();
152  if (m_CurrentSingleCollectionIterator->IsAtEnd()) //Current collection is finished iterated
153  {
154  delete m_CurrentSingleCollectionIterator;
155  m_CurrentSingleCollectionIterator = GetNextDataCollectionIterator(m_CurrentElement+1);
156  }
157  if (m_CurrentSingleCollectionIterator == NULL) //If no collection is known
158  {
159  m_IsAtEnd = true;
160  return;
161  }
162  }
163 }
164 
165 template <typename TDataType, int TImageDimension>
169 {
170  return m_Image;
171 }
172 
173 template <typename TDataType, int TImageDimension>
174 void
176 AddImage(ImageType *image, std::string name)
177 {
178  if (m_Collection->HasElement(m_ImageName))
179  {
180  itk::DataObject::Pointer dataObject = dynamic_cast<itk::DataObject*>(image);
181  m_Collection->AddData(dataObject, name);
182  } else
183  {
184  m_CurrentSingleCollectionIterator->AddImage(image, name);
185  }
186 }
187 
188 template <typename TDataType, int TImageDimension>
191 GetNextDataCollectionIterator(size_t start)
192 {
194  size_t index =start;
195  while (index < m_Collection->Size() && iterator == 0)
196  {
197  DataCollection* collection;
198  collection = dynamic_cast<DataCollection*>(m_Collection->GetData(index).GetPointer());
199  if (collection != 0)
200  {
201  iterator = new DataCollectionSingleImageIterator<TDataType, TImageDimension>(collection, m_ImageName);
202  if (iterator->IsAtEnd())
203  {
204  delete iterator;
205  iterator = 0;
206  ++index;
207  }
208  }
209  else
210  {
211  ++index;
212  }
213  }
214  m_CurrentElement = index;
215  if (iterator != 0)
216  m_Image = iterator->GetImage();
217  return iterator;
218 }
219 
220 // mitkDataCollectionSingleImageIterator_CXX
221 #endif
itk::SmartPointer< Self > Pointer
DataCollectionSingleImageIterator(mitk::DataCollection *collection, std::string imageName)
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.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.