ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkImageToListSampleAdaptor.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkImageToListSampleAdaptor_h
19 #define __itkImageToListSampleAdaptor_h
20 
21 #include <typeinfo>
22 
23 #include "itkImage.h"
24 #include "itkPixelTraits.h"
25 #include "itkListSample.h"
26 #include "itkSmartPointer.h"
27 #include "itkImageRegionIterator.h"
29 
30 namespace itk
31 {
32 namespace Statistics
33 {
52 template< class TImage >
53 class ITK_EXPORT ImageToListSampleAdaptor:
54  public ListSample< typename MeasurementVectorPixelTraits< typename TImage::PixelType >::MeasurementVectorType >
55 {
56 public:
59 
60  typedef ListSample< typename MeasurementVectorPixelTraits<
61  typename TImage::PixelType >::MeasurementVectorType >
63 
66 
68  itkTypeMacro(ImageToListSampleAdaptor, ListSample);
69 
71  itkNewMacro(Self);
72 
74  typedef TImage ImageType;
75  typedef typename ImageType::Pointer ImagePointer;
76  typedef typename ImageType::ConstPointer ImageConstPointer;
77  typedef typename ImageType::IndexType IndexType;
78  typedef typename ImageType::PixelType PixelType;
79  typedef typename ImageType::PixelContainerConstPointer PixelContainerConstPointer;
80 
85 
86 
91 
94 
95  typedef typename Superclass::AbsoluteFrequencyType AbsoluteFrequencyType;
96  typedef typename Superclass::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
97  typedef typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType;
98  typedef typename Superclass::InstanceIdentifier InstanceIdentifier;
99 
101 
103  void SetImage(const TImage *image);
104 
106  const TImage * GetImage() const;
107 
109  InstanceIdentifier Size() const;
110 
112  virtual const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const;
113 
114  virtual MeasurementVectorSizeType GetMeasurementVectorSize() const
115  {
116  // some filter are expected that this method returns something even if the
117  // input is not set. This won't be the right value for a variable length vector
118  // but it's better than an exception.
119  if( m_Image.IsNull() )
120  {
121  return Superclass::GetMeasurementVectorSize();
122  }
123  else
124  {
125  return m_Image->GetNumberOfComponentsPerPixel();
126  }
127  }
128 
130  AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const;
131 
133  TotalAbsoluteFrequencyType GetTotalFrequency() const;
134 
140  {
142 
143 public:
144 
146  {
147  *this = adaptor->Begin();
148  }
149 
151  {
152  m_Iter = iter.m_Iter;
153  m_InstanceIdentifier = iter.m_InstanceIdentifier;
154  }
155 
156  ConstIterator & operator=(const ConstIterator & iter)
157  {
158  m_Iter = iter.m_Iter;
159  m_InstanceIdentifier = iter.m_InstanceIdentifier;
160  return *this;
161  }
162 
163  AbsoluteFrequencyType GetFrequency() const
164  {
165  return 1;
166  }
167 
168  const MeasurementVectorType & GetMeasurementVector() const
169  {
170  MeasurementVectorTraits::Assign( this->m_MeasurementVectorCache, m_Iter.Get() );
171  return this->m_MeasurementVectorCache;
172  }
173 
174  InstanceIdentifier GetInstanceIdentifier() const
175  {
176  return m_InstanceIdentifier;
177  }
178 
179  ConstIterator & operator++()
180  {
181  ++m_Iter;
182  ++m_InstanceIdentifier;
183  return *this;
184  }
185 
186  bool operator!=(const ConstIterator & it)
187  {
188  return ( m_Iter != it.m_Iter );
189  }
190 
191  bool operator==(const ConstIterator & it)
192  {
193  return ( m_Iter == it.m_Iter );
194  }
195 
196 protected:
197  // This method should only be available to the ListSample class
200  InstanceIdentifier iid)
201  {
202  m_Iter = iter;
203  m_InstanceIdentifier = iid;
204  }
205 
206  // This method is purposely not implemented
207  ConstIterator();
208 
209 private:
213  };
214 
219  class Iterator:public ConstIterator
220  {
222 
223 public:
224 
225  Iterator(Self *adaptor):ConstIterator(adaptor)
226  {}
227 
228  Iterator(const Iterator & iter):ConstIterator(iter)
229  {}
230 
231  Iterator & operator=(const Iterator & iter)
232  {
233  this->ConstIterator::operator=(iter);
234  return *this;
235  }
236 
237 protected:
238  // To ensure const-correctness these method must not be in the public API.
239  // The are purposly not implemented, since they should never be called.
240  Iterator();
241  Iterator(const Self *adaptor);
243  Iterator(const ConstIterator & it);
244  ConstIterator & operator=(const ConstIterator & it);
245 
247  {}
248 
249 private:
250  };
251 
253  Iterator Begin()
254  {
255  ImagePointer nonConstImage = const_cast< ImageType * >( m_Image.GetPointer() );
256  ImageIteratorType imageIterator( nonConstImage, nonConstImage->GetLargestPossibleRegion() );
257  imageIterator.GoToBegin();
258  Iterator iter(imageIterator, 0);
259  return iter;
260  }
262 
264  Iterator End()
265  {
266  ImagePointer nonConstImage = const_cast< ImageType * >( m_Image.GetPointer() );
267  ImageIteratorType imageIterator( nonConstImage, nonConstImage->GetLargestPossibleRegion() );
268  imageIterator.GoToEnd();
269  Iterator iter( imageIterator, m_Image->GetPixelContainer()->Size() );
271 
272  return iter;
273  }
274 
276  ConstIterator Begin() const
277  {
278  ImageConstIteratorType imageConstIterator( m_Image, m_Image->GetLargestPossibleRegion() );
279  imageConstIterator.GoToBegin();
280  ConstIterator iter(imageConstIterator, 0);
282 
283  return iter;
284  }
285 
287  ConstIterator End() const
288  {
289  ImageConstIteratorType imageConstIterator( m_Image, m_Image->GetLargestPossibleRegion() );
290  imageConstIterator.GoToEnd();
291  ConstIterator iter( imageConstIterator, m_Image->GetPixelContainer()->Size() );
293 
294  return iter;
295  }
296 
297 protected:
300  void PrintSelf(std::ostream & os, Indent indent) const;
301 
302 private:
303  ImageToListSampleAdaptor(const Self &); //purposely not implemented
304  void operator=(const Self &); //purposely not implemented
305 
308 
309 }; // end of class ImageToListSampleAdaptor
310 } // end of namespace Statistics
311 } // end of namespace itk
312 
313 #ifndef ITK_MANUAL_INSTANTIATION
314 #include "itkImageToListSampleAdaptor.hxx"
315 #endif
316 
317 #endif
318