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
00155 FrequencyType GetFrequency() const
00156 { return 1;}
00157
00158 const MeasurementVectorType & GetMeasurementVector() const
00159 { return m_Container->GetMeasurementVector(m_Id);}
00160
00161 InstanceIdentifier GetInstanceIdentifier() const
00162 { return m_Id;}
00163
00164 Iterator& operator++()
00165 { ++m_Id; return *this;}
00166
00167
00168
00169
00170 Iterator& operator+(int n)
00171 { m_Id += n; return *this;}
00172
00173 Iterator& operator-(int n)
00174 { m_Id -= n; return *this;}
00175
00176 bool operator!=(const Iterator &it)
00177 {
00178 if (m_Id != it.m_Id)
00179 {return true;}
00180
00181 if (m_Container != it.m_Container)
00182 { return true;}
00183
00184 return false;
00185 }
00186
00187 bool operator==(const Iterator &it)
00188 { return !(*this != it);}
00189
00190 Iterator& operator = (const Iterator &iter)
00191 {
00192 m_Id = iter.m_Id;
00193 m_Container = iter.m_Container;
00194 return *this;
00195 }
00196
00197 Iterator(const Iterator &iter)
00198 {
00199 m_Id = iter.m_Id;
00200 m_Container = iter.m_Container;
00201 }
00202
00203 private:
00204 InstanceIdentifier m_Id;
00205 Pointer m_Container;
00206 };
00207
00208
00209 class ConstIterator
00210 {
00211 public:
00212
00213 ConstIterator(){}
00214
00215 ConstIterator(InstanceIdentifier id, ConstPointer pContainer)
00216 :m_Id(id),m_Container(pContainer)
00217 {}
00218
00219 FrequencyType GetFrequency() const
00220 { return 1;}
00221
00222 const MeasurementVectorType & GetMeasurementVector() const
00223 { return m_Container->GetMeasurementVector(m_Id);}
00224
00225 InstanceIdentifier GetInstanceIdentifier() const
00226 { return m_Id;}
00227
00228 ConstIterator& operator++()
00229 { ++m_Id; return *this;}
00230
00231
00232
00233
00234 ConstIterator& operator+(int n)
00235 { m_Id += n; return *this;}
00236
00237 ConstIterator& operator-(int n)
00238 { m_Id -= n; return *this;}
00239
00240 bool operator!=(const ConstIterator &it)
00241 {
00242 if (m_Id != it.m_Id)
00243 {return true;}
00244
00245 if (m_Container != it.m_Container)
00246 { return true;}
00247
00248 return false;
00249 }
00250
00251 bool operator==(const ConstIterator &it)
00252 { return !(*this != it);}
00253
00254 ConstIterator& operator = (const ConstIterator &iter)
00255 {
00256 m_Id = iter.m_Id;
00257 m_Container = iter.m_Container;
00258 return *this;
00259 }
00260
00261 ConstIterator(const ConstIterator &iter)
00262 {
00263 m_Id = iter.m_Id;
00264 m_Container = iter.m_Container;
00265 }
00266
00267 private:
00268 InstanceIdentifier m_Id;
00269 ConstPointer m_Container;
00270 };
00271
00272
00273
00274 Iterator Begin()
00275 {
00276 Iterator iter(0, this);
00277 return iter;
00278 }
00279
00280 Iterator End()
00281 {
00282 Iterator iter(this->Size(), this);
00283 return iter;
00284 }
00285
00286 ConstIterator Begin() const
00287 {
00288 ConstIterator iter(0, this);
00289 return iter;
00290 }
00291
00292 ConstIterator End() const
00293 {
00294 ConstIterator iter(this->Size(), this);
00295 return iter;
00296 }
00297
00298 protected:
00299 ImageToListAdaptor();
00300 virtual ~ImageToListAdaptor() {}
00301 void PrintSelf(std::ostream& os, Indent indent) const;
00302
00303 itkGetConstReferenceMacro(PixelContainer,PixelContainerConstPointer);
00304 itkGetConstReferenceMacro(UseBuffer,bool);
00305 itkGetConstReferenceMacro(ImageBeginIndex,IndexType);
00306 itkGetConstReferenceMacro(ImageEndIndex,IndexType);
00307
00308
00309 private:
00310 ImageToListAdaptor(const Self&);
00311 void operator=(const Self&);
00312
00313 PixelContainerConstPointer m_PixelContainer;
00314 bool m_UseBuffer;
00315 IndexType m_ImageBeginIndex;
00316 IndexType m_ImageEndIndex;
00317
00318 ImageConstPointer m_Image;
00319 };
00320
00321 template < class TImage, class TMeasurementVector >
00322 inline const TMeasurementVector &
00323 ImageToListAdaptor< TImage, TMeasurementVector >
00324 ::GetMeasurementVector(const InstanceIdentifier &id) const
00325 {
00326 if ( m_UseBuffer )
00327 {
00328 return *( reinterpret_cast< const MeasurementVectorType* >(&(*m_PixelContainer)[id]) );
00329 }
00330 else
00331 {
00332 return *(reinterpret_cast< const MeasurementVectorType* >
00333 ( &(m_Image->GetPixel( m_Image->ComputeIndex( id ) ) ) ) );
00334 }
00335 }
00336
00338 template < class TImage, class TMeasurementVector >
00339 inline unsigned int
00340 ImageToListAdaptor< TImage, TMeasurementVector >
00341 ::Size() const
00342 {
00343 return m_PixelContainer->Size();
00344 }
00345
00346 template < class TImage, class TMeasurementVector >
00347 inline typename ImageToListAdaptor< TImage, TMeasurementVector >::FrequencyType
00348 ImageToListAdaptor< TImage, TMeasurementVector >
00349 ::GetFrequency(const InstanceIdentifier &) const
00350 {
00351 return NumericTraits< FrequencyType >::One;
00352 }
00353
00354
00355
00356 }
00357 }
00358
00359 #ifndef ITK_MANUAL_INSTANTIATION
00360 #include "itkImageToListAdaptor.txx"
00361 #endif
00362
00363 #endif
00364