ITK  5.0.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 #include <utility>
23 
24 #include "itkImage.h"
25 #include "itkPixelTraits.h"
26 #include "itkListSample.h"
27 #include "itkSmartPointer.h"
28 #include "itkImageRegionIterator.h"
30 
31 namespace itk
32 {
33 namespace Statistics
34 {
53 template< typename TImage >
54 class ITK_TEMPLATE_EXPORT ImageToListSampleAdaptor:
55  public ListSample< typename MeasurementVectorPixelTraits< typename TImage::PixelType >::MeasurementVectorType >
56 {
57 public:
58  ITK_DISALLOW_COPY_AND_ASSIGN(ImageToListSampleAdaptor);
59 
62 
64  typename TImage::PixelType >::MeasurementVectorType >;
65 
68 
70  itkTypeMacro(ImageToListSampleAdaptor, ListSample);
71 
73  itkNewMacro(Self);
74 
76  using ImageType = TImage;
77  using ImagePointer = typename ImageType::Pointer;
78  using ImageConstPointer = typename ImageType::ConstPointer;
79  using IndexType = typename ImageType::IndexType;
80  using PixelType = typename ImageType::PixelType;
81  using PixelContainerConstPointer = typename ImageType::PixelContainerConstPointer;
82 
87 
88 
93 
96 
97  using AbsoluteFrequencyType = typename Superclass::AbsoluteFrequencyType;
98  using TotalAbsoluteFrequencyType = typename Superclass::TotalAbsoluteFrequencyType;
99  using MeasurementVectorSizeType = typename Superclass::MeasurementVectorSizeType;
100  using InstanceIdentifier = typename Superclass::InstanceIdentifier;
101 
103 
105  void SetImage(const TImage *image);
106 
108  const TImage * GetImage() const;
109 
111  InstanceIdentifier Size() const override;
112 
114  const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const override;
115 
117  {
118  // some filter are expected that this method returns something even if the
119  // input is not set. This won't be the right value for a variable length vector
120  // but it's better than an exception.
121  if( m_Image.IsNull() )
122  {
123  return Superclass::GetMeasurementVectorSize();
124  }
125  else
126  {
127  return m_Image->GetNumberOfComponentsPerPixel();
128  }
129  }
130 
132  AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const override;
133 
135  TotalAbsoluteFrequencyType GetTotalFrequency() const override;
136 
142  {
144 
145  public:
146 
148  {
149  *this = adaptor->Begin();
150  }
151 
152  ConstIterator(const ConstIterator & iter) :
153  m_Iter(iter.m_Iter),
154  m_InstanceIdentifier(iter.m_InstanceIdentifier)
155  {}
156 
158  {
159  m_Iter = iter.m_Iter;
160  m_InstanceIdentifier = iter.m_InstanceIdentifier;
161  return *this;
162  }
163 
165  {
166  return 1;
167  }
168 
170  {
171  MeasurementVectorTraits::Assign( this->m_MeasurementVectorCache, m_Iter.Get() );
172  return this->m_MeasurementVectorCache;
173  }
174 
176  {
177  return m_InstanceIdentifier;
178  }
179 
181  {
182  ++m_Iter;
183  ++m_InstanceIdentifier;
184  return *this;
185  }
186 
187  bool operator!=(const ConstIterator & it)
188  {
189  return ( m_Iter != it.m_Iter );
190  }
191 
192  bool operator==(const ConstIterator & it)
193  {
194  return ( m_Iter == it.m_Iter );
195  }
196 
197  protected:
198  // This method should only be available to the ListSample class
200  m_Iter(std::move(iter)),
201  m_InstanceIdentifier(iid)
202  {}
203 
204  private:
205  ConstIterator() = delete;
209  };
210 
215  class Iterator:
216  public ConstIterator
217  {
219 
220  public:
221 
222  Iterator(Self *adaptor) :
223  ConstIterator(adaptor)
224  {}
225 
226  Iterator(const Iterator & iter):
227  ConstIterator(iter)
228  {}
229 
230  Iterator & operator=(const Iterator & iter)
231  {
232  this->ConstIterator::operator=(iter);
233  return *this;
234  }
235 
236  protected:
238  ConstIterator(iter, iid)
239  {}
240 
241  private:
242  // To ensure const-correctness these method must not be in the public API.
243  // The are purposly not implemented, since they should never be called.
244  Iterator() = delete;
245  Iterator(const Self *adaptor) = delete;
246  Iterator(const ImageConstIteratorType & iter, InstanceIdentifier iid) = delete;
247  Iterator(const ConstIterator & it) = delete;
248  ConstIterator & operator=(const ConstIterator & it) = delete;
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 
277  {
278  ImageConstIteratorType imageConstIterator( m_Image, m_Image->GetLargestPossibleRegion() );
279  imageConstIterator.GoToBegin();
280  ConstIterator iter(imageConstIterator, 0);
282 
283  return iter;
284  }
285 
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:
300  ~ImageToListSampleAdaptor() override = default;
301  void PrintSelf(std::ostream & os, Indent indent) const override;
302 
303 private:
306 
307 }; // end of class ImageToListSampleAdaptor
308 } // end of namespace Statistics
309 } // end of namespace itk
310 
311 #ifndef ITK_MANUAL_INSTANTIATION
312 #include "itkImageToListSampleAdaptor.hxx"
313 #endif
314 
315 #endif
MeasurementVectorSizeType GetMeasurementVectorSize() const override
typename TMeasurementVector::ValueType ValueType
typename Superclass::AbsoluteFrequencyType AbsoluteFrequencyType
typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType
static void Assign(TArrayType &m, const TArrayType &v)
typename MeasurementVectorTraitsType::ValueType MeasurementType
Traits for a pixel that define the dimension and component type.
typename Superclass::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
ConstIterator(ImageConstIteratorType iter, InstanceIdentifier iid)
Iterator(const ImageIteratorType &iter, InstanceIdentifier iid)
typename Superclass::InstanceIdentifier InstanceIdentifier
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:68
typename ImageType::ConstPointer ImageConstPointer
typename MeasurementPixelTraitsType::MeasurementVectorType MeasurementVectorType
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
This class provides ListSample interface to ITK Image.
Base class for most ITK classes.
Definition: itkObject.h:60
typename ImageType::PixelContainerConstPointer PixelContainerConstPointer
Base class for all data objects in ITK.
A multi-dimensional iterator templated over image type that walks a region of pixels.