ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkVectorContainerToListSampleAdaptor.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 __itkVectorContainerToListSampleAdaptor_h
00019 #define __itkVectorContainerToListSampleAdaptor_h
00020 
00021 #include <typeinfo>
00022 
00023 #include "itkListSample.h"
00024 #include "itkSmartPointer.h"
00025 #include "itkVectorContainer.h"
00026 
00027 namespace itk
00028 {
00029 namespace Statistics
00030 {
00045 template< class TVectorContainer >
00046 class ITK_EXPORT VectorContainerToListSampleAdaptor:
00047   public ListSample< typename TVectorContainer::Element >
00048 {
00049 public:
00051   typedef VectorContainerToListSampleAdaptor                   Self;
00052   typedef ListSample<typename TVectorContainer::Element>       Superclass;
00053   typedef SmartPointer< Self >                                 Pointer;
00054   typedef SmartPointer< const Self >                           ConstPointer;
00055 
00057   itkTypeMacro( VectorContainerToListSampleAdaptor, ListSample );
00058 
00060   itkNewMacro( Self );
00061 
00063   itkStaticConstMacro( MeasurementVectorSize, unsigned int,
00064                        TVectorContainer::Element::Dimension );
00065 
00067   typedef TVectorContainer                          VectorContainerType;
00068   typedef typename TVectorContainer::Pointer        VectorContainerPointer;
00069   typedef typename TVectorContainer::ConstPointer   VectorContainerConstPointer;
00070   typedef typename TVectorContainer::Iterator       VectorContainerIterator;
00071   typedef typename TVectorContainer::ConstIterator  VectorContainerConstIterator;
00072 
00075   typedef typename Superclass::MeasurementType            MeasurementType;
00076   typedef typename Superclass::MeasurementVectorType      MeasurementVectorType;
00077   typedef typename Superclass::AbsoluteFrequencyType      AbsoluteFrequencyType;
00078   typedef typename Superclass::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
00079   typedef typename Superclass::MeasurementVectorSizeType  MeasurementVectorSizeType;
00080   typedef typename Superclass::InstanceIdentifier         InstanceIdentifier;
00081 
00082   typedef MeasurementVectorType ValueType;
00083 
00085   itkSetObjectMacro( VectorContainer, VectorContainerType );
00086 
00088   itkGetConstObjectMacro( VectorContainer, VectorContainerType );
00089 
00091   InstanceIdentifier Size() const;
00092 
00095   const MeasurementVectorType & GetMeasurementVector( InstanceIdentifier ) const;
00096 
00098   AbsoluteFrequencyType GetFrequency( InstanceIdentifier ) const;
00099 
00101   TotalAbsoluteFrequencyType GetTotalFrequency() const;
00102 
00106   class ConstIterator
00107   {
00108     friend class VectorContainerToListSampleAdaptor;
00109   public:
00110 
00111     ConstIterator(const VectorContainerToListSampleAdaptor *adaptor)
00112     {
00113       *this = adaptor->Begin();
00114     }
00115 
00116     ConstIterator(const ConstIterator & iter)
00117     {
00118       this->m_Iter = iter.m_Iter;
00119       this->m_InstanceIdentifier = iter.m_InstanceIdentifier;
00120     }
00121 
00122     ConstIterator & operator=(const ConstIterator & iter)
00123     {
00124       this->m_Iter = iter.m_Iter;
00125       this->m_InstanceIdentifier = iter.m_InstanceIdentifier;
00126       return *this;
00127     }
00128 
00129     AbsoluteFrequencyType GetFrequency() const
00130     {
00131       return 1;
00132     }
00133 
00134     const MeasurementVectorType & GetMeasurementVector() const
00135     {
00136       return ( const MeasurementVectorType & )m_Iter.Value();
00137     }
00138 
00139     InstanceIdentifier GetInstanceIdentifier() const
00140     {
00141       return this->m_InstanceIdentifier;
00142     }
00143 
00144     ConstIterator & operator++()
00145     {
00146       ++m_Iter;
00147       ++m_InstanceIdentifier;
00148       return *this;
00149     }
00150 
00151     bool operator!=( const ConstIterator & it )
00152     {
00153       return ( this->m_Iter != it.m_Iter );
00154     }
00155 
00156     bool operator==( const ConstIterator & it )
00157     {
00158       return ( this->m_Iter == it.m_Iter );
00159     }
00160 
00161   protected:
00162     // This method should only be available to the ListSample class
00163     ConstIterator( VectorContainerConstIterator iter,
00164       InstanceIdentifier iid )
00165     {
00166       this->m_Iter = iter;
00167       this->m_InstanceIdentifier = iid;
00168     }
00169 
00170     // This method is purposely not implemented
00171     ConstIterator();
00172   private:
00173     VectorContainerConstIterator      m_Iter;
00174     InstanceIdentifier                m_InstanceIdentifier;
00175   };
00176 
00180   class Iterator:public ConstIterator
00181   {
00182     friend class VectorContainerToListSampleAdaptor;
00183   public:
00184 
00185     Iterator(Self *adaptor):ConstIterator(adaptor)
00186     {}
00187 
00188     Iterator(const Iterator & iter):ConstIterator(iter)
00189     {}
00190 
00191     Iterator & operator=(const Iterator & iter)
00192     {
00193       this->ConstIterator::operator=(iter);
00194       return *this;
00195     }
00196 
00197   protected:
00198     // To ensure const-correctness these method must not be in the public API.
00199     // The are purposly not implemented, since they should never be called.
00200     Iterator();
00201     Iterator( const Self *adaptor );
00202     Iterator( VectorContainerConstIterator iter, InstanceIdentifier iid );
00203     Iterator( const ConstIterator & it);
00204     ConstIterator & operator=( const ConstIterator & it );
00205 
00206     Iterator( VectorContainerIterator iter, InstanceIdentifier iid )
00207       :ConstIterator( iter, iid )
00208     {}
00209 
00210   private:
00211   };
00212 
00214   Iterator Begin()
00215   {
00216     VectorContainerPointer nonConstVectorDataContainer =
00217       const_cast< VectorContainerType * >( this->m_VectorContainer.GetPointer() );
00218     Iterator iter( nonConstVectorDataContainer->Begin(), 0 );
00220 
00221     return iter;
00222   }
00223 
00225   Iterator End()
00226   {
00227     VectorContainerPointer nonConstVectorDataContainer =
00228       const_cast<VectorContainerType *>( this->m_VectorContainer.GetPointer() );
00229 
00230     Iterator iter( nonConstVectorDataContainer->End(),
00231       this->m_VectorContainer->Size() );
00232 
00233     return iter;
00234   }
00235 
00237   ConstIterator Begin() const
00238   {
00239     ConstIterator iter( this->m_VectorContainer->Begin(), 0 );
00240 
00241     return iter;
00242   }
00243 
00245   ConstIterator End() const
00246   {
00247     ConstIterator iter( this->m_VectorContainer->End(),
00248       this->m_VectorContainer->Size() );
00249     return iter;
00250   }
00252 
00253 protected:
00254   VectorContainerToListSampleAdaptor();
00255 
00256   virtual ~VectorContainerToListSampleAdaptor() {}
00257   void PrintSelf( std::ostream & os, Indent indent ) const;
00258 
00259 private:
00260   VectorContainerToListSampleAdaptor( const Self &  ); //purposely not implemented
00261   void operator=( const Self & );              //purposely not implemented
00262 
00265   VectorContainerConstPointer  m_VectorContainer;
00266 
00268   mutable typename VectorContainerType::Element m_TempPoint;
00269 };  // end of class VectorContainerToListSampleAdaptor
00270 } // end of namespace Statistics
00271 } // end of namespace itk
00272 
00273 #ifndef ITK_MANUAL_INSTANTIATION
00274 #include "itkVectorContainerToListSampleAdaptor.hxx"
00275 #endif
00276 
00277 #endif
00278