00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkImageIORegion_h
00018 #define __itkImageIORegion_h
00019
00020 #include <algorithm>
00021 #include "itkRegion.h"
00022 #include "itkObjectFactory.h"
00023 #include "itkImageRegion.h"
00024
00025 namespace itk
00026 {
00027
00045 class ITK_EXPORT ImageIORegion: public Region
00046 {
00047 public:
00049 typedef ImageIORegion Self;
00050 typedef Region Superclass;
00051
00053 itkTypeMacro(ImageIORegion, Region);
00054
00056 unsigned int GetImageDimension() const
00057 {
00058 return m_ImageDimension;
00059 }
00060
00064 unsigned int GetRegionDimension() const
00065 {
00066 unsigned int dim=0;
00067 for (unsigned int i=0; i<m_ImageDimension; i++)
00068 {
00069 if ( m_Size[i] > 1 ) dim++;
00070 }
00071 return dim;
00072 }
00074
00076 typedef std::vector<long> IndexType;
00077
00079 typedef std::vector<long> SizeType;
00080
00082 virtual Superclass::RegionType GetRegionType() const
00083 {return Superclass::ITK_STRUCTURED_REGION;}
00084
00087 ImageIORegion(unsigned int dimension)
00088 {
00089 m_ImageDimension = dimension;
00090 m_Index.resize(m_ImageDimension);
00091 m_Size.resize(m_ImageDimension);
00092 std::fill(m_Index.begin(), m_Index.end(), 0);
00093 std::fill(m_Size.begin(), m_Size.end(), 0);
00094 }
00096
00099 ImageIORegion()
00100 {
00101 m_ImageDimension = 2;
00102 m_Index.resize(2);
00103 m_Size.resize(2);
00104 std::fill(m_Index.begin(), m_Index.end(), 0);
00105 std::fill(m_Size.begin(), m_Size.end(), 0);
00106 }
00108
00111 virtual ~ImageIORegion(){};
00112
00115 ImageIORegion(const Self& region): Region()
00116 {
00117 m_Index =region.m_Index;
00118 m_Size = region.m_Size;
00119 m_ImageDimension = region.m_ImageDimension;
00120 }
00121
00124 void operator=(const Self& region)
00125 {
00126 m_Index = region.m_Index;
00127 m_Size = region.m_Size;
00128 m_ImageDimension = region.m_ImageDimension;
00129 }
00130
00132 void SetIndex(const IndexType &index)
00133 {
00134 m_Index = index;\
00135 }
00136
00138 const IndexType& GetIndex() const
00139 {
00140 return m_Index;
00141 }
00142
00145 void SetSize(const SizeType &size)
00146 {
00147 m_Size = size;
00148 }
00149
00151 const SizeType& GetSize() const
00152 {
00153 return m_Size;
00154 }
00155
00159 long GetSize(unsigned long i) const
00160 {
00161 return m_Size[i];
00162 }
00163 long GetIndex(unsigned long i) const
00164 {
00165 return m_Index[i];
00166 }
00167 void SetSize(const unsigned long i, long size)
00168 {
00169 m_Size[i] = size;
00170 }
00171 void SetIndex(const unsigned long i, long idx)
00172 {
00173 m_Index[i] = idx;
00174 }
00176
00178 bool
00179 operator==(const Self ®ion) const
00180 {
00181 bool same = 1;
00182 same = (m_Index == region.m_Index);
00183 same = same && (m_Size == region.m_Size);
00184 same = same && (m_ImageDimension == region.m_ImageDimension);
00185 return same;
00186 }
00188
00190 bool
00191 operator!=(const Self ®ion) const
00192 {
00193 bool same = 1;
00194 same = (m_Index == region.m_Index);
00195 same = same && (m_Size == region.m_Size);
00196 same = same && (m_ImageDimension == region.m_ImageDimension);
00197 return !same;
00198 }
00200
00202 bool
00203 IsInside(const IndexType &index) const
00204 {
00205 for(unsigned int i=0; i<m_ImageDimension; i++)
00206 {
00207 if( index[i] < m_Index[i] )
00208 {
00209 return false;
00210 }
00211 if( index[i] >= m_Index[i] + m_Size[i] )
00212 {
00213 return false;
00214 }
00215 }
00216 return true;
00217 }
00219
00222
00223
00224 protected:
00229 virtual void PrintSelf(std::ostream& os, Indent indent) const;
00230
00231 private:
00232 unsigned int m_ImageDimension;
00233 std::vector<long> m_Index;
00234 std::vector<long> m_Size;
00235 };
00236
00237
00238
00239 extern std::ostream & operator<<(std::ostream &os, const ImageIORegion ®ion);
00240
00245 template< unsigned int VDimension >
00246 class ImageIORegionAdaptor
00247 {
00248 public:
00249 typedef ImageRegion<VDimension> ImageRegionType;
00250 typedef ImageIORegion ImageIORegionType;
00251
00252 typedef typename ImageRegionType::SizeType ImageSizeType;
00253 typedef typename ImageRegionType::IndexType ImageIndexType;
00254
00255 static void Convert( const ImageRegionType & inImageRegion, ImageIORegionType & outIORegion )
00256 {
00257
00258
00259
00260
00261
00262
00263
00264 const unsigned int ioDimension = outIORegion.GetImageDimension();
00265 const unsigned int imageDimension = VDimension;
00266
00267 unsigned int minDimension = ( ioDimension > imageDimension ) ? imageDimension : ioDimension;
00268
00269 ImageSizeType size = inImageRegion.GetSize();
00270 ImageIndexType index = inImageRegion.GetIndex();
00271
00272 for( unsigned int i = 0; i < minDimension; i++ )
00273 {
00274 outIORegion.SetSize( i, size[i] );
00275 outIORegion.SetIndex( i, index[i] );
00276 }
00277
00278
00279
00280
00281 for( unsigned int k = minDimension; k < ioDimension; k++ )
00282 {
00283 outIORegion.SetSize( k, 1 );
00284 outIORegion.SetIndex( k, 0 );
00285 }
00286 }
00287
00288 static void Convert( const ImageIORegionType & inIORegion, ImageRegionType & outImageRegion )
00289 {
00290 ImageSizeType size;
00291 ImageIndexType index;
00292
00293 size.Fill(1);
00294 index.Fill(0);
00295
00296
00297
00298
00299
00300
00301
00302
00303 const unsigned int ioDimension = inIORegion.GetImageDimension();
00304 const unsigned int imageDimension = VDimension;
00305
00306 unsigned int minDimension = ( ioDimension > imageDimension ) ? imageDimension : ioDimension;
00307
00308 for(unsigned int i=0; i<minDimension; i++)
00309 {
00310 size[i] = inIORegion.GetSize(i);
00311 index[i] = inIORegion.GetIndex(i);
00312 }
00313
00314 outImageRegion.SetSize( size );
00315 outImageRegion.SetIndex( index );
00316 }
00317 };
00318
00319 }
00320
00321 #endif
00322