itkImageToListAdaptor.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkImageToListAdaptor_h
00018 #define __itkImageToListAdaptor_h
00019
00020 #include <typeinfo>
00021
00022 #include "itkImage.h"
00023 #include "itkPixelTraits.h"
00024 #include "itkListSampleBase.h"
00025 #include "itkSmartPointer.h"
00026 #include "itkImageRegionIterator.h"
00027 #include "itkFixedArray.h"
00028 #include "itkMacro.h"
00029
00030 namespace itk {
00031 namespace Statistics {
00032
00062 template < class TImage,
00063 class TMeasurementVector =
00064 ITK_TYPENAME TImage::PixelType >
00065 class ITK_EXPORT ImageToListAdaptor :
00066 public ListSampleBase< TMeasurementVector >
00067 {
00068 public:
00070 typedef ImageToListAdaptor Self;
00071 typedef ListSampleBase< TMeasurementVector > Superclass;
00072 typedef SmartPointer< Self > Pointer;
00073 typedef SmartPointer<const Self> ConstPointer;
00074
00076 itkTypeMacro(ImageToListAdaptor, ListSampleBase);
00077
00079 itkNewMacro(Self);
00080
00082 typedef TImage ImageType;
00083 typedef typename ImageType::Pointer ImagePointer;
00084 typedef typename ImageType::ConstPointer ImageConstPointer;
00085 typedef typename ImageType::IndexType IndexType;
00086 typedef typename ImageType::PixelType PixelType;
00087 typedef typename ImageType::PixelContainerConstPointer PixelContainerConstPointer;
00088 typedef typename ImageType::PixelContainer::ElementIdentifier
00089 InstanceIdentifier;
00090
00092 typedef ImageRegionIterator< ImageType > IteratorType;
00093 typedef PixelTraits< typename TImage::PixelType > PixelTraitsType;
00094
00097 typedef typename PixelTraitsType::ValueType MeasurementType;
00098 typedef typename Superclass::FrequencyType FrequencyType;
00099 typedef typename Superclass::TotalFrequencyType TotalFrequencyType;
00100 typedef typename Superclass::MeasurementVectorSizeType MeasurementVectorSizeType;
00101
00103 itkStaticConstMacro(MeasurementVectorSize, unsigned int,
00104 PixelTraitsType::Dimension);
00105
00106 virtual void SetMeasurementVectorSize( const MeasurementVectorSizeType s )
00107 {
00108
00109
00110
00111 if( s != MeasurementVectorSize )
00112 {
00113 itkExceptionMacro( << "Measurement vector size for the image adaptor obtained"
00114 << " from the pixel dimension is: " << MeasurementVectorSize << " but you "
00115 << "are setting it to " << s);
00116 }
00117 }
00118
00119 unsigned int GetMeasurementVectorSize() const
00120 {
00121 return MeasurementVectorSize;
00122 }
00123
00124
00125
00126 typedef TMeasurementVector MeasurementVectorType;
00127 typedef MeasurementVectorType ValueType;
00128
00130 void SetImage(const TImage* image);
00131
00133 const TImage* GetImage() const;
00134
00136 unsigned int Size() const;
00137
00138 inline virtual const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier &id) const;
00139
00140 inline FrequencyType GetFrequency(const InstanceIdentifier &id) const;
00141
00142 TotalFrequencyType GetTotalFrequency() const;
00143
00144
00145 class Iterator
00146 {
00147 public:
00148
00149 Iterator(){}
00150
00151 Iterator(InstanceIdentifier id, Pointer pContainer)
00152 :m_Id(id),m_Container(pContainer) {}
00153
00154 FrequencyType GetFrequency() const
00155 { return 1;}
00156
00157 const MeasurementVectorType & GetMeasurementVector() const
00158 { return m_Container->GetMeasurementVector(m_Id);}
00159
00160 InstanceIdentifier GetInstanceIdentifier() const
00161 { return m_Id;}
00162
00163 Iterator& operator++()
00164 { ++m_Id; return *this;}
00165
00166 Iterator& operator+(int n)
00167 { m_Id += n; return *this;}
00168
00169 Iterator& operator-(int n)
00170 { m_Id -= n; return *this;}
00171
00172 bool operator!=(const Iterator &it)
00173 {
00174 if (m_Id != it.m_Id)
00175 {return true;}
00176
00177 if (m_Container != it.m_Container)
00178 { return true;}
00179
00180 return false;
00181 }
00182
00183 bool operator==(const Iterator &it)
00184 { return !(*this != it);}
00185
00186 Iterator& operator = (const Iterator &iter)
00187 {
00188 m_Id = iter.m_Id;
00189 m_Container = iter.m_Container;
00190 return *this;
00191 }
00192
00193 Iterator(const Iterator &iter)
00194 {
00195 m_Id = iter.m_Id;
00196 m_Container = iter.m_Container;
00197 }
00198
00199 private:
00200 InstanceIdentifier m_Id;
00201 Pointer m_Container;
00202 };
00203
00204
00205 class ConstIterator
00206 {
00207 public:
00208
00209 ConstIterator(){}
00210
00211 ConstIterator(InstanceIdentifier id, ConstPointer pContainer)
00212 :m_Id(id),m_Container(pContainer) {}
00213
00214 FrequencyType GetFrequency() const
00215 { return 1;}
00216
00217 const MeasurementVectorType & GetMeasurementVector() const
00218 { return m_Container->GetMeasurementVector(m_Id);}
00219
00220 InstanceIdentifier GetInstanceIdentifier() const
00221 { return m_Id;}
00222
00223 ConstIterator& operator++()
00224 { ++m_Id; return *this;}
00225
00226 ConstIterator& operator+(int n)
00227 { m_Id += n; return *this;}
00228
00229 ConstIterator& operator-(int n)
00230 { m_Id -= n; return *this;}
00231
00232 bool operator!=(const ConstIterator &it)
00233 {
00234 if (m_Id != it.m_Id)
00235 {return true;}
00236
00237 if (m_Container != it.m_Container)
00238 { return true;}
00239
00240 return false;
00241 }
00242
00243 bool operator==(const ConstIterator &it)
00244 { return !(*this != it);}
00245
00246 ConstIterator& operator = (const ConstIterator &iter)
00247 {
00248 m_Id = iter.m_Id;
00249 m_Container = iter.m_Container;
00250 return *this;
00251 }
00252
00253 ConstIterator(const ConstIterator &iter)
00254 {
00255 m_Id = iter.m_Id;
00256 m_Container = iter.m_Container;
00257 }
00258
00259 private:
00260 InstanceIdentifier m_Id;
00261 ConstPointer m_Container;
00262 };
00263
00264 Iterator Begin()
00265 {
00266 Iterator iter(0, this);
00267 return iter;
00268 }
00269
00270 Iterator End()
00271 {
00272 Iterator iter(this->Size(), this);
00273 return iter;
00274 }
00275
00276 ConstIterator Begin() const
00277 {
00278 ConstIterator iter(0, this);
00279 return iter;
00280 }
00281
00282 ConstIterator End() const
00283 {
00284 ConstIterator iter(this->Size(), this);
00285 return iter;
00286 }
00287
00288 protected:
00289 ImageToListAdaptor();
00290 virtual ~ImageToListAdaptor() {}
00291 void PrintSelf(std::ostream& os, Indent indent) const;
00292
00293 itkGetConstReferenceMacro(PixelContainer,PixelContainerConstPointer);
00294 itkGetConstReferenceMacro(UseBuffer,bool);
00295 itkGetConstReferenceMacro(ImageBeginIndex,IndexType);
00296 itkGetConstReferenceMacro(ImageEndIndex,IndexType);
00297
00298
00299 private:
00300 ImageToListAdaptor(const Self&);
00301 void operator=(const Self&);
00302
00303 PixelContainerConstPointer m_PixelContainer;
00304 bool m_UseBuffer;
00305 IndexType m_ImageBeginIndex;
00306 IndexType m_ImageEndIndex;
00307
00308 ImageConstPointer m_Image;
00309 };
00310
00311 template < class TImage, class TMeasurementVector >
00312 inline const TMeasurementVector &
00313 ImageToListAdaptor< TImage, TMeasurementVector >
00314 ::GetMeasurementVector(const InstanceIdentifier &id) const
00315 {
00316 if ( m_UseBuffer )
00317 {
00318 return *( reinterpret_cast< const MeasurementVectorType* >(&(*m_PixelContainer)[id]) );
00319 }
00320 else
00321 {
00322 return *(reinterpret_cast< const MeasurementVectorType* >
00323 ( &(m_Image->GetPixel( m_Image->ComputeIndex( id ) ) ) ) );
00324 }
00325 }
00326
00328 template < class TImage, class TMeasurementVector >
00329 inline unsigned int
00330 ImageToListAdaptor< TImage, TMeasurementVector >
00331 ::Size() const
00332 {
00333 return m_PixelContainer->Size();
00334 }
00335
00336 template < class TImage, class TMeasurementVector >
00337 inline typename ImageToListAdaptor< TImage, TMeasurementVector >::FrequencyType
00338 ImageToListAdaptor< TImage, TMeasurementVector >
00339 ::GetFrequency(const InstanceIdentifier &) const
00340 {
00341 return NumericTraits< FrequencyType >::One;
00342 }
00343
00344 }
00345 }
00346
00347 #ifndef ITK_MANUAL_INSTANTIATION
00348 #include "itkImageToListAdaptor.txx"
00349 #endif
00350
00351 #endif
00352