ITK  4.9.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< typename TImage >
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 
99 
101 
103  void SetImage(const TImage *image);
104 
106  const TImage * GetImage() const;
107 
109  InstanceIdentifier Size() const ITK_OVERRIDE;
110 
112  virtual const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const ITK_OVERRIDE;
113 
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  {
122  }
123  else
124  {
125  return m_Image->GetNumberOfComponentsPerPixel();
126  }
127  }
128 
131 
133  TotalAbsoluteFrequencyType GetTotalFrequency() const ITK_OVERRIDE;
134 
140  {
142 
143  public:
144 
146  {
147  *this = adaptor->Begin();
148  }
149 
150  ConstIterator(const ConstIterator & iter) :
151  m_Iter(iter.m_Iter),
152  m_InstanceIdentifier(iter.m_InstanceIdentifier)
153  {}
154 
155  ConstIterator & operator=(const ConstIterator & iter)
156  {
157  m_Iter = iter.m_Iter;
158  m_InstanceIdentifier = iter.m_InstanceIdentifier;
159  return *this;
160  }
161 
163  {
164  return 1;
165  }
166 
168  {
169  MeasurementVectorTraits::Assign( this->m_MeasurementVectorCache, m_Iter.Get() );
170  return this->m_MeasurementVectorCache;
171  }
172 
174  {
175  return m_InstanceIdentifier;
176  }
177 
178  ConstIterator & operator++()
179  {
180  ++m_Iter;
181  ++m_InstanceIdentifier;
182  return *this;
183  }
184 
185  bool operator!=(const ConstIterator & it)
186  {
187  return ( m_Iter != it.m_Iter );
188  }
189 
190  bool operator==(const ConstIterator & it)
191  {
192  return ( m_Iter == it.m_Iter );
193  }
194 
195  protected:
196  // This method should only be available to the ListSample class
198  m_Iter(iter),
199  m_InstanceIdentifier(iid)
200  {}
201 
202  ConstIterator() ITK_DELETE_FUNCTION;
203 
204  private:
206  mutable MeasurementVectorType m_MeasurementVectorCache;
207  InstanceIdentifier m_InstanceIdentifier;
208  };
209 
214  class Iterator:
215  public ConstIterator
216  {
218 
219  public:
220 
221  Iterator(Self *adaptor) :
222  ConstIterator(adaptor)
223  {}
224 
225  Iterator(const Iterator & iter):
226  ConstIterator(iter)
227  {}
228 
229  Iterator & operator=(const Iterator & iter)
230  {
231  this->ConstIterator::operator=(iter);
232  return *this;
233  }
234 
235  protected:
236  // To ensure const-correctness these method must not be in the public API.
237  // The are purposly not implemented, since they should never be called.
238  Iterator() ITK_DELETE_FUNCTION;
239  Iterator(const Self *adaptor) ITK_DELETE_FUNCTION;
240  Iterator(const ImageConstIteratorType & iter, InstanceIdentifier iid) ITK_DELETE_FUNCTION;
241  Iterator(const ConstIterator & it) ITK_DELETE_FUNCTION;
242  ConstIterator & operator=(const ConstIterator & it) ITK_DELETE_FUNCTION;
243 
245  ConstIterator(iter, iid)
246  {}
247 
248  private:
249  };
250 
253  {
254  ImagePointer nonConstImage = const_cast< ImageType * >( m_Image.GetPointer() );
255  ImageIteratorType imageIterator( nonConstImage, nonConstImage->GetLargestPossibleRegion() );
256  imageIterator.GoToBegin();
257  Iterator iter(imageIterator, 0);
258  return iter;
259  }
261 
264  {
265  ImagePointer nonConstImage = const_cast< ImageType * >( m_Image.GetPointer() );
266  const typename ImageType::RegionType & largestRegion = nonConstImage->GetLargestPossibleRegion();
267  ImageIteratorType imageIterator( nonConstImage, largestRegion );
268  imageIterator.GoToEnd();
269  Iterator iter( imageIterator, largestRegion.GetNumberOfPixels() );
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  const typename ImageType::RegionType & largestRegion = m_Image->GetLargestPossibleRegion();
290  ImageConstIteratorType imageConstIterator( m_Image, largestRegion );
291  imageConstIterator.GoToEnd();
292  ConstIterator iter( imageConstIterator, largestRegion.GetNumberOfPixels() );
294 
295  return iter;
296  }
297 
298 protected:
301  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
302 
303 private:
304  ImageToListSampleAdaptor(const Self &) ITK_DELETE_FUNCTION;
305  void operator=(const Self &) ITK_DELETE_FUNCTION;
306 
309 
310 }; // end of class ImageToListSampleAdaptor
311 } // end of namespace Statistics
312 } // end of namespace itk
313 
314 #ifndef ITK_MANUAL_INSTANTIATION
315 #include "itkImageToListSampleAdaptor.hxx"
316 #endif
317 
318 #endif
AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const override
MeasurementVectorTraitsType::ValueType MeasurementType
virtual const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const override
virtual MeasurementVectorSizeType GetMeasurementVectorSize() const override
Superclass::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
Definition: itkListSample.h:71
ImageRegionConstIterator< ImageType > ImageConstIteratorType
ImageRegionIterator< ImageType > ImageIteratorType
static void Assign(TArrayType &m, const TArrayType &v)
Traits for a pixel that define the dimension and component type.
MeasurementVectorTraitsTypes< MeasurementVectorType > MeasurementVectorTraitsType
Superclass::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkListSample.h:70
Superclass::AbsoluteFrequencyType AbsoluteFrequencyType
Superclass::InstanceIdentifier InstanceIdentifier
Definition: itkListSample.h:72
MeasurementPixelTraitsType::MeasurementVectorType MeasurementVectorType
Superclass::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
InstanceIdentifier Size() const override
TotalAbsoluteFrequencyType GetTotalFrequency() const override
Superclass::MeasurementVectorSizeType MeasurementVectorSizeType
Definition: itkListSample.h:68
virtual MeasurementVectorSizeType GetMeasurementVectorSize() const
Superclass::MeasurementVectorSizeType MeasurementVectorSizeType
void PrintSelf(std::ostream &os, Indent indent) const override
ListSample< typename MeasurementVectorPixelTraits< typename TImage::PixelType >::MeasurementVectorType > Superclass
This class is the native implementation of the a Sample with an STL container.
Definition: itkListSample.h:51
Control indentation during Print() invocation.
Definition: itkIndent.h:49
void SetImage(const TImage *image)
This class provides ListSample interface to ITK Image.
PixelTraits< typename TImage::PixelType > PixelTraitsType
Base class for all data objects in ITK.
MeasurementVectorPixelTraits< PixelType > MeasurementPixelTraitsType
A multi-dimensional iterator templated over image type that walks a region of pixels.
ConstIterator(const ImageConstIteratorType &iter, InstanceIdentifier iid)
ImageType::PixelContainerConstPointer PixelContainerConstPointer