ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkImageToListSampleAdaptor.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkImageToListSampleAdaptor_h
00019 #define __itkImageToListSampleAdaptor_h
00020 
00021 #include <typeinfo>
00022 
00023 #include "itkImage.h"
00024 #include "itkPixelTraits.h"
00025 #include "itkListSample.h"
00026 #include "itkSmartPointer.h"
00027 #include "itkImageRegionIterator.h"
00028 #include "itkMeasurementVectorTraits.h"
00029 
00030 namespace itk
00031 {
00032 namespace Statistics
00033 {
00052 template< class TImage >
00053 class ITK_EXPORT ImageToListSampleAdaptor:
00054   public ListSample< typename MeasurementVectorPixelTraits< typename TImage::PixelType >::MeasurementVectorType >
00055 {
00056 public:
00058   typedef ImageToListSampleAdaptor Self;
00059 
00060   typedef ListSample< typename MeasurementVectorPixelTraits<
00061                         typename TImage::PixelType >::MeasurementVectorType >
00062   Superclass;
00063 
00064   typedef SmartPointer< Self >       Pointer;
00065   typedef SmartPointer< const Self > ConstPointer;
00066 
00068   itkTypeMacro(ImageToListSampleAdaptor, ListSample);
00069 
00071   itkNewMacro(Self);
00072 
00074   typedef TImage                                         ImageType;
00075   typedef typename ImageType::Pointer                    ImagePointer;
00076   typedef typename ImageType::ConstPointer               ImageConstPointer;
00077   typedef typename ImageType::IndexType                  IndexType;
00078   typedef typename ImageType::PixelType                  PixelType;
00079   typedef typename ImageType::PixelContainerConstPointer PixelContainerConstPointer;
00080 
00082   typedef ImageRegionIterator< ImageType >          ImageIteratorType;
00083   typedef ImageRegionConstIterator< ImageType >     ImageConstIteratorType;
00084   typedef PixelTraits< typename TImage::PixelType > PixelTraitsType;
00085 
00086 
00089   typedef MeasurementVectorPixelTraits< PixelType >                   MeasurementPixelTraitsType;
00090   typedef typename MeasurementPixelTraitsType::MeasurementVectorType  MeasurementVectorType;
00091 
00092   typedef MeasurementVectorTraitsTypes< MeasurementVectorType > MeasurementVectorTraitsType;
00093   typedef typename MeasurementVectorTraitsType::ValueType       MeasurementType;
00094 
00095   typedef typename Superclass::AbsoluteFrequencyType      AbsoluteFrequencyType;
00096   typedef typename Superclass::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
00097   typedef typename Superclass::MeasurementVectorSizeType  MeasurementVectorSizeType;
00098   typedef typename Superclass::InstanceIdentifier         InstanceIdentifier;
00099 
00100   typedef MeasurementVectorType ValueType;
00101 
00103   void SetImage(const TImage *image);
00104 
00106   const TImage * GetImage() const;
00107 
00109   InstanceIdentifier Size() const;
00110 
00112   virtual const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const;
00113 
00114   virtual MeasurementVectorSizeType GetMeasurementVectorSize() const
00115   {
00116     // some filter are expected that this method returns something even if the
00117     // input is not set. This won't be the right value for a variable length vector
00118     // but it's better than an exception.
00119     if( m_Image.IsNull() )
00120       {
00121       return Superclass::GetMeasurementVectorSize();
00122       }
00123     else
00124       {
00125       return m_Image->GetNumberOfComponentsPerPixel();
00126       }
00127   }
00128 
00130   AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const;
00131 
00133   TotalAbsoluteFrequencyType GetTotalFrequency() const;
00134 
00139   class ConstIterator
00140   {
00141     friend class ImageToListSampleAdaptor;
00142 public:
00143 
00144     ConstIterator(const ImageToListSampleAdaptor *adaptor)
00145     {
00146       *this = adaptor->Begin();
00147     }
00148 
00149     ConstIterator(const ConstIterator & iter)
00150     {
00151       m_Iter = iter.m_Iter;
00152       m_InstanceIdentifier = iter.m_InstanceIdentifier;
00153     }
00154 
00155     ConstIterator & operator=(const ConstIterator & iter)
00156     {
00157       m_Iter = iter.m_Iter;
00158       m_InstanceIdentifier = iter.m_InstanceIdentifier;
00159       return *this;
00160     }
00161 
00162     AbsoluteFrequencyType GetFrequency() const
00163     {
00164       return 1;
00165     }
00166 
00167     const MeasurementVectorType & GetMeasurementVector() const
00168     {
00169       MeasurementVectorTraits::Assign( this->m_MeasurementVectorCache, m_Iter.Get() );
00170       return this->m_MeasurementVectorCache;
00171     }
00172 
00173     InstanceIdentifier GetInstanceIdentifier() const
00174     {
00175       return m_InstanceIdentifier;
00176     }
00177 
00178     ConstIterator & operator++()
00179     {
00180       ++m_Iter;
00181       ++m_InstanceIdentifier;
00182       return *this;
00183     }
00184 
00185     bool operator!=(const ConstIterator & it)
00186     {
00187       return ( m_Iter != it.m_Iter );
00188     }
00189 
00190     bool operator==(const ConstIterator & it)
00191     {
00192       return ( m_Iter == it.m_Iter );
00193     }
00194 
00195 protected:
00196     // This method should only be available to the ListSample class
00197     ConstIterator(
00198       ImageConstIteratorType iter,
00199       InstanceIdentifier iid)
00200     {
00201       m_Iter = iter;
00202       m_InstanceIdentifier = iid;
00203     }
00204 
00205     // This method is purposely not implemented
00206     ConstIterator();
00207 private:
00208     ImageConstIteratorType        m_Iter;
00209     mutable MeasurementVectorType m_MeasurementVectorCache;
00210     InstanceIdentifier            m_InstanceIdentifier;
00211   };
00212 
00217   class Iterator:public ConstIterator
00218   {
00219     friend class ImageToListSampleAdaptor;
00220 public:
00221 
00222     Iterator(Self *adaptor):ConstIterator(adaptor)
00223     {}
00224 
00225     Iterator(const Iterator & iter):ConstIterator(iter)
00226     {}
00227 
00228     Iterator & operator=(const Iterator & iter)
00229     {
00230       this->ConstIterator::operator=(iter);
00231       return *this;
00232     }
00233 
00234 protected:
00235     // To ensure const-correctness these method must not be in the public API.
00236     // The are purposly not implemented, since they should never be called.
00237     Iterator();
00238     Iterator(const Self *adaptor);
00239     Iterator(ImageConstIteratorType iter, InstanceIdentifier iid);
00240     Iterator(const ConstIterator & it);
00241     ConstIterator & operator=(const ConstIterator & it);
00242 
00243     Iterator(
00244       ImageIteratorType iter,
00245       InstanceIdentifier iid):ConstIterator(iter, iid)
00246     {}
00247 
00248 private:
00249   };
00250 
00252   Iterator Begin()
00253   {
00254     ImagePointer      nonConstImage = const_cast< ImageType * >( m_Image.GetPointer() );
00255     ImageIteratorType imageIterator( nonConstImage, nonConstImage->GetLargestPossibleRegion() );
00256     Iterator          iter(imageIterator.Begin(), 0);
00258 
00259     return iter;
00260   }
00261 
00263   Iterator End()
00264   {
00265     ImagePointer      nonConstImage = const_cast< ImageType * >( m_Image.GetPointer() );
00266     ImageIteratorType imageIterator( nonConstImage, nonConstImage->GetLargestPossibleRegion() );
00267     Iterator          iter( imageIterator.End(), m_Image->GetPixelContainer()->Size() );
00269 
00270     return iter;
00271   }
00272 
00274   ConstIterator Begin() const
00275   {
00276     ImageConstIteratorType imageConstIterator( m_Image, m_Image->GetLargestPossibleRegion() );
00277     ConstIterator          iter(imageConstIterator.Begin(), 0);
00279 
00280     return iter;
00281   }
00282 
00284   ConstIterator End() const
00285   {
00286     ImageConstIteratorType imageConstIterator( m_Image, m_Image->GetLargestPossibleRegion() );
00287     ConstIterator          iter( imageConstIterator.End(), m_Image->GetPixelContainer()->Size() );
00289 
00290     return iter;
00291   }
00292 
00293 protected:
00294   ImageToListSampleAdaptor();
00295   virtual ~ImageToListSampleAdaptor() {}
00296   void PrintSelf(std::ostream & os, Indent indent) const;
00297 
00298 private:
00299   ImageToListSampleAdaptor(const Self &); //purposely not implemented
00300   void operator=(const Self &);           //purposely not implemented
00301 
00302   ImageConstPointer             m_Image;
00303   mutable MeasurementVectorType m_MeasurementVectorInternal;
00304 
00305 };  // end of class ImageToListSampleAdaptor
00306 } // end of namespace Statistics
00307 } // end of namespace itk
00308 
00309 #ifndef ITK_MANUAL_INSTANTIATION
00310 #include "itkImageToListSampleAdaptor.hxx"
00311 #endif
00312 
00313 #endif
00314