| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkImageRegion.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:39 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
Portions of this code are covered under the VTK copyright. |
| 13 |
|
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details. |
| 14 |
|
|
| 15 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 16 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 17 |
IND |
*****PURPOSE. See the above copyright notices for more information. |
| 18 |
|
|
| 19 |
|
=========================================================================*/ |
| 20 |
|
#ifndef __itkImageRegion_h |
| 21 |
|
#define __itkImageRegion_h |
| 22 |
|
|
| 23 |
|
#include "itkRegion.h" |
| 24 |
|
#include "itkObjectFactory.h" |
| 25 |
|
#include "itkIndex.h" |
| 26 |
|
#include "itkSize.h" |
| 27 |
|
#include "itkContinuousIndex.h" |
| 28 |
|
|
| 29 |
|
namespace itk |
| 30 |
|
{ |
| 31 |
|
// Forward declaration of ImageBase so it can be declared a friend |
| 32 |
|
// (needed for PrintSelf mechanism) |
| 33 |
|
template <unsigned int VImageDimension> class ImageBase; |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
/** \class ImageRegion |
| 37 |
|
* \brief An image region represents a structured region of data. |
| 38 |
|
* |
| 39 |
|
* ImageRegion is an class that represents some structured portion or |
| 40 |
|
* piece of an Image. The ImageRegion is represented with an index and |
| 41 |
|
* a size in each of the n-dimensions of the image. (The index is the |
| 42 |
|
* corner of the image, the size is the lengths of the image in each of |
| 43 |
|
* the topological directions.) |
| 44 |
|
* |
| 45 |
|
* \sa Region |
| 46 |
|
* \sa Index |
| 47 |
|
* \sa Size |
| 48 |
|
* \sa MeshRegion |
| 49 |
|
* |
| 50 |
|
* \ingroup ImageObjects |
| 51 |
|
*/ |
| 52 |
|
|
| 53 |
|
template <unsigned int VImageDimension> |
| 54 |
|
class ITK_EXPORT ImageRegion: public Region |
| 55 |
|
{ |
| 56 |
|
public: |
| 57 |
|
/** Standard class typedefs. */ |
| 58 |
|
typedef ImageRegion Self; |
| 59 |
TDA |
typedef Region Superclass; |
| 60 |
|
|
| 61 |
|
/** Standard part of all itk objects. */ |
| 62 |
|
itkTypeMacro(ImageRegion, Region); |
| 63 |
|
|
| 64 |
|
/** Dimension of the image available at compile time. */ |
| 65 |
|
itkStaticConstMacro(ImageDimension, unsigned int, VImageDimension); |
| 66 |
|
|
| 67 |
|
/** Dimension one lower than the image unless the image is one dimensional |
| 68 |
IND |
******in which case the SliceDimension is also one dimensional. */ |
| 69 |
|
itkStaticConstMacro(SliceDimension, unsigned int, |
| 70 |
|
(VImageDimension - (VImageDimension > 1))); |
| 71 |
|
|
| 72 |
|
/** Dimension of the image available at run time. */ |
| 73 |
|
static unsigned int GetImageDimension() |
| 74 |
|
{ return VImageDimension; } |
| 75 |
|
|
| 76 |
|
/** Index typedef support. An index is used to access pixel values. */ |
| 77 |
|
typedef Index<VImageDimension> IndexType; |
| 78 |
TDA |
typedef typename IndexType::IndexValueType IndexValueType; |
| 79 |
|
|
| 80 |
|
/** Size typedef support. A size is used to define region bounds. */ |
| 81 |
|
typedef Size<VImageDimension> SizeType; |
| 82 |
TDA |
typedef typename SizeType::SizeValueType SizeValueType; |
| 83 |
|
|
| 84 |
|
/** Slice region typedef. SliceRegion is one dimension less than Self. */ |
| 85 |
|
typedef ImageRegion<itkGetStaticConstMacro(SliceDimension)> SliceRegion; |
| 86 |
|
|
| 87 |
|
/** Return the region type. Images are described with structured regions. */ |
| 88 |
|
virtual typename Superclass::RegionType GetRegionType() const |
| 89 |
|
{return Superclass::ITK_STRUCTURED_REGION;} |
| 90 |
|
|
| 91 |
|
/** Constructor. ImageRegion is a lightweight object that is not reference |
| 92 |
|
* counted, so the constructor is public. */ |
| 93 |
|
ImageRegion(); |
| 94 |
|
|
| 95 |
|
/** Destructor. ImageRegion is a lightweight object that is not reference |
| 96 |
|
* counted, so the destructor is public. */ |
| 97 |
|
virtual ~ImageRegion(); |
| 98 |
|
|
| 99 |
|
/** Copy constructor. ImageRegion is a lightweight object that is not |
| 100 |
|
* reference counted, so the copy constructor is public. */ |
| 101 |
LEN |
ImageRegion(const Self& region): Region(region), m_Index( region.m_Index ), m_Size( region.m_Size ) {} |
| 102 |
|
|
| 103 |
|
/** Constructor that takes an index and size. ImageRegion is a lightweight |
| 104 |
|
* object that is not reference counted, so this constructor is public. */ |
| 105 |
|
ImageRegion(const IndexType &index, const SizeType &size) |
| 106 |
|
{ m_Index = index; m_Size = size; }; |
| 107 |
|
|
| 108 |
|
/** Constructor that takes a size and assumes an index of zeros. ImageRegion |
| 109 |
|
* is lightweight object that is not reference counted so this constructor |
| 110 |
|
* is public. */ |
| 111 |
|
ImageRegion(const SizeType &size) |
| 112 |
SEM |
{ m_Size = size; m_Index.Fill(0); } ; |
| 113 |
|
|
| 114 |
|
/** operator=. ImageRegion is a lightweight object that is not reference |
| 115 |
|
* counted, so operator= is public. */ |
| 116 |
|
void operator=(const Self& region) |
| 117 |
|
{ m_Index = region.m_Index; m_Size = region.m_Size; }; |
| 118 |
|
|
| 119 |
|
/** Set the index defining the corner of the region. */ |
| 120 |
|
void SetIndex(const IndexType &index) |
| 121 |
|
{ m_Index = index; }; |
| 122 |
|
|
| 123 |
|
/** Get index defining the corner of the region. */ |
| 124 |
|
const IndexType& GetIndex() const |
| 125 |
|
{ return m_Index; }; |
| 126 |
|
|
| 127 |
|
/** Set the size of the region. This plus the index determines the |
| 128 |
|
* rectangular shape, or extent, of the region. */ |
| 129 |
|
void SetSize(const SizeType &size) |
| 130 |
|
{ m_Size = size; }; |
| 131 |
|
|
| 132 |
|
/** Get the size of the region. */ |
| 133 |
|
const SizeType& GetSize() const |
| 134 |
|
{ return m_Size; } |
| 135 |
|
|
| 136 |
LEN |
/** Convenience methods to get and set the size of the particular dimension i. */ |
| 137 |
|
void SetSize(unsigned long i, SizeValueType sze) |
| 138 |
|
{ m_Size[i] = sze; } |
| 139 |
|
SizeValueType GetSize(unsigned long i) const |
| 140 |
|
{ return m_Size[i]; } |
| 141 |
|
|
| 142 |
LEN |
/** Convenience methods to get and set the index of the particular dimension i. */ |
| 143 |
|
void SetIndex(unsigned long i, IndexValueType sze) |
| 144 |
|
{ m_Index[i] = sze; } |
| 145 |
|
IndexValueType GetIndex(unsigned long i) const |
| 146 |
|
{ return m_Index[i]; } |
| 147 |
|
|
| 148 |
|
/** Compare two regions. */ |
| 149 |
|
bool |
| 150 |
|
operator==(const Self ®ion) const |
| 151 |
|
{ |
| 152 |
IND |
******bool same = 1; |
| 153 |
IND |
******same = (m_Index == region.m_Index); |
| 154 |
IND |
******same = same && (m_Size == region.m_Size); |
| 155 |
IND |
******return same; |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
/** Compare two regions. */ |
| 159 |
|
bool |
| 160 |
|
operator!=(const Self ®ion) const |
| 161 |
|
{ |
| 162 |
IND |
******bool same = 1; |
| 163 |
IND |
******same = (m_Index == region.m_Index); |
| 164 |
IND |
******same = same && (m_Size == region.m_Size); |
| 165 |
IND |
******return !same; |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
|
| 169 |
|
/** Test if an index is inside */ |
| 170 |
|
bool |
| 171 |
|
IsInside(const IndexType &index) const |
| 172 |
|
{ |
| 173 |
IND |
******for(unsigned int i=0; i<ImageDimension; i++) |
| 174 |
|
{ |
| 175 |
|
if( index[i] < m_Index[i] ) |
| 176 |
|
{ |
| 177 |
|
return false; |
| 178 |
|
} |
| 179 |
|
if( index[i] >= m_Index[i] + static_cast<long>(m_Size[i]) ) |
| 180 |
IND |
**********{ |
| 181 |
IND |
**********return false; |
| 182 |
IND |
**********} |
| 183 |
IND |
********} |
| 184 |
IND |
******return true; |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
/** Test if an index is inside */ |
| 188 |
|
template <typename TCoordRepType> |
| 189 |
|
bool |
| 190 |
|
IsInside(const ContinuousIndex<TCoordRepType,VImageDimension> &index) const |
| 191 |
|
{ |
| 192 |
IND |
******for(unsigned int i=0; i<ImageDimension; i++) |
| 193 |
|
{ |
| 194 |
|
if( index[i] < static_cast<TCoordRepType>( m_Index[i] ) ) |
| 195 |
IND |
**********{ |
| 196 |
IND |
**********return false; |
| 197 |
IND |
**********} |
| 198 |
IND |
********// bound is the last valid pixel location |
| 199 |
IND |
********const TCoordRepType bound = static_cast<TCoordRepType>( |
| 200 |
|
m_Index[i] + static_cast<long>(m_Size[i]) - 1); |
| 201 |
|
|
| 202 |
IND |
********if( index[i] > bound ) |
| 203 |
IND |
**********{ |
| 204 |
IND |
**********return false; |
| 205 |
IND |
**********} |
| 206 |
IND |
********} |
| 207 |
IND |
******return true; |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
|
| 211 |
|
/** Test if a region (the argument) is completly inside of this region */ |
| 212 |
|
bool |
| 213 |
|
IsInside(const Self ®ion) const |
| 214 |
|
{ |
| 215 |
IND |
******IndexType beginCorner = region.GetIndex(); |
| 216 |
IND |
******if( ! this->IsInside( beginCorner ) ) |
| 217 |
IND |
********{ |
| 218 |
IND |
********return false; |
| 219 |
IND |
********} |
| 220 |
IND |
******IndexType endCorner; |
| 221 |
IND |
******SizeType size = region.GetSize(); |
| 222 |
IND |
******for(unsigned int i=0; i<ImageDimension; i++) |
| 223 |
|
{ |
| 224 |
|
endCorner[i] = beginCorner[i] + size[i] - 1; |
| 225 |
|
} |
| 226 |
|
if( ! this->IsInside( endCorner ) ) |
| 227 |
IND |
********{ |
| 228 |
IND |
********return false; |
| 229 |
IND |
********} |
| 230 |
IND |
******return true; |
| 231 |
|
} |
| 232 |
|
|
| 233 |
|
/** Get the number of pixels contained in this region. This just |
| 234 |
|
* multiplies the size components. */ |
| 235 |
|
unsigned long GetNumberOfPixels() const; |
| 236 |
|
|
| 237 |
|
/** Pad an image region by the specified radius. Region can be padded |
| 238 |
|
* uniformly in all dimensions or can be padded by different amounts |
| 239 |
|
* in each dimension. */ |
| 240 |
|
void PadByRadius(unsigned long radius); |
| 241 |
|
void PadByRadius(const unsigned long radius[VImageDimension]); |
| 242 |
|
void PadByRadius(const SizeType &radius); |
| 243 |
|
|
| 244 |
|
/** Crop a region by another region. If this region is outside of the |
| 245 |
|
* crop, this method returns false and does not modify the |
| 246 |
|
* region. Otherwise, this method returns true and the region is |
| 247 |
|
* modified to reflect the crop. */ |
| 248 |
|
bool Crop(const Self& region); |
| 249 |
|
|
| 250 |
|
/** Slice a region, producing a region that is one dimension lower |
| 251 |
|
* than the current region. Parameter "dim" specifies which dimension |
| 252 |
|
* to remove. */ |
| 253 |
|
SliceRegion Slice(const unsigned long dim) const; |
| 254 |
|
|
| 255 |
|
protected: |
| 256 |
|
/** Methods invoked by Print() to print information about the object |
| 257 |
|
* including superclasses. Typically not called by the user (use Print() |
| 258 |
|
* instead) but used in the hierarchical print process to combine the |
| 259 |
|
* output of several classes. */ |
| 260 |
|
virtual void PrintSelf(std::ostream& os, Indent indent) const; |
| 261 |
|
|
| 262 |
|
private: |
| 263 |
|
IndexType m_Index; |
| 264 |
|
SizeType m_Size; |
| 265 |
|
|
| 266 |
|
/** Friends of ImageRegion */ |
| 267 |
|
friend class ImageBase<VImageDimension>; |
| 268 |
|
}; |
| 269 |
|
|
| 270 |
|
|
| 271 |
|
template<unsigned int VImageDimension> |
| 272 |
LEN |
std::ostream & operator<<(std::ostream &os, const ImageRegion<VImageDimension> ®ion) |
| 273 |
|
{ |
| 274 |
|
region.Print(os); |
| 275 |
|
return os; |
| 276 |
|
} |
| 277 |
|
|
| 278 |
|
} // end namespace itk |
| 279 |
|
|
| 280 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 281 |
|
#include "itkImageRegion.txx" |
| 282 |
|
#endif |
| 283 |
|
|
| 284 |
|
#endif |
| 285 |
|
|
| 286 |
EOF |
|