ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkPointSetToListSampleAdaptor.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 __itkPointSetToListSampleAdaptor_h
00019 #define __itkPointSetToListSampleAdaptor_h
00020 
00021 #include <typeinfo>
00022 
00023 #include "itkPointSet.h"
00024 #include "itkListSample.h"
00025 #include "itkSmartPointer.h"
00026 
00027 namespace itk
00028 {
00029 namespace Statistics
00030 {
00045 template< class TPointSet >
00046 class ITK_EXPORT PointSetToListSampleAdaptor:
00047   public ListSample< typename TPointSet::PointType >
00048 {
00049 public:
00051   typedef PointSetToListSampleAdaptor                 Self;
00052   typedef ListSample< typename TPointSet::PointType > Superclass;
00053   typedef SmartPointer< Self >                        Pointer;
00054   typedef SmartPointer< const Self >                  ConstPointer;
00055 
00057   itkTypeMacro(PointSetToListSampleAdaptor, ListSample);
00058 
00060   itkNewMacro(Self);
00061 
00063   itkStaticConstMacro(MeasurementVectorSize, unsigned int,
00064                       TPointSet::PointDimension);
00065 
00067   typedef TPointSet                                        PointSetType;
00068   typedef typename TPointSet::Pointer                      PointSetPointer;
00069   typedef typename TPointSet::ConstPointer                 PointSetConstPointer;
00070   typedef typename TPointSet::PointsContainer              PointsContainer;
00071   typedef typename TPointSet::PointsContainerPointer       PointsContainerPointer;
00072   typedef typename TPointSet::PointsContainerConstPointer  PointsContainerConstPointer;
00073   typedef typename TPointSet::PointsContainerIterator      PointsContainerIteratorType;
00074   typedef typename TPointSet::PointsContainerConstIterator PointsContainerConstIteratorType;
00075   typedef typename TPointSet::PointType                    PointType;
00076 
00079   typedef typename Superclass::MeasurementType            MeasurementType;
00080   typedef typename Superclass::MeasurementVectorType      MeasurementVectorType;
00081   typedef typename Superclass::AbsoluteFrequencyType      AbsoluteFrequencyType;
00082   typedef typename Superclass::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
00083   typedef typename Superclass::MeasurementVectorSizeType  MeasurementVectorSizeType;
00084   typedef typename Superclass::InstanceIdentifier         InstanceIdentifier;
00085 
00086   typedef MeasurementVectorType ValueType;
00087 
00089   void SetPointSet(const TPointSet *pointSet);
00090 
00092   const TPointSet * GetPointSet();
00093 
00095   InstanceIdentifier Size() const;
00096 
00099   const MeasurementVectorType & GetMeasurementVector(InstanceIdentifier id) const;
00100 
00102   AbsoluteFrequencyType GetFrequency(InstanceIdentifier id) const;
00103 
00105   TotalAbsoluteFrequencyType GetTotalFrequency() const;
00106 
00110   class ConstIterator
00111   {
00112     friend class PointSetToListSampleAdaptor;
00113 public:
00114 
00115     ConstIterator(const PointSetToListSampleAdaptor *adaptor)
00116     {
00117       *this = adaptor->Begin();
00118     }
00119 
00120     ConstIterator(const ConstIterator & iter)
00121     {
00122       m_Iter = iter.m_Iter;
00123       m_InstanceIdentifier = iter.m_InstanceIdentifier;
00124     }
00125 
00126     ConstIterator & operator=(const ConstIterator & iter)
00127     {
00128       m_Iter = iter.m_Iter;
00129       m_InstanceIdentifier = iter.m_InstanceIdentifier;
00130       return *this;
00131     }
00132 
00133     AbsoluteFrequencyType GetFrequency() const
00134     {
00135       return 1;
00136     }
00137 
00138     const MeasurementVectorType & GetMeasurementVector() const
00139     {
00140       return ( const MeasurementVectorType & )m_Iter.Value();
00141     }
00142 
00143     InstanceIdentifier GetInstanceIdentifier() const
00144     {
00145       return m_InstanceIdentifier;
00146     }
00147 
00148     ConstIterator & operator++()
00149     {
00150       ++m_Iter;
00151       ++m_InstanceIdentifier;
00152       return *this;
00153     }
00154 
00155     bool operator!=(const ConstIterator & it)
00156     {
00157       return ( m_Iter != it.m_Iter );
00158     }
00159 
00160     bool operator==(const ConstIterator & it)
00161     {
00162       return ( m_Iter == it.m_Iter );
00163     }
00164 
00165 protected:
00166     // This method should only be available to the ListSample class
00167     ConstIterator(
00168       PointsContainerConstIteratorType iter,
00169       InstanceIdentifier iid)
00170     {
00171       m_Iter = iter;
00172       m_InstanceIdentifier = iid;
00173     }
00174 
00175     // This method is purposely not implemented
00176     ConstIterator();
00177 private:
00178     PointsContainerConstIteratorType m_Iter;
00179     InstanceIdentifier               m_InstanceIdentifier;
00180   };
00181 
00185   class Iterator:public ConstIterator
00186   {
00187     friend class PointSetToListSampleAdaptor;
00188 public:
00189 
00190     Iterator(Self *adaptor):ConstIterator(adaptor)
00191     {}
00192 
00193     Iterator(const Iterator & iter):ConstIterator(iter)
00194     {}
00195 
00196     Iterator & operator=(const Iterator & iter)
00197     {
00198       this->ConstIterator::operator=(iter);
00199       return *this;
00200     }
00201 
00202 protected:
00203     // To ensure const-correctness these method must not be in the public API.
00204     // The are purposly not implemented, since they should never be called.
00205     Iterator();
00206     Iterator(const Self *adaptor);
00207     Iterator(PointsContainerConstIteratorType iter, InstanceIdentifier iid);
00208     Iterator(const ConstIterator & it);
00209     ConstIterator & operator=(const ConstIterator & it);
00210 
00211     Iterator(
00212       PointsContainerIteratorType iter,
00213       InstanceIdentifier iid):ConstIterator(iter, iid)
00214     {}
00215 
00216 private:
00217   };
00218 
00220   Iterator Begin()
00221   {
00222     PointsContainerPointer nonConstPointsDataContainer =
00223       const_cast< PointsContainer * >( m_PointsContainer.GetPointer() );
00224     Iterator iter(nonConstPointsDataContainer->Begin(), 0);
00226 
00227     return iter;
00228   }
00229 
00231   Iterator End()
00232   {
00233     PointsContainerPointer nonConstPointsDataContainer =
00234       const_cast< PointsContainer * >( m_PointsContainer.GetPointer() );
00235 
00236     Iterator iter( nonConstPointsDataContainer->End(), m_PointsContainer->Size() );
00237 
00238     return iter;
00239   }
00240 
00242   ConstIterator Begin() const
00243   {
00244     ConstIterator iter(m_PointsContainer->Begin(), 0);
00245 
00246     return iter;
00247   }
00248 
00250   ConstIterator End() const
00251   {
00252     ConstIterator iter( m_PointsContainer->End(), m_PointsContainer->Size() );
00253 
00254     return iter;
00255   }
00256 
00257 protected:
00258   PointSetToListSampleAdaptor();
00259 
00260   virtual ~PointSetToListSampleAdaptor() {}
00261   void PrintSelf(std::ostream & os, Indent indent) const;
00262 
00263 private:
00264   PointSetToListSampleAdaptor(const Self &); //purposely not implemented
00265   void operator=(const Self &);              //purposely not implemented
00266 
00268   PointSetConstPointer m_PointSet;
00269 
00272   PointsContainerConstPointer m_PointsContainer;
00273 
00275   mutable PointType m_TempPoint;
00276 };  // end of class PointSetToListSampleAdaptor
00277 } // end of namespace Statistics
00278 } // end of namespace itk
00279 
00280 #ifndef ITK_MANUAL_INSTANTIATION
00281 #include "itkPointSetToListSampleAdaptor.hxx"
00282 #endif
00283 
00284 #endif
00285