00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __itkImageRegion_h
00021 #define __itkImageRegion_h
00022
00023 #include "itkRegion.h"
00024 #include "itkObjectFactory.h"
00025 #include "itkIndex.h"
00026 #include "itkSize.h"
00027 #include "itkContinuousIndex.h"
00028
00029 namespace itk
00030 {
00031
00032
00033 template <unsigned int VImageDimension> class ImageBase;
00034
00035
00053 template <unsigned int VImageDimension>
00054 class ITK_EXPORT ImageRegion: public Region
00055 {
00056 public:
00058 typedef ImageRegion Self;
00059 typedef Region Superclass;
00060
00062 itkTypeMacro(ImageRegion, Region);
00063
00065 itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension);
00066
00069 itkStaticConstMacro(SliceDimension, unsigned int,
00070 (VImageDimension - (VImageDimension > 1)));
00071
00073 static unsigned int GetImageDimension()
00074 { return VImageDimension; }
00075
00077 typedef Index<VImageDimension> IndexType;
00078 typedef typename IndexType::IndexValueType IndexValueType;
00079
00081 typedef Size<VImageDimension> SizeType;
00082 typedef typename SizeType::SizeValueType SizeValueType;
00083
00085 typedef ImageRegion<itkGetStaticConstMacro(SliceDimension)> SliceRegion;
00086
00088 virtual typename Superclass::RegionType GetRegionType() const
00089 {return Superclass::ITK_STRUCTURED_REGION;}
00090
00093 ImageRegion();
00094
00097 virtual ~ImageRegion();
00098
00101 ImageRegion(const Self& region): Region(region), m_Index( region.m_Index ), m_Size( region.m_Size ) {}
00102
00105 ImageRegion(const IndexType &index, const SizeType &size)
00106 { m_Index = index; m_Size = size; };
00107
00111 ImageRegion(const SizeType &size)
00112 { m_Size = size; m_Index.Fill(0); } ;
00113
00116 void operator=(const Self& region)
00117 { m_Index = region.m_Index; m_Size = region.m_Size; };
00118
00120 void SetIndex(const IndexType &index)
00121 { m_Index = index; };
00122
00124 const IndexType& GetIndex() const
00125 { return m_Index; };
00126
00129 void SetSize(const SizeType &size)
00130 { m_Size = size; };
00131
00133 const SizeType& GetSize() const
00134 { return m_Size; }
00135
00137 void SetSize(unsigned long i, SizeValueType sze)
00138 { m_Size[i] = sze; }
00139 SizeValueType GetSize(unsigned long i) const
00140 { return m_Size[i]; }
00141
00143 void SetIndex(unsigned long i, IndexValueType sze)
00144 { m_Index[i] = sze; }
00145 IndexValueType GetIndex(unsigned long i) const
00146 { return m_Index[i]; }
00147
00149 bool
00150 operator==(const Self ®ion) const
00151 {
00152 bool same = 1;
00153 same = (m_Index == region.m_Index);
00154 same = same && (m_Size == region.m_Size);
00155 return same;
00156 }
00157
00159 bool
00160 operator!=(const Self ®ion) const
00161 {
00162 bool same = 1;
00163 same = (m_Index == region.m_Index);
00164 same = same && (m_Size == region.m_Size);
00165 return !same;
00166 }
00167
00168
00170 bool
00171 IsInside(const IndexType &index) const
00172 {
00173 for(unsigned int i=0; i<ImageDimension; i++)
00174 {
00175 if( index[i] < m_Index[i] )
00176 {
00177 return false;
00178 }
00179 if( index[i] >= m_Index[i] + static_cast<long>(m_Size[i]) )
00180 {
00181 return false;
00182 }
00183 }
00184 return true;
00185 }
00186
00188 template <typename TCoordRepType>
00189 bool
00190 IsInside(const ContinuousIndex<TCoordRepType,VImageDimension> &index) const
00191 {
00192 for(unsigned int i=0; i<ImageDimension; i++)
00193 {
00194 if( index[i] < static_cast<TCoordRepType>( m_Index[i] ) )
00195 {
00196 return false;
00197 }
00198
00199 const TCoordRepType bound = static_cast<TCoordRepType>(
00200 m_Index[i] + static_cast<long>(m_Size[i]) - 1);
00201
00202 if( index[i] > bound )
00203 {
00204 return false;
00205 }
00206 }
00207 return true;
00208 }
00209
00210
00212 bool
00213 IsInside(const Self ®ion) const
00214 {
00215 IndexType beginCorner = region.GetIndex();
00216 if( ! this->IsInside( beginCorner ) )
00217 {
00218 return false;
00219 }
00220 IndexType endCorner;
00221 SizeType size = region.GetSize();
00222 for(unsigned int i=0; i<ImageDimension; i++)
00223 {
00224 endCorner[i] = beginCorner[i] + size[i] - 1;
00225 }
00226 if( ! this->IsInside( endCorner ) )
00227 {
00228 return false;
00229 }
00230 return true;
00231 }
00232
00235 unsigned long GetNumberOfPixels() const;
00236
00240 void PadByRadius(unsigned long radius);
00241 void PadByRadius(const unsigned long radius[VImageDimension]);
00242 void PadByRadius(const SizeType &radius);
00243
00248 bool Crop(const Self& region);
00249
00253 SliceRegion Slice(const unsigned long dim) const;
00254
00255 protected:
00260 virtual void PrintSelf(std::ostream& os, Indent indent) const;
00261
00262 private:
00263 IndexType m_Index;
00264 SizeType m_Size;
00265
00267 friend class ImageBase<VImageDimension>;
00268 };
00269
00270
00271 template<unsigned int VImageDimension>
00272 std::ostream & operator<<(std::ostream &os, const ImageRegion<VImageDimension> ®ion)
00273 {
00274 region.Print(os);
00275 return os;
00276 }
00277
00278 }
00279
00280 #ifndef ITK_MANUAL_INSTANTIATION
00281 #include "itkImageRegion.txx"
00282 #endif
00283
00284 #endif
00285